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 / GSIMAGE.C < prev    next >
C/C++ Source or Header  |  1992-08-08  |  26KB  |  814 lines

  1. /* Copyright (C) 1989, 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. /* gsimage.c */
  21. /* Image procedures for Ghostscript library */
  22. #include "gx.h"
  23. #include "memory_.h"
  24. #include "gpcheck.h"
  25. #include "gserrors.h"
  26. #include "gxfixed.h"
  27. #include "gxarith.h"
  28. #include "gxmatrix.h"
  29. #include "gspaint.h"
  30. #include "gzstate.h"
  31. #include "gzdevice.h"            /* requires gsstate.h */
  32. #include "gzcolor.h"            /* requires gxdevice.h */
  33. #include "gzpath.h"
  34. #include "gxcpath.h"
  35. #include "gxdevmem.h"
  36. #include "gximage.h"
  37.  
  38. /* Exported size of enumerator */
  39. const uint gs_image_enum_sizeof = sizeof(gs_image_enum);
  40.  
  41. /* Forward declarations */
  42. private int image_init_map(P3(byte *, int, const float *));
  43. private int image_init(P9(gs_image_enum *, int, int, int, int, int,
  44.   gs_matrix *, gs_state *, fixed));
  45. /* Procedures for unpacking the input data into 8 bits/sample. */
  46. /* Some of these have been split off to gsimage2. */
  47. private void image_unpack_1(iunpack_proc_args);
  48. extern void image_unpack_1_spread(iunpack_proc_args);
  49. private void image_unpack_2(iunpack_proc_args);
  50. extern void image_unpack_2_spread(iunpack_proc_args);
  51. private void image_unpack_4(iunpack_proc_args);
  52. private void image_unpack_8(iunpack_proc_args);
  53. extern void image_unpack_8_spread(iunpack_proc_args);
  54. extern void image_unpack_12(iunpack_proc_args);
  55. /* The image_render procedures work on fully expanded, complete rows. */
  56. /* These take a height argument, which is an integer > 0; */
  57. /* they return a negative code, or the number of */
  58. /* rows actually processed (which may be less than the height). */
  59. private int image_render_skip(irender_proc_args);
  60. private int image_render_direct(irender_proc_args);
  61. private int image_render_simple(irender_proc_args);
  62. private int image_render_mono(irender_proc_args);
  63. extern int image_render_color(irender_proc_args);
  64.  
  65. /* Set up a gs_color with a transfer-mapped gray sample. */
  66. #define image_set_rgb(rcolor,sample_value)\
  67.   (rcolor.luminance = rcolor.red = rcolor.green = rcolor.blue =\
  68.     gx_map_color_param_byte(pgs, sample_value, gray),\
  69.    gx_color_from_rgb(&rcolor))
  70.  
  71. /* Standard mask tables for spreading input data. */
  72. /* Note that the mask tables depend on the end-orientation of the CPU. */
  73. /* We can't simply define them as byte arrays, because */
  74. /* they might not wind up properly long- or short-aligned. */
  75. #define map4tox(z,a,b,c,d)\
  76.     z, z^a, z^b, z^(a+b),\
  77.     z^c, z^(a+c), z^(b+c), z^(a+b+c),\
  78.     z^d, z^(a+d), z^(b+d), z^(a+b+d),\
  79.     z^(c+d), z^(a+c+d), z^(b+c+d), z^(a+b+c+d)
  80. #if arch_is_big_endian
  81. private const unsigned long map_4x1_to_32[16] =
  82.    {    map4tox(0L, 0xffL, 0xff00L, 0xff0000L, 0xff000000L)    };
  83. private const unsigned long map_4x1_to_32_invert[16] =
  84.    {    map4tox(0xffffffffL, 0xffL, 0xff00L, 0xff0000L, 0xff000000L)    };
  85. #else                    /* !arch_is_big_endian */
  86. private const unsigned long map_4x1_to_32[16] =
  87.    {    map4tox(0L, 0xff000000L, 0xff0000L, 0xff00L, 0xffL)    };
  88. private const unsigned long map_4x1_to_32_invert[16] =
  89.    {    map4tox(0xffffffffL, 0xff000000L, 0xff0000L, 0xff00L, 0xffL)    };
  90. #endif
  91.  
  92. /* Start processing an image */
  93. int
  94. gs_image_init(gs_image_enum *penum, gs_state *pgs,
  95.   int width, int height, int bps, int spp, const float *decode,
  96.   gs_matrix *pmat)
  97. {    int spread;
  98.     int code;
  99.     static const float decode_01[2] = { 0.0, 1.0 };
  100.     if ( pgs->in_cachedevice )
  101.         return_error(gs_error_undefined);
  102.     switch ( spp )
  103.        {
  104.     case 1: case 3: case 4:
  105.         spread = 1; break;
  106.     case -3: case -4:
  107.         spp = -spp; spread = spp; break;
  108.     default:
  109.         return_error(gs_error_rangecheck);
  110.        }
  111.     if ( spp == 1 )
  112.        {    /* Initialize the color table */
  113. #define chtl(i)\
  114.   penum->dev_colors[i].halftone_level
  115.         switch ( bps )
  116.            {
  117.         default:
  118.            {    /* Yes, clearing the entire table is slow, */
  119.             /* but for 8 bit-per-sample images, it's worth it. */
  120.             register gx_device_color *pcht = &penum->dev_colors[0];
  121.             register int n = 64;
  122.             do
  123.                {    pcht[0].halftone_level =
  124.                   pcht[1].halftone_level =
  125.                   pcht[2].halftone_level =
  126.                   pcht[3].halftone_level = -1;
  127.                 pcht += 4;
  128.                }
  129.             while ( --n > 0 );
  130.             break;
  131.            }
  132.         case 4:
  133.             chtl(17) = chtl(2*17) = chtl(3*17) =
  134.               chtl(4*17) = chtl(6*17) = chtl(7*17) =
  135.               chtl(8*17) = chtl(9*17) = chtl(11*17) =
  136.               chtl(12*17) = chtl(13*17) = chtl(14*17) = -1;
  137.             /* falls through */
  138.         case 2:
  139.             chtl(5*17) = chtl(10*17) = -1;
  140.         case 1:
  141.             ;
  142.            }
  143.         /* Pre-map entries 0 and 255. */
  144.            {    gs_color rcolor;
  145.             image_set_rgb(rcolor, 0);
  146.             gx_color_render(&rcolor, &penum->icolor0, pgs);
  147.             image_set_rgb(rcolor, 255);
  148.             gx_color_render(&rcolor, &penum->icolor1, pgs);
  149.            }
  150. #undef chtl
  151.        }
  152.     penum->masked = 0;
  153.     /* Initialize the map from samples to intensities. */
  154.     if ( decode == 0 )
  155.         decode = decode_01;
  156.     if ( bps > 2 || spread != 1 )
  157.         code = image_init_map(&penum->map.to8[0], 1 << bps, decode);
  158.     else
  159.     {    /* The map index encompasses more than one pixel. */
  160.         byte map[4];
  161.         register int i;
  162.         code = image_init_map(&map[0], 1 << bps, decode);
  163.         switch ( bps )
  164.         {
  165.         case 1:
  166.         {    register ulong *p = &penum->map.from4x1to32[0];
  167.             if ( map[0] == 0 && map[1] == 0xff )
  168.                 memcpy((byte *)p, map_4x1_to_32, 16 * 4);
  169.             else if ( map[0] == 0xff && map[1] == 0 )
  170.                 memcpy((byte *)p, map_4x1_to_32_invert, 16 * 4);
  171.             else
  172.               for ( i = 0; i < 16; i++, p++ )
  173.                 ((byte *)p)[0] = map[i >> 3],
  174.                 ((byte *)p)[1] = map[(i >> 2) & 1],
  175.                 ((byte *)p)[2] = map[(i >> 1) & 1],
  176.                 ((byte *)p)[3] = map[i & 1];
  177.         }    break;
  178.         case 2:
  179.         {    register ushort *p = &penum->map.from2x2to16[0];
  180.             for ( i = 0; i < 16; i++, p++ )
  181.                 ((byte *)p)[0] = map[i >> 2],
  182.                 ((byte *)p)[1] = map[i & 3];
  183.         }    break;
  184.         }
  185.     }
  186.     if ( code < 0 ) return code;
  187.     return image_init(penum, width, height, bps, spp, spread,
  188.               pmat, pgs, (fixed)0);
  189. }
  190. /* Construct a mapping table for sample values. */
  191. /* map_size is 2, 4, 16, or 256.  Note that 255 % (map_size - 1) == 0. */
  192. private int
  193. image_init_map(byte *map, int map_size, const float *decode)
  194. {    float min_v = decode[0], max_v = decode[1];
  195.     uint value;
  196.     uint diff;
  197.     byte *limit = map + map_size;
  198.     if ( min_v < 0 || min_v > 1 || max_v < 0 || max_v > 1 )
  199.         return_error(gs_error_rangecheck);
  200.     value = min_v * 0xffffL;
  201.     /* The division in the next statement is exact, */
  202.     /* see the comment above. */
  203.     diff = (max_v - min_v) * (0xffffL / (map_size - 1));
  204.     for ( ; map != limit; map++, value += diff )
  205.         *map = value >> 8;
  206.     return 0;
  207. }
  208.  
  209. /* Start processing a masked image */
  210. int
  211. gs_imagemask_init(gs_image_enum *penum, gs_state *pgs,
  212.   int width, int height, int invert, gs_matrix *pmat, int adjust)
  213. {    /* Initialize color entries 0 and 255. */
  214.     penum->icolor0.halftone_level = 0;
  215.     penum->icolor0.color1 = penum->icolor0.color2 = gx_no_color_index;
  216.     penum->icolor1 = *pgs->dev_color;
  217.     penum->masked = 1;
  218.     memcpy(&penum->map.from4x1to32[0],
  219.            (invert ? map_4x1_to_32_invert : map_4x1_to_32),
  220.            16 * 4);
  221.     return image_init(penum, width, height, 1, 1, 1, pmat, pgs,
  222.               (adjust && pgs->in_cachedevice ?
  223.                float2fixed(0.25) : (fixed)0));
  224. }
  225.  
  226. /* Common setup for image and imagemask. */
  227. /* Caller has set penum->masked, map, dev_colors[]. */
  228. private int
  229. image_init(register gs_image_enum *penum, int width, int height,
  230.   int bps, int spp, int spread, gs_matrix *pmat, gs_state *pgs,
  231.   fixed adjust)
  232. {    int code;
  233.     int index_bps;
  234.     gs_matrix mat;
  235.     gs_fixed_rect clip_box;
  236.     uint bsize = (width + 8) * spp;    /* round up, +1 for end-of-run byte */
  237.     byte *buffer;
  238.     fixed mtx, mty;
  239.     if ( width <= 0 || height < 0 )
  240.         return_error(gs_error_rangecheck);
  241.     switch ( bps )
  242.        {
  243.     case 1: index_bps = 0; break;
  244.     case 2: index_bps = 1; break;
  245.     case 4: index_bps = 2; break;
  246.     case 8: index_bps = 3; break;
  247.     case 12: index_bps = 4; break;
  248.     default: return_error(gs_error_rangecheck);
  249.        }
  250.     if ( height == 0 ) return 0;    /* empty image */
  251.     if (    (code = gs_matrix_invert(pmat, &mat)) < 0 ||
  252.         (code = gs_matrix_multiply(&mat, &ctm_only(pgs), &mat)) < 0
  253.        )    return code;
  254.     buffer = (byte *)gs_malloc(1, bsize, "image buffer");
  255.     if ( buffer == 0 ) return_error(gs_error_VMerror);
  256.     penum->width = width;
  257.     penum->height = height;
  258.     penum->bps = bps;
  259.     penum->spp = spp;
  260.     penum->spread = spread;
  261.     penum->fxx = float2fixed(mat.xx);
  262.     penum->fxy = float2fixed(mat.xy);
  263.     penum->fyx = float2fixed(mat.yx);
  264.     penum->fyy = float2fixed(mat.yy);
  265.     penum->skewed = (penum->fxy | penum->fyx) != 0;
  266.     penum->xcur = mtx = float2fixed(mat.tx);
  267.     penum->ycur = mty = float2fixed(mat.ty);
  268.     penum->pgs = pgs;
  269.     clip_box = pgs->clip_path->path.bbox;    /* box is known to be up to date */
  270.     penum->clip_box = clip_box;
  271.     penum->buffer = buffer;
  272.     penum->buffer_size = bsize;
  273.     penum->bytes_per_row =
  274.         (uint)(((ulong)width * (bps * spp) / spread + 7) >> 3);
  275.     penum->slow_loop = penum->skewed;
  276.     /* If all four extrema of the image fall within the clipping */
  277.     /* rectangle, clipping is never required. */
  278.        {    gs_fixed_rect cbox;
  279.         fixed edx = float2fixed(mat.xx * width);
  280.         fixed edy = float2fixed(mat.yy * height);
  281.         fixed epx, epy, eqx, eqy;
  282.         if ( edx < 0 ) epx = edx, eqx = 0;
  283.         else epx = 0, eqx = edx;
  284.         if ( edy < 0 ) epy = edy, eqy = 0;
  285.         else epy = 0, eqy = edy;
  286.         if ( penum->skewed )
  287.            {    edx = float2fixed(mat.yx * height);
  288.             edy = float2fixed(mat.xy * width);
  289.             if ( edx < 0 ) epx += edx; else eqx += edx;
  290.             if ( edy < 0 ) epy += edy; else eqy += edy;
  291.            }
  292.         gx_cpath_box_for_check(pgs->clip_path, &cbox);
  293.         penum->never_clip =
  294.             mtx + epx >= cbox.p.x && mtx + eqx <= cbox.q.x &&
  295.             mty + epy >= cbox.p.y && mty + eqy <= cbox.q.y;
  296.         if_debug7('b',
  297.               "[b]Image: cbox=(%g,%g),(%g,%g)\n     mt=(%g,%g) never_clip=%d\n",
  298.               fixed2float(cbox.p.x), fixed2float(cbox.p.y),
  299.               fixed2float(cbox.q.x), fixed2float(cbox.q.y),
  300.               fixed2float(mtx), fixed2float(mty),
  301.               penum->never_clip);
  302.        }
  303.        {    static void (*procs[5])(iunpack_proc_args) = {
  304.             image_unpack_1, image_unpack_2,
  305.             image_unpack_4, image_unpack_8, image_unpack_12
  306.            };
  307.         static void (*spread_procs[5])(iunpack_proc_args) = {
  308.             image_unpack_1_spread, image_unpack_2_spread,
  309.             image_unpack_4, image_unpack_8_spread,
  310.             image_unpack_12
  311.            };
  312.         penum->slow_loop |=
  313.             /* Use slow loop for imagemask with a halftone */
  314.             (penum->masked &&
  315.              !color_is_pure(pgs->dev_color));
  316.         if ( pgs->in_charpath )
  317.             penum->render = image_render_skip;
  318.         else if ( spp == 1 && bps == 1 && !penum->slow_loop &&
  319.               (penum->masked ||
  320.                (color_is_pure(&penum->icolor0) &&
  321.                 color_is_pure(&penum->icolor1)))
  322.            )
  323.             penum->render =
  324.               (fixed2long_rounded(mtx + width * penum->fxx) -
  325.                fixed2long_rounded(mtx) == width ?
  326.                 image_render_direct :
  327.                penum->masked ? image_render_mono :
  328.                image_render_simple);
  329.         else
  330.             penum->render =
  331.               (spp == 1 ? image_render_mono : image_render_color);
  332.         /* The following should just be an assignment of */
  333.         /* a conditional expression, but the Ultrix C compiler */
  334.         /* can't handle it. */
  335.         if ( penum->render == image_render_direct ||
  336.              penum->render == image_render_simple
  337.            )
  338.           { penum->unpack = image_unpack_8;
  339.             /* If the image is 1-for-1 with the device, */
  340.             /* we don't want to spread the samples, */
  341.             /* but we have to reset bps to prevent the buffer */
  342.             /* pointer from being incremented by 8 bytes */
  343.             /* per input byte. */
  344.             penum->bps = 8;
  345.           }
  346.         else if ( spread != 1 )
  347.           penum->unpack = spread_procs[index_bps];
  348.         else
  349.           penum->unpack = procs[index_bps];
  350.        }
  351.     if ( !penum->never_clip )
  352.        {    /* Set up the clipping device. */
  353.         gx_device *dev = (gx_device *)&penum->clip_dev;
  354.         penum->clip_dev = gs_clip_device;
  355.         penum->clip_dev.target = gs_currentdevice(pgs);
  356.         penum->clip_dev.list = pgs->clip_path->list;
  357.         (*dev->procs->open_device)(dev);
  358.        }
  359.     penum->adjust = adjust;
  360.     penum->plane_index = 0;
  361.     penum->byte_in_row = 0;
  362.     penum->y = 0;
  363.     if_debug9('b', "[b]Image: w=%d h=%d %s\n   [%f %f %f %f %f %f]\n",
  364.          width, height,
  365.          (penum->never_clip ? "no clip" : "must clip"),
  366.          mat.xx, mat.xy, mat.yx, mat.yy, mat.tx, mat.ty);
  367.     return 0;
  368. }
  369.  
  370. /* Process the next piece of an image */
  371. int
  372. gs_image_next(register gs_image_enum *penum, byte *dbytes, uint dsize)
  373. {    uint rsize = penum->bytes_per_row;
  374.     uint pos = penum->byte_in_row;
  375.     int width = penum->width;
  376.     uint dleft = dsize;
  377.     uint dpos = 0;
  378.     gs_state *pgs = penum->pgs;
  379.     gx_device *save_dev = 0;
  380.     int code;
  381.     /* Accumulate separated colors, if needed */
  382.     if ( penum->plane_index == 0 )
  383.         penum->plane_size = dsize;
  384.     else if ( dsize != penum->plane_size )
  385.         return_error(gs_error_undefinedresult);
  386.     penum->planes[penum->plane_index] = dbytes;
  387.     if ( ++(penum->plane_index) != penum->spread )
  388.         return 0;
  389.     penum->plane_index = 0;
  390.     /* We've accumulated an entire set of planes. */
  391.     if ( !penum->never_clip )
  392.        {    /* Install the clipping device if needed. */
  393.         gx_device *dev = (gx_device *)&penum->clip_dev;
  394.         save_dev = gs_currentdevice(pgs);
  395.         penum->clip_dev.target = save_dev;
  396.         gx_set_device_only(pgs, dev);
  397.        }
  398.     while ( dleft )
  399.        {    /* Fill up a row, then display it. */
  400.         uint bcount = min(dleft, rsize - pos);
  401.         byte *bptr =
  402.           penum->buffer + (pos << 3) / penum->bps * penum->spread;
  403.         int px;
  404.         for ( px = 0; px < penum->spread; px++ )
  405.           (*penum->unpack)(penum, bptr + px, penum->planes[px] + dpos, bcount, pos);
  406.         pos += bcount;
  407.         dpos += bcount;
  408.         dleft -= bcount;
  409.         if ( pos == rsize )    /* filled an entire row */
  410.            {
  411. #ifdef DEBUG
  412. if ( gs_debug['B'] )
  413.    {            int i, n = width * penum->spp;
  414.             dputs("[B]row:");
  415.             for ( i = 0; i < n; i++ )
  416.                 dprintf1(" %02x", penum->buffer[i]);
  417.             dputs("\n");
  418.    }
  419. #endif
  420.             if ( !penum->skewed )
  421.               { /* Precompute integer y and height, */
  422.                 /* and check for clipping. */
  423.                 fixed yc = penum->ycur, yn;
  424.                 fixed dyy = penum->fyy;
  425.                 fixed adjust = penum->adjust;
  426.                 if ( dyy > 0 )
  427.                   dyy += adjust << 1,
  428.                   yc -= adjust;
  429.                 else
  430.                   dyy = (adjust << 1) - dyy,
  431.                   yc -= dyy - adjust;
  432.                 if ( yc >= penum->clip_box.q.y ) goto mt;
  433.                 yn = yc + dyy;
  434.                 if ( yn <= penum->clip_box.p.y ) goto mt;
  435.                 penum->yci = fixed2int_var_rounded(yc);
  436.                 penum->hci =
  437.                   fixed2int_var_rounded(yn) - penum->yci;
  438.                 if ( penum->hci == 0 ) goto mt;
  439.               }
  440.             code = (*penum->render)(penum, penum->buffer, width * penum->spp, 1);
  441.             if ( code < 0 ) goto err;
  442. mt:            if ( ++(penum->y) == penum->height ) goto end;
  443.             pos = 0;
  444.             penum->xcur += penum->fyx;
  445.             penum->ycur += penum->fyy;
  446.            }
  447.        }
  448.     penum->byte_in_row = pos;
  449.     code = 0;
  450.     goto out;
  451. end:    /* End of data */
  452.     code = 1;
  453.     /* falls through */
  454. err:    /* Error, abort */
  455.     gs_free((char *)penum->buffer, penum->buffer_size, 1, "image buffer");
  456. out:    if ( save_dev != 0 ) gx_set_device_only(pgs, save_dev);
  457.     return code;
  458. }
  459.  
  460. /* ------ Unpacking procedures ------ */
  461.  
  462. private void
  463. image_unpack_1(const gs_image_enum *penum, byte *bptr,
  464.   register const byte *data, uint dsize, uint inpos)
  465. {    register ulong *bufp = (unsigned long *)bptr;
  466.     int left = dsize;
  467.     register const ulong *map = &penum->map.from4x1to32[0];
  468.     register uint b;
  469.     if ( left & 1 )
  470.        {    b = data[0];
  471.         bufp[0] = map[b >> 4];
  472.         bufp[1] = map[b & 0xf];
  473.         data++, bufp += 2;
  474.        }
  475.     left >>= 1;
  476.     while ( left-- )
  477.        {    b = data[0];
  478.         bufp[0] = map[b >> 4];
  479.         bufp[1] = map[b & 0xf];
  480.         b = data[1];
  481.         bufp[2] = map[b >> 4];
  482.         bufp[3] = map[b & 0xf];
  483.         data += 2, bufp += 4;
  484.        }
  485. }
  486.  
  487. private void
  488. image_unpack_2(const gs_image_enum *penum, byte *bptr,
  489.   register const byte *data, uint dsize, uint inpos)
  490. {    register ushort *bufp = (unsigned short *)bptr;
  491.     int left = dsize;
  492.     register const ushort *map = &penum->map.from2x2to16[0];
  493.     while ( left-- )
  494.        {    register unsigned b = *data++;
  495.         *bufp++ = map[b >> 4];
  496.         *bufp++ = map[b & 0xf];
  497.        }
  498. }
  499.  
  500. private void
  501. image_unpack_4(const gs_image_enum *penum, register byte *bufp,
  502.   register const byte *data, uint dsize, uint inpos)
  503. {    register int spread = penum->spread;
  504.     int left = dsize;
  505.     register const byte *map = &penum->map.to8[0];
  506.     while ( left-- )
  507.        {    register unsigned b = *data++;
  508.         *bufp = map[b >> 4]; bufp += spread;
  509.         *bufp = map[b & 0xf]; bufp += spread;
  510.        }
  511. }
  512.  
  513. private void
  514. image_unpack_8(const gs_image_enum *penum, byte *bufp,
  515.   const byte *data, uint dsize, uint inpos)
  516. {    if ( data != bufp ) memcpy(bufp, data, dsize);
  517. }
  518.  
  519. /* ------ Rendering procedures ------ */
  520.  
  521. /* Rendering procedure for ignoring an image.  We still need to iterate */
  522. /* over the samples, because the procedure might have side effects. */
  523. private int
  524. image_render_skip(gs_image_enum *penum, byte *data, uint w, int h)
  525. {    return h;
  526. }
  527.  
  528. /* Rendering procedure for a 1-bit-per-pixel sampled image */
  529. /* with no skewing/rotation or X scaling. */
  530. /* In this case a direct BitBlt is possible. */
  531. private int
  532. image_render_direct(register gs_image_enum *penum, byte *data, uint w, int h)
  533. {    int ix = fixed2int_var_rounded(penum->xcur), iw = w;
  534.     int iy = penum->yci, ih = penum->hci;
  535.     uint raster = (w + 7) >> 3;
  536.     gx_device *dev = penum->pgs->device->info;
  537.     dev_proc_copy_mono((*copy_mono)) = dev->procs->copy_mono;
  538.     gx_color_index
  539.         zero = penum->icolor0.color1,
  540.         one = penum->icolor1.color1;
  541.     if_debug4('b', "[b]direct (%d,%d),(%d,%d)\n", ix, iy, iw, ih);
  542.     /* Check for inverted imagemask */
  543.     if ( penum->map.from4x1to32[0] != 0 )
  544.         zero = penum->icolor1.color1,
  545.         one = penum->icolor0.color1;
  546.        {    /* Do just one row, clipping if necessary. */
  547.         int dy;
  548.         for ( dy = 0; dy < ih; dy++ )
  549.             (*copy_mono)(dev, data, 0, raster, gx_no_bitmap_id,
  550.                 ix, iy + dy, iw, 1, zero, one);
  551.         gp_check_interrupts();
  552.         return 1;
  553.        }
  554. }
  555.  
  556. /* Rendering procedure for a non-mask monobit image with no */
  557. /* skew or rotation and pure colors. */
  558. private int
  559. image_render_simple(gs_image_enum *penum, byte *buffer, uint w, int h)
  560. {    fixed xrun = penum->xcur + fixed_half;
  561.     fixed xl;
  562.     const fixed dxx = penum->fxx;
  563.     const fixed dxx_8 = dxx << 3;
  564.     register const byte *psrc = buffer;
  565.     gx_device *dev = penum->pgs->device->info;
  566.     dev_proc_fill_rectangle((*fill_proc)) = dev->procs->fill_rectangle;
  567.     const int iy = penum->yci, ih = penum->hci;
  568.     gx_color_index
  569.         zero = penum->icolor0.color1,
  570.         one = penum->icolor1.color1;
  571.     int sbit = 7;
  572.     byte *endp = buffer + (w >> 3);
  573.     const int endbit = ~w & 7;
  574.     const byte endmask = 1 << endbit;
  575.     int xi = fixed2int_var(xrun);
  576.     int xni, code;
  577.     byte data;
  578.     if ( penum->map.from4x1to32[0] != 0 )
  579.         zero = penum->icolor1.color1,
  580.         one = penum->icolor0.color1;
  581.     /* Invert the bit following the last valid data bit. */
  582.     if ( endmask == 0x80 ) *endp = ~endp[-1] << 7;
  583.     else if ( *endp & (endmask << 1) ) *endp &= ~endmask;
  584.     else *endp |= endmask;
  585.     data = *psrc;
  586. #define fill_run(color)\
  587.   xni = fixed2int_var(xl);\
  588.   if ( xni > xi )\
  589.    { code = (*fill_proc)(dev, xi, iy, xni-xi, ih, color);\
  590.      if ( code < 0 ) return code;\
  591.    }\
  592.   else if ( xni < xi )\
  593.    { code = (*fill_proc)(dev, xni, iy, xi-xni, ih, color);\
  594.      if ( code < 0 ) return code;\
  595.    }
  596.     /* Pre-fill the scan line with the zero color. */
  597.     /* This halves the number of fill_proc calls. */
  598.     xl = xrun + (dxx * w);
  599.     fill_run(zero);
  600.     xl = xrun;
  601.     /* Loop invariants: data = *psrc; 0<=sbit<=7. */
  602.     do
  603.     {    /* Scan a run of zeros. */
  604.         switch ( sbit )
  605.         {
  606.         case 7:
  607. w7:            if ( data & 0x80 ) { sbit = 7; break; } xl += dxx;
  608.         case 6:
  609.             if ( data & 0x40 ) { sbit = 6; break; } xl += dxx;
  610.         case 5:
  611.             if ( data & 0x20 ) { sbit = 5; break; } xl += dxx;
  612.         case 4:
  613.             if ( data & 0x10 ) { sbit = 4; break; } xl += dxx;
  614.         case 3:
  615.             if ( data & 0x08 ) { sbit = 3; break; } xl += dxx;
  616.         case 2:
  617.             if ( data & 0x04 ) { sbit = 2; break; } xl += dxx;
  618.         case 1:
  619.             if ( data & 0x02 ) { sbit = 1; break; } xl += dxx;
  620.         case 0:
  621.             if ( data & 0x01 ) { sbit = 0; break; } xl += dxx;
  622.             while ( (data = *++psrc) == 0 ) xl += dxx_8;
  623.             goto w7;
  624.         }
  625.         if ( !(psrc < endp || sbit > endbit) )
  626.             break;
  627.         xrun = xl, xi = fixed2int_var(xrun);
  628.         /* Scan a run of ones. */
  629.         while ( (data >> sbit) & 1 )
  630.         {    xl += dxx;
  631.             if ( --sbit < 0 )
  632.             {    sbit = 7;
  633.                 while ( (data = *++psrc) == 0xff )
  634.                     xl += dxx_8;
  635.             }
  636.         }
  637.         fill_run(one);
  638.     } while ( psrc < endp || sbit > endbit );
  639. #undef fill_run
  640.     return 1;
  641. }
  642.  
  643. /* Rendering procedure for the general case of displaying a */
  644. /* monochrome image, dealing with multiple bit-per-sample images, */
  645. /* bits not 1-for-1 with the device, and general transformations. */
  646. /* This procedure handles a single scan line. */
  647. private int
  648. image_render_mono(gs_image_enum *penum, byte *buffer, uint w, int h)
  649. {    gs_state *pgs = penum->pgs;
  650.     const int masked = penum->masked;
  651.     const fixed dxx = penum->fxx;
  652.     fixed xt = penum->xcur;
  653.     gs_color rcolor;
  654.     gx_device_color *pdevc = pgs->dev_color;
  655.     /* Make sure the cache setup matches the graphics state. */
  656.     /* Also determine whether all tiles fit in the cache. */
  657.     int tiles_fit = gx_check_tile_cache(pgs);
  658. #define image_set_gray(sample_value)\
  659.    { pdevc = &penum->dev_colors[sample_value];\
  660.      switch ( pdevc->halftone_level )\
  661.       { default:        /* halftone */\
  662.       if ( !tiles_fit ) gx_color_load(pdevc, pgs); break;\
  663.         case -1:        /* not computed yet */\
  664.       image_set_rgb(rcolor, sample_value);\
  665.       gx_color_render(&rcolor, pdevc, pgs);\
  666.     case 0: ;        /* pure color */\
  667.       }\
  668.    }
  669.     fixed xl = xt;
  670.     register const byte *psrc = buffer;
  671.     byte *endp = buffer + w;
  672.     fixed xrun = xt;        /* x at start of run */
  673.     register byte run = *psrc;    /* run value */
  674.     int htrun =            /* halftone run value */
  675.       (masked ? 255 : -2);
  676.     *endp = ~endp[-1];    /* force end of run */
  677.     gx_set_gray_only(&rcolor, (color_param)0);
  678.     if ( penum->slow_loop )
  679.       { /* Skewed, or imagemask with a halftone. */
  680.         const fixed
  681.           dxy = penum->fxy, dyx = penum->fyx,
  682.           dyy = penum->fyy;
  683.         fixed ytf = penum->ycur;
  684.         fixed yrun = ytf;
  685.         for ( ; ; )
  686.           { if ( *psrc++ != run )
  687.           { /* Fill the region between xrun and xl. */
  688.             if ( run != htrun )
  689.               { if ( run == 0 )
  690.               { if ( masked ) goto trans;
  691.               }
  692.             htrun = run;
  693.             image_set_gray(run);
  694.               }
  695.             gz_fill_pgram_fixed(xrun, yrun, xl - xrun,
  696.                     ytf - yrun, dyx, dyy,
  697.                     pdevc, pgs);
  698. trans:            if ( psrc > endp ) break;
  699.             yrun = ytf;
  700.             xrun = xl;
  701.             run = psrc[-1];
  702.           }
  703.         xl += dxx;
  704.         ytf += dxy;        /* harmless if no skew */
  705.           }
  706.       }
  707.     else            /* fast loop */
  708.       { /* No skew, and not imagemask with a halftone. */
  709.         const fixed adjust = penum->adjust;
  710.         fixed xa = (dxx >= 0 ? adjust : -adjust);
  711.         const int yt = penum->yci, iht = penum->hci;
  712.         gx_device *dev = pgs->device->info;
  713.         dev_proc_fill_rectangle((*fill_proc)) = dev->procs->fill_rectangle;
  714.         dev_proc_tile_rectangle((*tile_proc)) = dev->procs->tile_rectangle;
  715.         dev_proc_copy_mono((*copy_mono_proc)) = dev->procs->copy_mono;
  716.         dev_proc_copy_color((*copy_color_proc)) = dev->procs->copy_color;
  717.         /* If each pixel is likely to fit in a single halftone tile, */
  718.         /* determine that now (tile_offset = offset of row within tile). */
  719.         int tile_offset =
  720.           gx_check_tile_size(pgs,
  721.                  fixed2int_rounded(any_abs(dxx) + (xa << 1)),
  722.                  yt, iht);
  723.         /* Fold the adjustment into xrun and xl, */
  724.         /* including the +0.5 for rounding. */
  725.         xrun = xrun - xa + fixed_half;
  726.         xl = xl + xa + fixed_half;
  727.         xa <<= 1;
  728.         for ( ; ; )
  729.           { /* Skip large constant regions quickly, */
  730.             /* but don't slow down transitions too much. */
  731.             while ( psrc[0] == run )
  732.         { if ( psrc[1] == run )
  733.           { if ( psrc[2] == run )
  734.             { if ( psrc[3] == run )
  735.               { psrc += 4, xl += dxx << 2;
  736.             continue;
  737.               }
  738.               else
  739.                 psrc += 3, xl += (dxx << 1) + dxx;
  740.             }
  741.             else
  742.               psrc += 2, xl += dxx << 1;
  743.           }
  744.           else
  745.             psrc++, xl += dxx;
  746.           break;
  747.         }
  748.         psrc++;
  749.           { /* Fill the region between xrun and xl. */
  750.             int xi = fixed2int_var(xrun);
  751.             int wi = fixed2int_var(xl) - xi;
  752.             int tsx, code;
  753.             gx_bitmap *tile;
  754.             if ( wi <= 0 )
  755.               { if ( wi == 0 ) goto mt;
  756.             xi += wi, wi = -wi;
  757.               }
  758.             switch ( run )
  759.               {
  760.               case 0:
  761.             if ( masked ) goto mt;
  762.             if ( !color_is_pure(&penum->icolor0) ) goto ht;
  763.             code = (*fill_proc)(dev, xi, yt, wi, iht, penum->icolor0.color1);
  764.             break;
  765.               case 255:        /* just for speed */
  766.             if ( !color_is_pure(&penum->icolor1) ) goto ht;
  767.             code = (*fill_proc)(dev, xi, yt, wi, iht, penum->icolor1.color1);
  768.             break;
  769.               default:
  770. ht:            /* Use halftone if needed */
  771.             if ( run != htrun )
  772.               { image_set_gray(run);
  773.                 htrun = run;
  774.               }
  775.             /* We open-code gz_fill_rectangle_open, */
  776.             /* because we've done some of the work for */
  777.             /* halftone tiles in advance. */
  778.             if ( color_is_pure(pdevc) )
  779.               { code = (*fill_proc)(dev, xi, yt, wi, iht, pdevc->color1);
  780.               }
  781.             else if ( tile_offset >= 0 &&
  782.                   (tile = pdevc->tile,
  783.                    (tsx = (xi + pgs->phase_mod.x) % tile->rep_width) + wi <= tile->size.x)
  784.                 )
  785.               { /* The pixel(s) fit(s) in a single tile. */
  786.                 byte *row = tile->data + tile_offset;
  787.                 code = (color_is_color_halftone(pdevc) ?
  788.                     (*copy_color_proc)
  789.                       (dev, row, tsx, tile->raster, gx_no_bitmap_id,
  790.                        xi, yt, wi, iht) :
  791.                     (*copy_mono_proc)
  792.                       (dev, row, tsx, tile->raster, gx_no_bitmap_id,
  793.                        xi, yt, wi, iht,
  794.                        pdevc->color1, pdevc->color2)
  795.                     );
  796.                 gp_check_interrupts();
  797.               }
  798.             else
  799.               { code = (*tile_proc)(dev, pdevc->tile, xi, yt, wi, iht,
  800.                          pdevc->color1, pdevc->color2,
  801.                          pgs->phase_mod.x, pgs->phase_mod.y);
  802.               }
  803.               }
  804.             if ( code < 0 ) return code;
  805. mt:            if ( psrc > endp ) break;
  806.             xrun = xl - xa;    /* original xa << 1 */
  807.             run = psrc[-1];
  808.           }
  809.         xl += dxx;
  810.           }
  811.       }
  812.     return 1;
  813. }
  814.