1
0

gxdhtserial.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /* Copyright (C) 2002 artofcode LLC. 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: gxdhtserial.c,v 1.8 2005/03/14 18:08:37 dan Exp $ */
  14. /* Serialization and de-serialization for (traditional) halftones */
  15. #include "memory_.h"
  16. #include <assert.h>
  17. #include "gx.h"
  18. #include "gscdefs.h"
  19. #include "gserrors.h"
  20. #include "gsstruct.h"
  21. #include "gsutil.h" /* for gs_next_ids */
  22. #include "gzstate.h"
  23. #include "gxdevice.h" /* for gzht.h */
  24. #include "gzht.h"
  25. #include "gswts.h"
  26. #include "gxdhtres.h"
  27. #include "gsserial.h"
  28. #include "gxdhtserial.h"
  29. /*
  30. * Declare the set of procedures that return resident halftones. This
  31. * declares both the array of procedures and their type. It is used
  32. * only to check if a transmitted halftone order matches one in ROM.
  33. */
  34. extern_gx_device_halftone_list();
  35. /*
  36. * An enumeration of halftone transfer functions. These must distinguish
  37. * between cases in which no transfer function is present, and when one
  38. * is present but provides the identity transformation (an empty
  39. * PostScript array).
  40. */
  41. typedef enum {
  42. gx_ht_tf_none = 0,
  43. gx_ht_tf_identity,
  44. gx_ht_tf_complete
  45. } gx_ht_tf_type_t;
  46. /* enumeration to distinguish well-tempered screening orders from others */
  47. typedef enum {
  48. gx_ht_traditional,
  49. gx_ht_wts
  50. } gx_ht_order_type_t;
  51. /*
  52. * Serialize a transfer function. These will occupy one byte if they are
  53. * not present or provide an identity mapping,
  54. * 1 + transfer_map_size * sizeof(frac) otherwise.
  55. *
  56. * Returns:
  57. *
  58. * 0, with *psize set the the amount of space required, if successful
  59. *
  60. * gs_error_rangecheck, with *psize set to the size required, if the
  61. * original *psize was not large enough
  62. */
  63. private int
  64. gx_ht_write_tf(
  65. const gx_transfer_map * pmap,
  66. byte * data,
  67. uint * psize )
  68. {
  69. int req_size = 1; /* minimum of one byte */
  70. /* check for sufficient space */
  71. if ( pmap != 0 && pmap->proc != gs_identity_transfer)
  72. req_size += sizeof(pmap->values);
  73. if (req_size > *psize) {
  74. *psize = req_size;
  75. return gs_error_rangecheck;
  76. }
  77. if (req_size == 1)
  78. *data = (byte)(pmap == 0 ? gx_ht_tf_none : gx_ht_tf_identity);
  79. else {
  80. *data++ = (byte)gx_ht_tf_complete;
  81. memcpy(data, pmap->values, sizeof(pmap->values));
  82. }
  83. *psize = req_size;
  84. return 0;
  85. }
  86. /*
  87. * Reconstruct a transfer function from its serial representation. The
  88. * buffer provided is expected to be large enough to hold the entire
  89. * transfer function.
  90. *
  91. * Returns the number of bytes read, or < 0 in the event of an error.
  92. */
  93. private int
  94. gx_ht_read_tf(
  95. gx_transfer_map ** ppmap,
  96. const byte * data,
  97. uint size,
  98. gs_memory_t * mem )
  99. {
  100. gx_ht_tf_type_t tf_type;
  101. gx_transfer_map * pmap;
  102. /* read the type byte */
  103. if (size == 0)
  104. return_error(gs_error_rangecheck);
  105. --size;
  106. tf_type = (gx_ht_tf_type_t)*data++;
  107. /* if no transfer function, exit now */
  108. if (tf_type == gx_ht_tf_none) {
  109. *ppmap = 0;
  110. return 1;
  111. }
  112. /* allocate a transfer map */
  113. rc_alloc_struct_1( pmap,
  114. gx_transfer_map,
  115. &st_transfer_map,
  116. mem,
  117. return_error(gs_error_VMerror),
  118. "gx_ht_read_tf" );
  119. pmap->id = gs_next_ids(mem, 1);
  120. pmap->closure.proc = 0;
  121. pmap->closure.data = 0;
  122. if (tf_type == gx_ht_tf_identity) {
  123. gx_set_identity_transfer(pmap);
  124. return 1;
  125. } else if (tf_type == gx_ht_tf_complete && size >= sizeof(pmap->values)) {
  126. memcpy(pmap->values, data, sizeof(pmap->values));
  127. pmap->proc = gs_mapped_transfer;
  128. *ppmap = pmap;
  129. return 1 + sizeof(pmap->values);
  130. } else {
  131. rc_decrement(pmap, "gx_ht_read_tf");
  132. return_error(gs_error_rangecheck);
  133. }
  134. }
  135. /*
  136. * Serialize a halftone component. The only part that is serialized is the
  137. * halftone order; the other two components are only required during
  138. * halftone construction.
  139. *
  140. * Returns:
  141. *
  142. * 0, with *psize set the the amount of space required, if successful
  143. *
  144. * gs_error_rangecheck, with *psize set to the size required, if the
  145. * original *psize was not large enough
  146. *
  147. * some other error code, with *psize unchange, in the event of an
  148. * error other than lack of space
  149. */
  150. private int
  151. gx_ht_write_component(
  152. const gx_ht_order_component * pcomp,
  153. byte * data,
  154. uint * psize )
  155. {
  156. const gx_ht_order * porder = &pcomp->corder;
  157. byte * data0 = data;
  158. int code, levels_size, bits_size;
  159. uint tmp_size = 0;
  160. int req_size;
  161. /*
  162. * There is no need to transmit the comp_number field, as this must be
  163. * the same as the index in the component array (see gx_ht_write).
  164. *
  165. * There is also no reason to transmit the colorant name (cname), as
  166. * this is only used by some high-level devices that would not be targets
  167. * of the command list device (and even those devices should be able to
  168. * get the information from their color models).
  169. *
  170. * This leaves the order itself.
  171. *
  172. * Check if we are a well-tempered-screening order. Serialization of these
  173. * is not yet implemented.
  174. */
  175. if (porder->wts != 0)
  176. return_error(gs_error_unknownerror); /* not yet supported */
  177. /*
  178. * The following order fields are not transmitted:
  179. *
  180. * params Only required during halftone cell construction
  181. *
  182. * wse, wts Only used for well-tempered screens (see above)
  183. *
  184. * raster Can be re-calculated by the renderer from the width
  185. *
  186. * orig_height, The only potential use for these parameters is in
  187. * orig_shift this routine; they are not useful to the renderer.
  188. *
  189. * full_height Can be re-calculated by the renderer from the
  190. * height, width, and shift values.
  191. *
  192. * data_memory Must be provided by the renderer.
  193. *
  194. * cache Must be provided by the renderer.
  195. *
  196. * screen_params Ony required during halftone cell construction
  197. *
  198. * In addition, the procs parameter is passed as an index into the
  199. * ht_order_procs_table, as the renderer may not be in the same address
  200. * space as the writer.
  201. *
  202. * Calculate the size required.
  203. */
  204. levels_size = porder->num_levels * sizeof(porder->levels[0]);
  205. bits_size = porder->num_bits * porder->procs->bit_data_elt_size;
  206. req_size = 1 /* gx_ht_type_t */
  207. + enc_u_sizew(porder->width)
  208. + enc_u_sizew(porder->height)
  209. + enc_u_sizew(porder->shift)
  210. + enc_u_sizew(porder->num_levels)
  211. + enc_u_sizew(porder->num_bits)
  212. + 1 /* order procs, as index into table */
  213. + levels_size
  214. + bits_size;
  215. code = gx_ht_write_tf(porder->transfer, data, &tmp_size);
  216. if (code < 0 && code != gs_error_rangecheck)
  217. return code;
  218. req_size += tmp_size;
  219. if (req_size > *psize) {
  220. *psize = req_size;
  221. return gs_error_rangecheck;
  222. }
  223. /* identify this as a traditional halftone */
  224. *data++ = (byte)gx_ht_traditional;
  225. /* write out the dimensional data */
  226. enc_u_putw(porder->width, data);
  227. enc_u_putw(porder->height, data);
  228. enc_u_putw(porder->shift, data);
  229. enc_u_putw(porder->num_levels, data);
  230. enc_u_putw(porder->num_bits, data);
  231. /* white out the procs index */
  232. *data++ = porder->procs - ht_order_procs_table;
  233. /* copy the levels array and whitening order array */
  234. memcpy(data, porder->levels, levels_size);
  235. data += levels_size;
  236. memcpy(data, porder->bit_data, bits_size);
  237. data += bits_size;
  238. /* write out the transfer function */
  239. tmp_size = *psize - (data - data0);
  240. if ((code = gx_ht_write_tf(porder->transfer, data, &tmp_size)) == 0)
  241. *psize = tmp_size + (data - data0);
  242. return code;
  243. }
  244. /*
  245. * Reconstruct a halftone component from its serial representation. The
  246. * buffer provided is expected to be large enough to hold the entire
  247. * halftone component.
  248. *
  249. * Because halftone components are allocated in arrays (an unfortunate
  250. * arrangement, as it prevents component sharing), a pointer to an
  251. * already allocated component structure is passed as an operand, as
  252. * opposed to the more normal mechanism that would have a read routine
  253. * allocate the component. The memory pointer is still passed, however,
  254. * as the levels and bit_data arrays must be allocated.
  255. *
  256. * Returns the number of bytes read, or < 0 in the event of an error.
  257. */
  258. private int
  259. gx_ht_read_component(
  260. gx_ht_order_component * pcomp,
  261. const byte * data,
  262. uint size,
  263. gs_memory_t * mem )
  264. {
  265. gx_ht_order new_order;
  266. const byte * data0 = data;
  267. const byte * data_lim = data + size;
  268. gx_ht_order_type_t order_type;
  269. int i, code, levels_size, bits_size;
  270. const gx_dht_proc * phtrp = gx_device_halftone_list;
  271. /* check the order type */
  272. if (size == 0)
  273. return_error(gs_error_rangecheck);
  274. --size;
  275. order_type = (gx_ht_order_type_t)*data++;
  276. /* currently only the traditional halftone order are supported */
  277. if (order_type != gx_ht_traditional)
  278. return_error(gs_error_unknownerror);
  279. /*
  280. * For performance reasons, the number encoding macros do not
  281. * support full buffer size verification. The code below verifies
  282. * that a minimum number of bytes is available, then converts
  283. * blindly and does not check again until the various integers are
  284. * read. Obviously this can be hazardous, but should not be a
  285. * problem in practice, as the calling code should have verified
  286. * that the data provided holds the entire halftone.
  287. */
  288. if (size < 7)
  289. return_error(gs_error_rangecheck);
  290. enc_u_getw(new_order.width, data);
  291. enc_u_getw(new_order.height, data);
  292. enc_u_getw(new_order.shift, data);
  293. enc_u_getw(new_order.num_levels, data);
  294. enc_u_getw(new_order.num_bits, data);
  295. if (data >= data_lim)
  296. return_error(gs_error_rangecheck);
  297. new_order.procs = &ht_order_procs_table[*data++];
  298. /* calculate the space required for levels and bit data */
  299. levels_size = new_order.num_levels * sizeof(new_order.levels[0]);
  300. bits_size = new_order.num_bits * new_order.procs->bit_data_elt_size;
  301. /* + 1 below is for the minimal transfer function */
  302. if (data + bits_size + levels_size + 1 > data_lim)
  303. return_error(gs_error_rangecheck);
  304. /*
  305. * Allocate the levels and bit data structures. The gx_ht_alloc_ht_order
  306. * has a name that is both strange and misleading. The routine does
  307. * not allocate a halftone order. Rather, it initializes the order,
  308. * and allocates the levels and bit data arrays. In particular, it
  309. * sets all of the following fields:
  310. *
  311. * wse = 0,
  312. * wts = 0,
  313. * width = operand width
  314. * height = operand height
  315. * raster = bitmap_raster(operand width)
  316. * shift = operand shift
  317. * orig_height = operand height
  318. * orig_shift = operand strip_shift
  319. * num_levels = operand num_levels
  320. * num_bits = operand num_bits
  321. * procs = operand procs
  322. * levels = newly allocated array
  323. * bit_data = new allocated array
  324. * cache = 0
  325. * transfer = 0
  326. *
  327. * Since several of the list fields are already set, this call
  328. * effectively sets them to the values they already have. This is a
  329. * bit peculiar but not otherwise harmful.
  330. *
  331. * For reasons that are not known and are probably historical, the
  332. * procedure does not initialize the params or screen_params fields.
  333. * In the unlikely event that these fields are ever contain pointers,
  334. * we initialize them explicitly here. Wse, params, and scrren_params
  335. * probably should not occur in the device halftone at all; they are
  336. * themselves historical artifacts.
  337. */
  338. code = gx_ht_alloc_ht_order( &new_order,
  339. new_order.width,
  340. new_order.height,
  341. new_order.num_levels,
  342. new_order.num_bits,
  343. new_order.shift,
  344. new_order.procs,
  345. mem );
  346. if (code < 0)
  347. return code;
  348. memset(&new_order.params, 0, sizeof(new_order.params));
  349. memset(&new_order.screen_params, 0, sizeof(new_order.screen_params));
  350. /* fill in the levels and bit_data arrays */
  351. memcpy(new_order.levels, data, levels_size);
  352. data += levels_size;
  353. memcpy(new_order.bit_data, data, bits_size);
  354. data += bits_size;
  355. /* process the transfer function */
  356. code = gx_ht_read_tf(&new_order.transfer, data, data_lim - data, mem);
  357. if (code < 0) {
  358. gx_ht_order_release(&new_order, mem, false);
  359. return code;
  360. }
  361. data += code;
  362. /*
  363. * Check to see if the order is in ROM. Since it is possible (if not
  364. * particularly likely) that the command list writer and renderer do
  365. * not have the same set of ROM-based halftones, the full halftone
  366. * order is transmitted and compared against the set ROM set provided
  367. * by the renderer. If there is a match, the transmitted version is
  368. * discarded and the ROM version used.
  369. *
  370. * It is not clear which, if any or the currently used devices
  371. * provide a ROM-based halftone order set.
  372. */
  373. for (i = 0; phtrp[i] != 0; i++) {
  374. const gx_device_halftone_resource_t *const * pphtr = phtrp[i]();
  375. const gx_device_halftone_resource_t * phtr;
  376. while ((phtr = *pphtr++) != 0) {
  377. /*
  378. * This test does not check for strict equality of the order,
  379. * nor is strict equality necessary. The ROM data will replace
  380. * just the levels and bit_data arrays of the transmitted
  381. * order, so only these must be the same. We don't even care
  382. * if the ROM's levels and bit_data arrays are larger; we
  383. * will never check values beyond the range required by the
  384. * current order.
  385. */
  386. if ( phtr->num_levels * sizeof(phtr->levels[0]) >= levels_size &&
  387. phtr->Width * phtr->Height * phtr->elt_size >= bits_size &&
  388. memcmp(phtr->levels, new_order.levels, levels_size) == 0 &&
  389. memcmp(phtr->bit_data, new_order.bit_data, bits_size) == 0 ) {
  390. /* the casts below are required to discard const qualifiers */
  391. gs_free_object(mem, new_order.bit_data, "gx_ht_read_component");
  392. new_order.bit_data = (void *)phtr->bit_data;
  393. gs_free_object(mem, new_order.levels, "gx_ht_read_component");
  394. new_order.levels = (uint *)phtr->levels;
  395. goto done;
  396. }
  397. }
  398. }
  399. done:
  400. /* everything OK, save the order and return the # of bytes read */
  401. pcomp->corder = new_order;
  402. pcomp->cname = 0;
  403. return data - data0;
  404. }
  405. /*
  406. * Serialize a halftone. The essential step is the serialization of the
  407. * halftone orders; beyond this only the halftone type must be
  408. * transmitted.
  409. *
  410. * Returns:
  411. *
  412. * 0, with *psize set the the amount of space required, if successful
  413. *
  414. * gs_error_rangecheck, with *psize set to the size required, if the
  415. * original *psize was not large enough
  416. *
  417. * some other error code, with *psize unchange, in the event of an
  418. * error other than lack of space
  419. */
  420. int
  421. gx_ht_write(
  422. const gx_device_halftone * pdht,
  423. const gx_device * dev,
  424. byte * data,
  425. uint * psize )
  426. {
  427. int num_dev_comps = pdht->num_dev_comp;
  428. int i, code;
  429. uint req_size = 2, used_size = 2;
  430. /* 1 for halftone type, 1 for num_dev_comps */
  431. /*
  432. * With the introduction of color models, there should never be a
  433. * halftone that includes just one component. Enforce this
  434. * restriction, even though it is not present in much of the rest
  435. * of the code.
  436. *
  437. * NB: the pdht->order field is ignored by this code.
  438. */
  439. assert(pdht != 0 && pdht->components != 0);
  440. /*
  441. * The following fields do not need to be transmitted:
  442. *
  443. * order Ignored by this code (see above).
  444. *
  445. * rc, id Recreated by the allocation code on the renderer.
  446. *
  447. * lcm_width, Can be recreated by the de-serialization code on the
  448. * lcm_height the renderer. Since halftones are transmitted
  449. * infrequently (for normal jobs), the time required
  450. * for re-calculation is not significant.
  451. *
  452. * Hence, the only fields that must be serialized are the type,and
  453. * the number of components. (The number of components for the halftone
  454. * may not match the device's if we are compositing with a process color
  455. * model which does not match the output device.
  456. *
  457. * Several halftone components may be identical, but there is
  458. * currently no simple way to determine this. Halftones are normally
  459. * transmitted only once per page, so it is not clear that use of
  460. * such information would significantly reduce command list size.
  461. */
  462. /* calculate the required data space */
  463. for ( i = 0, code = gs_error_rangecheck;
  464. i < num_dev_comps && code == gs_error_rangecheck;
  465. i++) {
  466. uint tmp_size = 0;
  467. /* sanity check */
  468. assert(i == pdht->components[i].comp_number);
  469. code = gx_ht_write_component( &pdht->components[i],
  470. data,
  471. &tmp_size );
  472. req_size += tmp_size;
  473. }
  474. if (code < 0 && code != gs_error_rangecheck)
  475. return code;
  476. else if (*psize < req_size) {
  477. *psize = req_size;
  478. return 0;
  479. }
  480. req_size = *psize;
  481. /* the halftone type is known to fit in a byte */
  482. *data++ = (byte)pdht->type;
  483. /* the number of components is known to fit in a byte */
  484. *data++ = (byte)num_dev_comps;
  485. /* serialize the halftone components */
  486. for (i = 0, code = 0; i < num_dev_comps && code == 0; i++) {
  487. uint tmp_size = req_size - used_size;
  488. code = gx_ht_write_component( &pdht->components[i],
  489. data,
  490. &tmp_size );
  491. used_size += tmp_size;
  492. data += tmp_size;
  493. }
  494. if (code < 0) {
  495. if (code == gs_error_rangecheck)
  496. code = gs_error_unknownerror;
  497. return code;
  498. }
  499. *psize = used_size;
  500. return 0;
  501. }
  502. /*
  503. * Reconstruct a halftone from its serial representation, and install it
  504. * as the current halftone. The buffer provided is expected to be large
  505. * enough to hold the entire halftone.
  506. *
  507. * The reading and installation phases are combined in this routine so as
  508. * to avoid unnecessarily allocating a device halftone and its component
  509. * array, just to release them immediately after installation is complete.
  510. * There is also not much reason to reconstuct a halftone except to make
  511. * it the current halftone.
  512. *
  513. * Returns the number of bytes read, or <0 in the event of an error.
  514. */
  515. int
  516. gx_ht_read_and_install(
  517. gs_imager_state * pis,
  518. const gx_device * dev,
  519. const byte * data,
  520. uint size,
  521. gs_memory_t * mem )
  522. {
  523. gx_ht_order_component components[GX_DEVICE_COLOR_MAX_COMPONENTS];
  524. const byte * data0 = data;
  525. gx_device_halftone dht;
  526. int num_dev_comps;
  527. int i, code;
  528. /* fill in some fixed fields */
  529. memset(&dht.order, 0, sizeof(dht.order));
  530. memset(&dht.rc, 0, sizeof(dht.rc));
  531. dht.id = gs_no_id; /* updated during installation */
  532. dht.components = components;
  533. dht.lcm_width = 1; /* recalculated during installation */
  534. dht.lcm_height = 1;
  535. /* clear pointers in the components array in case we need to abort */
  536. memset(components, 0, sizeof(components));
  537. /* get the halftone type */
  538. if (size-- < 1)
  539. return_error(gs_error_rangecheck);
  540. dht.type = (gs_halftone_type)(*data++);
  541. num_dev_comps = dht.num_dev_comp = dht.num_comp = *data++;
  542. /* process the component orders */
  543. for (i = 0, code = 0; i < num_dev_comps && code >= 0; i++) {
  544. components[i].comp_number = i;
  545. code = gx_ht_read_component(&components[i], data, size, mem);
  546. if (code >= 0) {
  547. size -= code;
  548. data += code;
  549. }
  550. }
  551. /* if everything is OK, install the halftone */
  552. if (code >= 0)
  553. code = gx_imager_dev_ht_install(pis, &dht, dht.type, dev);
  554. /*
  555. * If installation failed, discard the allocated elements. We can't
  556. * use the gx_device_halftone_release procedure, as the components
  557. * array is on the stack rather than in the heap.
  558. */
  559. if (code < 0) {
  560. for (i = 0; i < num_dev_comps; i++)
  561. gx_ht_order_release(&components[i].corder, mem, false);
  562. }
  563. return code < 0 ? code : data - data0;
  564. }