home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Web / Utilities / wwwcount-2.3 / combine / flatten.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-02  |  6.8 KB  |  358 lines

  1. /*
  2.  *    FlattenImage()    -    flattens images
  3.  *
  4.  *    RCS:
  5.  *        $Revision: 2.3 $
  6.  *        $Date: 1996/05/03 02:21:34 $
  7.  *
  8.  *    Security:
  9.  *        Unclassified
  10.  *
  11.  *    Description:
  12.  *      Adapted from ImageMagick
  13.  *
  14.  *    Input Parameters:
  15.  *        type    identifier    description
  16.  *
  17.  *        Image *baseimage     base image struct
  18.  *        Image *subimage         subimage struct 
  19.  *        unsigned int compose composite operator 
  20.  *        int x_offset         x offset from upper left corner, + rightward
  21.  *        int y_offset         y offset from upper left corner, + downward 
  22.  *
  23.  *    Output Parameters:
  24.  *        type    identifier    description
  25.  *
  26.  *        text
  27.  *
  28.  *    Return Values:
  29.  *        value    description
  30.  *
  31.  *    Side Effects:
  32.  *        text
  33.  *
  34.  *    Limitations and Comments:
  35.  *        text
  36.  *
  37.  *    Development History:
  38.  *        when    who        why
  39.  *    08/26/94    muquit    first cut
  40.  */
  41.  
  42. #include "combine.h"
  43. #include "defines.h"
  44.  
  45. void FlattenImage(baseimage,subimage,compose,x_offset,y_offset)
  46. Image
  47.     *baseimage;
  48.  
  49. Image
  50.     *subimage;
  51.  
  52. unsigned int
  53.     compose;
  54.  
  55. int
  56.     x_offset,
  57.     y_offset;
  58. {
  59. int
  60.     blue,
  61.     green,
  62.     red;
  63.  
  64. register int
  65.     i,
  66.     x,
  67.     y;
  68.  
  69. register Runlength
  70.     *p,
  71.     *q;
  72.  
  73. register short
  74.     index;
  75.  
  76.     blue=red=green=MaxRGB;
  77.     index=0;
  78.  
  79.     if (((x_offset+(int) baseimage->columns) < 0) ||
  80.         ((y_offset+(int) baseimage->rows) < 0) ||
  81.         (x_offset > (int) baseimage->columns) || 
  82.         (y_offset > (int) baseimage->rows))
  83.     {
  84.       (void) fprintf (stderr,
  85.         "Unable to flatten image!\n");
  86.       return;
  87.     }
  88.  
  89.     if (!UncompressImage(baseimage))
  90.         return;
  91. #ifdef XDEBUG
  92.     switch (baseimage->class)
  93.     {
  94.         case PseudoClass:
  95.         {
  96.             fprintf (stderr, "Base Image Class: PseudoClass\n");
  97.             break;
  98.         }
  99.  
  100.         case DirectClass:
  101.         {
  102.             fprintf (stderr, "Base Image Class: DirectClass\n");
  103.             break;
  104.         }
  105.     }
  106.  
  107.     switch (subimage->class)
  108.     {
  109.         case PseudoClass:
  110.         {
  111.             fprintf (stderr, "Sub Image Class: PseudoClass\n");
  112.             break;
  113.         }
  114.  
  115.         case DirectClass:
  116.         {
  117.             fprintf (stderr, "Sub Image Class: DirectClass\n");
  118.             break;
  119.         }
  120.  
  121.     }
  122. #endif
  123.  
  124.     if (compose == ReplaceCompositeOp)
  125.     {
  126.       /*
  127.       ** promote image to DirectClass if colormap differs
  128.       */
  129.  
  130.       if (baseimage->class == PseudoClass)
  131.         if (subimage->class == DirectClass)
  132.           baseimage->class=DirectClass;
  133.         else
  134.           {
  135.             if (baseimage->signature == (char *) NULL)
  136.               ColormapSignature(baseimage);
  137.             if (subimage->signature == (char *) NULL)
  138.               ColormapSignature(subimage);
  139.             if (strcmp(baseimage->signature,subimage->signature) != 0)
  140.               baseimage->class=DirectClass;
  141.           }
  142.           if (baseimage->alpha && !subimage->alpha)
  143.           {
  144.             p=subimage->pixels;
  145.             for (i=0; i < subimage->packets; i++)
  146.             {
  147.                 p->index=Opaque;
  148.                 p++;
  149.             }
  150.             subimage->class=DirectClass;
  151.             subimage->alpha=True;
  152.           }
  153.     }
  154.   else
  155.     {
  156.  
  157.     /*
  158.     ** initialize alpha channel of the image
  159.     */
  160.         if (!baseimage->alpha)
  161.         {
  162.             q=baseimage->pixels;
  163.             red = q->red;
  164.             green = q->green;
  165.             blue = q->blue;
  166.  
  167.             for (i=0; i < baseimage->packets; i++)
  168.             {
  169.                 q->index = MaxRGB;
  170.                 if ((q->red == red) && 
  171.                     (q->green == green) && 
  172.                     (q->blue == blue))
  173.                     q->index = 0;
  174.                 q++;
  175.             }
  176.             baseimage->class = DirectClass;
  177.             baseimage->alpha = True;
  178.         }
  179.  
  180.         if (!subimage->alpha)
  181.         {
  182.             p = subimage->pixels;
  183.             red = p->red;
  184.             green = p->green;
  185.             blue = p->blue;
  186.  
  187.             for (i=0; i < subimage->packets; i++)
  188.             {
  189.                 p->index = MaxRGB;
  190.                 if ((p->red == red) && 
  191.                     (p->green == green) && 
  192.                     (p->blue == blue))
  193.                     p->index = 0;
  194.                 p++;
  195.             }
  196.             subimage->class = DirectClass;
  197.             subimage->alpha = True;
  198.         }
  199.     
  200.  
  201.     }
  202. #ifdef XDEBUG
  203.     switch (baseimage->class)
  204.     {
  205.         case PseudoClass:
  206.         {
  207.             fprintf (stderr, "Base Image Class: PseudoClass\n");
  208.             break;
  209.         }
  210.         
  211.         case DirectClass:
  212.         {
  213.             fprintf (stderr, "Base Image Class: DirectClass\n");
  214.             break;
  215.         }
  216.     }
  217.  
  218.     switch (subimage->class)
  219.     {
  220.         case PseudoClass:
  221.         {
  222.             fprintf (stderr, "Sub Image Class: PseudoClass\n");
  223.             break;
  224.         }
  225.         
  226.         case DirectClass:
  227.         {
  228.             fprintf (stderr, "Sub Image Class: DirectClass\n");
  229.             break;
  230.         }
  231.  
  232.     }
  233. #endif
  234.  
  235. #ifdef DEBUG
  236. (void) fprintf (stderr," flatten: subimage->alpha: %d\n",subimage->alpha);
  237. #endif
  238.  
  239.     p=subimage->pixels;
  240.     subimage->runlength=p->length+1;
  241.  
  242.     for (y=0; y < subimage->rows; y++)
  243.     {
  244.         if (((y_offset+y) < 0) || ((y_offset+y) >= baseimage->rows))
  245.           continue;
  246.  
  247.         q=baseimage->pixels+(y_offset+y)*baseimage->columns+x_offset;
  248.  
  249.         for (x=0; x < subimage->columns; x++)
  250.         {
  251.           if (subimage->runlength != 0)
  252.             subimage->runlength--;
  253.           else
  254.         {
  255.             p++;
  256.             subimage->runlength=p->length;
  257.         }
  258.  
  259.         if (((x_offset+x) < 0) || ((x_offset+x) >= baseimage->columns))
  260.         {
  261.             q++;
  262.             continue;
  263.         }
  264.  
  265.     /*
  266.     ** Now handle compositing
  267.     */
  268.  
  269.     switch (compose)
  270.     {
  271.         /*
  272.         ** make black pixels [r,g,b=(0,0,0)] of the sub_image transparent
  273.         ** NOTE: NITFS black is 1, 1, 1, so we do not have to worry
  274.         ** about entities having black colors, because it will be 1,1,1, not
  275.         ** 0,0,0
  276.         */
  277.       
  278.         case BlackTransparentOp:
  279.         {
  280.             if (((int)p->red == 0) && ((int)p->green == 0) &&
  281.                 ((int)p->blue == 0))
  282.             {
  283.                 /*
  284.                 ** these pixels are transparent, i.e., keep the pixels of
  285.                 ** the base image intact
  286.                 */
  287.          
  288.                 red = (int) q->red;
  289.                 green = (int) q->green;
  290.                 blue = (int) q->blue;
  291.             }       
  292.             else
  293.             {
  294.                 /*
  295.                 ** these pixels are opaque..
  296.                 ** replace these pixels of the base image by subimage
  297.                 */
  298.         
  299.                 red = (int) p->red;   
  300.                 green = (int) p->green;
  301.                 blue = (int) p->blue;
  302.             }
  303.             break;
  304.         }   /* end case BlackTransparentOp*/
  305.  
  306.         case ReplaceCompositeOp:
  307.         {
  308.             /*
  309.             ** these pixels of the sub_image are opaque, replace the
  310.             ** pixels of the base image with these
  311.             */
  312.  
  313.             red=p->red;
  314.             green=p->green;
  315.             blue=p->blue;
  316.             index=p->index;
  317.             break;
  318.             }
  319.     }    /* end switch */
  320.  
  321.       /*
  322.       ** do some checking anyway
  323.       */
  324.  
  325.       if (red > MaxRGB)
  326.         q->red=MaxRGB;
  327.       else
  328.         if (red < 0)
  329.           q->red=0;
  330.         else
  331.           q->red=red;
  332.       if (green > MaxRGB)
  333.         q->green=MaxRGB;
  334.       else
  335.         if (green < 0)
  336.           q->green=0;
  337.         else
  338.           q->green=green;
  339.       if (blue > MaxRGB)
  340.         q->blue=MaxRGB;
  341.       else
  342.         if (blue < 0)
  343.           q->blue=0;
  344.         else
  345.           q->blue=blue;
  346.       if (index > 255)
  347.         q->index=255;
  348.       else
  349.         if (index < 0)
  350.           q->index=0;
  351.         else
  352.           q->index=index;
  353.       q->length=0;
  354.       q++;
  355.     }
  356.   }
  357. }
  358.