gdevm8.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gdevm8.c,v 1.5 2002/06/16 05:48:55 lpd Exp $ */
  14. /* 8-bit-per-pixel "memory" (stored bitmap) device */
  15. #include "memory_.h"
  16. #include "gx.h"
  17. #include "gxdevice.h"
  18. #include "gxdevmem.h" /* semi-public definitions */
  19. #include "gdevmem.h" /* private definitions */
  20. #define mem_gray8_strip_copy_rop mem_gray8_rgb24_strip_copy_rop
  21. /* ================ Standard (byte-oriented) device ================ */
  22. #undef chunk
  23. #define chunk byte
  24. /* Procedures */
  25. declare_mem_procs(mem_mapped8_copy_mono, mem_mapped8_copy_color, mem_mapped8_fill_rectangle);
  26. /* The device descriptor. */
  27. const gx_device_memory mem_mapped8_device =
  28. mem_device("image8", 8, 0,
  29. mem_mapped_map_rgb_color, mem_mapped_map_color_rgb,
  30. mem_mapped8_copy_mono, mem_mapped8_copy_color, mem_mapped8_fill_rectangle,
  31. mem_gray8_strip_copy_rop);
  32. /* Convert x coordinate to byte offset in scan line. */
  33. #undef x_to_byte
  34. #define x_to_byte(x) (x)
  35. /* Fill a rectangle with a color. */
  36. private int
  37. mem_mapped8_fill_rectangle(gx_device * dev,
  38. int x, int y, int w, int h, gx_color_index color)
  39. {
  40. gx_device_memory * const mdev = (gx_device_memory *)dev;
  41. fit_fill(dev, x, y, w, h);
  42. bytes_fill_rectangle(scan_line_base(mdev, y) + x, mdev->raster,
  43. (byte) color, w, h);
  44. return 0;
  45. }
  46. /* Copy a monochrome bitmap. */
  47. /* We split up this procedure because of limitations in the bcc32 compiler. */
  48. private void mapped8_copy01(chunk *, const byte *, int, int, uint,
  49. int, int, byte, byte);
  50. private void mapped8_copyN1(chunk *, const byte *, int, int, uint,
  51. int, int, byte);
  52. private void mapped8_copy0N(chunk *, const byte *, int, int, uint,
  53. int, int, byte);
  54. private int
  55. mem_mapped8_copy_mono(gx_device * dev,
  56. const byte * base, int sourcex, int sraster, gx_bitmap_id id,
  57. int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  58. {
  59. gx_device_memory * const mdev = (gx_device_memory *)dev;
  60. const byte *line;
  61. int first_bit;
  62. declare_scan_ptr(dest);
  63. fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  64. setup_rect(dest);
  65. line = base + (sourcex >> 3);
  66. first_bit = 0x80 >> (sourcex & 7);
  67. #define is_color(c) ((int)(c) != (int)gx_no_color_index)
  68. if (is_color(one)) {
  69. if (is_color(zero))
  70. mapped8_copy01(dest, line, first_bit, sraster, draster,
  71. w, h, (byte) zero, (byte) one);
  72. else
  73. mapped8_copyN1(dest, line, first_bit, sraster, draster,
  74. w, h, (byte) one);
  75. } else if (is_color(zero))
  76. mapped8_copy0N(dest, line, first_bit, sraster, draster,
  77. w, h, (byte) zero);
  78. #undef is_color
  79. return 0;
  80. }
  81. /* Macros for copy loops */
  82. #define COPY_BEGIN\
  83. while ( h-- > 0 )\
  84. { register byte *pptr = dest;\
  85. const byte *sptr = line;\
  86. register int sbyte = *sptr;\
  87. register uint bit = first_bit;\
  88. int count = w;\
  89. do\
  90. {
  91. #define COPY_END\
  92. if ( (bit >>= 1) == 0 )\
  93. bit = 0x80, sbyte = *++sptr;\
  94. pptr++;\
  95. }\
  96. while ( --count > 0 );\
  97. line += sraster;\
  98. inc_ptr(dest, draster);\
  99. }
  100. /* Halftone coloring */
  101. private void
  102. mapped8_copy01(chunk * dest, const byte * line, int first_bit,
  103. int sraster, uint draster, int w, int h, byte b0, byte b1)
  104. {
  105. COPY_BEGIN
  106. * pptr = (sbyte & bit ? b1 : b0);
  107. COPY_END
  108. }
  109. /* Stenciling */
  110. private void
  111. mapped8_copyN1(chunk * dest, const byte * line, int first_bit,
  112. int sraster, uint draster, int w, int h, byte b1)
  113. {
  114. COPY_BEGIN
  115. if (sbyte & bit)
  116. *pptr = b1;
  117. COPY_END
  118. }
  119. /* Reverse stenciling */
  120. private void
  121. mapped8_copy0N(chunk * dest, const byte * line, int first_bit,
  122. int sraster, uint draster, int w, int h, byte b0)
  123. {
  124. COPY_BEGIN
  125. if (!(sbyte & bit))
  126. *pptr = b0;
  127. COPY_END
  128. }
  129. #undef COPY_BEGIN
  130. #undef COPY_END
  131. /* Copy a color bitmap. */
  132. private int
  133. mem_mapped8_copy_color(gx_device * dev,
  134. const byte * base, int sourcex, int sraster, gx_bitmap_id id,
  135. int x, int y, int w, int h)
  136. {
  137. gx_device_memory * const mdev = (gx_device_memory *)dev;
  138. fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  139. mem_copy_byte_rect(mdev, base, sourcex, sraster, x, y, w, h);
  140. return 0;
  141. }
  142. /* ================ "Word"-oriented device ================ */
  143. /* Note that on a big-endian machine, this is the same as the */
  144. /* standard byte-oriented-device. */
  145. #if !arch_is_big_endian
  146. /* Procedures */
  147. declare_mem_procs(mem8_word_copy_mono, mem8_word_copy_color, mem8_word_fill_rectangle);
  148. /* Here is the device descriptor. */
  149. const gx_device_memory mem_mapped8_word_device =
  150. mem_full_device("image8w", 8, 0, mem_open,
  151. mem_mapped_map_rgb_color, mem_mapped_map_color_rgb,
  152. mem8_word_copy_mono, mem8_word_copy_color, mem8_word_fill_rectangle,
  153. gx_default_map_cmyk_color, gx_default_strip_tile_rectangle,
  154. gx_no_strip_copy_rop, mem_word_get_bits_rectangle);
  155. /* Fill a rectangle with a color. */
  156. private int
  157. mem8_word_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  158. gx_color_index color)
  159. {
  160. gx_device_memory * const mdev = (gx_device_memory *)dev;
  161. byte *base;
  162. uint raster;
  163. fit_fill(dev, x, y, w, h);
  164. base = scan_line_base(mdev, y);
  165. raster = mdev->raster;
  166. mem_swap_byte_rect(base, raster, x << 3, w << 3, h, true);
  167. bytes_fill_rectangle(base + x, raster, (byte) color, w, h);
  168. mem_swap_byte_rect(base, raster, x << 3, w << 3, h, true);
  169. return 0;
  170. }
  171. /* Copy a bitmap. */
  172. private int
  173. mem8_word_copy_mono(gx_device * dev,
  174. const byte * base, int sourcex, int sraster, gx_bitmap_id id,
  175. int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  176. {
  177. gx_device_memory * const mdev = (gx_device_memory *)dev;
  178. byte *row;
  179. uint raster;
  180. bool store;
  181. fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  182. row = scan_line_base(mdev, y);
  183. raster = mdev->raster;
  184. store = (zero != gx_no_color_index && one != gx_no_color_index);
  185. mem_swap_byte_rect(row, raster, x << 3, w << 3, h, store);
  186. mem_mapped8_copy_mono(dev, base, sourcex, sraster, id,
  187. x, y, w, h, zero, one);
  188. mem_swap_byte_rect(row, raster, x << 3, w << 3, h, false);
  189. return 0;
  190. }
  191. /* Copy a color bitmap. */
  192. private int
  193. mem8_word_copy_color(gx_device * dev,
  194. const byte * base, int sourcex, int sraster, gx_bitmap_id id,
  195. int x, int y, int w, int h)
  196. {
  197. gx_device_memory * const mdev = (gx_device_memory *)dev;
  198. byte *row;
  199. uint raster;
  200. fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  201. row = scan_line_base(mdev, y);
  202. raster = mdev->raster;
  203. mem_swap_byte_rect(row, raster, x << 3, w << 3, h, true);
  204. mem_copy_byte_rect(mdev, base, sourcex, sraster, x, y, w, h);
  205. mem_swap_byte_rect(row, raster, x << 3, w << 3, h, false);
  206. return 0;
  207. }
  208. #endif /* !arch_is_big_endian */