zht2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* Copyright (C) 1992, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: zht2.c,v 1.2 2000/09/19 19:00:54 lpd Exp $ */
  16. /* Level 2 sethalftone operator */
  17. #include "ghost.h"
  18. #include "oper.h"
  19. #include "gsstruct.h"
  20. #include "gxdevice.h" /* for gzht.h */
  21. #include "gzht.h"
  22. #include "estack.h"
  23. #include "ialloc.h"
  24. #include "iddict.h"
  25. #include "idparam.h"
  26. #include "igstate.h"
  27. #include "icolor.h"
  28. #include "iht.h"
  29. #include "store.h"
  30. /* Forward references */
  31. private int dict_spot_params(P4(const ref *, gs_spot_halftone *,
  32. ref *, ref *));
  33. private int dict_spot_results(P3(i_ctx_t *, ref *, const gs_spot_halftone *));
  34. private int dict_threshold_params(P3(const ref *, gs_threshold_halftone *,
  35. ref *));
  36. private int dict_threshold2_params(P4(const ref *, gs_threshold2_halftone *,
  37. ref *, gs_memory_t *));
  38. /* Dummy spot function */
  39. private float
  40. spot1_dummy(floatp x, floatp y)
  41. {
  42. return (x + y) / 2;
  43. }
  44. /* <dict> <dict5> .sethalftone5 - */
  45. private int sethalftone_finish(P1(i_ctx_t *));
  46. private int sethalftone_cleanup(P1(i_ctx_t *));
  47. private int
  48. zsethalftone5(i_ctx_t *i_ctx_p)
  49. {
  50. os_ptr op = osp;
  51. uint count;
  52. gs_halftone_component *phtc;
  53. gs_halftone_component *pc;
  54. int code = 0;
  55. int i, j;
  56. gs_halftone *pht;
  57. gx_device_halftone *pdht;
  58. static const char *const color_names[] = {
  59. gs_ht_separation_name_strings
  60. };
  61. ref sprocs[countof(color_names)];
  62. ref tprocs[countof(color_names)];
  63. gs_memory_t *mem;
  64. uint edepth = ref_stack_count(&e_stack);
  65. int npop = 2;
  66. check_type(*op, t_dictionary);
  67. check_dict_read(*op);
  68. check_type(op[-1], t_dictionary);
  69. check_dict_read(op[-1]);
  70. count = 0;
  71. for (i = 0; i < countof(color_names); i++) {
  72. ref *pvalue;
  73. if (dict_find_string(op, color_names[i], &pvalue) > 0)
  74. count++;
  75. else if (i == gs_ht_separation_Default)
  76. return_error(e_typecheck);
  77. }
  78. mem = (gs_memory_t *) idmemory->spaces_indexed[r_space_index(op - 1)];
  79. check_estack(5); /* for sampling Type 1 screens */
  80. refset_null(sprocs, countof(sprocs));
  81. refset_null(tprocs, countof(tprocs));
  82. rc_alloc_struct_0(pht, gs_halftone, &st_halftone,
  83. imemory, pht = 0, ".sethalftone5");
  84. phtc = gs_alloc_struct_array(mem, count, gs_halftone_component,
  85. &st_ht_component_element,
  86. ".sethalftone5");
  87. rc_alloc_struct_0(pdht, gx_device_halftone, &st_device_halftone,
  88. imemory, pdht = 0, ".sethalftone5");
  89. if (pht == 0 || phtc == 0 || pdht == 0)
  90. code = gs_note_error(e_VMerror);
  91. else
  92. for (i = 0, j = 0, pc = phtc; i < countof(color_names); i++) {
  93. int type;
  94. ref *pvalue;
  95. if (dict_find_string(op, color_names[i], &pvalue) > 0) {
  96. check_type_only(*pvalue, t_dictionary);
  97. check_dict_read(*pvalue);
  98. if (dict_int_param(pvalue, "HalftoneType", 1, 7, 0,
  99. &type) < 0
  100. ) {
  101. code = gs_note_error(e_typecheck);
  102. break;
  103. }
  104. switch (type) {
  105. default:
  106. code = gs_note_error(e_rangecheck);
  107. break;
  108. case 1:
  109. code = dict_spot_params(pvalue,
  110. &pc->params.spot, sprocs + j,
  111. tprocs + j);
  112. pc->params.spot.screen.spot_function = spot1_dummy;
  113. pc->type = ht_type_spot;
  114. break;
  115. case 3:
  116. code = dict_threshold_params(pvalue,
  117. &pc->params.threshold, tprocs + j);
  118. pc->type = ht_type_threshold;
  119. break;
  120. case 7:
  121. code = dict_threshold2_params(pvalue,
  122. &pc->params.threshold2, tprocs + j,
  123. imemory);
  124. pc->type = ht_type_threshold2;
  125. break;
  126. }
  127. if (code < 0)
  128. break;
  129. pc->cname = (gs_ht_separation_name) i;
  130. pc++, j++;
  131. }
  132. }
  133. if (code >= 0) {
  134. /*
  135. * We think that Type 2 and Type 4 halftones, like
  136. * screens set by setcolorscreen, adapt automatically to
  137. * the device color space, so we need to mark them
  138. * with a different internal halftone type.
  139. */
  140. int type = 0;
  141. dict_int_param(op - 1, "HalftoneType", 1, 5, 0, &type);
  142. pht->type =
  143. (type == 2 || type == 4 ? ht_type_multiple_colorscreen :
  144. ht_type_multiple);
  145. pht->params.multiple.components = phtc;
  146. pht->params.multiple.num_comp = count;
  147. code = gs_sethalftone_prepare(igs, pht, pdht);
  148. }
  149. if (code >= 0)
  150. for (j = 0, pc = phtc; j < count; j++, pc++) {
  151. if (pc->type == ht_type_spot) {
  152. ref *pvalue;
  153. dict_find_string(op, color_names[pc->cname], &pvalue);
  154. code = dict_spot_results(i_ctx_p, pvalue, &pc->params.spot);
  155. if (code < 0)
  156. break;
  157. }
  158. }
  159. if (code >= 0) {
  160. /*
  161. * Schedule the sampling of any Type 1 screens,
  162. * and any (Type 1 or Type 3) TransferFunctions.
  163. * Save the stack depths in case we have to back out.
  164. */
  165. uint odepth = ref_stack_count(&o_stack);
  166. ref odict, odict5;
  167. odict = op[-1];
  168. odict5 = *op;
  169. pop(2);
  170. op = osp;
  171. esp += 5;
  172. make_mark_estack(esp - 4, es_other, sethalftone_cleanup);
  173. esp[-3] = odict;
  174. make_istruct(esp - 2, 0, pht);
  175. make_istruct(esp - 1, 0, pdht);
  176. make_op_estack(esp, sethalftone_finish);
  177. for (j = 0; j < count; j++) {
  178. gx_ht_order *porder =
  179. (pdht->components == 0 ? &pdht->order :
  180. &pdht->components[j].corder);
  181. switch (phtc[j].type) {
  182. case ht_type_spot:
  183. code = zscreen_enum_init(i_ctx_p, porder,
  184. &phtc[j].params.spot.screen,
  185. &sprocs[j], 0, 0, mem);
  186. if (code < 0)
  187. break;
  188. /* falls through */
  189. case ht_type_threshold:
  190. if (!r_has_type(tprocs + j, t__invalid)) {
  191. /* Schedule TransferFunction sampling. */
  192. /****** check_xstack IS WRONG ******/
  193. check_ostack(zcolor_remap_one_ostack);
  194. check_estack(zcolor_remap_one_estack);
  195. code = zcolor_remap_one(i_ctx_p, tprocs + j,
  196. porder->transfer, igs,
  197. zcolor_remap_one_finish);
  198. op = osp;
  199. }
  200. break;
  201. default: /* not possible here, but to keep */
  202. /* the compilers happy.... */
  203. ;
  204. }
  205. if (code < 0) { /* Restore the stack. */
  206. ref_stack_pop_to(&o_stack, odepth);
  207. ref_stack_pop_to(&e_stack, edepth);
  208. op = osp;
  209. op[-1] = odict;
  210. *op = odict5;
  211. break;
  212. }
  213. npop = 0;
  214. }
  215. }
  216. if (code < 0) {
  217. gs_free_object(mem, pdht, ".sethalftone5");
  218. gs_free_object(mem, phtc, ".sethalftone5");
  219. gs_free_object(mem, pht, ".sethalftone5");
  220. return code;
  221. }
  222. pop(npop);
  223. return (ref_stack_count(&e_stack) > edepth ? o_push_estack : 0);
  224. }
  225. /* Install the halftone after sampling. */
  226. private int
  227. sethalftone_finish(i_ctx_t *i_ctx_p)
  228. {
  229. gx_device_halftone *pdht = r_ptr(esp, gx_device_halftone);
  230. int code;
  231. if (pdht->components)
  232. pdht->order = pdht->components[0].corder;
  233. code = gx_ht_install(igs, r_ptr(esp - 1, gs_halftone), pdht);
  234. if (code < 0)
  235. return code;
  236. istate->halftone = esp[-2];
  237. esp -= 4;
  238. sethalftone_cleanup(i_ctx_p);
  239. return o_pop_estack;
  240. }
  241. /* Clean up after installing the halftone. */
  242. private int
  243. sethalftone_cleanup(i_ctx_t *i_ctx_p)
  244. {
  245. gx_device_halftone *pdht = r_ptr(&esp[4], gx_device_halftone);
  246. gs_halftone *pht = r_ptr(&esp[3], gs_halftone);
  247. gs_free_object(pdht->rc.memory, pdht,
  248. "sethalftone_cleanup(device halftone)");
  249. gs_free_object(pht->rc.memory, pht,
  250. "sethalftone_cleanup(halftone)");
  251. return 0;
  252. }
  253. /* ------ Initialization procedure ------ */
  254. const op_def zht2_l2_op_defs[] =
  255. {
  256. op_def_begin_level2(),
  257. {"2.sethalftone5", zsethalftone5},
  258. /* Internal operators */
  259. {"0%sethalftone_finish", sethalftone_finish},
  260. op_def_end(0)
  261. };
  262. /* ------ Internal routines ------ */
  263. /* Extract frequency, angle, spot function, and accurate screens flag */
  264. /* from a dictionary. */
  265. private int
  266. dict_spot_params(const ref * pdict, gs_spot_halftone * psp,
  267. ref * psproc, ref * ptproc)
  268. {
  269. int code;
  270. check_dict_read(*pdict);
  271. if ((code = dict_float_param(pdict, "Frequency", 0.0,
  272. &psp->screen.frequency)) != 0 ||
  273. (code = dict_float_param(pdict, "Angle", 0.0,
  274. &psp->screen.angle)) != 0 ||
  275. (code = dict_proc_param(pdict, "SpotFunction", psproc, false)) != 0 ||
  276. (code = dict_bool_param(pdict, "AccurateScreens",
  277. gs_currentaccuratescreens(),
  278. &psp->accurate_screens)) < 0 ||
  279. (code = dict_proc_param(pdict, "TransferFunction", ptproc, false)) < 0
  280. )
  281. return (code < 0 ? code : e_undefined);
  282. psp->transfer = (code > 0 ? (gs_mapping_proc) 0 : gs_mapped_transfer);
  283. psp->transfer_closure.proc = 0;
  284. psp->transfer_closure.data = 0;
  285. return 0;
  286. }
  287. /* Set actual frequency and angle in a dictionary. */
  288. private int
  289. dict_real_result(i_ctx_t *i_ctx_p, ref * pdict, const char *kstr, floatp val)
  290. {
  291. int code = 0;
  292. ref *ignore;
  293. if (dict_find_string(pdict, kstr, &ignore) > 0) {
  294. ref rval;
  295. check_dict_write(*pdict);
  296. make_real(&rval, val);
  297. code = idict_put_string(pdict, kstr, &rval);
  298. }
  299. return code;
  300. }
  301. private int
  302. dict_spot_results(i_ctx_t *i_ctx_p, ref * pdict, const gs_spot_halftone * psp)
  303. {
  304. int code;
  305. code = dict_real_result(i_ctx_p, pdict, "ActualFrequency",
  306. psp->screen.actual_frequency);
  307. if (code < 0)
  308. return code;
  309. return dict_real_result(i_ctx_p, pdict, "ActualAngle",
  310. psp->screen.actual_angle);
  311. }
  312. /* Extract Width, Height, and TransferFunction from a dictionary. */
  313. private int
  314. dict_threshold_common_params(const ref * pdict,
  315. gs_threshold_halftone_common * ptp,
  316. ref **pptstring, ref *ptproc)
  317. {
  318. int code;
  319. check_dict_read(*pdict);
  320. if ((code = dict_int_param(pdict, "Width", 1, 0x7fff, -1,
  321. &ptp->width)) < 0 ||
  322. (code = dict_int_param(pdict, "Height", 1, 0x7fff, -1,
  323. &ptp->height)) < 0 ||
  324. (code = dict_find_string(pdict, "Thresholds", pptstring)) <= 0 ||
  325. (code = dict_proc_param(pdict, "TransferFunction", ptproc, false)) < 0
  326. )
  327. return (code < 0 ? code : e_undefined);
  328. ptp->transfer_closure.proc = 0;
  329. ptp->transfer_closure.data = 0;
  330. return code;
  331. }
  332. /* Extract threshold common parameters + Thresholds. */
  333. private int
  334. dict_threshold_params(const ref * pdict, gs_threshold_halftone * ptp,
  335. ref * ptproc)
  336. {
  337. ref *tstring;
  338. int code =
  339. dict_threshold_common_params(pdict,
  340. (gs_threshold_halftone_common *)ptp,
  341. &tstring, ptproc);
  342. if (code < 0)
  343. return code;
  344. check_read_type_only(*tstring, t_string);
  345. if (r_size(tstring) != (long)ptp->width * ptp->height)
  346. return_error(e_rangecheck);
  347. ptp->thresholds.data = tstring->value.const_bytes;
  348. ptp->thresholds.size = r_size(tstring);
  349. ptp->transfer = (code > 0 ? (gs_mapping_proc) 0 : gs_mapped_transfer);
  350. return 0;
  351. }
  352. /* Extract threshold common parameters + Thresholds, Width2, Height2, */
  353. /* BitsPerSample. */
  354. private int
  355. dict_threshold2_params(const ref * pdict, gs_threshold2_halftone * ptp,
  356. ref * ptproc, gs_memory_t *mem)
  357. {
  358. ref *tstring;
  359. int code =
  360. dict_threshold_common_params(pdict,
  361. (gs_threshold_halftone_common *)ptp,
  362. &tstring, ptproc);
  363. int bps;
  364. uint size;
  365. int cw2, ch2;
  366. if (code < 0 ||
  367. (code = cw2 = dict_int_param(pdict, "Width2", 0, 0x7fff, 0,
  368. &ptp->width2)) < 0 ||
  369. (code = ch2 = dict_int_param(pdict, "Height2", 0, 0x7fff, 0,
  370. &ptp->height2)) < 0 ||
  371. (code = dict_int_param(pdict, "BitsPerSample", 8, 16, -1, &bps)) < 0
  372. )
  373. return code;
  374. if ((bps != 8 && bps != 16) || cw2 != ch2 ||
  375. (!cw2 && (ptp->width2 == 0 || ptp->height2 == 0))
  376. )
  377. return_error(e_rangecheck);
  378. ptp->bytes_per_sample = bps / 8;
  379. switch (r_type(tstring)) {
  380. case t_string:
  381. size = r_size(tstring);
  382. gs_bytestring_from_string(&ptp->thresholds, tstring->value.const_bytes,
  383. size);
  384. break;
  385. case t_astruct:
  386. if (gs_object_type(mem, tstring->value.pstruct) != &st_bytes)
  387. return_error(e_typecheck);
  388. size = gs_object_size(mem, tstring->value.pstruct);
  389. gs_bytestring_from_bytes(&ptp->thresholds, r_ptr(tstring, byte),
  390. 0, size);
  391. break;
  392. default:
  393. return_error(e_typecheck);
  394. }
  395. check_read(*tstring);
  396. if (size != (ptp->width * ptp->height + ptp->width2 * ptp->height2) *
  397. ptp->bytes_per_sample)
  398. return_error(e_rangecheck);
  399. return 0;
  400. }