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 / GDEVMEM3.C < prev    next >
C/C++ Source or Header  |  1992-09-11  |  8KB  |  271 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. /* gdevmem3.c */
  21. /* 2- and 4-bit-per-pixel "memory" (stored bitmap) devices */
  22. /* for Ghostscript library. */
  23. #include "memory_.h"
  24. #include "gx.h"
  25. #include "gxdevice.h"
  26. #include "gxdevmem.h"            /* semi-public definitions */
  27. #include "gdevmem.h"            /* private definitions */
  28.  
  29. /* The current implementations are quite inefficient. */
  30. /* We intend to improve them someday.... */
  31.  
  32. /* ------ Generic procedures ------ */
  33.  
  34. /* We do everything byte-by-byte. */
  35. #define chunk byte
  36.  
  37. /* Import the color mapping procedures from gdevmem2. */
  38. extern dev_proc_map_rgb_color(mem_mapped_map_rgb_color);
  39. extern dev_proc_map_color_rgb(mem_mapped_map_color_rgb);
  40.  
  41. /* Implement fill_rectangle by tiling. */
  42. private int
  43. fill_2or4_by_tiling(gx_device *dev, int scaled_x, int y, int scaled_w, int h,
  44.   ulong *ppattern)
  45. {    gx_bitmap tile;
  46.     tile.data = (byte *)ppattern;
  47.     tile.raster = sizeof(ulong);
  48.     tile.size.x = sizeof(ulong) * 8, tile.size.y = 1;
  49.     tile.id = gx_no_bitmap_id;
  50.     tile.rep_width = 1, tile.rep_height = 1;
  51.     return (*mem_mono_device.procs->tile_rectangle)
  52.       (dev, &tile, scaled_x, y, scaled_w, h,
  53.        (gx_color_index)0, (gx_color_index)1, 0, 0);
  54. }
  55.  
  56. /* ------ Mapped 2-bit color ------ */
  57.  
  58. /* Procedures */
  59. declare_mem_procs(mem_mapped2_copy_mono, mem_mapped2_copy_color, mem_mapped2_fill_rectangle);
  60.  
  61. /* The device descriptor. */
  62. private gx_device_procs mem_mapped2_procs =
  63.   mem_procs(mem_mapped_map_rgb_color, mem_mapped_map_color_rgb,
  64.     mem_mapped2_copy_mono, mem_mapped2_copy_color, mem_mapped2_fill_rectangle);
  65.  
  66. /* The instance is public. */
  67. const gx_device_memory mem_mapped2_color_device =
  68.   mem_device("image(2)", 2, mem_mapped2_procs);
  69.  
  70. /* Convert x coordinate to byte offset in scan line. */
  71. #undef x_to_byte
  72. #define x_to_byte(x) ((x) >> 2)
  73.  
  74. /* Fill a rectangle with a color. */
  75. private int
  76. mem_mapped2_fill_rectangle(gx_device *dev,
  77.   int x, int y, int w, int h, gx_color_index color)
  78. {    int code;
  79.     fit_fill(dev, x, y, w, h);
  80.     /* Patch the width in the device temporarily. */
  81.     dev->width <<= 1;
  82.     if ( color == 0 || color == 3 )
  83.        {    /* Use monobit fill_rectangle. */
  84.         code = (*mem_mono_device.procs->fill_rectangle)
  85.           (dev, x << 1, y, w << 1, h, color & 1);
  86.        }
  87.     else
  88.        {    /* Use monobit tile_rectangle. */
  89.         static ulong tile_patterns[4] =
  90.            {    0, 0x55555555, 0xaaaaaaaa, 0xffffffff
  91.            };
  92.         code = fill_2or4_by_tiling(dev, x << 1, y, w << 1, h,
  93.                        &tile_patterns[color]);
  94.        }
  95.     /* Restore the correct width. */
  96.     dev->width >>= 1;
  97.     return code;
  98. }
  99.  
  100. /* Copy a bitmap. */
  101. private int
  102. mem_mapped2_copy_mono(gx_device *dev,
  103.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  104.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  105. {    const byte *line;
  106.     int first_bit;
  107.     byte first_mask, b0, b1;
  108.     static byte btab[4] = { 0, 0x55, 0xaa, 0xff };
  109.     static byte bmask[4] = { 0xc0, 0x30, 0xc, 3 };
  110.     declare_scan_ptr(dest);
  111.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  112.     setup_rect(dest);
  113.     line = base + (sourcex >> 3);
  114.     first_bit = 0x80 >> (sourcex & 7);
  115.     first_mask = bmask[x & 3];
  116.     b0 = btab[zero & 3];
  117.     b1 = btab[one & 3];
  118.     while ( h-- > 0 )
  119.        {    register byte *pptr = (byte *)dest;
  120.         const byte *sptr = line;
  121.         register int sbyte = *sptr++;
  122.         register int bit = first_bit;
  123.         register byte mask = first_mask;
  124.         int count = w;
  125.         do
  126.            {    if ( sbyte & bit )
  127.                {    if ( one != gx_no_color_index )
  128.                   *pptr = (*pptr & ~mask) + (b1 & mask);
  129.                }
  130.             else
  131.                {    if ( zero != gx_no_color_index )
  132.                   *pptr = (*pptr & ~mask) + (b0 & mask);
  133.                }
  134.             if ( (bit >>= 1) == 0 )
  135.                 bit = 0x80, sbyte = *sptr++;
  136.             mask = (mask << 6) + (mask >> 2);
  137.             pptr++;
  138.            }
  139.         while ( --count > 0 );
  140.         line += sraster;
  141.         inc_chunk_ptr(dest, draster);
  142.        }
  143.     return 0;
  144. }
  145.  
  146. /* Copy a color bitmap. */
  147. private int
  148. mem_mapped2_copy_color(gx_device *dev,
  149.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  150.   int x, int y, int w, int h)
  151. {    int code;
  152.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  153.     /* Use monobit copy_mono. */
  154.     /* Patch the width in the device temporarily. */
  155.     dev->width <<= 1;
  156.     code = (*mem_mono_device.procs->copy_mono)
  157.       (dev, base, sourcex << 1, sraster, id,
  158.        x << 1, y, w << 1, h, (gx_color_index)0, (gx_color_index)1);
  159.     /* Restore the correct width. */
  160.     dev->width >>= 1;
  161.     return code;
  162. }
  163.  
  164. /* ------ Mapped 4-bit color ------ */
  165.  
  166. /* Procedures */
  167. declare_mem_procs(mem_mapped4_copy_mono, mem_mapped4_copy_color, mem_mapped4_fill_rectangle);
  168.  
  169. /* The device descriptor. */
  170. private gx_device_procs mem_mapped4_procs =
  171.   mem_procs(mem_mapped_map_rgb_color, mem_mapped_map_color_rgb,
  172.     mem_mapped4_copy_mono, mem_mapped4_copy_color, mem_mapped4_fill_rectangle);
  173.  
  174. /* The instance is public. */
  175. const gx_device_memory mem_mapped4_color_device =
  176.   mem_device("image(4)", 4, mem_mapped4_procs);
  177.  
  178. /* Convert x coordinate to byte offset in scan line. */
  179. #undef x_to_byte
  180. #define x_to_byte(x) ((x) >> 1)
  181.  
  182. /* Fill a rectangle with a color. */
  183. private int
  184. mem_mapped4_fill_rectangle(gx_device *dev,
  185.   int x, int y, int w, int h, gx_color_index color)
  186. {    int code;
  187.     fit_fill(dev, x, y, w, h);
  188.     /* Patch the width in the device temporarily. */
  189.     dev->width <<= 2;
  190.     if ( color == 0 || color == 15 )
  191.        {    /* Use monobit fill_rectangle. */
  192.         code = (*mem_mono_device.procs->fill_rectangle)
  193.           (dev, x << 2, y, w << 2, h, color & 1);
  194.        }
  195.     else
  196.        {    /* Use monobit tile_rectangle. */
  197.         static ulong tile_patterns[16] =
  198.            {    0, 0x11111111, 0x22222222, 0x33333333,
  199.             0x44444444, 0x55555555, 0x66666666, 0x77777777,
  200.             0x88888888, 0x99999999, 0xaaaaaaaa, 0xbbbbbbbb,
  201.             0xcccccccc, 0xdddddddd, 0xeeeeeeee, 0xffffffff
  202.            };
  203.         code = fill_2or4_by_tiling(dev, x << 2, y, w << 2, h,
  204.                        &tile_patterns[color]);
  205.        }
  206.     dev->width >>= 2;
  207.     return code;
  208. }
  209.  
  210. /* Copy a bitmap. */
  211. private int
  212. mem_mapped4_copy_mono(gx_device *dev,
  213.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  214.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  215. {    const byte *line;
  216.     int first_bit;
  217.     byte first_mask, b0, b1;
  218.     declare_scan_ptr(dest);
  219.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  220.     setup_rect(dest);
  221.     line = base + (sourcex >> 3);
  222.     first_bit = 0x80 >> (sourcex & 7);
  223.     first_mask = (x & 1 ? 0xf : 0xf0);
  224.     b0 = ((byte)zero << 4) + (byte)zero;
  225.     b1 = ((byte)one << 4) + (byte)one;
  226.     while ( h-- > 0 )
  227.        {    register byte *pptr = (byte *)dest;
  228.         const byte *sptr = line;
  229.         register int sbyte = *sptr++;
  230.         register int bit = first_bit;
  231.         register byte mask = first_mask;
  232.         int count = w;
  233.         do
  234.            {    if ( sbyte & bit )
  235.                {    if ( one != gx_no_color_index )
  236.                   *pptr = (*pptr & ~mask) + (b1 & mask);
  237.                }
  238.             else
  239.                {    if ( zero != gx_no_color_index )
  240.                   *pptr = (*pptr & ~mask) + (b0 & mask);
  241.                }
  242.             if ( (bit >>= 1) == 0 )
  243.                 bit = 0x80, sbyte = *sptr++;
  244.             mask = ~mask;
  245.             pptr++;
  246.            }
  247.         while ( --count > 0 );
  248.         line += sraster;
  249.         inc_chunk_ptr(dest, draster);
  250.        }
  251.     return 0;
  252. }
  253.  
  254. /* Copy a color bitmap. */
  255. private int
  256. mem_mapped4_copy_color(gx_device *dev,
  257.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  258.   int x, int y, int w, int h)
  259. {    int code;
  260.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  261.     /* Use monobit copy_mono. */
  262.     /* Patch the width in the device temporarily. */
  263.     dev->width <<= 2;
  264.     code = (*mem_mono_device.procs->copy_mono)
  265.       (dev, base, sourcex << 2, sraster, id,
  266.        x << 2, y, w << 2, h, (gx_color_index)0, (gx_color_index)1);
  267.     /* Restore the correct width. */
  268.     dev->width >>= 2;
  269.     return code;
  270. }
  271.