4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
27 #include "tif_config.h"
30 #include <stdlib.h> /* for atof */
49 * Richard Nolde: Reinstate code from Feb 2009 that never got
50 * accepted into CVS with major modifications to handle -H and -W
51 * options. Replaced original PlaceImage function with several
52 * new functions that make support for multiple output pages
53 * from a single image easier to understand. Added additional
54 * warning messages for incompatible command line options.
55 * Add new command line options to specify PageOrientation
56 * Document Structuring Comment for landscape or portrait
57 * and code to determine the values from ouput width and height
58 * if not specified on the command line.
59 * Add new command line option to specify document creator
60 * as an alterntive to the string "tiff2ps" following model
61 * of patch submitted by Thomas Jarosch for specifiying a
62 * document title which is also supported now.
65 * Richard Nolde: Added support for rotations of 90, 180, 270
66 * and auto using -r <90|180|270|auto>. Auto picks the best
67 * fit for the image on the specified paper size (eg portrait
68 * or landscape) if -h or -w is specified. Rotation is in
69 * degrees counterclockwise since that is how Postscript does
70 * it. The auto opption rotates the image 90 degrees ccw to
71 * produce landscape if that is a better fit than portait.
73 * Cleaned up code in TIFF2PS and broke into smaller functions
74 * to simplify rotations.
76 * Identified incompatible options and returned errors, eg
77 * -i for imagemask operator is only available for Level2 or
78 * Level3 Postscript in the current implmentation since there
79 * is a difference in the way the operands are called for Level1
80 * and there is no function to provide the Level1 version.
81 * -H was not handled properly if -h and/or -w were specified.
82 * It should only clip the masked images if the scaled image
83 * exceeds the maxPageHeight specified with -H.
85 * New design allows for all of the following combinations:
86 * Conversion of TIFF to Postscript with optional rotations
87 * of 90, 180, 270, or auto degrees counterclockwise
88 * Conversion of TIFF to Postscript with entire image scaled
89 * to maximum of values spedified with -h or -w while
90 * maintaining aspect ratio. Same rotations apply.
91 * Conversion of TIFF to Postscript with clipping of output
92 * viewport to height specified with -H, producing multiple
93 * pages at this height and original width as needed.
94 * Same rotations apply.
95 * Conversion of TIFF to Postscript with image scaled to
96 * maximum specified by -h and -w and the resulting scaled
97 * image is presented in an output viewport clipped by -H height.
98 * The same rotations apply.
100 * Added maxPageWidth option using -W flag. MaxPageHeight and
101 * MaxPageWidth are mutually exclusive since the aspect ratio
102 * cannot be maintained if you set both.
103 * Rewrote PlaceImage to allow maxPageHeight and maxPageWidth
104 * options to work with values smaller or larger than the
105 * physical paper size and still preserve the aspect ratio.
106 * This is accomplished by creating multiple pages across
107 * as well as down if need be.
110 * I (Bruce A. Mallett) added this revision history comment ;)
112 * Fixed PS_Lvl2page() code which outputs non-ASCII85 raw
113 * data. Moved test for when to output a line break to
114 * *after* the output of a character. This just serves
115 * to fix an eye-nuisance where the first line of raw
116 * data was one character shorter than subsequent lines.
118 * Added an experimental ASCII85 encoder which can be used
119 * only when there is a single buffer of bytes to be encoded.
120 * This version is much faster at encoding a straight-line
121 * buffer of data because it can avoid a lot of the loop
122 * overhead of the byte-by-byte version. To use this version
123 * you need to define EXP_ASCII85ENCODER (experimental ...).
125 * Added bug fix given by Michael Schmidt to PS_Lvl2page()
126 * in which an end-of-data marker ('>') was not being output
127 * when producing non-ASCII85 encoded PostScript Level 2
130 * Fixed PS_Lvl2colorspace() so that it no longer assumes that
131 * a TIFF having more than 2 planes is a CMYK. This routine
132 * no longer looks at the samples per pixel but instead looks
133 * at the "photometric" value. This change allows support of
136 * Modified the PostScript L2 imaging loop so as to test if
137 * the input stream is still open before attempting to do a
138 * flushfile on it. This was done because some RIPs close
139 * the stream after doing the image operation.
141 * Got rid of the realloc() being done inside a loop in the
142 * PSRawDataBW() routine. The code now walks through the
143 * byte-size array outside the loop to determine the largest
144 * size memory block that will be needed.
146 * Added "-m" switch to ask tiff2ps to, where possible, use the
147 * "imagemask" operator instead of the "image" operator.
149 * Added the "-i #" switch to allow interpolation to be disabled.
151 * Unrolled a loop or two to improve performance.
155 * Define EXP_ASCII85ENCODER if you want to use an experimental
156 * version of the ASCII85 encoding routine. The advantage of
157 * using this routine is that tiff2ps will convert to ASCII85
158 * encoding at between 3 and 4 times the speed as compared to
159 * using the old (non-experimental) encoder. The disadvantage
160 * is that you will be using a new (and unproven) encoding
161 * routine. So user beware, you have been warned!
164 #define EXP_ASCII85ENCODER
167 * NB: this code assumes uint32 works with printf's %l[ud].
174 int ascii85
= FALSE
; /* use ASCII85 encoding */
175 int interpolate
= TRUE
; /* interpolate level2 image */
176 int level2
= FALSE
; /* generate PostScript level 2 */
177 int level3
= FALSE
; /* generate PostScript level 3 */
178 int printAll
= FALSE
; /* print all images in file */
179 int generateEPSF
= TRUE
; /* generate Encapsulated PostScript */
180 int PSduplex
= FALSE
; /* enable duplex printing */
181 int PStumble
= FALSE
; /* enable top edge binding */
182 int PSavoiddeadzone
= TRUE
; /* enable avoiding printer deadzone */
183 double maxPageHeight
= 0; /* maximum height to select from image and print per page */
184 double maxPageWidth
= 0; /* maximum width to select from image and print per page */
185 double splitOverlap
= 0; /* amount for split pages to overlag */
186 int rotation
= 0; /* optional value for rotation angle */
187 int auto_rotate
= 0; /* rotate image for best fit on the page */
188 char *filename
= NULL
; /* input filename */
189 char *title
= NULL
; /* optional document title string */
190 char *creator
= NULL
; /* optional document creator string */
191 char pageOrientation
[12]; /* set optional PageOrientation DSC to Landscape or Portrait */
192 int useImagemask
= FALSE
; /* Use imagemask instead of image operator */
193 uint16 res_unit
= 0; /* Resolution units: 2 - inches, 3 - cm */
196 * ASCII85 Encoding Support.
198 unsigned char ascii85buf
[10];
202 int TIFF2PS(FILE*, TIFF
*, double, double, double, double, int);
203 void PSpage(FILE*, TIFF
*, uint32
, uint32
);
204 void PSColorContigPreamble(FILE*, uint32
, uint32
, int);
205 void PSColorSeparatePreamble(FILE*, uint32
, uint32
, int);
206 void PSDataColorContig(FILE*, TIFF
*, uint32
, uint32
, int);
207 void PSDataColorSeparate(FILE*, TIFF
*, uint32
, uint32
, int);
208 void PSDataPalette(FILE*, TIFF
*, uint32
, uint32
);
209 void PSDataBW(FILE*, TIFF
*, uint32
, uint32
);
210 void PSRawDataBW(FILE*, TIFF
*, uint32
, uint32
);
211 void Ascii85Init(void);
212 void Ascii85Put(unsigned char code
, FILE* fd
);
213 void Ascii85Flush(FILE* fd
);
214 void PSHead(FILE*, double, double, double, double);
215 void PSTail(FILE*, int);
216 int psStart(FILE *, int, int, int *, double *, double, double, double,
217 double, double, double, double, double, double, double);
218 int psPageSize(FILE *, int, double, double, double, double, double, double);
219 int psRotateImage(FILE *, int, double, double, double, double);
220 int psMaskImage(FILE *, TIFF
*, int, int, int *, double, double,
221 double, double, double, double, double, double, double);
222 int psScaleImage(FILE *, double, int, int, double, double, double, double,
224 int get_viewport (double, double, double, double, double *, double *, int);
225 int exportMaskedImage(FILE *, double, double, double, double, int, int,
226 double, double, double, int, int);
228 #if defined( EXP_ASCII85ENCODER)
229 tsize_t
Ascii85EncodeBlock( uint8
* ascii85_p
, unsigned f_eod
, const uint8
* raw_p
, tsize_t raw_l
);
232 static void usage(int);
235 main(int argc
, char* argv
[])
237 int dirnum
= -1, c
, np
= 0;
239 double bottommargin
= 0;
240 double leftmargin
= 0;
241 double pageWidth
= 0;
242 double pageHeight
= 0;
246 FILE* output
= stdout
;
248 pageOrientation
[0] = '\0';
250 while ((c
= getopt(argc
, argv
, "b:d:h:H:W:L:i:w:l:o:O:P:C:r:t:acemxyzps1238DT")) != -1)
253 bottommargin
= atof(optarg
);
261 case 'd': /* without -a, this only processes one image at this IFD */
262 dirnum
= atoi(optarg
);
268 interpolate
= atoi(optarg
) ? TRUE
:FALSE
;
274 PSavoiddeadzone
= FALSE
;
278 pageHeight
= atof(optarg
);
281 maxPageHeight
= atof(optarg
);
284 maxPageWidth
= atof(optarg
);
287 splitOverlap
= atof(optarg
);
304 case '9': diroff
= (uint32
) strtoul(optarg
, NULL
, 0);
306 default: TIFFError ("-o", "Offset must be a numeric value.");
310 case 'O': /* XXX too bad -o is already taken */
311 output
= fopen(optarg
, "w");
312 if (output
== NULL
) {
314 "%s: %s: Cannot open output file.\n",
323 case 'L': strcpy (pageOrientation
, "Landscape");
326 case 'P': strcpy (pageOrientation
, "Portrait");
328 default: TIFFError ("-P", "Page orientation must be Landscape or Portrait");
333 leftmargin
= atof(optarg
);
335 case 'a': /* removed fall through to generate warning below, R Nolde 09-01-2010 */
339 generateEPSF
= FALSE
;
342 if (strcmp (optarg
, "auto") == 0)
349 rotation
= atoi(optarg
);
360 fprintf (stderr
, "Rotation angle must be 90, 180, 270 (degrees ccw) or auto\n");
371 pageWidth
= atof(optarg
);
374 PSavoiddeadzone
= FALSE
;
383 ascii85
= TRUE
; /* default to yes */
387 ascii85
= TRUE
; /* default to yes */
393 res_unit
= RESUNIT_CENTIMETER
;
396 res_unit
= RESUNIT_INCH
;
402 if (useImagemask
== TRUE
)
404 if ((level2
== FALSE
) && (level3
== FALSE
))
406 TIFFError ("-m "," imagemask operator requres Postscript Level2 or Level3");
411 if (pageWidth
&& (maxPageWidth
> pageWidth
))
413 TIFFError ("-W", "Max viewport width cannot exceed page width");
417 if (pageHeight
&& (maxPageHeight
> pageHeight
))
419 TIFFError ("-H", "Max viewport height cannot exceed page height");
423 /* auto rotate requires a specified page width and height */
424 if (auto_rotate
== TRUE
)
426 if ((pageWidth
== 0) || (pageHeight
== 0))
427 TIFFWarning ("-r auto", " requires page height and width specified with -h and -w");
429 if ((maxPageWidth
> 0) || (maxPageHeight
> 0))
431 TIFFError ("-r auto", " is incompatible with maximum page width/height specified by -H or -W");
435 if ((maxPageWidth
> 0) && (maxPageHeight
> 0))
437 TIFFError ("-H and -W", " Use only one of -H or -W to define a viewport");
441 if ((generateEPSF
== TRUE
) && (printAll
== TRUE
))
443 TIFFError(" -e and -a", "Warning: Cannot generate Encapsulated Postscript for multiple images");
444 generateEPSF
= FALSE
;
447 if ((generateEPSF
== TRUE
) && (PSduplex
== TRUE
))
449 TIFFError(" -e and -D", "Warning: Encapsulated Postscript does not support Duplex option");
453 if ((generateEPSF
== TRUE
) && (PStumble
== TRUE
))
455 TIFFError(" -e and -T", "Warning: Encapsulated Postscript does not support Top Edge Binding option");
459 if ((generateEPSF
== TRUE
) && (PSavoiddeadzone
== TRUE
))
460 PSavoiddeadzone
= FALSE
;
462 for (; argc
- optind
> 0; optind
++) {
463 TIFF
* tif
= TIFFOpen(filename
= argv
[optind
], "r");
466 && !TIFFSetDirectory(tif
, (tdir_t
)dirnum
))
468 else if (diroff
!= 0 &&
469 !TIFFSetSubDirectory(tif
, diroff
))
471 np
= TIFF2PS(output
, tif
, pageWidth
, pageHeight
,
472 leftmargin
, bottommargin
, centered
);
475 TIFFError("Error", "Unable to process %s", filename
);
484 if (output
!= stdout
)
489 static uint16 samplesperpixel
;
490 static uint16 bitspersample
;
491 static uint16 planarconfiguration
;
492 static uint16 photometric
;
493 static uint16 compression
;
494 static uint16 extrasamples
;
498 checkImage(TIFF
* tif
)
500 switch (photometric
) {
501 case PHOTOMETRIC_YCBCR
:
502 if ((compression
== COMPRESSION_JPEG
|| compression
== COMPRESSION_OJPEG
)
503 && planarconfiguration
== PLANARCONFIG_CONTIG
) {
504 /* can rely on libjpeg to convert to RGB */
505 TIFFSetField(tif
, TIFFTAG_JPEGCOLORMODE
,
507 photometric
= PHOTOMETRIC_RGB
;
509 if (level2
|| level3
)
511 TIFFError(filename
, "Can not handle image with %s",
512 "PhotometricInterpretation=YCbCr");
516 case PHOTOMETRIC_RGB
:
517 if (alpha
&& bitspersample
!= 8) {
519 "Can not handle %d-bit/sample RGB image with alpha",
524 case PHOTOMETRIC_SEPARATED
:
525 case PHOTOMETRIC_PALETTE
:
526 case PHOTOMETRIC_MINISBLACK
:
527 case PHOTOMETRIC_MINISWHITE
:
529 case PHOTOMETRIC_LOGL
:
530 case PHOTOMETRIC_LOGLUV
:
531 if (compression
!= COMPRESSION_SGILOG
&&
532 compression
!= COMPRESSION_SGILOG24
) {
534 "Can not handle %s data with compression other than SGILog",
535 (photometric
== PHOTOMETRIC_LOGL
) ?
540 /* rely on library to convert to RGB/greyscale */
541 TIFFSetField(tif
, TIFFTAG_SGILOGDATAFMT
, SGILOGDATAFMT_8BIT
);
542 photometric
= (photometric
== PHOTOMETRIC_LOGL
) ?
543 PHOTOMETRIC_MINISBLACK
: PHOTOMETRIC_RGB
;
546 case PHOTOMETRIC_CIELAB
:
550 "Can not handle image with PhotometricInterpretation=%d",
554 switch (bitspersample
) {
560 TIFFError(filename
, "Can not handle %d-bit/sample image",
564 if (planarconfiguration
== PLANARCONFIG_SEPARATE
&& extrasamples
> 0)
565 TIFFWarning(filename
, "Ignoring extra samples");
569 #define PS_UNIT_SIZE 72.0F
570 #define PSUNITS(npix,res) ((npix) * (PS_UNIT_SIZE / (res)))
572 static char RGBcolorimage
[] = "\
575 dup length 3 idiv string 0 3 0\n\
577 add 2 1 roll 1 sub dup 0 eq {\n\
584 } { 2 1 roll } ifelse\n\
588 /colorimage where {pop} {\n\
589 /colorimage {pop pop /rgbproc exch def {bwproc} image} bind def\n\
594 * Adobe Photoshop requires a comment line of the form:
596 * %ImageData: <cols> <rows> <depth> <main channels> <pad channels>
597 * <block size> <1 for binary|2 for hex> "data start"
599 * It is claimed to be part of some future revision of the EPS spec.
602 PhotoshopBanner(FILE* fd
, uint32 w
, uint32 h
, int bs
, int nc
, char* startline
)
604 fprintf(fd
, "%%ImageData: %ld %ld %d %d 0 %d 2 \"",
605 (long) w
, (long) h
, bitspersample
, nc
, bs
);
606 fprintf(fd
, startline
, nc
);
610 /* Convert pixel width and height pw, ph, to points pprw, pprh
611 * using image resolution and resolution units from TIFF tags.
612 * pw : image width in pixels
613 * ph : image height in pixels
614 * pprw : image width in PS units (72 dpi)
615 * pprh : image height in PS units (72 dpi)
618 setupPageState(TIFF
* tif
, uint32
* pw
, uint32
* ph
, double* pprw
, double* pprh
)
620 float xres
= 0.0F
, yres
= 0.0F
;
622 TIFFGetField(tif
, TIFFTAG_IMAGEWIDTH
, pw
);
623 TIFFGetField(tif
, TIFFTAG_IMAGELENGTH
, ph
);
624 if (res_unit
== 0) /* Not specified as command line option */
625 if (!TIFFGetFieldDefaulted(tif
, TIFFTAG_RESOLUTIONUNIT
, &res_unit
))
626 res_unit
= RESUNIT_INCH
;
628 * Calculate printable area.
630 if (!TIFFGetField(tif
, TIFFTAG_XRESOLUTION
, &xres
)
631 || fabs(xres
) < 0.0000001)
633 if (!TIFFGetField(tif
, TIFFTAG_YRESOLUTION
, &yres
)
634 || fabs(yres
) < 0.0000001)
637 case RESUNIT_CENTIMETER
:
638 xres
*= 2.54F
, yres
*= 2.54F
;
642 case RESUNIT_NONE
: /* Subsequent code assumes we have converted to inches! */
643 res_unit
= RESUNIT_INCH
;
645 default: /* Last ditch guess for unspecified RESUNIT case
646 * check that the resolution is not inches before scaling it.
647 * Moved to end of function with additional check, RJN, 08-31-2010
648 * if (xres != PS_UNIT_SIZE || yres != PS_UNIT_SIZE)
649 * xres *= PS_UNIT_SIZE, yres *= PS_UNIT_SIZE;
653 /* This is a hack to deal with images that have no meaningful Resolution Size
654 * but may have x and/or y resolutions of 1 pixel per undefined unit.
656 if ((xres
> 1.0) && (xres
!= PS_UNIT_SIZE
))
657 *pprw
= PSUNITS(*pw
, xres
);
659 *pprw
= PSUNITS(*pw
, PS_UNIT_SIZE
);
660 if ((yres
> 1.0) && (yres
!= PS_UNIT_SIZE
))
661 *pprh
= PSUNITS(*ph
, yres
);
663 *pprh
= PSUNITS(*ph
, PS_UNIT_SIZE
);
667 isCCITTCompression(TIFF
* tif
)
670 TIFFGetField(tif
, TIFFTAG_COMPRESSION
, &compress
);
671 return (compress
== COMPRESSION_CCITTFAX3
||
672 compress
== COMPRESSION_CCITTFAX4
||
673 compress
== COMPRESSION_CCITTRLE
||
674 compress
== COMPRESSION_CCITTRLEW
);
677 static tsize_t tf_bytesperrow
;
678 static tsize_t ps_bytesperrow
;
679 static tsize_t tf_rowsperstrip
;
680 static tsize_t tf_numberstrips
;
681 static char *hex
= "0123456789abcdef";
684 * Pagewidth and pageheight are the output size in points,
685 * may refer to values specified with -h and -w, or to
686 * values read from the image if neither -h nor -w are used.
687 * Imagewidth and imageheight are image size in points.
688 * Ximages and Yimages are number of pages across and down.
689 * Only one of maxPageHeight or maxPageWidth can be used.
690 * These are global variables unfortunately.
692 int get_subimage_count(double pagewidth
, double pageheight
,
693 double imagewidth
, double imageheight
,
694 int *ximages
, int *yimages
,
695 int rotation
, double scale
)
698 double splitheight
= 0; /* Requested Max Height in points */
699 double splitwidth
= 0; /* Requested Max Width in points */
700 double overlap
= 0; /* Repeated edge width in points */
702 splitheight
= maxPageHeight
* PS_UNIT_SIZE
;
703 splitwidth
= maxPageWidth
* PS_UNIT_SIZE
;
704 overlap
= splitOverlap
* PS_UNIT_SIZE
;
705 pagewidth
*= PS_UNIT_SIZE
;
706 pageheight
*= PS_UNIT_SIZE
;
708 if ((imagewidth
< 1.0) || (imageheight
< 1.0))
710 TIFFError("get_subimage_count", "Invalid image width or height");
717 case 180: if (splitheight
> 0) /* -H maxPageHeight */
719 if (imageheight
> splitheight
) /* More than one vertical image segment */
722 *ximages
= (int)ceil((scale
* imagewidth
) / (pagewidth
- overlap
));
725 *yimages
= (int)ceil((scale
* imageheight
) / (splitheight
- overlap
)); /* Max vert pages needed */
730 *ximages
= (int)ceil((scale
* imagewidth
) / (pagewidth
- overlap
)); /* Max horz pages needed */
733 *yimages
= 1; /* Max vert pages needed */
738 if (splitwidth
> 0) /* -W maxPageWidth */
740 if (imagewidth
>splitwidth
)
742 *ximages
= (int)ceil((scale
* imagewidth
) / (splitwidth
- overlap
)); /* Max horz pages needed */
744 *yimages
= (int)ceil((scale
* imageheight
) / (pageheight
- overlap
)); /* Max vert pages needed */
750 *ximages
= 1; /* Max vert pages needed */
752 *yimages
= (int)ceil((scale
* imageheight
) / (pageheight
- overlap
)); /* Max vert pages needed */
765 case 270: if (splitheight
> 0) /* -H maxPageHeight */
767 if (imagewidth
> splitheight
) /* More than one vertical image segment */
769 *yimages
= (int)ceil((scale
* imagewidth
) / (splitheight
- overlap
)); /* Max vert pages needed */
771 *ximages
= (int)ceil((scale
* imageheight
) / (pagewidth
- overlap
)); /* Max horz pages needed */
777 *yimages
= 1; /* Max vert pages needed */
779 *ximages
= (int)ceil((scale
* imageheight
) / (pagewidth
- overlap
)); /* Max horz pages needed */
786 if (splitwidth
> 0) /* -W maxPageWidth */
788 if (imageheight
> splitwidth
)
791 *yimages
= (int)ceil((scale
* imagewidth
) / (pageheight
- overlap
)); /* Max vert pages needed */
794 *ximages
= (int)ceil((scale
* imageheight
) / (splitwidth
- overlap
)); /* Max horz pages needed */
799 *yimages
= (int)ceil((scale
* imagewidth
) / (pageheight
- overlap
)); /* Max horz pages needed */
802 *ximages
= 1; /* Max vert pages needed */
812 default: *ximages
= 1;
815 pages
= (*ximages
) * (*yimages
);
819 /* New version of PlaceImage that handles only the translation and rotation
820 * for a single output page.
822 int exportMaskedImage(FILE *fp
, double pagewidth
, double pageheight
,
823 double imagewidth
, double imageheight
,
825 double left_offset
, double bott_offset
,
826 double scale
, int center
, int rotation
)
834 double splitheight
= 0; /* Requested Max Height in points */
835 double splitwidth
= 0; /* Requested Max Width in points */
836 double overlap
= 0; /* Repeated edge width in points */
837 double subimage_height
= 0.0;
839 splitheight
= maxPageHeight
* PS_UNIT_SIZE
;
840 splitwidth
= maxPageWidth
* PS_UNIT_SIZE
;
841 overlap
= splitOverlap
* PS_UNIT_SIZE
;
842 xscale
= scale
* imagewidth
;
843 yscale
= scale
* imageheight
;
845 if ((xscale
< 0.0) || (yscale
< 0.0))
847 TIFFError("exportMaskedImage", "Invalid parameters.");
851 /* If images are cropped to a vewport with -H or -W, the output pages are shifted to
852 * the top of each output page rather than the Postscript default lower edge.
857 case 180: if (splitheight
> 0) /* -H maxPageHeight */
859 if (splitheight
< imageheight
) /* More than one vertical image segments */
861 xtran
= -1.0 * column
* (pagewidth
- overlap
);
862 subimage_height
= imageheight
- ((splitheight
- overlap
) * row
);
863 ytran
= pageheight
- subimage_height
* (pageheight
/ splitheight
);
865 else /* Only one page in vertical direction */
867 xtran
= -1.0 * column
* (pagewidth
- overlap
);
868 ytran
= splitheight
- imageheight
;
873 if (splitwidth
> 0) /* maxPageWidth */
875 if (splitwidth
< imagewidth
)
877 xtran
= -1.0 * column
* splitwidth
;
878 ytran
= -1.0 * row
* (pageheight
- overlap
);
880 else /* Only one page in horizontal direction */
882 ytran
= -1.0 * row
* (pageheight
- overlap
);
886 else /* Simple case, no splitting */
888 ytran
= pageheight
- imageheight
;
892 bott_offset
+= ytran
/ (center
? 2 : 1);
893 left_offset
+= xtran
/ (center
? 2 : 1);
896 case 270: if (splitheight
> 0) /* -H maxPageHeight */
898 if (splitheight
< imagewidth
) /* More than one vertical image segments */
900 xtran
= -1.0 * column
* (pageheight
- overlap
);
901 /* Commented code places image at bottom of page instead of top.
902 ytran = -1.0 * row * splitheight;
905 ytran
= -1.0 * (imagewidth
- splitheight
);
907 ytran
= -1.0 * (imagewidth
- (splitheight
- overlap
) * (row
+ 1));
909 else /* Only one page in vertical direction */
911 xtran
= -1.0 * column
* (pageheight
- overlap
);
912 ytran
= splitheight
- imagewidth
;
917 if (splitwidth
> 0) /* maxPageWidth */
919 if (splitwidth
< imageheight
)
921 xtran
= -1.0 * column
* splitwidth
;
922 ytran
= -1.0 * row
* (pagewidth
- overlap
);
924 else /* Only one page in horizontal direction */
926 ytran
= -1.0 * row
* (pagewidth
- overlap
);
930 else /* Simple case, no splitting */
932 ytran
= pageheight
- imageheight
;
933 xtran
= 0; /* pagewidth - imagewidth; */
936 bott_offset
+= ytran
/ (center
? 2 : 1);
937 left_offset
+= xtran
/ (center
? 2 : 1);
945 case 0: fprintf(fp
, "%f %f translate\n", left_offset
, bott_offset
);
946 fprintf(fp
, "%f %f scale\n", xscale
, yscale
);
948 case 180: fprintf(fp
, "%f %f translate\n", left_offset
, bott_offset
);
949 fprintf(fp
, "%f %f scale\n1 1 translate 180 rotate\n", xscale
, yscale
);
951 case 90: fprintf(fp
, "%f %f translate\n", left_offset
, bott_offset
);
952 fprintf(fp
, "%f %f scale\n1 0 translate 90 rotate\n", yscale
, xscale
);
954 case 270: fprintf(fp
, "%f %f translate\n", left_offset
, bott_offset
);
955 fprintf(fp
, "%f %f scale\n0 1 translate 270 rotate\n", yscale
, xscale
);
957 default: TIFFError ("exportMaskedImage", "Unsupported rotation angle %d. No rotation", rotation
);
958 fprintf( fp
, "%f %f scale\n", xscale
, yscale
);
965 /* Rotate an image without scaling or clipping */
966 int psRotateImage (FILE * fd
, int rotation
, double pswidth
, double psheight
,
967 double left_offset
, double bottom_offset
)
969 if ((left_offset
!= 0.0) || (bottom_offset
!= 0))
970 fprintf (fd
, "%f %f translate\n", left_offset
, bottom_offset
);
972 /* Exchange width and height for 90/270 rotations */
975 case 0: fprintf (fd
, "%f %f scale\n", pswidth
, psheight
);
977 case 90: fprintf (fd
, "%f %f scale\n1 0 translate 90 rotate\n", psheight
, pswidth
);
979 case 180: fprintf (fd
, "%f %f scale\n1 1 translate 180 rotate\n", pswidth
, psheight
);
981 case 270: fprintf (fd
, "%f %f scale\n0 1 translate 270 rotate\n", psheight
, pswidth
);
983 default: TIFFError ("psRotateImage", "Unsupported rotation %d.", rotation
);
984 fprintf( fd
, "%f %f scale\n", pswidth
, psheight
);
990 /* Scale and rotate an image to a single output page. */
991 int psScaleImage(FILE * fd
, double scale
, int rotation
, int center
,
992 double reqwidth
, double reqheight
, double pswidth
, double psheight
,
993 double left_offset
, double bottom_offset
)
995 double hcenter
= 0.0, vcenter
= 0.0;
997 /* Adjust offsets for centering */
1002 case 90: vcenter
= (reqheight
- pswidth
* scale
) / 2;
1003 hcenter
= (reqwidth
- psheight
* scale
) / 2;
1004 fprintf (fd
, "%f %f translate\n", hcenter
, vcenter
);
1005 fprintf (fd
, "%f %f scale\n1 0 translate 90 rotate\n", psheight
* scale
, pswidth
* scale
);
1007 case 180: hcenter
= (reqwidth
- pswidth
* scale
) / 2;
1008 vcenter
= (reqheight
- psheight
* scale
) / 2;
1009 fprintf (fd
, "%f %f translate\n", hcenter
, vcenter
);
1010 fprintf (fd
, "%f %f scale\n1 1 translate 180 rotate\n", pswidth
* scale
, psheight
* scale
);
1012 case 270: vcenter
= (reqheight
- pswidth
* scale
) / 2;
1013 hcenter
= (reqwidth
- psheight
* scale
) / 2;
1014 fprintf (fd
, "%f %f translate\n", hcenter
, vcenter
);
1015 fprintf (fd
, "%f %f scale\n0 1 translate 270 rotate\n", psheight
* scale
, pswidth
* scale
);
1018 default: hcenter
= (reqwidth
- pswidth
* scale
) / 2;
1019 vcenter
= (reqheight
- psheight
* scale
) / 2;
1020 fprintf (fd
, "%f %f translate\n", hcenter
, vcenter
);
1021 fprintf (fd
, "%f %f scale\n", pswidth
* scale
, psheight
* scale
);
1025 else /* Not centered */
1029 case 0: fprintf (fd
, "%f %f translate\n", left_offset
? left_offset
: 0.0,
1030 bottom_offset
? bottom_offset
: reqheight
- (psheight
* scale
));
1031 fprintf (fd
, "%f %f scale\n", pswidth
* scale
, psheight
* scale
);
1033 case 90: fprintf (fd
, "%f %f translate\n", left_offset
? left_offset
: 0.0,
1034 bottom_offset
? bottom_offset
: reqheight
- (pswidth
* scale
));
1035 fprintf (fd
, "%f %f scale\n1 0 translate 90 rotate\n", psheight
* scale
, pswidth
* scale
);
1037 case 180: fprintf (fd
, "%f %f translate\n", left_offset
? left_offset
: 0.0,
1038 bottom_offset
? bottom_offset
: reqheight
- (psheight
* scale
));
1039 fprintf (fd
, "%f %f scale\n1 1 translate 180 rotate\n", pswidth
* scale
, psheight
* scale
);
1041 case 270: fprintf (fd
, "%f %f translate\n", left_offset
? left_offset
: 0.0,
1042 bottom_offset
? bottom_offset
: reqheight
- (pswidth
* scale
));
1043 fprintf (fd
, "%f %f scale\n0 1 translate 270 rotate\n", psheight
* scale
, pswidth
* scale
);
1045 default: TIFFError ("psScaleImage", "Unsupported rotation %d", rotation
);
1046 fprintf (fd
, "%f %f scale\n", pswidth
* scale
, psheight
* scale
);
1054 /* This controls the visible portion of the page which is displayed.
1055 * N.B. Setting maxPageHeight no longer sets pageheight if not set explicitly
1057 int psPageSize (FILE * fd
, int rotation
, double pgwidth
, double pgheight
,
1058 double reqwidth
, double reqheight
, double pswidth
, double psheight
)
1060 double xscale
= 1.0, yscale
= 1.0, scale
= 1.0;
1066 splitheight
= maxPageHeight
* PS_UNIT_SIZE
;
1067 splitwidth
= maxPageWidth
* PS_UNIT_SIZE
;
1072 case 180: if ((splitheight
> 0) || (splitwidth
> 0))
1074 if (pgwidth
!= 0 || pgheight
!= 0)
1076 xscale
= reqwidth
/ (splitwidth
? splitwidth
: pswidth
);
1077 yscale
= reqheight
/ (splitheight
? splitheight
: psheight
);
1078 scale
= (xscale
< yscale
) ? xscale
: yscale
;
1080 new_width
= splitwidth
? splitwidth
: scale
* pswidth
;
1081 new_height
= splitheight
? splitheight
: scale
* psheight
;
1082 if (strlen(pageOrientation
))
1083 fprintf (fd
, "%%%%PageOrientation: %s\n", pageOrientation
);
1085 fprintf (fd
, "%%%%PageOrientation: %s\n", (new_width
> new_height
) ? "Landscape" : "Portrait");
1086 fprintf (fd
, "%%%%PageBoundingBox: 0 0 %ld %ld\n", (long)new_width
, (long)new_height
);
1087 fprintf (fd
, "1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n",
1088 new_width
, new_height
);
1090 else /* No viewport defined with -H or -W */
1092 if ((pgwidth
== 0) && (pgheight
== 0)) /* Image not scaled */
1094 if (strlen(pageOrientation
))
1095 fprintf (fd
, "%%%%PageOrientation: %s\n", pageOrientation
);
1097 fprintf (fd
, "%%%%PageOrientation: %s\n", (pswidth
> psheight
) ? "Landscape" : "Portrait");
1098 fprintf (fd
, "%%%%PageBoundingBox: 0 0 %ld %ld\n", (long)pswidth
, (long)psheight
);
1099 fprintf(fd
, "1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n",
1102 else /* Image scaled */
1104 if (strlen(pageOrientation
))
1105 fprintf (fd
, "%%%%PageOrientation: %s\n", pageOrientation
);
1107 fprintf (fd
, "%%%%PageOrientation: %s\n", (reqwidth
> reqheight
) ? "Landscape" : "Portrait");
1108 fprintf (fd
, "%%%%PageBoundingBox: 0 0 %ld %ld\n", (long)reqwidth
, (long)reqheight
);
1109 fprintf(fd
, "1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n",
1110 reqwidth
, reqheight
);
1115 case 270: if ((splitheight
> 0) || (splitwidth
> 0))
1117 if (pgwidth
!= 0 || pgheight
!= 0)
1119 xscale
= reqwidth
/ (splitwidth
? splitwidth
: pswidth
);
1120 yscale
= reqheight
/ (splitheight
? splitheight
: psheight
);
1121 scale
= (xscale
< yscale
) ? xscale
: yscale
;
1123 new_width
= splitwidth
? splitwidth
: scale
* psheight
;
1124 new_height
= splitheight
? splitheight
: scale
* pswidth
;
1126 if (strlen(pageOrientation
))
1127 fprintf (fd
, "%%%%PageOrientation: %s\n", pageOrientation
);
1129 fprintf (fd
, "%%%%PageOrientation: %s\n", (new_width
> new_height
) ? "Landscape" : "Portrait");
1130 fprintf (fd
, "%%%%PageBoundingBox: 0 0 %ld %ld\n", (long)new_width
, (long)new_height
);
1131 fprintf (fd
, "1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n",
1132 new_width
, new_height
);
1136 if ((pgwidth
== 0) && (pgheight
== 0)) /* Image not scaled */
1138 if (strlen(pageOrientation
))
1139 fprintf (fd
, "%%%%PageOrientation: %s\n", pageOrientation
);
1141 fprintf (fd
, "%%%%PageOrientation: %s\n", (psheight
> pswidth
) ? "Landscape" : "Portrait");
1142 fprintf (fd
, "%%%%PageBoundingBox: 0 0 %ld %ld\n", (long)psheight
, (long)pswidth
);
1143 fprintf(fd
, "1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n",
1146 else /* Image scaled */
1148 if (strlen(pageOrientation
))
1149 fprintf (fd
, "%%%%PageOrientation: %s\n", pageOrientation
);
1151 fprintf (fd
, "%%%%PageOrientation: %s\n", (reqwidth
> reqheight
) ? "Landscape" : "Portrait");
1152 fprintf (fd
, "%%%%PageBoundingBox: 0 0 %ld %ld\n", (long)reqwidth
, (long)reqheight
);
1153 fprintf(fd
, "1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n",
1154 reqwidth
, reqheight
);
1158 default: TIFFError ("psPageSize", "Invalid rotation %d", rotation
);
1161 fputs("<<\n /Policies <<\n /PageSize 3\n >>\n>> setpagedevice\n", fd
);
1164 } /* end psPageSize */
1166 /* Mask an image as a series of pages, each only showing a section defined
1167 * by the maxPageHeight or maxPageWidth options.
1169 int psMaskImage(FILE *fd
, TIFF
*tif
, int rotation
, int center
,
1170 int *npages
, double pixwidth
, double pixheight
,
1171 double left_margin
, double bottom_margin
,
1172 double pgwidth
, double pgheight
,
1173 double pswidth
, double psheight
, double scale
)
1176 int ximages
= 1, yimages
= 1;
1177 int pages
= *npages
;
1178 double view_width
= 0;
1179 double view_height
= 0;
1181 if (get_viewport (pgwidth
, pgheight
, pswidth
, psheight
, &view_width
, &view_height
, rotation
))
1183 TIFFError ("get_viewport", "Unable to set image viewport");
1187 if (get_subimage_count(pgwidth
, pgheight
, pswidth
, psheight
,
1188 &ximages
, &yimages
, rotation
, scale
) < 1)
1190 TIFFError("get_subimage_count", "Invalid image count: %d columns, %d rows", ximages
, yimages
);
1194 for (i
= 0; i
< yimages
; i
++)
1196 for (j
= 0; j
< ximages
; j
++)
1200 fprintf(fd
, "%%%%Page: %d %d\n", pages
, pages
);
1202 /* Write out the PageSize info for non EPS files */
1203 if (!generateEPSF
&& ( level2
|| level3
))
1205 if (psPageSize(fd
, rotation
, pgwidth
, pgheight
,
1206 view_width
, view_height
, pswidth
, psheight
))
1209 fprintf(fd
, "gsave\n");
1210 fprintf(fd
, "100 dict begin\n");
1211 if (exportMaskedImage(fd
, view_width
, view_height
, pswidth
, psheight
,
1212 i
, j
, left_margin
, bottom_margin
,
1213 scale
, center
, rotation
))
1215 TIFFError("exportMaskedImage", "Invalid image parameters.");
1218 PSpage(fd
, tif
, pixwidth
, pixheight
);
1219 fprintf(fd
, "end\n");
1220 fprintf(fd
, "grestore\n");
1221 fprintf(fd
, "showpage\n");
1228 /* Compute scale factor and write out file header */
1229 int psStart(FILE *fd
, int npages
, int auto_rotate
, int *rotation
, double *scale
,
1230 double ox
, double oy
, double pgwidth
, double pgheight
,
1231 double reqwidth
, double reqheight
, double pswidth
, double psheight
,
1232 double left_offset
, double bottom_offset
)
1234 double maxsource
= 0.0; /* Used for auto rotations */
1235 double maxtarget
= 0.0;
1236 double xscale
= 1.0, yscale
= 1.0;
1239 double view_width
= 0.0, view_height
= 0.0;
1240 double page_width
= 0.0, page_height
= 0.0;
1242 /* Splitheight and splitwidth are in inches */
1243 splitheight
= maxPageHeight
* PS_UNIT_SIZE
;
1244 splitwidth
= maxPageWidth
* PS_UNIT_SIZE
;
1246 page_width
= pgwidth
* PS_UNIT_SIZE
;
1247 page_height
= pgheight
* PS_UNIT_SIZE
;
1249 /* If user has specified a page width and height and requested the
1250 * image to be auto-rotated to fit on that media, we match the
1251 * longest dimension of the image to the longest dimension of the
1252 * target media but we have to ignore auto rotate if user specified
1253 * maxPageHeight since this makes life way too complicated. */
1256 if ((splitheight
!= 0) || (splitwidth
!= 0))
1258 TIFFError ("psStart", "Auto-rotate is incompatible with page splitting ");
1262 /* Find longest edges in image and output media */
1263 maxsource
= (pswidth
>= psheight
) ? pswidth
: psheight
;
1264 maxtarget
= (reqwidth
>= reqheight
) ? reqwidth
: reqheight
;
1266 if (((maxsource
== pswidth
) && (maxtarget
!= reqwidth
)) ||
1267 ((maxsource
== psheight
) && (maxtarget
!= reqheight
)))
1268 { /* optimal orientaion does not match input orientation */
1270 xscale
= (reqwidth
- left_offset
)/psheight
;
1271 yscale
= (reqheight
- bottom_offset
)/pswidth
;
1273 else /* optimal orientaion matches input orientation */
1275 xscale
= (reqwidth
- left_offset
)/pswidth
;
1276 yscale
= (reqheight
- bottom_offset
)/psheight
;
1278 *scale
= (xscale
< yscale
) ? xscale
: yscale
;
1280 /* Do not scale image beyound original size */
1284 /* Set the size of the displayed image to requested page size
1285 * and optimal orientation.
1288 PSHead(fd
, reqwidth
, reqheight
, ox
, oy
);
1293 /* N.B. If pgwidth or pgheight are set from maxPageHeight/Width,
1294 * we have a problem with the tests below under splitheight.
1297 switch (*rotation
) /* Auto rotate has NOT been specified */
1300 case 180: if ((splitheight
!= 0) || (splitwidth
!= 0))
1301 { /* Viewport clipped to maxPageHeight or maxPageWidth */
1302 if ((page_width
!= 0) || (page_height
!= 0)) /* Image scaled */
1304 xscale
= (reqwidth
- left_offset
) / (page_width
? page_width
: pswidth
);
1305 yscale
= (reqheight
- bottom_offset
) / (page_height
? page_height
: psheight
);
1306 *scale
= (xscale
< yscale
) ? xscale
: yscale
;
1312 else /* Image clipped but not scaled */
1315 view_width
= splitwidth
? splitwidth
: *scale
* pswidth
;
1316 view_height
= splitheight
? splitheight
: *scale
* psheight
;
1318 else /* Viewport not clipped to maxPageHeight or maxPageWidth */
1320 if ((page_width
!= 0) || (page_height
!= 0))
1321 { /* Image scaled */
1322 xscale
= (reqwidth
- left_offset
) / pswidth
;
1323 yscale
= (reqheight
- bottom_offset
) / psheight
;
1325 view_width
= reqwidth
;
1326 view_height
= reqheight
;
1329 { /* Image not scaled */
1330 xscale
= (pswidth
- left_offset
)/pswidth
;
1331 yscale
= (psheight
- bottom_offset
)/psheight
;
1333 view_width
= pswidth
;
1334 view_height
= psheight
;
1339 case 270: if ((splitheight
!= 0) || (splitwidth
!= 0))
1340 { /* Viewport clipped to maxPageHeight or maxPageWidth */
1341 if ((page_width
!= 0) || (page_height
!= 0)) /* Image scaled */
1343 xscale
= (reqwidth
- left_offset
)/ psheight
;
1344 yscale
= (reqheight
- bottom_offset
)/ pswidth
;
1345 *scale
= (xscale
< yscale
) ? xscale
: yscale
;
1351 else /* Image clipped but not scaled */
1353 view_width
= splitwidth
? splitwidth
: *scale
* psheight
;
1354 view_height
= splitheight
? splitheight
: *scale
* pswidth
;
1356 else /* Viewport not clipped to maxPageHeight or maxPageWidth */
1358 if ((page_width
!= 0) || (page_height
!= 0)) /* Image scaled */
1360 xscale
= (reqwidth
- left_offset
) / psheight
;
1361 yscale
= (reqheight
- bottom_offset
) / pswidth
;
1363 view_width
= reqwidth
;
1364 view_height
= reqheight
;
1368 xscale
= (pswidth
- left_offset
)/ psheight
;
1369 yscale
= (psheight
- bottom_offset
)/ pswidth
;
1371 view_width
= psheight
;
1372 view_height
= pswidth
;
1376 default: TIFFError ("psPageSize", "Invalid rotation %d", *rotation
);
1381 PSHead(fd
, (page_width
? page_width
: view_width
), (page_height
? page_height
: view_height
), ox
, oy
);
1383 *scale
= (xscale
< yscale
) ? xscale
: yscale
;
1390 int get_viewport (double pgwidth
, double pgheight
, double pswidth
, double psheight
,
1391 double *view_width
, double *view_height
, int rotation
)
1393 /* Only one of maxPageHeight or maxPageWidth can be specified */
1394 if (maxPageHeight
!= 0) /* Clip the viewport to maxPageHeight on each page */
1396 *view_height
= maxPageHeight
* PS_UNIT_SIZE
;
1398 * if (res_unit == RESUNIT_CENTIMETER)
1399 * *view_height /= 2.54F;
1404 if (pgheight
!= 0) /* User has set PageHeight with -h flag */
1406 *view_height
= pgheight
* PS_UNIT_SIZE
; /* Postscript size for Page Height in inches */
1407 /* if (res_unit == RESUNIT_CENTIMETER)
1408 * *view_height /= 2.54F;
1411 else /* If no width or height are specified, use the original size from image */
1416 case 180: *view_height
= psheight
;
1419 case 270: *view_height
= pswidth
;
1424 if (maxPageWidth
!= 0) /* Clip the viewport to maxPageWidth on each page */
1426 *view_width
= maxPageWidth
* PS_UNIT_SIZE
;
1427 /* if (res_unit == RESUNIT_CENTIMETER)
1428 * *view_width /= 2.54F;
1433 if (pgwidth
!= 0) /* User has set PageWidth with -w flag */
1435 *view_width
= pgwidth
* PS_UNIT_SIZE
; /* Postscript size for Page Width in inches */
1436 /* if (res_unit == RESUNIT_CENTIMETER)
1437 * *view_width /= 2.54F;
1440 else /* If no width or height are specified, use the original size from image */
1445 case 180: *view_width
= pswidth
;
1448 case 270: *view_width
= psheight
; /* (*view_height / psheight) * psheight; */
1456 /* pgwidth and pgheight specify page width and height in inches from -h and -w flags
1457 * lm and bm are the LeftMargin and BottomMargin in inches
1458 * center causes the image to be centered on the page if the paper size is
1459 * larger than the image size
1460 * returns the sequence number of the page processed or -1 on error
1463 int TIFF2PS(FILE* fd
, TIFF
* tif
, double pgwidth
, double pgheight
, double lm
, double bm
, int center
)
1465 uint32 pixwidth
= 0, pixheight
= 0; /* Image width and height in pixels */
1466 double ox
= 0.0, oy
= 0.0; /* Offset from current Postscript origin */
1467 double pswidth
, psheight
; /* Original raw image width and height in points */
1468 double view_width
, view_height
; /* Viewport width and height in points */
1470 double left_offset
= lm
* PS_UNIT_SIZE
;
1471 double bottom_offset
= bm
* PS_UNIT_SIZE
;
1474 static int npages
= 0;
1476 if (!TIFFGetField(tif
, TIFFTAG_XPOSITION
, &ox
))
1478 if (!TIFFGetField(tif
, TIFFTAG_YPOSITION
, &oy
))
1481 /* Consolidated all the tag information into one code segment, Richard Nolde */
1483 tf_numberstrips
= TIFFNumberOfStrips(tif
);
1484 TIFFGetFieldDefaulted(tif
, TIFFTAG_ROWSPERSTRIP
, &tf_rowsperstrip
);
1485 TIFFGetFieldDefaulted(tif
, TIFFTAG_BITSPERSAMPLE
, &bitspersample
);
1486 TIFFGetFieldDefaulted(tif
, TIFFTAG_SAMPLESPERPIXEL
, &samplesperpixel
);
1487 TIFFGetFieldDefaulted(tif
, TIFFTAG_PLANARCONFIG
, &planarconfiguration
);
1488 TIFFGetField(tif
, TIFFTAG_COMPRESSION
, &compression
);
1489 TIFFGetFieldDefaulted(tif
, TIFFTAG_EXTRASAMPLES
, &extrasamples
, &sampleinfo
);
1490 alpha
= (extrasamples
== 1 && sampleinfo
[0] == EXTRASAMPLE_ASSOCALPHA
);
1491 if (!TIFFGetField(tif
, TIFFTAG_PHOTOMETRIC
, &photometric
))
1493 switch (samplesperpixel
- extrasamples
)
1495 case 1: if (isCCITTCompression(tif
))
1496 photometric
= PHOTOMETRIC_MINISWHITE
;
1498 photometric
= PHOTOMETRIC_MINISBLACK
;
1500 case 3: photometric
= PHOTOMETRIC_RGB
;
1502 case 4: photometric
= PHOTOMETRIC_SEPARATED
;
1507 /* Read image tags for width and height in pixels pixwidth, pixheight,
1508 * and convert to points pswidth, psheight
1510 setupPageState(tif
, &pixwidth
, &pixheight
, &pswidth
, &psheight
);
1511 view_width
= pswidth
;
1512 view_height
= psheight
;
1514 if (get_viewport (pgwidth
, pgheight
, pswidth
, psheight
, &view_width
, &view_height
, rotation
))
1516 TIFFError("get_viewport", "Unable to set image viewport");
1520 /* Write the Postscript file header with Bounding Box and Page Size definitions */
1521 if (psStart(fd
, npages
, auto_rotate
, &rotation
, &scale
, ox
, oy
,
1522 pgwidth
, pgheight
, view_width
, view_height
, pswidth
, psheight
,
1523 left_offset
, bottom_offset
))
1526 if (checkImage(tif
)) /* Aborts if unsupported image parameters */
1528 tf_bytesperrow
= TIFFScanlineSize(tif
);
1530 /* Set viewport clipping and scaling options */
1531 if ((maxPageHeight
) || (maxPageWidth
) || (pgwidth
!= 0) || (pgheight
!= 0))
1533 if ((maxPageHeight
) || (maxPageWidth
)) /* used -H or -W option */
1535 if (psMaskImage(fd
, tif
, rotation
, center
, &npages
, pixwidth
, pixheight
,
1536 left_offset
, bottom_offset
, pgwidth
, pgheight
,
1537 pswidth
, psheight
, scale
) < 0)
1540 else /* N.B. Setting maxPageHeight no longer sets pgheight */
1542 if (pgwidth
!= 0 || pgheight
!= 0)
1544 /* User did not specify a maxium page height or width using -H or -W flag
1545 * but did use -h or -w flag to scale to a specific size page.
1548 fprintf(fd
, "%%%%Page: %d %d\n", npages
, npages
);
1550 if (!generateEPSF
&& ( level2
|| level3
))
1552 /* Write out the PageSize info for non EPS files */
1553 if (psPageSize(fd
, rotation
, pgwidth
, pgheight
,
1554 view_width
, view_height
, pswidth
, psheight
))
1557 fprintf(fd
, "gsave\n");
1558 fprintf(fd
, "100 dict begin\n");
1559 if (psScaleImage(fd
, scale
, rotation
, center
, view_width
, view_height
,
1560 pswidth
, psheight
, left_offset
, bottom_offset
))
1563 PSpage(fd
, tif
, pixwidth
, pixheight
);
1564 fprintf(fd
, "end\n");
1565 fprintf(fd
, "grestore\n");
1566 fprintf(fd
, "showpage\n");
1570 else /* Simple rotation: user did not use -H, -W, -h or -w */
1573 fprintf(fd
, "%%%%Page: %d %d\n", npages
, npages
);
1575 if (!generateEPSF
&& ( level2
|| level3
))
1577 /* Write out the PageSize info for non EPS files */
1578 if (psPageSize(fd
, rotation
, pgwidth
, pgheight
,
1579 view_width
, view_height
, pswidth
, psheight
))
1582 fprintf(fd
, "gsave\n");
1583 fprintf(fd
, "100 dict begin\n");
1584 if (psRotateImage(fd
, rotation
, pswidth
, psheight
, left_offset
, bottom_offset
))
1587 PSpage(fd
, tif
, pixwidth
, pixheight
);
1588 fprintf(fd
, "end\n");
1589 fprintf(fd
, "grestore\n");
1590 fprintf(fd
, "showpage\n");
1595 TIFFGetFieldDefaulted(tif
, TIFFTAG_SUBFILETYPE
, &subfiletype
);
1596 } while (((subfiletype
& FILETYPE_PAGE
) || printAll
) && TIFFReadDirectory(tif
));
1601 static char DuplexPreamble
[] = "\
1602 %%BeginFeature: *Duplex True\n\
1604 /languagelevel where { pop languagelevel } { 1 } ifelse\n\
1605 2 ge { 1 dict dup /Duplex true put setpagedevice }\n\
1606 { statusdict /setduplex known { statusdict begin setduplex true end } if\n\
1612 static char TumblePreamble
[] = "\
1613 %%BeginFeature: *Tumble True\n\
1615 /languagelevel where { pop languagelevel } { 1 } ifelse\n\
1616 2 ge { 1 dict dup /Tumble true put setpagedevice }\n\
1617 { statusdict /settumble known { statusdict begin true settumble end } if\n\
1623 static char AvoidDeadZonePreamble
[] = "\
1624 gsave newpath clippath pathbbox grestore\n\
1625 4 2 roll 2 copy translate\n\
1626 exch 3 1 roll sub 3 1 roll sub exch\n\
1627 currentpagedevice /PageSize get aload pop\n\
1628 exch 3 1 roll div 3 1 roll div abs exch abs\n\
1629 2 copy gt { exch } if pop\n\
1630 dup 1 lt { dup scale } { pop } ifelse\n\
1634 PSHead(FILE *fd
, double pagewidth
, double pageheight
, double xoff
, double yoff
)
1639 fprintf(fd
, "%%!PS-Adobe-3.0%s\n", generateEPSF
? " EPSF-3.0" : "");
1640 fprintf(fd
, "%%%%Creator: %s\n", creator
? creator
: "tiff2ps");
1641 fprintf(fd
, "%%%%Title: %s\n", title
? title
: filename
);
1642 fprintf(fd
, "%%%%CreationDate: %s", ctime(&t
));
1643 fprintf(fd
, "%%%%DocumentData: Clean7Bit\n");
1644 /* NB: should use PageBoundingBox for each page instead of BoundingBox *
1645 * PageBoundingBox DSC added in PSPageSize function, R Nolde 09-01-2010
1647 fprintf(fd
, "%%%%Origin: %ld %ld\n", (long) xoff
, (long) yoff
);
1648 fprintf(fd
, "%%%%BoundingBox: 0 0 %ld %ld\n",
1649 (long) ceil(pagewidth
), (long) ceil(pageheight
));
1651 fprintf(fd
, "%%%%LanguageLevel: %d\n", (level3
? 3 : (level2
? 2 : 1)));
1652 if (generateEPSF
== TRUE
)
1653 fprintf(fd
, "%%%%Pages: 1 1\n");
1655 fprintf(fd
, "%%%%Pages: (atend)\n");
1656 fprintf(fd
, "%%%%EndComments\n");
1657 if (generateEPSF
== FALSE
)
1659 fprintf(fd
, "%%%%BeginSetup\n");
1661 fprintf(fd
, "%s", DuplexPreamble
);
1663 fprintf(fd
, "%s", TumblePreamble
);
1664 if (PSavoiddeadzone
&& (level2
|| level3
))
1665 fprintf(fd
, "%s", AvoidDeadZonePreamble
);
1666 fprintf(fd
, "%%%%EndSetup\n");
1671 PSTail(FILE *fd
, int npages
)
1673 fprintf(fd
, "%%%%Trailer\n");
1674 if (generateEPSF
== FALSE
)
1675 fprintf(fd
, "%%%%Pages: %d\n", npages
);
1676 fprintf(fd
, "%%%%EOF\n");
1680 checkcmap(TIFF
* tif
, int n
, uint16
* r
, uint16
* g
, uint16
* b
)
1684 if (*r
++ >= 256 || *g
++ >= 256 || *b
++ >= 256)
1686 TIFFWarning(filename
, "Assuming 8-bit colormap");
1691 PS_Lvl2colorspace(FILE* fd
, TIFF
* tif
)
1693 uint16
*rmap
, *gmap
, *bmap
;
1695 const char * colorspace_p
;
1697 switch ( photometric
)
1699 case PHOTOMETRIC_SEPARATED
:
1700 colorspace_p
= "CMYK";
1703 case PHOTOMETRIC_RGB
:
1704 colorspace_p
= "RGB";
1708 colorspace_p
= "Gray";
1712 * Set up PostScript Level 2 colorspace according to
1713 * section 4.8 in the PostScript refenence manual.
1715 fputs("% PostScript Level 2 only.\n", fd
);
1716 if (photometric
!= PHOTOMETRIC_PALETTE
) {
1717 if (photometric
== PHOTOMETRIC_YCBCR
) {
1718 /* MORE CODE HERE */
1720 fprintf(fd
, "/Device%s setcolorspace\n", colorspace_p
);
1725 * Set up an indexed/palette colorspace
1727 num_colors
= (1 << bitspersample
);
1728 if (!TIFFGetField(tif
, TIFFTAG_COLORMAP
, &rmap
, &gmap
, &bmap
)) {
1730 "Palette image w/o \"Colormap\" tag");
1733 if (checkcmap(tif
, num_colors
, rmap
, gmap
, bmap
) == 16) {
1735 * Convert colormap to 8-bits values.
1737 #define CVT(x) (((x) * 255) / ((1L<<16)-1))
1738 for (i
= 0; i
< num_colors
; i
++) {
1739 rmap
[i
] = CVT(rmap
[i
]);
1740 gmap
[i
] = CVT(gmap
[i
]);
1741 bmap
[i
] = CVT(bmap
[i
]);
1745 fprintf(fd
, "[ /Indexed /DeviceRGB %d", num_colors
- 1);
1749 ascii85breaklen
-= 2;
1752 for (i
= 0; i
< num_colors
; i
++) {
1754 Ascii85Put((unsigned char)rmap
[i
], fd
);
1755 Ascii85Put((unsigned char)gmap
[i
], fd
);
1756 Ascii85Put((unsigned char)bmap
[i
], fd
);
1758 fputs((i
% 8) ? " " : "\n ", fd
);
1759 fprintf(fd
, "%02x%02x%02x",
1760 rmap
[i
], gmap
[i
], bmap
[i
]);
1767 fputs("] setcolorspace\n", fd
);
1771 PS_Lvl2ImageDict(FILE* fd
, TIFF
* tif
, uint32 w
, uint32 h
)
1774 uint32 tile_width
, tile_height
;
1775 uint16 predictor
, minsamplevalue
, maxsamplevalue
;
1777 char im_h
[64], im_x
[64], im_y
[64];
1778 char * imageOp
= "image";
1780 if ( useImagemask
&& (bitspersample
== 1) )
1781 imageOp
= "imagemask";
1783 (void)strcpy(im_x
, "0");
1784 (void)sprintf(im_y
, "%lu", (long) h
);
1785 (void)sprintf(im_h
, "%lu", (long) h
);
1788 if (TIFFIsTiled(tif
)) {
1789 repeat_count
= TIFFNumberOfTiles(tif
);
1790 TIFFGetField(tif
, TIFFTAG_TILEWIDTH
, &tile_width
);
1791 TIFFGetField(tif
, TIFFTAG_TILELENGTH
, &tile_height
);
1792 if (tile_width
> w
|| tile_height
> h
||
1793 (w
% tile_width
) != 0 || (h
% tile_height
!= 0)) {
1795 * The tiles does not fit image width and height.
1796 * Set up a clip rectangle for the image unit square.
1798 fputs("0 0 1 1 rectclip\n", fd
);
1800 if (tile_width
< w
) {
1801 fputs("/im_x 0 def\n", fd
);
1802 (void)strcpy(im_x
, "im_x neg");
1804 if (tile_height
< h
) {
1805 fputs("/im_y 0 def\n", fd
);
1806 (void)sprintf(im_y
, "%lu im_y sub", (unsigned long) h
);
1809 repeat_count
= tf_numberstrips
;
1810 tile_height
= tf_rowsperstrip
;
1811 if (tile_height
> h
)
1813 if (repeat_count
> 1) {
1814 fputs("/im_y 0 def\n", fd
);
1815 fprintf(fd
, "/im_h %lu def\n",
1816 (unsigned long) tile_height
);
1817 (void)strcpy(im_h
, "im_h");
1818 (void)sprintf(im_y
, "%lu im_y sub", (unsigned long) h
);
1823 * Output start of exec block
1825 fputs("{ % exec\n", fd
);
1827 if (repeat_count
> 1)
1828 fprintf(fd
, "%d { %% repeat\n", repeat_count
);
1831 * Output filter options and image dictionary.
1834 fputs(" /im_stream currentfile /ASCII85Decode filter def\n",
1837 fputs(" /ImageType 1\n", fd
);
1838 fprintf(fd
, " /Width %lu\n", (unsigned long) tile_width
);
1840 * Workaround for some software that may crash when last strip
1841 * of image contains fewer number of scanlines than specified
1842 * by the `/Height' variable. So for stripped images with multiple
1843 * strips we will set `/Height' as `im_h', because one is
1844 * recalculated for each strip - including the (smaller) final strip.
1845 * For tiled images and images with only one strip `/Height' will
1846 * contain number of scanlines in tile (or image height in case of
1847 * one-stripped image).
1849 if (TIFFIsTiled(tif
) || tf_numberstrips
== 1)
1850 fprintf(fd
, " /Height %lu\n", (unsigned long) tile_height
);
1852 fprintf(fd
, " /Height im_h\n");
1854 if (planarconfiguration
== PLANARCONFIG_SEPARATE
&& samplesperpixel
> 1)
1855 fputs(" /MultipleDataSources true\n", fd
);
1856 fprintf(fd
, " /ImageMatrix [ %lu 0 0 %ld %s %s ]\n",
1857 (unsigned long) w
, - (long)h
, im_x
, im_y
);
1858 fprintf(fd
, " /BitsPerComponent %d\n", bitspersample
);
1859 fprintf(fd
, " /Interpolate %s\n", interpolate
? "true" : "false");
1861 switch (samplesperpixel
- extrasamples
) {
1863 switch (photometric
) {
1864 case PHOTOMETRIC_MINISBLACK
:
1865 fputs(" /Decode [0 1]\n", fd
);
1867 case PHOTOMETRIC_MINISWHITE
:
1868 switch (compression
) {
1869 case COMPRESSION_CCITTRLE
:
1870 case COMPRESSION_CCITTRLEW
:
1871 case COMPRESSION_CCITTFAX3
:
1872 case COMPRESSION_CCITTFAX4
:
1874 * Manage inverting with /Blackis1 flag
1875 * since there migth be uncompressed parts
1877 fputs(" /Decode [0 1]\n", fd
);
1883 fputs(" /Decode [1 0]\n", fd
);
1887 case PHOTOMETRIC_PALETTE
:
1888 TIFFGetFieldDefaulted(tif
, TIFFTAG_MINSAMPLEVALUE
,
1890 TIFFGetFieldDefaulted(tif
, TIFFTAG_MAXSAMPLEVALUE
,
1892 fprintf(fd
, " /Decode [%u %u]\n",
1893 minsamplevalue
, maxsamplevalue
);
1899 fputs(" /Decode [0 1]\n", fd
);
1904 switch (photometric
) {
1905 case PHOTOMETRIC_RGB
:
1906 fputs(" /Decode [0 1 0 1 0 1]\n", fd
);
1908 case PHOTOMETRIC_MINISWHITE
:
1909 case PHOTOMETRIC_MINISBLACK
:
1914 fputs(" /Decode [0 1 0 1 0 1]\n", fd
);
1922 fputs(" /Decode [0 1 0 1 0 1 0 1]\n", fd
);
1925 fputs(" /DataSource", fd
);
1926 if (planarconfiguration
== PLANARCONFIG_SEPARATE
&&
1927 samplesperpixel
> 1)
1930 fputs(" im_stream", fd
);
1932 fputs(" currentfile /ASCIIHexDecode filter", fd
);
1935 switch (compression
) {
1936 case COMPRESSION_NONE
: /* 1: uncompressed */
1938 case COMPRESSION_CCITTRLE
: /* 2: CCITT modified Huffman RLE */
1939 case COMPRESSION_CCITTRLEW
: /* 32771: #1 w/ word alignment */
1940 case COMPRESSION_CCITTFAX3
: /* 3: CCITT Group 3 fax encoding */
1941 case COMPRESSION_CCITTFAX4
: /* 4: CCITT Group 4 fax encoding */
1942 fputs("\n\t<<\n", fd
);
1943 if (compression
== COMPRESSION_CCITTFAX3
) {
1946 fputs("\t /EndOfLine true\n", fd
);
1947 fputs("\t /EndOfBlock false\n", fd
);
1948 if (!TIFFGetField(tif
, TIFFTAG_GROUP3OPTIONS
,
1951 if (g3_options
& GROUP3OPT_2DENCODING
)
1952 fprintf(fd
, "\t /K %s\n", im_h
);
1953 if (g3_options
& GROUP3OPT_UNCOMPRESSED
)
1954 fputs("\t /Uncompressed true\n", fd
);
1955 if (g3_options
& GROUP3OPT_FILLBITS
)
1956 fputs("\t /EncodedByteAlign true\n", fd
);
1958 if (compression
== COMPRESSION_CCITTFAX4
) {
1961 fputs("\t /K -1\n", fd
);
1962 TIFFGetFieldDefaulted(tif
, TIFFTAG_GROUP4OPTIONS
,
1964 if (g4_options
& GROUP4OPT_UNCOMPRESSED
)
1965 fputs("\t /Uncompressed true\n", fd
);
1967 if (!(tile_width
== w
&& w
== 1728U))
1968 fprintf(fd
, "\t /Columns %lu\n",
1969 (unsigned long) tile_width
);
1970 fprintf(fd
, "\t /Rows %s\n", im_h
);
1971 if (compression
== COMPRESSION_CCITTRLE
||
1972 compression
== COMPRESSION_CCITTRLEW
) {
1973 fputs("\t /EncodedByteAlign true\n", fd
);
1974 fputs("\t /EndOfBlock false\n", fd
);
1976 if (photometric
== PHOTOMETRIC_MINISBLACK
)
1977 fputs("\t /BlackIs1 true\n", fd
);
1978 fprintf(fd
, "\t>> /CCITTFaxDecode filter");
1980 case COMPRESSION_LZW
: /* 5: Lempel-Ziv & Welch */
1981 TIFFGetFieldDefaulted(tif
, TIFFTAG_PREDICTOR
, &predictor
);
1982 if (predictor
== 2) {
1983 fputs("\n\t<<\n", fd
);
1984 fprintf(fd
, "\t /Predictor %u\n", predictor
);
1985 fprintf(fd
, "\t /Columns %lu\n",
1986 (unsigned long) tile_width
);
1987 fprintf(fd
, "\t /Colors %u\n", samplesperpixel
);
1988 fprintf(fd
, "\t /BitsPerComponent %u\n",
1992 fputs(" /LZWDecode filter", fd
);
1994 case COMPRESSION_DEFLATE
: /* 5: ZIP */
1995 case COMPRESSION_ADOBE_DEFLATE
:
1997 TIFFGetFieldDefaulted(tif
, TIFFTAG_PREDICTOR
, &predictor
);
1998 if (predictor
> 1) {
1999 fprintf(fd
, "\t %% PostScript Level 3 only.");
2000 fputs("\n\t<<\n", fd
);
2001 fprintf(fd
, "\t /Predictor %u\n", predictor
);
2002 fprintf(fd
, "\t /Columns %lu\n",
2003 (unsigned long) tile_width
);
2004 fprintf(fd
, "\t /Colors %u\n", samplesperpixel
);
2005 fprintf(fd
, "\t /BitsPerComponent %u\n",
2009 fputs(" /FlateDecode filter", fd
);
2011 use_rawdata
= FALSE
;
2014 case COMPRESSION_PACKBITS
: /* 32773: Macintosh RLE */
2015 fputs(" /RunLengthDecode filter", fd
);
2018 case COMPRESSION_OJPEG
: /* 6: !6.0 JPEG */
2019 case COMPRESSION_JPEG
: /* 7: %JPEG DCT compression */
2022 * Code not tested yet
2024 fputs(" /DCTDecode filter", fd
);
2027 use_rawdata
= FALSE
;
2030 case COMPRESSION_NEXT
: /* 32766: NeXT 2-bit RLE */
2031 case COMPRESSION_THUNDERSCAN
: /* 32809: ThunderScan RLE */
2032 case COMPRESSION_PIXARFILM
: /* 32908: Pixar companded 10bit LZW */
2033 case COMPRESSION_JBIG
: /* 34661: ISO JBIG */
2034 use_rawdata
= FALSE
;
2036 case COMPRESSION_SGILOG
: /* 34676: SGI LogL or LogLuv */
2037 case COMPRESSION_SGILOG24
: /* 34677: SGI 24-bit LogLuv */
2038 use_rawdata
= FALSE
;
2044 use_rawdata
= FALSE
;
2047 if (planarconfiguration
== PLANARCONFIG_SEPARATE
&&
2048 samplesperpixel
> 1) {
2052 * NOTE: This code does not work yet...
2054 for (i
= 1; i
< samplesperpixel
; i
++)
2059 fprintf( fd
, "\n >> %s\n", imageOp
);
2061 fputs(" im_stream status { im_stream flushfile } if\n", fd
);
2062 if (repeat_count
> 1) {
2063 if (tile_width
< w
) {
2064 fprintf(fd
, " /im_x im_x %lu add def\n",
2065 (unsigned long) tile_width
);
2066 if (tile_height
< h
) {
2067 fprintf(fd
, " im_x %lu ge {\n",
2069 fputs(" /im_x 0 def\n", fd
);
2070 fprintf(fd
, " /im_y im_y %lu add def\n",
2071 (unsigned long) tile_height
);
2072 fputs(" } if\n", fd
);
2075 if (tile_height
< h
) {
2076 if (tile_width
>= w
) {
2077 fprintf(fd
, " /im_y im_y %lu add def\n",
2078 (unsigned long) tile_height
);
2079 if (!TIFFIsTiled(tif
)) {
2080 fprintf(fd
, " /im_h %lu im_y sub",
2082 fprintf(fd
, " dup %lu gt { pop",
2083 (unsigned long) tile_height
);
2084 fprintf(fd
, " %lu } if def\n",
2085 (unsigned long) tile_height
);
2089 fputs("} repeat\n", fd
);
2092 * End of exec function
2096 return(use_rawdata
);
2099 /* Flip the byte order of buffers with 16 bit samples */
2101 PS_FlipBytes(unsigned char* buf
, tsize_t count
)
2106 if (count
<= 0 || bitspersample
<= 8) {
2112 for (i
= 0; i
< count
; i
+= 2) {
2114 buf
[i
] = buf
[i
+ 1];
2122 PS_Lvl2page(FILE* fd
, TIFF
* tif
, uint32 w
, uint32 h
)
2125 int use_rawdata
, tiled_image
, breaklen
= MAXLINE
;
2126 uint32 chunk_no
, num_chunks
;
2128 unsigned char *buf_data
, *cp
;
2129 tsize_t chunk_size
, byte_count
;
2131 #if defined( EXP_ASCII85ENCODER )
2132 tsize_t ascii85_l
; /* Length, in bytes, of ascii85_p[] data */
2133 uint8
* ascii85_p
= 0; /* Holds ASCII85 encoded data */
2136 PS_Lvl2colorspace(fd
, tif
);
2137 use_rawdata
= PS_Lvl2ImageDict(fd
, tif
, w
, h
);
2139 /* See http://bugzilla.remotesensing.org/show_bug.cgi?id=80 */
2140 #ifdef ENABLE_BROKEN_BEGINENDDATA
2141 fputs("%%BeginData:\n", fd
);
2143 fputs("exec\n", fd
);
2145 tiled_image
= TIFFIsTiled(tif
);
2147 num_chunks
= TIFFNumberOfTiles(tif
);
2148 TIFFGetField(tif
, TIFFTAG_TILEBYTECOUNTS
, &bc
);
2150 num_chunks
= TIFFNumberOfStrips(tif
);
2151 TIFFGetField(tif
, TIFFTAG_STRIPBYTECOUNTS
, &bc
);
2155 chunk_size
= (tsize_t
) bc
[0];
2156 for (chunk_no
= 1; chunk_no
< num_chunks
; chunk_no
++)
2157 if ((tsize_t
) bc
[chunk_no
] > chunk_size
)
2158 chunk_size
= (tsize_t
) bc
[chunk_no
];
2161 chunk_size
= TIFFTileSize(tif
);
2163 chunk_size
= TIFFStripSize(tif
);
2165 buf_data
= (unsigned char *)_TIFFmalloc(chunk_size
);
2167 TIFFError(filename
, "Can't alloc %lu bytes for %s.",
2168 (unsigned long) chunk_size
, tiled_image
? "tiles" : "strips");
2172 #if defined( EXP_ASCII85ENCODER )
2175 * Allocate a buffer to hold the ASCII85 encoded data. Note
2176 * that it is allocated with sufficient room to hold the
2177 * encoded data (5*chunk_size/4) plus the EOD marker (+8)
2178 * and formatting line breaks. The line breaks are more
2179 * than taken care of by using 6*chunk_size/4 rather than
2183 ascii85_p
= _TIFFmalloc( (chunk_size
+(chunk_size
/2)) + 8 );
2186 _TIFFfree( buf_data
);
2188 TIFFError( filename
, "Cannot allocate ASCII85 encoding buffer." );
2194 TIFFGetFieldDefaulted(tif
, TIFFTAG_FILLORDER
, &fillorder
);
2195 for (chunk_no
= 0; chunk_no
< num_chunks
; chunk_no
++) {
2202 byte_count
= TIFFReadRawTile(tif
, chunk_no
,
2203 buf_data
, chunk_size
);
2205 byte_count
= TIFFReadRawStrip(tif
, chunk_no
,
2206 buf_data
, chunk_size
);
2207 if (fillorder
== FILLORDER_LSB2MSB
)
2208 TIFFReverseBits(buf_data
, byte_count
);
2211 byte_count
= TIFFReadEncodedTile(tif
,
2215 byte_count
= TIFFReadEncodedStrip(tif
,
2219 if (byte_count
< 0) {
2220 TIFFError(filename
, "Can't read %s %d.",
2221 tiled_image
? "tile" : "strip", chunk_no
);
2223 Ascii85Put('\0', fd
);
2226 * for 16 bits, the two bytes must be most significant
2229 if (bitspersample
== 16 && !TIFFIsBigEndian(tif
)) {
2230 PS_FlipBytes(buf_data
, byte_count
);
2233 * For images with alpha, matte against a white background;
2234 * i.e. Cback * (1 - Aimage) where Cback = 1. We will fill the
2235 * lower part of the buffer with the modified values.
2237 * XXX: needs better solution
2240 int adjust
, i
, j
= 0;
2241 int ncomps
= samplesperpixel
- extrasamples
;
2242 for (i
= 0; i
< byte_count
; i
+=samplesperpixel
) {
2243 adjust
= 255 - buf_data
[i
+ ncomps
];
2246 buf_data
[j
++] = buf_data
[i
] + adjust
;
2249 buf_data
[j
++] = buf_data
[i
] + adjust
;
2250 buf_data
[j
++] = buf_data
[i
+1] + adjust
;
2253 buf_data
[j
++] = buf_data
[i
] + adjust
;
2254 buf_data
[j
++] = buf_data
[i
+1] + adjust
;
2255 buf_data
[j
++] = buf_data
[i
+2] + adjust
;
2263 #if defined( EXP_ASCII85ENCODER )
2264 ascii85_l
= Ascii85EncodeBlock(ascii85_p
, 1, buf_data
, byte_count
);
2266 if ( ascii85_l
> 0 )
2267 fwrite( ascii85_p
, ascii85_l
, 1, fd
);
2269 for (cp
= buf_data
; byte_count
> 0; byte_count
--)
2270 Ascii85Put(*cp
++, fd
);
2275 for (cp
= buf_data
; byte_count
> 0; byte_count
--) {
2276 putc(hex
[((*cp
)>>4)&0xf], fd
);
2277 putc(hex
[(*cp
)&0xf], fd
);
2280 if (--breaklen
<= 0) {
2288 if ( level2
|| level3
)
2292 #if !defined( EXP_ASCII85ENCODER )
2298 #if defined( EXP_ASCII85ENCODER )
2300 _TIFFfree( ascii85_p
);
2303 _TIFFfree(buf_data
);
2304 #ifdef ENABLE_BROKEN_BEGINENDDATA
2305 fputs("%%EndData\n", fd
);
2311 PSpage(FILE* fd
, TIFF
* tif
, uint32 w
, uint32 h
)
2313 char * imageOp
= "image";
2315 if ( useImagemask
&& (bitspersample
== 1) )
2316 imageOp
= "imagemask";
2318 if ((level2
|| level3
) && PS_Lvl2page(fd
, tif
, w
, h
))
2320 ps_bytesperrow
= tf_bytesperrow
- (extrasamples
* bitspersample
/ 8)*w
;
2321 switch (photometric
) {
2322 case PHOTOMETRIC_RGB
:
2323 if (planarconfiguration
== PLANARCONFIG_CONTIG
) {
2324 fprintf(fd
, "%s", RGBcolorimage
);
2325 PSColorContigPreamble(fd
, w
, h
, 3);
2326 PSDataColorContig(fd
, tif
, w
, h
, 3);
2328 PSColorSeparatePreamble(fd
, w
, h
, 3);
2329 PSDataColorSeparate(fd
, tif
, w
, h
, 3);
2332 case PHOTOMETRIC_SEPARATED
:
2333 /* XXX should emit CMYKcolorimage */
2334 if (planarconfiguration
== PLANARCONFIG_CONTIG
) {
2335 PSColorContigPreamble(fd
, w
, h
, 4);
2336 PSDataColorContig(fd
, tif
, w
, h
, 4);
2338 PSColorSeparatePreamble(fd
, w
, h
, 4);
2339 PSDataColorSeparate(fd
, tif
, w
, h
, 4);
2342 case PHOTOMETRIC_PALETTE
:
2343 fprintf(fd
, "%s", RGBcolorimage
);
2344 PhotoshopBanner(fd
, w
, h
, 1, 3, "false 3 colorimage");
2345 fprintf(fd
, "/scanLine %ld string def\n",
2346 (long) ps_bytesperrow
* 3L);
2347 fprintf(fd
, "%lu %lu 8\n",
2348 (unsigned long) w
, (unsigned long) h
);
2349 fprintf(fd
, "[%lu 0 0 -%lu 0 %lu]\n",
2350 (unsigned long) w
, (unsigned long) h
, (unsigned long) h
);
2351 fprintf(fd
, "{currentfile scanLine readhexstring pop} bind\n");
2352 fprintf(fd
, "false 3 colorimage\n");
2353 PSDataPalette(fd
, tif
, w
, h
);
2355 case PHOTOMETRIC_MINISBLACK
:
2356 case PHOTOMETRIC_MINISWHITE
:
2357 PhotoshopBanner(fd
, w
, h
, 1, 1, imageOp
);
2358 fprintf(fd
, "/scanLine %ld string def\n",
2359 (long) ps_bytesperrow
);
2360 fprintf(fd
, "%lu %lu %d\n",
2361 (unsigned long) w
, (unsigned long) h
, bitspersample
);
2362 fprintf(fd
, "[%lu 0 0 -%lu 0 %lu]\n",
2363 (unsigned long) w
, (unsigned long) h
, (unsigned long) h
);
2365 "{currentfile scanLine readhexstring pop} bind\n");
2366 fprintf(fd
, "%s\n", imageOp
);
2367 PSDataBW(fd
, tif
, w
, h
);
2374 PSColorContigPreamble(FILE* fd
, uint32 w
, uint32 h
, int nc
)
2376 ps_bytesperrow
= nc
* (tf_bytesperrow
/ samplesperpixel
);
2377 PhotoshopBanner(fd
, w
, h
, 1, nc
, "false %d colorimage");
2378 fprintf(fd
, "/line %ld string def\n", (long) ps_bytesperrow
);
2379 fprintf(fd
, "%lu %lu %d\n",
2380 (unsigned long) w
, (unsigned long) h
, bitspersample
);
2381 fprintf(fd
, "[%lu 0 0 -%lu 0 %lu]\n",
2382 (unsigned long) w
, (unsigned long) h
, (unsigned long) h
);
2383 fprintf(fd
, "{currentfile line readhexstring pop} bind\n");
2384 fprintf(fd
, "false %d colorimage\n", nc
);
2388 PSColorSeparatePreamble(FILE* fd
, uint32 w
, uint32 h
, int nc
)
2392 PhotoshopBanner(fd
, w
, h
, ps_bytesperrow
, nc
, "true %d colorimage");
2393 for (i
= 0; i
< nc
; i
++)
2394 fprintf(fd
, "/line%d %ld string def\n",
2395 i
, (long) ps_bytesperrow
);
2396 fprintf(fd
, "%lu %lu %d\n",
2397 (unsigned long) w
, (unsigned long) h
, bitspersample
);
2398 fprintf(fd
, "[%lu 0 0 -%lu 0 %lu] \n",
2399 (unsigned long) w
, (unsigned long) h
, (unsigned long) h
);
2400 for (i
= 0; i
< nc
; i
++)
2401 fprintf(fd
, "{currentfile line%d readhexstring pop}bind\n", i
);
2402 fprintf(fd
, "true %d colorimage\n", nc
);
2405 #define DOBREAK(len, howmany, fd) \
2406 if (((len) -= (howmany)) <= 0) { \
2408 (len) = MAXLINE-(howmany); \
2410 #define PUTHEX(c,fd) putc(hex[((c)>>4)&0xf],fd); putc(hex[(c)&0xf],fd)
2413 PSDataColorContig(FILE* fd
, TIFF
* tif
, uint32 w
, uint32 h
, int nc
)
2416 int breaklen
= MAXLINE
, es
= samplesperpixel
- nc
;
2418 unsigned char *tf_buf
;
2419 unsigned char *cp
, c
;
2422 tf_buf
= (unsigned char *) _TIFFmalloc(tf_bytesperrow
);
2423 if (tf_buf
== NULL
) {
2424 TIFFError(filename
, "No space for scanline buffer");
2427 for (row
= 0; row
< h
; row
++) {
2428 if (TIFFReadScanline(tif
, tf_buf
, row
, 0) < 0)
2432 * for 16 bits, the two bytes must be most significant
2435 if (bitspersample
== 16 && !HOST_BIGENDIAN
) {
2436 PS_FlipBytes(cp
, tf_bytesperrow
);
2441 for (; cc
< tf_bytesperrow
; cc
+= samplesperpixel
) {
2442 DOBREAK(breaklen
, nc
, fd
);
2444 * For images with alpha, matte against
2445 * a white background; i.e.
2446 * Cback * (1 - Aimage)
2449 adjust
= 255 - cp
[nc
];
2451 case 4: c
= *cp
++ + adjust
; PUTHEX(c
,fd
);
2452 case 3: c
= *cp
++ + adjust
; PUTHEX(c
,fd
);
2453 case 2: c
= *cp
++ + adjust
; PUTHEX(c
,fd
);
2454 case 1: c
= *cp
++ + adjust
; PUTHEX(c
,fd
);
2460 for (; cc
< tf_bytesperrow
; cc
+= samplesperpixel
) {
2461 DOBREAK(breaklen
, nc
, fd
);
2463 case 4: c
= *cp
++; PUTHEX(c
,fd
);
2464 case 3: c
= *cp
++; PUTHEX(c
,fd
);
2465 case 2: c
= *cp
++; PUTHEX(c
,fd
);
2466 case 1: c
= *cp
++; PUTHEX(c
,fd
);
2472 _TIFFfree((char *) tf_buf
);
2476 PSDataColorSeparate(FILE* fd
, TIFF
* tif
, uint32 w
, uint32 h
, int nc
)
2479 int breaklen
= MAXLINE
;
2482 unsigned char *tf_buf
;
2483 unsigned char *cp
, c
;
2486 tf_buf
= (unsigned char *) _TIFFmalloc(tf_bytesperrow
);
2487 if (tf_buf
== NULL
) {
2488 TIFFError(filename
, "No space for scanline buffer");
2491 maxs
= (samplesperpixel
> nc
? nc
: samplesperpixel
);
2492 for (row
= 0; row
< h
; row
++) {
2493 for (s
= 0; s
< maxs
; s
++) {
2494 if (TIFFReadScanline(tif
, tf_buf
, row
, s
) < 0)
2496 for (cp
= tf_buf
, cc
= 0; cc
< tf_bytesperrow
; cc
++) {
2497 DOBREAK(breaklen
, 1, fd
);
2503 _TIFFfree((char *) tf_buf
);
2506 #define PUTRGBHEX(c,fd) \
2507 PUTHEX(rmap[c],fd); PUTHEX(gmap[c],fd); PUTHEX(bmap[c],fd)
2510 PSDataPalette(FILE* fd
, TIFF
* tif
, uint32 w
, uint32 h
)
2512 uint16
*rmap
, *gmap
, *bmap
;
2514 int breaklen
= MAXLINE
, nc
;
2516 unsigned char *tf_buf
;
2517 unsigned char *cp
, c
;
2520 if (!TIFFGetField(tif
, TIFFTAG_COLORMAP
, &rmap
, &gmap
, &bmap
)) {
2521 TIFFError(filename
, "Palette image w/o \"Colormap\" tag");
2524 switch (bitspersample
) {
2525 case 8: case 4: case 2: case 1:
2528 TIFFError(filename
, "Depth %d not supported", bitspersample
);
2531 nc
= 3 * (8 / bitspersample
);
2532 tf_buf
= (unsigned char *) _TIFFmalloc(tf_bytesperrow
);
2533 if (tf_buf
== NULL
) {
2534 TIFFError(filename
, "No space for scanline buffer");
2537 if (checkcmap(tif
, 1<<bitspersample
, rmap
, gmap
, bmap
) == 16) {
2539 #define CVT(x) ((unsigned short) (((x) * 255) / ((1U<<16)-1)))
2540 for (i
= (1<<bitspersample
)-1; i
>= 0; i
--) {
2541 rmap
[i
] = CVT(rmap
[i
]);
2542 gmap
[i
] = CVT(gmap
[i
]);
2543 bmap
[i
] = CVT(bmap
[i
]);
2547 for (row
= 0; row
< h
; row
++) {
2548 if (TIFFReadScanline(tif
, tf_buf
, row
, 0) < 0)
2550 for (cp
= tf_buf
, cc
= 0; cc
< tf_bytesperrow
; cc
++) {
2551 DOBREAK(breaklen
, nc
, fd
);
2552 switch (bitspersample
) {
2554 c
= *cp
++; PUTRGBHEX(c
, fd
);
2557 c
= *cp
++; PUTRGBHEX(c
&0xf, fd
);
2558 c
>>= 4; PUTRGBHEX(c
, fd
);
2561 c
= *cp
++; PUTRGBHEX(c
&0x3, fd
);
2562 c
>>= 2; PUTRGBHEX(c
&0x3, fd
);
2563 c
>>= 2; PUTRGBHEX(c
&0x3, fd
);
2564 c
>>= 2; PUTRGBHEX(c
, fd
);
2567 c
= *cp
++; PUTRGBHEX(c
&0x1, fd
);
2568 c
>>= 1; PUTRGBHEX(c
&0x1, fd
);
2569 c
>>= 1; PUTRGBHEX(c
&0x1, fd
);
2570 c
>>= 1; PUTRGBHEX(c
&0x1, fd
);
2571 c
>>= 1; PUTRGBHEX(c
&0x1, fd
);
2572 c
>>= 1; PUTRGBHEX(c
&0x1, fd
);
2573 c
>>= 1; PUTRGBHEX(c
&0x1, fd
);
2574 c
>>= 1; PUTRGBHEX(c
, fd
);
2579 _TIFFfree((char *) tf_buf
);
2583 PSDataBW(FILE* fd
, TIFF
* tif
, uint32 w
, uint32 h
)
2585 int breaklen
= MAXLINE
;
2586 unsigned char* tf_buf
;
2588 tsize_t stripsize
= TIFFStripSize(tif
);
2591 #if defined( EXP_ASCII85ENCODER )
2592 tsize_t ascii85_l
; /* Length, in bytes, of ascii85_p[] data */
2593 uint8
*ascii85_p
= 0; /* Holds ASCII85 encoded data */
2597 tf_buf
= (unsigned char *) _TIFFmalloc(stripsize
);
2598 if (tf_buf
== NULL
) {
2599 TIFFError(filename
, "No space for scanline buffer");
2604 memset(tf_buf
, 0, stripsize
);
2606 #if defined( EXP_ASCII85ENCODER )
2609 * Allocate a buffer to hold the ASCII85 encoded data. Note
2610 * that it is allocated with sufficient room to hold the
2611 * encoded data (5*stripsize/4) plus the EOD marker (+8)
2612 * and formatting line breaks. The line breaks are more
2613 * than taken care of by using 6*stripsize/4 rather than
2617 ascii85_p
= _TIFFmalloc( (stripsize
+(stripsize
/2)) + 8 );
2620 _TIFFfree( tf_buf
);
2622 TIFFError( filename
, "Cannot allocate ASCII85 encoding buffer." );
2631 for (s
= 0; s
< TIFFNumberOfStrips(tif
); s
++) {
2632 tmsize_t cc
= TIFFReadEncodedStrip(tif
, s
, tf_buf
, stripsize
);
2634 TIFFError(filename
, "Can't read strip");
2638 if (photometric
== PHOTOMETRIC_MINISWHITE
) {
2639 for (cp
+= cc
; --cp
>= tf_buf
;)
2644 * for 16 bits, the two bytes must be most significant
2647 if (bitspersample
== 16 && !HOST_BIGENDIAN
) {
2648 PS_FlipBytes(cp
, cc
);
2651 #if defined( EXP_ASCII85ENCODER )
2654 for (i
= 0; i
< cc
; i
+=2) {
2655 adjust
= 255 - cp
[i
+ 1];
2656 cp
[i
/ 2] = cp
[i
] + adjust
;
2661 ascii85_l
= Ascii85EncodeBlock( ascii85_p
, 1, cp
, cc
);
2663 if ( ascii85_l
> 0 )
2664 fwrite( ascii85_p
, ascii85_l
, 1, fd
);
2667 Ascii85Put(*cp
++, fd
);
2668 #endif /* EXP_ASCII85_ENCODER */
2675 DOBREAK(breaklen
, 1, fd
);
2677 * For images with alpha, matte against
2678 * a white background; i.e.
2679 * Cback * (1 - Aimage)
2682 adjust
= 255 - cp
[1];
2683 c
= *cp
++ + adjust
; PUTHEX(c
,fd
);
2689 DOBREAK(breaklen
, 1, fd
);
2698 if ( level2
|| level3
)
2701 #if !defined( EXP_ASCII85ENCODER )
2706 _TIFFfree( ascii85_p
);
2713 PSRawDataBW(FILE* fd
, TIFF
* tif
, uint32 w
, uint32 h
)
2717 int breaklen
= MAXLINE
;
2720 unsigned char *tf_buf
;
2721 unsigned char *cp
, c
;
2724 #if defined( EXP_ASCII85ENCODER )
2725 tsize_t ascii85_l
; /* Length, in bytes, of ascii85_p[] data */
2726 uint8
* ascii85_p
= 0; /* Holds ASCII85 encoded data */
2730 TIFFGetFieldDefaulted(tif
, TIFFTAG_FILLORDER
, &fillorder
);
2731 TIFFGetField(tif
, TIFFTAG_STRIPBYTECOUNTS
, &bc
);
2734 * Find largest strip:
2737 bufsize
= (uint32
) bc
[0];
2739 for ( s
= 0; ++s
< (tstrip_t
)tf_numberstrips
; ) {
2740 if ( bc
[s
] > bufsize
)
2741 bufsize
= (uint32
) bc
[s
];
2744 tf_buf
= (unsigned char*) _TIFFmalloc(bufsize
);
2745 if (tf_buf
== NULL
) {
2746 TIFFError(filename
, "No space for strip buffer");
2750 #if defined( EXP_ASCII85ENCODER )
2753 * Allocate a buffer to hold the ASCII85 encoded data. Note
2754 * that it is allocated with sufficient room to hold the
2755 * encoded data (5*bufsize/4) plus the EOD marker (+8)
2756 * and formatting line breaks. The line breaks are more
2757 * than taken care of by using 6*bufsize/4 rather than
2761 ascii85_p
= _TIFFmalloc( (bufsize
+(bufsize
/2)) + 8 );
2764 _TIFFfree( tf_buf
);
2766 TIFFError( filename
, "Cannot allocate ASCII85 encoding buffer." );
2772 for (s
= 0; s
< (tstrip_t
) tf_numberstrips
; s
++) {
2773 cc
= TIFFReadRawStrip(tif
, s
, tf_buf
, (tmsize_t
) bc
[s
]);
2775 TIFFError(filename
, "Can't read strip");
2778 if (fillorder
== FILLORDER_LSB2MSB
)
2779 TIFFReverseBits(tf_buf
, cc
);
2781 for (cp
= tf_buf
; cc
> 0; cc
--) {
2782 DOBREAK(breaklen
, 1, fd
);
2790 #if defined( EXP_ASCII85ENCODER )
2791 ascii85_l
= Ascii85EncodeBlock( ascii85_p
, 1, tf_buf
, cc
);
2793 if ( ascii85_l
> 0 )
2794 fwrite( ascii85_p
, ascii85_l
, 1, fd
);
2796 for (cp
= tf_buf
; cc
> 0; cc
--)
2797 Ascii85Put(*cp
++, fd
);
2799 #endif /* EXP_ASCII85ENCODER */
2802 _TIFFfree((char *) tf_buf
);
2804 #if defined( EXP_ASCII85ENCODER )
2806 _TIFFfree( ascii85_p
);
2813 ascii85breaklen
= 2*MAXLINE
;
2818 Ascii85Encode(unsigned char* raw
)
2820 static char encoded
[6];
2823 word
= (((raw
[0]<<8)+raw
[1])<<16) + (raw
[2]<<8) + raw
[3];
2828 q
= word
/ (85L*85*85*85); /* actually only a byte */
2829 encoded
[0] = (char) (q
+ '!');
2831 word
-= q
* (85L*85*85*85); q
= word
/ (85L*85*85);
2832 encoded
[1] = (char) (q
+ '!');
2834 word
-= q
* (85L*85*85); q
= word
/ (85*85);
2835 encoded
[2] = (char) (q
+ '!');
2837 w1
= (uint16
) (word
- q
*(85L*85));
2838 encoded
[3] = (char) ((w1
/ 85) + '!');
2839 encoded
[4] = (char) ((w1
% 85) + '!');
2842 encoded
[0] = 'z', encoded
[1] = '\0';
2847 Ascii85Put(unsigned char code
, FILE* fd
)
2849 ascii85buf
[ascii85count
++] = code
;
2850 if (ascii85count
>= 4) {
2854 for (n
= ascii85count
, p
= ascii85buf
; n
>= 4; n
-= 4, p
+= 4) {
2856 for (cp
= Ascii85Encode(p
); *cp
; cp
++) {
2858 if (--ascii85breaklen
== 0) {
2860 ascii85breaklen
= 2*MAXLINE
;
2864 _TIFFmemcpy(ascii85buf
, p
, n
);
2870 Ascii85Flush(FILE* fd
)
2872 if (ascii85count
> 0) {
2874 _TIFFmemset(&ascii85buf
[ascii85count
], 0, 3);
2875 res
= Ascii85Encode(ascii85buf
);
2876 fwrite(res
[0] == 'z' ? "!!!!" : res
, ascii85count
+ 1, 1, fd
);
2880 #if defined( EXP_ASCII85ENCODER)
2882 #define A85BREAKCNTR ascii85breaklen
2883 #define A85BREAKLEN (2*MAXLINE)
2885 /*****************************************************************************
2887 * Name: Ascii85EncodeBlock( ascii85_p, f_eod, raw_p, raw_l )
2889 * Description: This routine will encode the raw data in the buffer described
2890 * by raw_p and raw_l into ASCII85 format and store the encoding
2891 * in the buffer given by ascii85_p.
2893 * Parameters: ascii85_p - A buffer supplied by the caller which will
2894 * contain the encoded ASCII85 data.
2895 * f_eod - Flag: Nz means to end the encoded buffer with
2896 * an End-Of-Data marker.
2897 * raw_p - Pointer to the buffer of data to be encoded
2898 * raw_l - Number of bytes in raw_p[] to be encoded
2900 * Returns: (int) < 0 Error, see errno
2901 * >= 0 Number of bytes written to ascii85_p[].
2903 * Notes: An external variable given by A85BREAKCNTR is used to
2904 * determine when to insert newline characters into the
2905 * encoded data. As each byte is placed into ascii85_p this
2906 * external is decremented. If the variable is decrement to
2907 * or past zero then a newline is inserted into ascii85_p
2908 * and the A85BREAKCNTR is then reset to A85BREAKLEN.
2909 * Note: for efficiency reasons the A85BREAKCNTR variable
2910 * is not actually checked on *every* character
2911 * placed into ascii85_p but often only for every
2914 * THE CALLER IS RESPONSIBLE FOR ENSURING THAT ASCII85_P[] IS
2915 * SUFFICIENTLY LARGE TO THE ENCODED DATA!
2916 * You will need at least 5 * (raw_l/4) bytes plus space for
2917 * newline characters and space for an EOD marker (if
2918 * requested). A safe calculation is to use 6*(raw_l/4) + 8
2919 * to size ascii85_p.
2921 *****************************************************************************/
2923 tsize_t
Ascii85EncodeBlock( uint8
* ascii85_p
, unsigned f_eod
, const uint8
* raw_p
, tsize_t raw_l
)
2926 char ascii85
[5]; /* Encoded 5 tuple */
2927 tsize_t ascii85_l
; /* Number of bytes written to ascii85_p[] */
2928 int rc
; /* Return code */
2929 uint32 val32
; /* Unencoded 4 tuple */
2931 ascii85_l
= 0; /* Nothing written yet */
2935 --raw_p
; /* Prepare for pre-increment fetches */
2937 for ( ; raw_l
> 3; raw_l
-= 4 )
2939 val32
= *(++raw_p
) << 24;
2940 val32
+= *(++raw_p
) << 16;
2941 val32
+= *(++raw_p
) << 8;
2942 val32
+= *(++raw_p
);
2944 if ( val32
== 0 ) /* Special case */
2946 ascii85_p
[ascii85_l
] = 'z';
2952 ascii85
[4] = (char) ((val32
% 85) + 33);
2955 ascii85
[3] = (char) ((val32
% 85) + 33);
2958 ascii85
[2] = (char) ((val32
% 85) + 33);
2961 ascii85
[1] = (char) ((val32
% 85) + 33);
2962 ascii85
[0] = (char) ((val32
/ 85) + 33);
2964 _TIFFmemcpy( &ascii85_p
[ascii85_l
], ascii85
, sizeof(ascii85
) );
2965 rc
= sizeof(ascii85
);
2970 if ( (A85BREAKCNTR
-= rc
) <= 0 )
2972 ascii85_p
[ascii85_l
] = '\n';
2974 A85BREAKCNTR
= A85BREAKLEN
;
2979 * Output any straggler bytes:
2984 tsize_t len
; /* Output this many bytes */
2987 val32
= *++raw_p
<< 24; /* Prime the pump */
2989 if ( --raw_l
> 0 ) val32
+= *(++raw_p
) << 16;
2990 if ( --raw_l
> 0 ) val32
+= *(++raw_p
) << 8;
2994 ascii85
[3] = (char) ((val32
% 85) + 33);
2997 ascii85
[2] = (char) ((val32
% 85) + 33);
3000 ascii85
[1] = (char) ((val32
% 85) + 33);
3001 ascii85
[0] = (char) ((val32
/ 85) + 33);
3003 _TIFFmemcpy( &ascii85_p
[ascii85_l
], ascii85
, len
);
3009 * If requested add an ASCII85 End Of Data marker:
3014 ascii85_p
[ascii85_l
++] = '~';
3015 ascii85_p
[ascii85_l
++] = '>';
3016 ascii85_p
[ascii85_l
++] = '\n';
3019 return ( ascii85_l
);
3021 } /* Ascii85EncodeBlock() */
3023 #endif /* EXP_ASCII85ENCODER */
3027 "usage: tiff2ps [options] input.tif ...",
3028 "where options are:",
3029 " -1 generate PostScript Level 1 (default)",
3030 " -2 generate PostScript Level 2",
3031 " -3 generate PostScript Level 3",
3032 " -8 disable use of ASCII85 encoding with PostScript Level 2/3",
3033 " -a convert all directories in file (default is first), Not EPS",
3034 " -b # set the bottom margin to # inches",
3035 " -c center image (-b and -l still add to this)",
3036 " -d # set initial directory to # counting from zero",
3037 " -D enable duplex printing (two pages per sheet of paper)",
3038 " -e generate Encapsulated PostScript (EPS) (implies -z)",
3039 " -h # set printed page height to # inches (no default)",
3040 " -w # set printed page width to # inches (no default)",
3041 " -H # split image if height is more than # inches",
3042 " -P L or P set optional PageOrientation DSC comment to Landscape or Portrait",
3043 " -W # split image if width is more than # inches",
3044 " -L # overLap split images by # inches",
3045 " -i # enable/disable (Nz/0) pixel interpolation (default: enable)",
3046 " -l # set the left margin to # inches",
3047 " -m use \"imagemask\" operator instead of \"image\"",
3048 " -o # convert directory at file offset # bytes",
3049 " -O file write PostScript to file instead of standard output",
3050 " -p generate regular PostScript",
3051 " -r # or auto rotate by 90, 180, 270 degrees or auto",
3052 " -s generate PostScript for a single image",
3053 " -t name set postscript document title. Otherwise the filename is used",
3054 " -T print pages for top edge binding",
3055 " -x override resolution units as centimeters",
3056 " -y override resolution units as inches",
3057 " -z enable printing in the deadzone (only for PostScript Level 2/3)",
3067 setbuf(stderr
, buf
);
3068 fprintf(stderr
, "%s\n\n", TIFFGetVersion());
3069 for (i
= 0; stuff
[i
] != NULL
; i
++)
3070 fprintf(stderr
, "%s\n", stuff
[i
]);