Index: trunk/psLib/src/dataManip/psMatrixVectorArithmetic.c
===================================================================
--- trunk/psLib/src/dataManip/psMatrixVectorArithmetic.c	(revision 2204)
+++ trunk/psLib/src/dataManip/psMatrixVectorArithmetic.c	(revision 2273)
@@ -29,6 +29,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.29 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-10-27 00:57:31 $
+ *  @version $Revision: 1.30 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-11-04 01:04:59 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -48,4 +48,6 @@
 #include "psScalar.h"
 #include "psLogMsg.h"
+#include "psConstants.h"
+#include "psDataManipErrors.h"
 
 /*****************************************************************************
@@ -58,19 +60,4 @@
 // Conversion for radians to degrees
 #define R2D 57.29577950924861   /* 180.0/PI */
-
-/* IEEE Standards on floating-point makes this function pointless, really.
-// Division with NAN checking
-static complex double psNanDiv(complex double a, complex double b)
-{
-    complex double out = 0 + 0i;
- 
-    out = a / b;
-    if (isnan(creal(out)) || isnan(cimag(out))) {
-        psError(__func__, ": Divide by zero");
-    }
- 
-    return out;
-}
-*/
 
 // Binary SCALAR_XXXX operations
@@ -142,7 +129,7 @@
 #define VECTOR_VECTOR(OUT,IN1,OP,IN2,TYPE)                                                                   \
 {                                                                                                            \
-    psS32 i = 0;                                                                                               \
-    psS32 n1 = 0;                                                                                              \
-    psS32 n2 = 0;                                                                                              \
+    psS32 i = 0;                                                                                             \
+    psS32 n1 = 0;                                                                                            \
+    psS32 n2 = 0;                                                                                            \
     ps##TYPE *o = NULL;                                                                                      \
     ps##TYPE *i1 = NULL;                                                                                     \
@@ -151,5 +138,7 @@
     n2  = ((psVector* )IN2)->n;                                                                              \
     if(n1 != n2) {                                                                                           \
-        psError(__func__, ": Inconsistent element count: %d vs %d", n1, n2);                                 \
+        psError(PS_ERR_BAD_PARAMETER_SIZE, true,                                                             \
+                PS_ERRORTEXT_psMatrix_COUNT_DIFFERS,                                                         \
+                n1, n2);                                                                                     \
         if (OUT != IN1 && OUT != IN2) {                                                                      \
             psFree(OUT);                                                                                     \
@@ -167,9 +156,9 @@
 #define VECTOR_IMAGE(OUT,IN1,OP,IN2,TYPE)                                                                    \
 {                                                                                                            \
-    psS32 i = 0;                                                                                               \
-    psS32 j = 0;                                                                                               \
-    psS32 n1 = 0;                                                                                              \
-    psS32 numRows2 = 0;                                                                                        \
-    psS32 numCols2 = 0;                                                                                        \
+    psS32 i = 0;                                                                                             \
+    psS32 j = 0;                                                                                             \
+    psS32 n1 = 0;                                                                                            \
+    psS32 numRows2 = 0;                                                                                      \
+    psS32 numCols2 = 0;                                                                                      \
     psDimen dim1 = 0;                                                                                        \
     ps##TYPE *o = NULL;                                                                                      \
@@ -181,7 +170,9 @@
     numCols2 = ((psImage* )IN2)->numCols;                                                                    \
     \
-    if(dim1 == PS_DIMEN_VECTOR) { /* Regular vectors */                                                      \
+    if(dim1 == PS_DIMEN_VECTOR) { /* Regular vectors */                                             \
         if(n1!=numRows2) {                                                                                   \
-            psError(__func__, ": Inconsistent element count: %d vs %d", n1, numRows2);                       \
+            psError(PS_ERR_BAD_PARAMETER_SIZE, true,                                                         \
+                    PS_ERRORTEXT_psMatrix_COUNT_DIFFERS,                                                           \
+                    n1, numRows2);                                                                           \
             if (OUT != IN1 && OUT != IN2) {                                                                  \
                 psFree(OUT);                                                                                 \
@@ -200,5 +191,7 @@
     } else {  /* Transposed vectors */                                                                       \
         if(n1!=numCols2) {                                                                                   \
-            psError(__func__, ": Inconsistent element count: %d vs %d", n1, numCols2);                       \
+            psError(PS_ERR_BAD_PARAMETER_SIZE, true,                                                 \
+                    PS_ERRORTEXT_psMatrix_COUNT_DIFFERS,                                                     \
+                    n1, numCols2);                       \
             if (OUT != IN1 && OUT != IN2) {                                                                  \
                 psFree(OUT);                                                                                 \
@@ -256,7 +249,9 @@
     numCols1 = ((psImage* )IN1)->numCols;                                                                    \
     \
-    if(dim2 == PS_DIMEN_VECTOR) { /* Regular vectors */                                                      \
+    if(dim2 == PS_DIMEN_VECTOR) { /* Regular vectors */                                             \
         if(n2!=numRows1) {                                                                                   \
-            psError(__func__, ": Inconsistent element count: %d vs %d", n2, numRows1);                       \
+            psError(PS_ERR_BAD_PARAMETER_SIZE, true,                                                         \
+                    PS_ERRORTEXT_psMatrix_COUNT_DIFFERS,                                                     \
+                    n2, numRows1);                                                                           \
             if (OUT != IN1 && OUT != IN2) {                                                                  \
                 psFree(OUT);                                                                                 \
@@ -275,5 +270,7 @@
     } else {  /* Transposed vectors */                                                                       \
         if(n2!=numCols1) {                                                                                   \
-            psError(__func__, ": Inconsistent element count: %d vs %d", n2, numCols1);                       \
+            psError(PS_ERR_BAD_PARAMETER_SIZE, true,                                                         \
+                    PS_ERRORTEXT_psMatrix_COUNT_DIFFERS,                                                     \
+                    n2, numCols1);                                                                           \
             if (OUT != IN1) {                                                                                \
                 psFree(OUT);                                                                                 \
@@ -309,6 +306,7 @@
     numCols2 = ((psImage* )IN2)->numCols;                                                                    \
     if(numRows1!=numRows2 || numCols1!=numCols2) {                                                           \
-        psError(__func__, ": Inconsistent element count: numRows: %d vs %d numCols: %d vs %d", numRows1,     \
-                numRows2, numCols1, numCols2);                                                               \
+        psError(PS_ERR_BAD_PARAMETER_SIZE, true,                                                             \
+                PS_ERRORTEXT_psMatrix_IMAGE_SIZE_DIFFERS,                                                     \
+                numCols1, numRows1, numCols2, numRows2);                                                     \
         if (OUT != IN1 && OUT != IN2) {                                                                      \
             psFree(OUT);                                                                                     \
@@ -366,5 +364,9 @@
     break;                                                                                                   \
 default:                                                                                                     \
-    psError(__func__, ": Invalid PS_TYPE: %d", IN1->type);                                                   \
+    /* char* strType; \
+    PS_TYPE_NAME(strType,IN1->type);                                                                         \
+    psError(PS_ERR_BAD_PARAMETER_TYPE, true,                                                                 \
+            PS_ERRORTEXT_psMatrix_TYPE_MISMATCH,                                                             \
+            strType);  */                                                                                      \
     if (OUT != IN1 && OUT != IN2) {                                                                          \
         psFree(OUT);                                                                                         \
@@ -391,5 +393,6 @@
 } else if(!strncmp(OP, "min", 3)) {                                                                          \
     if(PS_IS_PSELEMTYPE_COMPLEX(IN1->type)) {                                                                \
-        psError(__func__, ": Minimum operation not supported for complex numbers");                          \
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,                                                            \
+                PS_ERRORTEXT_psMatrix_MIN_COMPLEX_SUPPORT);                                                  \
         if (OUT != IN1 && OUT != IN2) {                                                                      \
             psFree(OUT);                                                                                     \
@@ -401,5 +404,6 @@
 } else if(!strncmp(OP, "max", 3)) {                                                                          \
     if(PS_IS_PSELEMTYPE_COMPLEX(IN1->type)) {                                                                \
-        psError(__func__, ": Maximum operation not supported for complex numbers");                          \
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,                                                            \
+                PS_ERRORTEXT_psMatrix_MAX_COMPLEX_SUPPORT);                                                  \
         if (OUT != IN1 && OUT != IN2) {                                                                      \
             psFree(OUT);                                                                                     \
@@ -410,5 +414,7 @@
     }                                                                                                        \
 } else {                                                                                                     \
-    psError(__func__, ": Invalid operation: %s", OP);                                                        \
+    psError(PS_ERR_BAD_PARAMETER_VALUE, true,                                                                \
+            PS_ERRORTEXT_psMatrix_OPERATION_UNSUPPORTED,                                                     \
+            OP);                                                                                             \
     if (OUT != IN1 && OUT != IN2) {                                                                          \
         psFree(OUT);                                                                                         \
@@ -419,67 +425,38 @@
 psPtr psBinaryOp(psPtr out, psPtr in1, char *op, psPtr in2)
 {
-    psDimen dim1 = 0;
-    psDimen dim2 = 0;
-    psElemType elType1 = 0;
-    psElemType elType2 = 0;
-    psType* psType1 = NULL;
-    psType* psType2 = NULL;
-
-    psType1 = (psType* ) in1;
-    if (psType1 == NULL) {
-        psError(__func__, ": Line %d - Null in1 argument", __LINE__);
-        if (out != in1 && out != in2) {
-            psFree(out);
-        }
-        return NULL;
-    }
-
-    psType2 = (psType* ) in2;
-    if (psType2 == NULL) {
-        psError(__func__, ": Line %d - Null in2 argument", __LINE__);
-        if (out != in1 && out != in2) {
-            psFree(out);
-        }
-        return NULL;
-    }
-
-    if (op == NULL) {
-        psError(__func__, ": Line %d - Null op argument", __LINE__);
-        if (out != in1 && out != in2) {
-            psFree(out);
-        }
-        return NULL;
-    }
-
-    dim1 = psType1->dimen;
-    dim2 = psType2->dimen;
-    elType1 = psType1->type;
-    elType2 = psType2->type;
-
-    if (elType1 != elType2) {
-        psError(__func__, ": Line %d - Element types for arguments inconsistent: (%d, %d)", __LINE__,
-                elType1, elType2);
-        if (out != in1 && out != in2) {
-            psFree(out);
-        }
-        return NULL;
-    }
-
-    if (dim1 == PS_DIMEN_OTHER || dim2 == PS_DIMEN_OTHER) {
-        psError(__func__, ": Line %d - PS_DIMEN_OTHER not allowed for arguments: (%d, %d)", __LINE__,
-                dim1, dim2);
-        if (out != in1 && out != in2) {
-            psFree(out);
-        }
-        return NULL;
-    }
+
+    psVector* input1 = (psVector* ) in1;
+    psVector* input2 = (psVector* ) in2;
+
+    #define psBinaryOp_EXIT { \
+                              if (out != in1 && out != in2) { \
+                              psFree(out); \
+                              } \
+                              return NULL; \
+                            }
+
+    PS_PTR_CHECK_NULL_GENERAL(input1, psBinaryOp_EXIT);
+    PS_PTR_CHECK_NULL_GENERAL(input2, psBinaryOp_EXIT);
+    PS_PTR_CHECK_NULL_GENERAL(op, psBinaryOp_EXIT);
+
+    PS_PTR_CHECK_TYPE_EQUAL_GENERAL(input1,input2, psBinaryOp_EXIT);
+
+    PS_PTR_CHECK_DIMEN_GENERAL(input1, PS_DIMEN_OTHER, psBinaryOp_EXIT);
+    PS_PTR_CHECK_DIMEN_GENERAL(input2, PS_DIMEN_OTHER, psBinaryOp_EXIT);
+
+    psType* psType1 = (psType*)in1;
+    psType* psType2 = (psType*)in2;
+    psDimen dim1 = psType1->dimen;
+    psDimen dim2 = psType2->dimen;
+    psElemType elType1 = psType1->type;
+    psElemType elType2 = psType2->type;
 
     if (dim1 == PS_DIMEN_VECTOR || dim1 == PS_DIMEN_TRANSV) {
         if (((psVector* ) in1)->n == 0) {
-            psLogMsg(__func__, PS_LOG_WARN, ": Line %d - Vector contains zero elements");
+            psLogMsg(__func__, PS_LOG_WARN, "Vector contains zero elements");
         }
     } else if (dim1 == PS_DIMEN_IMAGE) {
         if (((psImage* ) in1)->numCols == 0 || ((psImage* ) in1)->numRows == 0) {
-            psLogMsg(__func__, PS_LOG_WARN, ": Line %d - Image contains zero length row or cols");
+            psLogMsg(__func__, PS_LOG_WARN, "Image contains zero length row or cols");
         }
     }
@@ -487,9 +464,9 @@
     if (dim2 == PS_DIMEN_VECTOR || dim2 == PS_DIMEN_TRANSV) {
         if (((psVector* ) in2)->n == 0) {
-            psLogMsg(__func__, PS_LOG_WARN, ": Line %d - Vector contains zero elements");
+            psLogMsg(__func__, PS_LOG_WARN, "Vector contains zero elements");
         }
     } else if (dim2 == PS_DIMEN_IMAGE) {
         if (((psImage* ) in2)->numCols == 0 || ((psImage* ) in2)->numRows == 0) {
-            psLogMsg(__func__, PS_LOG_WARN, ": Line %d - Image contains zero length row or cols");
+            psLogMsg(__func__, PS_LOG_WARN, "Image contains zero length row or cols");
         }
     }
@@ -513,5 +490,6 @@
             out = psVectorRecycle(out,((psVector*)in2)->n,elType1);
             if (out == NULL) {
-                psError(__func__, "Couldn't create a proper output psVector.");
+                psError(PS_ERR_UNKNOWN, false,
+                        PS_ERRORTEXT_psMatrix_OUTPUT_VECTOR_NOT_CREATED);
                 return NULL;
             }
@@ -520,10 +498,14 @@
             out = psImageRecycle(out, ((psImage* ) in2)->numCols, ((psImage* ) in2)->numRows,elType1);
             if (out == NULL) {
-                psError(__func__, "Couldn't create a proper output psImage.");
+                psError(PS_ERR_UNKNOWN, false,
+                        PS_ERRORTEXT_psMatrix_OUTPUT_IMAGE_NOT_CREATED);
                 return NULL;
             }
             BINARY_OP(SCALAR, IMAGE, out, psType1, op, psType2);        // scalar op image
         } else {
-            psError(__func__, ": Line %d - Invalid dimensionality for in2 arg: %d", __LINE__, dim2);
+            psError(PS_ERR_BAD_PARAMETER_TYPE, true,
+                    PS_ERRORTEXT_psMatrix_DIMEN_INVALID,
+                    "in2",dim2);
+            psBinaryOp_EXIT;
         }
     } else if (dim1 == PS_DIMEN_VECTOR || dim1 == PS_DIMEN_TRANSV) {
@@ -531,5 +513,6 @@
             out = psVectorRecycle(out,((psVector*)in1)->n,elType1);
             if (out == NULL) {
-                psError(__func__, "Couldn't create a proper output psVector.");
+                psError(PS_ERR_UNKNOWN, false,
+                        PS_ERRORTEXT_psMatrix_OUTPUT_VECTOR_NOT_CREATED);
                 return NULL;
             }
@@ -538,5 +521,6 @@
             out = psVectorRecycle(out,((psVector*)in2)->n,elType2);
             if (out == NULL) {
-                psError(__func__, "Couldn't create a proper output psVector.");
+                psError(PS_ERR_UNKNOWN, false,
+                        PS_ERRORTEXT_psMatrix_OUTPUT_VECTOR_NOT_CREATED);
                 return NULL;
             }
@@ -545,15 +529,20 @@
             out = psImageRecycle(out, ((psImage* ) in2)->numCols, ((psImage* ) in2)->numRows, elType2);
             if (out == NULL) {
-                psError(__func__, "Couldn't create a proper output psImage.");
+                psError(PS_ERR_UNKNOWN, false,
+                        PS_ERRORTEXT_psMatrix_OUTPUT_IMAGE_NOT_CREATED);
                 return NULL;
             }
             BINARY_OP(VECTOR, IMAGE, out, psType1, op, psType2);        // vector op image
         } else {
-            psError(__func__, ": Line %d - Invalid dimensionality for in2 arg: %d", __LINE__, dim2);
+            psError(PS_ERR_BAD_PARAMETER_TYPE, true,
+                    PS_ERRORTEXT_psMatrix_DIMEN_INVALID,
+                    "in2",dim2);
+            psBinaryOp_EXIT;
         }
     } else if (dim1 == PS_DIMEN_IMAGE) {
         out = psImageRecycle(out, ((psImage*)in1)->numCols, ((psImage*)in1)->numRows, elType1);
         if (out == NULL) {
-            psError(__func__, "Couldn't create a proper output psImage.");
+            psError(PS_ERR_UNKNOWN, false,
+                    PS_ERRORTEXT_psMatrix_OUTPUT_IMAGE_NOT_CREATED);
             return NULL;
         }
@@ -565,16 +554,14 @@
             BINARY_OP(IMAGE, IMAGE, out, psType1, op, psType2); // image op image
         } else {
-            if (out != in1 && out != in2) {
-                psFree(out);
-            }
-            psError(__func__, ": Line %d - Invalid dimensionality for in2 arg: %d", __LINE__, dim2);
-            return NULL;
+            psError(PS_ERR_BAD_PARAMETER_TYPE, true,
+                    PS_ERRORTEXT_psMatrix_DIMEN_INVALID,
+                    "in2",dim2);
+            psBinaryOp_EXIT;
         }
     } else {
-        if (out != in1 && out != in2) {
-            psFree(out);
-        }
-        psError(__func__, ": Line %d - Invalid dimensionality for in1 arg: %d", __LINE__, dim1);
-        return NULL;
+        psError(PS_ERR_BAD_PARAMETER_TYPE, true,
+                PS_ERRORTEXT_psMatrix_DIMEN_INVALID,
+                "in1",dim1);
+        psBinaryOp_EXIT;
     }
 
@@ -605,7 +592,7 @@
 #define VECTOR(OUT,IN,OP,TYPE)                                                                               \
 {                                                                                                            \
-    psS32 i = 0;                                                                                               \
-    psS32 nIn = 0;                                                                                             \
-    psS32 nOut = 0;                                                                                            \
+    psS32 i = 0;                                                                                             \
+    psS32 nIn = 0;                                                                                           \
+    psS32 nOut = 0;                                                                                          \
     ps##TYPE *o = NULL;                                                                                      \
     ps##TYPE *i1 = NULL;                                                                                     \
@@ -613,5 +600,7 @@
     nOut = ((psVector* )OUT)->n;                                                                             \
     if(nIn != nOut) {                                                                                        \
-        psError(__func__, ": Inconsistent element count: %d vs %d", nIn, nOut);                              \
+        psError(PS_ERR_BAD_PARAMETER_SIZE, true,                                                             \
+                PS_ERRORTEXT_psMatrix_COUNT_DIFFERS,                                                         \
+                nIn, nOut);                                                                                  \
         if (OUT != IN) {                                                                                     \
             psFree(OUT);                                                                                     \
@@ -629,10 +618,10 @@
 #define IMAGE(OUT,IN,OP,TYPE)                                                                                \
 {                                                                                                            \
-    psS32 i = 0;                                                                                               \
-    psS32 j = 0;                                                                                               \
-    psS32 numRowsIn = 0;                                                                                       \
-    psS32 numColsIn = 0;                                                                                       \
-    psS32 numRowsOut = 0;                                                                                      \
-    psS32 numColsOut = 0;                                                                                      \
+    psS32 i = 0;                                                                                             \
+    psS32 j = 0;                                                                                             \
+    psS32 numRowsIn = 0;                                                                                     \
+    psS32 numColsIn = 0;                                                                                     \
+    psS32 numRowsOut = 0;                                                                                    \
+    psS32 numColsOut = 0;                                                                                    \
     ps##TYPE *o = NULL;                                                                                      \
     ps##TYPE *i1 = NULL;                                                                                     \
@@ -642,6 +631,7 @@
     numColsOut = ((psImage* )OUT)->numCols;                                                                  \
     if(numRowsIn!=numRowsOut || numColsIn!=numColsOut) {                                                     \
-        psError(__func__, ": Inconsistent element count: numRows: %d vs %d numCols: %d vs %d", numRowsIn,    \
-                numRowsOut, numColsIn, numColsOut);                                                          \
+        psError(PS_ERR_BAD_PARAMETER_SIZE, true,                                                             \
+                PS_ERRORTEXT_psMatrix_IMAGE_SIZE_DIFFERS,                                                    \
+                numColsIn, numRowsIn, numColsOut, numRowsOut);                                               \
         if (OUT != IN) {                                                                                     \
             psFree(OUT);                                                                                     \
@@ -697,10 +687,15 @@
     DIM(OUT,IN,OP,C32);                                                                                      \
     break;                                                                                                   \
-default:                                                                                                     \
-    psError(__func__, ": Invalid PS_TYPE: %d", IN->type);                                                    \
-    if (OUT != IN) {                                                                                         \
-        psFree(OUT);                                                                                         \
-    }                                                                                                        \
-    return NULL;                                                                                             \
+default: {                                                                                                     \
+        char* strType;                                                                                           \
+        PS_TYPE_NAME(strType, IN->type);                                                                         \
+        psError(PS_ERR_BAD_PARAMETER_TYPE, true,                                                                 \
+                PS_ERRORTEXT_psMatrix_TYPE_MISMATCH,                                                             \
+                strType);                                                                                        \
+        if (OUT != IN) {                                                                                         \
+            psFree(OUT);                                                                                         \
+        }                                                                                                        \
+        return NULL;                                                                                             \
+    } \
 }
 
@@ -815,32 +810,28 @@
     }                                                                                                        \
 } else {                                                                                                     \
-    psError(__func__, ": Invalid operation: %s", OP);                                                        \
+    psError(PS_ERR_BAD_PARAMETER_VALUE, true,                                                                \
+            PS_ERRORTEXT_psMatrix_OPERATION_UNSUPPORTED,                                                     \
+            OP);                                                                                             \
 }
 
 psPtr psUnaryOp(psPtr out, psPtr in, char *op)
 {
-    psDimen dimIn = 0;
-    psElemType elTypeIn = 0;
-    psType* psTypeIn = NULL;
-
-    psTypeIn = (psType* ) in;
-    if (psTypeIn == NULL) {
-        psError(__func__, ": Line %d - Null in argument", __LINE__);
-        if (out != in) {
-            psFree(out);
-        }
-        return NULL;
-    }
-
-    if (op == NULL) {
-        psError(__func__, ": Line %d - Null op argument", __LINE__);
-        if (out != in) {
-            psFree(out);
-        }
-        return NULL;
-    }
-
-    dimIn = psTypeIn->dimen;
-    elTypeIn = psTypeIn->type;
+    #define psUnaryOp_EXIT { \
+                             if (out != in) { \
+                             psFree(out); \
+                             } \
+                             if(psTypeIn->dimen==PS_DIMEN_SCALAR && in!=out) { \
+                             psFree(in); \
+                             } \
+                             return NULL; \
+                           }
+
+    psType* psTypeIn = (psType* ) in;
+
+    PS_PTR_CHECK_NULL_GENERAL(in, psUnaryOp_EXIT);
+    PS_PTR_CHECK_NULL_GENERAL(op, psUnaryOp_EXIT);
+
+    psDimen dimIn = psTypeIn->dimen;
+    psElemType elTypeIn = psTypeIn->type;
 
     switch (dimIn) {
@@ -857,9 +848,7 @@
     case PS_DIMEN_TRANSV:
         if (((psVector*)in)->n == 0) {
-            if (out != in) {
-                psFree(out);
-            }
-            psError(__func__, ": Line %d - Vector contains zero elements");
-            return NULL;
+            psError(PS_ERR_BAD_PARAMETER_SIZE, true,
+                    PS_ERRORTEXT_psMatrix_VECTOR_EMPTY);
+            psUnaryOp_EXIT;
         }
 
@@ -868,6 +857,7 @@
                               elTypeIn);
         if (out == NULL) {
-            psError(__func__, "Couldn't create a proper output psVector.");
-            return NULL;
+            psError(PS_ERR_UNKNOWN, false,
+                    PS_ERRORTEXT_psMatrix_OUTPUT_VECTOR_NOT_CREATED);
+            psUnaryOp_EXIT;
         }
 
@@ -876,9 +866,7 @@
     case PS_DIMEN_IMAGE:
         if (((psImage* ) in)->numCols == 0 || ((psImage* ) in)->numRows == 0) {
-            if (out != in) {
-                psFree(out);
-            }
-            psError(__func__, ": Line %d - Image contains zero length row or cols");
-            return NULL;
+            psError(PS_ERR_BAD_PARAMETER_SIZE, true,
+                    PS_ERRORTEXT_psMatrix_IMAGE_EMPTY);
+            psUnaryOp_EXIT;
         }
 
@@ -888,6 +876,7 @@
                              elTypeIn);
         if (out == NULL) {
-            psError(__func__, "Couldn't create a proper output psImage.");
-            return NULL;
+            psError(PS_ERR_UNKNOWN, false,
+                    PS_ERRORTEXT_psMatrix_OUTPUT_IMAGE_NOT_CREATED);
+            psUnaryOp_EXIT;
         }
 
@@ -898,6 +887,8 @@
             psFree(out);
         }
-        psError(__func__, ": Line %d - Invalid dimensionality for in arg: %d", __LINE__, dimIn);
-        return NULL;
+        psError(PS_ERR_BAD_PARAMETER_SIZE, true,
+                PS_ERRORTEXT_psMatrix_DIMEN_INVALID,
+                "in", dimIn);
+        psUnaryOp_EXIT;
     }
 
