home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 594a.lha / IP_Interface_example / negative.c < prev    next >
C/C++ Source or Header  |  1992-01-03  |  7KB  |  185 lines

  1. /*
  2.  * negative.c - Does a simple negative in Imagemaster
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. /*
  8.  * Each individual buffer has the following struture for use in a
  9.  * Public Interface Module. Note that the "mask" pointer is usually
  10.  * NULL; it is only there when the buffer is non-rectangular, for
  11.  * instance when the user has cut out a clip using freehand.
  12.  * if it is there, you are well advised to use it. There is data
  13.  * available in the buffer where the mask is zero, but it is
  14.  * hidden from the user and processing in it is a waste of time.
  15.  * Although all three RGB pointers should be there, you should
  16.  * still check them for safety's sake.
  17.  */
  18. struct jackinbuff
  19.   {
  20.     unsigned char *red;     /* points to red component of buffer     */
  21.     unsigned char *green;   /* points to green component of buffer   */
  22.     unsigned char *blue;    /* points to blue component of buffer    */
  23.     unsigned char *mask;    /* points to local mask, IF there is one */
  24.     unsigned short x;       /* X size of buffer (pixels per line)    */
  25.     unsigned short y;       /* Y size of buffer (pixels per column)  */
  26.   };
  27.  
  28. /*
  29.  * This is the main "handle" you get from the ARexx "jackin" command.
  30.  * Note that any, or ALL of the buffer pointers may be NULL, and you
  31.  * MUST check them for validity before using them or you may blow up
  32.  * the system. Macros may be invoked without a single buffer available.
  33.  */
  34. struct jackina
  35.   {
  36.     struct jackinbuff *primary;     /* current working buffer               */
  37.     struct jackinbuff *secondary;   /* compositing buffer                   */
  38.     struct jackinbuff *undo;        /* undo buffer                          */
  39.     struct jackinbuff *blend;       /* blend buffer                         */
  40.     struct jackinbuff *brush;       /* brush buffer                         */
  41.     unsigned char     *mask;        /* working mask (use WITH local masks)  */
  42.     char              jack[4];      /* ID for sanity chk: 'J' 'A' 'C' 'K'   */
  43.     long              showscr;      /* points to control panel screen       */
  44.     unsigned char     pname[4];     /* 'IM'00 'IP'00 or 'IMFC' - sanity chk */
  45.     short             ver[2];       /* version:revision numbers             */
  46.   };
  47.  
  48. int main(argc,argv)
  49.   int argc;
  50.   char *argv[];
  51.   {
  52.   unsigned int x,y,xw,yw,offset,yoffset;
  53.   unsigned char rr,gg,bb;
  54.   unsigned char *mask;
  55.   unsigned char *lmask;
  56.   unsigned char *rbu;
  57.   unsigned char *gbu;
  58.   unsigned char *bbu;
  59.   short cancel;
  60.   struct jackina *jackins;
  61.   
  62.     if (argc <= 1)
  63.       {
  64.         imes("NEGATIVE <pointer to IM jackin structure>",
  65.              "No pointer argument supplied!"); /* ARexx problem? */
  66.         return(1);
  67.       }
  68.     else
  69.       {
  70.         sscanf(argv[1],"%x",(int*)&jackins);
  71.       }
  72.  
  73.     if (jackins == 0)
  74.       {
  75.         imes("NEGATIVE : Argument is ZERO!",0); /* ARexx problem? */
  76.         return(1);
  77.       }
  78.       
  79.     if (jackins->jack[0] != 'J' ||
  80.         jackins->jack[1] != 'A' ||
  81.         jackins->jack[2] != 'C' ||
  82.         jackins->jack[3] != 'K' )
  83.       {
  84.         imes("NEGATIVE : Problem with information", /* VERY scary! */
  85.              "passed from image processor!");
  86.         return(1);
  87.       }
  88.     if (beginvbars(jackins->showscr)) /* open communications to panel */
  89.       {
  90.         imes("Unable to open Progress Communication!",0); /* scary! */
  91.         return(1);
  92.       }
  93.     /*
  94.      * Up to this point, if there is a problem, "imes()" will send its
  95.      * output to the STDOUT stream; we were not certain that we were
  96.      * successfully linked to the image processor until now. From now on,
  97.      * messages from "imes()" will appear upon the user's control panel.
  98.      */
  99.     if (jackins->primary == NULL) /* if no primary buffer */
  100.       {
  101.         imes("NEGATIVE : Primary buffer is NULL!",0); /* ok, but silly */
  102.         return(1);
  103.       }
  104.     mask = jackins->mask;
  105.     if (jackins->mask == NULL) /* if no mask available */
  106.       {
  107.         imes("NEGATIVE : No area selection mask found!",0); /* scary! */
  108.         return(1);
  109.       }
  110.     xw = jackins->primary->x;
  111.     yw = jackins->primary->y;
  112.     if (xw == 0 || yw == 0) /* if X:Y size of primary is insane */
  113.       {
  114.         imes("NEGATIVE : Primary buffer is Empty!",0); /* scary! */
  115.         return(1);
  116.       }
  117.     rbu   = jackins->primary->red;
  118.     gbu   = jackins->primary->green;
  119.     bbu   = jackins->primary->blue;
  120.     lmask = jackins->primary->mask; /* this can be NULL - we check later */
  121.     if ((rbu==NULL) || (gbu==NULL) || (bbu==NULL)) /* any pointer missing? */
  122.       {
  123.         imes("NEGATIVE : Primary buffer is corrupted!",0); /* scary! */
  124.         return(1);
  125.       }
  126.     /*
  127.      * Actual operation is performed here
  128.      */
  129.     if (progressaset(yw,"-Negative-")) /* open progress bar */
  130.       {
  131.         imes("Cannot open progress bar!",0);
  132.         return(1);
  133.       }
  134.     cancel = 0;
  135.     for (y=0; y<yw; y++) /* for each scan line of the image */
  136.       {
  137.         yoffset = y * xw; /* make address offset - saves lots of multiplies */
  138.         for (x=0; x<xw; x++) /* for each pixel in the scan line */
  139.           {
  140.             offset = yoffset + x;
  141.             if (lmask) /* if there is a local mask */
  142.               {
  143.                 if (mask[offset] & lmask[offset]) /* selected & selectable? */
  144.                   {
  145.                     rr = *(rbu+offset); /* get the pixel RGB data */
  146.                     gg = *(gbu+offset);
  147.                     bb = *(bbu+offset);
  148.                     *(rbu+offset) = 255 - rr;  /* perform color negative */
  149.                     *(gbu+offset) = 255 - gg;
  150.                     *(bbu+offset) = 255 - bb;
  151.                   }
  152.               }
  153.             else /* there was no local mask - just use main mask */
  154.               {
  155.                 if (mask[offset]) /* selected? */
  156.                   {
  157.                     rr = *(rbu+offset); /* get the pixel RGB data */
  158.                     gg = *(gbu+offset);
  159.                     bb = *(bbu+offset);
  160.                     *(rbu+offset) = 255 - rr;  /* perform color negative */
  161.                     *(gbu+offset) = 255 - gg;
  162.                     *(bbu+offset) = 255 - bb;
  163.                   }
  164.               }
  165.           }
  166.         if (progressupd(y)) /* show progress, look for press of CLOSE gadget */
  167.           {
  168.             if (y != yw) /* if we're not already done */
  169.               {
  170.                 cancel = 1; /* indicate user forced exit */
  171.                 y = yw;     /* force structured exit from processing loop */
  172.               }
  173.           }
  174.       }
  175.     progressend(); /* close progress bar */
  176.     if (cancel)    /* ack the press of close gadget */
  177.       {
  178.         imes("Operation Cancelled at User's Request",0);
  179.         return(1); /* return not ok - operation incomplete */
  180.       }
  181.     endvbars(); /* close ALL references to image processor screen */
  182.     return(0);
  183.   }
  184.  
  185.