home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / GSIMAGE2.C < prev    next >
C/C++ Source or Header  |  1992-08-08  |  8KB  |  256 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gsimage2.c */
  21. /* Additional image procedures for Ghostscript library */
  22. /* This file is logically part of gsimage.c; we have split it out */
  23. /* to reduce the code working set. */
  24. #include "gx.h"
  25. #include "memory_.h"
  26. #include "gpcheck.h"
  27. #include "gserrors.h"
  28. #include "gxfixed.h"
  29. #include "gxarith.h"
  30. #include "gxmatrix.h"
  31. #include "gspaint.h"
  32. #include "gzstate.h"
  33. #include "gzdevice.h"            /* requires gsstate.h */
  34. #include "gzcolor.h"            /* requires gxdevice.h */
  35. #include "gzpath.h"
  36. #include "gxcpath.h"
  37. #include "gxdevmem.h"
  38. #include "gximage.h"
  39.  
  40. /* ------ Unpacking procedures ------ */
  41.  
  42. void
  43. image_unpack_1_spread(const gs_image_enum *penum, register byte *bufp,
  44.   register const byte *data, uint dsize, uint inpos)
  45. {    register int spread = penum->spread;
  46.     int left = dsize;
  47.     register const byte *map = &penum->map.to8[0];
  48.     while ( left-- )
  49.        {    register uint b = *data++;
  50.         *bufp = map[b >> 7]; bufp += spread;
  51.         *bufp = map[(b >> 6) & 1]; bufp += spread;
  52.         *bufp = map[(b >> 5) & 1]; bufp += spread;
  53.         *bufp = map[(b >> 4) & 1]; bufp += spread;
  54.         *bufp = map[(b >> 3) & 1]; bufp += spread;
  55.         *bufp = map[(b >> 2) & 1]; bufp += spread;
  56.         *bufp = map[(b >> 1) & 1]; bufp += spread;
  57.         *bufp = map[b & 1]; bufp += spread;
  58.        }
  59. }
  60.  
  61. void
  62. image_unpack_2_spread(const gs_image_enum *penum, register byte *bufp,
  63.   register const byte *data, uint dsize, uint inpos)
  64. {    register int spread = penum->spread;
  65.     int left = dsize;
  66.     register const byte *map = &penum->map.to8[0];
  67.     while ( left-- )
  68.        {    register unsigned b = *data++;
  69.         *bufp = map[b >> 6]; bufp += spread;
  70.         *bufp = map[(b >> 4) & 3]; bufp += spread;
  71.         *bufp = map[(b >> 2) & 3]; bufp += spread;
  72.         *bufp = map[b & 3]; bufp += spread;
  73.        }
  74. }
  75.  
  76. void
  77. image_unpack_8_spread(const gs_image_enum *penum, register byte *bufp,
  78.   register const byte *data, uint dsize, uint inpos)
  79. {    register int spread = penum->spread;
  80.     register int left = dsize;
  81.     register const byte *map = &penum->map.to8[0];
  82.     while ( left-- )
  83.        {    *bufp = map[*data++]; bufp += spread;
  84.        }
  85. }
  86.  
  87. void
  88. image_unpack_12(const gs_image_enum *penum, register byte *bufp,
  89.   register const byte *data, uint dsize, uint inpos)
  90. {    register int spread = penum->spread;
  91.     register int left = dsize;
  92.     register const byte *map = &penum->map.to8[0];
  93.     /* We have to deal with the 3 cases of inpos % 3 separately. */
  94.     /* (In fact, this is the only reason inpos is passed to */
  95.     /* the unpacking procedures at all.) */
  96.     /****** DOESN'T DO MAPPING RIGHT. ******/
  97.     /* Let N = inpos / 3. */
  98.     switch ( inpos % 3 )
  99.        {
  100.     case 1:
  101.         /* bufp points to byte 2N, which was already filled */
  102.         /* with the leftover byte from the previous call. */
  103.         bufp += spread;
  104.         *bufp = *data++ << 4;
  105.         if ( !--left ) return;
  106.     case 2:
  107.         /* bufp points to byte 2N+1, which was half-filled */
  108.         /* with the second leftover byte from the previous call. */
  109.         *bufp = map[*bufp + (*data++ >> 4)];
  110.         --left;
  111.     case 0:
  112.         /* Nothing special to do. */
  113.         ;
  114.        }
  115.     /* Just drop the low 4 bits of each 12. */
  116.     while ( left >= 3 )
  117.        {    *bufp = map[*data];
  118.         bufp += spread;
  119.         *bufp = map[(data[1] << 4) + (data[2] >> 4)];
  120.         bufp += spread;
  121.         data += 3;
  122.        }
  123.     switch ( left )
  124.        {
  125.     case 2:                /* dddddddd xxxxdddd */
  126.         bufp[1] = data[1] << 4;
  127.     case 1:                /* dddddddd */
  128.         *bufp = map[*data];
  129.     case 0:                /* Nothing more to do. */
  130.         ;
  131.        }
  132. }
  133.  
  134. /* ------ Rendering procedures ------ */
  135.  
  136. /* Rendering procedure for handling color images. */
  137. typedef union { struct { byte r, g, b, skip; } v; ulong all; } color_sample;
  138. int
  139. image_render_color(gs_image_enum *penum, byte *buffer, uint w, int h)
  140. {    gs_state *pgs = penum->pgs;
  141.     fixed    dxx = penum->fxx, dxy = penum->fxy,
  142.         dyx = penum->fyx, dyy = penum->fyy;
  143.     int skew = penum->skewed;
  144.     fixed xt = penum->xcur;
  145.     fixed ytf = penum->ycur;
  146.     int yt = penum->yci, iht = penum->hci;
  147.     gs_color rcolor;
  148.     gx_device_color devc1, devc2;
  149.     gx_device_color _ss *spdevc = &devc1;
  150.     gx_device_color _ss *spdevc_next = &devc2;
  151. #define pdevc ((gx_device_color *)spdevc)
  152. #define pdevc_next ((gx_device_color *)spdevc_next)
  153.     int spp = penum->spp;
  154.     fixed xl = xt;
  155.     const byte *psrc = buffer;
  156.     fixed xrun = xt;        /* x at start of run */
  157.     int irun = fixed2int_var_rounded(xrun);    /* int xrun */
  158.     fixed yrun = ytf;        /* y ditto */
  159.     color_sample run;        /* run value */
  160.     color_sample next;        /* next sample value */
  161.     byte *bufend = buffer + w;
  162.     bufend[0] = ~bufend[-spp];    /* force end of run */
  163.     if_debug5('b', "[b]y=%d w=%d xt=%f yt=%f yb=%f\n",
  164.           penum->y, w,
  165.           fixed2float(xt), fixed2float(ytf), fixed2float(ytf + dyy));
  166.     run.all = 0;
  167.     next.all = 0;
  168.     rcolor.red = rcolor.green = rcolor.blue = 0;
  169.     gx_color_from_rgb(&rcolor);
  170.     gx_color_render(&rcolor, pdevc, pgs);
  171.     while ( psrc <= bufend )    /* 1 extra iteration */
  172.                 /* to handle final run */
  173.        {    if ( spp == 4 )        /* cmyk */
  174.            {    switch ( psrc[3] )
  175.                {
  176.             case 0:        /* no black */
  177.                 next.v.r = ~psrc[0];
  178.                 next.v.g = ~psrc[1];
  179.                 next.v.b = ~psrc[2];
  180.                 break;
  181.             case 0xff:    /* all black */
  182.                 next.v.r = next.v.g = next.v.b = 0;
  183.                 break;
  184.             default:
  185.                {    uint black = 0xff - psrc[3];
  186.                 /* The following is equivalent to */
  187.                 /* v * black / 0xff, without the divide. */
  188.                 register uint temp;
  189. #define deduct_black(v)\
  190.   (temp = (v) * black, (temp + (temp >> 8) + 1) >> 8)
  191.                 next.v.r = deduct_black(0xff - psrc[0]);
  192.                 next.v.g = deduct_black(0xff - psrc[1]);
  193.                 next.v.b = deduct_black(0xff - psrc[2]);
  194. #undef deduct_black
  195.                }
  196.                }
  197.             psrc += 4;
  198.            }
  199.         else            /* rgb */
  200.            {    next.v.r = psrc[0];
  201.             next.v.g = psrc[1];
  202.             next.v.b = psrc[2];
  203.             psrc += 3;
  204.            }
  205.         if ( next.all != run.all )
  206.            {    rcolor.red = gx_map_color_param_byte(pgs, next.v.r, red);
  207.             rcolor.green = gx_map_color_param_byte(pgs, next.v.g, green);
  208.             rcolor.blue = gx_map_color_param_byte(pgs, next.v.b, blue);
  209.             gx_color_from_rgb(&rcolor);
  210.             gx_color_render(&rcolor, pdevc_next, pgs);
  211.             if_debug9('B', "[B]0x%x,0x%x,0x%x -> 0x%x,0x%x,0x%x -> %ld,%ld,%d\n",
  212.                 next.v.r, next.v.g, next.v.b,
  213.                 rcolor.red, rcolor.green, rcolor.blue,
  214.                 pdevc_next->color1, pdevc_next->color2,
  215.                 pdevc_next->halftone_level);
  216.             /* Even though the supplied colors don't match, */
  217.             /* the device colors might. */
  218.             if ( devc1.color1 != devc2.color1 ||
  219.                  devc1.halftone_level != devc2.halftone_level ||
  220.                  (devc1.halftone_level &&
  221.                   devc1.color2 != devc2.color2) ||
  222.                  psrc > bufend    /* force end of last run */
  223.                )
  224.                {    /* Fill the region between */
  225.                 /* xrun/irun and xl */
  226.                 gx_device_color _ss *sptemp;
  227.                 int code;
  228.                 if ( skew )
  229.                {    /* Parallelogram */
  230.                 code = gz_fill_pgram_fixed(xrun, yrun,
  231.                     xl - xrun, ytf - yrun, dyx, dyy,
  232.                     pdevc, pgs);
  233.                 xrun = xl;
  234.                 yrun = ytf;
  235.                }
  236.                 else
  237.                {    /* Rectangle */
  238.                 int xi = irun;
  239.                 int wi = (irun = fixed2int_var_rounded(xl)) - xi;
  240.                 if ( wi < 0 ) xi += wi, wi = -wi;
  241.                 code = gz_fill_rectangle(xi, yt, wi, iht, pdevc, pgs);
  242.                 gp_check_interrupts();
  243.                }
  244.                 if ( code < 0 ) return code;
  245.                 sptemp = spdevc;
  246.                 spdevc = spdevc_next;
  247.                 spdevc_next = sptemp;
  248.                }
  249.             run.all = next.all;
  250.            }
  251.         xl += dxx;
  252.         ytf += dxy;        /* harmless if no skew */
  253.        }
  254.     return 1;
  255. }
  256.