gxht.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /* Copyright (C) 1989, 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: gxht.c,v 1.17 2005/05/23 22:33:22 dan Exp $ */
  14. /* Halftone rendering for imaging library */
  15. #include "memory_.h"
  16. #include "gx.h"
  17. #include "gserrors.h"
  18. #include "gsstruct.h"
  19. #include "gsbitops.h"
  20. #include "gsutil.h" /* for gs_next_ids */
  21. #include "gxdcolor.h"
  22. #include "gxfixed.h"
  23. #include "gxdevice.h" /* for gzht.h */
  24. #include "gxistate.h"
  25. #include "gzht.h"
  26. #include "gsserial.h"
  27. /* Define the sizes of the halftone cache. */
  28. #define max_cached_tiles_HUGE 5000 /* not used */
  29. #define max_ht_bits_HUGE 1000000 /* not used */
  30. #define max_cached_tiles_LARGE 577
  31. #define max_ht_bits_LARGE 100000
  32. #define max_cached_tiles_SMALL 25
  33. #define max_ht_bits_SMALL 1000
  34. /* Define the binary halftone device color type. */
  35. /* The type descriptor must be public for Pattern types. */
  36. gs_public_st_composite(st_dc_ht_binary, gx_device_color, "dc_ht_binary",
  37. dc_ht_binary_enum_ptrs, dc_ht_binary_reloc_ptrs);
  38. private dev_color_proc_save_dc(gx_dc_ht_binary_save_dc);
  39. private dev_color_proc_get_dev_halftone(gx_dc_ht_binary_get_dev_halftone);
  40. private dev_color_proc_load(gx_dc_ht_binary_load);
  41. private dev_color_proc_fill_rectangle(gx_dc_ht_binary_fill_rectangle);
  42. private dev_color_proc_fill_masked(gx_dc_ht_binary_fill_masked);
  43. private dev_color_proc_equal(gx_dc_ht_binary_equal);
  44. private dev_color_proc_write(gx_dc_ht_binary_write);
  45. private dev_color_proc_read(gx_dc_ht_binary_read);
  46. const gx_device_color_type_t
  47. gx_dc_type_data_ht_binary =
  48. {&st_dc_ht_binary,
  49. gx_dc_ht_binary_save_dc, gx_dc_ht_binary_get_dev_halftone,
  50. gx_dc_ht_get_phase,
  51. gx_dc_ht_binary_load, gx_dc_ht_binary_fill_rectangle,
  52. gx_dc_ht_binary_fill_masked, gx_dc_ht_binary_equal,
  53. gx_dc_ht_binary_write, gx_dc_ht_binary_read,
  54. gx_dc_ht_binary_get_nonzero_comps
  55. };
  56. #undef gx_dc_type_ht_binary
  57. const gx_device_color_type_t *const gx_dc_type_ht_binary =
  58. &gx_dc_type_data_ht_binary;
  59. #define gx_dc_type_ht_binary (&gx_dc_type_data_ht_binary)
  60. /* GC procedures */
  61. private
  62. ENUM_PTRS_WITH(dc_ht_binary_enum_ptrs, gx_device_color *cptr) return 0;
  63. ENUM_PTR(0, gx_device_color, colors.binary.b_ht);
  64. case 1:
  65. {
  66. gx_ht_tile *tile = cptr->colors.binary.b_tile;
  67. ENUM_RETURN(tile ? tile - tile->index : 0);
  68. }
  69. ENUM_PTRS_END
  70. private RELOC_PTRS_WITH(dc_ht_binary_reloc_ptrs, gx_device_color *cptr)
  71. {
  72. gx_ht_tile *tile = cptr->colors.binary.b_tile;
  73. uint index = tile ? tile->index : 0;
  74. RELOC_PTR(gx_device_color, colors.binary.b_ht);
  75. RELOC_TYPED_OFFSET_PTR(gx_device_color, colors.binary.b_tile, index);
  76. }
  77. RELOC_PTRS_END
  78. #undef cptr
  79. /* Other GC procedures */
  80. private_st_ht_tiles();
  81. private
  82. ENUM_PTRS_BEGIN_PROC(ht_tiles_enum_ptrs)
  83. {
  84. return 0;
  85. }
  86. ENUM_PTRS_END_PROC
  87. private RELOC_PTRS_BEGIN(ht_tiles_reloc_ptrs)
  88. {
  89. /* Reset the bitmap pointers in the tiles. */
  90. /* We know the first tile points to the base of the bits. */
  91. gx_ht_tile *ht_tiles = vptr;
  92. byte *bits = ht_tiles->tiles.data;
  93. uint diff;
  94. if (bits == 0)
  95. return;
  96. RELOC_VAR(bits);
  97. if (size == size_of(gx_ht_tile)) { /* only 1 tile */
  98. ht_tiles->tiles.data = bits;
  99. return;
  100. }
  101. diff = ht_tiles[1].tiles.data - ht_tiles[0].tiles.data;
  102. for (; size; ht_tiles++, size -= size_of(gx_ht_tile), bits += diff) {
  103. ht_tiles->tiles.data = bits;
  104. }
  105. }
  106. RELOC_PTRS_END
  107. private_st_ht_cache();
  108. /* Return the default sizes of the halftone cache. */
  109. uint
  110. gx_ht_cache_default_tiles(void)
  111. {
  112. #if arch_small_memory
  113. return max_cached_tiles_SMALL;
  114. #else
  115. return (gs_debug_c('.') ? max_cached_tiles_SMALL :
  116. max_cached_tiles_LARGE);
  117. #endif
  118. }
  119. uint
  120. gx_ht_cache_default_bits(void)
  121. {
  122. #if arch_small_memory
  123. return max_ht_bits_SMALL;
  124. #else
  125. return (gs_debug_c('.') ? max_ht_bits_SMALL :
  126. max_ht_bits_LARGE);
  127. #endif
  128. }
  129. /* Allocate a halftone cache. */
  130. gx_ht_cache *
  131. gx_ht_alloc_cache(gs_memory_t * mem, uint max_tiles, uint max_bits)
  132. {
  133. gx_ht_cache *pcache =
  134. gs_alloc_struct(mem, gx_ht_cache, &st_ht_cache,
  135. "alloc_ht_cache(struct)");
  136. byte *tbits =
  137. gs_alloc_bytes(mem, max_bits, "alloc_ht_cache(bits)");
  138. gx_ht_tile *ht_tiles =
  139. gs_alloc_struct_array(mem, max_tiles, gx_ht_tile, &st_ht_tiles,
  140. "alloc_ht_cache(ht_tiles)");
  141. if (pcache == 0 || tbits == 0 || ht_tiles == 0) {
  142. gs_free_object(mem, ht_tiles, "alloc_ht_cache(ht_tiles)");
  143. gs_free_object(mem, tbits, "alloc_ht_cache(bits)");
  144. gs_free_object(mem, pcache, "alloc_ht_cache(struct)");
  145. return 0;
  146. }
  147. pcache->bits = tbits;
  148. pcache->bits_size = max_bits;
  149. pcache->ht_tiles = ht_tiles;
  150. pcache->num_tiles = max_tiles;
  151. pcache->order.cache = pcache;
  152. pcache->order.transfer = 0;
  153. gx_ht_clear_cache(pcache);
  154. return pcache;
  155. }
  156. /* Free a halftone cache. */
  157. void
  158. gx_ht_free_cache(gs_memory_t * mem, gx_ht_cache * pcache)
  159. {
  160. gs_free_object(mem, pcache->ht_tiles, "free_ht_cache(ht_tiles)");
  161. gs_free_object(mem, pcache->bits, "free_ht_cache(bits)");
  162. gs_free_object(mem, pcache, "free_ht_cache(struct)");
  163. }
  164. /* Check whether the tile cache corresponds to the current order */
  165. bool
  166. gx_check_tile_cache_current(const gs_imager_state * pis)
  167. {
  168. /* TO_DO_DEVICEN - this routine is no longer used - delete. */
  169. return false;
  170. }
  171. /* Make the cache order current, and return whether */
  172. /* there is room for all possible tiles in the cache. */
  173. bool
  174. gx_check_tile_cache(const gs_imager_state * pis)
  175. {
  176. /* TO_DO_DEVICEN - this routine is no longer used - delete. */
  177. return false;
  178. }
  179. /*
  180. * Determine whether a given (width, y, height) might fit into a single
  181. * (non-strip) tile. If so, return the byte offset of the appropriate row
  182. * from the beginning of the tile, and set *ppx to the x phase offset
  183. * within the tile; if not, return -1.
  184. *
  185. * This routine cannot be supported in the DeviceN code.
  186. */
  187. int
  188. gx_check_tile_size(const gs_imager_state * pis, int w, int y, int h,
  189. gs_color_select_t select, int *ppx)
  190. {
  191. /* TO_DO_DEVICEN - this routine is no longer used - delete. */
  192. return -1;
  193. }
  194. /* Render a given level into a halftone cache. */
  195. private int render_ht(gx_ht_tile *, int, const gx_ht_order *,
  196. gx_bitmap_id);
  197. private gx_ht_tile *
  198. gx_render_ht_default(gx_ht_cache * pcache, int b_level)
  199. {
  200. const gx_ht_order *porder = &pcache->order;
  201. int level = porder->levels[b_level];
  202. gx_ht_tile *bt = &pcache->ht_tiles[level / pcache->levels_per_tile];
  203. if (bt->level != level) {
  204. int code = render_ht(bt, level, porder, pcache->base_id + b_level);
  205. if (code < 0)
  206. return 0;
  207. }
  208. return bt;
  209. }
  210. /* Faster code if num_tiles == 1. */
  211. private gx_ht_tile *
  212. gx_render_ht_1_tile(gx_ht_cache * pcache, int b_level)
  213. {
  214. const gx_ht_order *porder = &pcache->order;
  215. int level = porder->levels[b_level];
  216. gx_ht_tile *bt = &pcache->ht_tiles[0];
  217. if (bt->level != level) {
  218. int code = render_ht(bt, level, porder, pcache->base_id + b_level);
  219. if (code < 0)
  220. return 0;
  221. }
  222. return bt;
  223. }
  224. /* Faster code if levels_per_tile == 1. */
  225. private gx_ht_tile *
  226. gx_render_ht_1_level(gx_ht_cache * pcache, int b_level)
  227. {
  228. const gx_ht_order *porder = &pcache->order;
  229. int level = porder->levels[b_level];
  230. gx_ht_tile *bt = &pcache->ht_tiles[level];
  231. if (bt->level != level) {
  232. int code = render_ht(bt, level, porder, pcache->base_id + b_level);
  233. if (code < 0)
  234. return 0;
  235. }
  236. return bt;
  237. }
  238. /* save information about the operand binary halftone color */
  239. private void
  240. gx_dc_ht_binary_save_dc(const gx_device_color * pdevc,
  241. gx_device_color_saved * psdc)
  242. {
  243. psdc->type = pdevc->type;
  244. psdc->colors.binary.b_color[0] = pdevc->colors.binary.color[0];
  245. psdc->colors.binary.b_color[1] = pdevc->colors.binary.color[1];
  246. psdc->colors.binary.b_level = pdevc->colors.binary.b_level;
  247. psdc->colors.binary.b_index = pdevc->colors.binary.b_index;
  248. psdc->phase = pdevc->phase;
  249. }
  250. /* get the halftone used for a binary halftone color */
  251. private const gx_device_halftone *
  252. gx_dc_ht_binary_get_dev_halftone(const gx_device_color * pdevc)
  253. {
  254. return pdevc->colors.binary.b_ht;
  255. }
  256. /* Load the device color into the halftone cache if needed. */
  257. private int
  258. gx_dc_ht_binary_load(gx_device_color * pdevc, const gs_imager_state * pis,
  259. gx_device * dev, gs_color_select_t select)
  260. {
  261. int component_index = pdevc->colors.binary.b_index;
  262. const gx_ht_order *porder =
  263. (component_index < 0 ?
  264. &pdevc->colors.binary.b_ht->order :
  265. &pdevc->colors.binary.b_ht->components[component_index].corder);
  266. gx_ht_cache *pcache = porder->cache;
  267. if (pcache->order.bit_data != porder->bit_data)
  268. gx_ht_init_cache(pis->memory, pcache, porder);
  269. /*
  270. * We do not load the cache now. Instead we wait until we are ready
  271. * to actually render the color. This allows multiple colors to be
  272. * loaded without cache conflicts. (Cache conflicts can occur when
  273. * if two device colors use the same cache elements. This can occur
  274. * when the tile size is large enough that we do not have a separate
  275. * tile for each half tone level.) See gx_dc_ht_binary_load_cache.
  276. */
  277. pdevc->colors.binary.b_tile = NULL;
  278. return 0;
  279. }
  280. /*
  281. * Load the half tone tile in the halftone cache.
  282. */
  283. private int
  284. gx_dc_ht_binary_load_cache(const gx_device_color * pdevc)
  285. {
  286. int component_index = pdevc->colors.binary.b_index;
  287. const gx_ht_order *porder =
  288. &pdevc->colors.binary.b_ht->components[component_index].corder;
  289. gx_ht_cache *pcache = porder->cache;
  290. int b_level = pdevc->colors.binary.b_level;
  291. int level = porder->levels[b_level];
  292. gx_ht_tile *bt = &pcache->ht_tiles[level / pcache->levels_per_tile];
  293. if (bt->level != level) {
  294. int code = render_ht(bt, level, porder, pcache->base_id + b_level);
  295. if (code < 0)
  296. return_error(gs_error_Fatal);
  297. }
  298. ((gx_device_color *)pdevc)->colors.binary.b_tile = bt;
  299. return 0;
  300. }
  301. /* Fill a rectangle with a binary halftone. */
  302. /* Note that we treat this as "texture" for RasterOp. */
  303. private int
  304. gx_dc_ht_binary_fill_rectangle(const gx_device_color * pdevc, int x, int y,
  305. int w, int h, gx_device * dev, gs_logical_operation_t lop,
  306. const gx_rop_source_t * source)
  307. {
  308. gx_rop_source_t no_source;
  309. /* Load the halftone cache for the color */
  310. gx_dc_ht_binary_load_cache(pdevc);
  311. /*
  312. * Observation of H-P devices and documentation yields confusing
  313. * evidence about whether white pixels in halftones are always
  314. * opaque. It appears that for black-and-white devices, these
  315. * pixels are *not* opaque.
  316. */
  317. if (dev->color_info.depth > 1)
  318. lop &= ~lop_T_transparent;
  319. if (source == NULL && lop_no_S_is_T(lop))
  320. return (*dev_proc(dev, strip_tile_rectangle)) (dev,
  321. &pdevc->colors.binary.b_tile->tiles,
  322. x, y, w, h, pdevc->colors.binary.color[0],
  323. pdevc->colors.binary.color[1],
  324. pdevc->phase.x, pdevc->phase.y);
  325. /* Adjust the logical operation per transparent colors. */
  326. if (pdevc->colors.binary.color[0] == gx_no_color_index)
  327. lop = rop3_use_D_when_T_0(lop);
  328. if (pdevc->colors.binary.color[1] == gx_no_color_index)
  329. lop = rop3_use_D_when_T_1(lop);
  330. if (source == NULL)
  331. set_rop_no_source(source, no_source, dev);
  332. return (*dev_proc(dev, strip_copy_rop)) (dev, source->sdata,
  333. source->sourcex, source->sraster, source->id,
  334. (source->use_scolors ? source->scolors : NULL),
  335. &pdevc->colors.binary.b_tile->tiles,
  336. pdevc->colors.binary.color,
  337. x, y, w, h, pdevc->phase.x, pdevc->phase.y,
  338. lop);
  339. }
  340. private int
  341. gx_dc_ht_binary_fill_masked(const gx_device_color * pdevc, const byte * data,
  342. int data_x, int raster, gx_bitmap_id id, int x, int y, int w, int h,
  343. gx_device * dev, gs_logical_operation_t lop, bool invert)
  344. {
  345. /*
  346. * Load the halftone cache for the color. We do not do it earlier
  347. * because for small halftone caches, each cache tile may be used for
  348. * for more than one halftone level. This can cause conflicts if more
  349. * than one device color has been set and they use the same cache
  350. * entry.
  351. */
  352. int code = gx_dc_ht_binary_load_cache(pdevc);
  353. if (code < 0)
  354. return code;
  355. return gx_dc_default_fill_masked(pdevc, data, data_x, raster, id,
  356. x, y, w, h, dev, lop, invert);
  357. }
  358. /* Compare two binary halftones for equality. */
  359. private bool
  360. gx_dc_ht_binary_equal(const gx_device_color * pdevc1,
  361. const gx_device_color * pdevc2)
  362. {
  363. return pdevc2->type == pdevc1->type &&
  364. pdevc1->phase.x == pdevc2->phase.x &&
  365. pdevc1->phase.y == pdevc2->phase.y &&
  366. gx_dc_binary_color0(pdevc1) == gx_dc_binary_color0(pdevc2) &&
  367. gx_dc_binary_color1(pdevc1) == gx_dc_binary_color1(pdevc2) &&
  368. pdevc1->colors.binary.b_level == pdevc2->colors.binary.b_level;
  369. }
  370. /*
  371. * Flags to indicate the pieces of a binary halftone that are included
  372. * in its string representation. The first byte of the string holds this
  373. * set of flags.
  374. *
  375. * The binary halftone tile is never transmitted as part of the string
  376. * representation, so there is also no flag bit for it.
  377. */
  378. private const int dc_ht_binary_has_color0 = 0x01;
  379. private const int dc_ht_binary_has_color1 = 0x02;
  380. private const int dc_ht_binary_has_level = 0x04;
  381. private const int dc_ht_binary_has_index = 0x08;
  382. /*
  383. * Serialize a binany halftone device color.
  384. *
  385. * Operands:
  386. *
  387. * pdevc pointer to device color to be serialized
  388. *
  389. * psdc pointer ot saved version of last serialized color (for
  390. * this band)
  391. *
  392. * dev pointer to the current device, used to retrieve process
  393. * color model information
  394. *
  395. * pdata pointer to buffer in which to write the data
  396. *
  397. * psize pointer to a location that, on entry, contains the size of
  398. * the buffer pointed to by pdata; on return, the size of
  399. * the data required or actually used will be written here.
  400. *
  401. * Returns:
  402. * 1, with *psize set to 0, if *psdc and *pdevc represent the same color
  403. *
  404. * 0, with *psize set to the amount of data written, if everything OK
  405. *
  406. * gs_error_rangecheck, with *psize set to the size of buffer required,
  407. * if *psize was not large enough
  408. *
  409. * < 0, != gs_error_rangecheck, in the event of some other error; in this
  410. * case *psize is not changed.
  411. */
  412. private int
  413. gx_dc_ht_binary_write(
  414. const gx_device_color * pdevc,
  415. const gx_device_color_saved * psdc0,
  416. const gx_device * dev,
  417. byte * pdata,
  418. uint * psize )
  419. {
  420. int req_size = 1; /* flag bits */
  421. int flag_bits = 0;
  422. uint tmp_size;
  423. byte * pdata0 = pdata;
  424. const gx_device_color_saved * psdc = psdc0;
  425. int code;
  426. /* check if operand and saved colors are the same type */
  427. if (psdc != 0 && psdc->type != pdevc->type)
  428. psdc = 0;
  429. /* check for the information that must be transmitted */
  430. if ( psdc == 0 ||
  431. pdevc->colors.binary.color[0] != psdc->colors.binary.b_color[0] ) {
  432. flag_bits |= dc_ht_binary_has_color0;
  433. tmp_size = 0;
  434. (void)gx_dc_write_color( pdevc->colors.binary.color[0],
  435. dev,
  436. pdata,
  437. &tmp_size );
  438. req_size += tmp_size;
  439. }
  440. if ( psdc == 0 ||
  441. pdevc->colors.binary.color[1] != psdc->colors.binary.b_color[1] ) {
  442. flag_bits |= dc_ht_binary_has_color1;
  443. tmp_size = 0;
  444. (void)gx_dc_write_color( pdevc->colors.binary.color[1],
  445. dev,
  446. pdata,
  447. &tmp_size );
  448. req_size += tmp_size;
  449. }
  450. if ( psdc == 0 ||
  451. pdevc->colors.binary.b_level != psdc->colors.binary.b_level ) {
  452. flag_bits |= dc_ht_binary_has_level;
  453. req_size += enc_u_sizew(pdevc->colors.binary.b_level);
  454. }
  455. if ( psdc == 0 ||
  456. pdevc->colors.binary.b_index != psdc->colors.binary.b_index ) {
  457. flag_bits |= dc_ht_binary_has_index;
  458. req_size += 1;
  459. }
  460. /* check if there is anything to be done */
  461. if (flag_bits == 0) {
  462. *psize = 0;
  463. return 1;
  464. }
  465. /* check if sufficient space has been provided */
  466. if (req_size > *psize) {
  467. *psize = req_size;
  468. return gs_error_rangecheck;
  469. }
  470. /* write out the flag byte */
  471. *pdata++ = (byte)flag_bits;
  472. /* write out such other parts of the device color as are required */
  473. if ((flag_bits & dc_ht_binary_has_color0) != 0) {
  474. tmp_size = req_size - (pdata - pdata0);
  475. code = gx_dc_write_color( pdevc->colors.binary.color[0],
  476. dev,
  477. pdata,
  478. &tmp_size );
  479. if (code < 0)
  480. return code;
  481. pdata += tmp_size;
  482. }
  483. if ((flag_bits & dc_ht_binary_has_color1) != 0) {
  484. tmp_size = req_size - (pdata - pdata0);
  485. code = gx_dc_write_color( pdevc->colors.binary.color[1],
  486. dev,
  487. pdata,
  488. &tmp_size );
  489. if (code < 0)
  490. return code;
  491. pdata += tmp_size;
  492. }
  493. if ((flag_bits & dc_ht_binary_has_level) != 0)
  494. enc_u_putw(pdevc->colors.binary.b_level, pdata);
  495. if ((flag_bits & dc_ht_binary_has_index) != 0)
  496. *pdata++ = pdevc->colors.binary.b_index;
  497. *psize = pdata - pdata0;
  498. return 0;
  499. }
  500. /*
  501. * Reconstruct a binary halftone device color from its serial representation.
  502. *
  503. * Operands:
  504. *
  505. * pdevc pointer to the location in which to write the
  506. * reconstructed device color
  507. *
  508. * pis pointer to the current imager state (to access the
  509. * current halftone)
  510. *
  511. * prior_devc pointer to the current device color (this is provided
  512. * separately because the device color is not part of the
  513. * imager state)
  514. *
  515. * dev pointer to the current device, used to retrieve process
  516. * color model information
  517. *
  518. * pdata pointer to the buffer to be read
  519. *
  520. * size size of the buffer to be read; this should be large
  521. * enough to hold the entire color description
  522. *
  523. * mem pointer to the memory to be used for allocations
  524. * (ignored here)
  525. *
  526. * Returns:
  527. *
  528. * # of bytes read if everthing OK, < 0 in the event of an error
  529. */
  530. private int
  531. gx_dc_ht_binary_read(
  532. gx_device_color * pdevc,
  533. const gs_imager_state * pis,
  534. const gx_device_color * prior_devc,
  535. const gx_device * dev, /* ignored */
  536. const byte * pdata,
  537. uint size,
  538. gs_memory_t * mem ) /* ignored */
  539. {
  540. gx_device_color devc;
  541. const byte * pdata0 = pdata;
  542. int code, flag_bits;
  543. /* if prior information is available, use it */
  544. if (prior_devc != 0 && prior_devc->type == gx_dc_type_ht_binary)
  545. devc = *prior_devc;
  546. else
  547. memset(&devc, 0, sizeof(devc)); /* clear pointers */
  548. devc.type = gx_dc_type_ht_binary;
  549. /* the halftone is always taken from the imager state */
  550. devc.colors.binary.b_ht = pis->dev_ht;
  551. /* cache is not porvided until the device color is used */
  552. devc.colors.binary.b_tile = 0;
  553. /* verify the minimum amount of information */
  554. if (size == 0)
  555. return_error(gs_error_rangecheck);
  556. size --;
  557. flag_bits = *pdata++;
  558. /* read the other information provided */
  559. if ((flag_bits & dc_ht_binary_has_color0) != 0) {
  560. code = gx_dc_read_color( &devc.colors.binary.color[0],
  561. dev,
  562. pdata,
  563. size );
  564. if (code < 0)
  565. return code;
  566. size -= code;
  567. pdata += code;
  568. }
  569. if ((flag_bits & dc_ht_binary_has_color1) != 0) {
  570. code = gx_dc_read_color( &devc.colors.binary.color[1],
  571. dev,
  572. pdata,
  573. size );
  574. if (code < 0)
  575. return code;
  576. size -= code;
  577. pdata += code;
  578. }
  579. if ((flag_bits & dc_ht_binary_has_level) != 0) {
  580. const byte * pdata_start = pdata;
  581. if (size < 1)
  582. return_error(gs_error_rangecheck);
  583. enc_u_getw(devc.colors.binary.b_level, pdata);
  584. size -= pdata - pdata_start;
  585. }
  586. if ((flag_bits & dc_ht_binary_has_index) != 0) {
  587. if (size == 0)
  588. return_error(gs_error_rangecheck);
  589. --size;
  590. devc.colors.binary.b_index = *pdata++;
  591. }
  592. /* set the phase as required (select value is arbitrary) */
  593. /* set the phase as required (select value is arbitrary) */
  594. color_set_phase_mod( &devc,
  595. pis->screen_phase[0].x,
  596. pis->screen_phase[0].y,
  597. pis->dev_ht->lcm_width,
  598. pis->dev_ht->lcm_height );
  599. /* everything looks good */
  600. *pdevc = devc;
  601. return pdata - pdata0;
  602. }
  603. /*
  604. * Get the nonzero components of a binary halftone. This is used to
  605. * distinguish components that are given zero intensity due to halftoning
  606. * from those for which the original color intensity was in fact zero.
  607. *
  608. * Since this device color type involves only a single halftone component,
  609. * we can reasonably assume that b_level != 0. Hence, we need to check
  610. * for components with identical intensities in color[0] and color[1].
  611. */
  612. int
  613. gx_dc_ht_binary_get_nonzero_comps(
  614. const gx_device_color * pdevc,
  615. const gx_device * dev,
  616. gx_color_index * pcomp_bits )
  617. {
  618. int code;
  619. gx_color_value cvals_0[GX_DEVICE_COLOR_MAX_COMPONENTS],
  620. cvals_1[GX_DEVICE_COLOR_MAX_COMPONENTS];
  621. if ( (code = dev_proc(dev, decode_color)( (gx_device *)dev,
  622. pdevc->colors.binary.color[0],
  623. cvals_0 )) >= 0 &&
  624. (code = dev_proc(dev, decode_color)( (gx_device *)dev,
  625. pdevc->colors.binary.color[1],
  626. cvals_1 )) >= 0 ) {
  627. int i, ncomps = dev->color_info.num_components;
  628. int mask = 0x1, comp_bits = 0;
  629. for (i = 0; i < ncomps; i++, mask <<= 1) {
  630. if (cvals_0[i] != 0 || cvals_1[i] != 0)
  631. comp_bits |= mask;
  632. }
  633. *pcomp_bits = comp_bits;
  634. code = 0;
  635. }
  636. return code;
  637. }
  638. /* Initialize the tile cache for a given screen. */
  639. /* Cache as many different levels as will fit. */
  640. void
  641. gx_ht_init_cache(const gs_memory_t *mem, gx_ht_cache * pcache, const gx_ht_order * porder)
  642. {
  643. uint width = porder->width;
  644. uint height = porder->height;
  645. uint size = width * height + 1;
  646. int width_unit =
  647. (width <= ht_mask_bits / 2 ? ht_mask_bits / width * width :
  648. width);
  649. int height_unit = height;
  650. uint raster = porder->raster;
  651. uint tile_bytes = raster * height;
  652. uint shift = porder->shift;
  653. int num_cached;
  654. int i;
  655. byte *tbits = pcache->bits;
  656. /* Non-monotonic halftones may have more bits than size. */
  657. if (porder->num_bits >= size)
  658. size = porder->num_bits + 1;
  659. /* Make sure num_cached is within bounds */
  660. num_cached = pcache->bits_size / tile_bytes;
  661. if (num_cached > size)
  662. num_cached = size;
  663. if (num_cached > pcache->num_tiles)
  664. num_cached = pcache->num_tiles;
  665. if (num_cached == size &&
  666. tile_bytes * num_cached <= pcache->bits_size / 2
  667. ) {
  668. /*
  669. * We can afford to replicate every tile in the cache,
  670. * which will reduce breakage when tiling. Since
  671. * horizontal breakage is more expensive than vertical,
  672. * and since wide shallow fills are more common than
  673. * narrow deep fills, we replicate the tile horizontally.
  674. * We do have to be careful not to replicate the tile
  675. * to an absurdly large size, however.
  676. */
  677. uint rep_raster =
  678. ((pcache->bits_size / num_cached) / height) &
  679. ~(align_bitmap_mod - 1);
  680. uint rep_count = rep_raster * 8 / width;
  681. /*
  682. * There's no real value in replicating the tile
  683. * beyond the point where the byte width of the replicated
  684. * tile is a multiple of a long.
  685. */
  686. if (rep_count > sizeof(ulong) * 8)
  687. rep_count = sizeof(ulong) * 8;
  688. width_unit = width * rep_count;
  689. raster = bitmap_raster(width_unit);
  690. tile_bytes = raster * height;
  691. }
  692. pcache->base_id = gs_next_ids(mem, porder->num_levels + 1);
  693. pcache->order = *porder;
  694. /* The transfer function is irrelevant, and might become dangling. */
  695. pcache->order.transfer = 0;
  696. pcache->num_cached = num_cached;
  697. pcache->levels_per_tile = (size + num_cached - 1) / num_cached;
  698. pcache->tiles_fit = -1;
  699. memset(tbits, 0, pcache->bits_size);
  700. for (i = 0; i < num_cached; i++, tbits += tile_bytes) {
  701. register gx_ht_tile *bt = &pcache->ht_tiles[i];
  702. bt->level = 0;
  703. bt->index = i;
  704. bt->tiles.data = tbits;
  705. bt->tiles.raster = raster;
  706. bt->tiles.size.x = width_unit;
  707. bt->tiles.size.y = height_unit;
  708. bt->tiles.rep_width = width;
  709. bt->tiles.rep_height = height;
  710. bt->tiles.shift = bt->tiles.rep_shift = shift;
  711. }
  712. pcache->render_ht =
  713. (pcache->num_tiles == 1 ? gx_render_ht_1_tile :
  714. pcache->levels_per_tile == 1 ? gx_render_ht_1_level :
  715. gx_render_ht_default);
  716. }
  717. /*
  718. * Compute and save the rendering of a given gray level
  719. * with the current halftone. The cache holds multiple tiles,
  720. * where each tile covers a range of possible levels.
  721. * We adjust the tile whose range includes the desired level incrementally;
  722. * this saves a lot of time for the average image, where gray levels
  723. * don't change abruptly. Note that the "level" is the number of bits,
  724. * not the index in the levels vector.
  725. */
  726. private int
  727. render_ht(gx_ht_tile * pbt, int level /* [1..num_bits-1] */ ,
  728. const gx_ht_order * porder, gx_bitmap_id new_id)
  729. {
  730. byte *data = pbt->tiles.data;
  731. int code;
  732. if_debug7('H', "[H]Halftone cache slot 0x%lx: old=%d, new=%d, w=%d(%d), h=%d(%d):\n",
  733. (ulong) data, pbt->level, level,
  734. pbt->tiles.size.x, porder->width,
  735. pbt->tiles.size.y, porder->num_bits / porder->width);
  736. #ifdef DEBUG
  737. if (level < 0 || level > porder->num_bits) {
  738. lprintf3("Error in render_ht: level=%d, old level=%d, num_bits=%d\n",
  739. level, pbt->level, porder->num_bits);
  740. return_error(gs_error_Fatal);
  741. }
  742. #endif
  743. code = porder->procs->render(pbt, level, porder);
  744. if (code < 0)
  745. return code;
  746. pbt->level = level;
  747. pbt->tiles.id = new_id;
  748. /*
  749. * Check whether we want to replicate the tile in the cache.
  750. * Since we only do this when all the renderings will fit
  751. * in the cache, we only do it once per level, and it doesn't
  752. * have to be very efficient.
  753. */
  754. /****** TEST IS WRONG if width > rep_width but tile.raster ==
  755. ****** order raster.
  756. ******/
  757. if (pbt->tiles.raster > porder->raster)
  758. bits_replicate_horizontally(data, pbt->tiles.rep_width,
  759. pbt->tiles.rep_height, porder->raster,
  760. pbt->tiles.size.x, pbt->tiles.raster);
  761. if (pbt->tiles.size.y > pbt->tiles.rep_height &&
  762. pbt->tiles.shift == 0
  763. )
  764. bits_replicate_vertically(data, pbt->tiles.rep_height,
  765. pbt->tiles.raster, pbt->tiles.size.y);
  766. #ifdef DEBUG
  767. if (gs_debug_c('H')) {
  768. const byte *p = pbt->tiles.data;
  769. int wb = pbt->tiles.raster;
  770. const byte *ptr = p + wb * pbt->tiles.size.y;
  771. while (p < ptr) {
  772. dprintf8(" %d%d%d%d%d%d%d%d",
  773. *p >> 7, (*p >> 6) & 1, (*p >> 5) & 1,
  774. (*p >> 4) & 1, (*p >> 3) & 1, (*p >> 2) & 1,
  775. (*p >> 1) & 1, *p & 1);
  776. if ((++p - data) % wb == 0)
  777. dputc('\n');
  778. }
  779. }
  780. #endif
  781. return 0;
  782. }