~ubuntu-branches/ubuntu/jaunty/ghostscript/jaunty-updates

« back to all changes in this revision

Viewing changes to base/gxdhtserial.c

  • Committer: Bazaar Package Importer
  • Author(s): Till Kamppeter
  • Date: 2009-01-20 16:40:45 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090120164045-lnfhi0n30o5lwhwa
Tags: 8.64.dfsg.1~svn9377-0ubuntu1
* New upstream release (SVN rev 9377)
   o Fixes many bugs concerning PDF rendering, to make the PDF printing
     workflow correctly working.
   o Fixes long-standing bugs in many drivers, like input paper tray and
     duplex options not working for the built-in PCL 4, 5, 5c, 5e, and
     6/XL drivers, PDF input not working for bjc600, bjc800, and cups
     output devices, several options not working and uninitialized
     memory with cups output device.
   o Merged nearly all patches of the Ubuntu and Debian packages upstream.
   o Fixes LP: #317810, LP: #314439, LP: #314018.
* debian/patches/03_libpaper_support.dpatch,
  debian/patches/11_gs-cjk_font_glyph_handling_fix.dpatch,
  debian/patches/12_gs-cjk_vertical_writing_metrics_fix.dpatch,
  debian/patches/13_gs-cjk_cjkps_examples.dpatch,
  debian/patches/20_bbox_segv_fix.dpatch,
  debian/patches/21_brother_7x0_gdi_fix.dpatch,
  debian/patches/22_epsn_margin_workaround.dpatch,
  debian/patches/24_gs_man_fix.dpatch,
  debian/patches/25_toolbin_insecure_tmp_usage_fix.dpatch,
  debian/patches/26_assorted_script_fixes.dpatch,
  debian/patches/29_gs_css_fix.dpatch,
  debian/patches/30_ps2pdf_man_improvement.dpatch,
  debian/patches/31_fix-gc-sigbus.dpatch,
  debian/patches/34_ftbfs-on-hurd-fix.dpatch,
  debian/patches/35_disable_libcairo.dpatch,
  debian/patches/38_pxl-duplex.dpatch,
  debian/patches/39_pxl-resolution.dpatch,
  debian/patches/42_gs-init-ps-delaybind-fix.dpatch,
  debian/patches/45_bjc600-bjc800-pdf-input.dpatch,
  debian/patches/48_cups-output-device-pdf-duplex-uninitialized-memory-fix.dpatch,
  debian/patches/50_lips4-floating-point-exception.dpatch,
  debian/patches/52_cups-device-logging.dpatch,
  debian/patches/55_pcl-input-slot-fix.dpatch,
  debian/patches/57_pxl-input-slot-fix.dpatch,
  debian/patches/60_pxl-cups-driver-pdf.dpatch,
  debian/patches/62_onebitcmyk-pdf.dpatch,
  debian/patches/65_too-big-temp-files-1.dpatch,
  debian/patches/67_too-big-temp-files-2.dpatch,
  debian/patches/70_take-into-account-data-in-stream-buffer-before-refill.dpatch:
  Removed, applied upstream.
* debian/patches/01_docdir_fix_for_debian.dpatch,
  debian/patches/02_gs_man_fix_debian.dpatch,
  debian/patches/01_docdir-fix-for-debian.dpatch,
  debian/patches/02_docdir-fix-for-debian.dpatch: Renamed patches to
  make merging with Debian easier.
* debian/patches/32_improve-handling-of-media-size-changes-from-gv.dpatch, 
  debian/patches/33_bad-params-to-xinitimage-on-large-bitmaps.dpatch:
  regenerated for new source directory structure.
* debian/rules: Corrected paths to remove cidfmap (it is in Resource/Init/
  in GS 8.64) and to install headers (source paths are psi/ and base/ now).
* debian/rules: Remove all fontmaps, as DeFoMa replaces them.
* debian/local/pdftoraster/pdftoraster.c,
  debian/local/pdftoraster/pdftoraster.convs, debian/rules: Removed
  added pdftoraster filter and use the one which comes with Ghostscript.
* debian/ghostscript.links: s/8.63/8.64/

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
 
2
   All Rights Reserved.
 
3
  
 
4
   This software is provided AS-IS with no warranty, either express or
 
5
   implied.
 
6
 
 
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.
 
12
*/
 
13
 
 
14
/* $Id: gxdhtserial.c 8714 2008-05-08 18:56:10Z ray $ */
 
15
/* Serialization and de-serialization for (traditional) halftones */
 
16
 
 
17
#include "memory_.h"
 
18
#include "gx.h"
 
19
#include "gscdefs.h"
 
20
#include "gserrors.h"
 
21
#include "gsstruct.h"
 
22
#include "gsutil.h"             /* for gs_next_ids */
 
23
#include "gzstate.h"
 
24
#include "gxdevice.h"           /* for gzht.h */
 
25
#include "gzht.h"
 
26
#include "gswts.h"
 
27
#include "gxdhtres.h"
 
28
#include "gsserial.h"
 
29
#include "gxdhtserial.h"
 
30
 
 
31
 
 
32
/*
 
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.
 
36
 */
 
37
extern_gx_device_halftone_list();
 
38
 
 
39
 
 
40
/*
 
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
 
44
 * PostScript array).
 
45
 */
 
46
typedef enum {
 
47
    gx_ht_tf_none = 0,
 
48
    gx_ht_tf_identity,
 
49
    gx_ht_tf_complete
 
50
} gx_ht_tf_type_t;
 
51
 
 
52
/* enumeration to distinguish well-tempered screening orders from others */
 
53
typedef enum {
 
54
    gx_ht_traditional,
 
55
    gx_ht_wts
 
56
} gx_ht_order_type_t;
 
57
 
 
58
 
 
59
/*
 
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.
 
63
 *
 
64
 * Returns:
 
65
 *
 
66
 *    0, with *psize set the the amount of space required, if successful
 
67
 *
 
68
 *    gs_error_rangecheck, with *psize set to the size required, if the
 
69
 *        original *psize was not large enough
 
70
 */
 
71
static int
 
72
gx_ht_write_tf(
 
73
    const gx_transfer_map * pmap,
 
74
    byte *                  data,    
 
75
    uint *                  psize )
 
76
{
 
77
    int                     req_size = 1;   /* minimum of one byte */
 
78
 
 
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) {
 
83
        *psize = req_size;
 
84
        return gs_error_rangecheck;
 
85
    }
 
86
 
 
87
    if (req_size == 1)
 
88
        *data = (byte)(pmap == 0 ? gx_ht_tf_none : gx_ht_tf_identity);
 
89
    else {
 
90
        *data++ = (byte)gx_ht_tf_complete;
 
91
        memcpy(data, pmap->values, sizeof(pmap->values));
 
92
    }
 
93
 
 
94
    *psize = req_size;
 
95
    return 0;
 
96
}
 
97
 
 
98
/*
 
99
 * Reconstruct a transfer function from its serial representation. The
 
100
 * buffer provided is expected to be large enough to hold the entire
 
101
 * transfer function.
 
102
 *
 
103
 * Returns the number of bytes read, or < 0 in the event of an error.
 
104
 */
 
105
static int
 
106
gx_ht_read_tf(
 
107
    gx_transfer_map **  ppmap,
 
108
    const byte *        data,
 
109
    uint                size,
 
110
    gs_memory_t *       mem )
 
111
{
 
112
    gx_ht_tf_type_t     tf_type;
 
113
    gx_transfer_map *   pmap;
 
114
 
 
115
    /* read the type byte */
 
116
    if (size == 0)
 
117
        return_error(gs_error_rangecheck);
 
118
    --size;
 
119
    tf_type = (gx_ht_tf_type_t)*data++;
 
120
 
 
121
    /* if no transfer function, exit now */
 
122
    if (tf_type == gx_ht_tf_none) {
 
123
        *ppmap = 0;
 
124
        return 1;
 
125
    }
 
126
 
 
127
    /* allocate a transfer map */
 
128
    rc_alloc_struct_1( pmap,
 
129
                       gx_transfer_map,
 
130
                       &st_transfer_map,
 
131
                       mem,
 
132
                       return_error(gs_error_VMerror),
 
133
                       "gx_ht_read_tf" );
 
134
 
 
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);
 
140
        return 1;
 
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;
 
144
        *ppmap = pmap;
 
145
        return 1 + sizeof(pmap->values);
 
146
    } else {
 
147
        rc_decrement(pmap, "gx_ht_read_tf");
 
148
        return_error(gs_error_rangecheck);
 
149
    }
 
150
}
 
151
 
 
152
static int
 
153
gx_ht_write_component_wts(const wts_screen_t *wts, byte *data, uint *psize)
 
154
{
 
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;
 
159
 
 
160
    if (req_size > *psize) {
 
161
        *psize = req_size;
 
162
        return gs_error_rangecheck;
 
163
    }
 
164
 
 
165
    /* identify this as a wts halftone. */
 
166
    *data++ = (byte)gx_ht_wts;
 
167
 
 
168
    /* copy in wts header */
 
169
    memcpy(data, wts, hdr_size);
 
170
    ((wts_screen_t *)data)->samples = NULL;
 
171
    data += hdr_size;
 
172
 
 
173
    /* copy in treshold cell */
 
174
    memcpy(data, wts->samples, cell_size);
 
175
    *psize = req_size;
 
176
    return 0;
 
177
}
 
178
 
 
179
/*
 
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.
 
183
 *
 
184
 * Returns:
 
185
 *
 
186
 *    0, with *psize set the the amount of space required, if successful
 
187
 *
 
188
 *    gs_error_rangecheck, with *psize set to the size required, if the
 
189
 *        original *psize was not large enough
 
190
 *
 
191
 *    some other error code, with *psize unchanged, in the event of an
 
192
 *        error other than lack of space
 
193
 */
 
194
static int
 
195
gx_ht_write_component(
 
196
    const gx_ht_order_component *   pcomp,
 
197
    byte *                          data,
 
198
    uint *                          psize )
 
199
{
 
200
    const gx_ht_order *             porder = &pcomp->corder;
 
201
    byte *                          data0 = data;
 
202
    int                             code, levels_size, bits_size;
 
203
    uint                            tmp_size = 0;
 
204
    int                             req_size;
 
205
 
 
206
    /*
 
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).
 
209
     *
 
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).
 
214
     *
 
215
     * This leaves the order itself.
 
216
     *
 
217
     * Check if we are a well-tempered-screening order. Serialization of these
 
218
     * is handled in a separate function.
 
219
     */
 
220
    if (porder->wts != 0)
 
221
        return gx_ht_write_component_wts(porder->wts, data, psize);
 
222
 
 
223
    /*
 
224
     * The following order fields are not transmitted:
 
225
     *
 
226
     *  params          Only required during halftone cell construction
 
227
     *
 
228
     *  wse, wts        Only used for well-tempered screens (see above)
 
229
     *
 
230
     *  raster          Can be re-calculated by the renderer from the width
 
231
     *
 
232
     *  orig_height,    The only potential use for these parameters is in
 
233
     *  orig_shift      this routine; they are not useful to the renderer.
 
234
     *
 
235
     *  full_height     Can be re-calculated by the renderer from the
 
236
     *                  height, width, and shift values.
 
237
     *
 
238
     *  data_memory     Must be provided by the renderer.
 
239
     *
 
240
     *  cache           Must be provided by the renderer.
 
241
     *
 
242
     *  screen_params   Ony required during halftone cell construction
 
243
     *
 
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.
 
247
     *
 
248
     * Calculate the size required.
 
249
     */
 
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 */
 
259
               + levels_size
 
260
               + bits_size;
 
261
    code = gx_ht_write_tf(porder->transfer, data, &tmp_size);
 
262
    if (code < 0 && code != gs_error_rangecheck)
 
263
        return code;
 
264
    req_size += tmp_size;
 
265
    if (req_size > *psize) {
 
266
        *psize = req_size;
 
267
        return gs_error_rangecheck;
 
268
    }
 
269
 
 
270
    /* identify this as a traditional halftone */
 
271
    *data++ = (byte)gx_ht_traditional;
 
272
 
 
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);
 
279
 
 
280
    /* white out the procs index */
 
281
    *data++ = porder->procs - ht_order_procs_table;
 
282
 
 
283
    /* copy the levels array and whitening order array */
 
284
    memcpy(data, porder->levels, levels_size);
 
285
    data += levels_size;
 
286
    memcpy(data, porder->bit_data, bits_size);
 
287
    data += bits_size;
 
288
 
 
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);
 
293
    return code;
 
294
}
 
295
 
 
296
static int
 
297
gx_ht_read_component_wts(gx_ht_order_component *pcomp,
 
298
                         const byte *data, uint size,
 
299
                         gs_memory_t *mem)
 
300
{
 
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;
 
306
 
 
307
    memset(&pcomp->corder, 0, sizeof(pcomp->corder));
 
308
 
 
309
    if (size < bufsize)
 
310
        return -1;
 
311
    pcomp->corder.wts = gs_wts_from_buf(data, bufsize);
 
312
    pcomp->cname = 0;
 
313
    if (pcomp->corder.wts == NULL)
 
314
        return -1;
 
315
 
 
316
    return bufsize;
 
317
}
 
318
 
 
319
/*
 
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.
 
323
 *
 
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.
 
330
 *
 
331
 * Returns the number of bytes read, or < 0 in the event of an error.
 
332
 */
 
333
static int
 
334
gx_ht_read_component(
 
335
    gx_ht_order_component * pcomp,
 
336
    const byte *            data,
 
337
    uint                    size,
 
338
    gs_memory_t *           mem )
 
339
{
 
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;
 
346
 
 
347
    /* check the order type */
 
348
    if (size == 0)
 
349
        return_error(gs_error_rangecheck);
 
350
    --size;
 
351
    order_type = (gx_ht_order_type_t)*data++;
 
352
 
 
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);
 
356
 
 
357
    /*
 
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.
 
365
     */
 
366
    if (size < 7)
 
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++];
 
376
 
 
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;
 
380
 
 
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);
 
384
 
 
385
    /*
 
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:
 
391
     *
 
392
     *    wse = 0,
 
393
     *    wts = 0,
 
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
 
405
     *    cache = 0
 
406
     *    transfer = 0
 
407
     *
 
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.
 
411
     *
 
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.
 
418
     */
 
419
    code = gx_ht_alloc_ht_order( &new_order,
 
420
                                 new_order.width,
 
421
                                 new_order.height,
 
422
                                 new_order.num_levels,
 
423
                                 new_order.num_bits,
 
424
                                 new_order.shift,
 
425
                                 new_order.procs,
 
426
                                 mem );
 
427
    if (code < 0)
 
428
        return code;
 
429
    memset(&new_order.params, 0, sizeof(new_order.params));
 
430
    memset(&new_order.screen_params, 0, sizeof(new_order.screen_params));
 
431
 
 
432
    /* fill in the levels and bit_data arrays */
 
433
    memcpy(new_order.levels, data, levels_size);
 
434
    data += levels_size;
 
435
    memcpy(new_order.bit_data, data, bits_size);
 
436
    data += bits_size;
 
437
 
 
438
    /* process the transfer function */
 
439
    code = gx_ht_read_tf(&new_order.transfer, data, data_lim - data, mem);
 
440
    if (code < 0) {
 
441
        gx_ht_order_release(&new_order, mem, false);
 
442
        return code;
 
443
    }
 
444
    data += code;
 
445
 
 
446
    /*
 
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.
 
453
     *
 
454
     * It is not clear which, if any or the currently used devices
 
455
     * provide a ROM-based halftone order set.
 
456
     */
 
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;
 
460
 
 
461
        while ((phtr = *pphtr++) != 0) {
 
462
            /*
 
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
 
469
             * current order.
 
470
             */
 
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;
 
480
                goto done;
 
481
            }
 
482
        }
 
483
    }
 
484
 
 
485
  done:
 
486
    /* everything OK, save the order and return the # of bytes read */
 
487
    pcomp->corder = new_order;
 
488
    pcomp->cname = 0;
 
489
    return data - data0;
 
490
}
 
491
 
 
492
 
 
493
/*
 
494
 * Serialize a halftone. The essential step is the serialization of the
 
495
 * halftone orders; beyond this only the halftone type must be
 
496
 * transmitted.
 
497
 *
 
498
 * Returns:
 
499
 *
 
500
 *    0, with *psize set the the amount of space required, if successful
 
501
 *
 
502
 *    gs_error_rangecheck, with *psize set to the size required, if the
 
503
 *        original *psize was not large enough
 
504
 *
 
505
 *    some other error code, with *psize unchange, in the event of an
 
506
 *        error other than lack of space
 
507
 */
 
508
int
 
509
gx_ht_write(
 
510
    const gx_device_halftone *  pdht,
 
511
    const gx_device *           dev,
 
512
    byte *                      data,
 
513
    uint *                      psize )
 
514
{
 
515
    int                         num_dev_comps = pdht->num_dev_comp;
 
516
    int                         i, code;
 
517
    uint                        req_size = 2, used_size = 2;
 
518
                                /* 1 for halftone type, 1 for num_dev_comps */
 
519
 
 
520
    /*
 
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
 
524
     * of the code.
 
525
     *
 
526
     * NB: the pdht->order field is ignored by this code.
 
527
     */
 
528
    if (pdht == 0 || pdht->components == 0)
 
529
        return_error(gs_error_unregistered); /* Must not happen. */
 
530
 
 
531
    /*
 
532
     * The following fields do not need to be transmitted:
 
533
     *
 
534
     *  order       Ignored by this code (see above).
 
535
     *
 
536
     *  rc, id      Recreated by the allocation code on the renderer.
 
537
     *
 
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.
 
542
     *
 
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.
 
547
     *
 
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.
 
552
     */
 
553
 
 
554
    /* calculate the required data space */
 
555
    for ( i = 0, code = gs_error_rangecheck;
 
556
          i < num_dev_comps && code == gs_error_rangecheck;
 
557
          i++) {
 
558
        uint     tmp_size = 0;
 
559
 
 
560
        /* sanity check */
 
561
        if (i != pdht->components[i].comp_number)
 
562
            return_error(gs_error_unregistered); /* Must not happen. */
 
563
 
 
564
        code = gx_ht_write_component( &pdht->components[i],
 
565
                                      data,
 
566
                                      &tmp_size );
 
567
        req_size += tmp_size;
 
568
    }
 
569
    if (code < 0 && code != gs_error_rangecheck)
 
570
        return code;
 
571
    else if (*psize < req_size) {
 
572
        *psize = req_size;
 
573
        return 0;
 
574
    }
 
575
    req_size = *psize;
 
576
 
 
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;
 
581
 
 
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;
 
585
 
 
586
        code = gx_ht_write_component( &pdht->components[i],
 
587
                                      data,
 
588
                                      &tmp_size );
 
589
        used_size += tmp_size;
 
590
        data += tmp_size;
 
591
    }
 
592
 
 
593
    if (code < 0) {
 
594
        if (code == gs_error_rangecheck)
 
595
            code = gs_error_unknownerror;
 
596
        return code;
 
597
    }
 
598
 
 
599
    *psize = used_size;
 
600
    return 0;
 
601
}
 
602
 
 
603
/*
 
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.
 
607
 *
 
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.
 
613
 *
 
614
 * Returns the number of bytes read, or <0 in the event of an error.
 
615
 */
 
616
int
 
617
gx_ht_read_and_install(
 
618
    gs_imager_state *       pis,
 
619
    const gx_device *       dev,
 
620
    const byte *            data,
 
621
    uint                    size,
 
622
    gs_memory_t *           mem )
 
623
{
 
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;
 
628
    int                     num_dev_comps;
 
629
    int                     i, code;
 
630
 
 
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 */
 
637
    dht.lcm_height = 1;
 
638
 
 
639
    /* clear pointers in the components array in case we need to abort */
 
640
    memset(components, 0, sizeof(components));
 
641
 
 
642
    /* get the halftone type */
 
643
    if (size < 2)
 
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++;
 
647
    size -= 2;
 
648
 
 
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);
 
653
        if (code >= 0) {
 
654
            size -= code;
 
655
            data += code;
 
656
        }
 
657
    }
 
658
 
 
659
    /* if everything is OK, install the halftone */
 
660
    if (code >= 0) {
 
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);
 
667
    }
 
668
 
 
669
    /*
 
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.
 
673
     */
 
674
    if (code < 0) {
 
675
        for (i = 0; i < num_dev_comps; i++)
 
676
            gx_ht_order_release(&components[i].corder, mem, false);
 
677
    }
 
678
 
 
679
    return code < 0 ? code : data - data0;
 
680
}