gdevdgbr.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /* Copyright (C) 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: gdevdgbr.c,v 1.2 2000/09/19 19:00:12 lpd Exp $ */
  16. /* Default implementation of device get_bits[_rectangle] */
  17. #include "memory_.h"
  18. #include "gx.h"
  19. #include "gserrors.h"
  20. #include "gxdevice.h"
  21. #include "gxdevmem.h"
  22. #include "gxgetbit.h"
  23. #include "gxlum.h"
  24. #include "gdevmem.h"
  25. int
  26. gx_no_get_bits(gx_device * dev, int y, byte * data, byte ** actual_data)
  27. {
  28. return_error(gs_error_unknownerror);
  29. }
  30. int
  31. gx_default_get_bits(gx_device * dev, int y, byte * data, byte ** actual_data)
  32. { /*
  33. * Hand off to get_bits_rectangle, being careful to avoid a
  34. * possible recursion loop.
  35. */
  36. dev_proc_get_bits((*save_get_bits)) = dev_proc(dev, get_bits);
  37. gs_int_rect rect;
  38. gs_get_bits_params_t params;
  39. int code;
  40. rect.p.x = 0, rect.p.y = y;
  41. rect.q.x = dev->width, rect.q.y = y + 1;
  42. params.options =
  43. (actual_data ? GB_RETURN_POINTER : 0) | GB_RETURN_COPY |
  44. (GB_ALIGN_STANDARD | GB_OFFSET_0 | GB_RASTER_STANDARD |
  45. /* No depth specified, we always use native colors. */
  46. GB_PACKING_CHUNKY | GB_COLORS_NATIVE | GB_ALPHA_NONE);
  47. params.x_offset = 0;
  48. params.raster = bitmap_raster(dev->width * dev->color_info.depth);
  49. params.data[0] = data;
  50. set_dev_proc(dev, get_bits, gx_no_get_bits);
  51. code = (*dev_proc(dev, get_bits_rectangle))
  52. (dev, &rect, &params, NULL);
  53. if (actual_data)
  54. *actual_data = params.data[0];
  55. set_dev_proc(dev, get_bits, save_get_bits);
  56. return code;
  57. }
  58. /*
  59. * Determine whether we can satisfy a request by simply using the stored
  60. * representation. dev is used only for color_info.{num_components, depth}.
  61. */
  62. private bool
  63. requested_includes_stored(const gx_device *dev,
  64. const gs_get_bits_params_t *requested,
  65. const gs_get_bits_params_t *stored)
  66. {
  67. gs_get_bits_options_t both = requested->options & stored->options;
  68. if (!(both & GB_PACKING_ALL))
  69. return false;
  70. if (stored->options & GB_SELECT_PLANES) {
  71. /*
  72. * The device only provides a subset of the planes.
  73. * Make sure it provides all the requested ones.
  74. */
  75. int i;
  76. int n = (stored->options & GB_PACKING_BIT_PLANAR ?
  77. dev->color_info.depth : dev->color_info.num_components);
  78. if (!(requested->options & GB_SELECT_PLANES) ||
  79. !(both & (GB_PACKING_PLANAR || GB_PACKING_BIT_PLANAR))
  80. )
  81. return false;
  82. for (i = 0; i < n; ++i)
  83. if (requested->data[i] && !stored->data[i])
  84. return false;
  85. }
  86. if (both & GB_COLORS_NATIVE)
  87. return true;
  88. if (both & GB_COLORS_STANDARD_ALL) {
  89. if ((both & GB_ALPHA_ALL) && (both & GB_DEPTH_ALL))
  90. return true;
  91. }
  92. return false;
  93. }
  94. /*
  95. * Try to implement get_bits_rectangle by returning a pointer.
  96. * Note that dev is used only for computing the default raster
  97. * and for color_info.depth.
  98. * This routine does not check x or h for validity.
  99. */
  100. int
  101. gx_get_bits_return_pointer(gx_device * dev, int x, int h,
  102. gs_get_bits_params_t *params,
  103. const gs_get_bits_params_t *stored,
  104. byte * stored_base)
  105. {
  106. gs_get_bits_options_t options = params->options;
  107. gs_get_bits_options_t both = options & stored->options;
  108. if (!(options & GB_RETURN_POINTER) ||
  109. !requested_includes_stored(dev, params, stored)
  110. )
  111. return -1;
  112. /*
  113. * See whether we can return the bits in place. Note that even if
  114. * OFFSET_ANY isn't set, x_offset and x don't have to be equal: their
  115. * bit offsets only have to match modulo align_bitmap_mod * 8 (to
  116. * preserve alignment) if ALIGN_ANY isn't set, or mod 8 (since
  117. * byte alignment is always required) if ALIGN_ANY is set.
  118. */
  119. {
  120. int depth = dev->color_info.depth;
  121. /*
  122. * For PLANAR devices, we assume that each plane consists of
  123. * depth/num_components bits. This is wrong in general, but if
  124. * the device wants something else, it should implement
  125. * get_bits_rectangle itself.
  126. */
  127. uint dev_raster =
  128. (both & GB_PACKING_CHUNKY ?
  129. gx_device_raster(dev, true) :
  130. both & GB_PACKING_PLANAR ?
  131. bitmap_raster(dev->color_info.depth /
  132. dev->color_info.num_components * dev->width) :
  133. both & GB_PACKING_BIT_PLANAR ?
  134. bitmap_raster(dev->width) :
  135. 0 /* not possible */);
  136. uint raster =
  137. (options & (GB_RASTER_STANDARD | GB_RASTER_ANY) ? dev_raster :
  138. params->raster);
  139. byte *base;
  140. if (h <= 1 || raster == dev_raster) {
  141. int x_offset =
  142. (options & GB_OFFSET_ANY ? x :
  143. options & GB_OFFSET_0 ? 0 : params->x_offset);
  144. if (x_offset == x) {
  145. base = stored_base;
  146. params->x_offset = x;
  147. } else {
  148. uint align_mod =
  149. (options & GB_ALIGN_ANY ? 8 : align_bitmap_mod * 8);
  150. int bit_offset = x - x_offset;
  151. int bytes;
  152. if (bit_offset & (align_mod - 1))
  153. return -1; /* can't align */
  154. if (depth & (depth - 1)) {
  155. /* step = lcm(depth, align_mod) */
  156. int step = depth / igcd(depth, align_mod) * align_mod;
  157. bytes = bit_offset / step * step;
  158. } else {
  159. /* Use a faster algorithm if depth is a power of 2. */
  160. bytes = bit_offset & (-depth & -align_mod);
  161. }
  162. base = stored_base + arith_rshift(bytes, 3);
  163. params->x_offset = (bit_offset - bytes) / depth;
  164. }
  165. params->options =
  166. GB_ALIGN_STANDARD | GB_RETURN_POINTER | GB_RASTER_STANDARD |
  167. (stored->options & ~GB_PACKING_ALL) /*see below for PACKING*/ |
  168. (params->x_offset == 0 ? GB_OFFSET_0 : GB_OFFSET_SPECIFIED);
  169. if (both & GB_PACKING_CHUNKY) {
  170. params->options |= GB_PACKING_CHUNKY;
  171. params->data[0] = base;
  172. } else {
  173. int n =
  174. (stored->options & GB_PACKING_BIT_PLANAR ?
  175. (params->options |= GB_PACKING_BIT_PLANAR,
  176. dev->color_info.depth) :
  177. (params->options |= GB_PACKING_PLANAR,
  178. dev->color_info.num_components));
  179. int i;
  180. for (i = 0; i < n; ++i)
  181. if (!(both & GB_SELECT_PLANES) || stored->data[i] != 0) {
  182. params->data[i] = base;
  183. base += dev_raster * dev->height;
  184. }
  185. }
  186. return 0;
  187. }
  188. }
  189. return -1;
  190. }
  191. /*
  192. * Implement gx_get_bits_copy (see below) for the case of converting
  193. * 4-bit CMYK to 24-bit RGB with standard mapping, used heavily by PCL.
  194. */
  195. private void
  196. gx_get_bits_copy_cmyk_1bit(byte *dest_line, uint dest_raster,
  197. const byte *src_line, uint src_raster,
  198. int src_bit, int w, int h)
  199. {
  200. for (; h > 0; dest_line += dest_raster, src_line += src_raster, --h) {
  201. const byte *src = src_line;
  202. byte *dest = dest_line;
  203. bool hi = (src_bit & 4) != 0; /* last nibble processed was hi */
  204. int i;
  205. for (i = w; i > 0; dest += 3, --i) {
  206. uint pixel =
  207. ((hi = !hi)? *src >> 4 : *src++ & 0xf);
  208. if (pixel & 1)
  209. dest[0] = dest[1] = dest[2] = 0;
  210. else {
  211. dest[0] = (byte)((pixel >> 3) - 1);
  212. dest[1] = (byte)(((pixel >> 2) & 1) - 1);
  213. dest[2] = (byte)(((pixel >> 1) & 1) - 1);
  214. }
  215. }
  216. }
  217. }
  218. /*
  219. * Convert pixels between representations, primarily for get_bits_rectangle.
  220. * stored indicates how the data are actually stored, and includes:
  221. * - one option from the GB_PACKING group;
  222. * - if h > 1, one option from the GB_RASTER group;
  223. * - optionally (and normally), GB_COLORS_NATIVE;
  224. * - optionally, one option each from the GB_COLORS_STANDARD, GB_DEPTH,
  225. * and GB_ALPHA groups.
  226. * Note that dev is used only for color mapping. This routine assumes that
  227. * the stored data are aligned.
  228. *
  229. * Note: this routine does not check x, w, h for validity.
  230. *
  231. * The code for converting between standard and native colors has been
  232. * factored out into single-use procedures strictly for readability.
  233. * A good optimizing compiler would compile them in-line.
  234. */
  235. private int
  236. gx_get_bits_std_to_native(P10(gx_device * dev, int x, int w, int h,
  237. gs_get_bits_params_t * params,
  238. const gs_get_bits_params_t *stored,
  239. const byte * src_base, uint dev_raster,
  240. int x_offset, uint raster)),
  241. gx_get_bits_native_to_std(P11(gx_device * dev, int x, int w, int h,
  242. gs_get_bits_params_t * params,
  243. const gs_get_bits_params_t *stored,
  244. const byte * src_base, uint dev_raster,
  245. int x_offset, uint raster, uint std_raster));
  246. int
  247. gx_get_bits_copy(gx_device * dev, int x, int w, int h,
  248. gs_get_bits_params_t * params,
  249. const gs_get_bits_params_t *stored,
  250. const byte * src_base, uint dev_raster)
  251. {
  252. gs_get_bits_options_t options = params->options;
  253. gs_get_bits_options_t stored_options = stored->options;
  254. int x_offset = (options & GB_OFFSET_0 ? 0 : params->x_offset);
  255. int depth = dev->color_info.depth;
  256. int bit_x = x * depth;
  257. const byte *src = src_base;
  258. /*
  259. * If the stored representation matches a requested representation,
  260. * we can copy the data without any transformations.
  261. */
  262. bool direct_copy = requested_includes_stored(dev, params, stored);
  263. int code = 0;
  264. /*
  265. * The request must include either GB_PACKING_CHUNKY or
  266. * GB_PACKING_PLANAR + GB_SELECT_PLANES, GB_RETURN_COPY,
  267. * and an offset and raster specification. In the planar case,
  268. * the request must include GB_ALIGN_STANDARD, the stored
  269. * representation must include GB_PACKING_CHUNKY, and both must
  270. * include GB_COLORS_NATIVE.
  271. */
  272. if ((~options & GB_RETURN_COPY) ||
  273. !(options & (GB_OFFSET_0 | GB_OFFSET_SPECIFIED)) ||
  274. !(options & (GB_RASTER_STANDARD | GB_RASTER_SPECIFIED))
  275. )
  276. return_error(gs_error_rangecheck);
  277. if (options & GB_PACKING_CHUNKY) {
  278. byte *data = params->data[0];
  279. int end_bit = (x_offset + w) * depth;
  280. uint std_raster =
  281. (options & GB_ALIGN_STANDARD ? bitmap_raster(end_bit) :
  282. (end_bit + 7) >> 3);
  283. uint raster =
  284. (options & GB_RASTER_STANDARD ? std_raster : params->raster);
  285. int dest_bit_x = x_offset * depth;
  286. int skew = bit_x - dest_bit_x;
  287. /*
  288. * If the bit positions line up, use bytes_copy_rectangle.
  289. * Since bytes_copy_rectangle doesn't require alignment,
  290. * the bit positions only have to match within a byte,
  291. * not within align_bitmap_mod bytes.
  292. */
  293. if (!(skew & 7) && direct_copy) {
  294. int bit_w = w * depth;
  295. bytes_copy_rectangle(data + (dest_bit_x >> 3), raster,
  296. src + (bit_x >> 3), dev_raster,
  297. ((bit_x + bit_w + 7) >> 3) - (bit_x >> 3), h);
  298. } else if (direct_copy) {
  299. /*
  300. * Use the logic already in mem_mono_copy_mono to copy the
  301. * bits to the destination. We do this one line at a time,
  302. * to avoid having to allocate a line pointer table.
  303. */
  304. gx_device_memory tdev;
  305. byte *line_ptr = data;
  306. tdev.line_ptrs = &tdev.base;
  307. for (; h > 0; line_ptr += raster, src += dev_raster, --h) {
  308. /* Make sure the destination is aligned. */
  309. int align = ALIGNMENT_MOD(line_ptr, align_bitmap_mod);
  310. tdev.base = line_ptr - align;
  311. (*dev_proc(&mem_mono_device, copy_mono))
  312. ((gx_device *) & tdev, src, bit_x, dev_raster, gx_no_bitmap_id,
  313. dest_bit_x + (align << 3), 0, w, 1,
  314. (gx_color_index) 0, (gx_color_index) 1);
  315. }
  316. } else if (options & ~stored_options & GB_COLORS_NATIVE) {
  317. /* Convert standard colors to native. */
  318. code = gx_get_bits_std_to_native(dev, x, w, h, params, stored,
  319. src_base, dev_raster,
  320. x_offset, raster);
  321. options = params->options;
  322. } else {
  323. /* Convert native colors to standard. */
  324. code = gx_get_bits_native_to_std(dev, x, w, h, params, stored,
  325. src_base, dev_raster,
  326. x_offset, raster, std_raster);
  327. options = params->options;
  328. }
  329. params->options =
  330. (options & (GB_COLORS_ALL | GB_ALPHA_ALL)) | GB_PACKING_CHUNKY |
  331. (options & GB_COLORS_NATIVE ? 0 : options & GB_DEPTH_ALL) |
  332. (options & GB_ALIGN_STANDARD ? GB_ALIGN_STANDARD : GB_ALIGN_ANY) |
  333. GB_RETURN_COPY |
  334. (x_offset == 0 ? GB_OFFSET_0 : GB_OFFSET_SPECIFIED) |
  335. (raster == std_raster ? GB_RASTER_STANDARD : GB_RASTER_SPECIFIED);
  336. } else if (!(~options &
  337. (GB_PACKING_PLANAR | GB_SELECT_PLANES | GB_ALIGN_STANDARD)) &&
  338. (stored_options & GB_PACKING_CHUNKY) &&
  339. ((options & stored_options) & GB_COLORS_NATIVE)
  340. ) {
  341. int num_planes = dev->color_info.num_components;
  342. int dest_depth = depth / num_planes;
  343. bits_plane_t source, dest;
  344. int plane = -1;
  345. int i;
  346. /* Make sure only one plane is being requested. */
  347. for (i = 0; i < num_planes; ++i)
  348. if (params->data[i] != 0) {
  349. if (plane >= 0)
  350. return_error(gs_error_rangecheck); /* > 1 plane */
  351. plane = i;
  352. }
  353. source.data.read = src_base;
  354. source.raster = dev_raster;
  355. source.depth = depth;
  356. source.x = x;
  357. dest.data.write = params->data[plane];
  358. dest.raster =
  359. (options & GB_RASTER_STANDARD ?
  360. bitmap_raster((x_offset + w) * dest_depth) : params->raster);
  361. dest.depth = dest_depth;
  362. dest.x = x_offset;
  363. return bits_extract_plane(&dest, &source,
  364. (num_planes - 1 - plane) * dest_depth,
  365. w, h);
  366. } else
  367. return_error(gs_error_rangecheck);
  368. return code;
  369. }
  370. /*
  371. * Convert standard colors to native. Note that the source
  372. * may have depths other than 8 bits per component.
  373. */
  374. private int
  375. gx_get_bits_std_to_native(gx_device * dev, int x, int w, int h,
  376. gs_get_bits_params_t * params,
  377. const gs_get_bits_params_t *stored,
  378. const byte * src_base, uint dev_raster,
  379. int x_offset, uint raster)
  380. {
  381. int depth = dev->color_info.depth;
  382. int dest_bit_offset = x_offset * depth;
  383. byte *dest_line = params->data[0] + (dest_bit_offset >> 3);
  384. int ncolors =
  385. (stored->options & GB_COLORS_RGB ? 3 :
  386. stored->options & GB_COLORS_CMYK ? 4 :
  387. stored->options & GB_COLORS_GRAY ? 1 : -1);
  388. int ncomp = ncolors +
  389. ((stored->options & (GB_ALPHA_FIRST | GB_ALPHA_LAST)) != 0);
  390. int src_depth = GB_OPTIONS_DEPTH(stored->options);
  391. int src_bit_offset = x * src_depth * ncomp;
  392. const byte *src_line = src_base + (src_bit_offset >> 3);
  393. gx_color_value src_max = (1 << src_depth) - 1;
  394. #define v2cv(value) ((ulong)(value) * gx_max_color_value / src_max)
  395. gx_color_value alpha_default = src_max;
  396. params->options &= ~GB_COLORS_ALL | GB_COLORS_NATIVE;
  397. for (; h > 0; dest_line += raster, src_line += dev_raster, --h) {
  398. int i;
  399. sample_load_declare_setup(src, sbit, src_line,
  400. src_bit_offset & 7, src_depth);
  401. sample_store_declare_setup(dest, dbit, dbyte, dest_line,
  402. dest_bit_offset & 7, depth);
  403. for (i = 0; i < w; ++i) {
  404. int j;
  405. gx_color_value v[4], va = alpha_default;
  406. gx_color_index pixel;
  407. /* Fetch the source data. */
  408. if (stored->options & GB_ALPHA_FIRST) {
  409. sample_load_next16(va, src, sbit, src_depth);
  410. va = v2cv(va);
  411. }
  412. for (j = 0; j < ncolors; ++j) {
  413. gx_color_value vj;
  414. sample_load_next16(vj, src, sbit, src_depth);
  415. v[j] = v2cv(vj);
  416. }
  417. if (stored->options & GB_ALPHA_LAST) {
  418. sample_load_next16(va, src, sbit, src_depth);
  419. va = v2cv(va);
  420. }
  421. /* Convert and store the pixel value. */
  422. switch (ncolors) {
  423. case 1:
  424. v[2] = v[1] = v[0];
  425. case 3:
  426. pixel = (*dev_proc(dev, map_rgb_alpha_color))
  427. (dev, v[0], v[1], v[2], va);
  428. break;
  429. case 4:
  430. /****** NO ALPHA FOR CMYK ******/
  431. pixel = (*dev_proc(dev, map_cmyk_color))
  432. (dev, v[0], v[1], v[2], v[3]);
  433. break;
  434. default:
  435. return_error(gs_error_rangecheck);
  436. }
  437. sample_store_next32(pixel, dest, dbit, depth, dbyte);
  438. }
  439. sample_store_flush(dest, dbit, depth, dbyte);
  440. }
  441. return 0;
  442. }
  443. /*
  444. * Convert native colors to standard. Only GB_DEPTH_8 is supported.
  445. */
  446. private int
  447. gx_get_bits_native_to_std(gx_device * dev, int x, int w, int h,
  448. gs_get_bits_params_t * params,
  449. const gs_get_bits_params_t *stored,
  450. const byte * src_base, uint dev_raster,
  451. int x_offset, uint raster, uint std_raster)
  452. {
  453. int depth = dev->color_info.depth;
  454. int src_bit_offset = x * depth;
  455. const byte *src_line = src_base + (src_bit_offset >> 3);
  456. gs_get_bits_options_t options = params->options;
  457. int ncomp =
  458. (options & (GB_ALPHA_FIRST | GB_ALPHA_LAST) ? 4 : 3);
  459. byte *dest_line = params->data[0] + x_offset * ncomp;
  460. byte *mapped[16];
  461. int dest_bytes;
  462. int i;
  463. if (!(options & GB_DEPTH_8)) {
  464. /*
  465. * We don't support general depths yet, or conversion between
  466. * different formats. Punt.
  467. */
  468. return_error(gs_error_rangecheck);
  469. }
  470. /* Pick the representation that's most likely to be useful. */
  471. if (options & GB_COLORS_RGB)
  472. params->options = options &= ~GB_COLORS_STANDARD_ALL | GB_COLORS_RGB,
  473. dest_bytes = 3;
  474. else if (options & GB_COLORS_CMYK)
  475. params->options = options &= ~GB_COLORS_STANDARD_ALL | GB_COLORS_CMYK,
  476. dest_bytes = 4;
  477. else if (options & GB_COLORS_GRAY)
  478. params->options = options &= ~GB_COLORS_STANDARD_ALL | GB_COLORS_GRAY,
  479. dest_bytes = 1;
  480. else
  481. return_error(gs_error_rangecheck);
  482. /* Recompute the destination raster based on the color space. */
  483. if (options & GB_RASTER_STANDARD) {
  484. uint end_byte = (x_offset + w) * dest_bytes;
  485. raster = std_raster =
  486. (options & GB_ALIGN_STANDARD ?
  487. bitmap_raster(end_byte << 3) : end_byte);
  488. }
  489. /* Check for the one special case we care about. */
  490. if (((options & (GB_COLORS_RGB | GB_ALPHA_FIRST | GB_ALPHA_LAST))
  491. == GB_COLORS_RGB) &&
  492. dev_proc(dev, map_color_rgb) == cmyk_1bit_map_color_rgb) {
  493. gx_get_bits_copy_cmyk_1bit(dest_line, raster,
  494. src_line, dev_raster,
  495. src_bit_offset & 7, w, h);
  496. return 0;
  497. }
  498. if (options & (GB_ALPHA_FIRST | GB_ALPHA_LAST))
  499. ++dest_bytes;
  500. /* Clear the color translation cache. */
  501. for (i = (depth > 4 ? 16 : 1 << depth); --i >= 0; )
  502. mapped[i] = 0;
  503. for (; h > 0; dest_line += raster, src_line += dev_raster, --h) {
  504. sample_load_declare_setup(src, bit, src_line,
  505. src_bit_offset & 7, depth);
  506. byte *dest = dest_line;
  507. for (i = 0; i < w; ++i) {
  508. gx_color_index pixel = 0;
  509. gx_color_value rgba[4];
  510. sample_load_next32(pixel, src, bit, depth);
  511. if (pixel < 16) {
  512. if (mapped[pixel]) {
  513. /* Use the value from the cache. */
  514. memcpy(dest, mapped[pixel], dest_bytes);
  515. dest += dest_bytes;
  516. continue;
  517. }
  518. mapped[pixel] = dest;
  519. }
  520. (*dev_proc(dev, map_color_rgb_alpha)) (dev, pixel, rgba);
  521. if (options & GB_ALPHA_FIRST)
  522. *dest++ = gx_color_value_to_byte(rgba[3]);
  523. /* Convert to the requested color space. */
  524. if (options & GB_COLORS_RGB) {
  525. dest[0] = gx_color_value_to_byte(rgba[0]);
  526. dest[1] = gx_color_value_to_byte(rgba[1]);
  527. dest[2] = gx_color_value_to_byte(rgba[2]);
  528. dest += 3;
  529. } else if (options & GB_COLORS_CMYK) {
  530. /* Use the standard RGB to CMYK algorithm, */
  531. /* with maximum black generation and undercolor removal. */
  532. gx_color_value white = max(rgba[0], max(rgba[1], rgba[2]));
  533. dest[0] = gx_color_value_to_byte(white - rgba[0]);
  534. dest[1] = gx_color_value_to_byte(white - rgba[1]);
  535. dest[2] = gx_color_value_to_byte(white - rgba[2]);
  536. dest[3] = gx_color_value_to_byte(gx_max_color_value - white);
  537. dest += 4;
  538. } else { /* GB_COLORS_GRAY */
  539. /* Use the standard RGB to Gray algorithm. */
  540. *dest++ = gx_color_value_to_byte(
  541. ((rgba[0] * (ulong) lum_red_weight) +
  542. (rgba[1] * (ulong) lum_green_weight) +
  543. (rgba[2] * (ulong) lum_blue_weight) +
  544. (lum_all_weights / 2))
  545. / lum_all_weights);
  546. }
  547. if (options & GB_ALPHA_LAST)
  548. *dest++ = gx_color_value_to_byte(rgba[3]);
  549. }
  550. }
  551. return 0;
  552. }
  553. /* ------ Default implementations of get_bits_rectangle ------ */
  554. int
  555. gx_no_get_bits_rectangle(gx_device * dev, const gs_int_rect * prect,
  556. gs_get_bits_params_t * params, gs_int_rect ** unread)
  557. {
  558. return_error(gs_error_unknownerror);
  559. }
  560. int
  561. gx_default_get_bits_rectangle(gx_device * dev, const gs_int_rect * prect,
  562. gs_get_bits_params_t * params, gs_int_rect ** unread)
  563. {
  564. dev_proc_get_bits_rectangle((*save_get_bits_rectangle)) =
  565. dev_proc(dev, get_bits_rectangle);
  566. int depth = dev->color_info.depth;
  567. uint min_raster = (dev->width * depth + 7) >> 3;
  568. gs_get_bits_options_t options = params->options;
  569. int code;
  570. /* Avoid a recursion loop. */
  571. set_dev_proc(dev, get_bits_rectangle, gx_no_get_bits_rectangle);
  572. /*
  573. * If the parameters are right, try to call get_bits directly. Note
  574. * that this may fail if a device only implements get_bits_rectangle
  575. * (not get_bits) for a limited set of options. Note also that this
  576. * must handle the case of the recursive call from within
  577. * get_bits_rectangle (see below): because of this, and only because
  578. * of this, it must handle partial scan lines.
  579. */
  580. if (prect->q.y == prect->p.y + 1 &&
  581. !(~options &
  582. (GB_RETURN_COPY | GB_PACKING_CHUNKY | GB_COLORS_NATIVE)) &&
  583. (options & (GB_ALIGN_STANDARD | GB_ALIGN_ANY)) &&
  584. ((options & (GB_OFFSET_0 | GB_OFFSET_ANY)) ||
  585. ((options & GB_OFFSET_SPECIFIED) && params->x_offset == 0)) &&
  586. ((options & (GB_RASTER_STANDARD | GB_RASTER_ANY)) ||
  587. ((options & GB_RASTER_SPECIFIED) &&
  588. params->raster >= min_raster)) &&
  589. unread == NULL
  590. ) {
  591. byte *data = params->data[0];
  592. byte *row = data;
  593. if (!(prect->p.x == 0 && prect->q.x == dev->width)) {
  594. /* Allocate an intermediate row buffer. */
  595. row = gs_alloc_bytes(dev->memory, min_raster,
  596. "gx_default_get_bits_rectangle");
  597. if (row == 0) {
  598. code = gs_note_error(gs_error_VMerror);
  599. goto ret;
  600. }
  601. }
  602. code = (*dev_proc(dev, get_bits))
  603. (dev, prect->p.y, row, &params->data[0]);
  604. if (code >= 0) {
  605. if (row != data) {
  606. if (prect->p.x == 0 && params->data[0] != row) {
  607. /*
  608. * get_bits returned an appropriate pointer: we can
  609. * avoid doing any copying.
  610. */
  611. DO_NOTHING;
  612. } else {
  613. /* Copy the partial row into the supplied buffer. */
  614. int width_bits = (prect->q.x - prect->p.x) * depth;
  615. gx_device_memory tdev;
  616. tdev.width = width_bits;
  617. tdev.height = 1;
  618. tdev.line_ptrs = &tdev.base;
  619. tdev.base = data;
  620. code = (*dev_proc(&mem_mono_device, copy_mono))
  621. ((gx_device *) & tdev, params->data[0], prect->p.x * depth,
  622. min_raster, gx_no_bitmap_id, 0, 0, width_bits, 1,
  623. (gx_color_index) 0, (gx_color_index) 1);
  624. params->data[0] = data;
  625. }
  626. gs_free_object(dev->memory, row,
  627. "gx_default_get_bits_rectangle");
  628. }
  629. params->options =
  630. GB_ALIGN_STANDARD | GB_OFFSET_0 | GB_PACKING_CHUNKY |
  631. GB_ALPHA_NONE | GB_COLORS_NATIVE | GB_RASTER_STANDARD |
  632. (params->data[0] == data ? GB_RETURN_COPY : GB_RETURN_POINTER);
  633. goto ret;
  634. }
  635. } {
  636. /* Do the transfer row-by-row using a buffer. */
  637. int x = prect->p.x, w = prect->q.x - x;
  638. int bits_per_pixel = depth;
  639. byte *row;
  640. if (options & GB_COLORS_STANDARD_ALL) {
  641. /*
  642. * Make sure the row buffer can hold the standard color
  643. * representation, in case the device decides to use it.
  644. */
  645. int bpc = GB_OPTIONS_MAX_DEPTH(options);
  646. int nc =
  647. (options & GB_COLORS_CMYK ? 4 :
  648. options & GB_COLORS_RGB ? 3 : 1) +
  649. (options & (GB_ALPHA_ALL - GB_ALPHA_NONE) ? 1 : 0);
  650. int bpp = bpc * nc;
  651. if (bpp > bits_per_pixel)
  652. bits_per_pixel = bpp;
  653. }
  654. row = gs_alloc_bytes(dev->memory, (bits_per_pixel * w + 7) >> 3,
  655. "gx_default_get_bits_rectangle");
  656. if (row == 0) {
  657. code = gs_note_error(gs_error_VMerror);
  658. } else {
  659. uint dev_raster = gx_device_raster(dev, true);
  660. uint raster =
  661. (options & GB_RASTER_SPECIFIED ? params->raster :
  662. options & GB_ALIGN_STANDARD ? bitmap_raster(depth * w) :
  663. (depth * w + 7) >> 3);
  664. gs_int_rect rect;
  665. gs_get_bits_params_t copy_params;
  666. gs_get_bits_options_t copy_options =
  667. (GB_ALIGN_STANDARD | GB_ALIGN_ANY) |
  668. (GB_RETURN_COPY | GB_RETURN_POINTER) |
  669. (GB_OFFSET_0 | GB_OFFSET_ANY) |
  670. (GB_RASTER_STANDARD | GB_RASTER_ANY) | GB_PACKING_CHUNKY |
  671. GB_COLORS_NATIVE | (options & (GB_DEPTH_ALL | GB_COLORS_ALL)) |
  672. GB_ALPHA_ALL;
  673. byte *dest = params->data[0];
  674. int y;
  675. rect.p.x = x, rect.q.x = x + w;
  676. code = 0;
  677. for (y = prect->p.y; y < prect->q.y; ++y) {
  678. rect.p.y = y, rect.q.y = y + 1;
  679. copy_params.options = copy_options;
  680. copy_params.data[0] = row;
  681. code = (*save_get_bits_rectangle)
  682. (dev, &rect, &copy_params, NULL);
  683. if (code < 0)
  684. break;
  685. if (copy_params.options & GB_OFFSET_0)
  686. copy_params.x_offset = 0;
  687. params->data[0] = dest + (y - prect->p.y) * raster;
  688. code = gx_get_bits_copy(dev, copy_params.x_offset, w, 1,
  689. params, &copy_params,
  690. copy_params.data[0], dev_raster);
  691. if (code < 0)
  692. break;
  693. }
  694. gs_free_object(dev->memory, row, "gx_default_get_bits_rectangle");
  695. params->data[0] = dest;
  696. }
  697. }
  698. ret:set_dev_proc(dev, get_bits_rectangle, save_get_bits_rectangle);
  699. return (code < 0 ? code : 0);
  700. }
  701. /* ------ Debugging printout ------ */
  702. #ifdef DEBUG
  703. void
  704. debug_print_gb_options(gx_bitmap_format_t options)
  705. {
  706. static const char *const option_names[] = {
  707. GX_BITMAP_FORMAT_NAMES
  708. };
  709. const char *prev = " ";
  710. int i;
  711. dlprintf1("0x%lx", (ulong) options);
  712. for (i = 0; i < sizeof(options) * 8; ++i)
  713. if ((options >> i) & 1) {
  714. dprintf2("%c%s",
  715. (!memcmp(prev, option_names[i], 3) ? '|' : ','),
  716. option_names[i]);
  717. prev = option_names[i];
  718. }
  719. dputc('\n');
  720. }
  721. void
  722. debug_print_gb_planes(const gs_get_bits_params_t * params, int num_planes)
  723. {
  724. gs_get_bits_options_t options = params->options;
  725. int i;
  726. debug_print_gb_options(options);
  727. for (i = 0; i < num_planes; ++i)
  728. dprintf2("data[%d]=0x%lx ", i, (ulong)params->data[i]);
  729. if (options & GB_OFFSET_SPECIFIED)
  730. dprintf1("x_offset=%d ", params->x_offset);
  731. if (options & GB_RASTER_SPECIFIED)
  732. dprintf1("raster=%u", params->raster);
  733. dputc('\n');
  734. }
  735. void
  736. debug_print_gb_params(const gs_get_bits_params_t * params)
  737. {
  738. debug_print_gb_planes(params, 1);
  739. }
  740. #endif /* DEBUG */