File: gdevpdfk.c

package info (click to toggle)
ghostscript 8.71~dfsg2-9%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 79,924 kB
  • ctags: 80,691
  • sloc: ansic: 501,432; sh: 25,689; python: 4,853; cpp: 3,633; perl: 3,597; tcl: 1,480; makefile: 1,187; lisp: 407; asm: 284; xml: 263; awk: 66; csh: 17; yacc: 15
file content (757 lines) | stat: -rw-r--r-- 23,364 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
/* Copyright (C) 2001-2006 Artifex Software, Inc.
   All Rights Reserved.
  
   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied, modified
   or distributed except as expressly authorized under the terms of that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/

/* $Id: gdevpdfk.c 9620 2009-04-07 16:07:22Z ken $ */
/* Lab and ICCBased color space writing */
#include "math_.h"
#include "memory_.h"
#include "gx.h"
#include "gxcspace.h"
#include "stream.h"
#include "gsicc.h"
#include "gserrors.h"
#include "gxcie.h"
#include "gdevpdfx.h"
#include "gdevpdfg.h"
#include "gdevpdfc.h"
#include "gdevpdfo.h"
#include "strimpl.h"

/* ------ CIE space synthesis ------ */

/* Add a /Range entry to a CIE-based color space dictionary. */
static int
pdf_cie_add_ranges(cos_dict_t *pcd, const gs_range *prange, int n, bool clamp)
{
    cos_array_t *pca = cos_array_alloc(pcd->pdev, "pdf_cie_add_ranges");
    int code = 0, i;

    if (pca == 0)
	return_error(gs_error_VMerror);
    for (i = 0; i < n; ++i) {
	floatp rmin = prange[i].rmin, rmax = prange[i].rmax;

	if (clamp) {
	    if (rmin < 0) rmin = 0;
	    if (rmax > 1) rmax = 1;
	}
	if ((code = cos_array_add_real(pca, rmin)) < 0 ||
	    (code = cos_array_add_real(pca, rmax)) < 0
	    )
	    break;
    }
    if (code >= 0)
	code = cos_dict_put_c_key_object(pcd, "/Range", COS_OBJECT(pca));
    if (code < 0)
	COS_FREE(pca, "pdf_cie_add_ranges");
    return code;
}

/* Transform a CIEBased color to XYZ. */
static int
cie_to_xyz(const double *in, double out[3], const gs_color_space *pcs,
	   const gs_imager_state *pis)
{
    gs_client_color cc;
    frac xyz[3];
    int ncomp = gs_color_space_num_components(pcs);
    int i;

    for (i = 0; i < ncomp; ++i)
	cc.paint.values[i] = in[i];
    cs_concretize_color(&cc, pcs, xyz, pis);
    out[0] = frac2float(xyz[0]);
    out[1] = frac2float(xyz[1]);
    out[2] = frac2float(xyz[2]);
    return 0;
}

/* ------ Lab space writing and synthesis ------ */

/* Transform XYZ values to Lab. */
static double
lab_g_inverse(double v)
{
    if (v >= (6.0 * 6.0 * 6.0) / (29 * 29 * 29))
	return pow(v, 1.0 / 3);	/* use cbrt if available? */
    else
	return (v * (841.0 / 108) + 4.0 / 29);
}
static void
xyz_to_lab(const double xyz[3], double lab[3], const gs_cie_common *pciec)
{
    const gs_vector3 *const pWhitePoint = &pciec->points.WhitePoint;
    double L, lunit;

    /* Calculate L* first. */
    L = lab_g_inverse(xyz[1] / pWhitePoint->v) * 116 - 16;
    /* Clamp L* to the PDF range [0..100]. */
    if (L < 0)
	L = 0;
    else if (L > 100)
	L = 100;
    lab[1] = L;
    lunit = (L + 16) / 116;

    /* Calculate a* and b*. */
    lab[0] = (lab_g_inverse(xyz[0] / pWhitePoint->u) - lunit) * 500;
    lab[2] = (lab_g_inverse(xyz[2] / pWhitePoint->w) - lunit) * -200;
}

/* Create a PDF Lab color space corresponding to a CIEBased color space. */
static int
lab_range(gs_range range_out[3] /* only [1] and [2] used */,
	  const gs_color_space *pcs, const gs_cie_common *pciec,
	  const gs_range *ranges, gs_memory_t *mem)
{
    /*
     * Determine the range of a* and b* by evaluating the color space
     * mapping at all of its extrema.
     */
    int ncomp = gs_color_space_num_components(pcs);
    gs_imager_state *pis;
    int code = gx_cie_to_xyz_alloc(&pis, pcs, mem);
    int i, j;

    if (code < 0)
	return code;
    for (j = 1; j < 3; ++j)
	range_out[j].rmin = 1000.0, range_out[j].rmax = -1000.0;
    for (i = 0; i < 1 << ncomp; ++i) {
	double in[4], xyz[3];

	for (j = 0; j < ncomp; ++j)
	    in[j] = (i & (1 << j) ? ranges[j].rmax : ranges[j].rmin);
	if (cie_to_xyz(in, xyz, pcs, pis) >= 0) {
	    double lab[3];

	    xyz_to_lab(xyz, lab, pciec);
	    for (j = 1; j < 3; ++j) {
		range_out[j].rmin = min(range_out[j].rmin, lab[j]);
		range_out[j].rmax = max(range_out[j].rmax, lab[j]);
	    }
	}
    }
    gx_cie_to_xyz_free(pis);
    return 0;
}
/*
 * Create a Lab color space object.
 * This procedure is exported for Lab color spaces in gdevpdfc.c.
 */
int
pdf_put_lab_color_space(cos_array_t *pca, cos_dict_t *pcd,
			const gs_range ranges[3] /* only [1] and [2] used */)
{
    int code;
    cos_value_t v;

    if ((code = cos_array_add(pca, cos_c_string_value(&v, "/Lab"))) >= 0)
	code = pdf_cie_add_ranges(pcd, ranges + 1, 2, false);
    return code;
}

/*
 * Create a Lab color space for a CIEBased space that can't be represented
 * directly as a Calxxx or Lab space.
 */
static int
pdf_convert_cie_to_lab(gx_device_pdf *pdev, cos_array_t *pca,
		       const gs_color_space *pcs,
		       const gs_cie_common *pciec, const gs_range *prange)
{
    cos_dict_t *pcd;
    gs_range ranges[3];
    int code;

    /****** NOT IMPLEMENTED YET, REQUIRES TRANSFORMING VALUES ******/
    if (1) return_error(gs_error_rangecheck);
    pcd = cos_dict_alloc(pdev, "pdf_convert_cie_to_lab(dict)");
    if (pcd == 0)
	return_error(gs_error_VMerror);
    if ((code = lab_range(ranges, pcs, pciec, prange, pdev->pdf_memory)) < 0 ||
	(code = pdf_put_lab_color_space(pca, pcd, ranges)) < 0 ||
	(code = pdf_finish_cie_space(pca, pcd, pciec)) < 0
	)
	COS_FREE(pcd, "pdf_convert_cie_to_lab(dict)");
    return code;
}

/* ------ ICCBased space writing and synthesis ------ */

/*
 * Create an ICCBased color space object (internal).  The client must write
 * the profile data on *ppcstrm.
 */
static int
pdf_make_iccbased(gx_device_pdf *pdev, cos_array_t *pca, int ncomps,
		  const gs_range *prange /*[4]*/,
		  const gs_color_space *pcs_alt,
		  cos_stream_t **ppcstrm,
		  const gs_range_t **pprange /* if scaling is needed */)

{
    cos_value_t v;
    int code;
    cos_stream_t * pcstrm = 0;
    cos_array_t * prngca = 0;
    bool std_ranges = true;
    bool scale_inputs = false;
    int i;

    /* Check the ranges. */
    if (pprange)
	*pprange = 0;
    for (i = 0; i < ncomps; ++i) {
	double rmin = prange[i].rmin, rmax = prange[i].rmax;

	if (rmin < 0.0 || rmax > 1.0) {
	    /* We'll have to scale the inputs.  :-( */
	    if (pprange == 0)
		return_error(gs_error_rangecheck); /* scaling not allowed */
	    *pprange = prange;
	    scale_inputs = true;
	}
	else if (rmin > 0.0 || rmax < 1.0)
	    std_ranges = false;
    }

    /* ICCBased color spaces are essentially copied to the output. */
    if ((code = cos_array_add(pca, cos_c_string_value(&v, "/ICCBased"))) < 0)
	return code;

    /* Create a stream for the output. */
    if ((pcstrm = cos_stream_alloc(pdev, "pdf_make_iccbased(stream)")) == 0) {
	code = gs_note_error(gs_error_VMerror);
	goto fail;
    }

    /* Indicate the number of components. */
    code = cos_dict_put_c_key_int(cos_stream_dict(pcstrm), "/N", ncomps);
    if (code < 0)
	goto fail;

    /* Indicate the range, if needed. */
    if (!std_ranges && !scale_inputs) {
	code = pdf_cie_add_ranges(cos_stream_dict(pcstrm), prange, ncomps, true);
	if (code < 0)
	    goto fail;
    }

    /* Output the alternate color space, if necessary. */
    switch (gs_color_space_get_index(pcs_alt)) {
    case gs_color_space_index_DeviceGray:
    case gs_color_space_index_DeviceRGB:
    case gs_color_space_index_DeviceCMYK:
	break;			/* implicit (default) */
    default:
	if ((code = pdf_color_space_named(pdev, &v, NULL, pcs_alt,
				    &pdf_color_space_names, false, NULL, 0)) < 0 ||
	    (code = cos_dict_put_c_key(cos_stream_dict(pcstrm), "/Alternate",
				       &v)) < 0
	    )
	    goto fail;
    }

    /* Wrap up. */
    if ((code = cos_array_add_object(pca, COS_OBJECT(pcstrm))) < 0)
	goto fail;
    *ppcstrm = pcstrm;
    return code;
 fail:
    if (prngca)
	COS_FREE(prngca, "pdf_make_iccbased(Range)");
    if (pcstrm)
	COS_FREE(pcstrm, "pdf_make_iccbased(stream)");
    return code;
}
/*
 * Finish writing the data stream for an ICCBased color space object.
 */
static int
pdf_finish_iccbased(cos_stream_t *pcstrm)
{
    /*
     * The stream must be an indirect object.  Assign an ID, and write the
     * object out now.
     */
    gx_device_pdf *pdev = pcstrm->pdev;

    pcstrm->id = pdf_obj_ref(pdev);
    return cos_write_object(COS_OBJECT(pcstrm), pdev);
}

/*
 * Create an ICCBased color space for a CIEBased space that can't be
 * represented directly as a Calxxx or Lab space.
 */

typedef struct profile_table_s profile_table_t;
struct profile_table_s {
    const char *tag;
    const byte *data;
    uint length;
    uint data_length;		/* may be < length if write != 0 */
    int (*write)(cos_stream_t *, const profile_table_t *, gs_memory_t *);
    const void *write_data;
    const gs_range_t *ranges;
};
static profile_table_t *
add_table(profile_table_t **ppnt, const char *tag, const byte *data,
	  uint length)
{
    profile_table_t *pnt = (*ppnt)++;

    pnt->tag = tag, pnt->data = data, pnt->length = length;
    pnt->data_length = length;
    pnt->write = NULL;
    /* write_data not set */
    pnt->ranges = NULL;
    return pnt;
}
static void
set_uint32(byte bytes[4], uint value)
{
    bytes[0] = (byte)(value >> 24);
    bytes[1] = (byte)(value >> 16);
    bytes[2] = (byte)(value >> 8);
    bytes[3] = (byte)value;
}
static void
set_XYZ(byte bytes[4], floatp value)
{
    set_uint32(bytes, (uint)(int)(value * 65536));
}
static void
add_table_xyz3(profile_table_t **ppnt, const char *tag, byte bytes[20],
	       const gs_vector3 *pv)
{
    memcpy(bytes, "XYZ \000\000\000\000", 8);
    set_XYZ(bytes + 8, pv->u);
    set_XYZ(bytes + 12, pv->v);
    set_XYZ(bytes + 16, pv->w);
    DISCARD(add_table(ppnt, tag, bytes, 20));
}
static void
set_sample16(byte *p, floatp v)
{
    int value = (int)(v * 65535);

    if (value < 0)
	value = 0;
    else if (value > 65535)
	value = 65535;
    p[0] = (byte)(value >> 8);
    p[1] = (byte)value;
}
/* Create and write a TRC curve table. */
static int write_trc_abc(cos_stream_t *, const profile_table_t *, gs_memory_t *);
static int write_trc_lmn(cos_stream_t *, const profile_table_t *, gs_memory_t *);
static profile_table_t *
add_trc(profile_table_t **ppnt, const char *tag, byte bytes[12],
	const gs_cie_common *pciec, cie_cache_one_step_t one_step)
{
    const int count = gx_cie_cache_size;
    profile_table_t *pnt;

    memcpy(bytes, "curv\000\000\000\000", 8);
    set_uint32(bytes + 8, count);
    pnt = add_table(ppnt, tag, bytes, 12);
    pnt->length += count * 2;
    pnt->write = (one_step == ONE_STEP_ABC ? write_trc_abc : write_trc_lmn);
    pnt->write_data = (const gs_cie_abc *)pciec;
    return pnt;
}
static int
rgb_to_index(const profile_table_t *pnt)
{
    switch (pnt->tag[0]) {
    case 'r': return 0;
    case 'g': return 1;
    case 'b': default: /* others can't happen */ return 2;
    }
}
static double
cache_arg(int i, int denom, const gs_range_t *range)
{
    double arg = i / (double)denom;

    if (range) {
	/* Sample over the range [range->rmin .. range->rmax]. */
	arg = arg * (range->rmax - range->rmin) + range->rmin;
    }
    return arg;
}

static int
write_trc_abc(cos_stream_t *pcstrm, const profile_table_t *pnt,
	      gs_memory_t *ignore_mem)
{
    /* Write the curve table from DecodeABC. */
    const gs_cie_abc *pabc = pnt->write_data;
    int ci = rgb_to_index(pnt);
    gs_cie_abc_proc proc = pabc->DecodeABC.procs[ci];
    byte samples[gx_cie_cache_size * 2];
    byte *p = samples;
    int i;

    for (i = 0; i < gx_cie_cache_size; ++i, p += 2)
	set_sample16(p, proc(cache_arg(i, gx_cie_cache_size - 1, pnt->ranges),
			     pabc));
    return cos_stream_add_bytes(pcstrm, samples, gx_cie_cache_size * 2);
}
static int
write_trc_lmn(cos_stream_t *pcstrm, const profile_table_t *pnt,
	      gs_memory_t *ignore_mem)
{
    const gs_cie_common *pciec = pnt->write_data;
    int ci = rgb_to_index(pnt);
    gs_cie_common_proc proc = pciec->DecodeLMN.procs[ci];
    byte samples[gx_cie_cache_size * 2];
    byte *p = samples;
    int i;

    /* Write the curve table from DecodeLMN. */
    for (i = 0; i < gx_cie_cache_size; ++i, p += 2)
	set_sample16(p, proc(cache_arg(i, gx_cie_cache_size - 1, pnt->ranges),
			     pciec));
    return cos_stream_add_bytes(pcstrm, samples, gx_cie_cache_size * 2);
}
/* Create and write an a2b0 lookup table. */
#define NUM_IN_ENTRIES 2	/* assume linear interpolation */
#define NUM_OUT_ENTRIES 2	/* ibid. */
#define MAX_CLUT_ENTRIES 2500	/* enough for 7^4 */
typedef struct icc_a2b0_s {
    byte header[52];
    const gs_color_space *pcs;
    int num_points;		/* on each axis of LUT */
    int count;			/* total # of entries in LUT */
} icc_a2b0_t;
static int write_a2b0(cos_stream_t *, const profile_table_t *, gs_memory_t *);
static profile_table_t *
add_a2b0(profile_table_t **ppnt, icc_a2b0_t *pa2b, int ncomps,
	 const gs_color_space *pcs)
{
    static const byte a2b0_data[sizeof(pa2b->header)] = {
	'm', 'f', 't', '2',		/* type signature */
	0, 0, 0, 0,			/* reserved, 0 */
	0,				/* # of input channels **VARIABLE** */
	3,				/* # of output channels */
	0,				/* # of CLUT points **VARIABLE** */
	0,				/* reserved, padding */
	0, 1, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0, /* matrix column 0 */
	0, 0, 0, 0,  0, 1, 0, 0,  0, 0, 0, 0, /* matrix column 1 */
	0, 0, 0, 0,  0, 0, 0, 0,  0, 1, 0, 0, /* matrix column 2 */
	0, NUM_IN_ENTRIES,		/* # of input table entries */
	0, NUM_OUT_ENTRIES		/* # of output table entries */
    };
    int num_points = (int)floor(pow(MAX_CLUT_ENTRIES, 1.0 / ncomps));
    profile_table_t *pnt;

    num_points = min(num_points, 255);
    memcpy(pa2b->header, a2b0_data, sizeof(a2b0_data));
    pa2b->header[8] = ncomps;
    pa2b->header[10] = num_points;
    pa2b->pcs = pcs;
    pa2b->num_points = num_points;
    pa2b->count = (int)pow(num_points, ncomps);
    pnt = add_table(ppnt, "A2B0", pa2b->header,
		    sizeof(pa2b->header) +
		    ncomps * 2 * NUM_IN_ENTRIES + /* in */
		    pa2b->count * (3 * 2) + /* clut: XYZ, 16-bit values */
		    3 * 2 * NUM_OUT_ENTRIES /* out */
		    );
    pnt->data_length = sizeof(pa2b->header); /* only write fixed part */
    pnt->write = write_a2b0;
    pnt->write_data = pa2b;
    return pnt;
}
static int
write_a2b0(cos_stream_t *pcstrm, const profile_table_t *pnt,
	   gs_memory_t *mem)
{
    const icc_a2b0_t *pa2b = pnt->write_data;
    const gs_color_space *pcs = pa2b->pcs;
    int ncomps = pa2b->header[8];
    int num_points = pa2b->num_points;
    int i;
#define MAX_NCOMPS 4		/* CIEBasedDEFG */
    static const byte v01[MAX_NCOMPS * 2 * 2] = {
	0,0, 255,255,   0,0, 255,255,   0,0, 255,255,   0,0, 255,255
    };
    gs_imager_state *pis;
    int code;

    /* Write the input table. */

    if ((code = cos_stream_add_bytes(pcstrm, v01, ncomps * 4)) < 0
	)
	return code;

    /* Write the lookup table. */

    code = gx_cie_to_xyz_alloc(&pis, pcs, mem);
    if (code < 0)
	return code;
    for (i = 0; i < pa2b->count; ++i) {
	double in[MAX_NCOMPS], xyz[3];
	byte entry[3 * 2];
	byte *p = entry;
	int n, j;

	for (n = i, j = ncomps - 1; j >= 0; --j, n /= num_points)
	    in[j] = cache_arg(n % num_points, num_points - 1,
			      (pnt->ranges ? pnt->ranges + j : NULL));
	cie_to_xyz(in, xyz, pcs, pis);
	/*
	 * NOTE: Due to an obscure provision of the ICC Profile
	 * specification, values in a2b0 lookup tables do *not* represent
	 * the range [0 .. 1], but rather the range [0
	 * .. MAX_ICC_XYZ_VALUE].  This caused us a lot of grief before we
	 * figured it out!
	 */
#define MAX_ICC_XYZ_VALUE (1 + 32767.0/32768)
	for (j = 0; j < 3; ++j, p += 2)
	    set_sample16(p, xyz[j] / MAX_ICC_XYZ_VALUE);
#undef MAX_ICC_XYZ_VALUE
	if ((code = cos_stream_add_bytes(pcstrm, entry, sizeof(entry))) < 0)
	    break;
    }
    gx_cie_to_xyz_free(pis);
    if (code < 0)
	return code;

    /* Write the output table. */

    return cos_stream_add_bytes(pcstrm, v01, 3 * 4);
}
static int
pdf_convert_cie_to_iccbased(gx_device_pdf *pdev, cos_array_t *pca,
			    const gs_color_space *pcs, const char *dcsname,
			    const gs_cie_common *pciec, const gs_range *prange,
			    cie_cache_one_step_t one_step,
			    const gs_matrix3 *pmat, const gs_range_t **pprange)
{
    /*
     * We have two options for creating an ICCBased color space to represent
     * a CIEBased space.  For CIEBasedABC spaces using only a single
     * Decode step followed by a single Matrix step, we can use [rgb]TRC
     * and [rgb]XYZ; for CIEBasedA spaces using only DecodeA, we could use
     * kTRC (but don't); otherwise, we must use a mft2 LUT.
     */
    int code;
    int ncomps = gs_color_space_num_components(pcs);
    gs_color_space *alt_space;
    cos_stream_t *pcstrm;

    /*
     * Even though Ghostscript includes icclib, icclib is unusable here,
     * because it requires random access to the output stream.
     * Instead, we construct the ICC profile by hand.
     */
    /* Header */
    byte header[128];
    static const byte header_data[] = {
	0, 0, 0, 0,			/* profile size **VARIABLE** */
	0, 0, 0, 0,			/* CMM type signature */
	0x02, 0x20, 0, 0,		/* profile version number */
	's', 'c', 'n', 'r',		/* profile class signature */
	0, 0, 0, 0,			/* data color space **VARIABLE** */
	'X', 'Y', 'Z', ' ',		/* connection color space */
	2002 / 256, 2002 % 256, 0, 1, 0, 1, /* date (1/1/2002) */
	0, 0, 0, 0, 0, 0,		/* time */
	'a', 'c', 's', 'p',		/* profile file signature */
	0, 0, 0, 0,			/* primary platform signature */
	0, 0, 0, 3,			/* profile flags (embedded use only) */
	0, 0, 0, 0, 0, 0, 0, 0,		/* device manufacturer */
	0, 0, 0, 0,			/* device model */
	0, 0, 0, 0, 0, 0, 0, 2		/* device attributes */
	/* Remaining fields are zero or variable. */
	/* [4] */			/* rendering intent */
	/* 3 * [4] */			/* illuminant */
    };
    /* Description */
#define DESC_LENGTH 5		/* "adhoc" */
    byte desc[12 + DESC_LENGTH + 1 + 11 + 67];
    static const byte desc_data[] = {
	'd', 'e', 's', 'c',		/* type signature */
	0, 0, 0, 0,			/* reserved, 0 */
	0, 0, 0, DESC_LENGTH + 1,	/* ASCII description length */
	'a', 'd', 'h', 'o', 'c', 0,	/* ASCII description */
	/* Remaining fields are zero. */
    };
    /* White point */
    byte wtpt[20];
    /* Copyright (useless, but required by icclib) */
    static const byte cprt_data[] = {
	't', 'e', 'x', 't',	/* type signature */
	0, 0, 0, 0,		/* reserved, 0 */
	'n', 'o', 'n', 'e', 0	/* must be null-terminated (!) */
    };
    /* Lookup table */
    icc_a2b0_t a2b0;
    /* [rgb]TRC */
    byte rTRC[12], gTRC[12], bTRC[12];
    /* [rgb]XYZ */
    byte rXYZ[20], gXYZ[20], bXYZ[20];
    /* Table structures */
#define MAX_NUM_TABLES 9	/* desc, [rgb]TRC, [rgb]xYZ, wtpt, cprt */
    profile_table_t tables[MAX_NUM_TABLES];
    profile_table_t *next_table = tables;

    pdf_cspace_init_Device(pdev->memory, &alt_space, ncomps);	/* can't fail */
    code = pdf_make_iccbased(pdev, pca, ncomps, prange, alt_space,
			     &pcstrm, pprange);
    rc_decrement(alt_space, "pdf_convert_cie_to_iccbased");
    if (code < 0)
	return code;

    /* Fill in most of the header, except for the total size. */

    memset(header, 0, sizeof(header));
    memcpy(header, header_data, sizeof(header_data));
    memcpy(header + 16, dcsname, 4);

    /* Construct the tables. */

    /* desc */
    memset(desc, 0, sizeof(desc));
    memcpy(desc, desc_data, sizeof(desc_data));
    DISCARD(add_table(&next_table, "desc", desc, sizeof(desc)));

    /* wtpt */
    add_table_xyz3(&next_table, "wtpt", wtpt, &pciec->points.WhitePoint);
    memcpy(header + 68, wtpt + 8, 12); /* illuminant = white point */

    /* cprt */
    /* (We have no use for this tag, but icclib requires it.) */
    DISCARD(add_table(&next_table, "cprt", cprt_data, sizeof(cprt_data)));

    /* Use TRC + XYZ if possible, otherwise AToB. */
    if ((one_step == ONE_STEP_ABC || one_step == ONE_STEP_LMN) && pmat != 0) {
	/* Use TRC + XYZ. */
	profile_table_t *tr =
	    add_trc(&next_table, "rTRC", rTRC, pciec, one_step);
	profile_table_t *tg =
	    add_trc(&next_table, "gTRC", gTRC, pciec, one_step);
	profile_table_t *tb =
	    add_trc(&next_table, "bTRC", bTRC, pciec, one_step);

	if (*pprange) {
	    tr->ranges = *pprange;
	    tg->ranges = *pprange + 1;
	    tb->ranges = *pprange + 2;
	}
	add_table_xyz3(&next_table, "rXYZ", rXYZ, &pmat->cu);
	add_table_xyz3(&next_table, "gXYZ", gXYZ, &pmat->cv);
	add_table_xyz3(&next_table, "bXYZ", bXYZ, &pmat->cw);
    } else {
	/* General case, use a lookup table. */
	/* AToB (mft2) */
	profile_table_t *pnt = add_a2b0(&next_table, &a2b0, ncomps, pcs);

	pnt->ranges = *pprange;
    }

    /* Write the profile. */
    {
	byte bytes[4 + MAX_NUM_TABLES * 12];
	int num_tables = next_table - tables;
	int i;
	byte *p;
	uint table_size = 4 + num_tables * 12;
	uint offset = sizeof(header) + table_size;

	set_uint32(bytes, next_table - tables);
	for (i = 0, p = bytes + 4; i < num_tables; ++i, p += 12) {
	    memcpy(p, tables[i].tag, 4);
	    set_uint32(p + 4, offset);
	    set_uint32(p + 8, tables[i].length);
	    offset += round_up(tables[i].length, 4);
	}
	set_uint32(header, offset);
	if ((code = cos_stream_add_bytes(pcstrm, header, sizeof(header))) < 0 ||
	    (code = cos_stream_add_bytes(pcstrm, bytes, table_size)) < 0
	    )
	    return code;
	for (i = 0; i < num_tables; ++i) {
	    uint len = tables[i].data_length;
	    static const byte pad[3] = {0, 0, 0};

	    if ((code = cos_stream_add_bytes(pcstrm, tables[i].data, len)) < 0 ||
		(tables[i].write != 0 &&
		 (code = tables[i].write(pcstrm, &tables[i], pdev->pdf_memory)) < 0) ||
		(code = cos_stream_add_bytes(pcstrm, pad, 
			-(int)(tables[i].length) & 3)) < 0
		)
		return code;
	}
    }

    return pdf_finish_iccbased(pcstrm);
}

/* ------ Entry points (from gdevpdfc.c) ------ */

/*
 * Create an ICCBased color space.  This is a single-use procedure,
 * broken out only for readability.
 */
int
pdf_iccbased_color_space(gx_device_pdf *pdev, cos_value_t *pvalue,
			 const gs_color_space *pcs, cos_array_t *pca)
{
    /*
     * This would arise only in a pdf ==> pdf translation, but we
     * should allow for it anyway.
     */
    const gs_icc_params * picc_params = &pcs->params.icc;
    const gs_cie_icc * picc_info = picc_params->picc_info;
    cos_stream_t * pcstrm;
    int code =
	pdf_make_iccbased(pdev, pca, picc_info->num_components,
			  picc_info->Range.ranges,
			  pcs->base_space,
			  &pcstrm, NULL);

    if (code < 0)
	return code;

    /* Transfer the profile stream. */
    code = cos_stream_add_stream_contents(pcstrm, picc_info->instrp);
    if (code >= 0)
	code = pdf_finish_iccbased(pcstrm);
    /*
     * The stream has been added to the array: in case of failure, the
     * caller will free the array, so there is no need to free the stream
     * explicitly here.
     */
    return code;
}

/* Convert a CIEBased space to Lab or ICCBased. */
int
pdf_convert_cie_space(gx_device_pdf *pdev, cos_array_t *pca,
		      const gs_color_space *pcs, const char *dcsname,
		      const gs_cie_common *pciec, const gs_range *prange,
		      cie_cache_one_step_t one_step, const gs_matrix3 *pmat,
		      const gs_range_t **pprange)
{
    return (pdev->CompatibilityLevel < 1.3 ?
	    /* PDF 1.2 or earlier, use a Lab space. */
	    pdf_convert_cie_to_lab(pdev, pca, pcs, pciec, prange) :
	    /* PDF 1.3 or later, use an ICCBased space. */
	    pdf_convert_cie_to_iccbased(pdev, pca, pcs, dcsname, pciec, prange,
					one_step, pmat, pprange)
	    );
}