home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / ch_2.3 / imgbool / imgbool.c next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  5.2 KB  |  180 lines

  1. /* 
  2.  * imgbool.c
  3.  * 
  4.  * Practical Algorithms for Image Analysis
  5.  * 
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /* IMGBOOL:     program combines two images by boolean operation of
  10.  *            AND, OR, or XOR
  11.  *          usage: imgbool in1img in2img outimg [-o OPERAND] [-x X] [-y Y] [-L]
  12.  */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <tiffimage.h>          /* picfile info on images */
  17. #include <images.h>             /* for sun images */
  18. extern void print_sos_lic ();
  19.  
  20. int usage (short);
  21. int input (int, char **, char *, long *, long *);
  22.  
  23. main (argc, argv)
  24.      int argc;
  25.      char *argv[];
  26. {
  27.   register int x1, y1, x2, y2;  /* image incrementors */
  28.   register long temp;
  29.   Image *imgI1, *imgI2;         /* 2 input images, first is also output */
  30.   unsigned char **image1,       /* first and second input image */
  31.   **image2;                     /* first is also output image */
  32.   struct point imgSize1, imgSize2;  /* image dimensions */
  33.   char oper;                    /* operand */
  34.   long x0, y0;                  /* x,y offset of second image in first */
  35.   long endX, endY;              /* end coord.s of image2 in image1 */
  36.  
  37. /* read user parameter values */
  38.   if ((input (argc, argv, &oper, &x0, &y0)) < 0)
  39.     return (-1);
  40.  
  41. /* open images */
  42.   imgI1 = ImageIn (argv[1]);
  43.   if (imgI1->bps == 8 && imgI1->spp == 3) {
  44.     printf ("Got RGB image!!!\nInput image must be Grayscale or B&W!!\n");
  45.     exit (1);
  46.   }
  47.   image1 = imgI1->img;
  48.   imgSize1.y = ImageGetHeight (imgI1);
  49.   imgSize1.x = ImageGetWidth (imgI1);
  50.  
  51.   imgI2 = ImageIn (argv[2]);
  52.   if (imgI2->bps == 8 && imgI2->spp == 3) {
  53.     printf ("Got RGB image!!!\nInput image must be Grayscale or B&W!!\n");
  54.     exit (1);
  55.   }
  56.   image2 = imgI2->img;
  57.   imgSize2.y = ImageGetHeight (imgI2);
  58.   imgSize2.x = ImageGetWidth (imgI2);
  59.  
  60.   printf ("input/output image size is %dx%d\n", imgSize1.x, imgSize1.y);
  61.   if (imgSize1.x != imgSize2.x || imgSize1.y != imgSize2.y)
  62.     printf ("(second image size is %dx%d)\n", imgSize2.x, imgSize2.y);
  63.  
  64. /* perform boolean operation */
  65.  
  66.   endX = (imgSize2.x + x0 > imgSize1.x) ? imgSize1.x : imgSize2.x + x0;
  67.   endY = (imgSize2.y + y0 > imgSize1.y) ? imgSize1.y : imgSize2.y + y0;
  68.  
  69.   for (y1 = y0, y2 = 0; y1 < endY; y1++, y2++) {
  70.     for (x1 = x0, x2 = 0; x1 < endX; x1++, x2++) {
  71.       switch (oper) {
  72.       case 'A':
  73.         temp = (image1[y1][x1] != 0 && image2[y2][x2] != 0) ? 255 : 0;
  74.         break;
  75.       case 'O':
  76.         temp = (image1[y1][x1] != 0 || image2[y2][x2] != 0) ? 255 : 0;
  77.         break;
  78.       case 'X':
  79.         temp = ((image1[y1][x1] != 0 && image2[y2][x2] == 0)
  80.                 || (image1[y1][x1] == 0 && image2[y2][x2] != 0)) ? 255 : 0;
  81.         break;
  82.       }
  83.       image1[y1][x1] = (unsigned char) temp;
  84.     }
  85.   }
  86.  
  87. /* write output image */
  88.   ImageOut (argv[3], imgI1);
  89.   return (0);
  90. }
  91.  
  92.  
  93. /* USAGE:       function gives instructions on usage of program
  94.  *                    usage: usage (flag)
  95.  *              When flag is 1, the long message is given, 0 gives short.
  96.  */
  97.  
  98. int
  99. usage (flag)
  100.      short flag;                /* flag =1 for long message; =0 for short message */
  101. {
  102.  
  103. /* print short usage message or long */
  104.   printf ("usage: imgbool in1img in2img outimg [-o OPERAND] [-x X] [-y Y] [-L]\n");
  105.   if (flag == 0)
  106.     return (-1);
  107.  
  108.   printf ("\nimgbool performs boolean combination between two\n");
  109.   printf ("images: OR, AND, or XOR\n\n");
  110.   printf ("ARGUMENTS:\n");
  111.   printf ("   in1img: first input image filename (TIF)\n");
  112.   printf ("   in2img: second input image filename (TIF)\n");
  113.   printf ("   outimg: output image filename (TIF)\n\n");
  114.   printf ("OPTIONS:\n");
  115.   printf ("       -o: OPERAND <O,A,X> for OR, AND, or XOR.\n");
  116.   printf ("           Default is OR (O).\n");
  117.   printf ("     -x X: X-offset location of second image in first.\n");
  118.   printf ("     -y Y: Y-offset location of second image in first.\n");
  119.   printf ("           Default = (0,0).\n");
  120.   printf ("           Note that for images of different sizes, the resultant image\n");
  121.   printf ("           will be the size of the first specified image.\n");
  122.   printf ("       -L: print Software License for this module\n");
  123.  
  124.   return (-1);
  125. }
  126.  
  127.  
  128. /* INPUT:       function reads input parameters
  129.  *                  usage: input (argc, argv, &oper, &x0, &y0)
  130.  */
  131.  
  132. #define USAGE_EXIT(VALUE) {usage (VALUE); return (-1);}
  133.  
  134. int
  135. input (argc, argv, oper, x0, y0)
  136.      int argc;
  137.      char *argv[];
  138.      char *oper;                /* operand */
  139.      long *x0, *y0;             /* offset of second image in first */
  140. {
  141.   long n;
  142.  
  143.   if (argc == 1)
  144.     USAGE_EXIT (1);
  145.   if (argc == 2 || argc == 3)
  146.     USAGE_EXIT (0);
  147.  
  148.   *oper = 'O';
  149.   *x0 = *y0 = 0;
  150.  
  151.   for (n = 4; n < argc; n++) {
  152.     if (strcmp (argv[n], "-o") == 0) {
  153.       if (++n == argc || argv[n][0] == '-')
  154.         USAGE_EXIT (0);
  155.       *oper = *(argv[n]);
  156.     }
  157.     else if (strcmp (argv[n], "-x") == 0) {
  158.       if (++n == argc || argv[n][0] == '-')
  159.         USAGE_EXIT (0);
  160.       *x0 = atol (argv[n]);
  161.     }
  162.     else if (strcmp (argv[n], "-y") == 0) {
  163.       if (++n == argc || argv[n][0] == '-')
  164.         USAGE_EXIT (0);
  165.       *y0 = atol (argv[n]);
  166.     }
  167.     else if (strcmp (argv[n], "-L") == 0) {
  168.       print_sos_lic ();
  169.       exit (0);
  170.     }
  171.     else
  172.       USAGE_EXIT (0);
  173.   }
  174.  
  175.   if (*oper != 'O' && *oper != 'A' && *oper != 'X')
  176.     USAGE_EXIT (0);
  177.  
  178.   return (0);
  179. }
  180.