-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiff2ps.c
3361 lines (3136 loc) · 115 KB
/
tiff2ps.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
/*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 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 "tiff_tools_internal.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h> /* for atof */
#include <string.h>
#include <time.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <tiffio.h>
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif
/*
* Revision history
* 2013-Jan-21
* Richard Nolde: Fix bug in auto rotate option code. Once a
* rotation angle was set by the auto rotate check, it was
* retained for all pages that followed instead of being
* retested for each page.
*
* 2010-Sep-17
* Richard Nolde: Reinstate code from Feb 2009 that never got
* accepted into CVS with major modifications to handle -H and -W
* options. Replaced original PlaceImage function with several
* new functions that make support for multiple output pages
* from a single image easier to understand. Added additional
* warning messages for incompatible command line options.
* Add new command line options to specify PageOrientation
* Document Structuring Comment for landscape or portrait
* and code to determine the values from output width and height
* if not specified on the command line.
* Add new command line option to specify document creator
* as an alterntive to the string "tiff2ps" following model
* of patch submitted by Thomas Jarosch for specifying a
* document title which is also supported now.
*
* 2009-Feb-11
* Richard Nolde: Added support for rotations of 90, 180, 270
* and auto using -r <90|180|270|auto>. Auto picks the best
* fit for the image on the specified paper size (eg portrait
* or landscape) if -h or -w is specified. Rotation is in
* degrees counterclockwise since that is how Postscript does
* it. The auto opption rotates the image 90 degrees ccw to
* produce landscape if that is a better fit than portrait.
*
* Cleaned up code in TIFF2PS and broke into smaller functions
* to simplify rotations.
*
* Identified incompatible options and returned errors, eg
* -i for imagemask operator is only available for Level2 or
* Level3 Postscript in the current implementation since there
* is a difference in the way the operands are called for Level1
* and there is no function to provide the Level1 version.
* -H was not handled properly if -h and/or -w were specified.
* It should only clip the masked images if the scaled image
* exceeds the maxPageHeight specified with -H.
*
* New design allows for all of the following combinations:
* Conversion of TIFF to Postscript with optional rotations
* of 90, 180, 270, or auto degrees counterclockwise
* Conversion of TIFF to Postscript with entire image scaled
* to maximum of values specified with -h or -w while
* maintaining aspect ratio. Same rotations apply.
* Conversion of TIFF to Postscript with clipping of output
* viewport to height specified with -H, producing multiple
* pages at this height and original width as needed.
* Same rotations apply.
* Conversion of TIFF to Postscript with image scaled to
* maximum specified by -h and -w and the resulting scaled
* image is presented in an output viewport clipped by -H height.
* The same rotations apply.
*
* Added maxPageWidth option using -W flag. MaxPageHeight and
* MaxPageWidth are mutually exclusive since the aspect ratio
* cannot be maintained if you set both.
* Rewrote PlaceImage to allow maxPageHeight and maxPageWidth
* options to work with values smaller or larger than the
* physical paper size and still preserve the aspect ratio.
* This is accomplished by creating multiple pages across
* as well as down if need be.
*
* 2001-Mar-21
* I (Bruce A. Mallett) added this revision history comment ;)
*
* Fixed PS_Lvl2page() code which outputs non-ASCII85 raw
* data. Moved test for when to output a line break to
* *after* the output of a character. This just serves
* to fix an eye-nuisance where the first line of raw
* data was one character shorter than subsequent lines.
*
* Added an experimental ASCII85 encoder which can be used
* only when there is a single buffer of bytes to be encoded.
* This version is much faster at encoding a straight-line
* buffer of data because it can avoid a lot of the loop
* overhead of the byte-by-byte version. To use this version
* you need to define EXP_ASCII85ENCODER (experimental ...).
*
* Added bug fix given by Michael Schmidt to PS_Lvl2page()
* in which an end-of-data marker ('>') was not being output
* when producing non-ASCII85 encoded PostScript Level 2
* data.
*
* Fixed PS_Lvl2colorspace() so that it no longer assumes that
* a TIFF having more than 2 planes is a CMYK. This routine
* no longer looks at the samples per pixel but instead looks
* at the "photometric" value. This change allows support of
* CMYK TIFFs.
*
* Modified the PostScript L2 imaging loop so as to test if
* the input stream is still open before attempting to do a
* flushfile on it. This was done because some RIPs close
* the stream after doing the image operation.
*
* Got rid of the realloc() being done inside a loop in the
* PSRawDataBW() routine. The code now walks through the
* byte-size array outside the loop to determine the largest
* size memory block that will be needed.
*
* Added "-m" switch to ask tiff2ps to, where possible, use the
* "imagemask" operator instead of the "image" operator.
*
* Added the "-i #" switch to allow interpolation to be disabled.
*
* Unrolled a loop or two to improve performance.
*/
/*
* Define EXP_ASCII85ENCODER if you want to use an experimental
* version of the ASCII85 encoding routine. The advantage of
* using this routine is that tiff2ps will convert to ASCII85
* encoding at between 3 and 4 times the speed as compared to
* using the old (non-experimental) encoder. The disadvantage
* is that you will be using a new (and unproven) encoding
* routine. So user beware, you have been warned!
*/
#define EXP_ASCII85ENCODER
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#define DEFAULT_MAX_MALLOC (256 * 1024 * 1024)
/* malloc size limit (in bytes)
* disabled when set to 0 */
static tmsize_t maxMalloc = DEFAULT_MAX_MALLOC;
int ascii85 = FALSE; /* use ASCII85 encoding */
int interpolate = TRUE; /* interpolate level2 image */
int level2 = FALSE; /* generate PostScript level 2 */
int level3 = FALSE; /* generate PostScript level 3 */
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 */
int PSavoiddeadzone = TRUE; /* enable avoiding printer deadzone */
double maxPageHeight =
0; /* maximum height to select from image and print per page */
double maxPageWidth =
0; /* maximum width to select from image and print per page */
double splitOverlap = 0; /* amount for split pages to overlag */
int rotation = 0; /* optional value for rotation angle */
int auto_rotate = 0; /* rotate image for best fit on the page */
char *filename = NULL; /* input filename */
char *title = NULL; /* optional document title string */
char *creator = NULL; /* optional document creator string */
char pageOrientation[12]; /* set optional PageOrientation DSC to Landscape or
Portrait */
int useImagemask = FALSE; /* Use imagemask instead of image operator */
uint16_t res_unit = 0; /* Resolution units: 2 - inches, 3 - cm */
/*
* ASCII85 Encoding Support.
*/
unsigned char ascii85buf[10];
int ascii85count;
int ascii85breaklen;
int TIFF2PS(FILE *, TIFF *, double, double, double, double, int);
void PSpage(FILE *, TIFF *, uint32_t, uint32_t);
void PSColorContigPreamble(FILE *, uint32_t, uint32_t, int);
void PSColorSeparatePreamble(FILE *, uint32_t, uint32_t, int);
void PSDataColorContig(FILE *, TIFF *, uint32_t, uint32_t, int);
void PSDataColorSeparate(FILE *, TIFF *, uint32_t, uint32_t, int);
void PSDataPalette(FILE *, TIFF *, uint32_t, uint32_t);
void PSDataBW(FILE *, TIFF *, uint32_t, uint32_t);
void Ascii85Init(void);
void Ascii85Put(unsigned char code, FILE *fd);
void Ascii85Flush(FILE *fd);
void PSHead(FILE *, double, double, double, double);
void PSTail(FILE *, int);
int psStart(FILE *, int, int, int *, double *, double, double, double, double,
double, double, double, double, double, double);
int psPageSize(FILE *, int, double, double, double, double, double, double);
int psRotateImage(FILE *, int, double, double, double, double);
int psMaskImage(FILE *, TIFF *, int, int, int *, double, double, double, double,
double, double, double, double, double);
int psScaleImage(FILE *, double, int, int, double, double, double, double,
double, double);
int get_viewport(double, double, double, double, double *, double *, int);
int exportMaskedImage(FILE *, double, double, double, double, int, int, double,
double, double, int, int);
#if defined(EXP_ASCII85ENCODER)
tsize_t Ascii85EncodeBlock(uint8_t *ascii85_p, unsigned f_eod,
const uint8_t *raw_p, tsize_t raw_l);
#endif
static void usage(int);
/**
* This custom malloc function enforce a maximum allocation size
*/
static void *limitMalloc(tmsize_t s)
{
/* tmsize_t is signed and _TIFFmalloc() converts s to size_t. Therefore
* check for negative s. */
if (maxMalloc && ((s > maxMalloc) || (s < 0)))
{
fprintf(stderr,
"MemoryLimitError: allocation of %" TIFF_SSIZE_FORMAT
" bytes is forbidden. Limit is %" TIFF_SSIZE_FORMAT ".\n",
s, maxMalloc);
fprintf(stderr, " use -M option to change limit.\n");
return NULL;
}
return _TIFFmalloc(s);
}
int main(int argc, char *argv[])
{
int dirnum = -1, c, np = 0;
int centered = 0;
double bottommargin = 0;
double leftmargin = 0;
double pageWidth = 0;
double pageHeight = 0;
uint32_t diroff = 0;
#if !HAVE_DECL_OPTARG
extern char *optarg;
extern int optind;
#endif
FILE *output = stdout;
pageOrientation[0] = '\0';
while ((c = getopt(argc, argv,
"b:d:h:H:W:L:M:i:w:l:o:O:P:C:r:t:acemxyzps1238DT")) !=
-1)
switch (c)
{
case 'M':
maxMalloc = (tmsize_t)strtoul(optarg, NULL, 0) << 20;
break;
case 'b':
bottommargin = atof(optarg);
break;
case 'c':
centered = 1;
break;
case 'C':
creator = optarg;
break;
case 'd': /* without -a, this only processes one image at this IFD
*/
dirnum = atoi(optarg);
break;
case 'D':
PSduplex = TRUE;
break;
case 'i':
interpolate = atoi(optarg) ? TRUE : FALSE;
break;
case 'T':
PStumble = TRUE;
break;
case 'e':
PSavoiddeadzone = FALSE;
generateEPSF = TRUE;
break;
case 'h':
pageHeight = atof(optarg);
break;
case 'H':
maxPageHeight = atof(optarg);
break;
case 'W':
maxPageWidth = atof(optarg);
break;
case 'L':
splitOverlap = atof(optarg);
break;
case 'm':
useImagemask = TRUE;
break;
case 'o':
switch (optarg[0])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
diroff = (uint32_t)strtoul(optarg, NULL, 0);
break;
default:
TIFFError("-o", "Offset must be a numeric value.");
exit(EXIT_FAILURE);
}
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(EXIT_FAILURE);
}
break;
case 'P':
switch (optarg[0])
{
case 'l':
case 'L':
strcpy(pageOrientation, "Landscape");
break;
case 'p':
case 'P':
strcpy(pageOrientation, "Portrait");
break;
default:
TIFFError(
"-P",
"Page orientation must be Landscape or Portrait");
exit(EXIT_FAILURE);
}
break;
case 'l':
leftmargin = atof(optarg);
break;
case 'a': /* removed fall through to generate warning below, R Nolde
09-01-2010 */
printAll = TRUE;
break;
case 'p':
generateEPSF = FALSE;
break;
case 'r':
if (strcmp(optarg, "auto") == 0)
{
rotation = 0;
auto_rotate = TRUE;
}
else
{
rotation = atoi(optarg);
auto_rotate = FALSE;
}
switch (rotation)
{
case 0:
case 90:
case 180:
case 270:
break;
default:
fprintf(stderr, "Rotation angle must be 90, 180, 270 "
"(degrees ccw) or auto\n");
exit(EXIT_FAILURE);
}
break;
case 's':
printAll = FALSE;
break;
case 't':
title = optarg;
break;
case 'w':
pageWidth = atof(optarg);
break;
case 'z':
PSavoiddeadzone = FALSE;
break;
case '1':
level2 = FALSE;
level3 = FALSE;
ascii85 = FALSE;
break;
case '2':
level2 = TRUE;
ascii85 = TRUE; /* default to yes */
break;
case '3':
level3 = TRUE;
ascii85 = TRUE; /* default to yes */
break;
case '8':
ascii85 = FALSE;
break;
case 'x':
res_unit = RESUNIT_CENTIMETER;
break;
case 'y':
res_unit = RESUNIT_INCH;
break;
case '?':
usage(EXIT_FAILURE);
}
if (useImagemask == TRUE)
{
if ((level2 == FALSE) && (level3 == FALSE))
{
TIFFError(
"-m ",
" imagemask operator requires Postscript Level2 or Level3");
exit(EXIT_FAILURE);
}
}
if (pageWidth && (maxPageWidth > pageWidth))
{
TIFFError("-W", "Max viewport width cannot exceed page width");
exit(EXIT_FAILURE);
}
/* auto rotate requires a specified page width and height */
if (auto_rotate == TRUE)
{
/*
if ((pageWidth == 0) || (pageHeight == 0))
TIFFWarning ("-r auto", " requires page height and width specified with
-h and -w");
*/
if ((maxPageWidth > 0) || (maxPageHeight > 0))
{
TIFFError("-r auto", " is incompatible with maximum page "
"width/height specified by -H or -W");
exit(EXIT_FAILURE);
}
}
if ((maxPageWidth > 0) && (maxPageHeight > 0))
{
TIFFError("-H and -W",
" Use only one of -H or -W to define a viewport");
exit(EXIT_FAILURE);
}
if ((generateEPSF == TRUE) && (printAll == TRUE))
{
TIFFError(" -e and -a", "Warning: Cannot generate Encapsulated "
"Postscript for multiple images");
generateEPSF = FALSE;
}
if ((generateEPSF == TRUE) && (PSduplex == TRUE))
{
TIFFError(
" -e and -D",
"Warning: Encapsulated Postscript does not support Duplex option");
PSduplex = FALSE;
}
if ((generateEPSF == TRUE) && (PStumble == TRUE))
{
TIFFError(" -e and -T", "Warning: Encapsulated Postscript does not "
"support Top Edge Binding option");
PStumble = FALSE;
}
if ((generateEPSF == TRUE) && (PSavoiddeadzone == TRUE))
PSavoiddeadzone = FALSE;
for (; argc - optind > 0; optind++)
{
TIFFOpenOptions *opts = TIFFOpenOptionsAlloc();
if (opts == NULL)
{
return EXIT_FAILURE;
}
TIFFOpenOptionsSetMaxSingleMemAlloc(opts, maxMalloc);
TIFF *tif = TIFFOpenExt(filename = argv[optind], "r", opts);
TIFFOpenOptionsFree(opts);
if (tif != NULL)
{
if (dirnum != -1 && !TIFFSetDirectory(tif, (tdir_t)dirnum))
{
TIFFClose(tif);
return (EXIT_FAILURE);
}
else if (diroff != 0 && !TIFFSetSubDirectory(tif, diroff))
{
TIFFClose(tif);
return (EXIT_FAILURE);
}
np = TIFF2PS(output, tif, pageWidth, pageHeight, leftmargin,
bottommargin, centered);
if (np < 0)
{
TIFFError("Error", "Unable to process %s", filename);
}
TIFFClose(tif);
}
}
if (np)
PSTail(output, np);
else
usage(EXIT_FAILURE);
if (output != stdout)
fclose(output);
return (EXIT_SUCCESS);
}
static uint16_t samplesperpixel;
static uint16_t bitspersample;
static uint16_t planarconfiguration;
static uint16_t photometric;
static uint16_t compression;
static uint16_t extrasamples;
static int alpha;
static int checkImage(TIFF *tif)
{
switch (photometric)
{
case PHOTOMETRIC_YCBCR:
if ((compression == COMPRESSION_JPEG ||
compression == COMPRESSION_OJPEG) &&
planarconfiguration == PLANARCONFIG_CONTIG)
{
/* can rely on libjpeg to convert to RGB */
TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
photometric = PHOTOMETRIC_RGB;
}
else
{
if (level2 || level3)
break;
TIFFError(filename, "Can not handle image with %s",
"PhotometricInterpretation=YCbCr");
return (0);
}
/* fall through... */
case PHOTOMETRIC_RGB:
if (alpha && bitspersample != 8)
{
TIFFError(filename,
"Can not handle %" PRIu16
"-bit/sample RGB image with alpha",
bitspersample);
return (0);
}
/* fall through... */
case PHOTOMETRIC_SEPARATED:
case PHOTOMETRIC_PALETTE:
case PHOTOMETRIC_MINISBLACK:
case PHOTOMETRIC_MINISWHITE:
break;
case PHOTOMETRIC_LOGL:
case PHOTOMETRIC_LOGLUV:
if (compression != COMPRESSION_SGILOG &&
compression != COMPRESSION_SGILOG24)
{
TIFFError(
filename,
"Can not handle %s data with compression other than SGILog",
(photometric == PHOTOMETRIC_LOGL) ? "LogL" : "LogLuv");
return (0);
}
/* rely on library to convert to RGB/greyscale */
TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
photometric = (photometric == PHOTOMETRIC_LOGL)
? PHOTOMETRIC_MINISBLACK
: PHOTOMETRIC_RGB;
bitspersample = 8;
break;
case PHOTOMETRIC_CIELAB:
/* fall through... */
default:
TIFFError(
filename,
"Can not handle image with PhotometricInterpretation=%" PRIu16,
photometric);
return (0);
}
switch (bitspersample)
{
case 1:
case 2:
case 4:
case 8:
case 16:
break;
default:
TIFFError(filename, "Can not handle %" PRIu16 "-bit/sample image",
bitspersample);
return (0);
}
if (planarconfiguration == PLANARCONFIG_SEPARATE && extrasamples > 0)
TIFFWarning(filename, "Ignoring extra samples");
return (1);
}
#define PS_UNIT_SIZE 72.0F
#define PSUNITS(npix, res) ((npix) * (PS_UNIT_SIZE / (res)))
static const 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_t w, uint32_t h, int bs, int nc,
const char *startline)
{
fprintf(fd, "%%ImageData: %" PRIu32 " %" PRIu32 " %" PRIu16 " %d 0 %d 2 \"",
w, h, bitspersample, nc, bs);
fprintf(fd, startline, nc);
fprintf(fd, "\"\n");
}
/* Convert pixel width and height pw, ph, to points pprw, pprh
* using image resolution and resolution units from TIFF tags.
* pw : image width in pixels
* ph : image height in pixels
* pprw : image width in PS units (72 dpi)
* pprh : image height in PS units (72 dpi)
*/
static void setupPageState(TIFF *tif, uint32_t *pw, uint32_t *ph, double *pprw,
double *pprh)
{
float xres = 0.0F, yres = 0.0F;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, pw);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, ph);
if (res_unit == 0) /* Not specified as command line option */
if (!TIFFGetFieldDefaulted(tif, TIFFTAG_RESOLUTIONUNIT, &res_unit))
res_unit = RESUNIT_INCH;
/*
* Calculate printable area.
*/
if (!TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xres) ||
fabs(xres) < 0.0000001)
xres = PS_UNIT_SIZE;
if (!TIFFGetField(tif, TIFFTAG_YRESOLUTION, &yres) ||
fabs(yres) < 0.0000001)
yres = PS_UNIT_SIZE;
switch (res_unit)
{
case RESUNIT_CENTIMETER:
xres *= 2.54F, yres *= 2.54F;
break;
case RESUNIT_INCH:
break;
case RESUNIT_NONE: /* Subsequent code assumes we have converted to
inches! */
res_unit = RESUNIT_INCH;
break;
default: /* Last ditch guess for unspecified RESUNIT case
* check that the resolution is not inches before scaling it.
* Moved to end of function with additional check, RJN,
* 08-31-2010 if (xres != PS_UNIT_SIZE || yres != PS_UNIT_SIZE)
* xres *= PS_UNIT_SIZE, yres *= PS_UNIT_SIZE;
*/
break;
}
/* This is a hack to deal with images that have no meaningful Resolution
* Size but may have x and/or y resolutions of 1 pixel per undefined unit.
*/
if ((xres > 1.0) && (xres != PS_UNIT_SIZE))
*pprw = PSUNITS(*pw, xres);
else
*pprw = PSUNITS(*pw, PS_UNIT_SIZE);
if ((yres > 1.0) && (yres != PS_UNIT_SIZE))
*pprh = PSUNITS(*ph, yres);
else
*pprh = PSUNITS(*ph, PS_UNIT_SIZE);
}
static int isCCITTCompression(TIFF *tif)
{
uint16_t 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 uint32_t tf_rowsperstrip;
static uint32_t tf_numberstrips;
static char *hex = "0123456789abcdef";
/*
* Pagewidth and pageheight are the output size in points,
* may refer to values specified with -h and -w, or to
* values read from the image if neither -h nor -w are used.
* Imagewidth and imageheight are image size in points.
* Ximages and Yimages are number of pages across and down.
* Only one of maxPageHeight or maxPageWidth can be used.
* These are global variables unfortunately.
*/
int get_subimage_count(double pagewidth, double pageheight, double imagewidth,
double imageheight, int *ximages, int *yimages,
int rotation, double scale)
{
int pages = 1;
double splitheight = 0; /* Requested Max Height in points */
double splitwidth = 0; /* Requested Max Width in points */
double overlap = 0; /* Repeated edge width in points */
splitheight = maxPageHeight * PS_UNIT_SIZE;
splitwidth = maxPageWidth * PS_UNIT_SIZE;
overlap = splitOverlap * PS_UNIT_SIZE;
pagewidth *= PS_UNIT_SIZE;
pageheight *= PS_UNIT_SIZE;
if ((imagewidth < 1.0) || (imageheight < 1.0))
{
TIFFError("get_subimage_count", "Invalid image width or height");
return (0);
}
switch (rotation)
{
case 0:
case 180:
if (splitheight > 0) /* -H maxPageHeight */
{
if (imageheight >
splitheight) /* More than one vertical image segment */
{
if (pagewidth)
*ximages = (int)ceil((scale * imagewidth) /
(pagewidth - overlap));
else
*ximages = 1;
*yimages = (int)ceil(
(scale * imageheight) /
(splitheight - overlap)); /* Max vert pages needed */
}
else
{
if (pagewidth)
*ximages = (int)ceil(
(scale * imagewidth) /
(pagewidth - overlap)); /* Max horz pages needed */
else
*ximages = 1;
*yimages = 1; /* Max vert pages needed */
}
}
else
{
if (splitwidth > 0) /* -W maxPageWidth */
{
if (imagewidth > splitwidth)
{
*ximages = (int)ceil(
(scale * imagewidth) /
(splitwidth - overlap)); /* Max horz pages needed */
if (pageheight)
*yimages = (int)ceil(
(scale * imageheight) /
(pageheight -
overlap)); /* Max vert pages needed */
else
*yimages = 1;
}
else
{
*ximages = 1; /* Max vert pages needed */
if (pageheight)
*yimages = (int)ceil(
(scale * imageheight) /
(pageheight -
overlap)); /* Max vert pages needed */
else
*yimages = 1;
}
}
else
{
*ximages = 1;
*yimages = 1;
}
}
break;
case 90:
case 270:
if (splitheight > 0) /* -H maxPageHeight */
{
if (imagewidth >
splitheight) /* More than one vertical image segment */
{
*yimages = (int)ceil(
(scale * imagewidth) /
(splitheight - overlap)); /* Max vert pages needed */
if (pagewidth)
*ximages = (int)ceil(
(scale * imageheight) /
(pagewidth - overlap)); /* Max horz pages needed */
else
*ximages = 1;
}
else
{
*yimages = 1; /* Max vert pages needed */
if (pagewidth)
*ximages = (int)ceil(
(scale * imageheight) /
(pagewidth - overlap)); /* Max horz pages needed */
else
*ximages = 1;
}
}
else
{
if (splitwidth > 0) /* -W maxPageWidth */
{
if (imageheight > splitwidth)
{
if (pageheight)
*yimages = (int)ceil(
(scale * imagewidth) /
(pageheight -
overlap)); /* Max vert pages needed */
else
*yimages = 1;
*ximages = (int)ceil(
(scale * imageheight) /
(splitwidth - overlap)); /* Max horz pages needed */
}
else
{
if (pageheight)
*yimages = (int)ceil(
(scale * imagewidth) /
(pageheight -
overlap)); /* Max horz pages needed */
else
*yimages = 1;
*ximages = 1; /* Max vert pages needed */
}
}
else
{
*ximages = 1;
*yimages = 1;
}
}
break;
default:
*ximages = 1;
*yimages = 1;
}
pages = (*ximages) * (*yimages);
return (pages);
}
/* New version of PlaceImage that handles only the translation and rotation
* for a single output page.
*/
int exportMaskedImage(FILE *fp, double pagewidth, double pageheight,
double imagewidth, double imageheight, int row,
int column, double left_offset, double bott_offset,
double scale, int center, int rotation)
{
double xtran = 0.0;
double ytran = 0.0;
double xscale = 1.0;
double yscale = 1.0;
double splitheight = 0; /* Requested Max Height in points */
double splitwidth = 0; /* Requested Max Width in points */
double overlap = 0; /* Repeated edge width in points */
double subimage_height = 0.0;
splitheight = maxPageHeight * PS_UNIT_SIZE;
splitwidth = maxPageWidth * PS_UNIT_SIZE;
overlap = splitOverlap * PS_UNIT_SIZE;
xscale = scale * imagewidth;
yscale = scale * imageheight;
if ((xscale < 0.0) || (yscale < 0.0))
{
TIFFError("exportMaskedImage", "Invalid parameters.");
return (-1);
}
/* If images are cropped to a vewport with -H or -W, the output pages are
* shifted to the top of each output page rather than the Postscript default
* lower edge.
*/
switch (rotation)
{
case 0:
case 180:
if (splitheight > 0) /* -H maxPageHeight */
{
if (splitheight <
imageheight) /* More than one vertical image segments */
{
/* Intra2net: Keep correct apspect ratio */
xscale = (imagewidth + overlap) *
(pageheight / splitheight) * scale;
xtran = -1.0 * column * (pagewidth - overlap);
subimage_height =
imageheight - ((splitheight - overlap) * row);
ytran = pageheight -
subimage_height * (pageheight / splitheight);
}
else /* Only one page in vertical direction */
{
xtran = -1.0 * column * (pagewidth - overlap);
ytran = splitheight - imageheight;
}
}
else
{
if (splitwidth > 0) /* maxPageWidth */
{
if (splitwidth < imagewidth)
{
xtran = -1.0 * column * splitwidth;
ytran = -1.0 * row * (pageheight - overlap);
}
else /* Only one page in horizontal direction */
{
ytran = -1.0 * row * (pageheight - overlap);
xtran = 0;
}
}