IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 7, 2004, 4:48:01 PM (22 years ago)
Author:
desonia
Message:

fixed psBinaryOp/psUnaryOp to handle the out parameter in a manner like
other functions. Also expanded the types supported to all numeric types.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/dataManip/psMatrixVectorArithmetic.c

    r1648 r2021  
    1 
    21/** @file  psMatrixVectorArithmetic.c
    32 *
     
    3029 *  @author Ross Harman, MHPCC
    3130 *
    32  *  @version $Revision: 1.27 $ $Name: not supported by cvs2svn $
    33  *  @date $Date: 2004-08-27 23:33:31 $
     31 *  @version $Revision: 1.28 $ $Name: not supported by cvs2svn $
     32 *  @date $Date: 2004-10-08 02:48:01 $
    3433 *
    3534 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    3635 */
    3736
    38 /******************************************************************************/
    39 
    40 /*  INCLUDE FILES                                                             */
    41 
    42 /******************************************************************************/
     37/******************************************************************************
     38 *  INCLUDE FILES                                                             *
     39 ******************************************************************************/
    4340#include <string.h>
    4441#include <math.h>
     
    5047#include "psVector.h"
    5148#include "psScalar.h"
    52 
    53 /******************************************************************************/
    54 
    55 /*  DEFINE STATEMENTS                                                         */
    56 
    57 /******************************************************************************/
    58 
    59 // None
    60 
    61 /******************************************************************************/
    62 
    63 /*  TYPE DEFINITIONS                                                          */
    64 
    65 /******************************************************************************/
    66 
    67 // None
    68 
    69 /*****************************************************************************/
    70 
    71 /*  GLOBAL VARIABLES                                                         */
    72 
    73 /*****************************************************************************/
    74 
    75 // None
    76 
    77 /*****************************************************************************/
    78 
    79 /*  FILE STATIC VARIABLES                                                    */
    80 
    81 /*****************************************************************************/
    82 
    83 // None
    84 
    85 /*****************************************************************************/
    86 
    87 /*  FUNCTION IMPLEMENTATION - LOCAL                                          */
    88 
    89 /*****************************************************************************/
     49#include "psLogMsg.h"
     50
     51/*****************************************************************************
     52 *  FUNCTION IMPLEMENTATION - LOCAL                                          *
     53 *****************************************************************************/
    9054
    9155// Conversion for degrees to radians
     
    9559#define R2D 57.29577950924861   /* 180.0/PI */
    9660
     61/* IEEE Standards on floating-point makes this function pointless, really.
    9762// Division with NAN checking
    9863static complex double psNanDiv(complex double a, complex double b)
    9964{
    10065    complex double out = 0 + 0i;
    101 
     66 
    10267    out = a / b;
    10368    if (isnan(creal(out)) || isnan(cimag(out))) {
    10469        psError(__func__, ": Divide by zero");
    10570    }
    106 
     71 
    10772    return out;
    10873}
     74*/
    10975
    11076// Binary SCALAR_XXXX operations
     
    186152    if(n1 != n2) {                                                                                           \
    187153        psError(__func__, ": Inconsistent element count: %d vs %d", n1, n2);                                 \
    188         return OUT;                                                                                          \
     154        if (OUT != IN1 && OUT != IN2) {                                                                      \
     155            psFree(OUT);                                                                                     \
     156        }                                                                                                    \
     157        return NULL;                                                                                         \
    189158    }                                                                                                        \
    190159    o  = ((psVector* )OUT)->data.TYPE;                                                                       \
     
    215184        if(n1!=numRows2) {                                                                                   \
    216185            psError(__func__, ": Inconsistent element count: %d vs %d", n1, numRows2);                       \
    217             return OUT;                                                                                      \
     186            if (OUT != IN1 && OUT != IN2) {                                                                  \
     187                psFree(OUT);                                                                                 \
     188            }                                                                                                \
     189            return NULL;                                                                                     \
    218190        }                                                                                                    \
    219191        \
     
    229201        if(n1!=numCols2) {                                                                                   \
    230202            psError(__func__, ": Inconsistent element count: %d vs %d", n1, numCols2);                       \
    231             return OUT;                                                                                      \
     203            if (OUT != IN1 && OUT != IN2) {                                                                  \
     204                psFree(OUT);                                                                                 \
     205            }                                                                                                \
     206            return NULL;                                                                                     \
    232207        }                                                                                                    \
    233208        \
     
    284259        if(n2!=numRows1) {                                                                                   \
    285260            psError(__func__, ": Inconsistent element count: %d vs %d", n2, numRows1);                       \
    286             return OUT;                                                                                      \
     261            if (OUT != IN1 && OUT != IN2) {                                                                  \
     262                psFree(OUT);                                                                                 \
     263            }                                                                                                \
     264            return NULL;                                                                                     \
    287265        }                                                                                                    \
    288266        \
     
    298276        if(n2!=numCols1) {                                                                                   \
    299277            psError(__func__, ": Inconsistent element count: %d vs %d", n2, numCols1);                       \
    300             return OUT;                                                                                      \
     278            if (OUT != IN1) {                                                                                \
     279                psFree(OUT);                                                                                 \
     280            }                                                                                                \
     281            return NULL;                                                                                     \
    301282        }                                                                                                    \
    302283        \
     
    330311        psError(__func__, ": Inconsistent element count: numRows: %d vs %d numCols: %d vs %d", numRows1,     \
    331312                numRows2, numCols1, numCols2);                                                               \
    332         return OUT;                                                                                          \
     313        if (OUT != IN1 && OUT != IN2) {                                                                      \
     314            psFree(OUT);                                                                                     \
     315        }                                                                                                    \
     316        return NULL;                                                                                         \
    333317    }                                                                                                        \
    334318    for(j = 0; j < numRows1; j++) {                                                                          \
     
    345329#define BINARY_TYPE(DIM1,DIM2,OUT,IN1,OP,IN2)                                                                \
    346330switch (IN1->type) {                                                                                         \
     331case PS_TYPE_U8:                                                                                             \
     332    DIM1##_##DIM2(OUT,IN1,OP,IN2,U8);                                                                        \
     333    break;                                                                                                   \
     334case PS_TYPE_U16:                                                                                            \
     335    DIM1##_##DIM2(OUT,IN1,OP,IN2,U16);                                                                       \
     336    break;                                                                                                   \
     337case PS_TYPE_U32:                                                                                            \
     338    DIM1##_##DIM2(OUT,IN1,OP,IN2,U32);                                                                       \
     339    break;                                                                                                   \
     340case PS_TYPE_U64:                                                                                            \
     341    DIM1##_##DIM2(OUT,IN1,OP,IN2,U64);                                                                       \
     342    break;                                                                                                   \
     343case PS_TYPE_S8:                                                                                             \
     344    DIM1##_##DIM2(OUT,IN1,OP,IN2,S8);                                                                        \
     345    break;                                                                                                   \
     346case PS_TYPE_S16:                                                                                            \
     347    DIM1##_##DIM2(OUT,IN1,OP,IN2,S16);                                                                       \
     348    break;                                                                                                   \
    347349case PS_TYPE_S32:                                                                                            \
    348350    DIM1##_##DIM2(OUT,IN1,OP,IN2,S32);                                                                       \
    349351    break;                                                                                                   \
     352case PS_TYPE_S64:                                                                                            \
     353    DIM1##_##DIM2(OUT,IN1,OP,IN2,S64);                                                                       \
     354    break;                                                                                                   \
    350355case PS_TYPE_F32:                                                                                            \
    351356    DIM1##_##DIM2(OUT,IN1,OP,IN2,F32);                                                                       \
     
    357362    DIM1##_##DIM2(OUT,IN1,OP,IN2,C32);                                                                       \
    358363    break;                                                                                                   \
     364case PS_TYPE_C64:                                                                                            \
     365    DIM1##_##DIM2(OUT,IN1,OP,IN2,C64);                                                                       \
     366    break;                                                                                                   \
    359367default:                                                                                                     \
    360 case PS_TYPE_S8:                                                                                             \
    361 case PS_TYPE_U8:                                                                                             \
    362 case PS_TYPE_S16:                                                                                            \
    363 case PS_TYPE_U16:                                                                                            \
    364 case PS_TYPE_U32:                                                                                            \
    365 case PS_TYPE_S64:                                                                                            \
    366 case PS_TYPE_U64:                                                                                            \
    367 case PS_TYPE_C64:                                                                                            \
    368368    psError(__func__, ": Invalid PS_TYPE: %d", IN1->type);                                                   \
     369    if (OUT != IN1 && OUT != IN2) {                                                                          \
     370        psFree(OUT);                                                                                         \
     371    }                                                                                                        \
     372    return NULL;                                                                                             \
    369373}
    370374
     
    378382    BINARY_TYPE(DIM1,DIM2,OUT,IN1,*i1 * *i2,IN2);                                                            \
    379383} else if(!strncmp(OP, "/", 1)) {                                                                            \
    380     BINARY_TYPE(DIM1,DIM2,OUT,IN1,psNanDiv(*i1,*i2),IN2);                                                    \
     384    BINARY_TYPE(DIM1,DIM2,OUT,IN1,*i1 / *i2,IN2);                                                    \
    381385} else if(!strncmp(OP, "^", 1)) {                                                                            \
    382386    if(PS_IS_PSELEMTYPE_COMPLEX(IN1->type)) {                                                                \
     
    388392    if(PS_IS_PSELEMTYPE_COMPLEX(IN1->type)) {                                                                \
    389393        psError(__func__, ": Minimum operation not supported for complex numbers");                          \
     394        if (OUT != IN1 && OUT != IN2) {                                                                      \
     395            psFree(OUT);                                                                                     \
     396        }                                                                                                    \
     397        return NULL;                                                                                         \
    390398    } else {                                                                                                 \
    391399        BINARY_TYPE(DIM1,DIM2,OUT,IN1,fmin(*i1,*i2),IN2);                                                    \
     
    394402    if(PS_IS_PSELEMTYPE_COMPLEX(IN1->type)) {                                                                \
    395403        psError(__func__, ": Maximum operation not supported for complex numbers");                          \
     404        if (OUT != IN1 && OUT != IN2) {                                                                      \
     405            psFree(OUT);                                                                                     \
     406        }                                                                                                    \
     407        return NULL;                                                                                         \
    396408    } else {                                                                                                 \
    397409        BINARY_TYPE(DIM1,DIM2,OUT,IN1,fmax(*i1,*i2),IN2);                                                    \
     
    399411} else {                                                                                                     \
    400412    psError(__func__, ": Invalid operation: %s", OP);                                                        \
     413    if (OUT != IN1 && OUT != IN2) {                                                                          \
     414        psFree(OUT);                                                                                         \
     415    }                                                                                                        \
     416    return NULL;                                                                                             \
    401417}
    402418
     
    405421    psDimen dim1 = 0;
    406422    psDimen dim2 = 0;
    407     psDimen dimOut = 0;
    408423    psElemType elType1 = 0;
    409424    psElemType elType2 = 0;
    410     psElemType elTypeOut = 0;
    411425    psType* psType1 = NULL;
    412426    psType* psType2 = NULL;
    413     psType* psTypeOut = NULL;
    414 
    415     psTypeOut = (psType* ) out;
    416     if (psTypeOut == NULL) {
    417         psError(__func__, ": Line %d - Null out argument", __LINE__);
    418         return out;
    419     }
    420427
    421428    psType1 = (psType* ) in1;
    422429    if (psType1 == NULL) {
    423430        psError(__func__, ": Line %d - Null in1 argument", __LINE__);
    424         return out;
     431        if (out != in1 && out != in2) {
     432            psFree(out);
     433        }
     434        return NULL;
    425435    }
    426436
     
    428438    if (psType2 == NULL) {
    429439        psError(__func__, ": Line %d - Null in2 argument", __LINE__);
    430         return out;
     440        if (out != in1 && out != in2) {
     441            psFree(out);
     442        }
     443        return NULL;
    431444    }
    432445
    433446    if (op == NULL) {
    434447        psError(__func__, ": Line %d - Null op argument", __LINE__);
    435         return out;
     448        if (out != in1 && out != in2) {
     449            psFree(out);
     450        }
     451        return NULL;
    436452    }
    437453
    438454    dim1 = psType1->dimen;
    439455    dim2 = psType2->dimen;
    440     dimOut = psTypeOut->dimen;
    441456    elType1 = psType1->type;
    442457    elType2 = psType2->type;
    443     elTypeOut = psTypeOut->type;
    444 
    445     if (elType1 != elType2 && elType1 != elTypeOut) {
    446         psError(__func__, ": Line %d - Element types for arguments inconsistent: (%d, %d, %d)", __LINE__,
    447                 elType1, elType2, elTypeOut);
    448         return out;
    449     }
    450 
    451     if (dim1 == PS_DIMEN_OTHER || dim2 == PS_DIMEN_OTHER || dimOut == PS_DIMEN_OTHER) {
    452         psError(__func__, ": Line %d - PS_DIMEN_OTHER not allowed for arguments: (%d, %d, %d)", __LINE__,
    453                 dim1, dim2, dimOut);
    454         return out;
    455     }
    456 
    457     if (dim1 == PS_DIMEN_VECTOR) {
     458
     459    if (elType1 != elType2) {
     460        psError(__func__, ": Line %d - Element types for arguments inconsistent: (%d, %d)", __LINE__,
     461                elType1, elType2);
     462        if (out != in1 && out != in2) {
     463            psFree(out);
     464        }
     465        return NULL;
     466    }
     467
     468    if (dim1 == PS_DIMEN_OTHER || dim2 == PS_DIMEN_OTHER) {
     469        psError(__func__, ": Line %d - PS_DIMEN_OTHER not allowed for arguments: (%d, %d)", __LINE__,
     470                dim1, dim2);
     471        if (out != in1 && out != in2) {
     472            psFree(out);
     473        }
     474        return NULL;
     475    }
     476
     477    if (dim1 == PS_DIMEN_VECTOR || dim1 == PS_DIMEN_TRANSV) {
    458478        if (((psVector* ) in1)->n == 0) {
    459             psError(__func__, ": Line %d - Vector contains zero elements");
     479            psLogMsg(__func__, PS_LOG_WARN, ": Line %d - Vector contains zero elements");
    460480        }
    461481    } else if (dim1 == PS_DIMEN_IMAGE) {
    462482        if (((psImage* ) in1)->numCols == 0 || ((psImage* ) in1)->numRows == 0) {
    463             psError(__func__, ": Line %d - Image contains zero length row or cols");
    464         }
    465     }
    466 
    467     if (dim2 == PS_DIMEN_VECTOR) {
     483            psLogMsg(__func__, PS_LOG_WARN, ": Line %d - Image contains zero length row or cols");
     484        }
     485    }
     486
     487    if (dim2 == PS_DIMEN_VECTOR || dim2 == PS_DIMEN_TRANSV) {
    468488        if (((psVector* ) in2)->n == 0) {
    469             psError(__func__, ": Line %d - Vector contains zero elements");
     489            psLogMsg(__func__, PS_LOG_WARN, ": Line %d - Vector contains zero elements");
    470490        }
    471491    } else if (dim2 == PS_DIMEN_IMAGE) {
    472492        if (((psImage* ) in2)->numCols == 0 || ((psImage* ) in2)->numRows == 0) {
    473             psError(__func__, ": Line %d - Image contains zero length row or cols");
     493            psLogMsg(__func__, PS_LOG_WARN, ": Line %d - Image contains zero length row or cols");
    474494        }
    475495    }
    476496
    477497    if (dim1 == PS_DIMEN_SCALAR) {
     498        if ( out != NULL && ((psType*)out)->dimen != dim2) {
     499            if (out != in1 && out != in2) {
     500                psFree(out);
     501            }
     502            out = NULL;
     503        }
    478504        if (dim2 == PS_DIMEN_SCALAR) {
     505            if (out == NULL || ((psScalar*)out)->type.type != elType1) {
     506                if (out != in1 && out != in2) {
     507                    psFree(out);
     508                }
     509                out = psScalarAlloc(0.0,elType1);
     510            }
    479511            BINARY_OP(SCALAR, SCALAR, out, psType1, op, psType2);       // scalar op scalar
    480512        } else if (dim2 == PS_DIMEN_VECTOR || dim2 == PS_DIMEN_TRANSV) {
     513            out = psVectorRecycle(out,((psVector*)in2)->n,elType1);
     514            if (out == NULL) {
     515                psError(__func__, "Couldn't create a proper output psVector.");
     516                return NULL;
     517            }
    481518            BINARY_OP(SCALAR, VECTOR, out, psType1, op, psType2);       // scalar op vector
    482519        } else if (dim2 == PS_DIMEN_IMAGE) {
     520            out = psImageRecycle(out, ((psImage* ) in2)->numCols, ((psImage* ) in2)->numRows,elType1);
     521            if (out == NULL) {
     522                psError(__func__, "Couldn't create a proper output psImage.");
     523                return NULL;
     524            }
    483525            BINARY_OP(SCALAR, IMAGE, out, psType1, op, psType2);        // scalar op image
    484526        } else {
     
    487529    } else if (dim1 == PS_DIMEN_VECTOR || dim1 == PS_DIMEN_TRANSV) {
    488530        if (dim2 == PS_DIMEN_SCALAR) {
     531            out = psVectorRecycle(out,((psVector*)in1)->n,elType1);
     532            if (out == NULL) {
     533                psError(__func__, "Couldn't create a proper output psVector.");
     534                return NULL;
     535            }
    489536            BINARY_OP(VECTOR, SCALAR, out, psType1, op, psType2);       // vector op scalar
    490537        } else if (dim2 == PS_DIMEN_VECTOR || dim2 == PS_DIMEN_TRANSV) {
     538            out = psVectorRecycle(out,((psVector*)in2)->n,elType2);
     539            if (out == NULL) {
     540                psError(__func__, "Couldn't create a proper output psVector.");
     541                return NULL;
     542            }
    491543            BINARY_OP(VECTOR, VECTOR, out, psType1, op, psType2);       // vector op vector
    492544        } else if (dim2 == PS_DIMEN_IMAGE) {
     545            out = psImageRecycle(out, ((psImage* ) in2)->numCols, ((psImage* ) in2)->numRows, elType2);
     546            if (out == NULL) {
     547                psError(__func__, "Couldn't create a proper output psImage.");
     548                return NULL;
     549            }
    493550            BINARY_OP(VECTOR, IMAGE, out, psType1, op, psType2);        // vector op image
    494551        } else {
     
    496553        }
    497554    } else if (dim1 == PS_DIMEN_IMAGE) {
     555        out = psImageRecycle(out, ((psImage*)in1)->numCols, ((psImage*)in1)->numRows, elType1);
     556        if (out == NULL) {
     557            psError(__func__, "Couldn't create a proper output psImage.");
     558            return NULL;
     559        }
    498560        if (dim2 == PS_DIMEN_SCALAR) {
    499561            BINARY_OP(IMAGE, SCALAR, out, psType1, op, psType2);        // image op scalar
     
    503565            BINARY_OP(IMAGE, IMAGE, out, psType1, op, psType2); // image op image
    504566        } else {
     567            if (out != in1 && out != in2) {
     568                psFree(out);
     569            }
    505570            psError(__func__, ": Line %d - Invalid dimensionality for in2 arg: %d", __LINE__, dim2);
     571            return NULL;
    506572        }
    507573    } else {
     574        if (out != in1 && out != in2) {
     575            psFree(out);
     576        }
    508577        psError(__func__, ": Line %d - Invalid dimensionality for in1 arg: %d", __LINE__, dim1);
     578        return NULL;
    509579    }
    510580
     
    544614    if(nIn != nOut) {                                                                                        \
    545615        psError(__func__, ": Inconsistent element count: %d vs %d", nIn, nOut);                              \
    546         return OUT;                                                                                          \
     616        if (OUT != IN) {                                                                                     \
     617            psFree(OUT);                                                                                     \
     618        }                                                                                                    \
     619        return NULL;                                                                                         \
    547620    }                                                                                                        \
    548621    o  = ((psVector* )OUT)->data.TYPE;                                                                       \
     
    571644        psError(__func__, ": Inconsistent element count: numRows: %d vs %d numCols: %d vs %d", numRowsIn,    \
    572645                numRowsOut, numColsIn, numColsOut);                                                          \
    573         return OUT;                                                                                          \
     646        if (OUT != IN) {                                                                                     \
     647            psFree(OUT);                                                                                     \
     648        }                                                                                                    \
     649        return NULL;                                                                                         \
    574650    }                                                                                                        \
    575651    for(j = 0; j < numRowsIn; j++) {                                                                         \
     
    597673    DIM(OUT,IN,OP,C32);                                                                                      \
    598674    break;                                                                                                   \
     675case PS_TYPE_S8:                                                                                             \
     676    DIM(OUT,IN,OP,C32);                                                                                      \
     677    break;                                                                                                   \
     678case PS_TYPE_U8:                                                                                             \
     679    DIM(OUT,IN,OP,C32);                                                                                      \
     680    break;                                                                                                   \
     681case PS_TYPE_S16:                                                                                            \
     682    DIM(OUT,IN,OP,C32);                                                                                      \
     683    break;                                                                                                   \
     684case PS_TYPE_U16:                                                                                            \
     685    DIM(OUT,IN,OP,C32);                                                                                      \
     686    break;                                                                                                   \
     687case PS_TYPE_U32:                                                                                            \
     688    DIM(OUT,IN,OP,C32);                                                                                      \
     689    break;                                                                                                   \
     690case PS_TYPE_S64:                                                                                            \
     691    DIM(OUT,IN,OP,C32);                                                                                      \
     692    break;                                                                                                   \
     693case PS_TYPE_U64:                                                                                            \
     694    DIM(OUT,IN,OP,C32);                                                                                      \
     695    break;                                                                                                   \
     696case PS_TYPE_C64:                                                                                            \
     697    DIM(OUT,IN,OP,C32);                                                                                      \
     698    break;                                                                                                   \
    599699default:                                                                                                     \
    600 case PS_TYPE_S8:                                                                                             \
    601 case PS_TYPE_U8:                                                                                             \
    602 case PS_TYPE_S16:                                                                                            \
    603 case PS_TYPE_U16:                                                                                            \
    604 case PS_TYPE_U32:                                                                                            \
    605 case PS_TYPE_S64:                                                                                            \
    606 case PS_TYPE_U64:                                                                                            \
    607 case PS_TYPE_C64:                                                                                            \
    608700    psError(__func__, ": Invalid PS_TYPE: %d", IN->type);                                                    \
     701    if (OUT != IN) {                                                                                         \
     702        psFree(OUT);                                                                                         \
     703    }                                                                                                        \
     704    return NULL;                                                                                             \
    609705}
    610706
     
    725821{
    726822    psDimen dimIn = 0;
    727     psDimen dimOut = 0;
    728823    psElemType elTypeIn = 0;
    729     psElemType elTypeOut = 0;
    730824    psType* psTypeIn = NULL;
    731     psType* psTypeOut = NULL;
    732 
    733     psTypeOut = (psType* ) out;
    734     if (psTypeOut == NULL) {
    735         psError(__func__, ": Line %d - Null out argument", __LINE__);
    736         return out;
    737     }
    738825
    739826    psTypeIn = (psType* ) in;
    740827    if (psTypeIn == NULL) {
    741828        psError(__func__, ": Line %d - Null in argument", __LINE__);
    742         return out;
     829        if (out != in) {
     830            psFree(out);
     831        }
     832        return NULL;
    743833    }
    744834
    745835    if (op == NULL) {
    746836        psError(__func__, ": Line %d - Null op argument", __LINE__);
    747         return out;
     837        if (out != in) {
     838            psFree(out);
     839        }
     840        return NULL;
    748841    }
    749842
    750843    dimIn = psTypeIn->dimen;
    751     dimOut = psTypeOut->dimen;
    752844    elTypeIn = psTypeIn->type;
    753     elTypeOut = psTypeOut->type;
    754 
    755     if (elTypeIn != elTypeOut) {
    756         psError(__func__, ": Line %d - Element types for arguments inconsistent: (%d, %d)", __LINE__,
    757                 elTypeIn, elTypeOut);
    758         return out;
    759     }
    760 
    761     if (dimIn != dimOut) {
    762         psError(__func__, ": Line %d - Dimensionality for arguments inconsistent: (%d, %d)", __LINE__,
    763                 dimIn, dimOut);
    764         return out;
    765     }
    766 
    767     if (dimIn == PS_DIMEN_OTHER || dimOut == PS_DIMEN_OTHER) {
    768         psError(__func__, ": Line %d - PS_DIMEN_OTHER not allowed for arguments: (%d, %d)", __LINE__,
    769                 dimIn, dimOut);
    770         return out;
    771     }
    772 
    773     if (dimIn == PS_DIMEN_VECTOR) {
    774         if (((psVector* ) in)->n == 0) {
     845
     846    switch (dimIn) {
     847    case PS_DIMEN_SCALAR:
     848        if (out == NULL ||
     849                ((psType*)out)->dimen != PS_DIMEN_SCALAR ||
     850                ((psScalar*)out)->type.type != elTypeIn) {
     851            psFree(out);
     852            out = psScalarAlloc(0.0,elTypeIn);
     853        }
     854        UNARY_OP(SCALAR, out, psTypeIn, op);    // scalar
     855        break;
     856    case PS_DIMEN_VECTOR:
     857    case PS_DIMEN_TRANSV:
     858        if (((psVector*)in)->n == 0) {
     859            if (out != in) {
     860                psFree(out);
     861            }
    775862            psError(__func__, ": Line %d - Vector contains zero elements");
    776         }
    777     } else if (dimIn == PS_DIMEN_IMAGE) {
     863            return NULL;
     864        }
     865
     866        out = psVectorRecycle(out,
     867                              ((psVector*)in)->n,
     868                              elTypeIn);
     869        if (out == NULL) {
     870            psError(__func__, "Couldn't create a proper output psVector.");
     871            return NULL;
     872        }
     873
     874        UNARY_OP(VECTOR, out, psTypeIn, op);    // vector
     875        break;
     876    case PS_DIMEN_IMAGE:
    778877        if (((psImage* ) in)->numCols == 0 || ((psImage* ) in)->numRows == 0) {
     878            if (out != in) {
     879                psFree(out);
     880            }
    779881            psError(__func__, ": Line %d - Image contains zero length row or cols");
    780         }
    781     }
    782 
    783     if (dimOut == PS_DIMEN_VECTOR) {
    784         if (((psVector* ) out)->n == 0) {
    785             psError(__func__, ": Line %d - Vector contains zero elements");
    786         }
    787     } else if (dimOut == PS_DIMEN_IMAGE) {
    788         if (((psImage* ) out)->numCols == 0 || ((psImage* ) out)->numRows == 0) {
    789             psError(__func__, ": Line %d - Image contains zero length row or cols");
    790         }
    791     }
    792 
    793     if (dimIn == PS_DIMEN_SCALAR) {
    794         UNARY_OP(SCALAR, out, psTypeIn, op);    // scalar
    795     } else if (dimIn == PS_DIMEN_VECTOR || dimIn == PS_DIMEN_TRANSV) {
    796         UNARY_OP(VECTOR, out, psTypeIn, op);    // vector
    797     } else if (dimIn == PS_DIMEN_IMAGE) {
     882            return NULL;
     883        }
     884
     885        out = psImageRecycle(out,
     886                             ((psImage*)in)->numCols,
     887                             ((psImage*)in)->numRows,
     888                             elTypeIn);
     889        if (out == NULL) {
     890            psError(__func__, "Couldn't create a proper output psImage.");
     891            return NULL;
     892        }
     893
    798894        UNARY_OP(IMAGE, out, psTypeIn, op);     // image
    799     } else {
     895        break;
     896    default:
     897        if (out != in) {
     898            psFree(out);
     899        }
    800900        psError(__func__, ": Line %d - Invalid dimensionality for in arg: %d", __LINE__, dimIn);
     901        return NULL;
    801902    }
    802903
Note: See TracChangeset for help on using the changeset viewer.