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

  1. /* Copyright (C) 1989, 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. /* gdevmem2.c */
  20. /* 8-and-more-bit-per-pixel "memory" (stored bitmap) devices */
  21. /* for Ghostscript library. */
  22. #include "memory_.h"
  23. #include "gx.h"
  24. #include "gxdevice.h"
  25. #include "gxdevmem.h"            /* semi-public definitions */
  26. #include "gdevmem.h"            /* private definitions */
  27.  
  28. /* ------ Generic procedures ------ */
  29.  
  30. /* Copy a rectangle of bytes from a source to a destination. */
  31. #undef chunk
  32. #define chunk byte
  33. private int
  34. copy_byte_rect(gx_device_memory *dev, const byte *source, int sraster,
  35.   int offset, int y, int byte_count, int h)
  36. {    uint draster = dev->raster;
  37.     byte *dest = scan_line_base(dev, y) + offset;
  38.     while ( h-- > 0 )
  39.        {    memcpy(dest, source, byte_count);
  40.         source += sraster;
  41.         dest += draster;
  42.        }
  43.     return 0;
  44. }
  45.  
  46. /* Map a r-g-b color to a color index. */
  47. /* This requires searching the palette. */
  48. gx_color_index
  49. mem_mapped_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  50.   gx_color_value b)
  51. {    byte br = gx_color_value_to_byte(r);
  52.     byte bg = gx_color_value_to_byte(g);
  53.     byte bb = gx_color_value_to_byte(b);
  54.     register const byte *pptr = mdev->palette.data;
  55.     int cnt = mdev->palette.size;
  56.     const byte *which;
  57.     int best = 256*3;
  58.     while ( (cnt -= 3) >= 0 )
  59.        {    register int diff = *pptr - br;
  60.         if ( diff < 0 ) diff = -diff;
  61.         if ( diff < best )    /* quick rejection */
  62.            {    int dg = pptr[1] - bg;
  63.             if ( dg < 0 ) dg = -dg;
  64.             if ( (diff += dg) < best )    /* quick rejection */
  65.                {    int db = pptr[2] - bb;
  66.                 if ( db < 0 ) db = -db;
  67.                 if ( (diff += db) < best )
  68.                     which = pptr, best = diff;
  69.                }
  70.            }
  71.         pptr += 3;
  72.        }
  73.     return (gx_color_index)((which - mdev->palette.data) / 3);
  74. }
  75.  
  76. /* Map a color index to a r-g-b color. */
  77. int
  78. mem_mapped_map_color_rgb(gx_device *dev, gx_color_index color,
  79.   gx_color_value prgb[3])
  80. {    const byte *pptr = mdev->palette.data + (int)color * 3;
  81.     prgb[0] = gx_color_value_from_byte(pptr[0]);
  82.     prgb[1] = gx_color_value_from_byte(pptr[1]);
  83.     prgb[2] = gx_color_value_from_byte(pptr[2]);
  84.     return 0;
  85. }
  86.  
  87. /* ------ Mapped 8-bit color ------ */
  88.  
  89. /* Procedures */
  90. declare_mem_procs(mem_mapped8_copy_mono, mem_mapped8_copy_color, mem_mapped8_fill_rectangle);
  91.  
  92. /* The device descriptor. */
  93. /* The instance is exported for gdevmem1.c. */
  94. const gx_device_memory far_data mem_mapped8_color_device =
  95.   mem_device("image(8)", 8, 0,
  96.     mem_mapped_map_rgb_color, mem_mapped_map_color_rgb,
  97.     mem_mapped8_copy_mono, mem_mapped8_copy_color, mem_mapped8_fill_rectangle);
  98.  
  99. /* Convert x coordinate to byte offset in scan line. */
  100. #undef x_to_byte
  101. #define x_to_byte(x) (x)
  102.  
  103. /* Fill a rectangle with a color. */
  104. private int
  105. mem_mapped8_fill_rectangle(gx_device *dev,
  106.   int x, int y, int w, int h, gx_color_index color)
  107. {    declare_scan_ptr(dest);
  108.     fit_fill(dev, x, y, w, h);
  109.     setup_rect(dest);
  110.     while ( h-- > 0 )
  111.        {    memset(dest, (byte)color, w);
  112.         inc_chunk_ptr(dest, draster);
  113.        }
  114.     return 0;
  115. }
  116.  
  117. /* Copy a monochrome bitmap. */
  118. /* We split up this procedure because of limitations in the bcc32 compiler. */
  119. private void mapped8_copy01(P9(chunk *, const byte *, int, int, uint,
  120.   int, int, byte, byte));
  121. private void mapped8_copyN1(P8(chunk *, const byte *, int, int, uint,
  122.   int, int, byte));
  123. private void mapped8_copy0N(P8(chunk *, const byte *, int, int, uint,
  124.   int, int, byte));
  125. private int
  126. mem_mapped8_copy_mono(gx_device *dev,
  127.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  128.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  129. {    const byte *line;
  130.     int first_bit;
  131.     declare_scan_ptr(dest);
  132.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  133.     setup_rect(dest);
  134.     line = base + (sourcex >> 3);
  135.     first_bit = 0x80 >> (sourcex & 7);
  136. #define is_color(c) ((int)(c) != (int)gx_no_color_index)
  137.     if ( is_color(one) )
  138.     {    if ( is_color(zero) )
  139.           mapped8_copy01(dest, line, first_bit, sraster, draster,
  140.                  w, h, (byte)zero, (byte)one);
  141.         else
  142.           mapped8_copyN1(dest, line, first_bit, sraster, draster,
  143.                  w, h, (byte)one);
  144.     }
  145.     else if ( is_color(zero) )
  146.       mapped8_copy0N(dest, line, first_bit, sraster, draster,
  147.              w, h, (byte)zero);
  148. #undef is_color
  149.     return 0;
  150. }
  151. /* Macros for copy loops */
  152. #define COPY_BEGIN\
  153.     while ( h-- > 0 )\
  154.     {    register byte *pptr = dest;\
  155.         const byte *sptr = line;\
  156.         register int sbyte = *sptr;\
  157.         register uint bit = first_bit;\
  158.         int count = w;\
  159.         do\
  160.         {
  161. #define COPY_END\
  162.             if ( (bit >>= 1) == 0 )\
  163.                 bit = 0x80, sbyte = *++sptr;\
  164.             pptr++;\
  165.         }\
  166.         while ( --count > 0 );\
  167.         line += sraster;\
  168.         inc_chunk_ptr(dest, draster);\
  169.     }
  170. /* Halftone coloring */
  171. private void
  172. mapped8_copy01(chunk *dest, const byte *line, int first_bit,
  173.   int sraster, uint draster, int w, int h, byte b0, byte b1)
  174. {    COPY_BEGIN
  175.     *pptr = (sbyte & bit ? b1 : b0);
  176.     COPY_END
  177. }
  178. /* Stenciling */
  179. private void
  180. mapped8_copyN1(chunk *dest, const byte *line, int first_bit,
  181.   int sraster, uint draster, int w, int h, byte b1)
  182. {    COPY_BEGIN
  183.     if ( sbyte & bit )
  184.       *pptr = b1;
  185.     COPY_END
  186. }
  187. /* Reverse stenciling (probably never used) */
  188. private void
  189. mapped8_copy0N(chunk *dest, const byte *line, int first_bit,
  190.   int sraster, uint draster, int w, int h, byte b0)
  191. {    COPY_BEGIN
  192.     if ( !(sbyte & bit) )
  193.       *pptr = b0;
  194.     COPY_END
  195. }
  196. #undef COPY_BEGIN
  197. #undef COPY_END
  198.  
  199. /* Copy a color bitmap. */
  200. private int
  201. mem_mapped8_copy_color(gx_device *dev,
  202.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  203.   int x, int y, int w, int h)
  204. {    fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  205.     return copy_byte_rect(mdev, base + x_to_byte(sourcex), sraster,
  206.         x_to_byte(x), y, x_to_byte(w), h);
  207. }
  208.  
  209. /* ------ 16-bit true color ------ */
  210. /* The 16 bits are divided 5 for red, 6 for green, and 5 for blue. */
  211. /* Note that the bits must always be kept in big-endian order. */
  212.  
  213. /* Procedures */
  214. declare_mem_map_procs(mem_true16_map_rgb_color, mem_true16_map_color_rgb);
  215. declare_mem_procs(mem_true16_copy_mono, mem_true16_copy_color, mem_true16_fill_rectangle);
  216.  
  217. /* The device descriptor. */
  218. /* The instance is exported for gdevmem1.c. */
  219. const gx_device_memory far_data mem_true16_color_device =
  220.   mem_device("image(16)", 16, 0,
  221.     mem_true16_map_rgb_color, mem_true16_map_color_rgb,
  222.     mem_true16_copy_mono, mem_true16_copy_color, mem_true16_fill_rectangle);
  223.  
  224. /* Map a r-g-b color to a color index. */
  225. private gx_color_index
  226. mem_true16_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  227.   gx_color_value b)
  228. {    return ((r >> (gx_color_value_bits - 5)) << 11) +
  229.         ((g >> (gx_color_value_bits - 6)) << 5) +
  230.         (b >> (gx_color_value_bits - 5));
  231. }
  232.  
  233. /* Map a color index to a r-g-b color. */
  234. private int
  235. mem_true16_map_color_rgb(gx_device *dev, gx_color_index color,
  236.   gx_color_value prgb[3])
  237. {    ushort value;
  238.     value = color >> 11;
  239.     prgb[0] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
  240.     value = (color >> 6) & 0x7f;
  241.     prgb[1] = ((value << 10) + (value << 4) + (value >> 2)) >> (16 - gx_color_value_bits);
  242.     value = color & 0x3f;
  243.     prgb[2] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
  244.     return 0;
  245. }
  246.  
  247. /* Convert x coordinate to byte offset in scan line. */
  248. #undef x_to_byte
  249. #define x_to_byte(x) ((x) << 1)
  250.  
  251. /* Fill a rectangle with a color. */
  252. private int
  253. mem_true16_fill_rectangle(gx_device *dev,
  254.   int x, int y, int w, int h, gx_color_index color)
  255. {
  256. #if arch_is_big_endian
  257. #  define color16 ((ushort)color)
  258. #else
  259.     ushort color16 = ((uint)(byte)color << 8) + ((ushort)color >> 8);
  260. #endif
  261.     declare_scan_ptr(dest);
  262.     fit_fill(dev, x, y, w, h);
  263.     setup_rect(dest);
  264.     while ( h-- > 0 )
  265.        {    ushort *pptr = (ushort *)dest;
  266.         int cnt = w;
  267.         do { *pptr++ = color16; } while ( --cnt > 0 );
  268.         inc_chunk_ptr(dest, draster);
  269.        }
  270.     return 0;
  271. #undef color16
  272. }
  273.  
  274. /* Copy a monochrome bitmap. */
  275. private int
  276. mem_true16_copy_mono(gx_device *dev,
  277.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  278.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  279. {
  280. #if arch_is_big_endian
  281. #  define zero16 ((ushort)zero)
  282. #  define one16 ((ushort)one)
  283. #else
  284.     ushort zero16 = ((uint)(byte)zero << 8) + ((ushort)zero >> 8);
  285.     ushort one16 = ((uint)(byte)one << 8) + ((ushort)one >> 8);
  286. #endif
  287.     const byte *line;
  288.     int first_bit;
  289.     declare_scan_ptr(dest);
  290.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  291.     setup_rect(dest);
  292.     line = base + (sourcex >> 3);
  293.     first_bit = 0x80 >> (sourcex & 7);
  294.     while ( h-- > 0 )
  295.        {    register ushort *pptr = (ushort *)dest;
  296.         const byte *sptr = line;
  297.         register int sbyte = *sptr++;
  298.         register int bit = first_bit;
  299.         int count = w;
  300.         do
  301.            {    if ( sbyte & bit )
  302.                {    if ( one != gx_no_color_index )
  303.                   *pptr = one16;
  304.                }
  305.             else
  306.                {    if ( zero != gx_no_color_index )
  307.                   *pptr = zero16;
  308.                }
  309.             if ( (bit >>= 1) == 0 )
  310.                 bit = 0x80, sbyte = *sptr++;
  311.             pptr++;
  312.            }
  313.         while ( --count > 0 );
  314.         line += sraster;
  315.         inc_chunk_ptr(dest, draster);
  316.        }
  317.     return 0;
  318. #undef zero16
  319. #undef one16
  320. }
  321.  
  322. /* Copy a color bitmap. */
  323. private int
  324. mem_true16_copy_color(gx_device *dev,
  325.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  326.   int x, int y, int w, int h)
  327. {    fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  328.     return copy_byte_rect(mdev, base + x_to_byte(sourcex), sraster,
  329.         x_to_byte(x), y, x_to_byte(w), h);
  330. }
  331.  
  332. /* ------ 24-bit (RGB) color ------ */
  333.  
  334. /* Procedures */
  335. declare_mem_procs(mem_true24_copy_mono, mem_true24_copy_color, mem_true24_fill_rectangle);
  336.  
  337. /* The device descriptor. */
  338. const gx_device_memory far_data mem_true24_color_device =
  339.   mem_device("image(24)", 24, 0,
  340.     gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb,
  341.     mem_true24_copy_mono, mem_true24_copy_color, mem_true24_fill_rectangle);
  342.  
  343. /* Convert x coordinate to byte offset in scan line. */
  344. #undef x_to_byte
  345. #define x_to_byte(x) ((x) * 3)
  346.  
  347. /* Unpack a color into its bytes. */
  348. #define declare_unpack_color(r, g, b, color)\
  349.     byte r = (byte)(color >> 16);\
  350.     byte g = (byte)((uint)color >> 8);\
  351.     byte b = (byte)color
  352. #if arch_is_big_endian
  353. #  define declare_pack_color(cword, rgb, r, g, b)\
  354.     bits32 cword = (bits32)(rgb) << 8
  355. #else
  356. #  define declare_pack_color(cword, rgb, r, g, b)\
  357.     bits32 cword =\
  358.       ((bits32)(b) << 16) | ((bits16)(g) << 8) | (r)
  359. #endif
  360. /* Put a 24-bit color into the bitmap. */
  361. #define put3(ptr, r, g, b)\
  362.     (ptr)[0] = r, (ptr)[1] = g, (ptr)[2] = b
  363. /* Put 4 bytes of color into the bitmap. */
  364. #define putw(ptr, wxyz)\
  365.     *(bits32 *)(ptr) = (wxyz)
  366. /* Load the 3-word 24-color cache. */
  367. /* Free variables: [m]dev, rgbr, gbrg, brgb. */
  368. #if arch_is_big_endian
  369. #  define set_color24_cache(crgb, r, g, b)\
  370.     mdev->color24.rgbr = rgbr = ((bits32)(crgb) << 8) | (r),\
  371.     mdev->color24.gbrg = gbrg = (rgbr << 8) | (g),\
  372.     mdev->color24.brgb = brgb = (gbrg << 8) | (b),\
  373.     mdev->color24.rgb = (crgb)
  374. #else
  375. #  define set_color24_cache(crgb, r, g, b)\
  376.     mdev->color24.rgbr = rgbr =\
  377.         ((bits32)(r) << 24) | ((bits32)(b) << 16) |\
  378.         ((bits16)(g) << 8) | (r),\
  379.     mdev->color24.brgb = brgb = (rgbr << 8) | (b),\
  380.     mdev->color24.gbrg = gbrg = (brgb << 8) | (g),\
  381.     mdev->color24.rgb = (crgb)
  382. #endif
  383.  
  384. /* Fill a rectangle with a color. */
  385. private int
  386. mem_true24_fill_rectangle(gx_device *dev,
  387.   int x, int y, int w, int h, gx_color_index color)
  388. {    declare_unpack_color(r, g, b, color);
  389.     declare_scan_ptr(dest);
  390.     fit_fill(dev, x, y, w, h);
  391.     setup_rect(dest);
  392.     if ( w >= 5 )
  393.       { if ( r == g && r == b)
  394.           {
  395. #if 1
  396.         /* We think we can do better than the library's memset.... */
  397.         int bcntm7 = w * 3 - 7;
  398.         register bits32 cword = color | (color << 24);
  399.         while ( h-- > 0 )
  400.         {    register byte *pptr = dest;
  401.             byte *limit = pptr + bcntm7;
  402.             /* We want to store full words, but we have to */
  403.             /* guarantee that they are word-aligned. */
  404.             switch ( x & 3 )
  405.               {
  406.               case 3: *pptr++ = (byte)cword;
  407.               case 2: *pptr++ = (byte)cword;
  408.               case 1: *pptr++ = (byte)cword;
  409.               case 0: ;
  410.               }
  411.             /* Even with w = 5, we always store at least */
  412.             /* 3 full words, regardless of the starting x. */
  413.             *(bits32 *)pptr =
  414.               ((bits32 *)pptr)[1] =
  415.               ((bits32 *)pptr)[2] = cword;
  416.             pptr += 12;
  417.             while ( pptr < limit )
  418.               { *(bits32 *)pptr =
  419.                   ((bits32 *)pptr)[1] = cword;
  420.                 pptr += 8;
  421.               }
  422.             switch ( pptr - limit )
  423.               {
  424.               case 0: pptr[6] = (byte)cword;
  425.               case 1: pptr[5] = (byte)cword;
  426.               case 2: pptr[4] = (byte)cword;
  427.               case 3: *(bits32 *)pptr = cword;
  428.                 break;
  429.               case 4: pptr[2] = (byte)cword;
  430.               case 5: pptr[1] = (byte)cword;
  431.               case 6: pptr[0] = (byte)cword;
  432.               case 7: ;
  433.               }
  434.             inc_chunk_ptr(dest, draster);
  435.         }
  436. #else
  437.         int bcnt = w * 3;
  438.         while ( h-- > 0 )
  439.         {    memset(dest, r, bcnt);
  440.             inc_chunk_ptr(dest, draster);
  441.         }
  442. #endif
  443.           }
  444.         else
  445.           {    int x3 = -x & 3, ww = w - x3;
  446.         bits32 rgbr, gbrg, brgb;
  447.         if ( mdev->color24.rgb == color )
  448.           rgbr = mdev->color24.rgbr,
  449.           gbrg = mdev->color24.gbrg,
  450.           brgb = mdev->color24.brgb;
  451.         else
  452.           set_color24_cache(color, r, g, b);
  453.         while ( h-- > 0 )
  454.           {    register byte *pptr = dest;
  455.             int w1 = ww;
  456.             switch ( x3 )
  457.               {
  458.               case 1:
  459.                 put3(pptr, r, g, b);
  460.                 pptr += 3; break;
  461.               case 2:
  462.                 pptr[0] = r; pptr[1] = g;
  463.                 putw(pptr + 2, brgb);
  464.                 pptr += 6; break;
  465.               case 3:
  466.                 pptr[0] = r;
  467.                 putw(pptr + 1, gbrg);
  468.                 putw(pptr + 5, brgb);
  469.                 pptr += 9; break;
  470.               case 0:
  471.                 ;
  472.               }
  473.             while ( w1 >= 4 )
  474.               {    putw(pptr, rgbr);
  475.                 putw(pptr + 4, gbrg);
  476.                 putw(pptr + 8, brgb);
  477.                 pptr += 12;
  478.                 w1 -= 4;
  479.               }
  480.             switch ( w1 )
  481.               {
  482.               case 1:
  483.                 put3(pptr, r, g, b);
  484.                 break;
  485.               case 2:
  486.                 putw(pptr, rgbr);
  487.                 pptr[4] = g; pptr[5] = b;
  488.                 break;
  489.               case 3:
  490.                 putw(pptr, rgbr);
  491.                 putw(pptr + 4, gbrg);
  492.                 pptr[8] = b;
  493.                 break;
  494.               case 0:
  495.                 ;
  496.               }
  497.             inc_chunk_ptr(dest, draster);
  498.           }
  499.           }
  500.       }
  501.     else            /* w < 5 */
  502.     {    while ( h-- > 0 )
  503.           {    switch ( w )
  504.               {
  505.               case 4: put3(dest + 9, r, g, b);
  506.               case 3: put3(dest + 6, r, g, b);
  507.               case 2: put3(dest + 3, r, g, b);
  508.               case 1: put3(dest, r, g, b);
  509.               case 0: ;
  510.               }
  511.             inc_chunk_ptr(dest, draster);
  512.           }
  513.     }
  514.     return 0;
  515. }
  516.  
  517. /* Copy a monochrome bitmap. */
  518. private int
  519. mem_true24_copy_mono(gx_device *dev,
  520.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  521.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  522. {    const byte *line;
  523.     int sbit;
  524.     int first_bit;
  525.     declare_scan_ptr(dest);
  526.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  527.     setup_rect(dest);
  528.     line = base + (sourcex >> 3);
  529.     sbit = sourcex & 7;
  530.     first_bit = 0x80 >> sbit;
  531.     if ( zero != gx_no_color_index )
  532.       {    /* Loop for halftones or inverted masks */
  533.         /* (never used). */
  534.         declare_unpack_color(r0, g0, b0, zero);
  535.         declare_unpack_color(r1, g1, b1, one);
  536.         while ( h-- > 0 )
  537.            {    register byte *pptr = dest;
  538.             const byte *sptr = line;
  539.             register int sbyte = *sptr++;
  540.             register int bit = first_bit;
  541.             int count = w;
  542.             do
  543.               {    if ( sbyte & bit )
  544.                   { if ( one != gx_no_color_index )
  545.                       put3(pptr, r1, g1, b1);
  546.                   }
  547.                 else
  548.                   put3(pptr, r0, g0, b0);
  549.                 pptr += 3;
  550.                 if ( (bit >>= 1) == 0 )
  551.                   bit = 0x80, sbyte = *sptr++;
  552.               }
  553.             while ( --count > 0 );
  554.             line += sraster;
  555.             inc_chunk_ptr(dest, draster);
  556.            }
  557.       }
  558.     else if ( one != gx_no_color_index )
  559.       {    /* Loop for character and pattern masks. */
  560.         /* This is used heavily. */
  561.         declare_unpack_color(r1, g1, b1, one);
  562.         int first_mask = first_bit << 1;
  563.         int first_count, first_skip;
  564.         if ( sbit + w > 8 )
  565.           first_mask -= 1,
  566.           first_count = 8 - sbit;
  567.         else
  568.           first_mask -= first_mask >> w,
  569.           first_count = w;
  570.         first_skip = first_count * 3;
  571.         while ( h-- > 0 )
  572.            {    register byte *pptr = dest;
  573.             const byte *sptr = line;
  574.             register int sbyte = *sptr++ & first_mask;
  575.             int count = w - first_count;
  576.             if ( sbyte )
  577.               {    register int bit = first_bit;
  578.                 do
  579.                   {    if ( sbyte & bit )
  580.                       put3(pptr, r1, g1, b1);
  581.                     pptr += 3;
  582.                   }
  583.                 while ( (bit >>= 1) & first_mask );
  584.               }
  585.             else
  586.               pptr += first_skip;
  587.             while ( count >= 8 )
  588.               {    sbyte = *sptr++;
  589.                 if ( sbyte & 0xf0 )
  590.                   { if ( sbyte & 0x80 )
  591.                       put3(pptr, r1, g1, b1);
  592.                     if ( sbyte & 0x40 )
  593.                       put3(pptr + 3, r1, g1, b1);
  594.                     if ( sbyte & 0x20 )
  595.                       put3(pptr + 6, r1, g1, b1);
  596.                     if ( sbyte & 0x10 )
  597.                       put3(pptr + 9, r1, g1, b1);
  598.                   }
  599.                 if ( sbyte & 0xf )
  600.                   { if ( sbyte & 8 )
  601.                       put3(pptr + 12, r1, g1, b1);
  602.                     if ( sbyte & 4 )
  603.                       put3(pptr + 15, r1, g1, b1);
  604.                     if ( sbyte & 2 )
  605.                       put3(pptr + 18, r1, g1, b1);
  606.                     if ( sbyte & 1 )
  607.                       put3(pptr + 21, r1, g1, b1);
  608.                   }
  609.                 pptr += 24;
  610.                 count -= 8;
  611.               }
  612.             if ( count > 0 )
  613.               {    register int bit = 0x80;
  614.                 sbyte = *sptr++;
  615.                 do
  616.                   {    if ( sbyte & bit )
  617.                       put3(pptr, r1, g1, b1);
  618.                     pptr += 3;
  619.                     bit >>= 1;
  620.                   }
  621.                 while ( --count > 0 );
  622.               }
  623.             line += sraster;
  624.             inc_chunk_ptr(dest, draster);
  625.            }
  626.       }
  627.     return 0;
  628. }
  629.  
  630. /* Copy a color bitmap. */
  631. private int
  632. mem_true24_copy_color(gx_device *dev,
  633.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  634.   int x, int y, int w, int h)
  635. {    fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  636.     return copy_byte_rect(mdev, base + x_to_byte(sourcex), sraster,
  637.         x_to_byte(x), y, x_to_byte(w), h);
  638. }
  639.  
  640. /* ------ 32-bit (CMYK) color ------ */
  641.  
  642. /* Procedures */
  643. declare_mem_procs(mem_true32_copy_mono, mem_true32_copy_color, mem_true32_fill_rectangle);
  644.  
  645. /* The device descriptor. */
  646. const gx_device_memory far_data mem_true32_color_device =
  647.   mem_full_device("image(32)", 24, 8, mem_open,
  648.     gx_default_map_rgb_color, gx_default_map_color_rgb,
  649.     mem_true32_copy_mono, mem_true32_copy_color, mem_true32_fill_rectangle,
  650.     mem_get_bits, gx_default_cmyk_map_cmyk_color);
  651.  
  652. /* Convert x coordinate to byte offset in scan line. */
  653. #undef x_to_byte
  654. #define x_to_byte(x) ((x) << 2)
  655.  
  656. /* Swap the bytes of a color if needed. */
  657. #if arch_is_big_endian
  658. #  define arrange_bytes(color) (color)
  659. #else
  660. #  define arrange_bytes(color)\
  661.     (((color) >> 24) + (((color) >> 8) & 0xff00) +\
  662.      (((color) & 0xff00) << 8) + ((color) << 24))
  663. #endif
  664.  
  665. /* Fill a rectangle with a color. */
  666. private int
  667. mem_true32_fill_rectangle(gx_device *dev,
  668.   int x, int y, int w, int h, gx_color_index color)
  669. {    bits32 a_color = arrange_bytes(color);
  670.     declare_scan_ptr(dest);
  671.     fit_fill(dev, x, y, w, h);
  672.     setup_rect(dest);
  673.     while ( h-- > 0 )
  674.     {    bits32 *pptr = (bits32 *)dest;
  675.         int cnt = w;
  676.         do { *pptr++ = a_color; } while ( --cnt > 0 );
  677.         inc_chunk_ptr(dest, draster);
  678.     }
  679.     return 0;
  680. }
  681.  
  682. /* Copy a monochrome bitmap. */
  683. private int
  684. mem_true32_copy_mono(gx_device *dev,
  685.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  686.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  687. {    bits32 a_zero = arrange_bytes(zero);
  688.     bits32 a_one = arrange_bytes(one);
  689.     const byte *line;
  690.     int first_bit;
  691.     declare_scan_ptr(dest);
  692.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  693.     setup_rect(dest);
  694.     line = base + (sourcex >> 3);
  695.     first_bit = 0x80 >> (sourcex & 7);
  696.     while ( h-- > 0 )
  697.     {    register bits32 *pptr = (bits32 *)dest;
  698.         const byte *sptr = line;
  699.         register int sbyte = *sptr++;
  700.         register int bit = first_bit;
  701.         int count = w;
  702.         do
  703.         {    if ( sbyte & bit )
  704.             {    if ( one != gx_no_color_index )
  705.                   *pptr = a_one;
  706.             }
  707.             else
  708.             {    if ( zero != gx_no_color_index )
  709.                   *pptr = a_zero;
  710.             }
  711.             if ( (bit >>= 1) == 0 )
  712.                 bit = 0x80, sbyte = *sptr++;
  713.             pptr++;
  714.         }
  715.         while ( --count > 0 );
  716.         line += sraster;
  717.         inc_chunk_ptr(dest, draster);
  718.     }
  719.     return 0;
  720. }
  721.  
  722. /* Copy a color bitmap. */
  723. private int
  724. mem_true32_copy_color(gx_device *dev,
  725.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  726.   int x, int y, int w, int h)
  727. {    fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  728.     return copy_byte_rect(mdev, base + x_to_byte(sourcex), sraster,
  729.         x_to_byte(x), y, x_to_byte(w), h);
  730. }
  731.