gsht.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. /* Copyright (C) 1989, 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: gsht.c,v 1.23 2005/03/14 18:08:36 dan Exp $ */
  14. /* setscreen operator for Ghostscript library */
  15. #include "memory_.h"
  16. #include "string_.h"
  17. #include <stdlib.h> /* for qsort */
  18. #include "gx.h"
  19. #include "gserrors.h"
  20. #include "gsstruct.h"
  21. #include "gsutil.h" /* for gs_next_ids */
  22. #include "gxarith.h" /* for igcd */
  23. #include "gzstate.h"
  24. #include "gxdevice.h" /* for gzht.h */
  25. #include "gzht.h"
  26. #include "gswts.h"
  27. /* Forward declarations */
  28. void gx_set_effective_transfer(gs_state *);
  29. /*
  30. * *HACK ALERT*
  31. *
  32. * Value stored in the width field of a well-tempered screen halftone
  33. * order, to indicate that the wts field of this order points to the
  34. * same structure as an earlier order. This is used to suppress
  35. * multiple realeases of shared wts_screen_t orders.
  36. *
  37. * The width field is available for this purpose at it is nominally
  38. * unused in a well-tempered screening halftone.
  39. */
  40. private const ushort ht_wts_suppress_release = (ushort)(-1);
  41. /* Structure types */
  42. public_st_ht_order();
  43. private_st_ht_order_component();
  44. public_st_ht_order_comp_element();
  45. public_st_halftone();
  46. public_st_device_halftone();
  47. /* GC procedures */
  48. private
  49. ENUM_PTRS_WITH(ht_order_enum_ptrs, gx_ht_order *porder) return 0;
  50. case 0: ENUM_RETURN((porder->data_memory ? porder->levels : 0));
  51. case 1: ENUM_RETURN((porder->data_memory ? porder->bit_data : 0));
  52. case 2: ENUM_RETURN(porder->cache);
  53. case 3: ENUM_RETURN(porder->transfer);
  54. ENUM_PTRS_END
  55. private
  56. RELOC_PTRS_WITH(ht_order_reloc_ptrs, gx_ht_order *porder)
  57. {
  58. if (porder->data_memory) {
  59. RELOC_VAR(porder->levels);
  60. RELOC_VAR(porder->bit_data);
  61. }
  62. RELOC_VAR(porder->cache);
  63. RELOC_VAR(porder->transfer);
  64. }
  65. RELOC_PTRS_END
  66. private
  67. ENUM_PTRS_WITH(halftone_enum_ptrs, gs_halftone *hptr) return 0;
  68. case 0:
  69. switch (hptr->type)
  70. {
  71. case ht_type_spot:
  72. ENUM_RETURN((hptr->params.spot.transfer == 0 ?
  73. hptr->params.spot.transfer_closure.data :
  74. 0));
  75. case ht_type_threshold:
  76. ENUM_RETURN_CONST_STRING_PTR(gs_halftone, params.threshold.thresholds);
  77. case ht_type_threshold2:
  78. return ENUM_CONST_BYTESTRING(&hptr->params.threshold2.thresholds);
  79. case ht_type_client_order:
  80. ENUM_RETURN(hptr->params.client_order.client_data);
  81. case ht_type_multiple:
  82. case ht_type_multiple_colorscreen:
  83. ENUM_RETURN(hptr->params.multiple.components);
  84. case ht_type_none:
  85. case ht_type_screen:
  86. case ht_type_colorscreen:
  87. return 0;
  88. }
  89. case 1:
  90. switch (hptr->type) {
  91. case ht_type_threshold:
  92. ENUM_RETURN((hptr->params.threshold.transfer == 0 ?
  93. hptr->params.threshold.transfer_closure.data :
  94. 0));
  95. case ht_type_threshold2:
  96. ENUM_RETURN(hptr->params.threshold2.transfer_closure.data);
  97. case ht_type_client_order:
  98. ENUM_RETURN(hptr->params.client_order.transfer_closure.data);
  99. default:
  100. return 0;
  101. }
  102. ENUM_PTRS_END
  103. private RELOC_PTRS_WITH(halftone_reloc_ptrs, gs_halftone *hptr)
  104. {
  105. switch (hptr->type) {
  106. case ht_type_spot:
  107. if (hptr->params.spot.transfer == 0)
  108. RELOC_PTR(gs_halftone, params.spot.transfer_closure.data);
  109. break;
  110. case ht_type_threshold:
  111. RELOC_CONST_STRING_PTR(gs_halftone, params.threshold.thresholds);
  112. if (hptr->params.threshold.transfer == 0)
  113. RELOC_PTR(gs_halftone, params.threshold.transfer_closure.data);
  114. break;
  115. case ht_type_threshold2:
  116. RELOC_CONST_BYTESTRING_VAR(hptr->params.threshold2.thresholds);
  117. RELOC_OBJ_VAR(hptr->params.threshold2.transfer_closure.data);
  118. break;
  119. case ht_type_client_order:
  120. RELOC_PTR(gs_halftone, params.client_order.client_data);
  121. RELOC_PTR(gs_halftone, params.client_order.transfer_closure.data);
  122. break;
  123. case ht_type_multiple:
  124. case ht_type_multiple_colorscreen:
  125. RELOC_PTR(gs_halftone, params.multiple.components);
  126. break;
  127. case ht_type_none:
  128. case ht_type_screen:
  129. case ht_type_colorscreen:
  130. break;
  131. }
  132. }
  133. RELOC_PTRS_END
  134. /* setscreen */
  135. int
  136. gs_setscreen(gs_state * pgs, gs_screen_halftone * phsp)
  137. {
  138. gs_screen_enum senum;
  139. int code = gx_ht_process_screen(&senum, pgs, phsp,
  140. gs_currentaccuratescreens());
  141. if (code < 0)
  142. return code;
  143. return gs_screen_install(&senum);
  144. }
  145. /* currentscreen */
  146. int
  147. gs_currentscreen(const gs_state * pgs, gs_screen_halftone * phsp)
  148. {
  149. switch (pgs->halftone->type) {
  150. case ht_type_screen:
  151. *phsp = pgs->halftone->params.screen;
  152. return 0;
  153. case ht_type_colorscreen:
  154. *phsp = pgs->halftone->params.colorscreen.screens.colored.gray;
  155. return 0;
  156. default:
  157. return_error(gs_error_undefined);
  158. }
  159. }
  160. /* .currentscreenlevels */
  161. int
  162. gs_currentscreenlevels(const gs_state * pgs)
  163. {
  164. int gi = 0;
  165. if (pgs->device != 0)
  166. gi = pgs->device->color_info.gray_index;
  167. if (gi != GX_CINFO_COMP_NO_INDEX)
  168. return pgs->dev_ht->components[gi].corder.num_levels;
  169. else
  170. return pgs->dev_ht->components[0].corder.num_levels;
  171. }
  172. /* .setscreenphase */
  173. int
  174. gx_imager_setscreenphase(gs_imager_state * pis, int x, int y,
  175. gs_color_select_t select)
  176. {
  177. if (select == gs_color_select_all) {
  178. int i;
  179. for (i = 0; i < gs_color_select_count; ++i)
  180. gx_imager_setscreenphase(pis, x, y, (gs_color_select_t) i);
  181. return 0;
  182. } else if (select < 0 || select >= gs_color_select_count)
  183. return_error(gs_error_rangecheck);
  184. pis->screen_phase[select].x = x;
  185. pis->screen_phase[select].y = y;
  186. return 0;
  187. }
  188. int
  189. gs_setscreenphase(gs_state * pgs, int x, int y, gs_color_select_t select)
  190. {
  191. int code = gx_imager_setscreenphase((gs_imager_state *) pgs, x, y,
  192. select);
  193. /*
  194. * If we're only setting the source phase, we don't need to do
  195. * unset_dev_color, because the source phase doesn't affect painting
  196. * with the current color.
  197. */
  198. if (code >= 0 && (select == gs_color_select_texture ||
  199. select == gs_color_select_all)
  200. )
  201. gx_unset_dev_color(pgs);
  202. return code;
  203. }
  204. int
  205. gs_currentscreenphase_pis(const gs_imager_state * pis, gs_int_point * pphase,
  206. gs_color_select_t select)
  207. {
  208. if (select < 0 || select >= gs_color_select_count)
  209. return_error(gs_error_rangecheck);
  210. *pphase = pis->screen_phase[select];
  211. return 0;
  212. }
  213. /* .currentscreenphase */
  214. int
  215. gs_currentscreenphase(const gs_state * pgs, gs_int_point * pphase,
  216. gs_color_select_t select)
  217. {
  218. return gs_currentscreenphase_pis((const gs_imager_state *)pgs, pphase, select);
  219. }
  220. /* currenthalftone */
  221. int
  222. gs_currenthalftone(gs_state * pgs, gs_halftone * pht)
  223. {
  224. *pht = *pgs->halftone;
  225. return 0;
  226. }
  227. /* ------ Internal routines ------ */
  228. /* Process one screen plane. */
  229. int
  230. gx_ht_process_screen_memory(gs_screen_enum * penum, gs_state * pgs,
  231. gs_screen_halftone * phsp, bool accurate, gs_memory_t * mem)
  232. {
  233. gs_point pt;
  234. int code = gs_screen_init_memory(penum, pgs, phsp, accurate, mem);
  235. if (code < 0)
  236. return code;
  237. while ((code = gs_screen_currentpoint(penum, &pt)) == 0)
  238. if ((code = gs_screen_next(penum, (*phsp->spot_function) (pt.x, pt.y))) < 0)
  239. return code;
  240. return 0;
  241. }
  242. /*
  243. * Internal procedure to allocate and initialize either an internally
  244. * generated or a client-defined halftone order. For spot halftones,
  245. * the client is responsible for calling gx_compute_cell_values.
  246. *
  247. * Note: this function is used for old-style halftones only. WTS
  248. * halftones are allocated in gs_sethalftone_try_wts().
  249. */
  250. int
  251. gx_ht_alloc_ht_order(gx_ht_order * porder, uint width, uint height,
  252. uint num_levels, uint num_bits, uint strip_shift,
  253. const gx_ht_order_procs_t *procs, gs_memory_t * mem)
  254. {
  255. porder->wse = NULL;
  256. porder->wts = NULL;
  257. porder->width = width;
  258. porder->height = height;
  259. porder->raster = bitmap_raster(width);
  260. porder->shift = strip_shift;
  261. porder->orig_height = porder->height;
  262. porder->orig_shift = porder->shift;
  263. porder->full_height = ht_order_full_height(porder);
  264. porder->num_levels = num_levels;
  265. porder->num_bits = num_bits;
  266. porder->procs = procs;
  267. porder->data_memory = mem;
  268. if (num_levels > 0) {
  269. porder->levels =
  270. (uint *)gs_alloc_byte_array(mem, porder->num_levels, sizeof(uint),
  271. "alloc_ht_order_data(levels)");
  272. if (porder->levels == 0)
  273. return_error(gs_error_VMerror);
  274. } else
  275. porder->levels = 0;
  276. if (num_bits > 0) {
  277. porder->bit_data =
  278. gs_alloc_byte_array(mem, porder->num_bits,
  279. porder->procs->bit_data_elt_size,
  280. "alloc_ht_order_data(bit_data)");
  281. if (porder->bit_data == 0) {
  282. gs_free_object(mem, porder->levels, "alloc_ht_order_data(levels)");
  283. porder->levels = 0;
  284. return_error(gs_error_VMerror);
  285. }
  286. } else
  287. porder->bit_data = 0;
  288. porder->cache = 0;
  289. porder->transfer = 0;
  290. return 0;
  291. }
  292. /*
  293. * Procedure to copy a halftone order.
  294. */
  295. private int
  296. gx_ht_copy_ht_order(gx_ht_order * pdest, gx_ht_order * psrc, gs_memory_t * mem)
  297. {
  298. int code;
  299. *pdest = *psrc;
  300. code = gx_ht_alloc_ht_order(pdest, psrc->width, psrc->height,
  301. psrc->num_levels, psrc->num_bits, psrc->shift,
  302. psrc->procs, mem);
  303. if (code < 0)
  304. return code;
  305. if (pdest->levels != 0)
  306. memcpy(pdest->levels, psrc->levels, psrc->num_levels * sizeof(uint));
  307. if (pdest->bit_data != 0)
  308. memcpy(pdest->bit_data, psrc->bit_data,
  309. psrc->num_bits * psrc->procs->bit_data_elt_size);
  310. pdest->wse = psrc->wse;
  311. pdest->transfer = psrc->transfer;
  312. rc_increment(pdest->transfer);
  313. return 0;
  314. }
  315. /*
  316. * Set the destination component to match the source component, and
  317. * "assume ownership" of all of the refrernced data structures.
  318. */
  319. private void
  320. gx_ht_move_ht_order(gx_ht_order * pdest, gx_ht_order * psrc)
  321. {
  322. uint width = psrc->width, height = psrc->height, shift = psrc->shift;
  323. pdest->params = psrc->params;
  324. pdest->wse = psrc->wse;
  325. pdest->wts = 0;
  326. pdest->width = width;
  327. pdest->height = height;
  328. pdest->raster = bitmap_raster(width);
  329. pdest->shift = shift;
  330. pdest->orig_height = height;
  331. pdest->orig_shift = shift;
  332. pdest->full_height = ht_order_full_height(pdest);
  333. pdest->num_levels = psrc->num_levels;
  334. pdest->num_bits = psrc->num_bits;
  335. pdest->procs = psrc->procs;
  336. pdest->data_memory = psrc->data_memory;
  337. pdest->levels = psrc->levels;
  338. pdest->bit_data = psrc->bit_data;
  339. pdest->cache = psrc->cache; /* should be 0 */
  340. pdest->transfer = psrc->transfer;
  341. }
  342. /* Allocate and initialize the contents of a halftone order. */
  343. /* The client must have set the defining values in porder->params. */
  344. int
  345. gx_ht_alloc_order(gx_ht_order * porder, uint width, uint height,
  346. uint strip_shift, uint num_levels, gs_memory_t * mem)
  347. {
  348. gx_ht_order order;
  349. int code;
  350. order = *porder;
  351. gx_compute_cell_values(&order.params);
  352. code = gx_ht_alloc_ht_order(&order, width, height, num_levels,
  353. width * height, strip_shift,
  354. &ht_order_procs_default, mem);
  355. if (code < 0)
  356. return code;
  357. *porder = order;
  358. return 0;
  359. }
  360. /*
  361. * Allocate and initialize a threshold order, which may use the short
  362. * representation.
  363. */
  364. int
  365. gx_ht_alloc_threshold_order(gx_ht_order * porder, uint width, uint height,
  366. uint num_levels, gs_memory_t * mem)
  367. {
  368. gx_ht_order order;
  369. uint num_bits = width * height;
  370. const gx_ht_order_procs_t *procs =
  371. (num_bits > 2000 && num_bits <= max_ushort ?
  372. &ht_order_procs_short : &ht_order_procs_default);
  373. int code;
  374. order = *porder;
  375. gx_compute_cell_values(&order.params);
  376. code = gx_ht_alloc_ht_order(&order, width, height, num_levels,
  377. width * height, 0, procs, mem);
  378. if (code < 0)
  379. return code;
  380. *porder = order;
  381. return 0;
  382. }
  383. /* Allocate and initialize the contents of a client-defined halftone order. */
  384. int
  385. gx_ht_alloc_client_order(gx_ht_order * porder, uint width, uint height,
  386. uint num_levels, uint num_bits, gs_memory_t * mem)
  387. {
  388. gx_ht_order order;
  389. int code;
  390. order = *porder;
  391. order.params.M = width, order.params.N = 0;
  392. order.params.R = 1;
  393. order.params.M1 = height, order.params.N1 = 0;
  394. order.params.R1 = 1;
  395. gx_compute_cell_values(&order.params);
  396. code = gx_ht_alloc_ht_order(&order, width, height, num_levels,
  397. num_bits, 0, &ht_order_procs_default, mem);
  398. if (code < 0)
  399. return code;
  400. *porder = order;
  401. return 0;
  402. }
  403. /* Compare keys ("masks", actually sample values) for qsort. */
  404. private int
  405. compare_samples(const void *p1, const void *p2)
  406. {
  407. ht_sample_t m1 = ((const gx_ht_bit *)p1)->mask;
  408. ht_sample_t m2 = ((const gx_ht_bit *)p2)->mask;
  409. return (m1 < m2 ? -1 : m1 > m2 ? 1 : 0);
  410. }
  411. /* Sort the halftone order by sample value. */
  412. void
  413. gx_sort_ht_order(gx_ht_bit * recs, uint N)
  414. {
  415. int i;
  416. /* Tag each sample with its index, for sorting. */
  417. for (i = 0; i < N; i++)
  418. recs[i].offset = i;
  419. qsort((void *)recs, N, sizeof(*recs), compare_samples);
  420. #ifdef DEBUG
  421. if (gs_debug_c('H')) {
  422. uint i;
  423. dlputs("[H]Sorted samples:\n");
  424. for (i = 0; i < N; i++)
  425. dlprintf3("%5u: %5u: %u\n",
  426. i, recs[i].offset, recs[i].mask);
  427. }
  428. #endif
  429. }
  430. /*
  431. * Construct the halftone order from a sampled spot function. Only width x
  432. * strip samples have been filled in; we must replicate the resulting sorted
  433. * order vertically, shifting it by shift each time. See gxdht.h regarding
  434. * the invariants that must be restored.
  435. */
  436. void
  437. gx_ht_construct_spot_order(gx_ht_order * porder)
  438. {
  439. uint width = porder->width;
  440. uint num_levels = porder->num_levels; /* = width x strip */
  441. uint strip = num_levels / width;
  442. gx_ht_bit *bits = (gx_ht_bit *)porder->bit_data;
  443. uint *levels = porder->levels;
  444. uint shift = porder->orig_shift;
  445. uint full_height = porder->full_height;
  446. uint num_bits = porder->num_bits;
  447. uint copies = num_bits / (width * strip);
  448. gx_ht_bit *bp = bits + num_bits - 1;
  449. uint i;
  450. gx_sort_ht_order(bits, num_levels);
  451. if_debug5('h',
  452. "[h]spot order: num_levels=%u w=%u h=%u strip=%u shift=%u\n",
  453. num_levels, width, porder->orig_height, strip, shift);
  454. /* Fill in the levels array, replicating the bits vertically */
  455. /* if needed. */
  456. for (i = num_levels; i > 0;) {
  457. uint offset = bits[--i].offset;
  458. uint x = offset % width;
  459. uint hy = offset - x;
  460. uint k;
  461. levels[i] = i * copies;
  462. for (k = 0; k < copies;
  463. k++, bp--, hy += num_levels, x = (x + width - shift) % width
  464. )
  465. bp->offset = hy + x;
  466. }
  467. /* If we have a complete halftone, restore the invariant. */
  468. if (num_bits == width * full_height) {
  469. porder->height = full_height;
  470. porder->shift = 0;
  471. }
  472. gx_ht_construct_bits(porder);
  473. }
  474. /* Construct a single offset/mask. */
  475. void
  476. gx_ht_construct_bit(gx_ht_bit * bit, int width, int bit_num)
  477. {
  478. uint padding = bitmap_raster(width) * 8 - width;
  479. int pix = bit_num;
  480. ht_mask_t mask;
  481. byte *pb;
  482. pix += pix / width * padding;
  483. bit->offset = (pix >> 3) & -size_of(mask);
  484. mask = (ht_mask_t) 1 << (~pix & (ht_mask_bits - 1));
  485. /* Replicate the mask bits. */
  486. pix = ht_mask_bits - width;
  487. while ((pix -= width) >= 0)
  488. mask |= mask >> width;
  489. /* Store the mask, reversing bytes if necessary. */
  490. bit->mask = 0;
  491. for (pb = (byte *) & bit->mask + (sizeof(mask) - 1);
  492. mask != 0;
  493. mask >>= 8, pb--
  494. )
  495. *pb = (byte) mask;
  496. }
  497. /* Construct offset/masks from the whitening order. */
  498. /* porder->bits[i].offset contains the index of the bit position */
  499. /* that is i'th in the whitening order. */
  500. void
  501. gx_ht_construct_bits(gx_ht_order * porder)
  502. {
  503. uint i;
  504. gx_ht_bit *phb;
  505. for (i = 0, phb = (gx_ht_bit *)porder->bit_data;
  506. i < porder->num_bits;
  507. i++, phb++)
  508. gx_ht_construct_bit(phb, porder->width, phb->offset);
  509. #ifdef DEBUG
  510. if (gs_debug_c('H')) {
  511. dlprintf1("[H]Halftone order bits 0x%lx:\n", (ulong)porder->bit_data);
  512. for (i = 0, phb = (gx_ht_bit *)porder->bit_data;
  513. i < porder->num_bits;
  514. i++, phb++)
  515. dlprintf3("%4d: %u:0x%lx\n", i, phb->offset,
  516. (ulong) phb->mask);
  517. }
  518. #endif
  519. }
  520. /* Release a gx_device_halftone by freeing its components. */
  521. /* (Don't free the gx_device_halftone itself.) */
  522. void
  523. gx_ht_order_release(gx_ht_order * porder, gs_memory_t * mem, bool free_cache)
  524. {
  525. /* "free cache" is a proxy for "differs from default" */
  526. if (free_cache) {
  527. if (porder->cache != 0)
  528. gx_ht_free_cache(mem, porder->cache);
  529. else if (porder->wse != 0)
  530. gs_wts_free_enum(porder->wse);
  531. }
  532. porder->cache = 0;
  533. if (porder->wts != 0 && porder->width != ht_wts_suppress_release)
  534. gs_wts_free_screen(porder->wts);
  535. porder->wts = 0;
  536. rc_decrement(porder->transfer, "gx_ht_order_release(transfer)");
  537. porder->transfer = 0;
  538. if (porder->data_memory != 0) {
  539. gs_free_object(porder->data_memory, porder->bit_data,
  540. "gx_ht_order_release(bit_data)");
  541. gs_free_object(porder->data_memory, porder->levels,
  542. "gx_ht_order_release(levels)");
  543. }
  544. porder->levels = 0;
  545. porder->bit_data = 0;
  546. }
  547. void
  548. gx_device_halftone_release(gx_device_halftone * pdht, gs_memory_t * mem)
  549. {
  550. if (pdht->components) {
  551. int i;
  552. /* One of the components might be the same as the default */
  553. /* order, so check that we don't free it twice. */
  554. for (i = 0; i < pdht->num_comp; ++i)
  555. if (pdht->components[i].corder.bit_data !=
  556. pdht->order.bit_data
  557. ) { /* Currently, all orders except the default one */
  558. /* own their caches. */
  559. gx_ht_order_release(&pdht->components[i].corder, mem, true);
  560. }
  561. gs_free_object(mem, pdht->components,
  562. "gx_dev_ht_release(components)");
  563. pdht->components = 0;
  564. pdht->num_comp = 0;
  565. }
  566. gx_ht_order_release(&pdht->order, mem, false);
  567. }
  568. /*
  569. * This routine will take a color name (defined by a ptr and size) and
  570. * check if this is a valid colorant name for the current device. If
  571. * so then the device's colorant number is returned.
  572. *
  573. * Two other checks are also made. If the name is "Default" then a value
  574. * of GX_DEVICE_COLOR_MAX_COMPONENTS is returned. This is done to
  575. * simplify the handling of default halftones. Note: The device also
  576. * uses GX_DEVICE_COLOR_MAX_COMPONENTS to indicate colorants which are
  577. * known but not being used due to the SeparationOrder parameter. In this
  578. * case we return -1 since the colorant is not currently being used by the
  579. * device.
  580. *
  581. * If the halftone type is colorscreen or multiple colorscreen, then we
  582. * also check for Red/Cyan, Green/Magenta, Blue/Yellow, and Gray/Black
  583. * component name pairs. This is done since the setcolorscreen and
  584. * sethalftone types 2 and 4 imply the dual name sets.
  585. *
  586. * A negative value is returned if the color name is not found.
  587. */
  588. int
  589. gs_color_name_component_number(gx_device * dev, const char * pname,
  590. int name_size, int halftonetype)
  591. {
  592. int num_colorant;
  593. #define check_colorant_name(dev, name) \
  594. ((*dev_proc(dev, get_color_comp_index)) (dev, name, strlen(name), NO_COMP_NAME_TYPE))
  595. #define check_colorant_name_length(dev, name, length) \
  596. ((*dev_proc(dev, get_color_comp_index)) (dev, name, length, NO_COMP_NAME_TYPE))
  597. #define check_name(str, pname, length) \
  598. ((strlen(str) == length) && (strncmp(pname, str, length) == 0))
  599. /*
  600. * Check if this is a device colorant.
  601. */
  602. num_colorant = check_colorant_name_length(dev, pname, name_size);
  603. if (num_colorant >= 0) {
  604. /*
  605. * The device will return GX_DEVICE_COLOR_MAX_COMPONENTS if the
  606. * colorant is logically present in the device but not being used
  607. * because a SeparationOrder parameter is specified. Since we are
  608. * using this value to indicate 'Default', we use -1 to indicate
  609. * that the colorant is not really being used.
  610. */
  611. if (num_colorant == GX_DEVICE_COLOR_MAX_COMPONENTS)
  612. num_colorant = -1;
  613. return num_colorant;
  614. }
  615. /*
  616. * Check if this is the default component
  617. */
  618. if (check_name("Default", pname, name_size))
  619. return GX_DEVICE_COLOR_MAX_COMPONENTS;
  620. /* Halftones set by setcolorscreen, and (we think) */
  621. /* Type 2 and Type 4 halftones, are supposed to work */
  622. /* for both RGB and CMYK, so we need a special check here. */
  623. if (halftonetype == ht_type_colorscreen ||
  624. halftonetype == ht_type_multiple_colorscreen) {
  625. if (check_name("Red", pname, name_size))
  626. num_colorant = check_colorant_name(dev, "Cyan");
  627. else if (check_name("Green", pname, name_size))
  628. num_colorant = check_colorant_name(dev, "Magenta");
  629. else if (check_name("Blue", pname, name_size))
  630. num_colorant = check_colorant_name(dev, "Yellow");
  631. else if (check_name("Gray", pname, name_size))
  632. num_colorant = check_colorant_name(dev, "Black");
  633. #undef check_colorant_name
  634. #undef check_colorant_name_length
  635. #undef check_name
  636. }
  637. return num_colorant;
  638. }
  639. /*
  640. * See gs_color_name_component_number for main description.
  641. *
  642. * This version converts a name index value into a string and size and
  643. * then call gs_color_name_component_number.
  644. */
  645. int
  646. gs_cname_to_colorant_number(gs_state * pgs, byte * pname, uint name_size,
  647. int halftonetype)
  648. {
  649. gx_device * dev = pgs->device;
  650. return gs_color_name_component_number(dev, (char *)pname, name_size,
  651. halftonetype);
  652. }
  653. /*
  654. * Install a device halftone into the imager state.
  655. *
  656. * To allow halftones to be shared between graphic states, the imager
  657. * state contains a pointer to a device halftone structure. Thus, when
  658. * we say a halftone is "in" the imager state, we are only claiming
  659. * that the halftone pointer in the imager state points to that halftone.
  660. *
  661. * Though the operand halftone uses the same structure as the halftone
  662. * "in" the imager state, not all of its fields are filled in, and the
  663. * organization of components differs. Specifically, the following fields
  664. * are not filled in:
  665. *
  666. * rc The operand device halftone has only a transient existence,
  667. * its reference count information is not initialized. In many
  668. * cases, the operand device halftone structure is allocated
  669. * on the stack by clients.
  670. *
  671. * id A halftone is not considered to have an identity until it
  672. * is installed in the imager state. This is a design error
  673. * which reflects the PostScript origins of this code. In
  674. * PostScript, it is impossible to check if two halftone
  675. * specifications (sets of operands to setscreen/setcolorscreen
  676. * or halftone dictionaries) are the same. Hence, the only way
  677. * a halftone could be identified was by the graphic state in
  678. * which it was included. In PCL it is possible to directly
  679. * identify a halftone specification, but currently there is
  680. * no way to use this knowledge in the graphic library.
  681. *
  682. * (An altogether more reasonable approach would be to apply
  683. * id's to halftone orders.)
  684. *
  685. * type This is filled in by the type operand. It is used by
  686. * PostScript's currentscreen/currentcolorscreen operators to
  687. * determine if a sampling procedure or a halftone dictionary
  688. * should be pushed onto the stack. More importantly, it is
  689. * also used to determine if specific halftone components can
  690. * be used for either the additive or subtractive version of
  691. * that component in the process color model. For example, a
  692. * RedThreshold in a HalftoneType 4 dictionary can be applied
  693. * to either the component "Red" or the component "Cyan", but
  694. * the value of the key "Red" in a HalftoneType 5 dictionary
  695. * can only be used for a "Red" component (not a "Cyan"
  696. * component).
  697. *
  698. * num_comp For the operand halftone, this is the number of halftone
  699. * components included in the specification. For the device
  700. * halftone in the imager state, this is always the same as
  701. * the number of color model components (see num_dev_comp).
  702. *
  703. * num_dev_comp The number of components in the device process color model
  704. * when the operand halftone was created. With some compositor
  705. * devices (for example PDF 1.4) we can have differences in the
  706. * process color model of the compositor versus the output device.
  707. * These compositor devices do not halftone.
  708. *
  709. * components For the operand halftone, this field is non-null only if
  710. * multiple halftones are provided. In that case, the size
  711. * of the array pointed is the same as the number of
  712. * components provided. One of these components will usually
  713. * be the same as that identified by the "order" field.
  714. *
  715. * For the device halftone in the imager state, this field is
  716. * always non-null, and the size of the array pointed to will
  717. * be the same as the number of components in the process
  718. * color model.
  719. *
  720. * lcm_width, These fields provide the least common multiple of the
  721. * lcm_height halftone dimensions of the individual component orders.
  722. * They represent the dimensions of the smallest tile that
  723. * repeats for all color components (this is of interest
  724. * because Ghostscript uses a "chunky" raster format for all
  725. * drawing procedures). These fields cannot be set in the
  726. * operand device halftone as we do not yet know which of
  727. * the halftone components will actually be used.
  728. *
  729. * Conversely, the "order" field is significant only in the operand device
  730. * halftone. There it represents the default halftone component, which will
  731. * be used for all device color components for which a named halftone is
  732. * not available. It is ignored (filled with 0's) in the device halftone
  733. * in the imager state.
  734. *
  735. * The ordering of entries and the set of fields initialized in the
  736. * components array also vary between the operand device halftone and
  737. * the device halftone in the imager state.
  738. *
  739. * If the components array is present in the operand device halftone, the
  740. * cname field in each entry of the array will contain a name index
  741. * identifying the colorant name, and the comp_number field will provide the
  742. * index of the corresponding component in the process color model. The
  743. * order of entries in the components array is essentially arbitrary,
  744. * but in some common cases will reflect the order in which the halftone
  745. * specification is provided. By convention, if no explicit default order
  746. * is provided (i.e.: via a HalftoneType 5 dictionary), the first
  747. * entry of the array will be the same as the "order" (default) field.
  748. *
  749. * For the device halftone in the imager state, the components array is
  750. * always present, but the cname and comp_number fields of individual
  751. * entries are ignored. The order of the entries in the array always
  752. * matches the order of components in the device color model.
  753. *
  754. * The distinction between the operand device halftone and the one in
  755. * the graphic state extends even to the individual fields of the
  756. * gx_ht_order structure incorporated in the order field of the halftone
  757. * and the corder field of the elements of the components array. The
  758. * fields of this structure that are handled differently in the operand
  759. * and imager state device halftones are:
  760. *
  761. * params Provides a set of parameters that are required for
  762. * converting a halftone specification to a single
  763. * component order. This field is used only in the
  764. * operand device halftone; it is not set in the device
  765. * halftone in the imager state.
  766. *
  767. * wse Points to an "enumerator" instance, used to construct
  768. * a well-tempered screen. This is only required while
  769. * the well-tempered screen is being constructed. This
  770. * field is always a null pointer in the device halftone
  771. * in the imager state.
  772. *
  773. * wts Points to the "constructed" form of a well-tempered
  774. * screen. The "construction" operation occurs as part
  775. * of the installation process. Hence, this should
  776. * always be a null pointer in the operand device
  777. * halftone.
  778. *
  779. * orig_height, The height and shift values of the halftone cell,
  780. * orig_shift prior to any replication. These fields are currently
  781. * unused, and will always be the same as the height
  782. * and width fields in the device halftone in the
  783. * imager state.
  784. *
  785. * full_height The height of the smallest replicated tile whose shift
  786. * value is 0. This is calculated as part of the
  787. * installation process; it may be set in the operand
  788. * device halftone, but its value is ignored.
  789. *
  790. *
  791. * data_memory Points to the memory structure used to allocate the
  792. * levels and bit_data arrays. The handling of this field
  793. * is a bit complicated. For orders that are "taken over"
  794. * by the installation process, this field will have the
  795. * same value in the operand device halftone and the
  796. * device halftone in the imager state. For halftones
  797. * that are copied by the installation process, this
  798. * field will have the same value as the memory field in
  799. * the imager state (the two are usually the same).
  800. *
  801. * cache Pointer to a cache of tiles representing various
  802. * levels of the halftone. This may or may not be
  803. * provided in the operand device halftone (in principle
  804. * this should always be a null pointer in the operand
  805. * device halftone, but this is not the manner in which
  806. * the cache was handled historically).
  807. *
  808. * screen_params This structure contains transformation information
  809. * that is required when reading the sample data for a
  810. * screen. It is no longer required once the halftone
  811. * order has been constructed.
  812. *
  813. * In addition to what is noted above, this procedure is made somewhat
  814. * more complex than expected due to memory management considerations. To
  815. * clarify this, it is necessary to consider the properties of the pieces
  816. * that constitute a device halftone.
  817. *
  818. * The gx_device_halftone structure itself is shareable and uses
  819. * reference counts.
  820. *
  821. * The gx_ht_order_component array (components array entry) is in
  822. * principle shareable, though it does not provide any reference
  823. * counting mechanism. Hence any sharing needs to be done with
  824. * caution.
  825. *
  826. * Individual component orders are not shareable, as they are part of
  827. * the gx_ht_order_commponent structure (a major design error).
  828. *
  829. * The levels, bit_data, and cache structures referenced by the
  830. * gx_ht_order structure are in principle shareable, but they also do
  831. * not provide any reference counting mechanism. Traditionally, one set
  832. * of two component orders could share these structures, using the
  833. * halftone's "order" field and various scattered bits of special case
  834. * code. This practice has been ended because it did not extend to
  835. * sharing amongst more than two components.
  836. *
  837. * The gx_transfer_map structure referenced by the gx_ht_order structure
  838. * is shareable, and uses reference counts. Traditionally this structure
  839. * was not shared, but this is no longer the case.
  840. *
  841. * As noted, the rc field of the operand halftone is not initialized, so
  842. * this procedure cannot simply take ownership of the operand device
  843. * halftone structure (i.e.: an ostensibly shareable structure is not
  844. * shareable). Hence, this procedure will always create a new copy of the
  845. * gx_device_halftone structure, either by allocating a new structure or
  846. * re-using the structure already referenced by the imager state. This
  847. * feature must be retained, as in several cases the calling code will
  848. * allocate the operand device halftone structure on the stack.
  849. *
  850. * Traditionally, this procedure took ownership of all of the structures
  851. * referenced by the operand device halftone structure. This implied
  852. * that all structures referenced by the gx_device_halftone structure
  853. * needed to be allocated on the heap, and should not be released once
  854. * the call to gx_imager_dev_ht_install completes.
  855. *
  856. * There were two problems with this approach:
  857. *
  858. * 1. In the event of an error, the calling code most likely would have
  859. * to release referenced components, as the imager state had not yet
  860. * take ownership of them. In many cases, the code did not do this.
  861. *
  862. * 2. When the structures referenced by a single order needed to be
  863. * shared amongst more than one component, there was no easy way to
  864. * discover this sharing when the imager state's device halftone
  865. * subsequently needed to be released. Hence, objects would be
  866. * released multiple times.
  867. *
  868. * Subsequently, the code in this routine was changed to copy most of
  869. * the referenced structures (everything except the transfer functions).
  870. * Unfortunately, the calling code was not changed, which caused memory
  871. * leaks.
  872. *
  873. * The approach now taken uses a mixture of the two approaches.
  874. * Ownership to structures referenced by the operand device halftone is
  875. * assumed by the device halftone in the imager state where this is
  876. * possible. In these cases, the corresponding references are removed in
  877. * the operand device halftone (hence, this operand is no longer
  878. * qualified as const). When a structure is required but ownership cannot
  879. * be assumed, a copy is made and the reference in the operand device
  880. * halftone is left undisturbed. The calling code has also been modified
  881. * to release any remaining referenced structures when this routine
  882. * returns, whether or not an error is indicated.
  883. */
  884. int
  885. gx_imager_dev_ht_install(
  886. gs_imager_state * pis,
  887. gx_device_halftone * pdht,
  888. gs_halftone_type type,
  889. const gx_device * dev )
  890. {
  891. gx_device_halftone dht;
  892. int num_comps = pdht->num_dev_comp;
  893. int i, code = 0;
  894. bool used_default = false;
  895. int lcm_width = 1, lcm_height = 1;
  896. gs_wts_screen_enum_t * wse0 = pdht->order.wse;
  897. wts_screen_t * wts0 = 0;
  898. bool mem_diff = pdht->rc.memory != pis->memory;
  899. /* construct the new device halftone structure */
  900. memset(&dht.order, 0, sizeof(dht.order));
  901. /* the rc field is filled in later */
  902. dht.id = gs_next_ids(pis->memory, 1);
  903. dht.type = type;
  904. dht.components = gs_alloc_struct_array(
  905. pis->memory,
  906. num_comps,
  907. gx_ht_order_component,
  908. &st_ht_order_component_element,
  909. "gx_imager_dev_ht_install(components)" );
  910. if (dht.components == NULL)
  911. return_error(gs_error_VMerror);
  912. dht.num_comp = dht.num_dev_comp = num_comps;
  913. /* lcm_width, lcm_height are filled in later */
  914. /* initialize the components array */
  915. memset(dht.components, 0, num_comps * sizeof(dht.components[0]));
  916. for (i = 0; i < num_comps; i++)
  917. dht.components[i].comp_number = -1;
  918. /*
  919. * Duplicate any of the non-default components, but do not create copies
  920. * of the levels or bit_data arrays. If all goes according to plan, the
  921. * imager state's device halftone will assume ownership of these arrays
  922. * by clearing the corresponding pointers in the operand halftone's
  923. * orders.
  924. */
  925. if (pdht->components != 0) {
  926. int input_ncomps = pdht->num_comp;
  927. for (i = 0; i < input_ncomps && code >= 0; i++) {
  928. gx_ht_order_component * p_s_comp = &pdht->components[i];
  929. gx_ht_order * p_s_order = &p_s_comp->corder;
  930. int comp_num = p_s_comp->comp_number;
  931. if (comp_num >= 0 && comp_num < GX_DEVICE_COLOR_MAX_COMPONENTS) {
  932. gx_ht_order * p_d_order = &dht.components[comp_num].corder;
  933. /* indicate that this order has been filled in */
  934. dht.components[comp_num].comp_number = comp_num;
  935. /*
  936. * The component can be used only if it is from the
  937. * proper memory
  938. */
  939. if (mem_diff)
  940. code = gx_ht_copy_ht_order( p_d_order,
  941. p_s_order,
  942. pis->memory );
  943. else {
  944. /* check if this is also the default component */
  945. used_default = used_default ||
  946. p_s_order->bit_data == pdht->order.bit_data;
  947. gx_ht_move_ht_order(p_d_order, p_s_order);
  948. }
  949. }
  950. }
  951. }
  952. /*
  953. * Copy the default order to any remaining components.
  954. *
  955. * For well-tempered screens, generate the wts_screen_t structure
  956. * for each component that corresponds to the sample information
  957. * that has been gathered.
  958. *
  959. * Some caution is necessary here, as multiple component orders may
  960. * have wse fields pointing to the same gs_wts_creeen_enum_t
  961. * structure. This structure should only be released once. If
  962. * multiple components have such a wse value, it will be the same as
  963. * pdht->order.wse pointer, so we can just release that pointer once
  964. * when done.
  965. *
  966. * If serveral component orders have the same wse value, this code
  967. * will create just one wts_screen_t structure. In a somewhat ugly
  968. * hack, the width field (which is otherwise unused) will be set to
  969. * 0xffff for all components other than the first component that
  970. * makes use of a give wts_screen_t structure. gx_ht_order_release
  971. * will check this field to see if it should release the structure
  972. * pointed to by the wts field of a component order.
  973. *
  974. * Components that are not well-tempered screens require a cache.
  975. * In practice, either all or non of the components will be well-
  976. * tempered screens, but we ignore that fact here.
  977. *
  978. * While engaged in all of these other activities, also calculate
  979. * the lcm_width and lcm_heigth values (only for non-well-tempered
  980. * components).
  981. */
  982. for (i = 0; i < num_comps && code >= 0; i++) {
  983. gx_ht_order * porder = &dht.components[i].corder;
  984. gs_wts_screen_enum_t * wse;
  985. if (dht.components[i].comp_number != i) {
  986. if (used_default || mem_diff)
  987. code = gx_ht_copy_ht_order(porder, &pdht->order, pis->memory);
  988. else {
  989. gx_ht_move_ht_order(porder, &pdht->order);
  990. used_default = true;
  991. }
  992. dht.components[i].comp_number = i;
  993. }
  994. if ((wse = porder->wse) != 0) {
  995. wts_screen_t * wts = 0;
  996. porder->width = 0;
  997. porder->wse = 0;
  998. if (wse != wse0)
  999. wts = wts_screen_from_enum(wse);
  1000. else {
  1001. if (wts0 == 0)
  1002. wts0 = wts_screen_from_enum(wse);
  1003. else
  1004. porder->width = ht_wts_suppress_release;
  1005. wts = wts0;
  1006. }
  1007. if (wts == 0)
  1008. code = gs_error_VMerror;
  1009. else
  1010. porder->wts = wts;
  1011. } else {
  1012. uint w = porder->width, h = porder->full_height;
  1013. int dw = igcd(lcm_width, w), dh = igcd(lcm_height, h);
  1014. lcm_width /= dw;
  1015. lcm_height /= dh;
  1016. lcm_width = (w > max_int / lcm_width ? max_int : lcm_width * w);
  1017. lcm_height = (h > max_int / lcm_height ? max_int : lcm_height * h);
  1018. if (porder->cache == 0) {
  1019. uint tile_bytes, num_tiles;
  1020. gx_ht_cache * pcache;
  1021. tile_bytes = porder->raster
  1022. * (porder->num_bits / porder->width);
  1023. num_tiles = 1 + max_tile_cache_bytes / tile_bytes;
  1024. pcache = gx_ht_alloc_cache( pis->memory,
  1025. num_tiles,
  1026. tile_bytes * num_tiles );
  1027. if (pcache == NULL)
  1028. code = gs_error_VMerror;
  1029. else {
  1030. porder->cache = pcache;
  1031. gx_ht_init_cache(pis->memory, pcache, porder);
  1032. }
  1033. }
  1034. }
  1035. }
  1036. dht.lcm_width = lcm_width;
  1037. dht.lcm_height = lcm_height;
  1038. /*
  1039. * If everything is OK so far, allocate a unique copy of the device
  1040. * halftone reference by the imager state.
  1041. *
  1042. * This code requires a special check for the case in which the
  1043. * deivce halftone referenced by the imager state is already unique.
  1044. * In this case, we must explicitly release just the components array
  1045. * (and any structures it refers to) of the existing halftone. This
  1046. * cannot be done automatically, as the rc_unshare_struct macro only
  1047. * ensures that a unique instance of the top-level structure is
  1048. * created, not that any substructure references are updated.
  1049. *
  1050. * Though this is scheduled to be changed, for the time being the
  1051. * command list renderer may invoke this code with pdht == psi->dev_ht
  1052. * (in which case we know pis->dev_ht.rc.ref_count == 1). Special
  1053. * handling is required in that case, to avoid releasing structures
  1054. * we still need.
  1055. */
  1056. if (code >= 0) {
  1057. gx_device_halftone * pisdht = pis->dev_ht;
  1058. rc_header tmp_rc;
  1059. if (pisdht != 0 && pisdht->rc.ref_count == 1) {
  1060. if (pdht != pisdht)
  1061. gx_device_halftone_release(pisdht, pisdht->rc.memory);
  1062. } else {
  1063. rc_unshare_struct( pis->dev_ht,
  1064. gx_device_halftone,
  1065. &st_device_halftone,
  1066. pis->memory,
  1067. BEGIN code = gs_error_VMerror; goto err; END,
  1068. "gx_imager_dev_ht_install" );
  1069. pisdht = pis->dev_ht;
  1070. }
  1071. /*
  1072. * Everything worked. "Assume ownership" of the appropriate
  1073. * portions of the source device halftone by clearing the
  1074. * associated references. This includes explicitly releasing
  1075. * any gs_wts_screen_enum_t structures. Since we might have
  1076. * pdht == pis->dev_ht, this must done before updating pis->dev_ht.
  1077. *
  1078. * If the default order has been used for a device component, and
  1079. * any of the source component orders share their levels or bit_data
  1080. * arrays with the default order, clear the pointers in those orders
  1081. * now. This is necessary because the default order's pointers will
  1082. * be cleared immediately below, so subsequently it will not be
  1083. * possible to tell if that this information is being shared.
  1084. */
  1085. if (pdht->components != 0) {
  1086. int input_ncomps = pdht->num_comp;
  1087. for (i = 0; i < input_ncomps; i++) {
  1088. gx_ht_order_component * p_s_comp = &pdht->components[i];
  1089. gx_ht_order * p_s_order = &p_s_comp->corder;
  1090. int comp_num = p_s_comp->comp_number;
  1091. if ( comp_num >= 0 &&
  1092. comp_num < GX_DEVICE_COLOR_MAX_COMPONENTS ) {
  1093. if (p_s_order->wse != 0)
  1094. gs_wts_free_enum(p_s_order->wse);
  1095. memset(p_s_order, 0, sizeof(*p_s_order));
  1096. } else if ( comp_num == GX_DEVICE_COLOR_MAX_COMPONENTS &&
  1097. used_default )
  1098. memset(p_s_order, 0, sizeof(*p_s_order));
  1099. }
  1100. }
  1101. if (used_default) {
  1102. if (wse0 != 0)
  1103. gs_wts_free_enum(wse0);
  1104. memset(&pdht->order, 0, sizeof(pdht->order));
  1105. }
  1106. tmp_rc = pisdht->rc;
  1107. *pisdht = dht;
  1108. pisdht->rc = tmp_rc;
  1109. /* update the effective transfer function array */
  1110. gx_imager_set_effective_xfer(pis);
  1111. return 0;
  1112. }
  1113. /* something went amiss; release all copied components */
  1114. err:
  1115. for (i = 0; i < num_comps; i++) {
  1116. gx_ht_order_component * pcomp = &dht.components[i];
  1117. gx_ht_order * porder = &pcomp->corder;
  1118. if (pcomp->comp_number == -1)
  1119. gx_ht_order_release(porder, pis->memory, true);
  1120. }
  1121. gs_free_object(pis->memory, dht.components, "gx_imager_dev_ht_install");
  1122. return code;
  1123. }
  1124. /*
  1125. * Install a new halftone in the graphics state. Note that we copy the top
  1126. * level of the gs_halftone and the gx_device_halftone, and take ownership
  1127. * of any substructures.
  1128. */
  1129. int
  1130. gx_ht_install(gs_state * pgs, const gs_halftone * pht,
  1131. gx_device_halftone * pdht)
  1132. {
  1133. gs_memory_t *mem = pht->rc.memory;
  1134. gs_halftone *old_ht = pgs->halftone;
  1135. gs_halftone *new_ht;
  1136. int code;
  1137. pdht->num_dev_comp = pgs->device->color_info.num_components;
  1138. if (old_ht != 0 && old_ht->rc.memory == mem &&
  1139. old_ht->rc.ref_count == 1
  1140. )
  1141. new_ht = old_ht;
  1142. else
  1143. rc_alloc_struct_1(new_ht, gs_halftone, &st_halftone,
  1144. mem, return_error(gs_error_VMerror),
  1145. "gx_ht_install(new halftone)");
  1146. code = gx_imager_dev_ht_install((gs_imager_state *) pgs,
  1147. pdht, pht->type, gs_currentdevice_inline(pgs));
  1148. if (code < 0) {
  1149. if (new_ht != old_ht)
  1150. gs_free_object(mem, new_ht, "gx_ht_install(new halftone)");
  1151. return code;
  1152. }
  1153. /*
  1154. * Discard and unused components and the components array of the
  1155. * operand device halftone
  1156. */
  1157. gx_device_halftone_release(pdht, pdht->rc.memory);
  1158. if (new_ht != old_ht)
  1159. rc_decrement(old_ht, "gx_ht_install(old halftone)");
  1160. {
  1161. rc_header rc;
  1162. rc = new_ht->rc;
  1163. *new_ht = *pht;
  1164. new_ht->rc = rc;
  1165. }
  1166. pgs->halftone = new_ht;
  1167. gx_unset_dev_color(pgs);
  1168. return 0;
  1169. }
  1170. /*
  1171. * This macro will determine the colorant number of a given color name.
  1172. * A value of -1 indicates that the name is not valid.
  1173. */
  1174. #define check_colorant_name(name, dev) \
  1175. ((*dev_proc(dev, get_color_comp_index)) (dev, name, strlen(name), NO_NAME_TYPE))
  1176. /* Reestablish the effective transfer functions, taking into account */
  1177. /* any overrides from halftone dictionaries. */
  1178. void
  1179. gx_imager_set_effective_xfer(gs_imager_state * pis)
  1180. {
  1181. const gx_device_halftone *pdht = pis->dev_ht;
  1182. gx_transfer_map *pmap;
  1183. int i, component_num;
  1184. for (i = 0; i < GX_DEVICE_COLOR_MAX_COMPONENTS; i++)
  1185. pis->effective_transfer[i] = pis->set_transfer.gray; /* default */
  1186. /* Check if we have a transfer functions from setcolortransfer */
  1187. if (pis->set_transfer.red) {
  1188. component_num = pis->set_transfer.red_component_num;
  1189. if (component_num >= 0)
  1190. pis->effective_transfer[component_num] = pis->set_transfer.red;;
  1191. }
  1192. if (pis->set_transfer.green) {
  1193. component_num = pis->set_transfer.green_component_num;
  1194. if (component_num >= 0)
  1195. pis->effective_transfer[component_num] = pis->set_transfer.green;
  1196. }
  1197. if (pis->set_transfer.blue) {
  1198. component_num = pis->set_transfer.blue_component_num;
  1199. if (component_num >= 0)
  1200. pis->effective_transfer[component_num] = pis->set_transfer.blue;
  1201. }
  1202. if (pdht == NULL)
  1203. return; /* not initialized yet */
  1204. for (i = 0; i < pdht->num_comp; i++) {
  1205. pmap = pdht->components[i].corder.transfer;
  1206. if (pmap != NULL)
  1207. pis->effective_transfer[i] = pmap;
  1208. }
  1209. }
  1210. void
  1211. gx_set_effective_transfer(gs_state * pgs)
  1212. {
  1213. gx_imager_set_effective_xfer((gs_imager_state *) pgs);
  1214. }