gdevpsdp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /* Copyright (C) 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: gdevpsdp.c,v 1.14 2004/06/30 14:35:37 igor Exp $ */
  14. /* (Distiller) parameter handling for PostScript and PDF writers */
  15. #include "string_.h"
  16. #include "jpeglib_.h" /* for sdct.h */
  17. #include "gx.h"
  18. #include "gserrors.h"
  19. #include "gsutil.h"
  20. #include "gxdevice.h"
  21. #include "gsparamx.h"
  22. #include "gdevpsdf.h"
  23. #include "strimpl.h" /* for short-sighted compilers */
  24. #include "scfx.h"
  25. #include "sdct.h"
  26. #include "slzwx.h"
  27. #include "spprint.h"
  28. #include "srlx.h"
  29. #include "szlibx.h"
  30. /* Define a (bogus) GC descriptor for gs_param_string. */
  31. /* The only ones we use are GC-able and not persistent. */
  32. gs_private_st_composite(st_gs_param_string, gs_param_string, "gs_param_string",
  33. param_string_enum_ptrs, param_string_reloc_ptrs);
  34. private
  35. ENUM_PTRS_WITH(param_string_enum_ptrs, gs_param_string *pstr) return 0;
  36. case 0: return ENUM_CONST_STRING(pstr);
  37. ENUM_PTRS_END
  38. private
  39. RELOC_PTRS_WITH(param_string_reloc_ptrs, gs_param_string *pstr)
  40. {
  41. gs_const_string str;
  42. str.data = pstr->data, str.size = pstr->size;
  43. RELOC_CONST_STRING_VAR(str);
  44. pstr->data = str.data;
  45. }
  46. RELOC_PTRS_END
  47. gs_private_st_element(st_param_string_element, gs_param_string,
  48. "gs_param_string[]", param_string_elt_enum_ptrs,
  49. param_string_elt_reloc_ptrs, st_gs_param_string);
  50. /* ---------------- Get/put Distiller parameters ---------------- */
  51. /*
  52. * ColorConversionStrategy is supposed to affect output color space
  53. * according to the following table. ****** NOT IMPLEMENTED YET ******
  54. PS Input: LeaveCU UseDIC UseDICFI sRGB
  55. Gray art Gray CalGray/ICCBased Gray Gray
  56. Gray image Gray CalGray/ICCBased CalGray/ICCBased Gray
  57. RGB art RGB CalGray/ICCBased RGB CalRGB/sRGB
  58. RGB image RGB CalGray/ICCBased CalRGB/ICCBased CalRGB/sRGB
  59. CMYK art CMYK LAB/ICCBased CMYK CalRGB/sRGB
  60. CMYK image CMYK LAB/ICCBased LAB/ICCBased CalRGB/sRGB
  61. CIE art Cal/ICC Cal/ICC Cal/ICC CalRGB/sRGB
  62. CIE image Cal/ICC Cal/ICC Cal/ICC CalRGB/sRGB
  63. */
  64. /*
  65. * The Always/NeverEmbed parameters are defined as being incremental. Since
  66. * this isn't compatible with the general property of page devices that if
  67. * you do a currentpagedevice, doing a setpagedevice later will restore the
  68. * same state, we actually define the parameters in sets of 3:
  69. * - AlwaysEmbed is used for incremental additions.
  70. * - ~AlwaysEmbed is used for incremental deletions.
  71. * - .AlwaysEmbed is used for the complete list.
  72. * and analogously for NeverEmbed.
  73. */
  74. typedef struct psdf_image_filter_name_s {
  75. const char *pname;
  76. const stream_template *template;
  77. psdf_version min_version;
  78. } psdf_image_filter_name;
  79. private const psdf_image_filter_name Poly_filters[] = {
  80. {"DCTEncode", &s_DCTE_template},
  81. {"FlateEncode", &s_zlibE_template, psdf_version_ll3},
  82. {"LZWEncode", &s_LZWE_template},
  83. {0, 0}
  84. };
  85. private const psdf_image_filter_name Mono_filters[] = {
  86. {"CCITTFaxEncode", &s_CFE_template},
  87. {"FlateEncode", &s_zlibE_template, psdf_version_ll3},
  88. {"LZWEncode", &s_LZWE_template},
  89. {"RunLengthEncode", &s_RLE_template},
  90. {0, 0}
  91. };
  92. typedef struct psdf_image_param_names_s {
  93. const char *ACSDict; /* not used for mono */
  94. const char *Dict;
  95. const char *DownsampleType;
  96. float DownsampleThreshold_default;
  97. const psdf_image_filter_name *filter_names;
  98. const char *Filter;
  99. gs_param_item_t items[8]; /* AutoFilter (not used for mono), */
  100. /* AntiAlias, */
  101. /* Depth, Downsample, DownsampleThreshold, */
  102. /* Encode, Resolution, end marker */
  103. } psdf_image_param_names_t;
  104. #define pi(key, type, memb) { key, type, offset_of(psdf_image_params, memb) }
  105. #define psdf_image_param_names(acs, aa, af, de, di, ds, dt, dst, dstd, e, f, fns, r)\
  106. acs, di, dt, dstd, fns, f, {\
  107. pi(af, gs_param_type_bool, AutoFilter),\
  108. pi(aa, gs_param_type_bool, AntiAlias),\
  109. pi(de, gs_param_type_int, Depth),\
  110. pi(ds, gs_param_type_bool, Downsample),\
  111. pi(dst, gs_param_type_float, DownsampleThreshold),\
  112. pi(e, gs_param_type_bool, Encode),\
  113. pi(r, gs_param_type_int, Resolution),\
  114. gs_param_item_end\
  115. }
  116. private const psdf_image_param_names_t Color_names = {
  117. psdf_image_param_names(
  118. "ColorACSImageDict", "AntiAliasColorImages", "AutoFilterColorImages",
  119. "ColorImageDepth", "ColorImageDict",
  120. "DownsampleColorImages", "ColorImageDownsampleType",
  121. "ColorImageDownsampleThreshold", 1.5,
  122. "EncodeColorImages", "ColorImageFilter", Poly_filters,
  123. "ColorImageResolution"
  124. )
  125. };
  126. private const psdf_image_param_names_t Gray_names = {
  127. psdf_image_param_names(
  128. "GrayACSImageDict", "AntiAliasGrayImages", "AutoFilterGrayImages",
  129. "GrayImageDepth", "GrayImageDict",
  130. "DownsampleGrayImages", "GrayImageDownsampleType",
  131. "GrayImageDownsampleThreshold", 2.0,
  132. "EncodeGrayImages", "GrayImageFilter", Poly_filters,
  133. "GrayImageResolution"
  134. )
  135. };
  136. private const psdf_image_param_names_t Mono_names = {
  137. psdf_image_param_names(
  138. 0, "AntiAliasMonoImages", 0,
  139. "MonoImageDepth", "MonoImageDict",
  140. "DownsampleMonoImages", "MonoImageDownsampleType",
  141. "MonoImageDownsampleThreshold", 2.0,
  142. "EncodeMonoImages", "MonoImageFilter", Mono_filters,
  143. "MonoImageResolution"
  144. )
  145. };
  146. #undef pi
  147. private const char *const AutoRotatePages_names[] = {
  148. psdf_arp_names, 0
  149. };
  150. private const char *const ColorConversionStrategy_names[] = {
  151. psdf_ccs_names, 0
  152. };
  153. private const char *const DownsampleType_names[] = {
  154. psdf_ds_names, 0
  155. };
  156. private const char *const Binding_names[] = {
  157. psdf_binding_names, 0
  158. };
  159. private const char *const DefaultRenderingIntent_names[] = {
  160. psdf_ri_names, 0
  161. };
  162. private const char *const TransferFunctionInfo_names[] = {
  163. psdf_tfi_names, 0
  164. };
  165. private const char *const UCRandBGInfo_names[] = {
  166. psdf_ucrbg_names, 0
  167. };
  168. private const char *const CannotEmbedFontPolicy_names[] = {
  169. psdf_cefp_names, 0
  170. };
  171. private const gs_param_item_t psdf_param_items[] = {
  172. #define pi(key, type, memb) { key, type, offset_of(psdf_distiller_params, memb) }
  173. /* General parameters */
  174. pi("ASCII85EncodePages", gs_param_type_bool, ASCII85EncodePages),
  175. /* (AutoRotatePages) */
  176. /* (Binding) */
  177. pi("CompressPages", gs_param_type_bool, CompressPages),
  178. /* (DefaultRenderingIntent) */
  179. pi("DetectBlends", gs_param_type_bool, DetectBlends),
  180. pi("DoThumbnails", gs_param_type_bool, DoThumbnails),
  181. pi("ImageMemory", gs_param_type_long, ImageMemory),
  182. /* (LockDistillerParams) */
  183. pi("LZWEncodePages", gs_param_type_bool, LZWEncodePages),
  184. pi("OPM", gs_param_type_int, OPM),
  185. pi("PreserveHalftoneInfo", gs_param_type_bool, PreserveHalftoneInfo),
  186. pi("PreserveOPIComments", gs_param_type_bool, PreserveOPIComments),
  187. pi("PreserveOverprintSettings", gs_param_type_bool, PreserveOverprintSettings),
  188. /* (TransferFunctionInfo) */
  189. /* (UCRandBGInfo) */
  190. pi("UseFlateCompression", gs_param_type_bool, UseFlateCompression),
  191. /* Color image processing parameters */
  192. pi("ConvertCMYKImagesToRGB", gs_param_type_bool, ConvertCMYKImagesToRGB),
  193. pi("ConvertImagesToIndexed", gs_param_type_bool, ConvertImagesToIndexed),
  194. /* Font embedding parameters */
  195. /* (CannotEmbedFontPolicy) */
  196. pi("EmbedAllFonts", gs_param_type_bool, EmbedAllFonts),
  197. pi("MaxSubsetPct", gs_param_type_int, MaxSubsetPct),
  198. pi("SubsetFonts", gs_param_type_bool, SubsetFonts),
  199. #undef pi
  200. gs_param_item_end
  201. };
  202. /* -------- Get parameters -------- */
  203. private int
  204. psdf_write_name(gs_param_list *plist, const char *key, const char *str)
  205. {
  206. gs_param_string pstr;
  207. param_string_from_string(pstr, str);
  208. return param_write_name(plist, key, &pstr);
  209. }
  210. private int
  211. psdf_write_string_param(gs_param_list *plist, const char *key,
  212. const gs_const_string *pstr)
  213. {
  214. gs_param_string ps;
  215. ps.data = pstr->data;
  216. ps.size = pstr->size;
  217. ps.persistent = false;
  218. return param_write_string(plist, key, &ps);
  219. }
  220. /*
  221. * Get an image Dict parameter. Note that we return a default (empty)
  222. * dictionary if the parameter has never been set.
  223. */
  224. private int
  225. psdf_get_image_dict_param(gs_param_list * plist, const gs_param_name pname,
  226. gs_c_param_list *plvalue)
  227. {
  228. gs_param_dict dict;
  229. int code;
  230. if (pname == 0)
  231. return 0;
  232. dict.size = 12; /* enough for all param dicts we know about */
  233. if ((code = param_begin_write_dict(plist, pname, &dict, false)) < 0)
  234. return code;
  235. if (plvalue != 0) {
  236. gs_c_param_list_read(plvalue);
  237. code = param_list_copy(dict.list, (gs_param_list *)plvalue);
  238. }
  239. param_end_write_dict(plist, pname, &dict);
  240. return code;
  241. }
  242. /* Get a set of image-related parameters. */
  243. private int
  244. psdf_get_image_params(gs_param_list * plist,
  245. const psdf_image_param_names_t * pnames, psdf_image_params * params)
  246. {
  247. /* Skip AutoFilter for mono images. */
  248. const gs_param_item_t *items =
  249. (pnames->items[0].key == 0 ? pnames->items + 1 : pnames->items);
  250. int code;
  251. /*
  252. * We must actually return a value for every parameter, so that
  253. * all parameter names will be recognized as settable by -d or -s
  254. * from the command line.
  255. */
  256. if (
  257. (code = gs_param_write_items(plist, params, NULL, items)) < 0 ||
  258. (code = psdf_get_image_dict_param(plist, pnames->ACSDict,
  259. params->ACSDict)) < 0 ||
  260. /* (AntiAlias) */
  261. /* (AutoFilter) */
  262. /* (Depth) */
  263. (code = psdf_get_image_dict_param(plist, pnames->Dict,
  264. params->Dict)) < 0 ||
  265. /* (Downsample) */
  266. (code = psdf_write_name(plist, pnames->DownsampleType,
  267. DownsampleType_names[params->DownsampleType])) < 0 ||
  268. /* (DownsampleThreshold) */
  269. /* (Encode) */
  270. (code = psdf_write_name(plist, pnames->Filter,
  271. (params->Filter == 0 ?
  272. pnames->filter_names[0].pname :
  273. params->Filter))) < 0
  274. /* (Resolution) */
  275. )
  276. DO_NOTHING;
  277. return code;
  278. }
  279. /* Get a font embedding parameter. */
  280. private int
  281. psdf_get_embed_param(gs_param_list *plist, gs_param_name allpname,
  282. const gs_param_string_array *psa)
  283. {
  284. int code = param_write_name_array(plist, allpname, psa);
  285. if (code >= 0)
  286. code = param_write_name_array(plist, allpname + 1, psa);
  287. return code;
  288. }
  289. /* Get parameters. */
  290. int
  291. gdev_psdf_get_params(gx_device * dev, gs_param_list * plist)
  292. {
  293. gx_device_psdf *pdev = (gx_device_psdf *) dev;
  294. int code = gdev_vector_get_params(dev, plist);
  295. if (
  296. code < 0 ||
  297. (code = gs_param_write_items(plist, &pdev->params, NULL, psdf_param_items)) < 0 ||
  298. /* General parameters */
  299. (code = psdf_write_name(plist, "AutoRotatePages",
  300. AutoRotatePages_names[(int)pdev->params.AutoRotatePages])) < 0 ||
  301. (code = psdf_write_name(plist, "Binding",
  302. Binding_names[(int)pdev->params.Binding])) < 0 ||
  303. (code = psdf_write_name(plist, "DefaultRenderingIntent",
  304. DefaultRenderingIntent_names[(int)pdev->params.DefaultRenderingIntent])) < 0 ||
  305. (code = psdf_write_name(plist, "TransferFunctionInfo",
  306. TransferFunctionInfo_names[(int)pdev->params.TransferFunctionInfo])) < 0 ||
  307. (code = psdf_write_name(plist, "UCRandBGInfo",
  308. UCRandBGInfo_names[(int)pdev->params.UCRandBGInfo])) < 0 ||
  309. /* Color sampled image parameters */
  310. (code = psdf_get_image_params(plist, &Color_names, &pdev->params.ColorImage)) < 0 ||
  311. (code = psdf_write_name(plist, "ColorConversionStrategy",
  312. ColorConversionStrategy_names[(int)pdev->params.ColorConversionStrategy])) < 0 ||
  313. (code = psdf_write_string_param(plist, "CalCMYKProfile",
  314. &pdev->params.CalCMYKProfile)) < 0 ||
  315. (code = psdf_write_string_param(plist, "CalGrayProfile",
  316. &pdev->params.CalGrayProfile)) < 0 ||
  317. (code = psdf_write_string_param(plist, "CalRGBProfile",
  318. &pdev->params.CalRGBProfile)) < 0 ||
  319. (code = psdf_write_string_param(plist, "sRGBProfile",
  320. &pdev->params.sRGBProfile)) < 0 ||
  321. /* Gray sampled image parameters */
  322. (code = psdf_get_image_params(plist, &Gray_names, &pdev->params.GrayImage)) < 0 ||
  323. /* Mono sampled image parameters */
  324. (code = psdf_get_image_params(plist, &Mono_names, &pdev->params.MonoImage)) < 0 ||
  325. /* Font embedding parameters */
  326. (code = psdf_get_embed_param(plist, ".AlwaysEmbed", &pdev->params.AlwaysEmbed)) < 0 ||
  327. (code = psdf_get_embed_param(plist, ".NeverEmbed", &pdev->params.NeverEmbed)) < 0 ||
  328. (code = psdf_write_name(plist, "CannotEmbedFontPolicy",
  329. CannotEmbedFontPolicy_names[(int)pdev->params.CannotEmbedFontPolicy])) < 0
  330. )
  331. DO_NOTHING;
  332. return code;
  333. }
  334. /* -------- Put parameters -------- */
  335. extern stream_state_proc_put_params(s_CF_put_params, stream_CF_state);
  336. extern stream_state_proc_put_params(s_DCTE_put_params, stream_DCT_state);
  337. typedef stream_state_proc_put_params((*ss_put_params_t), stream_state);
  338. private int
  339. psdf_read_string_param(gs_param_list *plist, const char *key,
  340. gs_const_string *pstr, gs_memory_t *mem, int ecode)
  341. {
  342. gs_param_string ps;
  343. int code;
  344. switch (code = param_read_string(plist, key, &ps)) {
  345. case 0: {
  346. uint size = ps.size;
  347. byte *data = gs_alloc_string(mem, size, "psdf_read_string_param");
  348. if (data == 0)
  349. return_error(gs_error_VMerror);
  350. memcpy(data, ps.data, size);
  351. pstr->data = data;
  352. pstr->size = size;
  353. break;
  354. }
  355. default:
  356. ecode = code;
  357. case 1:
  358. break;
  359. }
  360. return ecode;
  361. }
  362. /*
  363. * The arguments and return value for psdf_put_enum are different because
  364. * we must cast the value both going in and coming out.
  365. */
  366. private int
  367. psdf_put_enum(gs_param_list *plist, const char *key, int value,
  368. const char *const pnames[], int *pecode)
  369. {
  370. *pecode = param_put_enum(plist, key, &value, pnames, *pecode);
  371. return value;
  372. }
  373. private int
  374. psdf_CF_put_params(gs_param_list * plist, stream_state * st)
  375. {
  376. stream_CFE_state *const ss = (stream_CFE_state *) st;
  377. (*s_CFE_template.set_defaults) (st);
  378. ss->K = -1;
  379. ss->BlackIs1 = true;
  380. return s_CF_put_params(plist, (stream_CF_state *) ss);
  381. }
  382. private int
  383. psdf_DCT_put_params(gs_param_list * plist, stream_state * st)
  384. {
  385. return psdf_DCT_filter(plist, st, 8 /*nominal*/, 8 /*ibid.*/, 3 /*ibid.*/,
  386. NULL);
  387. }
  388. /* Put [~](Always|Never)Embed parameters. */
  389. private int
  390. param_read_embed_array(gs_param_list * plist, gs_param_name pname,
  391. gs_param_string_array * psa, int ecode)
  392. {
  393. int code;
  394. psa->data = 0, psa->size = 0;
  395. switch (code = param_read_name_array(plist, pname, psa)) {
  396. default:
  397. ecode = code;
  398. param_signal_error(plist, pname, ecode);
  399. case 0:
  400. case 1:
  401. break;
  402. }
  403. return ecode;
  404. }
  405. private bool
  406. param_string_eq(const gs_param_string *ps1, const gs_param_string *ps2)
  407. {
  408. return !bytes_compare(ps1->data, ps1->size, ps2->data, ps2->size);
  409. }
  410. private int
  411. add_embed(gs_param_string_array *prsa, const gs_param_string_array *psa,
  412. gs_memory_t *mem)
  413. {
  414. uint i;
  415. gs_param_string *const rdata =
  416. (gs_param_string *)prsa->data; /* break const */
  417. uint count = prsa->size;
  418. for (i = 0; i < psa->size; ++i) {
  419. uint j;
  420. for (j = 0; j < count; ++j)
  421. if (param_string_eq(&psa->data[i], &rdata[j]))
  422. break;
  423. if (j == count) {
  424. uint size = psa->data[i].size;
  425. byte *data = gs_alloc_string(mem, size, "add_embed");
  426. if (data == 0)
  427. return_error(gs_error_VMerror);
  428. memcpy(data, psa->data[i].data, size);
  429. rdata[count].data = data;
  430. rdata[count].size = size;
  431. rdata[count].persistent = false;
  432. count++;
  433. }
  434. }
  435. prsa->size = count;
  436. return 0;
  437. }
  438. private void
  439. delete_embed(gs_param_string_array *prsa, const gs_param_string_array *pnsa,
  440. gs_memory_t *mem)
  441. {
  442. uint i;
  443. gs_param_string *const rdata =
  444. (gs_param_string *)prsa->data; /* break const */
  445. uint count = prsa->size;
  446. for (i = pnsa->size; i-- > 0;) {
  447. uint j;
  448. for (j = count; j-- > 0;)
  449. if (param_string_eq(&pnsa->data[i], &rdata[j]))
  450. break;
  451. if (j + 1 != 0) {
  452. gs_free_const_string(mem, rdata[j].data, rdata[j].size,
  453. "delete_embed");
  454. rdata[j] = rdata[--count];
  455. }
  456. }
  457. prsa->size = count;
  458. }
  459. private int
  460. psdf_put_embed_param(gs_param_list * plist, gs_param_name notpname,
  461. gs_param_name allpname, gs_param_string_array * psa,
  462. gs_memory_t *mem, int ecode)
  463. {
  464. gs_param_name pname = notpname + 1;
  465. gs_param_string_array sa, nsa, asa;
  466. bool replace;
  467. gs_param_string *rdata;
  468. gs_param_string_array rsa;
  469. int code = 0;
  470. mem = gs_memory_stable(mem);
  471. ecode = param_read_embed_array(plist, pname, &sa, ecode);
  472. ecode = param_read_embed_array(plist, notpname, &nsa, ecode);
  473. ecode = param_read_embed_array(plist, allpname, &asa, ecode);
  474. if (ecode < 0)
  475. return ecode;
  476. /*
  477. * Figure out whether we're replacing (sa == asa or asa and no sa,
  478. * no nsa) or updating (all other cases).
  479. */
  480. if (asa.data == 0 || nsa.data != 0)
  481. replace = false;
  482. else if (sa.data == 0)
  483. replace = true;
  484. else if (sa.size != asa.size)
  485. replace = false;
  486. else {
  487. /* Test whether sa == asa. */
  488. uint i;
  489. replace = true;
  490. for (i = 0; i < sa.size; ++i)
  491. if (!param_string_eq(&sa.data[i], &asa.data[i])) {
  492. replace = false;
  493. break;
  494. }
  495. if (replace)
  496. return 0; /* no-op */
  497. }
  498. if (replace) {
  499. /* Wholesale replacement, only asa is relevant. */
  500. rdata = gs_alloc_struct_array(mem, asa.size, gs_param_string,
  501. &st_param_string_element,
  502. "psdf_put_embed_param(replace)");
  503. if (rdata == 0)
  504. return_error(gs_error_VMerror);
  505. rsa.data = rdata;
  506. rsa.size = 0;
  507. if ((code = add_embed(&rsa, &asa, mem)) < 0) {
  508. gs_free_object(mem, rdata, "psdf_put_embed_param(replace)");
  509. ecode = code;
  510. } else
  511. delete_embed(psa, psa, mem);
  512. } else if (sa.data || nsa.data) {
  513. /* Incremental update, sa and nsa are relevant, asa is not. */
  514. rdata = gs_alloc_struct_array(mem, psa->size + sa.size,
  515. gs_param_string,
  516. &st_param_string_element,
  517. "psdf_put_embed_param(update)");
  518. if (rdata == 0)
  519. return_error(gs_error_VMerror);
  520. memcpy(rdata, psa->data, psa->size * sizeof(*psa->data));
  521. rsa.data = rdata;
  522. rsa.size = psa->size;
  523. if ((code = add_embed(&rsa, &sa, mem)) < 0) {
  524. gs_free_object(mem, rdata, "psdf_put_embed_param(update)");
  525. ecode = code;
  526. } else {
  527. delete_embed(&rsa, &nsa, mem);
  528. rsa.data = gs_resize_object(mem, rdata, rsa.size,
  529. "psdf_put_embed_param(resize)");
  530. }
  531. } else
  532. return 0; /* no-op */
  533. if (code >= 0) {
  534. gs_free_const_object(mem, psa->data, "psdf_put_embed_param(free)");
  535. rsa.persistent = false;
  536. *psa = rsa;
  537. }
  538. return ecode;
  539. }
  540. /* Put an image Dict parameter. */
  541. private int
  542. psdf_put_image_dict_param(gs_param_list * plist, const gs_param_name pname,
  543. gs_c_param_list **pplvalue,
  544. const stream_template * template,
  545. ss_put_params_t put_params, gs_memory_t * mem)
  546. {
  547. gs_param_dict dict;
  548. gs_c_param_list *plvalue = *pplvalue;
  549. int code;
  550. mem = gs_memory_stable(mem);
  551. switch (code = param_begin_read_dict(plist, pname, &dict, false)) {
  552. default:
  553. param_signal_error(plist, pname, code);
  554. return code;
  555. case 1:
  556. return 0;
  557. case 0: {
  558. /* Check the parameter values now. */
  559. stream_state *ss = s_alloc_state(mem, template->stype, pname);
  560. if (ss == 0)
  561. return_error(gs_error_VMerror);
  562. ss->template = template;
  563. if (template->set_defaults)
  564. template->set_defaults(ss);
  565. code = put_params(dict.list, ss);
  566. if (template->release)
  567. template->release(ss);
  568. gs_free_object(mem, ss, pname);
  569. if (code < 0) {
  570. param_signal_error(plist, pname, code);
  571. } else {
  572. plvalue = gs_c_param_list_alloc(mem, pname);
  573. if (plvalue == 0)
  574. return_error(gs_error_VMerror);
  575. gs_c_param_list_write(plvalue, mem);
  576. code = param_list_copy((gs_param_list *)plvalue,
  577. dict.list);
  578. if (code < 0) {
  579. gs_c_param_list_release(plvalue);
  580. gs_free_object(mem, plvalue, pname);
  581. plvalue = *pplvalue;
  582. }
  583. }
  584. }
  585. param_end_read_dict(plist, pname, &dict);
  586. break;
  587. }
  588. if (plvalue != *pplvalue) {
  589. if (*pplvalue)
  590. gs_c_param_list_release(*pplvalue);
  591. *pplvalue = plvalue;
  592. }
  593. return code;
  594. }
  595. /* Put a set of image-related parameters. */
  596. private int
  597. psdf_put_image_params(const gx_device_psdf * pdev, gs_param_list * plist,
  598. const psdf_image_param_names_t * pnames,
  599. psdf_image_params * params, int ecode)
  600. {
  601. gs_param_string fs;
  602. /*
  603. * Since this procedure can be called before the device is open,
  604. * we must use pdev->memory rather than pdev->v_memory.
  605. */
  606. gs_memory_t *mem = pdev->memory;
  607. gs_param_name pname;
  608. /* Skip AutoFilter for mono images. */
  609. const gs_param_item_t *items =
  610. (pnames->items[0].key == 0 ? pnames->items + 1 : pnames->items);
  611. int code = gs_param_read_items(plist, params, items);
  612. if ((pname = pnames->ACSDict) != 0) {
  613. code = psdf_put_image_dict_param(plist, pname, &params->ACSDict,
  614. &s_DCTE_template,
  615. psdf_DCT_put_params, mem);
  616. if (code < 0)
  617. ecode = code;
  618. }
  619. /* (AntiAlias) */
  620. /* (AutoFilter) */
  621. /* (Depth) */
  622. if ((pname = pnames->Dict) != 0) {
  623. const stream_template *template;
  624. ss_put_params_t put_params;
  625. /* Hack to determine what kind of a Dict we want: */
  626. if (pnames->Dict[0] == 'M')
  627. template = &s_CFE_template,
  628. put_params = psdf_CF_put_params;
  629. else
  630. template = &s_DCTE_template,
  631. put_params = psdf_DCT_put_params;
  632. code = psdf_put_image_dict_param(plist, pname, &params->Dict,
  633. template, put_params, mem);
  634. if (code < 0)
  635. ecode = code;
  636. }
  637. /* (Downsample) */
  638. params->DownsampleType = (enum psdf_downsample_type)
  639. psdf_put_enum(plist, pnames->DownsampleType,
  640. (int)params->DownsampleType, DownsampleType_names,
  641. &ecode);
  642. /* (DownsampleThreshold) */
  643. /* (Encode) */
  644. switch (code = param_read_string(plist, pnames->Filter, &fs)) {
  645. case 0:
  646. {
  647. const psdf_image_filter_name *pn = pnames->filter_names;
  648. while (pn->pname != 0 && !gs_param_string_eq(&fs, pn->pname))
  649. pn++;
  650. if (pn->pname == 0 || pn->min_version > pdev->version) {
  651. ecode = gs_error_rangecheck;
  652. goto ipe;
  653. }
  654. params->Filter = pn->pname;
  655. params->filter_template = pn->template;
  656. break;
  657. }
  658. default:
  659. ecode = code;
  660. ipe:param_signal_error(plist, pnames->Filter, ecode);
  661. case 1:
  662. break;
  663. }
  664. /* (Resolution) */
  665. if (ecode >= 0) { /* Force parameters to acceptable values. */
  666. if (params->Resolution < 1)
  667. params->Resolution = 1;
  668. if (params->DownsampleThreshold < 1 ||
  669. params->DownsampleThreshold > 10)
  670. params->DownsampleThreshold = pnames->DownsampleThreshold_default;
  671. switch (params->Depth) {
  672. default:
  673. params->Depth = -1;
  674. case 1:
  675. case 2:
  676. case 4:
  677. case 8:
  678. case -1:
  679. break;
  680. }
  681. }
  682. return ecode;
  683. }
  684. /* Put parameters. */
  685. int
  686. gdev_psdf_put_params(gx_device * dev, gs_param_list * plist)
  687. {
  688. gx_device_psdf *pdev = (gx_device_psdf *) dev;
  689. gs_memory_t *mem =
  690. (pdev->v_memory ? pdev->v_memory : dev->memory);
  691. int ecode, code;
  692. psdf_distiller_params params;
  693. params = pdev->params;
  694. /*
  695. * If LockDistillerParams was true and isn't being set to false,
  696. * ignore all other psdf parameters. However, do not ignore the
  697. * standard device parameters.
  698. */
  699. ecode = code = param_read_bool(plist, "LockDistillerParams",
  700. &params.LockDistillerParams);
  701. if (!(pdev->params.LockDistillerParams && params.LockDistillerParams)) {
  702. /* General parameters. */
  703. code = gs_param_read_items(plist, &params, psdf_param_items);
  704. if (code < 0)
  705. ecode = code;
  706. params.AutoRotatePages = (enum psdf_auto_rotate_pages)
  707. psdf_put_enum(plist, "AutoRotatePages", (int)params.AutoRotatePages,
  708. AutoRotatePages_names, &ecode);
  709. params.Binding = (enum psdf_binding)
  710. psdf_put_enum(plist, "Binding", (int)params.Binding,
  711. Binding_names, &ecode);
  712. params.DefaultRenderingIntent = (enum psdf_default_rendering_intent)
  713. psdf_put_enum(plist, "DefaultRenderingIntent",
  714. (int)params.DefaultRenderingIntent,
  715. DefaultRenderingIntent_names, &ecode);
  716. params.TransferFunctionInfo = (enum psdf_transfer_function_info)
  717. psdf_put_enum(plist, "TransferFunctionInfo",
  718. (int)params.TransferFunctionInfo,
  719. TransferFunctionInfo_names, &ecode);
  720. params.UCRandBGInfo = (enum psdf_ucr_and_bg_info)
  721. psdf_put_enum(plist, "UCRandBGInfo", (int)params.UCRandBGInfo,
  722. UCRandBGInfo_names, &ecode);
  723. ecode = param_put_bool(plist, "UseFlateCompression",
  724. &params.UseFlateCompression, ecode);
  725. /* Color sampled image parameters */
  726. ecode = psdf_put_image_params(pdev, plist, &Color_names,
  727. &params.ColorImage, ecode);
  728. params.ColorConversionStrategy = (enum psdf_color_conversion_strategy)
  729. psdf_put_enum(plist, "ColorConversionStrategy",
  730. (int)params.ColorConversionStrategy,
  731. ColorConversionStrategy_names, &ecode);
  732. ecode = psdf_read_string_param(plist, "CalCMYKProfile",
  733. &params.CalCMYKProfile, mem, ecode);
  734. ecode = psdf_read_string_param(plist, "CalGrayProfile",
  735. &params.CalGrayProfile, mem, ecode);
  736. ecode = psdf_read_string_param(plist, "CalRGBProfile",
  737. &params.CalRGBProfile, mem, ecode);
  738. ecode = psdf_read_string_param(plist, "sRGBProfile",
  739. &params.sRGBProfile, mem, ecode);
  740. /* Gray sampled image parameters */
  741. ecode = psdf_put_image_params(pdev, plist, &Gray_names,
  742. &params.GrayImage, ecode);
  743. /* Mono sampled image parameters */
  744. ecode = psdf_put_image_params(pdev, plist, &Mono_names,
  745. &params.MonoImage, ecode);
  746. /* Font embedding parameters */
  747. ecode = psdf_put_embed_param(plist, "~AlwaysEmbed", ".AlwaysEmbed",
  748. &params.AlwaysEmbed, mem, ecode);
  749. ecode = psdf_put_embed_param(plist, "~NeverEmbed", ".NeverEmbed",
  750. &params.NeverEmbed, mem, ecode);
  751. params.CannotEmbedFontPolicy = (enum psdf_cannot_embed_font_policy)
  752. psdf_put_enum(plist, "CannotEmbedFontPolicy",
  753. (int)params.CannotEmbedFontPolicy,
  754. CannotEmbedFontPolicy_names, &ecode);
  755. }
  756. if (ecode < 0)
  757. return ecode;
  758. code = gdev_vector_put_params(dev, plist);
  759. if (code < 0)
  760. return code;
  761. pdev->params = params; /* OK to update now */
  762. return 0;
  763. }