-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathraster-stream.c
1897 lines (1605 loc) · 53.3 KB
/
raster-stream.c
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Raster file routines for CUPS.
//
// Copyright © 2022 by OpenPrinting.
// Copyright © 2007-2019 by Apple Inc.
// Copyright © 1997-2006 by Easy Software Products.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
//
// Include necessary headers...
//
#include "raster-private.h"
//#include "debug-internal.h"
//
// Private structures...
//
typedef void (*_cups_copyfunc_t)(void *dst, const void *src, size_t bytes);
//
// Local globals...
//
static const char * const apple_media_types[] =
{ // media-type values for Apple Raster
"auto",
"stationery",
"transparency",
"envelope",
"cardstock",
"labels",
"stationery-letterhead",
"disc",
"photographic-matte",
"photographic-satin",
"photographic-semi-gloss",
"photographic-glossy",
"photographic-high-gloss",
"other"
};
#ifdef DEBUG
static const char * const cups_modes[] =
{ // Open modes
"CUPS_RASTER_READ",
"CUPS_RASTER_WRITE",
"CUPS_RASTER_WRITE_COMPRESSED",
"CUPS_RASTER_WRITE_PWG",
"CUPS_RASTER_WRITE_APPLE"
};
#endif // DEBUG
//
// Local functions...
//
static size_t cupsCopyString(char *dst, const char *src, size_t dstsize);
static ssize_t cups_raster_io(cups_raster_t *r, unsigned char *buf, size_t bytes);
static ssize_t cups_raster_read(cups_raster_t *r, unsigned char *buf, size_t bytes);
static int cups_raster_update(cups_raster_t *r);
static ssize_t cups_raster_write(cups_raster_t *r, const unsigned char *pixels);
static ssize_t cups_read_fd(void *ctx, unsigned char *buf, size_t bytes);
static void cups_swap(unsigned char *buf, size_t bytes);
static void cups_swap_copy(unsigned char *dst, const unsigned char *src, size_t bytes);
static ssize_t cups_write_fd(void *ctx, unsigned char *buf, size_t bytes);
//
// 'cupsCopyString()' - Safely copy a string.
//
static size_t // O - Size of string
cupsCopyString(char *dst, // I - Destination string buffer
const char *src, // I - Source string
size_t dstsize) // I - Size of destination string buffer
{
# ifdef __APPLE__
return (strlcpy(dst, src, dstsize));
# else
size_t srclen; // Length of source string
// Figure out how much room is needed...
dstsize --;
srclen = strlen(src);
// Copy the appropriate amount...
if (srclen > dstsize)
srclen = dstsize;
memmove(dst, src, srclen);
dst[srclen] = '\0';
return (srclen);
# endif // __APPLE__
}
//
// '_cupsRasterColorSpaceString()' - Return the colorspace name for a
// cupsColorSpace value.
//
const char *
_cupsRasterColorSpaceString(
cups_cspace_t cspace) // I - cupsColorSpace value
{
static const char * const cups_color_spaces[] =
{ // Color spaces
"W",
"RGB",
"RGBA",
"K",
"CMY",
"YMC",
"CMYK",
"YMCK",
"KCMY",
"KCMYcm",
"GMCK",
"GMCS",
"WHITE",
"GOLD",
"SILVER",
"CIEXYZ",
"CIELab",
"RGBW",
"SW",
"SRGB",
"ADOBERGB",
"21",
"22",
"23",
"24",
"25",
"26",
"27",
"28",
"29",
"30",
"31",
"ICC1",
"ICC2",
"ICC3",
"ICC4",
"ICC5",
"ICC6",
"ICC7",
"ICC8",
"ICC9",
"ICCA",
"ICCB",
"ICCC",
"ICCD",
"ICCE",
"ICCF",
"47",
"DEVICE1",
"DEVICE2",
"DEVICE3",
"DEVICE4",
"DEVICE5",
"DEVICE6",
"DEVICE7",
"DEVICE8",
"DEVICE9",
"DEVICEA",
"DEVICEB",
"DEVICEC",
"DEVICED",
"DEVICEE",
"DEVICEF"
};
if (cspace < CUPS_CSPACE_W || cspace > CUPS_CSPACE_DEVICEF)
return ("Unknown");
else
return (cups_color_spaces[cspace]);
}
//
// 'cupsRasterClose()' - Close a raster stream.
//
// The file descriptor associated with the raster stream must be closed
// separately as needed.
//
void
cupsRasterClose(cups_raster_t *r) // I - Stream to free
{
if (r != NULL)
{
free(r->buffer);
free(r->pixels);
free(r);
}
}
#if 0
//
// 'cupsRasterInitHeader()' - Initialize a page header for PWG Raster output.
//
// The "media" argument specifies the media to use. The "optimize", "quality",
// "intent", "orientation", and "sides" arguments specify additional IPP Job
// Template attribute values that are reflected in the raster header.
//
// The "type" argument specifies a "pwg-raster-document-type-supported" value
// that controls the color space and bit depth of the raster data. Supported
// values include:
//
// - "adobe-rgb_8": 8-bit per component (24-bit) AdobeRGB
// - "adobe-rgb_16": 16-bit per component (48-bit) AdobeRGB
// - "black_1": 1-bit black (K)
// - "black_8": 8-bit black (K)
// - "black_16": 16-bit black (K)
// - "cmyk_8": 8-bit per component (32-bit) CMYK
// - "cmyk_16": 16-bit per component (64-bit) CMYK
// - "device1_8" to "device15_8": 8-bit per component DeviceN
// - "device1_16" to "device15_16": 16-bit per component DeviceN
// - "rgb_8": 8-bit per component (24-bit) DeviceRGB
// - "rgb_16": 16-bit per component (32-bit) DeviceRGB
// - "sgray_1": 1-bit sGray
// - "sgray_8": 8-bit sGray
// - "sgray_16": 16-bit sGray
// - "srgb_8": 8-bit per component (24-bit) sRGB
// - "srgb_16": 16-bit per component (48-bit) sRGB
//
// The "xres" and "yres" arguments specify the raster resolution in dots per
// inch.
//
// The "sheet_back" argument specifies a "pwg-raster-document-sheet-back" value
// to apply for the back side of a page. Pass `NULL` for the front side.
//
bool // O - `true` on success, `false` on failure
cupsRasterInitHeader(
cups_page_header_t *h, // I - Page header
cups_size_t *media, // I - Media information
const char *optimize, // I - IPP "print-content-optimize" value
ipp_quality_t quality, // I - IPP "print-quality" value
const char *intent, // I - IPP "print-rendering-intent" value
ipp_orient_t orientation, // I - IPP "orientation-requested" value
const char *sides, // I - IPP "sides" value
const char *type, // I - PWG raster type string
int xdpi, // I - Cross-feed direction (horizontal) resolution
int ydpi, // I - Feed direction (vertical) resolution
const char *sheet_back) // I - Transform for back side or `NULL` for none
{
unsigned i; // Looping var
static const char * const media_positions[] =
{ // "media-source" to MediaPosition mapping
"auto",
"main",
"alternate",
"large-capacity",
"manual",
"envelope",
"disc",
"photo",
"hagaki",
"main-roll",
"alternate-roll",
"top",
"middle",
"bottom",
"side",
"left",
"right",
"center",
"rear",
"by-pass-tray",
"tray-1",
"tray-2",
"tray-3",
"tray-4",
"tray-5",
"tray-6",
"tray-7",
"tray-8",
"tray-9",
"tray-10",
"tray-11",
"tray-12",
"tray-13",
"tray-14",
"tray-15",
"tray-16",
"tray-17",
"tray-18",
"tray-19",
"tray-20",
"roll-1",
"roll-2",
"roll-3",
"roll-4",
"roll-5",
"roll-6",
"roll-7",
"roll-8",
"roll-9",
"roll-10"
};
if (!h || !media || !type || xdpi <= 0 || ydpi <= 0)
{
_cupsRasterAddError("%s", strerror(EINVAL));
return (false);
}
// Initialize the page header...
memset(h, 0, sizeof(cups_page_header_t));
cupsCopyString(h->MediaClass, "PwgRaster", sizeof(h->MediaClass));
cupsCopyString(h->MediaColor, media->color, sizeof(h->MediaColor));
cupsCopyString(h->MediaType, media->type, sizeof(h->MediaType));
for (i = 0; i < (sizeof(media_positions) / sizeof(media_positions[0])); i ++)
{
if (!strcmp(media->source, media_positions[i]))
{
h->MediaPosition = i;
break;
}
}
if (optimize)
cupsCopyString(h->OutputType, optimize, sizeof(h->OutputType));
switch (orientation)
{
default :
h->Orientation = CUPS_ORIENT_0;
break;
case IPP_ORIENT_LANDSCAPE :
h->Orientation = CUPS_ORIENT_90;
break;
case IPP_ORIENT_REVERSE_PORTRAIT :
h->Orientation = CUPS_ORIENT_180;
break;
case IPP_ORIENT_REVERSE_LANDSCAPE :
h->Orientation = CUPS_ORIENT_270;
break;
}
cupsCopyString(h->cupsPageSizeName, media->media, sizeof(h->cupsPageSizeName));
if (intent)
cupsCopyString(h->cupsRenderingIntent, intent, sizeof(h->cupsRenderingIntent));
h->cupsInteger[CUPS_RASTER_PWG_PrintQuality] = (unsigned)quality;
h->PageSize[0] = (unsigned)(72 * media->width / 2540);
h->PageSize[1] = (unsigned)(72 * media->length / 2540);
// This never gets written but is needed for some applications
h->cupsPageSize[0] = 72.0f * media->width / 2540.0f;
h->cupsPageSize[1] = 72.0f * media->length / 2540.0f;
h->ImagingBoundingBox[2] = h->PageSize[0];
h->ImagingBoundingBox[3] = h->PageSize[1];
h->HWResolution[0] = (unsigned)xdpi;
h->HWResolution[1] = (unsigned)ydpi;
h->cupsWidth = (unsigned)(media->width * xdpi / 2540);
h->cupsHeight = (unsigned)(media->length * ydpi / 2540);
if (h->cupsWidth > 0x00ffffff || h->cupsHeight > 0x00ffffff)
{
_cupsRasterAddError("Raster dimensions too large.");
return (false);
}
h->cupsInteger[CUPS_RASTER_PWG_ImageBoxRight] = h->cupsWidth;
h->cupsInteger[CUPS_RASTER_PWG_ImageBoxBottom] = h->cupsHeight;
// Colorspace and bytes per line...
if (!strcmp(type, "adobe-rgb_8"))
{
h->cupsBitsPerColor = 8;
h->cupsBitsPerPixel = 24;
h->cupsColorSpace = CUPS_CSPACE_ADOBERGB;
}
else if (!strcmp(type, "adobe-rgb_16"))
{
h->cupsBitsPerColor = 16;
h->cupsBitsPerPixel = 48;
h->cupsColorSpace = CUPS_CSPACE_ADOBERGB;
}
else if (!strcmp(type, "black_1"))
{
h->cupsBitsPerColor = 1;
h->cupsBitsPerPixel = 1;
h->cupsColorSpace = CUPS_CSPACE_K;
}
else if (!strcmp(type, "black_8"))
{
h->cupsBitsPerColor = 8;
h->cupsBitsPerPixel = 8;
h->cupsColorSpace = CUPS_CSPACE_K;
}
else if (!strcmp(type, "black_16"))
{
h->cupsBitsPerColor = 16;
h->cupsBitsPerPixel = 16;
h->cupsColorSpace = CUPS_CSPACE_K;
}
else if (!strcmp(type, "cmyk_8"))
{
h->cupsBitsPerColor = 8;
h->cupsBitsPerPixel = 32;
h->cupsColorSpace = CUPS_CSPACE_CMYK;
}
else if (!strcmp(type, "cmyk_16"))
{
h->cupsBitsPerColor = 16;
h->cupsBitsPerPixel = 64;
h->cupsColorSpace = CUPS_CSPACE_CMYK;
}
else if (!strncmp(type, "device", 6) && type[6] >= '1' && type[6] <= '9')
{
int ncolors, bits; // Number of colors and bits
if (sscanf(type, "device%d_%d", &ncolors, &bits) != 2 || ncolors > 15 || (bits != 8 && bits != 16))
{
_cupsRasterAddError("Unsupported raster type \'%s\'.", type);
return (false);
}
h->cupsBitsPerColor = (unsigned)bits;
h->cupsBitsPerPixel = (unsigned)(ncolors * bits);
h->cupsColorSpace = (cups_cspace_t)(CUPS_CSPACE_DEVICE1 + ncolors - 1);
}
else if (!strcmp(type, "rgb_8"))
{
h->cupsBitsPerColor = 8;
h->cupsBitsPerPixel = 24;
h->cupsColorSpace = CUPS_CSPACE_RGB;
}
else if (!strcmp(type, "rgb_16"))
{
h->cupsBitsPerColor = 16;
h->cupsBitsPerPixel = 48;
h->cupsColorSpace = CUPS_CSPACE_RGB;
}
else if (!strcmp(type, "sgray_1"))
{
h->cupsBitsPerColor = 1;
h->cupsBitsPerPixel = 1;
h->cupsColorSpace = CUPS_CSPACE_SW;
}
else if (!strcmp(type, "sgray_8"))
{
h->cupsBitsPerColor = 8;
h->cupsBitsPerPixel = 8;
h->cupsColorSpace = CUPS_CSPACE_SW;
}
else if (!strcmp(type, "sgray_16"))
{
h->cupsBitsPerColor = 16;
h->cupsBitsPerPixel = 16;
h->cupsColorSpace = CUPS_CSPACE_SW;
}
else if (!strcmp(type, "srgb_8"))
{
h->cupsBitsPerColor = 8;
h->cupsBitsPerPixel = 24;
h->cupsColorSpace = CUPS_CSPACE_SRGB;
}
else if (!strcmp(type, "srgb_16"))
{
h->cupsBitsPerColor = 16;
h->cupsBitsPerPixel = 48;
h->cupsColorSpace = CUPS_CSPACE_SRGB;
}
else
{
_cupsRasterAddError("Unsupported raster type \'%s\'.", type);
return (false);
}
h->cupsColorOrder = CUPS_ORDER_CHUNKED;
h->cupsNumColors = h->cupsBitsPerPixel / h->cupsBitsPerColor;
h->cupsBytesPerLine = (h->cupsWidth * h->cupsBitsPerPixel + 7) / 8;
// Duplex support...
h->cupsInteger[CUPS_RASTER_PWG_CrossFeedTransform] = 1;
h->cupsInteger[CUPS_RASTER_PWG_FeedTransform] = 1;
if (sides)
{
if (!strcmp(sides, "two-sided-long-edge"))
{
h->Duplex = 1;
}
else if (!strcmp(sides, "two-sided-short-edge"))
{
h->Duplex = 1;
h->Tumble = 1;
}
else if (strcmp(sides, "one-sided"))
{
_cupsRasterAddError("Unsupported sides value '%s'.", sides);
return (false);
}
if (sheet_back)
{
if (!strcmp(sheet_back, "flipped"))
{
if (h->Tumble)
h->cupsInteger[CUPS_RASTER_PWG_CrossFeedTransform] = 0xffffffffU;
else
h->cupsInteger[CUPS_RASTER_PWG_FeedTransform] = 0xffffffffU;
}
else if ((!strcmp(sheet_back, "manual-tumble") && h->Tumble) || (!strcmp(sheet_back, "rotated") || !h->Tumble))
{
h->cupsInteger[CUPS_RASTER_PWG_CrossFeedTransform] = 0xffffffffU;
h->cupsInteger[CUPS_RASTER_PWG_FeedTransform] = 0xffffffffU;
}
else if (strcmp(sheet_back, "normal"))
{
_cupsRasterAddError("Unsupported sheet_back value '%s'.", sheet_back);
return (false);
}
}
}
return (true);
}
#endif // 0
//
// '_cupsRasterNew()' - Create a raster stream using a callback function.
//
// This function associates a raster stream with the given callback function and
// context pointer.
//
// When writing raster data, the @code CUPS_RASTER_WRITE@,
// @code CUPS_RASTER_WRITE_COMPRESS@, or @code CUPS_RASTER_WRITE_PWG@ mode can
// be used - compressed and PWG output is generally 25-50% smaller but adds a
// 100-300% execution time overhead.
//
cups_raster_t * // O - New stream
_cupsRasterNew(
cups_raster_cb_t iocb, // I - Read/write callback
void *ctx, // I - Context pointer for callback
cups_raster_mode_t mode) // I - Mode - `CUPS_RASTER_READ`, `CUPS_RASTER_WRITE`, `CUPS_RASTER_WRITE_COMPRESSED`, `CUPS_RASTER_WRITE_PWG`
{
cups_raster_t *r; // New stream
DEBUG_printf(("_cupsRasterNwq(iocb=%p, ctx=%p, mode=%s)", (void *)iocb, ctx, cups_modes[mode]));
_cupsRasterClearError();
if ((r = calloc(sizeof(cups_raster_t), 1)) == NULL)
{
_cupsRasterAddError("Unable to allocate memory for raster stream: %s", strerror(errno));
DEBUG_puts("1_cupsRasterNwq: Returning NULL.");
return (NULL);
}
r->ctx = ctx;
r->iocb = iocb;
r->mode = mode;
if (mode == CUPS_RASTER_READ)
{
// Open for read - get sync word...
if (cups_raster_io(r, (unsigned char *)&(r->sync), sizeof(r->sync)) != sizeof(r->sync))
{
_cupsRasterAddError("Unable to read header from raster stream: %s", strerror(errno));
free(r);
DEBUG_puts("1_cupsRasterNew: Unable to read header, returning NULL.");
return (NULL);
}
if (r->sync != CUPS_RASTER_SYNC && r->sync != CUPS_RASTER_REVSYNC && r->sync != CUPS_RASTER_SYNCv1 && r->sync != CUPS_RASTER_REVSYNCv1 && r->sync != CUPS_RASTER_SYNCv2 && r->sync != CUPS_RASTER_REVSYNCv2 && r->sync != CUPS_RASTER_SYNCapple && r->sync != CUPS_RASTER_REVSYNCapple)
{
_cupsRasterAddError("Unknown raster format %08x.", r->sync);
free(r);
DEBUG_puts("1_cupsRasterNew: Unknown format, returning NULL.");
return (NULL);
}
if (r->sync == CUPS_RASTER_SYNCv2 || r->sync == CUPS_RASTER_REVSYNCv2 || r->sync == CUPS_RASTER_SYNCapple || r->sync == CUPS_RASTER_REVSYNCapple)
r->compressed = 1;
DEBUG_printf(("1_cupsRasterNew: sync=%08x", r->sync));
if (r->sync == CUPS_RASTER_REVSYNC || r->sync == CUPS_RASTER_REVSYNCv1 || r->sync == CUPS_RASTER_REVSYNCv2 || r->sync == CUPS_RASTER_REVSYNCapple)
r->swapped = 1;
if (r->sync == CUPS_RASTER_SYNCapple || r->sync == CUPS_RASTER_REVSYNCapple)
{
unsigned char header[8]; // File header
if (cups_raster_io(r, (unsigned char *)header, sizeof(header)) !=
sizeof(header))
{
_cupsRasterAddError("Unable to read header from raster stream: %s", strerror(errno));
free(r);
DEBUG_puts("1_cupsRasterNew: Unable to read header, returning NULL.");
return (NULL);
}
}
#ifdef DEBUG
r->iostart = r->iocount;
#endif // DEBUG
}
else
{
// Open for write - put sync word...
switch (mode)
{
default :
case CUPS_RASTER_WRITE :
r->sync = CUPS_RASTER_SYNC;
break;
case CUPS_RASTER_WRITE_COMPRESSED :
r->compressed = 1;
r->sync = CUPS_RASTER_SYNCv2;
break;
case CUPS_RASTER_WRITE_PWG :
r->compressed = 1;
r->sync = htonl(CUPS_RASTER_SYNC_PWG);
r->swapped = r->sync != CUPS_RASTER_SYNC_PWG;
break;
case CUPS_RASTER_WRITE_APPLE :
r->compressed = 1;
r->sync = htonl(CUPS_RASTER_SYNCapple);
r->swapped = r->sync != CUPS_RASTER_SYNCapple;
r->apple_page_count = 0xffffffffU;
break;
}
if (cups_raster_io(r, (unsigned char *)&(r->sync), sizeof(r->sync)) < (ssize_t)sizeof(r->sync))
{
_cupsRasterAddError("Unable to write raster stream header: %s", strerror(errno));
free(r);
DEBUG_puts("1_cupsRasterNew: Unable to write header, returning NULL.");
return (NULL);
}
}
DEBUG_printf(("1_cupsRasterNew: compressed=%d, swapped=%d, returning %p", r->compressed, r->swapped, (void *)r));
return (r);
}
//
// 'cupsRasterOpen()' - Open a raster stream using a file descriptor.
//
// This function associates a raster stream with the given file descriptor.
// For most printer driver filters, "fd" will be 0 (stdin). For most raster
// image processor (RIP) filters that generate raster data, "fd" will be 1
// (stdout).
//
// When writing raster data, the @code CUPS_RASTER_WRITE@,
// @code CUPS_RASTER_WRITE_COMPRESS@, or @code CUPS_RASTER_WRITE_PWG@ mode can
// be used - compressed and PWG output is generally 25-50% smaller but adds a
// 100-300% execution time overhead.
//
cups_raster_t * // O - New stream
cupsRasterOpen(int fd, // I - File descriptor
cups_raster_mode_t mode) // I - Mode - `CUPS_RASTER_READ`, `CUPS_RASTER_WRITE`, `CUPS_RASTER_WRITE_COMPRESSED`, `CUPS_RASTER_WRITE_PWG`
{
if (mode == CUPS_RASTER_READ)
return (_cupsRasterNew(cups_read_fd, (void *)((intptr_t)fd), mode));
else
return (_cupsRasterNew(cups_write_fd, (void *)((intptr_t)fd), mode));
}
//
// 'cupsRasterOpenIO()' - Open a raster stream using a callback function.
//
// This function associates a raster stream with the given callback function and
// context pointer.
//
// When writing raster data, the @code CUPS_RASTER_WRITE@,
// @code CUPS_RASTER_WRITE_COMPRESS@, or @code CUPS_RASTER_WRITE_PWG@ mode can
// be used - compressed and PWG output is generally 25-50% smaller but adds a
// 100-300% execution time overhead.
//
cups_raster_t * // O - New stream
cupsRasterOpenIO(
cups_raster_cb_t iocb, // I - Read/write callback
void *ctx, // I - Context pointer for callback
cups_raster_mode_t mode) // I - Mode - `CUPS_RASTER_READ`, `CUPS_RASTER_WRITE`, `CUPS_RASTER_WRITE_COMPRESSED`, `CUPS_RASTER_WRITE_PWG`
{
return (_cupsRasterNew(iocb, ctx, mode));
}
//
// 'cupsRasterReadHeader()' - Read a raster page header.
//
bool // O - `true` on success, `false` on failure
cupsRasterReadHeader(
cups_raster_t *r, // I - Raster stream
cups_page_header_t *h) // I - Pointer to header data
{
size_t len; // Length for read/swap
DEBUG_printf(("cupsRasterReadHeader(r=%p, h=%p), r->mode=%s", (void *)r, (void *)h, r ? cups_modes[r->mode] : ""));
if (r == NULL || r->mode != CUPS_RASTER_READ || !h)
return (false);
DEBUG_printf(("1cupsRasterReadHeader: r->iocount=" CUPS_LLFMT, CUPS_LLCAST r->iocount));
memset(&(r->header), 0, sizeof(r->header));
// Read the header...
switch (r->sync)
{
default :
// Get the length of the raster header...
if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1)
len = offsetof(cups_page_header_t, cupsNumColors);
else
len = sizeof(cups_page_header_t);
DEBUG_printf(("2cupsRasterReadHeader: len=%d", (int)len));
// Read it...
if (cups_raster_read(r, (unsigned char *)&(r->header), len) < (ssize_t)len)
{
DEBUG_printf(("2cupsRasterReadHeader: EOF, r->iocount=" CUPS_LLFMT, CUPS_LLCAST r->iocount));
return (false);
}
// Swap bytes as needed...
if (r->swapped)
{
unsigned *s, // Current word
temp; // Temporary copy
DEBUG_puts("2cupsRasterReadHeader: Swapping header bytes.");
for (len = 81, s = &(r->header.AdvanceDistance); len > 0; len --, s ++)
{
temp = *s;
*s = ((temp & 0xff) << 24) | ((temp & 0xff00) << 8) | ((temp & 0xff0000) >> 8) | ((temp & 0xff000000) >> 24);
DEBUG_printf(("2cupsRasterReadHeader: %08x => %08x", temp, *s));
}
}
break;
case CUPS_RASTER_SYNCapple :
case CUPS_RASTER_REVSYNCapple :
{
unsigned char appleheader[32]; // Raw header
static const unsigned rawcspace[] =
{
CUPS_CSPACE_SW,
CUPS_CSPACE_SRGB,
CUPS_CSPACE_CIELab,
CUPS_CSPACE_ADOBERGB,
CUPS_CSPACE_W,
CUPS_CSPACE_RGB,
CUPS_CSPACE_CMYK
};
static const unsigned rawnumcolors[] =
{
1,
3,
3,
3,
1,
3,
4
};
if (cups_raster_read(r, appleheader, sizeof(appleheader)) < (ssize_t)sizeof(appleheader))
{
DEBUG_printf(("2cupsRasterReadHeader: EOF, r->iocount=" CUPS_LLFMT, CUPS_LLCAST r->iocount));
return (false);
}
cupsCopyString(r->header.MediaClass, "PwgRaster", sizeof(r->header.MediaClass));
// PwgRaster
r->header.cupsBitsPerPixel = appleheader[0];
r->header.cupsColorSpace = appleheader[1] >= (sizeof(rawcspace) / sizeof(rawcspace[0])) ? CUPS_CSPACE_DEVICE1 : rawcspace[appleheader[1]];
r->header.cupsNumColors = appleheader[1] >= (sizeof(rawnumcolors) / sizeof(rawnumcolors[0])) ? 1 : rawnumcolors[appleheader[1]];
r->header.cupsBitsPerColor = r->header.cupsBitsPerPixel / r->header.cupsNumColors;
r->header.cupsWidth = ((((((unsigned)appleheader[12] << 8) | (unsigned)appleheader[13]) << 8) | (unsigned)appleheader[14]) << 8) | (unsigned)appleheader[15];
r->header.cupsHeight = ((((((unsigned)appleheader[16] << 8) | (unsigned)appleheader[17]) << 8) | (unsigned)appleheader[18]) << 8) | (unsigned)appleheader[19];
r->header.cupsBytesPerLine = r->header.cupsWidth * r->header.cupsBitsPerPixel / 8;
r->header.cupsColorOrder = CUPS_ORDER_CHUNKED;
r->header.HWResolution[0] = r->header.HWResolution[1] = ((((((unsigned)appleheader[20] << 8) | (unsigned)appleheader[21]) << 8) | (unsigned)appleheader[22]) << 8) | (unsigned)appleheader[23];
if (r->header.HWResolution[0] > 0)
{
r->header.PageSize[0] = (unsigned)(r->header.cupsWidth * 72 / r->header.HWResolution[0]);
r->header.PageSize[1] = (unsigned)(r->header.cupsHeight * 72 / r->header.HWResolution[1]);
r->header.cupsPageSize[0] = (float)(r->header.cupsWidth * 72.0 / r->header.HWResolution[0]);
r->header.cupsPageSize[1] = (float)(r->header.cupsHeight * 72.0 / r->header.HWResolution[1]);
}
r->header.cupsInteger[CUPS_RASTER_PWG_TotalPageCount] = r->apple_page_count;
r->header.cupsInteger[CUPS_RASTER_PWG_AlternatePrimary] = 0xffffff;
r->header.cupsInteger[CUPS_RASTER_PWG_PrintQuality] = appleheader[3];
if (appleheader[2] >= 2)
r->header.Duplex = 1;
if (appleheader[2] == 2)
r->header.Tumble = 1;
r->header.MediaPosition = appleheader[5];
if (appleheader[4] < (int)(sizeof(apple_media_types) / sizeof(apple_media_types[0])))
cupsCopyString(r->header.MediaType, apple_media_types[appleheader[4]], sizeof(r->header.MediaType));
else
cupsCopyString(r->header.MediaType, "other", sizeof(r->header.MediaType));
}
break;
}
// Update the header and row count...
if (!cups_raster_update(r))
return (false);
DEBUG_printf(("2cupsRasterReadHeader: cupsColorSpace=%s", _cupsRasterColorSpaceString(r->header.cupsColorSpace)));
DEBUG_printf(("2cupsRasterReadHeader: cupsBitsPerColor=%u", r->header.cupsBitsPerColor));
DEBUG_printf(("2cupsRasterReadHeader: cupsBitsPerPixel=%u", r->header.cupsBitsPerPixel));
DEBUG_printf(("2cupsRasterReadHeader: cupsBytesPerLine=%u", r->header.cupsBytesPerLine));
DEBUG_printf(("2cupsRasterReadHeader: cupsWidth=%u", r->header.cupsWidth));
DEBUG_printf(("2cupsRasterReadHeader: cupsHeight=%u", r->header.cupsHeight));
DEBUG_printf(("2cupsRasterReadHeader: r->bpp=%d", r->bpp));
memcpy(h, &r->header, sizeof(cups_page_header_t));
return (r->header.cupsBitsPerPixel > 0 && r->header.cupsBitsPerPixel <= 240 && r->header.cupsBitsPerColor > 0 && r->header.cupsBitsPerColor <= 16 && r->header.cupsBytesPerLine > 0 && r->header.cupsBytesPerLine <= 0x7fffffff && r->header.cupsHeight != 0 && (r->header.cupsBytesPerLine % r->bpp) == 0);
}
//
// 'cupsRasterReadPixels()' - Read raster pixels.
//
// For best performance, filters should read one or more whole lines.
// The "cupsBytesPerLine" value from the page header can be used to allocate
// the line buffer and as the number of bytes to read.
//
unsigned // O - Number of bytes read
cupsRasterReadPixels(
cups_raster_t *r, // I - Raster stream
unsigned char *p, // I - Pointer to pixel buffer
unsigned len) // I - Number of bytes to read
{
ssize_t bytes; // Bytes read
unsigned cupsBytesPerLine; // cupsBytesPerLine value
unsigned remaining; // Bytes remaining
unsigned char *ptr, // Pointer to read buffer
byte, // Byte from file
*temp; // Pointer into buffer
unsigned count; // Repetition count
DEBUG_printf(("cupsRasterReadPixels(r=%p, p=%p, len=%u)", (void *)r, (void *)p, len));
if (r == NULL || r->mode != CUPS_RASTER_READ || r->remaining == 0 ||
r->header.cupsBytesPerLine == 0)
{
DEBUG_puts("1cupsRasterReadPixels: Returning 0.");
return (0);
}
DEBUG_printf(("1cupsRasterReadPixels: compressed=%d, remaining=%u", r->compressed, r->remaining));
if (!r->compressed)
{
// Read without compression...
r->remaining -= len / r->header.cupsBytesPerLine;
if (cups_raster_io(r, p, len) < (ssize_t)len)
{
DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0.");
return (0);
}
// Swap bytes as needed...
if (r->swapped && (r->header.cupsBitsPerColor == 16 || r->header.cupsBitsPerPixel == 12 || r->header.cupsBitsPerPixel == 16))
cups_swap(p, len);
// Return...
DEBUG_printf(("1cupsRasterReadPixels: Returning %u", len));
return (len);
}
// Read compressed data...
remaining = len;
cupsBytesPerLine = r->header.cupsBytesPerLine;
while (remaining > 0 && r->remaining > 0)
{
if (r->count == 0)
{
// Need to read a new row...
if (remaining == cupsBytesPerLine)
ptr = p;
else
ptr = r->pixels;
// Read using a modified PackBits compression...
if (!cups_raster_read(r, &byte, 1))
{
DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0.");
return (0);
}
r->count = (unsigned)byte + 1;
if (r->count > 1)
ptr = r->pixels;
temp = ptr;
bytes = (ssize_t)cupsBytesPerLine;
while (bytes > 0)
{
// Get a new repeat count...
if (!cups_raster_read(r, &byte, 1))
{
DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0.");
return (0);
}
if (byte == 128)
{
// Clear to end of line...
switch (r->header.cupsColorSpace)
{
case CUPS_CSPACE_W :
case CUPS_CSPACE_RGB :
case CUPS_CSPACE_SW :
case CUPS_CSPACE_SRGB :
case CUPS_CSPACE_RGBW :
case CUPS_CSPACE_ADOBERGB :
memset(temp, 0xff, (size_t)bytes);
break;
default :
memset(temp, 0x00, (size_t)bytes);
break;
}
temp += bytes;
bytes = 0;
}
else if (byte & 128)
{
// Copy N literal pixels...
count = (unsigned)(257 - byte) * r->bpp;
if (count > (unsigned)bytes)
count = (unsigned)bytes;
if (!cups_raster_read(r, temp, count))
{
DEBUG_puts("1cupsRasterReadPixels: Read error, returning 0.");
return (0);
}
temp += count;
bytes -= (ssize_t)count;
}
else
{
// Repeat the next N bytes...
count = ((unsigned)byte + 1) * r->bpp;
if (count > (unsigned)bytes)
count = (unsigned)bytes;