gdevm8.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gdevm8.c,v 1.2 2000/09/19 19:00:13 lpd Exp $ */
  16. /* 8-bit-per-pixel "memory" (stored bitmap) device */
  17. #include "memory_.h"
  18. #include "gx.h"
  19. #include "gxdevice.h"
  20. #include "gxdevmem.h" /* semi-public definitions */
  21. #include "gdevmem.h" /* private definitions */
  22. #define mem_gray8_strip_copy_rop mem_gray8_rgb24_strip_copy_rop
  23. /* ================ Standard (byte-oriented) device ================ */
  24. #undef chunk
  25. #define chunk byte
  26. /* Procedures */
  27. declare_mem_procs(mem_mapped8_copy_mono, mem_mapped8_copy_color, mem_mapped8_fill_rectangle);
  28. /* The device descriptor. */
  29. const gx_device_memory mem_mapped8_device =
  30. mem_device("image8", 8, 0,
  31. mem_mapped_map_rgb_color, mem_mapped_map_color_rgb,
  32. mem_mapped8_copy_mono, mem_mapped8_copy_color, mem_mapped8_fill_rectangle,
  33. mem_gray8_strip_copy_rop);
  34. /* Convert x coordinate to byte offset in scan line. */
  35. #undef x_to_byte
  36. #define x_to_byte(x) (x)
  37. /* Fill a rectangle with a color. */
  38. private int
  39. mem_mapped8_fill_rectangle(gx_device * dev,
  40. int x, int y, int w, int h, gx_color_index color)
  41. {
  42. gx_device_memory * const mdev = (gx_device_memory *)dev;
  43. fit_fill(dev, x, y, w, h);
  44. bytes_fill_rectangle(scan_line_base(mdev, y) + x, mdev->raster,
  45. (byte) color, w, h);
  46. return 0;
  47. }
  48. /* Copy a monochrome bitmap. */
  49. /* We split up this procedure because of limitations in the bcc32 compiler. */
  50. private void mapped8_copy01(P9(chunk *, const byte *, int, int, uint,
  51. int, int, byte, byte));
  52. private void mapped8_copyN1(P8(chunk *, const byte *, int, int, uint,
  53. int, int, byte));
  54. private void mapped8_copy0N(P8(chunk *, const byte *, int, int, uint,
  55. int, int, byte));
  56. private int
  57. mem_mapped8_copy_mono(gx_device * dev,
  58. const byte * base, int sourcex, int sraster, gx_bitmap_id id,
  59. int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  60. {
  61. gx_device_memory * const mdev = (gx_device_memory *)dev;
  62. const byte *line;
  63. int first_bit;
  64. declare_scan_ptr(dest);
  65. fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  66. setup_rect(dest);
  67. line = base + (sourcex >> 3);
  68. first_bit = 0x80 >> (sourcex & 7);
  69. #define is_color(c) ((int)(c) != (int)gx_no_color_index)
  70. if (is_color(one)) {
  71. if (is_color(zero))
  72. mapped8_copy01(dest, line, first_bit, sraster, draster,
  73. w, h, (byte) zero, (byte) one);
  74. else
  75. mapped8_copyN1(dest, line, first_bit, sraster, draster,
  76. w, h, (byte) one);
  77. } else if (is_color(zero))
  78. mapped8_copy0N(dest, line, first_bit, sraster, draster,
  79. w, h, (byte) zero);
  80. #undef is_color
  81. return 0;
  82. }
  83. /* Macros for copy loops */
  84. #define COPY_BEGIN\
  85. while ( h-- > 0 )\
  86. { register byte *pptr = dest;\
  87. const byte *sptr = line;\
  88. register int sbyte = *sptr;\
  89. register uint bit = first_bit;\
  90. int count = w;\
  91. do\
  92. {
  93. #define COPY_END\
  94. if ( (bit >>= 1) == 0 )\
  95. bit = 0x80, sbyte = *++sptr;\
  96. pptr++;\
  97. }\
  98. while ( --count > 0 );\
  99. line += sraster;\
  100. inc_ptr(dest, draster);\
  101. }
  102. /* Halftone coloring */
  103. private void
  104. mapped8_copy01(chunk * dest, const byte * line, int first_bit,
  105. int sraster, uint draster, int w, int h, byte b0, byte b1)
  106. {
  107. COPY_BEGIN
  108. * pptr = (sbyte & bit ? b1 : b0);
  109. COPY_END
  110. }
  111. /* Stenciling */
  112. private void
  113. mapped8_copyN1(chunk * dest, const byte * line, int first_bit,
  114. int sraster, uint draster, int w, int h, byte b1)
  115. {
  116. COPY_BEGIN
  117. if (sbyte & bit)
  118. *pptr = b1;
  119. COPY_END
  120. }
  121. /* Reverse stenciling */
  122. private void
  123. mapped8_copy0N(chunk * dest, const byte * line, int first_bit,
  124. int sraster, uint draster, int w, int h, byte b0)
  125. {
  126. COPY_BEGIN
  127. if (!(sbyte & bit))
  128. *pptr = b0;
  129. COPY_END
  130. }
  131. #undef COPY_BEGIN
  132. #undef COPY_END
  133. /* Copy a color bitmap. */
  134. private int
  135. mem_mapped8_copy_color(gx_device * dev,
  136. const byte * base, int sourcex, int sraster, gx_bitmap_id id,
  137. int x, int y, int w, int h)
  138. {
  139. gx_device_memory * const mdev = (gx_device_memory *)dev;
  140. fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  141. mem_copy_byte_rect(mdev, base, sourcex, sraster, x, y, w, h);
  142. return 0;
  143. }
  144. /* ================ "Word"-oriented device ================ */
  145. /* Note that on a big-endian machine, this is the same as the */
  146. /* standard byte-oriented-device. */
  147. #if !arch_is_big_endian
  148. /* Procedures */
  149. declare_mem_procs(mem8_word_copy_mono, mem8_word_copy_color, mem8_word_fill_rectangle);
  150. /* Here is the device descriptor. */
  151. const gx_device_memory mem_mapped8_word_device =
  152. mem_full_device("image8w", 8, 0, mem_open,
  153. mem_mapped_map_rgb_color, mem_mapped_map_color_rgb,
  154. mem8_word_copy_mono, mem8_word_copy_color, mem8_word_fill_rectangle,
  155. gx_default_map_cmyk_color, gx_default_strip_tile_rectangle,
  156. gx_no_strip_copy_rop, mem_word_get_bits_rectangle);
  157. /* Fill a rectangle with a color. */
  158. private int
  159. mem8_word_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  160. gx_color_index color)
  161. {
  162. gx_device_memory * const mdev = (gx_device_memory *)dev;
  163. byte *base;
  164. uint raster;
  165. fit_fill(dev, x, y, w, h);
  166. base = scan_line_base(mdev, y);
  167. raster = mdev->raster;
  168. mem_swap_byte_rect(base, raster, x << 3, w << 3, h, true);
  169. bytes_fill_rectangle(base + x, raster, (byte) color, w, h);
  170. mem_swap_byte_rect(base, raster, x << 3, w << 3, h, true);
  171. return 0;
  172. }
  173. /* Copy a bitmap. */
  174. private int
  175. mem8_word_copy_mono(gx_device * dev,
  176. const byte * base, int sourcex, int sraster, gx_bitmap_id id,
  177. int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  178. {
  179. gx_device_memory * const mdev = (gx_device_memory *)dev;
  180. byte *row;
  181. uint raster;
  182. bool store;
  183. fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  184. row = scan_line_base(mdev, y);
  185. raster = mdev->raster;
  186. store = (zero != gx_no_color_index && one != gx_no_color_index);
  187. mem_swap_byte_rect(row, raster, x << 3, w << 3, h, store);
  188. mem_mapped8_copy_mono(dev, base, sourcex, sraster, id,
  189. x, y, w, h, zero, one);
  190. mem_swap_byte_rect(row, raster, x << 3, w << 3, h, false);
  191. return 0;
  192. }
  193. /* Copy a color bitmap. */
  194. private int
  195. mem8_word_copy_color(gx_device * dev,
  196. const byte * base, int sourcex, int sraster, gx_bitmap_id id,
  197. int x, int y, int w, int h)
  198. {
  199. gx_device_memory * const mdev = (gx_device_memory *)dev;
  200. byte *row;
  201. uint raster;
  202. fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  203. row = scan_line_base(mdev, y);
  204. raster = mdev->raster;
  205. mem_swap_byte_rect(row, raster, x << 3, w << 3, h, true);
  206. mem_copy_byte_rect(mdev, base, sourcex, sraster, x, y, w, h);
  207. mem_swap_byte_rect(row, raster, x << 3, w << 3, h, false);
  208. return 0;
  209. }
  210. #endif /* !arch_is_big_endian */