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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
|
/* $Header: /usr/people/sam/tiff/tools/RCS/tiff2ps.c,v 1.48 1996/01/10 19:35:35 sam Exp $ */
/*
* Copyright (c) 1988-1996 Sam Leffler
* Copyright (c) 1991-1996 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Sam Leffler and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Sam Leffler and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h> /* for atof */
#include <math.h>
#include <time.h>
#include "tiffio.h"
/*
* NB: this code assumes uint32 works with printf's %l[ud].
*/
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
int ascii85 = FALSE; /* use ASCII85 encoding */
int interpolate = TRUE; /* interpolate level2 image */
int level2 = FALSE; /* generate PostScript level 2 */
int printAll = FALSE; /* print all images in file */
int generateEPSF = TRUE; /* generate Encapsulated PostScript */
int PSduplex = FALSE; /* enable duplex printing */
int PStumble = FALSE; /* enable top edge binding */
char *filename; /* input filename */
/*
* ASCII85 Encoding Support.
*/
unsigned char ascii85buf[10];
int ascii85count;
int ascii85breaklen;
int TIFF2PS(FILE*, TIFF*, float, float);
void PSpage(FILE*, TIFF*, uint32, uint32);
void PSColorContigPreamble(FILE*, uint32, uint32, int);
void PSColorSeparatePreamble(FILE*, uint32, uint32, int);
void PSDataColorContig(FILE*, TIFF*, uint32, uint32, int);
void PSDataColorSeparate(FILE*, TIFF*, uint32, uint32, int);
void PSDataPalette(FILE*, TIFF*, uint32, uint32);
void PSDataBW(FILE*, TIFF*, uint32, uint32);
void PSRawDataBW(FILE*, TIFF*, uint32, uint32);
void Ascii85Init(void);
void Ascii85Put(unsigned char code, FILE* fd);
void Ascii85Flush(FILE* fd);
void PSHead(FILE*, TIFF*, uint32, uint32, float, float, float, float);
void PSTail(FILE*, int);
static void usage(int);
int
main(int argc, char* argv[])
{
int dirnum = -1, c, np = 0;
float pageWidth = 0;
float pageHeight = 0;
uint32 diroff = 0;
extern char *optarg;
extern int optind;
FILE* output = stdout;
while ((c = getopt(argc, argv, "h:w:d:o:O:aeps128DT")) != -1)
switch (c) {
case 'd':
dirnum = atoi(optarg);
break;
case 'D':
PSduplex = TRUE;
break;
case 'T':
PStumble = TRUE;
break;
case 'e':
generateEPSF = TRUE;
break;
case 'h':
pageHeight = atof(optarg);
break;
case 'o':
diroff = (uint32) strtoul(optarg, NULL, 0);
break;
case 'O': /* XXX too bad -o is already taken */
output = fopen(optarg, "w");
if (output == NULL) {
fprintf(stderr,
"%s: %s: Cannot open output file.\n",
argv[0], optarg);
exit(-2);
}
break;
case 'a':
printAll = TRUE;
/* fall thru... */
case 'p':
generateEPSF = FALSE;
break;
case 's':
printAll = FALSE;
break;
case 'w':
pageWidth = atof(optarg);
break;
case '1':
level2 = FALSE;
ascii85 = FALSE;
break;
case '2':
level2 = TRUE;
ascii85 = TRUE; /* default to yes */
break;
case '8':
ascii85 = FALSE;
break;
case '?':
usage(-1);
}
for (; argc - optind > 0; optind++) {
TIFF* tif = TIFFOpen(filename = argv[optind], "r");
if (tif != NULL) {
if (dirnum != -1 && !TIFFSetDirectory(tif, dirnum))
return (-1);
else if (diroff != 0 &&
!TIFFSetSubDirectory(tif, diroff))
return (-1);
np = TIFF2PS(output, tif, pageWidth, pageHeight);
TIFFClose(tif);
}
}
if (np)
PSTail(output, np);
else
usage(-1);
if (output != stdout)
fclose(output);
return (0);
}
static uint16 samplesperpixel;
static uint16 bitspersample;
static uint16 planarconfiguration;
static uint16 photometric;
static uint16 compression;
static uint16 extrasamples;
static int alpha;
static int
checkImage(TIFF* tif)
{
switch (bitspersample) {
case 1: case 2:
case 4: case 8:
break;
default:
TIFFError(filename, "Can not handle %d-bit/sample image",
bitspersample);
return (0);
}
switch (photometric) {
case PHOTOMETRIC_YCBCR:
if (compression == COMPRESSION_JPEG &&
planarconfiguration == PLANARCONFIG_CONTIG) {
/* can rely on libjpeg to convert to RGB */
TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE,
JPEGCOLORMODE_RGB);
photometric = PHOTOMETRIC_RGB;
} else {
if (level2)
break;
TIFFError(filename, "Can not handle image with %s",
"PhotometricInterpretation=YCbCr");
return (0);
}
/* fall thru... */
case PHOTOMETRIC_RGB:
if (alpha && bitspersample != 8) {
TIFFError(filename,
"Can not handle %d-bit/sample RGB image with alpha",
bitspersample);
return (0);
}
/* fall thru... */
case PHOTOMETRIC_SEPARATED:
case PHOTOMETRIC_PALETTE:
case PHOTOMETRIC_MINISBLACK:
case PHOTOMETRIC_MINISWHITE:
break;
case PHOTOMETRIC_CIELAB:
/* fall thru... */
default:
TIFFError(filename,
"Can not handle image with PhotometricInterpretation=%d",
photometric);
return (0);
}
if (planarconfiguration == PLANARCONFIG_SEPARATE && extrasamples > 0)
TIFFWarning(filename, "Ignoring extra samples");
return (1);
}
#define PS_UNIT_SIZE 72.0
#define PSUNITS(npix,res) ((npix) * (PS_UNIT_SIZE / (res)))
static char RGBcolorimage[] = "\
/bwproc {\n\
rgbproc\n\
dup length 3 idiv string 0 3 0\n\
5 -1 roll {\n\
add 2 1 roll 1 sub dup 0 eq {\n\
pop 3 idiv\n\
3 -1 roll\n\
dup 4 -1 roll\n\
dup 3 1 roll\n\
5 -1 roll put\n\
1 add 3 0\n\
} { 2 1 roll } ifelse\n\
} forall\n\
pop pop pop\n\
} def\n\
/colorimage where {pop} {\n\
/colorimage {pop pop /rgbproc exch def {bwproc} image} bind def\n\
} ifelse\n\
";
/*
* Adobe Photoshop requires a comment line of the form:
*
* %ImageData: <cols> <rows> <depth> <main channels> <pad channels>
* <block size> <1 for binary|2 for hex> "data start"
*
* It is claimed to be part of some future revision of the EPS spec.
*/
static void
PhotoshopBanner(FILE* fd, uint32 w, uint32 h, int bs, int nc, char* startline)
{
fprintf(fd, "%%ImageData: %ld %ld %d %d 0 %d 2 \"",
(long) w, (long) h, bitspersample, nc, bs);
fprintf(fd, startline, nc);
fprintf(fd, "\"\n");
}
static void
setupPageState(TIFF* tif, uint32* pw, uint32* ph, float* pprw, float* pprh)
{
uint16 res_unit;
float xres, yres;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, pw);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, ph);
TIFFGetFieldDefaulted(tif, TIFFTAG_RESOLUTIONUNIT, &res_unit);
/*
* Calculate printable area.
*/
if (!TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xres))
xres = PS_UNIT_SIZE;
if (!TIFFGetField(tif, TIFFTAG_YRESOLUTION, &yres))
yres = PS_UNIT_SIZE;
switch (res_unit) {
case RESUNIT_CENTIMETER:
xres /= 2.54, yres /= 2.54;
break;
case RESUNIT_NONE:
xres *= PS_UNIT_SIZE, yres *= PS_UNIT_SIZE;
break;
}
*pprh = PSUNITS(*ph, yres);
*pprw = PSUNITS(*pw, xres);
}
static int
isCCITTCompression(TIFF* tif)
{
uint16 compress;
TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
return (compress == COMPRESSION_CCITTFAX3 ||
compress == COMPRESSION_CCITTFAX4 ||
compress == COMPRESSION_CCITTRLE ||
compress == COMPRESSION_CCITTRLEW);
}
static tsize_t tf_bytesperrow;
static tsize_t ps_bytesperrow;
static tsize_t tf_rowsperstrip;
static tsize_t tf_numberstrips;
static char *hex = "0123456789abcdef";
/* returns the sequence number of the page processed */
int
TIFF2PS(FILE* fd, TIFF* tif, float pw, float ph)
{
uint32 w, h;
float ox, oy, prw, prh;
uint32 subfiletype;
uint16* sampleinfo;
static int npages = 0;
if (!TIFFGetField(tif, TIFFTAG_XPOSITION, &ox))
ox = 0;
if (!TIFFGetField(tif, TIFFTAG_YPOSITION, &oy))
oy = 0;
setupPageState(tif, &w, &h, &prw, &prh);
do {
tf_numberstrips = TIFFNumberOfStrips(tif);
TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP,
&tf_rowsperstrip);
setupPageState(tif, &w, &h, &prw, &prh);
if (!npages)
PSHead(fd, tif, w, h, prw, prh, ox, oy);
tf_bytesperrow = TIFFScanlineSize(tif);
TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE,
&bitspersample);
TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL,
&samplesperpixel);
TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG,
&planarconfiguration);
TIFFGetField(tif, TIFFTAG_COMPRESSION, &compression);
TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
&extrasamples, &sampleinfo);
alpha = (extrasamples == 1 &&
sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) {
switch (samplesperpixel - extrasamples) {
case 1:
if (isCCITTCompression(tif))
photometric = PHOTOMETRIC_MINISWHITE;
else
photometric = PHOTOMETRIC_MINISBLACK;
break;
case 3:
photometric = PHOTOMETRIC_RGB;
break;
}
}
if (checkImage(tif)) {
npages++;
fprintf(fd, "%%%%Page: %d %d\n", npages, npages);
fprintf(fd, "gsave\n");
fprintf(fd, "100 dict begin\n");
if (pw != 0 && ph != 0)
fprintf(fd, "%f %f scale\n",
pw*PS_UNIT_SIZE, ph*PS_UNIT_SIZE);
else
fprintf(fd, "%f %f scale\n", prw, prh);
PSpage(fd, tif, w, h);
fprintf(fd, "end\n");
fprintf(fd, "grestore\n");
fprintf(fd, "showpage\n");
}
if (generateEPSF)
break;
TIFFGetFieldDefaulted(tif, TIFFTAG_SUBFILETYPE, &subfiletype);
} while (((subfiletype & FILETYPE_PAGE) || printAll) &&
TIFFReadDirectory(tif));
return(npages);
}
static char DuplexPreamble[] = "\
%%BeginFeature: *Duplex True\n\
systemdict begin\n\
/languagelevel where { pop languagelevel } { 1 } ifelse\n\
2 ge { 1 dict dup /Duplex true put setpagedevice }\n\
{ statusdict /setduplex known { statusdict begin setduplex true end } if\n\
} ifelse\n\
end\n\
%%EndFeature\n\
";
static char TumblePreamble[] = "\
%%BeginFeature: *Tumble True\n\
systemdict begin\n\
/languagelevel where { pop languagelevel } { 1 } ifelse\n\
2 ge { 1 dict dup /Tumble true put setpagedevice }\n\
{ statusdict /settumble known { statusdict begin settumble true end } if\n\
} ifelse\n\
end\n\
%%EndFeature\n\
";
void
PSHead(FILE *fd, TIFF *tif, uint32 w, uint32 h, float pw, float ph,
float ox, float oy)
{
time_t t;
(void) tif; (void) w; (void) h;
t = time(0);
fprintf(fd, "%%!PS-Adobe-3.0%s\n", generateEPSF ? " EPSF-3.0" : "");
fprintf(fd, "%%%%Creator: tiff2ps\n");
fprintf(fd, "%%%%Title: %s\n", filename);
fprintf(fd, "%%%%CreationDate: %s", ctime(&t));
fprintf(fd, "%%%%DocumentData: Clean7Bit\n");
fprintf(fd, "%%%%Origin: %ld %ld\n", (long) ox, (long) oy);
/* NB: should use PageBoundingBox */
fprintf(fd, "%%%%BoundingBox: 0 0 %ld %ld\n",
(long) ceil(pw), (long) ceil(ph));
fprintf(fd, "%%%%LanguageLevel: %d\n", level2 ? 2 : 1);
fprintf(fd, "%%%%Pages: (atend)\n");
fprintf(fd, "%%%%EndComments\n");
fprintf(fd, "%%%%BeginSetup\n");
if (PSduplex)
fprintf(fd, "%s", DuplexPreamble);
if (PStumble)
fprintf(fd, "%s", TumblePreamble);
fprintf(fd, "%%%%EndSetup\n");
}
void
PSTail(FILE *fd, int npages)
{
fprintf(fd, "%%%%Trailer\n");
fprintf(fd, "%%%%Pages: %d\n", npages);
fprintf(fd, "%%%%EOF\n");
}
static int
checkcmap(TIFF* tif, int n, uint16* r, uint16* g, uint16* b)
{
(void) tif;
while (n-- > 0)
if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
return (16);
TIFFWarning(filename, "Assuming 8-bit colormap");
return (8);
}
static void
PS_Lvl2colorspace(FILE* fd, TIFF* tif)
{
uint16 *rmap, *gmap, *bmap;
int i, num_colors;
/*
* Set up PostScript Level 2 colorspace according to
* section 4.8 in the PostScript refenence manual.
*/
fputs("% PostScript Level 2 only.\n", fd);
if (photometric != PHOTOMETRIC_PALETTE) {
if (photometric == PHOTOMETRIC_YCBCR) {
/* MORE CODE HERE */
}
fprintf(fd, "/Device%s",
samplesperpixel > 2 ? "RGB" : "Gray");
fputs(" setcolorspace\n", fd);
return;
}
/*
* Set up an indexed/palette colorspace
*/
num_colors = (1 << bitspersample);
if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) {
TIFFError(filename,
"Palette image w/o \"Colormap\" tag");
return;
}
if (checkcmap(tif, num_colors, rmap, gmap, bmap) == 16) {
/*
* Convert colormap to 8-bits values.
*/
#define CVT(x) (((x) * 255) / ((1L<<16)-1))
for (i = 0; i < num_colors; i++) {
rmap[i] = CVT(rmap[i]);
gmap[i] = CVT(gmap[i]);
bmap[i] = CVT(bmap[i]);
}
#undef CVT
}
fprintf(fd, "[ /Indexed /DeviceRGB %d", num_colors - 1);
if (ascii85) {
Ascii85Init();
fputs("\n<~", fd);
ascii85breaklen -= 2;
} else
fputs(" <", fd);
for (i = 0; i < num_colors; i++) {
if (ascii85) {
Ascii85Put(rmap[i], fd);
Ascii85Put(gmap[i], fd);
Ascii85Put(bmap[i], fd);
} else {
fputs((i % 8) ? " " : "\n ", fd);
fprintf(fd, "%02x%02x%02x",
rmap[i], gmap[i], bmap[i]);
}
}
if (ascii85)
Ascii85Flush(fd);
else
fputs(">\n", fd);
fputs("] setcolorspace\n", fd);
}
static int
PS_Lvl2ImageDict(FILE* fd, TIFF* tif, uint32 w, uint32 h)
{
int use_rawdata;
uint32 tile_width, tile_height;
uint16 predictor, minsamplevalue, maxsamplevalue;
int repeat_count;
char im_h[64], im_x[64], im_y[64];
(void)strcpy(im_x, "0");
(void)sprintf(im_y, "%lu", (long) h);
(void)sprintf(im_h, "%lu", (long) h);
tile_width = w;
tile_height = h;
if (TIFFIsTiled(tif)) {
repeat_count = TIFFNumberOfTiles(tif);
TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tile_width);
TIFFGetField(tif, TIFFTAG_TILELENGTH, &tile_height);
if (tile_width > w || tile_height > h ||
(w % tile_width) != 0 || (h % tile_height != 0)) {
/*
* The tiles does not fit image width and height.
* Set up a clip rectangle for the image unit square.
*/
fputs("0 0 1 1 rectclip\n", fd);
}
if (tile_width < w) {
fputs("/im_x 0 def\n", fd);
(void)strcpy(im_x, "im_x neg");
}
if (tile_height < h) {
fputs("/im_y 0 def\n", fd);
(void)sprintf(im_y, "%lu im_y sub", (unsigned long) h);
}
} else {
repeat_count = tf_numberstrips;
tile_height = tf_rowsperstrip;
if (tile_height > h)
tile_height = h;
if (repeat_count > 1) {
fputs("/im_y 0 def\n", fd);
fprintf(fd, "/im_h %lu def\n",
(unsigned long) tile_height);
(void)strcpy(im_h, "im_h");
(void)sprintf(im_y, "%lu im_y sub", (unsigned long) h);
}
}
/*
* Output start of exec block
*/
fputs("{ % exec\n", fd);
if (repeat_count > 1)
fprintf(fd, "%d { %% repeat\n", repeat_count);
/*
* Output filter options and image dictionary.
*/
if (ascii85)
fputs(" /im_stream currentfile /ASCII85Decode filter def\n",
fd);
fputs(" <<\n", fd);
fputs(" /ImageType 1\n", fd);
fprintf(fd, " /Width %lu\n", (unsigned long) tile_width);
fprintf(fd, " /Height %lu\n", (unsigned long) tile_height);
if (planarconfiguration == PLANARCONFIG_SEPARATE)
fputs(" /MultipleDataSources true\n", fd);
fprintf(fd, " /ImageMatrix [ %lu 0 0 %ld %s %s ]\n",
(unsigned long) w, - (long)h, im_x, im_y);
fprintf(fd, " /BitsPerComponent %d\n", bitspersample);
fprintf(fd, " /Interpolate %s\n", interpolate ? "true" : "false");
switch (samplesperpixel) {
case 1:
switch (photometric) {
case PHOTOMETRIC_MINISBLACK:
fputs(" /Decode [0 1]\n", fd);
break;
case PHOTOMETRIC_MINISWHITE:
switch (compression) {
case COMPRESSION_CCITTRLE:
case COMPRESSION_CCITTRLEW:
case COMPRESSION_CCITTFAX3:
case COMPRESSION_CCITTFAX4:
/*
* Manage inverting with /Blackis1 flag
* since there migth be uncompressed parts
*/
fputs(" /Decode [0 1]\n", fd);
break;
default:
/*
* ERROR...
*/
fputs(" /Decode [1 0]\n", fd);
break;
}
break;
case PHOTOMETRIC_PALETTE:
TIFFGetFieldDefaulted(tif, TIFFTAG_MINSAMPLEVALUE,
&minsamplevalue);
TIFFGetFieldDefaulted(tif, TIFFTAG_MAXSAMPLEVALUE,
&maxsamplevalue);
fprintf(fd, " /Decode [%u %u]\n",
minsamplevalue, maxsamplevalue);
break;
default:
/*
* ERROR ?
*/
fputs(" /Decode [0 1]\n", fd);
break;
}
break;
case 3:
switch (photometric) {
case PHOTOMETRIC_RGB:
fputs(" /Decode [0 1 0 1 0 1]\n", fd);
break;
case PHOTOMETRIC_MINISWHITE:
case PHOTOMETRIC_MINISBLACK:
default:
/*
* ERROR??
*/
fputs(" /Decode [0 1 0 1 0 1]\n", fd);
break;
}
break;
case 4:
/*
* ERROR??
*/
fputs(" /Decode [0 1 0 1 0 1 0 1]\n", fd);
break;
}
fputs(" /DataSource", fd);
if (planarconfiguration == PLANARCONFIG_SEPARATE &&
samplesperpixel > 1)
fputs(" [", fd);
if (ascii85)
fputs(" im_stream", fd);
else
fputs(" currentfile /ASCIIHexDecode filter", fd);
use_rawdata = TRUE;
switch (compression) {
case COMPRESSION_NONE: /* 1: uncompressed */
break;
case COMPRESSION_CCITTRLE: /* 2: CCITT modified Huffman RLE */
case COMPRESSION_CCITTRLEW: /* 32771: #1 w/ word alignment */
case COMPRESSION_CCITTFAX3: /* 3: CCITT Group 3 fax encoding */
case COMPRESSION_CCITTFAX4: /* 4: CCITT Group 4 fax encoding */
fputs("\n\t<<\n", fd);
if (compression == COMPRESSION_CCITTFAX3) {
uint32 g3_options;
fputs("\t /EndOfLine true\n", fd);
fputs("\t /EndOfBlock false\n", fd);
if (!TIFFGetField(tif, TIFFTAG_GROUP3OPTIONS,
&g3_options))
g3_options = 0;
if (g3_options & GROUP3OPT_2DENCODING)
fprintf(fd, "\t /K %s\n", im_h);
if (g3_options & GROUP3OPT_UNCOMPRESSED)
fputs("\t /Uncompressed true\n", fd);
if (g3_options & GROUP3OPT_FILLBITS)
fputs("\t /EncodedByteAlign true\n", fd);
}
if (compression == COMPRESSION_CCITTFAX4) {
uint32 g4_options;
fputs("\t /K -1\n", fd);
TIFFGetFieldDefaulted(tif, TIFFTAG_GROUP4OPTIONS,
&g4_options);
if (g4_options & GROUP4OPT_UNCOMPRESSED)
fputs("\t /Uncompressed true\n", fd);
}
if (!(tile_width == w && w == 1728U))
fprintf(fd, "\t /Columns %lu\n",
(unsigned long) tile_width);
fprintf(fd, "\t /Rows %s\n", im_h);
if (compression == COMPRESSION_CCITTRLE ||
compression == COMPRESSION_CCITTRLEW) {
fputs("\t /EncodedByteAlign true\n", fd);
fputs("\t /EndOfBlock false\n", fd);
}
if (photometric == PHOTOMETRIC_MINISBLACK)
fputs("\t /BlackIs1 true\n", fd);
fprintf(fd, "\t>> /CCITTFaxDecode filter");
break;
case COMPRESSION_LZW: /* 5: Lempel-Ziv & Welch */
TIFFGetFieldDefaulted(tif, TIFFTAG_PREDICTOR, &predictor);
if (predictor == 2) {
fputs("\n\t<<\n", fd);
fprintf(fd, "\t /Predictor %u\n", predictor);
fprintf(fd, "\t /Columns %lu\n",
(unsigned long) tile_width);
fprintf(fd, "\t /Colors %u\n", samplesperpixel);
fprintf(fd, "\t /BitsPerComponent %u\n",
bitspersample);
fputs("\t>>", fd);
}
fputs(" /LZWDecode filter", fd);
break;
case COMPRESSION_PACKBITS: /* 32773: Macintosh RLE */
fputs(" /RunLengthDecode filter", fd);
use_rawdata = TRUE;
break;
case COMPRESSION_OJPEG: /* 6: !6.0 JPEG */
case COMPRESSION_JPEG: /* 7: %JPEG DCT compression */
#ifdef notdef
/*
* Code not tested yet
*/
fputs(" /DCTDecode filter", fd);
use_rawdata = TRUE;
#else
use_rawdata = FALSE;
#endif
break;
case COMPRESSION_NEXT: /* 32766: NeXT 2-bit RLE */
case COMPRESSION_THUNDERSCAN: /* 32809: ThunderScan RLE */
case COMPRESSION_PIXARFILM: /* 32908: Pixar companded 10bit LZW */
case COMPRESSION_DEFLATE: /* 32946: Deflate compression */
case COMPRESSION_JBIG: /* 34661: ISO JBIG */
use_rawdata = FALSE;
break;
default:
/*
* ERROR...
*/
use_rawdata = FALSE;
break;
}
if (planarconfiguration == PLANARCONFIG_SEPARATE &&
samplesperpixel > 1) {
uint16 i;
/*
* NOTE: This code does not work yet...
*/
for (i = 1; i < samplesperpixel; i++)
fputs(" dup", fd);
fputs(" ]", fd);
}
fputs("\n >> image\n", fd);
if (ascii85)
fputs(" im_stream flushfile\n", fd);
if (repeat_count > 1) {
if (tile_width < w) {
fprintf(fd, " /im_x im_x %lu add def\n",
(unsigned long) tile_width);
if (tile_height < h) {
fprintf(fd, " im_x %lu ge {\n",
(unsigned long) w);
fputs(" /im_x 0 def\n", fd);
fprintf(fd, " /im_y im_y %lu add def\n",
(unsigned long) tile_height);
fputs(" } if\n", fd);
}
}
if (tile_height < h) {
if (tile_width >= w) {
fprintf(fd, " /im_y im_y %lu add def\n",
(unsigned long) tile_height);
if (!TIFFIsTiled(tif)) {
fprintf(fd, " /im_h %lu im_y sub",
(unsigned long) h);
fprintf(fd, " dup %lu gt { pop",
(unsigned long) tile_height);
fprintf(fd, " %lu } if def\n",
(unsigned long) tile_height);
}
}
}
fputs("} repeat\n", fd);
}
/*
* End of exec function
*/
fputs("}\n", fd);
return(use_rawdata);
}
int
PS_Lvl2page(FILE* fd, TIFF* tif, uint32 w, uint32 h)
{
uint16 fillorder;
int use_rawdata, tiled_image, breaklen;
uint32 chunk_no, num_chunks, *bc;
unsigned char *buf_data, *cp;
tsize_t chunk_size, byte_count;
PS_Lvl2colorspace(fd, tif);
use_rawdata = PS_Lvl2ImageDict(fd, tif, w, h);
fputs("%%BeginData:\n", fd);
fputs("exec\n", fd);
tiled_image = TIFFIsTiled(tif);
if (tiled_image) {
num_chunks = TIFFNumberOfTiles(tif);
TIFFGetField(tif, TIFFTAG_TILEBYTECOUNTS, &bc);
} else {
num_chunks = TIFFNumberOfStrips(tif);
TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bc);
}
if (use_rawdata) {
chunk_size = bc[0];
for (chunk_no = 1; chunk_no < num_chunks; chunk_no++)
if (bc[chunk_no] > chunk_size)
chunk_size = bc[chunk_no];
} else {
if (tiled_image)
chunk_size = TIFFTileSize(tif);
else
chunk_size = TIFFStripSize(tif);
}
buf_data = (unsigned char *)_TIFFmalloc(chunk_size);
if (!buf_data) {
TIFFError(filename, "Can't alloc %u bytes for %s.",
chunk_size, tiled_image ? "tiles" : "strips");
return(FALSE);
}
TIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &fillorder);
for (chunk_no = 0; chunk_no < num_chunks; chunk_no++) {
if (ascii85)
Ascii85Init();
else
breaklen = 36;
if (use_rawdata) {
if (tiled_image)
byte_count = TIFFReadRawTile(tif, chunk_no,
buf_data, chunk_size);
else
byte_count = TIFFReadRawStrip(tif, chunk_no,
buf_data, chunk_size);
if (fillorder == FILLORDER_LSB2MSB)
TIFFReverseBits(buf_data, byte_count);
} else {
if (tiled_image)
byte_count = TIFFReadEncodedTile(tif,
chunk_no, buf_data,
chunk_size);
else
byte_count = TIFFReadEncodedStrip(tif,
chunk_no, buf_data,
chunk_size);
}
if (byte_count < 0) {
TIFFError(filename, "Can't read %s %d.",
tiled_image ? "tile" : "strip", chunk_no);
if (ascii85)
Ascii85Put('\0', fd);
}
for (cp = buf_data; byte_count > 0; byte_count--) {
if (ascii85)
Ascii85Put(*cp++, fd);
else {
if (--breaklen <= 0) {
putc('\n', fd);
breaklen = 36;
}
putc(hex[((*cp)>>4)&0xf], fd);
putc(hex[(*cp)&0xf], fd);
cp++;
}
}
if (ascii85)
Ascii85Flush(fd);
else
putc('\n', fd);
}
_TIFFfree(buf_data);
fputs("%%EndData\n", fd);
return(TRUE);
}
void
PSpage(FILE* fd, TIFF* tif, uint32 w, uint32 h)
{
if (level2 && PS_Lvl2page(fd, tif, w, h))
return;
ps_bytesperrow = tf_bytesperrow;
switch (photometric) {
case PHOTOMETRIC_RGB:
if (planarconfiguration == PLANARCONFIG_CONTIG) {
fprintf(fd, "%s", RGBcolorimage);
PSColorContigPreamble(fd, w, h, 3);
PSDataColorContig(fd, tif, w, h, 3);
} else {
PSColorSeparatePreamble(fd, w, h, 3);
PSDataColorSeparate(fd, tif, w, h, 3);
}
break;
case PHOTOMETRIC_SEPARATED:
/* XXX should emit CMYKcolorimage */
if (planarconfiguration == PLANARCONFIG_CONTIG) {
PSColorContigPreamble(fd, w, h, 4);
PSDataColorContig(fd, tif, w, h, 4);
} else {
PSColorSeparatePreamble(fd, w, h, 4);
PSDataColorSeparate(fd, tif, w, h, 4);
}
break;
case PHOTOMETRIC_PALETTE:
fprintf(fd, "%s", RGBcolorimage);
PhotoshopBanner(fd, w, h, 1, 3, "false 3 colorimage");
fprintf(fd, "/scanLine %ld string def\n",
(long) ps_bytesperrow);
fprintf(fd, "%lu %lu 8\n",
(unsigned long) w, (unsigned long) h);
fprintf(fd, "[%lu 0 0 -%lu 0 %lu]\n",
(unsigned long) w, (unsigned long) h, (unsigned long) h);
fprintf(fd, "{currentfile scanLine readhexstring pop} bind\n");
fprintf(fd, "false 3 colorimage\n");
PSDataPalette(fd, tif, w, h);
break;
case PHOTOMETRIC_MINISBLACK:
case PHOTOMETRIC_MINISWHITE:
PhotoshopBanner(fd, w, h, 1, 1, "image");
fprintf(fd, "/scanLine %ld string def\n",
(long) ps_bytesperrow);
fprintf(fd, "%lu %lu %d\n",
(unsigned long) w, (unsigned long) h, bitspersample);
fprintf(fd, "[%lu 0 0 -%lu 0 %lu]\n",
(unsigned long) w, (unsigned long) h, (unsigned long) h);
fprintf(fd,
"{currentfile scanLine readhexstring pop} bind\n");
fprintf(fd, "image\n");
PSDataBW(fd, tif, w, h);
break;
}
putc('\n', fd);
}
void
PSColorContigPreamble(FILE* fd, uint32 w, uint32 h, int nc)
{
ps_bytesperrow = nc * (tf_bytesperrow / samplesperpixel);
PhotoshopBanner(fd, w, h, 1, nc, "false %d colorimage");
fprintf(fd, "/line %ld string def\n", (long) ps_bytesperrow);
fprintf(fd, "%lu %lu %d\n",
(unsigned long) w, (unsigned long) h, bitspersample);
fprintf(fd, "[%lu 0 0 -%lu 0 %lu]\n",
(unsigned long) w, (unsigned long) h, (unsigned long) h);
fprintf(fd, "{currentfile line readhexstring pop} bind\n");
fprintf(fd, "false %d colorimage\n", nc);
}
void
PSColorSeparatePreamble(FILE* fd, uint32 w, uint32 h, int nc)
{
int i;
PhotoshopBanner(fd, w, h, ps_bytesperrow, nc, "true %d colorimage");
for (i = 0; i < nc; i++)
fprintf(fd, "/line%d %ld string def\n",
i, (long) ps_bytesperrow);
fprintf(fd, "%lu %lu %d\n",
(unsigned long) w, (unsigned long) h, bitspersample);
fprintf(fd, "[%lu 0 0 -%lu 0 %lu] \n",
(unsigned long) w, (unsigned long) h, (unsigned long) h);
for (i = 0; i < nc; i++)
fprintf(fd, "{currentfile line%d readhexstring pop}bind\n", i);
fprintf(fd, "true %d colorimage\n", nc);
}
#define MAXLINE 36
#define DOBREAK(len, howmany, fd) \
if (((len) -= (howmany)) <= 0) { \
putc('\n', fd); \
(len) = MAXLINE-(howmany); \
}
#define PUTHEX(c,fd) putc(hex[((c)>>4)&0xf],fd); putc(hex[(c)&0xf],fd)
void
PSDataColorContig(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc)
{
uint32 row;
int breaklen = MAXLINE, cc, es = samplesperpixel - nc;
unsigned char *tf_buf;
unsigned char *cp, c;
(void) w;
tf_buf = (unsigned char *) _TIFFmalloc(tf_bytesperrow);
if (tf_buf == NULL) {
TIFFError(filename, "No space for scanline buffer");
return;
}
for (row = 0; row < h; row++) {
if (TIFFReadScanline(tif, tf_buf, row, 0) < 0)
break;
cp = tf_buf;
if (alpha) {
int adjust;
cc = 0;
for (; cc < tf_bytesperrow; cc += samplesperpixel) {
DOBREAK(breaklen, nc, fd);
/*
* For images with alpha, matte against
* a white background; i.e.
* Cback * (1 - Aimage)
* where Cback = 1.
*/
adjust = 255 - cp[nc];
switch (nc) {
case 4: c = *cp++ + adjust; PUTHEX(c,fd);
case 3: c = *cp++ + adjust; PUTHEX(c,fd);
case 2: c = *cp++ + adjust; PUTHEX(c,fd);
case 1: c = *cp++ + adjust; PUTHEX(c,fd);
}
cp += es;
}
} else {
cc = 0;
for (; cc < tf_bytesperrow; cc += samplesperpixel) {
DOBREAK(breaklen, nc, fd);
switch (nc) {
case 4: c = *cp++; PUTHEX(c,fd);
case 3: c = *cp++; PUTHEX(c,fd);
case 2: c = *cp++; PUTHEX(c,fd);
case 1: c = *cp++; PUTHEX(c,fd);
}
cp += es;
}
}
}
_TIFFfree((char *) tf_buf);
}
void
PSDataColorSeparate(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc)
{
uint32 row;
int breaklen = MAXLINE, cc, s, maxs;
unsigned char *tf_buf;
unsigned char *cp, c;
(void) w;
tf_buf = (unsigned char *) _TIFFmalloc(tf_bytesperrow);
if (tf_buf == NULL) {
TIFFError(filename, "No space for scanline buffer");
return;
}
maxs = (samplesperpixel > nc ? nc : samplesperpixel);
for (row = 0; row < h; row++) {
for (s = 0; s < maxs; s++) {
if (TIFFReadScanline(tif, tf_buf, row, s) < 0)
break;
for (cp = tf_buf, cc = 0; cc < tf_bytesperrow; cc++) {
DOBREAK(breaklen, 1, fd);
c = *cp++;
PUTHEX(c,fd);
}
}
}
_TIFFfree((char *) tf_buf);
}
#define PUTRGBHEX(c,fd) \
PUTHEX(rmap[c],fd); PUTHEX(gmap[c],fd); PUTHEX(bmap[c],fd)
void
PSDataPalette(FILE* fd, TIFF* tif, uint32 w, uint32 h)
{
uint16 *rmap, *gmap, *bmap;
uint32 row;
int breaklen = MAXLINE, cc, nc;
unsigned char *tf_buf;
unsigned char *cp, c;
(void) w;
if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) {
TIFFError(filename, "Palette image w/o \"Colormap\" tag");
return;
}
switch (bitspersample) {
case 8: case 4: case 2: case 1:
break;
default:
TIFFError(filename, "Depth %d not supported", bitspersample);
return;
}
nc = 3 * (8 / bitspersample);
tf_buf = (unsigned char *) _TIFFmalloc(tf_bytesperrow);
if (tf_buf == NULL) {
TIFFError(filename, "No space for scanline buffer");
return;
}
if (checkcmap(tif, 1<<bitspersample, rmap, gmap, bmap) == 16) {
int i;
#define CVT(x) (((x) * 255) / ((1U<<16)-1))
for (i = (1<<bitspersample)-1; i >= 0; i--) {
rmap[i] = CVT(rmap[i]);
gmap[i] = CVT(gmap[i]);
bmap[i] = CVT(bmap[i]);
}
#undef CVT
}
for (row = 0; row < h; row++) {
if (TIFFReadScanline(tif, tf_buf, row, 0) < 0)
break;
for (cp = tf_buf, cc = 0; cc < tf_bytesperrow; cc++) {
DOBREAK(breaklen, nc, fd);
switch (bitspersample) {
case 8:
c = *cp++; PUTRGBHEX(c, fd);
break;
case 4:
c = *cp++; PUTRGBHEX(c&0xf, fd);
c >>= 4; PUTRGBHEX(c, fd);
break;
case 2:
c = *cp++; PUTRGBHEX(c&0x3, fd);
c >>= 2; PUTRGBHEX(c&0x3, fd);
c >>= 2; PUTRGBHEX(c&0x3, fd);
c >>= 2; PUTRGBHEX(c, fd);
break;
case 1:
c = *cp++; PUTRGBHEX(c&0x1, fd);
c >>= 1; PUTRGBHEX(c&0x1, fd);
c >>= 1; PUTRGBHEX(c&0x1, fd);
c >>= 1; PUTRGBHEX(c&0x1, fd);
c >>= 1; PUTRGBHEX(c&0x1, fd);
c >>= 1; PUTRGBHEX(c&0x1, fd);
c >>= 1; PUTRGBHEX(c&0x1, fd);
c >>= 1; PUTRGBHEX(c, fd);
break;
}
}
}
_TIFFfree((char *) tf_buf);
}
void
PSDataBW(FILE* fd, TIFF* tif, uint32 w, uint32 h)
{
int breaklen = MAXLINE;
unsigned char* tf_buf;
unsigned char* cp;
tsize_t stripsize = TIFFStripSize(tif);
tstrip_t s;
(void) w; (void) h;
tf_buf = (unsigned char *) _TIFFmalloc(stripsize);
if (tf_buf == NULL) {
TIFFError(filename, "No space for scanline buffer");
return;
}
if (ascii85)
Ascii85Init();
for (s = 0; s < TIFFNumberOfStrips(tif); s++) {
int cc = TIFFReadEncodedStrip(tif, s, tf_buf, stripsize);
if (cc < 0) {
TIFFError(filename, "Can't read strip");
break;
}
cp = tf_buf;
if (photometric == PHOTOMETRIC_MINISWHITE) {
for (cp += cc; --cp >= tf_buf;)
*cp = ~*cp;
cp++;
}
if (ascii85) {
while (cc-- > 0)
Ascii85Put(*cp++, fd);
} else {
while (cc-- > 0) {
unsigned char c = *cp++;
DOBREAK(breaklen, 1, fd);
PUTHEX(c, fd);
}
}
}
if (ascii85)
Ascii85Flush(fd);
else if (level2)
fputs(">\n", fd);
_TIFFfree(tf_buf);
}
void
PSRawDataBW(FILE* fd, TIFF* tif, uint32 w, uint32 h)
{
uint32 *bc;
uint32 bufsize;
int breaklen = MAXLINE, cc;
uint16 fillorder;
unsigned char *tf_buf;
unsigned char *cp, c;
tstrip_t s;
(void) w; (void) h;
TIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &fillorder);
TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bc);
bufsize = bc[0];
tf_buf = (unsigned char*) _TIFFmalloc(bufsize);
if (tf_buf == NULL) {
TIFFError(filename, "No space for strip buffer");
return;
}
for (s = 0; s < tf_numberstrips; s++) {
if (bc[s] > bufsize) {
tf_buf = (unsigned char *) _TIFFrealloc(tf_buf, bc[s]);
if (tf_buf == NULL) {
TIFFError(filename,
"No space for strip buffer");
return;
}
bufsize = bc[s];
}
cc = TIFFReadRawStrip(tif, s, tf_buf, bc[s]);
if (cc < 0) {
TIFFError(filename, "Can't read strip");
break;
}
if (fillorder == FILLORDER_LSB2MSB)
TIFFReverseBits(tf_buf, cc);
if (!ascii85) {
for (cp = tf_buf; cc > 0; cc--) {
DOBREAK(breaklen, 1, fd);
c = *cp++;
PUTHEX(c, fd);
}
fputs(">\n", fd);
breaklen = MAXLINE;
} else {
Ascii85Init();
for (cp = tf_buf; cc > 0; cc--)
Ascii85Put(*cp++, fd);
Ascii85Flush(fd);
}
}
_TIFFfree((char *) tf_buf);
}
void
Ascii85Init(void)
{
ascii85breaklen = 2*MAXLINE;
ascii85count = 0;
}
static char*
Ascii85Encode(unsigned char* raw)
{
static char encoded[6];
uint32 word;
word = (((raw[0]<<8)+raw[1])<<16) + (raw[2]<<8) + raw[3];
if (word != 0L) {
uint32 q;
uint16 w1;
q = word / (85L*85*85*85); /* actually only a byte */
encoded[0] = q + '!';
word -= q * (85L*85*85*85); q = word / (85L*85*85);
encoded[1] = q + '!';
word -= q * (85L*85*85); q = word / (85*85);
encoded[2] = q + '!';
w1 = (uint16) (word - q*(85L*85));
encoded[3] = (w1 / 85) + '!';
encoded[4] = (w1 % 85) + '!';
encoded[5] = '\0';
} else
encoded[0] = 'z', encoded[1] = '\0';
return (encoded);
}
void
Ascii85Put(unsigned char code, FILE* fd)
{
ascii85buf[ascii85count++] = code;
if (ascii85count >= 4) {
unsigned char* p;
int n;
for (n = ascii85count, p = ascii85buf; n >= 4; n -= 4, p += 4) {
char* cp;
for (cp = Ascii85Encode(p); *cp; cp++) {
putc(*cp, fd);
if (--ascii85breaklen == 0) {
putc('\n', fd);
ascii85breaklen = 2*MAXLINE;
}
}
p += 4;
}
_TIFFmemcpy(ascii85buf, p, n);
ascii85count = n;
}
}
void
Ascii85Flush(FILE* fd)
{
if (ascii85count > 0) {
char* res;
_TIFFmemset(&ascii85buf[ascii85count], 0, 3);
res = Ascii85Encode(ascii85buf);
fwrite(res[0] == 'z' ? "!!!!" : res, ascii85count + 1, 1, fd);
}
fputs("~>\n", fd);
}
char* stuff[] = {
"usage: tiff2ps [options] input.tif ...",
"where options are:",
" -1 generate PostScript Level I (default)",
" -2 generate PostScript Level II",
" -8 disable use of ASCII85 encoding with PostScript Level II",
" -d # convert directory number #",
" -D enable duplex printing (two pages per sheet of paper)",
" -e generate Encapsulated PostScript (EPS)",
" -h # assume printed page height is # inches (default 11)",
" -o # convert directory at file offset #",
" -O file write PostScript to file instead of standard output",
" -a convert all directories in file (default is first)",
" -p generate regular PostScript",
" -s generate PostScript for a single image",
" -T print pages for top edge binding",
" -w # assume printed page width is # inches (default 8.5)",
NULL
};
static void
usage(int code)
{
char buf[BUFSIZ];
int i;
setbuf(stderr, buf);
for (i = 0; stuff[i] != NULL; i++)
fprintf(stderr, "%s\n", stuff[i]);
exit(code);
}
|