1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/* $Id: gxdhtserial.c 8714 2008-05-08 18:56:10Z ray $ */
15
/* Serialization and de-serialization for (traditional) halftones */
22
#include "gsutil.h" /* for gs_next_ids */
24
#include "gxdevice.h" /* for gzht.h */
29
#include "gxdhtserial.h"
33
* Declare the set of procedures that return resident halftones. This
34
* declares both the array of procedures and their type. It is used
35
* only to check if a transmitted halftone order matches one in ROM.
37
extern_gx_device_halftone_list();
41
* An enumeration of halftone transfer functions. These must distinguish
42
* between cases in which no transfer function is present, and when one
43
* is present but provides the identity transformation (an empty
52
/* enumeration to distinguish well-tempered screening orders from others */
60
* Serialize a transfer function. These will occupy one byte if they are
61
* not present or provide an identity mapping,
62
* 1 + transfer_map_size * sizeof(frac) otherwise.
66
* 0, with *psize set the the amount of space required, if successful
68
* gs_error_rangecheck, with *psize set to the size required, if the
69
* original *psize was not large enough
73
const gx_transfer_map * pmap,
77
int req_size = 1; /* minimum of one byte */
79
/* check for sufficient space */
80
if ( pmap != 0 && pmap->proc != gs_identity_transfer)
81
req_size += sizeof(pmap->values);
82
if (req_size > *psize) {
84
return gs_error_rangecheck;
88
*data = (byte)(pmap == 0 ? gx_ht_tf_none : gx_ht_tf_identity);
90
*data++ = (byte)gx_ht_tf_complete;
91
memcpy(data, pmap->values, sizeof(pmap->values));
99
* Reconstruct a transfer function from its serial representation. The
100
* buffer provided is expected to be large enough to hold the entire
103
* Returns the number of bytes read, or < 0 in the event of an error.
107
gx_transfer_map ** ppmap,
112
gx_ht_tf_type_t tf_type;
113
gx_transfer_map * pmap;
115
/* read the type byte */
117
return_error(gs_error_rangecheck);
119
tf_type = (gx_ht_tf_type_t)*data++;
121
/* if no transfer function, exit now */
122
if (tf_type == gx_ht_tf_none) {
127
/* allocate a transfer map */
128
rc_alloc_struct_1( pmap,
132
return_error(gs_error_VMerror),
135
pmap->id = gs_next_ids(mem, 1);
136
pmap->closure.proc = 0;
137
pmap->closure.data = 0;
138
if (tf_type == gx_ht_tf_identity) {
139
gx_set_identity_transfer(pmap);
141
} else if (tf_type == gx_ht_tf_complete && size >= sizeof(pmap->values)) {
142
memcpy(pmap->values, data, sizeof(pmap->values));
143
pmap->proc = gs_mapped_transfer;
145
return 1 + sizeof(pmap->values);
147
rc_decrement(pmap, "gx_ht_read_tf");
148
return_error(gs_error_rangecheck);
153
gx_ht_write_component_wts(const wts_screen_t *wts, byte *data, uint *psize)
155
uint hdr_size = wts_size(wts);
156
uint cell_nsamples = wts->cell_width * wts->cell_height;
157
uint cell_size = cell_nsamples * sizeof(wts_screen_sample_t);
158
uint req_size = 1 + hdr_size + cell_size;
160
if (req_size > *psize) {
162
return gs_error_rangecheck;
165
/* identify this as a wts halftone. */
166
*data++ = (byte)gx_ht_wts;
168
/* copy in wts header */
169
memcpy(data, wts, hdr_size);
170
((wts_screen_t *)data)->samples = NULL;
173
/* copy in treshold cell */
174
memcpy(data, wts->samples, cell_size);
180
* Serialize a halftone component. The only part that is serialized is the
181
* halftone order; the other two components are only required during
182
* halftone construction.
186
* 0, with *psize set the the amount of space required, if successful
188
* gs_error_rangecheck, with *psize set to the size required, if the
189
* original *psize was not large enough
191
* some other error code, with *psize unchanged, in the event of an
192
* error other than lack of space
195
gx_ht_write_component(
196
const gx_ht_order_component * pcomp,
200
const gx_ht_order * porder = &pcomp->corder;
202
int code, levels_size, bits_size;
207
* There is no need to transmit the comp_number field, as this must be
208
* the same as the index in the component array (see gx_ht_write).
210
* There is also no reason to transmit the colorant name (cname), as
211
* this is only used by some high-level devices that would not be targets
212
* of the command list device (and even those devices should be able to
213
* get the information from their color models).
215
* This leaves the order itself.
217
* Check if we are a well-tempered-screening order. Serialization of these
218
* is handled in a separate function.
220
if (porder->wts != 0)
221
return gx_ht_write_component_wts(porder->wts, data, psize);
224
* The following order fields are not transmitted:
226
* params Only required during halftone cell construction
228
* wse, wts Only used for well-tempered screens (see above)
230
* raster Can be re-calculated by the renderer from the width
232
* orig_height, The only potential use for these parameters is in
233
* orig_shift this routine; they are not useful to the renderer.
235
* full_height Can be re-calculated by the renderer from the
236
* height, width, and shift values.
238
* data_memory Must be provided by the renderer.
240
* cache Must be provided by the renderer.
242
* screen_params Ony required during halftone cell construction
244
* In addition, the procs parameter is passed as an index into the
245
* ht_order_procs_table, as the renderer may not be in the same address
246
* space as the writer.
248
* Calculate the size required.
250
levels_size = porder->num_levels * sizeof(porder->levels[0]);
251
bits_size = porder->num_bits * porder->procs->bit_data_elt_size;
252
req_size = 1 /* gx_ht_type_t */
253
+ enc_u_sizew(porder->width)
254
+ enc_u_sizew(porder->height)
255
+ enc_u_sizew(porder->shift)
256
+ enc_u_sizew(porder->num_levels)
257
+ enc_u_sizew(porder->num_bits)
258
+ 1 /* order procs, as index into table */
261
code = gx_ht_write_tf(porder->transfer, data, &tmp_size);
262
if (code < 0 && code != gs_error_rangecheck)
264
req_size += tmp_size;
265
if (req_size > *psize) {
267
return gs_error_rangecheck;
270
/* identify this as a traditional halftone */
271
*data++ = (byte)gx_ht_traditional;
273
/* write out the dimensional data */
274
enc_u_putw(porder->width, data);
275
enc_u_putw(porder->height, data);
276
enc_u_putw(porder->shift, data);
277
enc_u_putw(porder->num_levels, data);
278
enc_u_putw(porder->num_bits, data);
280
/* white out the procs index */
281
*data++ = porder->procs - ht_order_procs_table;
283
/* copy the levels array and whitening order array */
284
memcpy(data, porder->levels, levels_size);
286
memcpy(data, porder->bit_data, bits_size);
289
/* write out the transfer function */
290
tmp_size = *psize - (data - data0);
291
if ((code = gx_ht_write_tf(porder->transfer, data, &tmp_size)) == 0)
292
*psize = tmp_size + (data - data0);
297
gx_ht_read_component_wts(gx_ht_order_component *pcomp,
298
const byte *data, uint size,
301
const wts_screen_t *ws = (const wts_screen_t *)data;
302
int hdr_size = wts_size(ws);
303
int cell_size = ws->cell_width * ws->cell_height *
304
sizeof(wts_screen_sample_t);
305
int bufsize = 1+hdr_size+cell_size;
307
memset(&pcomp->corder, 0, sizeof(pcomp->corder));
311
pcomp->corder.wts = gs_wts_from_buf(data, bufsize);
313
if (pcomp->corder.wts == NULL)
320
* Reconstruct a halftone component from its serial representation. The
321
* buffer provided is expected to be large enough to hold the entire
322
* halftone component.
324
* Because halftone components are allocated in arrays (an unfortunate
325
* arrangement, as it prevents component sharing), a pointer to an
326
* already allocated component structure is passed as an operand, as
327
* opposed to the more normal mechanism that would have a read routine
328
* allocate the component. The memory pointer is still passed, however,
329
* as the levels and bit_data arrays must be allocated.
331
* Returns the number of bytes read, or < 0 in the event of an error.
334
gx_ht_read_component(
335
gx_ht_order_component * pcomp,
340
gx_ht_order new_order;
341
const byte * data0 = data;
342
const byte * data_lim = data + size;
343
gx_ht_order_type_t order_type;
344
int i, code, levels_size, bits_size;
345
const gx_dht_proc * phtrp = gx_device_halftone_list;
347
/* check the order type */
349
return_error(gs_error_rangecheck);
351
order_type = (gx_ht_order_type_t)*data++;
353
/* currently only the traditional halftone order are supported */
354
if (order_type != gx_ht_traditional)
355
return gx_ht_read_component_wts(pcomp, data, size, mem);
358
* For performance reasons, the number encoding macros do not
359
* support full buffer size verification. The code below verifies
360
* that a minimum number of bytes is available, then converts
361
* blindly and does not check again until the various integers are
362
* read. Obviously this can be hazardous, but should not be a
363
* problem in practice, as the calling code should have verified
364
* that the data provided holds the entire halftone.
367
return_error(gs_error_rangecheck);
368
enc_u_getw(new_order.width, data);
369
enc_u_getw(new_order.height, data);
370
enc_u_getw(new_order.shift, data);
371
enc_u_getw(new_order.num_levels, data);
372
enc_u_getw(new_order.num_bits, data);
373
if (data >= data_lim)
374
return_error(gs_error_rangecheck);
375
new_order.procs = &ht_order_procs_table[*data++];
377
/* calculate the space required for levels and bit data */
378
levels_size = new_order.num_levels * sizeof(new_order.levels[0]);
379
bits_size = new_order.num_bits * new_order.procs->bit_data_elt_size;
381
/* + 1 below is for the minimal transfer function */
382
if (data + bits_size + levels_size + 1 > data_lim)
383
return_error(gs_error_rangecheck);
386
* Allocate the levels and bit data structures. The gx_ht_alloc_ht_order
387
* has a name that is both strange and misleading. The routine does
388
* not allocate a halftone order. Rather, it initializes the order,
389
* and allocates the levels and bit data arrays. In particular, it
390
* sets all of the following fields:
394
* width = operand width
395
* height = operand height
396
* raster = bitmap_raster(operand width)
397
* shift = operand shift
398
* orig_height = operand height
399
* orig_shift = operand strip_shift
400
* num_levels = operand num_levels
401
* num_bits = operand num_bits
402
* procs = operand procs
403
* levels = newly allocated array
404
* bit_data = new allocated array
408
* Since several of the list fields are already set, this call
409
* effectively sets them to the values they already have. This is a
410
* bit peculiar but not otherwise harmful.
412
* For reasons that are not known and are probably historical, the
413
* procedure does not initialize the params or screen_params fields.
414
* In the unlikely event that these fields are ever contain pointers,
415
* we initialize them explicitly here. Wse, params, and scrren_params
416
* probably should not occur in the device halftone at all; they are
417
* themselves historical artifacts.
419
code = gx_ht_alloc_ht_order( &new_order,
422
new_order.num_levels,
429
memset(&new_order.params, 0, sizeof(new_order.params));
430
memset(&new_order.screen_params, 0, sizeof(new_order.screen_params));
432
/* fill in the levels and bit_data arrays */
433
memcpy(new_order.levels, data, levels_size);
435
memcpy(new_order.bit_data, data, bits_size);
438
/* process the transfer function */
439
code = gx_ht_read_tf(&new_order.transfer, data, data_lim - data, mem);
441
gx_ht_order_release(&new_order, mem, false);
447
* Check to see if the order is in ROM. Since it is possible (if not
448
* particularly likely) that the command list writer and renderer do
449
* not have the same set of ROM-based halftones, the full halftone
450
* order is transmitted and compared against the set ROM set provided
451
* by the renderer. If there is a match, the transmitted version is
452
* discarded and the ROM version used.
454
* It is not clear which, if any or the currently used devices
455
* provide a ROM-based halftone order set.
457
for (i = 0; phtrp[i] != 0; i++) {
458
const gx_device_halftone_resource_t *const * pphtr = phtrp[i]();
459
const gx_device_halftone_resource_t * phtr;
461
while ((phtr = *pphtr++) != 0) {
463
* This test does not check for strict equality of the order,
464
* nor is strict equality necessary. The ROM data will replace
465
* just the levels and bit_data arrays of the transmitted
466
* order, so only these must be the same. We don't even care
467
* if the ROM's levels and bit_data arrays are larger; we
468
* will never check values beyond the range required by the
471
if ( phtr->num_levels * sizeof(phtr->levels[0]) >= levels_size &&
472
phtr->Width * phtr->Height * phtr->elt_size >= bits_size &&
473
memcmp(phtr->levels, new_order.levels, levels_size) == 0 &&
474
memcmp(phtr->bit_data, new_order.bit_data, bits_size) == 0 ) {
475
/* the casts below are required to discard const qualifiers */
476
gs_free_object(mem, new_order.bit_data, "gx_ht_read_component");
477
new_order.bit_data = (void *)phtr->bit_data;
478
gs_free_object(mem, new_order.levels, "gx_ht_read_component");
479
new_order.levels = (uint *)phtr->levels;
486
/* everything OK, save the order and return the # of bytes read */
487
pcomp->corder = new_order;
494
* Serialize a halftone. The essential step is the serialization of the
495
* halftone orders; beyond this only the halftone type must be
500
* 0, with *psize set the the amount of space required, if successful
502
* gs_error_rangecheck, with *psize set to the size required, if the
503
* original *psize was not large enough
505
* some other error code, with *psize unchange, in the event of an
506
* error other than lack of space
510
const gx_device_halftone * pdht,
511
const gx_device * dev,
515
int num_dev_comps = pdht->num_dev_comp;
517
uint req_size = 2, used_size = 2;
518
/* 1 for halftone type, 1 for num_dev_comps */
521
* With the introduction of color models, there should never be a
522
* halftone that includes just one component. Enforce this
523
* restriction, even though it is not present in much of the rest
526
* NB: the pdht->order field is ignored by this code.
528
if (pdht == 0 || pdht->components == 0)
529
return_error(gs_error_unregistered); /* Must not happen. */
532
* The following fields do not need to be transmitted:
534
* order Ignored by this code (see above).
536
* rc, id Recreated by the allocation code on the renderer.
538
* lcm_width, Can be recreated by the de-serialization code on the
539
* lcm_height the renderer. Since halftones are transmitted
540
* infrequently (for normal jobs), the time required
541
* for re-calculation is not significant.
543
* Hence, the only fields that must be serialized are the type,and
544
* the number of components. (The number of components for the halftone
545
* may not match the device's if we are compositing with a process color
546
* model which does not match the output device.
548
* Several halftone components may be identical, but there is
549
* currently no simple way to determine this. Halftones are normally
550
* transmitted only once per page, so it is not clear that use of
551
* such information would significantly reduce command list size.
554
/* calculate the required data space */
555
for ( i = 0, code = gs_error_rangecheck;
556
i < num_dev_comps && code == gs_error_rangecheck;
561
if (i != pdht->components[i].comp_number)
562
return_error(gs_error_unregistered); /* Must not happen. */
564
code = gx_ht_write_component( &pdht->components[i],
567
req_size += tmp_size;
569
if (code < 0 && code != gs_error_rangecheck)
571
else if (*psize < req_size) {
577
/* the halftone type is known to fit in a byte */
578
*data++ = (byte)pdht->type;
579
/* the number of components is known to fit in a byte */
580
*data++ = (byte)num_dev_comps;
582
/* serialize the halftone components */
583
for (i = 0, code = 0; i < num_dev_comps && code == 0; i++) {
584
uint tmp_size = req_size - used_size;
586
code = gx_ht_write_component( &pdht->components[i],
589
used_size += tmp_size;
594
if (code == gs_error_rangecheck)
595
code = gs_error_unknownerror;
604
* Reconstruct a halftone from its serial representation, and install it
605
* as the current halftone. The buffer provided is expected to be large
606
* enough to hold the entire halftone.
608
* The reading and installation phases are combined in this routine so as
609
* to avoid unnecessarily allocating a device halftone and its component
610
* array, just to release them immediately after installation is complete.
611
* There is also not much reason to reconstuct a halftone except to make
612
* it the current halftone.
614
* Returns the number of bytes read, or <0 in the event of an error.
617
gx_ht_read_and_install(
618
gs_imager_state * pis,
619
const gx_device * dev,
624
gx_ht_order_component components[GX_DEVICE_COLOR_MAX_COMPONENTS];
625
gx_ht_order_component components_save[GX_DEVICE_COLOR_MAX_COMPONENTS];
626
const byte * data0 = data;
627
gx_device_halftone dht;
631
/* fill in some fixed fields */
632
memset(&dht.order, 0, sizeof(dht.order));
633
memset(&dht.rc, 0, sizeof(dht.rc));
634
dht.id = gs_no_id; /* updated during installation */
635
dht.components = components;
636
dht.lcm_width = 1; /* recalculated during installation */
639
/* clear pointers in the components array in case we need to abort */
640
memset(components, 0, sizeof(components));
642
/* get the halftone type */
644
return_error(gs_error_rangecheck);
645
dht.type = (gs_halftone_type)(*data++);
646
num_dev_comps = dht.num_dev_comp = dht.num_comp = *data++;
649
/* process the component orders */
650
for (i = 0, code = 0; i < num_dev_comps && code >= 0; i++) {
651
components[i].comp_number = i;
652
code = gx_ht_read_component(&components[i], data, size, mem);
659
/* if everything is OK, install the halftone */
661
/* save since the 'install' copies the order, but then clears the source order */
662
for (i = 0; i < num_dev_comps; i++)
663
components_save[i] = components[i];
664
code = gx_imager_dev_ht_install(pis, &dht, dht.type, dev);
665
for (i = 0; i < num_dev_comps; i++)
666
gx_ht_order_release(&components_save[i].corder, mem, false);
670
* If installation failed, discard the allocated elements. We can't
671
* use the gx_device_halftone_release procedure, as the components
672
* array is on the stack rather than in the heap.
675
for (i = 0; i < num_dev_comps; i++)
676
gx_ht_order_release(&components[i].corder, mem, false);
679
return code < 0 ? code : data - data0;