gdevmr1.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* Copyright (C) 1995, 2000 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: gdevmr1.c,v 1.2 2000/09/19 19:00:14 lpd Exp $ */
  16. /* RasterOp implementation for monobit memory devices */
  17. #include "memory_.h"
  18. #include "gx.h"
  19. #include "gsbittab.h"
  20. #include "gserrors.h"
  21. #include "gsropt.h"
  22. #include "gxcindex.h"
  23. #include "gxdcolor.h"
  24. #include "gxdevice.h"
  25. #include "gxdevmem.h"
  26. #include "gxdevrop.h"
  27. #include "gdevmrop.h"
  28. /* Calculate the X offset for a given Y value, */
  29. /* taking shift into account if necessary. */
  30. #define x_offset(px, ty, textures)\
  31. ((textures)->shift == 0 ? (px) :\
  32. (px) + (ty) / (textures)->rep_height * (textures)->rep_shift)
  33. /* ---------------- Monobit RasterOp ---------------- */
  34. int
  35. mem_mono_strip_copy_rop(gx_device * dev,
  36. const byte * sdata, int sourcex, uint sraster, gx_bitmap_id id,
  37. const gx_color_index * scolors,
  38. const gx_strip_bitmap * textures, const gx_color_index * tcolors,
  39. int x, int y, int width, int height,
  40. int phase_x, int phase_y, gs_logical_operation_t lop)
  41. {
  42. gx_device_memory *mdev = (gx_device_memory *) dev;
  43. gs_rop3_t rop = gs_transparent_rop(lop); /* handle transparency */
  44. gx_strip_bitmap no_texture;
  45. bool invert;
  46. uint draster = mdev->raster;
  47. uint traster;
  48. int line_count;
  49. byte *drow;
  50. const byte *srow;
  51. int ty;
  52. /* If map_rgb_color isn't the default one for monobit memory */
  53. /* devices, palette might not be set; set it now if needed. */
  54. if (mdev->palette.data == 0)
  55. gdev_mem_mono_set_inverted(mdev,
  56. (*dev_proc(dev, map_rgb_color))
  57. (dev, (gx_color_value) 0,
  58. (gx_color_value) 0, (gx_color_value) 0)
  59. != 0);
  60. invert = mdev->palette.data[0] != 0;
  61. #ifdef DEBUG
  62. if (gs_debug_c('b'))
  63. trace_copy_rop("mem_mono_strip_copy_rop",
  64. dev, sdata, sourcex, sraster,
  65. id, scolors, textures, tcolors,
  66. x, y, width, height, phase_x, phase_y, lop);
  67. if (gs_debug_c('B'))
  68. debug_dump_bitmap(scan_line_base(mdev, y), mdev->raster,
  69. height, "initial dest bits");
  70. #endif
  71. /*
  72. * RasterOp is defined as operating in RGB space; in the monobit
  73. * case, this means black = 0, white = 1. However, most monobit
  74. * devices use the opposite convention. To make this work,
  75. * we must precondition the Boolean operation by swapping the
  76. * order of bits end-for-end and then inverting.
  77. */
  78. if (invert)
  79. rop = byte_reverse_bits[rop] ^ 0xff;
  80. /*
  81. * From this point on, rop works in terms of device pixel values,
  82. * not RGB-space values.
  83. */
  84. /* Modify the raster operation according to the source palette. */
  85. if (scolors != 0) { /* Source with palette. */
  86. switch ((int)((scolors[1] << 1) + scolors[0])) {
  87. case 0:
  88. rop = rop3_know_S_0(rop);
  89. break;
  90. case 1:
  91. rop = rop3_invert_S(rop);
  92. break;
  93. case 2:
  94. break;
  95. case 3:
  96. rop = rop3_know_S_1(rop);
  97. break;
  98. }
  99. }
  100. /* Modify the raster operation according to the texture palette. */
  101. if (tcolors != 0) { /* Texture with palette. */
  102. switch ((int)((tcolors[1] << 1) + tcolors[0])) {
  103. case 0:
  104. rop = rop3_know_T_0(rop);
  105. break;
  106. case 1:
  107. rop = rop3_invert_T(rop);
  108. break;
  109. case 2:
  110. break;
  111. case 3:
  112. rop = rop3_know_T_1(rop);
  113. break;
  114. }
  115. }
  116. /* Handle constant source and/or texture, and other special cases. */
  117. {
  118. gx_color_index color0, color1;
  119. switch (rop_usage_table[rop]) {
  120. case rop_usage_none:
  121. /* We're just filling with a constant. */
  122. return (*dev_proc(dev, fill_rectangle))
  123. (dev, x, y, width, height, (gx_color_index) (rop & 1));
  124. case rop_usage_D:
  125. /* This is either D (no-op) or ~D. */
  126. if (rop == rop3_D)
  127. return 0;
  128. /* Code no_S inline, then finish with no_T. */
  129. fit_fill(dev, x, y, width, height);
  130. sdata = scan_line_base(mdev, 0);
  131. sourcex = x;
  132. sraster = 0;
  133. goto no_T;
  134. case rop_usage_S:
  135. /* This is either S or ~S, which copy_mono can handle. */
  136. if (rop == rop3_S)
  137. color0 = 0, color1 = 1;
  138. else
  139. color0 = 1, color1 = 0;
  140. do_copy:return (*dev_proc(dev, copy_mono))
  141. (dev, sdata, sourcex, sraster, id, x, y, width, height,
  142. color0, color1);
  143. case rop_usage_DS:
  144. /* This might be a case that copy_mono can handle. */
  145. #define copy_case(c0, c1) color0 = c0, color1 = c1; goto do_copy;
  146. switch ((uint) rop) { /* cast shuts up picky compilers */
  147. case rop3_D & rop3_not(rop3_S):
  148. copy_case(gx_no_color_index, 0);
  149. case rop3_D | rop3_S:
  150. copy_case(gx_no_color_index, 1);
  151. case rop3_D & rop3_S:
  152. copy_case(0, gx_no_color_index);
  153. case rop3_D | rop3_not(rop3_S):
  154. copy_case(1, gx_no_color_index);
  155. default:;
  156. }
  157. #undef copy_case
  158. fit_copy(dev, sdata, sourcex, sraster, id, x, y, width, height);
  159. no_T: /* Texture is not used; textures may be garbage. */
  160. no_texture.data = scan_line_base(mdev, 0); /* arbitrary */
  161. no_texture.raster = 0;
  162. no_texture.size.x = width;
  163. no_texture.size.y = height;
  164. no_texture.rep_width = no_texture.rep_height = 1;
  165. no_texture.rep_shift = no_texture.shift = 0;
  166. textures = &no_texture;
  167. break;
  168. case rop_usage_T:
  169. /* This is either T or ~T, which tile_rectangle can handle. */
  170. if (rop == rop3_T)
  171. color0 = 0, color1 = 1;
  172. else
  173. color0 = 1, color1 = 0;
  174. do_tile:return (*dev_proc(dev, strip_tile_rectangle))
  175. (dev, textures, x, y, width, height, color0, color1,
  176. phase_x, phase_y);
  177. case rop_usage_DT:
  178. /* This might be a case that tile_rectangle can handle. */
  179. #define tile_case(c0, c1) color0 = c0, color1 = c1; goto do_tile;
  180. switch ((uint) rop) { /* cast shuts up picky compilers */
  181. case rop3_D & rop3_not(rop3_T):
  182. tile_case(gx_no_color_index, 0);
  183. case rop3_D | rop3_T:
  184. tile_case(gx_no_color_index, 1);
  185. case rop3_D & rop3_T:
  186. tile_case(0, gx_no_color_index);
  187. case rop3_D | rop3_not(rop3_T):
  188. tile_case(1, gx_no_color_index);
  189. default:;
  190. }
  191. #undef tile_case
  192. fit_fill(dev, x, y, width, height);
  193. /* Source is not used; sdata et al may be garbage. */
  194. sdata = mdev->base; /* arbitrary, as long as all */
  195. /* accesses are valid */
  196. sourcex = x; /* guarantee no source skew */
  197. sraster = 0;
  198. break;
  199. default: /* rop_usage_[D]ST */
  200. fit_copy(dev, sdata, sourcex, sraster, id, x, y, width, height);
  201. }
  202. }
  203. #ifdef DEBUG
  204. if_debug1('b', "final rop=0x%x\n", rop);
  205. #endif
  206. /* Set up transfer parameters. */
  207. line_count = height;
  208. srow = sdata;
  209. drow = scan_line_base(mdev, y);
  210. traster = textures->raster;
  211. ty = y + phase_y;
  212. /* Loop over scan lines. */
  213. for (; line_count-- > 0; drow += draster, srow += sraster, ++ty) {
  214. int sx = sourcex;
  215. int dx = x;
  216. int w = width;
  217. const byte *trow =
  218. textures->data + (ty % textures->rep_height) * traster;
  219. int xoff = x_offset(phase_x, ty, textures);
  220. int nw;
  221. /* Loop over (horizontal) copies of the tile. */
  222. for (; w > 0; sx += nw, dx += nw, w -= nw) {
  223. int dbit = dx & 7;
  224. int sbit = sx & 7;
  225. int sskew = sbit - dbit;
  226. int tx = (dx + xoff) % textures->rep_width;
  227. int tbit = tx & 7;
  228. int tskew = tbit - dbit;
  229. int left = nw = min(w, textures->size.x - tx);
  230. byte lmask = 0xff >> dbit;
  231. byte rmask = 0xff << (~(dbit + nw - 1) & 7);
  232. byte mask = lmask;
  233. int nx = 8 - dbit;
  234. byte *dptr = drow + (dx >> 3);
  235. const byte *sptr = srow + (sx >> 3);
  236. const byte *tptr = trow + (tx >> 3);
  237. if (sskew < 0)
  238. --sptr, sskew += 8;
  239. if (tskew < 0)
  240. --tptr, tskew += 8;
  241. for (; left > 0;
  242. left -= nx, mask = 0xff, nx = 8,
  243. ++dptr, ++sptr, ++tptr
  244. ) {
  245. byte dbyte = *dptr;
  246. #define fetch1(ptr, skew)\
  247. (skew ? (ptr[0] << skew) + (ptr[1] >> (8 - skew)) : *ptr)
  248. byte sbyte = fetch1(sptr, sskew);
  249. byte tbyte = fetch1(tptr, tskew);
  250. #undef fetch1
  251. byte result =
  252. (*rop_proc_table[rop]) (dbyte, sbyte, tbyte);
  253. if (left <= nx)
  254. mask &= rmask;
  255. *dptr = (mask == 0xff ? result :
  256. (result & mask) | (dbyte & ~mask));
  257. }
  258. }
  259. }
  260. #ifdef DEBUG
  261. if (gs_debug_c('B'))
  262. debug_dump_bitmap(scan_line_base(mdev, y), mdev->raster,
  263. height, "final dest bits");
  264. #endif
  265. return 0;
  266. }