Main Page   Data Structures   File List   Data Fields   Globals  

combine.c

Go to the documentation of this file.
00001 #include        "sadie.h"
00002 
00003 /*-Copyright Information------------------------------------------------------*/
00004 /* Copyright (c) 1990 by the University of Arizona Digital Image Analysis Lab */
00005 /*----------------------------------------------------------------------------*/
00006 /*-General Information--------------------------------------------------------*/
00007 /*                                                                            */
00008 /*   This function computes a weighted linear combination of two images.      */
00009 /*                                                                            */
00010 /*----------------------------------------------------------------------------*/
00011 /*-Interface Information------------------------------------------------------*/
00012 void
00013 COMBINE (IMAGE * in1,           /*  I   Pointer to the first input image.                     */
00014          IMAGE * in2,           /*  I   Pointer to the second input image.                    */
00015          short option,          /*  I   Switch, indicating type of combination operation:     */
00016          /*      ADD      - Sum of the input images is computed.       */
00017          /*      SUBTRACT - Difference of the input images is computed. */
00018          /*      MULTIPLY - Product of the input images is computed.   */
00019          /*      DIVIDE   - Quotient of the input images is computed.  */
00020          /*      MINIMUM  - Minimum of the input images is computed.   */
00021          /*      MAXIMUM  - Maximum of the input images is computed.   */
00022          double wgt1,           /*  I   Weight on the first input image.                      */
00023          double wgt2,           /*  I   Weight on the second input image.                     */
00024          IMAGE ** out           /*  O   Address of a pointer to the output image.             */
00025          /*      An input image may be overwritten (out = &in1,&in2).  */
00026 /*----------------------------------------------------------------------------*/
00027   )
00028 {
00029   register short i, j, k;
00030   double factor;
00031   char msg[SLEN];
00032 
00033   if (TIMES)
00034     TIMING (T_COMBINE);
00035   if (NAMES)
00036     {
00037       MESSAGE ('I', "");
00038       MESSAGE ('I', "COMBINE");
00039       MESSAGE ('I', "");
00040       sprintf (msg, " First input image:        %s", in1->text);
00041       MESSAGE ('I', msg);
00042       sprintf (msg, " Second input image:       %s", in2->text);
00043       MESSAGE ('I', msg);
00044       if (option == ADD)
00045         {
00046           MESSAGE ('I', " Operation:                add");
00047         }
00048       else if (option == SUBTRACT)
00049         {
00050           MESSAGE ('I', " Operation:                subtract");
00051         }
00052       else if (option == MULTIPLY)
00053         {
00054           MESSAGE ('I', " Operation:                multiply");
00055         }
00056       else if (option == DIVIDE)
00057         {
00058           MESSAGE ('I', " Operation:                divide");
00059         }
00060       else if (option == MINIMUM)
00061         {
00062           MESSAGE ('I', " Operation:                minimum");
00063         }
00064       else if (option == MAXIMUM)
00065         {
00066           MESSAGE ('I', " Operation:                maximum");
00067         }
00068       sprintf (msg, " Weight on first image:  %12.4e", wgt1);
00069       MESSAGE ('I', msg);
00070       sprintf (msg, " Weight on second image: %12.4e", wgt2);
00071       MESSAGE ('I', msg);
00072       MESSAGE ('I', " ...............");
00073     }
00074 
00075   /* check input */
00076   if (!CHECKIMG (in1))
00077     {
00078       MESSAGE ('E', " Can't identify first image.");
00079       goto the_end;
00080     }
00081   else if (!CHECKIMG (in2))
00082     {
00083       MESSAGE ('E', " Can't identify second image.");
00084       goto the_end;
00085     }
00086   else if (in1->nbnd != in2->nbnd)
00087     {
00088       MESSAGE ('E', " Number of bands must be identical in both images.");
00089       goto the_end;
00090     }
00091   else if (in1->nlin != in2->nlin)
00092     {
00093       MESSAGE ('E', " Number of lines must be identical in both images.");
00094       goto the_end;
00095     }
00096   else if (in1->npix != in2->npix)
00097     {
00098       MESSAGE ('E',
00099                " Number of pixels/line must be identical in both images.");
00100       goto the_end;
00101     }
00102   else if (option != ADD && option != SUBTRACT && option != MULTIPLY
00103            && option != DIVIDE && option != MINIMUM && option != MAXIMUM)
00104     {
00105       MESSAGE ('E',
00106                " Option must be one of ADD, SUBTRACT, MULTIPLY, DIVIDE, MINIMUM, or MAXIMUM.");
00107       goto the_end;
00108     }
00109   else if (option == DIVIDE && wgt2 == 0.0)
00110     {
00111       MESSAGE ('E', " Denominator weight must not be equal to zero.");
00112       goto the_end;
00113     }
00114 
00115   /* create image of appropriate size */
00116   if (!CHECKIMG (*out))
00117     GETMEM (in1->nbnd, in1->nlin, in1->npix, out);
00118   if (!*out)
00119     goto the_end;
00120 
00121   switch (option)
00122     {
00123     case ADD:
00124       /* add two images */
00125       for (i = 0; i < in1->nbnd; i++)
00126         {
00127           for (j = 0; j < in1->nlin; j++)
00128             {
00129               for (k = 0; k < in1->npix; k++)
00130                 {
00131                   (*out)->data[i][j][k] =
00132                     (PIXEL) ((double) in1->data[i][j][k] * wgt1 +
00133                              (double) in2->data[i][j][k] * wgt2);
00134                 }
00135             }
00136         }
00137       break;
00138     case SUBTRACT:
00139       /* subtract two images */
00140       for (i = 0; i < in1->nbnd; i++)
00141         {
00142           for (j = 0; j < in1->nlin; j++)
00143             {
00144               for (k = 0; k < in1->npix; k++)
00145                 {
00146                   (*out)->data[i][j][k] =
00147                     (PIXEL) ((double) in1->data[i][j][k] * wgt1 -
00148                              (double) in2->data[i][j][k] * wgt2);
00149                 }
00150             }
00151         }
00152       break;
00153     case MULTIPLY:
00154       /* multiply two images */
00155       factor = wgt1 * wgt2;
00156       for (i = 0; i < in1->nbnd; i++)
00157         {
00158           for (j = 0; j < in1->nlin; j++)
00159             {
00160               for (k = 0; k < in1->npix; k++)
00161                 {
00162                   (*out)->data[i][j][k] =
00163                     (PIXEL) ((double) in1->data[i][j][k] *
00164                              (double) in2->data[i][j][k] * factor);
00165                 }
00166             }
00167         }
00168       break;
00169     case DIVIDE:
00170       /* divide two images */
00171       factor = wgt1 / wgt2;
00172       for (i = 0; i < in1->nbnd; i++)
00173         {
00174           for (j = 0; j < in1->nlin; j++)
00175             {
00176               for (k = 0; k < in1->npix; k++)
00177                 {
00178                   if (in2->data[i][j][k] == (PIXEL) 0)
00179                     {
00180                       (*out)->data[i][j][k] =
00181                         k == 0 ? (PIXEL) 0 : (*out)->data[i][j][k - 1];
00182                     }
00183                   else
00184                     {
00185                       (*out)->data[i][j][k] =
00186                         (PIXEL) ((double) in1->data[i][j][k] /
00187                                  (double) in2->data[i][j][k] * factor);
00188                     }
00189                 }
00190             }
00191         }
00192       break;
00193     case MINIMUM:
00194       /* find minimum of two images */
00195       for (i = 0; i < in1->nbnd; i++)
00196         {
00197           for (j = 0; j < in1->nlin; j++)
00198             {
00199               for (k = 0; k < in1->npix; k++)
00200                 {
00201                   (*out)->data[i][j][k] =
00202                     (PIXEL) min ((double) in1->data[i][j][k] * wgt1,
00203                                  (double) in2->data[i][j][k] * wgt2);
00204                 }
00205             }
00206         }
00207       break;
00208     case MAXIMUM:
00209       /* find maximum of two images */
00210       for (i = 0; i < in1->nbnd; i++)
00211         {
00212           for (j = 0; j < in1->nlin; j++)
00213             {
00214               for (k = 0; k < in1->npix; k++)
00215                 {
00216                   (*out)->data[i][j][k] =
00217                     (PIXEL) max ((double) in1->data[i][j][k] * wgt1,
00218                                  (double) in2->data[i][j][k] * wgt2);
00219                 }
00220             }
00221         }
00222       break;
00223     }
00224 
00225 the_end:
00226   if (TIMES)
00227     TIMING (T_EXIT);
00228 }

Generated on Sun May 18 15:36:08 2003 for tclSadie by doxygen1.3