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

  1. /* Copyright (C) 1992, 1993 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. /* gdevmem3.c */
  20. /* 2- and 4-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. /* The current implementations are quite inefficient. */
  29. /* We intend to improve them someday.... */
  30.  
  31. /* ------ Generic procedures ------ */
  32.  
  33. /* We do tiling in big chunks. */
  34. #define chunk mono_chunk
  35.  
  36. /* Implement fill_rectangle by tiling. */
  37. /* This uses the same algorithm as mem_mono_fill_rectangle. */
  38. /* Note that x and w are in bit units, not pixel units. */
  39. #define x_to_byte(x) ((x) >> 3)
  40. private int
  41. fill_2or4_by_tiling(gx_device *dev, int x, int y, int w, int h,
  42.   const ulong pattern)
  43. {    uint bit;
  44.     chunk right_mask;
  45.     declare_scan_ptr(dest);
  46.     setup_rect(dest);
  47. #define write_loop(stat)\
  48.  { int line_count = h;\
  49.    chunk *ptr = dest;\
  50.    do { stat; inc_chunk_ptr(ptr, draster); }\
  51.    while ( --line_count );\
  52.  }
  53. #define write_partial(msk)\
  54.    write_loop(*ptr = (*ptr & ~msk) | (pattern & msk))
  55.     bit = x & chunk_align_bit_mask;
  56.     if ( bit + w <= chunk_bits )
  57.        {    set_mono_thin_mask(right_mask, w, bit);
  58.        }
  59.     else
  60.        {    int byte_count;
  61.         if ( bit )
  62.            {    chunk mask;
  63.             set_mono_left_mask(mask, bit);
  64.             write_partial(mask);
  65.             dest++;
  66.             w += bit - chunk_bits;
  67.            }
  68.         set_mono_right_mask(right_mask, w & chunk_bit_mask);
  69.         if ( (byte_count = (w >> 3) & -chunk_bytes) != 0 )
  70.            {    write_loop(memset(ptr, (byte)pattern, byte_count));
  71.             inc_chunk_ptr(dest, byte_count);
  72.            }
  73.        }
  74.     if ( right_mask )
  75.         write_partial(right_mask);
  76.     return 0;
  77. }
  78.  
  79. /* We do everything else byte-by-byte. */
  80. #undef chunk
  81. #define chunk byte
  82.  
  83. /* ------ Mapped 2-bit color ------ */
  84.  
  85. /* Procedures */
  86. declare_mem_procs(mem_mapped2_copy_mono, mem_mapped2_copy_color, mem_mapped2_fill_rectangle);
  87.  
  88. /* The device descriptor. */
  89. /* The instance is exported for gdevmem1.c. */
  90. const gx_device_memory far_data mem_mapped2_color_device =
  91.   mem_device("image(2)", 2, 0,
  92.     mem_mapped_map_rgb_color, mem_mapped_map_color_rgb,
  93.     mem_mapped2_copy_mono, mem_mapped2_copy_color, mem_mapped2_fill_rectangle);
  94.  
  95. /* Convert x coordinate to byte offset in scan line. */
  96. #undef x_to_byte
  97. #define x_to_byte(x) ((x) >> 2)
  98.  
  99. /* Fill a rectangle with a color. */
  100. private int
  101. mem_mapped2_fill_rectangle(gx_device *dev,
  102.   int x, int y, int w, int h, gx_color_index color)
  103. {    int code;
  104.     fit_fill(dev, x, y, w, h);
  105.     /* Patch the width in the device temporarily. */
  106.     dev->width <<= 1;
  107.     if ( color == 0 || color == 3 )
  108.        {    /* Use monobit fill_rectangle. */
  109.         code = (*dev_proc(&mem_mono_device, fill_rectangle))
  110.           (dev, x << 1, y, w << 1, h, color & 1);
  111.        }
  112.     else
  113.        {    /* Use monobit tile_rectangle. */
  114.         static const ulong tile_patterns[4] =
  115.            {    0, 0x55555555, 0xaaaaaaaa, 0xffffffff
  116.            };
  117.         code = fill_2or4_by_tiling(dev, x << 1, y, w << 1, h,
  118.                        tile_patterns[color]);
  119.        }
  120.     /* Restore the correct width. */
  121.     dev->width >>= 1;
  122.     return code;
  123. }
  124.  
  125. /* Copy a bitmap. */
  126. private int
  127. mem_mapped2_copy_mono(gx_device *dev,
  128.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  129.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  130. {    const byte *line;
  131.     int first_bit;
  132.     byte first_mask, b0, b1;
  133.     static byte btab[4] = { 0, 0x55, 0xaa, 0xff };
  134.     static byte bmask[4] = { 0xc0, 0x30, 0xc, 3 };
  135.     declare_scan_ptr(dest);
  136.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  137.     setup_rect(dest);
  138.     line = base + (sourcex >> 3);
  139.     first_bit = 0x80 >> (sourcex & 7);
  140.     first_mask = bmask[x & 3];
  141.     b0 = btab[zero & 3];
  142.     b1 = btab[one & 3];
  143.     while ( h-- > 0 )
  144.        {    register byte *pptr = (byte *)dest;
  145.         const byte *sptr = line;
  146.         register int sbyte = *sptr++;
  147.         register int bit = first_bit;
  148.         register byte mask = first_mask;
  149.         int count = w;
  150.         do
  151.            {    if ( sbyte & bit )
  152.                {    if ( one != gx_no_color_index )
  153.                   *pptr = (*pptr & ~mask) + (b1 & mask);
  154.                }
  155.             else
  156.                {    if ( zero != gx_no_color_index )
  157.                   *pptr = (*pptr & ~mask) + (b0 & mask);
  158.                }
  159.             if ( (bit >>= 1) == 0 )
  160.                 bit = 0x80, sbyte = *sptr++;
  161.             if ( (mask >>= 2) == 0 )
  162.                 mask = 0xc0, pptr++;
  163.            }
  164.         while ( --count > 0 );
  165.         line += sraster;
  166.         inc_chunk_ptr(dest, draster);
  167.        }
  168.     return 0;
  169. }
  170.  
  171. /* Copy a color bitmap. */
  172. private int
  173. mem_mapped2_copy_color(gx_device *dev,
  174.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  175.   int x, int y, int w, int h)
  176. {    int code;
  177.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  178.     /* Use monobit copy_mono. */
  179.     /* Patch the width in the device temporarily. */
  180.     dev->width <<= 1;
  181.     code = (*dev_proc(&mem_mono_device, copy_mono))
  182.       (dev, base, sourcex << 1, sraster, id,
  183.        x << 1, y, w << 1, h, (gx_color_index)0, (gx_color_index)1);
  184.     /* Restore the correct width. */
  185.     dev->width >>= 1;
  186.     return code;
  187. }
  188.  
  189. /* ------ Mapped 4-bit color ------ */
  190.  
  191. /* Procedures */
  192. declare_mem_procs(mem_mapped4_copy_mono, mem_mapped4_copy_color, mem_mapped4_fill_rectangle);
  193.  
  194. /* The device descriptor. */
  195. /* The instance is exported for gdevmem1.c. */
  196. const gx_device_memory far_data mem_mapped4_color_device =
  197.   mem_device("image(4)", 4, 0,
  198.     mem_mapped_map_rgb_color, mem_mapped_map_color_rgb,
  199.     mem_mapped4_copy_mono, mem_mapped4_copy_color, mem_mapped4_fill_rectangle);
  200.  
  201. /* Convert x coordinate to byte offset in scan line. */
  202. #undef x_to_byte
  203. #define x_to_byte(x) ((x) >> 1)
  204.  
  205. /* Fill a rectangle with a color. */
  206. private int
  207. mem_mapped4_fill_rectangle(gx_device *dev,
  208.   int x, int y, int w, int h, gx_color_index color)
  209. {    int code;
  210.     fit_fill(dev, x, y, w, h);
  211.     /* Patch the width in the device temporarily. */
  212.     dev->width <<= 2;
  213.     if ( color == 0 || color == 15 )
  214.        {    /* Use monobit fill_rectangle. */
  215.         code = (*dev_proc(&mem_mono_device, fill_rectangle))
  216.           (dev, x << 2, y, w << 2, h, color & 1);
  217.        }
  218.     else
  219.        {    /* Use monobit tile_rectangle. */
  220.         static const ulong tile_patterns[16] =
  221.            {    0, 0x11111111, 0x22222222, 0x33333333,
  222.             0x44444444, 0x55555555, 0x66666666, 0x77777777,
  223.             0x88888888, 0x99999999, 0xaaaaaaaa, 0xbbbbbbbb,
  224.             0xcccccccc, 0xdddddddd, 0xeeeeeeee, 0xffffffff
  225.            };
  226.         code = fill_2or4_by_tiling(dev, x << 2, y, w << 2, h,
  227.                        tile_patterns[color]);
  228.        }
  229.     dev->width >>= 2;
  230.     return code;
  231. }
  232.  
  233. /* Copy a bitmap. */
  234. private int
  235. mem_mapped4_copy_mono(gx_device *dev,
  236.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  237.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  238. {    const byte *line;
  239.     int first_bit;
  240.     byte first_mask, b0, b1;
  241.     declare_scan_ptr(dest);
  242.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  243.     setup_rect(dest);
  244.     line = base + (sourcex >> 3);
  245.     first_bit = 0x80 >> (sourcex & 7);
  246.     first_mask = (x & 1 ? 0xf : 0xf0);
  247.     b0 = ((byte)zero << 4) + (byte)zero;
  248.     b1 = ((byte)one << 4) + (byte)one;
  249.     while ( h-- > 0 )
  250.        {    register byte *pptr = (byte *)dest;
  251.         const byte *sptr = line;
  252.         register int sbyte = *sptr++;
  253.         register int bit = first_bit;
  254.         register byte mask = first_mask;
  255.         int count = w;
  256.         do
  257.            {    if ( sbyte & bit )
  258.                {    if ( one != gx_no_color_index )
  259.                   *pptr = (*pptr & ~mask) + (b1 & mask);
  260.                }
  261.             else
  262.                {    if ( zero != gx_no_color_index )
  263.                   *pptr = (*pptr & ~mask) + (b0 & mask);
  264.                }
  265.             if ( (bit >>= 1) == 0 )
  266.                 bit = 0x80, sbyte = *sptr++;
  267.             if ( (mask = ~mask) == 0xf0 )
  268.                 pptr++;
  269.            }
  270.         while ( --count > 0 );
  271.         line += sraster;
  272.         inc_chunk_ptr(dest, draster);
  273.        }
  274.     return 0;
  275. }
  276.  
  277. /* Copy a color bitmap. */
  278. private int
  279. mem_mapped4_copy_color(gx_device *dev,
  280.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  281.   int x, int y, int w, int h)
  282. {    int code;
  283.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  284.     /* Use monobit copy_mono. */
  285.     /* Patch the width in the device temporarily. */
  286.     dev->width <<= 2;
  287.     code = (*dev_proc(&mem_mono_device, copy_mono))
  288.       (dev, base, sourcex << 2, sraster, id,
  289.        x << 2, y, w << 2, h, (gx_color_index)0, (gx_color_index)1);
  290.     /* Restore the correct width. */
  291.     dev->width >>= 2;
  292.     return code;
  293. }
  294.