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

  1. /* Copyright (C) 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. /* gdevmem4.c */
  20. /* Any-depth planar "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. /*
  29.  * Planar memory devices store the bits by planes instead of by chunks.
  30.  * The plane corresponding to the least significant bit of the color index
  31.  * is stored first.
  32.  *
  33.  * The current implementations are quite inefficient.
  34.  * We may improve them someday if anyone cares.
  35.  */
  36.  
  37. /* Procedures */
  38. declare_mem_map_procs(mem_planar_map_rgb_color, mem_planar_map_color_rgb);
  39. declare_mem_procs(mem_planar_copy_mono, mem_planar_copy_color, mem_planar_fill_rectangle);
  40.  
  41. /* The device descriptor. */
  42. /* The instance is public. */
  43. /* The default instance has depth = 1, but clients may set this */
  44. /* to other values before opening the device. */
  45. private dev_proc_open_device(mem_planar_open);
  46. private dev_proc_get_bits(mem_planar_get_bits);
  47. const gx_device_memory far_data mem_planar_device =
  48.   mem_full_device("image(planar)", 0, 1, mem_planar_open,
  49.     mem_planar_map_rgb_color, mem_planar_map_color_rgb,
  50.     mem_planar_copy_mono, mem_planar_copy_color, mem_planar_fill_rectangle,
  51.     mem_planar_get_bits, gx_default_map_cmyk_color);
  52.  
  53. /* Open a planar memory device. */
  54. private int
  55. mem_planar_open(gx_device *dev)
  56. {    /* Temporarily reset the parameters, and call */
  57.     /* the generic open procedure. */
  58.     int depth = dev->color_info.depth;
  59.     int height = dev->height;
  60.     int code;
  61.     dev->height *= depth;
  62.     dev->color_info.depth = 1;
  63.     code = mem_open(dev);
  64.     dev->height = height;
  65.     dev->color_info.depth = depth;
  66.     return code;
  67. }
  68.  
  69. /* Map a r-g-b color to a color index. */
  70. private gx_color_index
  71. mem_planar_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  72.   gx_color_value b)
  73. {    int depth = dev->color_info.depth;
  74.     return (depth == 1 ? gx_default_map_rgb_color(dev, r, g, b) :
  75.         depth < 16 ? mem_mapped_map_rgb_color(dev, r, g, b) :
  76.         (*dev_proc(gdev_mem_device_for_bits(depth), map_rgb_color))
  77.             (dev, r, g, b));
  78. }
  79.  
  80. /* Map a color index to a r-g-b color. */
  81. private int
  82. mem_planar_map_color_rgb(gx_device *dev, gx_color_index color,
  83.   gx_color_value prgb[3])
  84. {    int depth = dev->color_info.depth;
  85.     return (depth == 1 ? gx_default_map_color_rgb(dev, color, prgb) :
  86.         depth < 16 ? mem_mapped_map_color_rgb(dev, color, prgb) :
  87.         (*dev_proc(gdev_mem_device_for_bits(depth), map_color_rgb))
  88.             (dev, color, prgb));
  89. }
  90.  
  91. /* Fill a rectangle with a color. */
  92. private int
  93. mem_planar_fill_rectangle(gx_device *dev,
  94.   int x, int y, int w, int h, gx_color_index color)
  95. {    byte **ptrs = mdev->line_ptrs;
  96.     int i;
  97.     for ( i = 0; i < dev->color_info.depth;
  98.           i++, mdev->line_ptrs += dev->height
  99.         )
  100.         (*dev_proc(&mem_mono_device, fill_rectangle))(dev,
  101.             x, y, w, h, (color >> i) & 1);
  102.     mdev->line_ptrs = ptrs;
  103.     return 0;
  104. }
  105.  
  106. /* Copy a bitmap. */
  107. private int
  108. mem_planar_copy_mono(gx_device *dev,
  109.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  110.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  111. {    byte **ptrs = mdev->line_ptrs;
  112.     int i;
  113.     for ( i = 0; i < dev->color_info.depth;
  114.           i++, mdev->line_ptrs += dev->height
  115.         )
  116.         (*dev_proc(&mem_mono_device, copy_mono))(dev,
  117.             base, sourcex, sraster, id, x, y, w, h,
  118.             (zero == gx_no_color_index ? gx_no_color_index :
  119.              (zero >> i) & 1),
  120.             (one == gx_no_color_index ? gx_no_color_index :
  121.              (one >> i) & 1));
  122.     mdev->line_ptrs = ptrs;
  123.     return 0;
  124. }
  125.  
  126. /* Copy a color bitmap. */
  127. /* This is very slow and messy. */
  128. private int
  129. mem_planar_copy_color(gx_device *dev,
  130.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  131.   int x, int y, int w, int h)
  132. {    byte **ptrs = mdev->line_ptrs;
  133.     int depth = dev->color_info.depth; 
  134.     int wleft = w;
  135.     int hleft = h;
  136.     const byte *srow = base;
  137.     int ynext = y;
  138. #define max_w 32
  139.     union _b {
  140.         long l[max_w / sizeof(long)];
  141.         byte b[max_w / 8];
  142.     } buf;
  143.     while ( wleft > max_w )
  144.     {    mem_planar_copy_color(dev, base,
  145.             sourcex + wleft - max_w, sraster, gx_no_bitmap_id,
  146.             x + wleft - max_w, y, max_w, h);
  147.         wleft -= max_w;
  148.     }
  149.     for ( ; hleft > 0;
  150.           srow += sraster, ynext++, hleft--,
  151.         mdev->line_ptrs += dev->height
  152.         )
  153.     {    int i;
  154.         for ( i = 0; i < depth;
  155.               i++, mdev->line_ptrs += dev->height
  156.             )
  157.         {    int sx, bx;
  158.             memset(buf.b, 0, sizeof(buf.b));
  159.             for ( sx = 0, bx = sourcex * depth + depth - 1 - i;
  160.                   sx < w; sx++, bx += depth
  161.                 )
  162.                 if ( srow[bx >> 3] & (0x80 >> (bx & 7)) )
  163.                     buf.b[sx >> 3] |= 0x80 >> (sx & 7);
  164.             (*dev_proc(&mem_mono_device, copy_mono))(dev,
  165.                 buf.b, 0, sizeof(buf), gx_no_bitmap_id,
  166.                 x, ynext, w, 1,
  167.                 (gx_color_index)0, (gx_color_index)1);
  168.         }
  169.         mdev->line_ptrs = ptrs;
  170.     }
  171.     return 0;
  172. }
  173.  
  174. /* Copy bits back from a planar memory device. */
  175. /****** NOT IMPLEMENTED YET ******/
  176. private int
  177. mem_planar_get_bits(gx_device *dev, int y, byte *str, byte **actual_data)
  178. {    return -1;
  179. }
  180.