gdevpsdp.c 26 KB

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