home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GSIMAGE2.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  15KB  |  464 lines

  1. /* Copyright (C) 1992, 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gsimage2.c */
  20. /* Level 2 and color image procedures for Ghostscript library */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "gpcheck.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxfrac.h"
  27. #include "gxarith.h"
  28. #include "gxmatrix.h"
  29. #include "gscspace.h"
  30. #include "gsccolor.h"
  31. #include "gspaint.h"
  32. #include "gzstate.h"
  33. #include "gxcmap.h"
  34. #include "gzpath.h"
  35. #include "gxcpath.h"
  36. #include "gxdevmem.h"
  37. #include "gximage.h"
  38. #include "gzdraw.h"
  39.  
  40. /* ------ Unpacking procedures ------ */
  41.  
  42. void
  43. image_unpack_1_spread(const gs_image_enum *penum, const sample_map *pmap,
  44.   byte *bptr, register const byte *data, uint dsize, uint inpos)
  45. {    register int spread = penum->spread;
  46.     register byte *bufp = bptr + (inpos << 3) * spread;
  47.     int left = dsize;
  48.     register const byte *map = &pmap->table.lookup8[0];
  49.     while ( left-- )
  50.        {    register uint b = *data++;
  51.         *bufp = map[b >> 7]; bufp += spread;
  52.         *bufp = map[(b >> 6) & 1]; bufp += spread;
  53.         *bufp = map[(b >> 5) & 1]; bufp += spread;
  54.         *bufp = map[(b >> 4) & 1]; bufp += spread;
  55.         *bufp = map[(b >> 3) & 1]; bufp += spread;
  56.         *bufp = map[(b >> 2) & 1]; bufp += spread;
  57.         *bufp = map[(b >> 1) & 1]; bufp += spread;
  58.         *bufp = map[b & 1]; bufp += spread;
  59.        }
  60. }
  61.  
  62. void
  63. image_unpack_2_spread(const gs_image_enum *penum, const sample_map *pmap,
  64.   byte *bptr, register const byte *data, uint dsize, uint inpos)
  65. {    register int spread = penum->spread;
  66.     register byte *bufp = bptr + (inpos << 2) * spread;
  67.     int left = dsize;
  68.     register const byte *map = &pmap->table.lookup8[0];
  69.     while ( left-- )
  70.        {    register unsigned b = *data++;
  71.         *bufp = map[b >> 6]; bufp += spread;
  72.         *bufp = map[(b >> 4) & 3]; bufp += spread;
  73.         *bufp = map[(b >> 2) & 3]; bufp += spread;
  74.         *bufp = map[b & 3]; bufp += spread;
  75.        }
  76. }
  77.  
  78. void
  79. image_unpack_8_spread(const gs_image_enum *penum, const sample_map *pmap,
  80.   byte *bptr, register const byte *data, uint dsize, uint inpos)
  81. {    register int spread = penum->spread;
  82.     register byte *bufp = bptr + inpos * spread;
  83.     register int left = dsize;
  84.     register const byte *map = &pmap->table.lookup8[0];
  85.     while ( left-- )
  86.        {    *bufp = map[*data++]; bufp += spread;
  87.        }
  88. }
  89.  
  90. void
  91. image_unpack_12(const gs_image_enum *penum, const sample_map *pmap,
  92.   byte *bptr, register const byte *data, uint dsize, uint inpos)
  93. {    register int spread = penum->spread;
  94.     register frac *bufp = (frac *)(bptr + inpos * 2 / 3 * spread);
  95. #define inc_bufp(bp, n) bp = (frac *)((byte *)(bp) + (n))
  96.     register uint sample;
  97.     register int left = dsize;
  98.     static const frac bits2frac_4[16] = {
  99. #define frac15(n) ((frac_1 / 15) * (n))
  100.         frac15(0), frac15(1), frac15(2), frac15(3),
  101.         frac15(4), frac15(5), frac15(6), frac15(7),
  102.         frac15(8), frac15(9), frac15(10), frac15(11),
  103.         frac15(12), frac15(13), frac15(14), frac15(15)
  104. #undef frac15
  105.     };
  106.     /* We have to deal with the 3 cases of inpos % 3 individually. */
  107.     /* Let N = inpos / 3. */
  108.     switch ( inpos % 3 )
  109.     {
  110.     case 1:
  111.         /* bufp points to frac N, which was already filled */
  112.         /* with the leftover byte from the previous call. */
  113.         sample = (frac2byte(*bufp) << 4) + (*data >> 4);
  114.         *bufp = bits2frac(sample, 12);
  115.         inc_bufp(bufp, spread);
  116.         *bufp = bits2frac_4[*data++ & 0xf];
  117.         if ( !--left ) return;
  118.     case 2:
  119.         /* bufp points to frac N+1, which was half-filled */
  120.         /* with the second leftover byte from the previous call. */
  121.         sample = (frac2bits(*bufp, 4) << 8) + *data++;
  122.         *bufp = bits2frac(sample, 12);
  123.         inc_bufp(bufp, spread);
  124.         --left;
  125.     case 0:
  126.         /* Nothing special to do. */
  127.         ;
  128.     }
  129.     while ( left >= 3 )
  130.     {    sample = ((uint)*data << 4) + (data[1] >> 4);
  131.         *bufp = bits2frac(sample, 12);
  132.         inc_bufp(bufp, spread);
  133.         sample = ((uint)(data[1] & 0xf) << 8) + data[2];
  134.         *bufp = bits2frac(sample, 12);
  135.         inc_bufp(bufp, spread);
  136.         data += 3;
  137.         left -= 3;
  138.     }
  139.     /* Handle trailing bytes. */
  140.     switch ( left )
  141.     {
  142.     case 2:                /* dddddddd ddddxxxx */
  143.         sample = ((uint)*data << 4) + (data[1] >> 4);
  144.         *bufp = bits2frac(sample, 12);
  145.         inc_bufp(bufp, spread);
  146.         *bufp = bits2frac_4[data[1] & 0xf];
  147.         break;
  148.     case 1:                /* dddddddd */
  149.         sample = (uint)*data << 4;
  150.         *bufp = bits2frac(sample, 12);
  151.         break;
  152.     case 0:                /* Nothing more to do. */
  153.         ;
  154.     }
  155. }
  156.  
  157. /* ------ Rendering procedures ------ */
  158.  
  159. /* Compare two device colors for equality. */
  160. #define dev_color_eq(devc1, devc2)\
  161.   (devc1.type == &gx_dc_pure ?\
  162.    devc2.type == &gx_dc_pure &&\
  163.    devc1.colors.pure == devc2.colors.pure :\
  164.    devc1.type == &gx_dc_ht_binary ?\
  165.    devc2.type == &gx_dc_ht_binary &&\
  166.    devc1.colors.binary.color[0] == devc2.colors.binary.color[0] &&\
  167.    devc1.colors.binary.color[1] == devc2.colors.binary.color[1] &&\
  168.    devc1.colors.binary.b_level == devc2.colors.binary.b_level :\
  169.    0)
  170.  
  171. /* Render a color image with 8 or fewer bits per sample. */
  172. typedef union {
  173.     byte v[4];
  174.     bits32 all;        /* for fast comparison & clearing */
  175. } color_samples;
  176. int
  177. image_render_color(gs_image_enum *penum, byte *buffer, uint w, int h)
  178. {    gs_state *pgs = penum->pgs;
  179.     fixed    dxx = penum->fxx, dxy = penum->fxy,
  180.         dyx = penum->fyx, dyy = penum->fyy;
  181.     int skew = penum->skewed;
  182.     fixed xt = penum->xcur;
  183.     fixed ytf = penum->ycur;
  184.     int yt = penum->yci, iht = penum->hci;
  185.     gs_color_space *pcs = pgs->color_space;
  186.     cs_proc_remap_color((*remap_color)) = pcs->type->remap_color;
  187.     gs_client_color cc;
  188.     int device_color = penum->device_color;
  189.     cmap_proc_rgb((*map_rgb)) = pgs->cmap_procs->map_rgb;
  190.     cmap_proc_cmyk((*map_cmyk)) = pgs->cmap_procs->map_cmyk;
  191.     gx_device_color devc1, devc2;
  192.     gx_device_color _ss *spdevc = &devc1;
  193.     gx_device_color _ss *spdevc_next = &devc2;
  194. #define pdevc ((gx_device_color *)spdevc)
  195. #define pdevc_next ((gx_device_color *)spdevc_next)
  196.     int spp = penum->spp;
  197.     fixed xl = xt;
  198.     const byte *psrc = buffer;
  199.     fixed xrun = xt;        /* x at start of run */
  200.     int irun = fixed2int_var_rounded(xrun);    /* int xrun */
  201.     fixed yrun = ytf;        /* y ditto */
  202.     color_samples run;        /* run value */
  203.     color_samples next;        /* next sample value */
  204.     byte *bufend = buffer + w;
  205.     bufend[0] = ~bufend[-spp];    /* force end of run */
  206.     if_debug5('b', "[b]y=%d w=%d xt=%f yt=%f yb=%f\n",
  207.           penum->y, w,
  208.           fixed2float(xt), fixed2float(ytf), fixed2float(ytf + dyy));
  209.     run.all = 0;
  210.     next.all = 0;
  211.     cc.paint.values[0] = cc.paint.values[1] =
  212.       cc.paint.values[2] = cc.paint.values[3] = 0;
  213.     cc.pattern = 0;
  214.     (*remap_color)(&cc, pcs, pdevc, pgs);
  215.     run.v[0] = ~psrc[0];        /* force remap */
  216.     while ( psrc <= bufend )    /* 1 extra iteration */
  217.                 /* to handle final run */
  218.     {    next.v[0] = psrc[0];
  219.         next.v[1] = psrc[1];
  220.         next.v[2] = psrc[2];
  221.         if ( spp == 4 )        /* cmyk */
  222.         {    next.v[3] = psrc[3];
  223.             psrc += 4;
  224.             if ( next.all == run.all ) goto inc;
  225.             if ( device_color )
  226.             {    (*map_cmyk)(byte2frac(next.v[0]),
  227.                     byte2frac(next.v[1]),
  228.                     byte2frac(next.v[2]),
  229.                     byte2frac(next.v[3]),
  230.                     pdevc_next, pgs);
  231.                 goto f;
  232.             }
  233.             else
  234.             {    decode_sample(next.v[3], cc, 3);
  235.                 if_debug1('B', "[B]cc[3]=%g\n",
  236.                       cc.paint.values[3]);
  237.             }
  238.         }
  239.         else            /* rgb */
  240.         {    psrc += 3;
  241.             if ( next.all == run.all ) goto inc;
  242.             if ( device_color )
  243.             {    (*map_rgb)(byte2frac(next.v[0]),
  244.                     byte2frac(next.v[1]),
  245.                     byte2frac(next.v[2]),
  246.                     pdevc_next, pgs);
  247.                 goto f;
  248.             }
  249.         }
  250.         decode_sample(next.v[0], cc, 0);
  251.         decode_sample(next.v[1], cc, 1);
  252.         decode_sample(next.v[2], cc, 2);
  253.         if_debug3('B', "[B]cc[0..2]=%g,%g,%g\n",
  254.               cc.paint.values[0], cc.paint.values[1],
  255.               cc.paint.values[2]);
  256.         (*remap_color)(&cc, pcs, pdevc_next, pgs);
  257. f:        if_debug7('B', "[B]0x%x,0x%x,0x%x,0x%x -> %ld,%ld,0x%x\n",
  258.             next.v[0], next.v[1], next.v[2], next.v[3],
  259.             pdevc_next->colors.binary.color[0],
  260.             pdevc_next->colors.binary.color[1],
  261.             (uint)pdevc_next->type);
  262.         /* Even though the supplied colors don't match, */
  263.         /* the device colors might. */
  264.         if ( !dev_color_eq(devc1, devc2) ||
  265.              psrc > bufend    /* force end of last run */
  266.            )
  267.         {    /* Fill the region between */
  268.             /* xrun/irun and xl */
  269.             gx_device_color _ss *sptemp;
  270.             int code;
  271.             if ( skew )
  272.             {    /* Parallelogram */
  273.                 code = gz_fill_pgram_fixed(xrun, yrun,
  274.                     xl - xrun, ytf - yrun, dyx, dyy,
  275.                     pdevc, pgs);
  276.                 xrun = xl;
  277.                 yrun = ytf;
  278.             }
  279.                 else
  280.             {    /* Rectangle */
  281.                 int xi = irun;
  282.                 int wi = (irun = fixed2int_var_rounded(xl)) - xi;
  283.                 if ( wi < 0 ) xi += wi, wi = -wi;
  284.                 code = gx_fill_rectangle(xi, yt, wi, iht,
  285.                              pdevc, pgs);
  286.             }
  287.             if ( code < 0 )
  288.                 return code;
  289.             sptemp = spdevc;
  290.             spdevc = spdevc_next;
  291.             spdevc_next = sptemp;
  292.         }
  293.         run.all = next.all;
  294. inc:        xl += dxx;
  295.         ytf += dxy;        /* harmless if no skew */
  296.     }
  297.     return 1;
  298. }
  299.  
  300. /* ---------------- Rendering for 12-bit samples ---------------- */
  301.  
  302. #define decode_frac(frac_value, cc, i)\
  303.   cc.paint.values[i] =\
  304.     penum->map[i].decode_base + (frac_value) * penum->map[i].decode_factor
  305.  
  306. /* Render an image with more than 8 bits per sample. */
  307. /* The samples have been expanded into fracs. */
  308. #define longs_per_4_fracs (arch_sizeof_frac * 4 / arch_sizeof_long)
  309. typedef union {
  310.     frac v[4];
  311.     long all[longs_per_4_fracs];        /* for fast comparison */
  312. } color_fracs;
  313. #if longs_per_4_fracs == 1
  314. #  define color_frac_eq(f1, f2)\
  315.      ((f1).all[0] == (f2).all[0])
  316. #else
  317. #if longs_per_4_fracs == 2
  318. #  define color_frac_eq(f1, f2)\
  319.      ((f1).all[0] == (f2).all[0] && (f1).all[1] == (f2).all[1])
  320. #endif
  321. #endif
  322. int
  323. image_render_frac(gs_image_enum *penum, byte *buffer, uint w, int h)
  324. {    gs_state *pgs = penum->pgs;
  325.     fixed    dxx = penum->fxx, dxy = penum->fxy,
  326.         dyx = penum->fyx, dyy = penum->fyy;
  327.     int skew = penum->skewed;
  328.     fixed xt = penum->xcur;
  329.     fixed ytf = penum->ycur;
  330.     int yt = penum->yci, iht = penum->hci;
  331.     gs_color_space *pcs = pgs->color_space;
  332.     cs_proc_remap_color((*remap_color)) = pcs->type->remap_color;
  333.     gs_client_color cc;
  334.     int device_color = penum->device_color;
  335.     cmap_proc_rgb((*map_rgb)) = pgs->cmap_procs->map_rgb;
  336.     cmap_proc_cmyk((*map_cmyk)) = pgs->cmap_procs->map_cmyk;
  337.     gx_device_color devc1, devc2;
  338.     gx_device_color _ss *spdevc = &devc1;
  339.     gx_device_color _ss *spdevc_next = &devc2;
  340. #define pdevc ((gx_device_color *)spdevc)
  341. #define pdevc_next ((gx_device_color *)spdevc_next)
  342.     int spp = penum->spp;
  343.     fixed xl = xt;
  344.     const frac *psrc = (frac *)buffer;
  345.     fixed xrun = xt;        /* x at start of run */
  346.     int irun = fixed2int_var_rounded(xrun);    /* int xrun */
  347.     fixed yrun = ytf;        /* y ditto */
  348.     color_fracs run;        /* run value */
  349.     color_fracs next;        /* next sample value */
  350.     frac *bufend = (frac *)buffer + w;
  351.     bufend[0] = ~bufend[-spp];    /* force end of run */
  352.     if_debug5('b', "[b]y=%d w=%d xt=%f yt=%f yb=%f\n",
  353.           penum->y, w,
  354.           fixed2float(xt), fixed2float(ytf), fixed2float(ytf + dyy));
  355.     run.v[0] = run.v[1] = run.v[2] = run.v[3] = 0;
  356.     next.v[0] = next.v[1] = next.v[2] = next.v[3] = 0;
  357.     cc.paint.values[0] = cc.paint.values[1] =
  358.       cc.paint.values[2] = cc.paint.values[3] = 0;
  359.     cc.pattern = 0;
  360.     (*remap_color)(&cc, pcs, pdevc, pgs);
  361.     run.v[0] = ~psrc[0];        /* force remap */
  362.  
  363.     while ( psrc <= bufend )    /* 1 extra iteration */
  364.                 /* to handle final run */
  365.     {    next.v[0] = psrc[0];
  366.         switch ( spp )
  367.         {
  368.         case 4:            /* cmyk */
  369.             next.v[1] = psrc[1];
  370.             next.v[2] = psrc[2];
  371.             next.v[3] = psrc[3];
  372.             psrc += 4;
  373.             if ( color_frac_eq(next, run) ) goto inc;
  374.             if ( device_color )
  375.             {    (*map_cmyk)(next.v[0], next.v[1],
  376.                         next.v[2], next.v[3],
  377.                         pdevc_next, pgs);
  378.                 goto f;
  379.             }
  380.             decode_frac(next.v[0], cc, 0);
  381.             decode_frac(next.v[1], cc, 1);
  382.             decode_frac(next.v[2], cc, 2);
  383.             decode_frac(next.v[3], cc, 3);
  384.             if_debug4('B', "[B]cc[0..3]=%g,%g,%g,%g\n",
  385.                   cc.paint.values[0], cc.paint.values[1],
  386.                   cc.paint.values[2], cc.paint.values[3]);
  387.             if_debug1('B', "[B]cc[3]=%g\n",
  388.                   cc.paint.values[3]);
  389.             break;
  390.         case 3:            /* rgb */
  391.             next.v[1] = psrc[1];
  392.             next.v[2] = psrc[2];
  393.             psrc += 3;
  394.             if ( color_frac_eq(next, run) ) goto inc;
  395.             if ( device_color )
  396.             {    (*map_rgb)(next.v[0], next.v[1],
  397.                        next.v[2], pdevc_next, pgs);
  398.                 goto f;
  399.             }
  400.             decode_frac(next.v[0], cc, 0);
  401.             decode_frac(next.v[1], cc, 1);
  402.             decode_frac(next.v[2], cc, 2);
  403.             if_debug3('B', "[B]cc[0..2]=%g,%g,%g\n",
  404.                   cc.paint.values[0], cc.paint.values[1],
  405.                   cc.paint.values[2]);
  406.             break;
  407.         case 1:            /* gray */
  408.             psrc++;
  409.             if ( next.v[0] == run.v[0] ) goto inc;
  410.             if ( device_color )
  411.             {    (*map_rgb)(next.v[0], next.v[0],
  412.                        next.v[0], pdevc_next, pgs);
  413.                 goto f;
  414.             }
  415.             decode_frac(next.v[0], cc, 0);
  416.             if_debug1('B', "[B]cc[0]=%g\n",
  417.                   cc.paint.values[0]);
  418.             break;
  419.         }
  420.         (*remap_color)(&cc, pcs, pdevc_next, pgs);
  421. f:        if_debug7('B', "[B]0x%x,0x%x,0x%x,0x%x -> %ld,%ld,0x%x\n",
  422.             next.v[0], next.v[1], next.v[2], next.v[3],
  423.             pdevc_next->colors.binary.color[0],
  424.             pdevc_next->colors.binary.color[1],
  425.             (uint)pdevc_next->type);
  426.         /* Even though the supplied colors don't match, */
  427.         /* the device colors might. */
  428.         if ( !dev_color_eq(devc1, devc2) ||
  429.              psrc > bufend    /* force end of last run */
  430.            )
  431.         {    /* Fill the region between */
  432.             /* xrun/irun and xl */
  433.             gx_device_color _ss *sptemp;
  434.             int code;
  435.             if ( skew )
  436.             {    /* Parallelogram */
  437.                 code = gz_fill_pgram_fixed(xrun, yrun,
  438.                     xl - xrun, ytf - yrun, dyx, dyy,
  439.                     pdevc, pgs);
  440.                 xrun = xl;
  441.                 yrun = ytf;
  442.             }
  443.                 else
  444.             {    /* Rectangle */
  445.                 int xi = irun;
  446.                 int wi = (irun = fixed2int_var_rounded(xl)) - xi;
  447.                 if ( wi < 0 ) xi += wi, wi = -wi;
  448.                 code = gx_fill_rectangle(xi, yt, wi, iht,
  449.                              pdevc, pgs);
  450.             }
  451.             if ( code < 0 )
  452.                 return code;
  453.             sptemp = spdevc;
  454.             spdevc = spdevc_next;
  455.             spdevc_next = sptemp;
  456.         }
  457.         run = next;
  458. inc:        xl += dxx;
  459.         ytf += dxy;        /* harmless if no skew */
  460.     }
  461.     return 1;
  462. }
  463.  
  464.