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 */
45 * I (Bruce A. Mallett) added this revision history comment ;)
47 * Fixed PS_Lvl2page() code which outputs non-ASCII85 raw
48 * data. Moved test for when to output a line break to
49 * *after* the output of a character. This just serves
50 * to fix an eye-nuisance where the first line of raw
51 * data was one character shorter than subsequent lines.
53 * Added an experimental ASCII85 encoder which can be used
54 * only when there is a single buffer of bytes to be encoded.
55 * This version is much faster at encoding a straight-line
56 * buffer of data because it can avoid alot of the loop
57 * overhead of the byte-by-bye version. To use this version
58 * you need to define EXP_ASCII85ENCODER (experimental ...).
60 * Added bug fix given by Michael Schmidt to PS_Lvl2page()
61 * in which an end-of-data marker ('>') was not being output
62 * when producing non-ASCII85 encoded PostScript Level 2
65 * Fixed PS_Lvl2colorspace() so that it no longer assumes that
66 * a TIFF having more than 2 planes is a CMYK. This routine
67 * no longer looks at the samples per pixel but instead looks
68 * at the "photometric" value. This change allows support of
71 * Modified the PostScript L2 imaging loop so as to test if
72 * the input stream is still open before attempting to do a
73 * flushfile on it. This was done because some RIPs close
74 * the stream after doing the image operation.
76 * Got rid of the realloc() being done inside a loop in the
77 * PSRawDataBW() routine. The code now walks through the
78 * byte-size array outside the loop to determine the largest
79 * size memory block that will be needed.
81 * Added "-m" switch to ask tiff2ps to, where possible, use the
82 * "imagemask" operator instead of the "image" operator.
84 * Added the "-i #" switch to allow interpolation to be disabled.
86 * Unrolled a loop or two to improve performance.
90 * Define EXP_ASCII85ENCODER if you want to use an experimental
91 * version of the ASCII85 encoding routine. The advantage of
92 * using this routine is that tiff2ps will convert to ASCII85
93 * encoding at between 3 and 4 times the speed as compared to
94 * using the old (non-experimental) encoder. The disadvantage
95 * is that you will be using a new (and unproven) encoding
96 * routine. So user beware, you have been warned!
99 #define EXP_ASCII85ENCODER
102 * NB: this code assumes uint32 works with printf's %l[ud].
109 int ascii85
= FALSE
; /* use ASCII85 encoding */
110 int interpolate
= TRUE
; /* interpolate level2 image */
111 int level2
= FALSE
; /* generate PostScript level 2 */
112 int level3
= FALSE
; /* generate PostScript level 3 */
113 int printAll
= FALSE
; /* print all images in file */
114 int generateEPSF
= TRUE
; /* generate Encapsulated PostScript */
115 int PSduplex
= FALSE
; /* enable duplex printing */
116 int PStumble
= FALSE
; /* enable top edge binding */
117 int PSavoiddeadzone
= TRUE
; /* enable avoiding printer deadzone */
118 double maxPageHeight
= 0; /* maximum size to fit on page */
119 double splitOverlap
= 0; /* amount for split pages to overlag */
120 int rotate
= FALSE
; /* rotate image by 180 degrees */
121 char *filename
; /* input filename */
122 int useImagemask
= FALSE
; /* Use imagemask instead of image operator */
123 uint16 res_unit
= 0; /* Resolution units: 2 - inches, 3 - cm */
126 * ASCII85 Encoding Support.
128 unsigned char ascii85buf
[10];
132 int TIFF2PS(FILE*, TIFF
*, double, double, double, double, int);
133 void PSpage(FILE*, TIFF
*, uint32
, uint32
);
134 void PSColorContigPreamble(FILE*, uint32
, uint32
, int);
135 void PSColorSeparatePreamble(FILE*, uint32
, uint32
, int);
136 void PSDataColorContig(FILE*, TIFF
*, uint32
, uint32
, int);
137 void PSDataColorSeparate(FILE*, TIFF
*, uint32
, uint32
, int);
138 void PSDataPalette(FILE*, TIFF
*, uint32
, uint32
);
139 void PSDataBW(FILE*, TIFF
*, uint32
, uint32
);
140 void PSRawDataBW(FILE*, TIFF
*, uint32
, uint32
);
141 void Ascii85Init(void);
142 void Ascii85Put(unsigned char code
, FILE* fd
);
143 void Ascii85Flush(FILE* fd
);
144 void PSHead(FILE*, TIFF
*, uint32
, uint32
, double, double, double, double);
145 void PSTail(FILE*, int);
147 #if defined( EXP_ASCII85ENCODER)
148 int Ascii85EncodeBlock( uint8
* ascii85_p
, unsigned f_eod
, const uint8
* raw_p
, int raw_l
);
151 static void usage(int);
154 main(int argc
, char* argv
[])
156 int dirnum
= -1, c
, np
= 0;
158 double bottommargin
= 0;
159 double leftmargin
= 0;
160 double pageWidth
= 0;
161 double pageHeight
= 0;
165 FILE* output
= stdout
;
167 while ((c
= getopt(argc
, argv
, "b:d:h:H:L:i:w:l:o:O:acelmrxyzps1238DT")) != -1)
170 bottommargin
= atof(optarg
);
176 dirnum
= atoi(optarg
);
182 interpolate
= atoi(optarg
) ? TRUE
:FALSE
;
188 PSavoiddeadzone
= FALSE
;
192 pageHeight
= atof(optarg
);
195 maxPageHeight
= atof(optarg
);
196 if (pageHeight
==0) pageHeight
= maxPageHeight
;
199 splitOverlap
= atof(optarg
);
205 diroff
= (uint32
) strtoul(optarg
, NULL
, 0);
207 case 'O': /* XXX too bad -o is already taken */
208 output
= fopen(optarg
, "w");
209 if (output
== NULL
) {
211 "%s: %s: Cannot open output file.\n",
217 leftmargin
= atof(optarg
);
223 generateEPSF
= FALSE
;
232 pageWidth
= atof(optarg
);
235 PSavoiddeadzone
= FALSE
;
244 ascii85
= TRUE
; /* default to yes */
248 ascii85
= TRUE
; /* default to yes */
254 res_unit
= RESUNIT_CENTIMETER
;
257 res_unit
= RESUNIT_INCH
;
262 for (; argc
- optind
> 0; optind
++) {
263 TIFF
* tif
= TIFFOpen(filename
= argv
[optind
], "r");
266 && !TIFFSetDirectory(tif
, (tdir_t
)dirnum
))
268 else if (diroff
!= 0 &&
269 !TIFFSetSubDirectory(tif
, diroff
))
271 np
= TIFF2PS(output
, tif
, pageWidth
, pageHeight
,
272 leftmargin
, bottommargin
, centered
);
280 if (output
!= stdout
)
285 static uint16 samplesperpixel
;
286 static uint16 bitspersample
;
287 static uint16 planarconfiguration
;
288 static uint16 photometric
;
289 static uint16 compression
;
290 static uint16 extrasamples
;
294 checkImage(TIFF
* tif
)
296 switch (photometric
) {
297 case PHOTOMETRIC_YCBCR
:
298 if ((compression
== COMPRESSION_JPEG
|| compression
== COMPRESSION_OJPEG
)
299 && planarconfiguration
== PLANARCONFIG_CONTIG
) {
300 /* can rely on libjpeg to convert to RGB */
301 TIFFSetField(tif
, TIFFTAG_JPEGCOLORMODE
,
303 photometric
= PHOTOMETRIC_RGB
;
305 if (level2
|| level3
)
307 TIFFError(filename
, "Can not handle image with %s",
308 "PhotometricInterpretation=YCbCr");
312 case PHOTOMETRIC_RGB
:
313 if (alpha
&& bitspersample
!= 8) {
315 "Can not handle %d-bit/sample RGB image with alpha",
320 case PHOTOMETRIC_SEPARATED
:
321 case PHOTOMETRIC_PALETTE
:
322 case PHOTOMETRIC_MINISBLACK
:
323 case PHOTOMETRIC_MINISWHITE
:
325 case PHOTOMETRIC_LOGL
:
326 case PHOTOMETRIC_LOGLUV
:
327 if (compression
!= COMPRESSION_SGILOG
&&
328 compression
!= COMPRESSION_SGILOG24
) {
330 "Can not handle %s data with compression other than SGILog",
331 (photometric
== PHOTOMETRIC_LOGL
) ?
336 /* rely on library to convert to RGB/greyscale */
337 TIFFSetField(tif
, TIFFTAG_SGILOGDATAFMT
, SGILOGDATAFMT_8BIT
);
338 photometric
= (photometric
== PHOTOMETRIC_LOGL
) ?
339 PHOTOMETRIC_MINISBLACK
: PHOTOMETRIC_RGB
;
342 case PHOTOMETRIC_CIELAB
:
346 "Can not handle image with PhotometricInterpretation=%d",
350 switch (bitspersample
) {
355 TIFFError(filename
, "Can not handle %d-bit/sample image",
359 if (planarconfiguration
== PLANARCONFIG_SEPARATE
&& extrasamples
> 0)
360 TIFFWarning(filename
, "Ignoring extra samples");
364 #define PS_UNIT_SIZE 72.0F
365 #define PSUNITS(npix,res) ((npix) * (PS_UNIT_SIZE / (res)))
367 static char RGBcolorimage
[] = "\
370 dup length 3 idiv string 0 3 0\n\
372 add 2 1 roll 1 sub dup 0 eq {\n\
379 } { 2 1 roll } ifelse\n\
383 /colorimage where {pop} {\n\
384 /colorimage {pop pop /rgbproc exch def {bwproc} image} bind def\n\
389 * Adobe Photoshop requires a comment line of the form:
391 * %ImageData: <cols> <rows> <depth> <main channels> <pad channels>
392 * <block size> <1 for binary|2 for hex> "data start"
394 * It is claimed to be part of some future revision of the EPS spec.
397 PhotoshopBanner(FILE* fd
, uint32 w
, uint32 h
, int bs
, int nc
, char* startline
)
399 fprintf(fd
, "%%ImageData: %ld %ld %d %d 0 %d 2 \"",
400 (long) w
, (long) h
, bitspersample
, nc
, bs
);
401 fprintf(fd
, startline
, nc
);
406 * pw : image width in pixels
407 * ph : image height in pixels
408 * pprw : image width in PS units (72 dpi)
409 * pprh : image height in PS units (72 dpi)
412 setupPageState(TIFF
* tif
, uint32
* pw
, uint32
* ph
, double* pprw
, double* pprh
)
414 float xres
= 0.0F
, yres
= 0.0F
;
416 TIFFGetField(tif
, TIFFTAG_IMAGEWIDTH
, pw
);
417 TIFFGetField(tif
, TIFFTAG_IMAGELENGTH
, ph
);
419 TIFFGetFieldDefaulted(tif
, TIFFTAG_RESOLUTIONUNIT
, &res_unit
);
421 * Calculate printable area.
423 if (!TIFFGetField(tif
, TIFFTAG_XRESOLUTION
, &xres
)
424 || fabs(xres
) < 0.0000001)
426 if (!TIFFGetField(tif
, TIFFTAG_YRESOLUTION
, &yres
)
427 || fabs(yres
) < 0.0000001)
430 case RESUNIT_CENTIMETER
:
431 xres
*= 2.54F
, yres
*= 2.54F
;
437 xres
*= PS_UNIT_SIZE
, yres
*= PS_UNIT_SIZE
;
440 *pprh
= PSUNITS(*ph
, yres
);
441 *pprw
= PSUNITS(*pw
, xres
);
445 isCCITTCompression(TIFF
* tif
)
448 TIFFGetField(tif
, TIFFTAG_COMPRESSION
, &compress
);
449 return (compress
== COMPRESSION_CCITTFAX3
||
450 compress
== COMPRESSION_CCITTFAX4
||
451 compress
== COMPRESSION_CCITTRLE
||
452 compress
== COMPRESSION_CCITTRLEW
);
455 static tsize_t tf_bytesperrow
;
456 static tsize_t ps_bytesperrow
;
457 static tsize_t tf_rowsperstrip
;
458 static tsize_t tf_numberstrips
;
459 static char *hex
= "0123456789abcdef";
462 * imagewidth & imageheight are 1/72 inches
463 * pagewidth & pageheight are inches
466 PlaceImage(FILE *fp
, double pagewidth
, double pageheight
,
467 double imagewidth
, double imageheight
, int splitpage
,
468 double lm
, double bm
, int cnt
)
474 double left_offset
= lm
* PS_UNIT_SIZE
;
475 double bottom_offset
= bm
* PS_UNIT_SIZE
;
476 double subimageheight
;
480 pagewidth
*= PS_UNIT_SIZE
;
481 pageheight
*= PS_UNIT_SIZE
;
483 if (maxPageHeight
==0)
486 splitheight
= maxPageHeight
* PS_UNIT_SIZE
;
487 overlap
= splitOverlap
* PS_UNIT_SIZE
;
491 * if too wide, scrunch to fit
492 * else leave it alone
494 if (imagewidth
<= pagewidth
) {
501 * if too long, scrunch to fit
502 * if too short, move to top of page
504 if (imageheight
<= pageheight
) {
505 yscale
= imageheight
;
506 ytran
= pageheight
- imageheight
;
507 } else if (imageheight
> pageheight
&&
508 (splitheight
== 0 || imageheight
<= splitheight
)) {
510 } else /* imageheight > splitheight */ {
511 subimageheight
= imageheight
- (pageheight
-overlap
)*splitpage
;
512 if (subimageheight
<= pageheight
) {
513 yscale
= imageheight
;
514 ytran
= pageheight
- subimageheight
;
516 } else if ( subimageheight
> pageheight
&& subimageheight
<= splitheight
) {
517 yscale
= imageheight
* pageheight
/ subimageheight
;
520 } else /* sumimageheight > splitheight */ {
521 yscale
= imageheight
;
522 ytran
= pageheight
- subimageheight
;
527 bottom_offset
+= ytran
/ (cnt
?2:1);
529 left_offset
+= xtran
/ 2;
530 fprintf(fp
, "%f %f translate\n", left_offset
, bottom_offset
);
531 fprintf(fp
, "%f %f scale\n", xscale
, yscale
);
533 fputs ("1 1 translate 180 rotate\n", fp
);
539 /* returns the sequence number of the page processed */
541 TIFF2PS(FILE* fd
, TIFF
* tif
,
542 double pw
, double ph
, double lm
, double bm
, int cnt
)
550 static int npages
= 0;
553 if (!TIFFGetField(tif
, TIFFTAG_XPOSITION
, &ox
))
555 if (!TIFFGetField(tif
, TIFFTAG_YPOSITION
, &oy
))
557 setupPageState(tif
, &w
, &h
, &prw
, &prh
);
560 tf_numberstrips
= TIFFNumberOfStrips(tif
);
561 TIFFGetFieldDefaulted(tif
, TIFFTAG_ROWSPERSTRIP
,
563 setupPageState(tif
, &w
, &h
, &prw
, &prh
);
565 PSHead(fd
, tif
, w
, h
, prw
, prh
, ox
, oy
);
566 TIFFGetFieldDefaulted(tif
, TIFFTAG_BITSPERSAMPLE
,
568 TIFFGetFieldDefaulted(tif
, TIFFTAG_SAMPLESPERPIXEL
,
570 TIFFGetFieldDefaulted(tif
, TIFFTAG_PLANARCONFIG
,
571 &planarconfiguration
);
572 TIFFGetField(tif
, TIFFTAG_COMPRESSION
, &compression
);
573 TIFFGetFieldDefaulted(tif
, TIFFTAG_EXTRASAMPLES
,
574 &extrasamples
, &sampleinfo
);
575 alpha
= (extrasamples
== 1 &&
576 sampleinfo
[0] == EXTRASAMPLE_ASSOCALPHA
);
577 if (!TIFFGetField(tif
, TIFFTAG_PHOTOMETRIC
, &photometric
)) {
578 switch (samplesperpixel
- extrasamples
) {
580 if (isCCITTCompression(tif
))
581 photometric
= PHOTOMETRIC_MINISWHITE
;
583 photometric
= PHOTOMETRIC_MINISBLACK
;
586 photometric
= PHOTOMETRIC_RGB
;
589 photometric
= PHOTOMETRIC_SEPARATED
;
593 if (checkImage(tif
)) {
594 tf_bytesperrow
= TIFFScanlineSize(tif
);
596 fprintf(fd
, "%%%%Page: %d %d\n", npages
, npages
);
597 if (!generateEPSF
&& ( level2
|| level3
)) {
600 psw
= pw
* PS_UNIT_SIZE
;
601 if (res_unit
== RESUNIT_CENTIMETER
)
604 psw
=rotate
? prh
:prw
;
606 psh
= ph
* PS_UNIT_SIZE
;
607 if (res_unit
== RESUNIT_CENTIMETER
)
610 psh
=rotate
? prw
:prh
;
612 "1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n",
615 "<<\n /Policies <<\n /PageSize 3\n >>\n>> setpagedevice\n",
618 fprintf(fd
, "gsave\n");
619 fprintf(fd
, "100 dict begin\n");
620 if (pw
!= 0 || ph
!= 0) {
621 double psw
= pw
, psh
= ph
;
626 if (maxPageHeight
) { /* used -H option */
627 split
= PlaceImage(fd
,psw
,psh
,prw
,prh
,
630 PSpage(fd
, tif
, w
, h
);
631 fprintf(fd
, "end\n");
632 fprintf(fd
, "grestore\n");
633 fprintf(fd
, "showpage\n");
635 fprintf(fd
, "%%%%Page: %d %d\n",
637 fprintf(fd
, "gsave\n");
638 fprintf(fd
, "100 dict begin\n");
639 split
= PlaceImage(fd
,psw
,psh
,prw
,prh
,
643 double left_offset
= lm
* PS_UNIT_SIZE
;
644 double bottom_offset
= bm
* PS_UNIT_SIZE
;
648 /* NB: maintain image aspect ratio */
649 scale
= psw
/prw
< psh
/prh
?
655 (psh
- prh
* scale
) / 2;
657 (psw
- prw
* scale
) / 2;
659 fprintf(fd
, "%f %f translate\n",
660 left_offset
, bottom_offset
);
661 fprintf(fd
, "%f %f scale\n",
662 prw
* scale
, prh
* scale
);
664 fputs ("1 1 translate 180 rotate\n", fd
);
667 fprintf(fd
, "%f %f scale\n", prw
, prh
);
669 fputs ("1 1 translate 180 rotate\n", fd
);
671 PSpage(fd
, tif
, w
, h
);
672 fprintf(fd
, "end\n");
673 fprintf(fd
, "grestore\n");
674 fprintf(fd
, "showpage\n");
678 TIFFGetFieldDefaulted(tif
, TIFFTAG_SUBFILETYPE
, &subfiletype
);
679 } while (((subfiletype
& FILETYPE_PAGE
) || printAll
) &&
680 TIFFReadDirectory(tif
));
686 static char DuplexPreamble
[] = "\
687 %%BeginFeature: *Duplex True\n\
689 /languagelevel where { pop languagelevel } { 1 } ifelse\n\
690 2 ge { 1 dict dup /Duplex true put setpagedevice }\n\
691 { statusdict /setduplex known { statusdict begin setduplex true end } if\n\
697 static char TumblePreamble
[] = "\
698 %%BeginFeature: *Tumble True\n\
700 /languagelevel where { pop languagelevel } { 1 } ifelse\n\
701 2 ge { 1 dict dup /Tumble true put setpagedevice }\n\
702 { statusdict /settumble known { statusdict begin true settumble end } if\n\
708 static char AvoidDeadZonePreamble
[] = "\
709 gsave newpath clippath pathbbox grestore\n\
710 4 2 roll 2 copy translate\n\
711 exch 3 1 roll sub 3 1 roll sub exch\n\
712 currentpagedevice /PageSize get aload pop\n\
713 exch 3 1 roll div 3 1 roll div abs exch abs\n\
714 2 copy gt { exch } if pop\n\
715 dup 1 lt { dup scale } { pop } ifelse\n\
719 PSHead(FILE *fd
, TIFF
*tif
, uint32 w
, uint32 h
,
720 double pw
, double ph
, double ox
, double oy
)
724 (void) tif
; (void) w
; (void) h
;
726 fprintf(fd
, "%%!PS-Adobe-3.0%s\n", generateEPSF
? " EPSF-3.0" : "");
727 fprintf(fd
, "%%%%Creator: tiff2ps\n");
728 fprintf(fd
, "%%%%Title: %s\n", filename
);
729 fprintf(fd
, "%%%%CreationDate: %s", ctime(&t
));
730 fprintf(fd
, "%%%%DocumentData: Clean7Bit\n");
731 fprintf(fd
, "%%%%Origin: %ld %ld\n", (long) ox
, (long) oy
);
732 /* NB: should use PageBoundingBox */
733 fprintf(fd
, "%%%%BoundingBox: 0 0 %ld %ld\n",
734 (long) ceil(pw
), (long) ceil(ph
));
735 fprintf(fd
, "%%%%LanguageLevel: %d\n", (level3
? 3 : (level2
? 2 : 1)));
736 fprintf(fd
, "%%%%Pages: (atend)\n");
737 fprintf(fd
, "%%%%EndComments\n");
738 fprintf(fd
, "%%%%BeginSetup\n");
740 fprintf(fd
, "%s", DuplexPreamble
);
742 fprintf(fd
, "%s", TumblePreamble
);
743 if (PSavoiddeadzone
&& (level2
|| level3
))
744 fprintf(fd
, "%s", AvoidDeadZonePreamble
);
745 fprintf(fd
, "%%%%EndSetup\n");
749 PSTail(FILE *fd
, int npages
)
751 fprintf(fd
, "%%%%Trailer\n");
752 fprintf(fd
, "%%%%Pages: %d\n", npages
);
753 fprintf(fd
, "%%%%EOF\n");
757 checkcmap(TIFF
* tif
, int n
, uint16
* r
, uint16
* g
, uint16
* b
)
761 if (*r
++ >= 256 || *g
++ >= 256 || *b
++ >= 256)
763 TIFFWarning(filename
, "Assuming 8-bit colormap");
768 PS_Lvl2colorspace(FILE* fd
, TIFF
* tif
)
770 uint16
*rmap
, *gmap
, *bmap
;
772 const char * colorspace_p
;
774 switch ( photometric
)
776 case PHOTOMETRIC_SEPARATED
:
777 colorspace_p
= "CMYK";
780 case PHOTOMETRIC_RGB
:
781 colorspace_p
= "RGB";
785 colorspace_p
= "Gray";
789 * Set up PostScript Level 2 colorspace according to
790 * section 4.8 in the PostScript refenence manual.
792 fputs("% PostScript Level 2 only.\n", fd
);
793 if (photometric
!= PHOTOMETRIC_PALETTE
) {
794 if (photometric
== PHOTOMETRIC_YCBCR
) {
797 fprintf(fd
, "/Device%s setcolorspace\n", colorspace_p
);
802 * Set up an indexed/palette colorspace
804 num_colors
= (1 << bitspersample
);
805 if (!TIFFGetField(tif
, TIFFTAG_COLORMAP
, &rmap
, &gmap
, &bmap
)) {
807 "Palette image w/o \"Colormap\" tag");
810 if (checkcmap(tif
, num_colors
, rmap
, gmap
, bmap
) == 16) {
812 * Convert colormap to 8-bits values.
814 #define CVT(x) (((x) * 255) / ((1L<<16)-1))
815 for (i
= 0; i
< num_colors
; i
++) {
816 rmap
[i
] = CVT(rmap
[i
]);
817 gmap
[i
] = CVT(gmap
[i
]);
818 bmap
[i
] = CVT(bmap
[i
]);
822 fprintf(fd
, "[ /Indexed /DeviceRGB %d", num_colors
- 1);
826 ascii85breaklen
-= 2;
829 for (i
= 0; i
< num_colors
; i
++) {
831 Ascii85Put((unsigned char)rmap
[i
], fd
);
832 Ascii85Put((unsigned char)gmap
[i
], fd
);
833 Ascii85Put((unsigned char)bmap
[i
], fd
);
835 fputs((i
% 8) ? " " : "\n ", fd
);
836 fprintf(fd
, "%02x%02x%02x",
837 rmap
[i
], gmap
[i
], bmap
[i
]);
844 fputs("] setcolorspace\n", fd
);
848 PS_Lvl2ImageDict(FILE* fd
, TIFF
* tif
, uint32 w
, uint32 h
)
851 uint32 tile_width
, tile_height
;
852 uint16 predictor
, minsamplevalue
, maxsamplevalue
;
854 char im_h
[64], im_x
[64], im_y
[64];
855 char * imageOp
= "image";
857 if ( useImagemask
&& (bitspersample
== 1) )
858 imageOp
= "imagemask";
860 (void)strcpy(im_x
, "0");
861 (void)sprintf(im_y
, "%lu", (long) h
);
862 (void)sprintf(im_h
, "%lu", (long) h
);
865 if (TIFFIsTiled(tif
)) {
866 repeat_count
= TIFFNumberOfTiles(tif
);
867 TIFFGetField(tif
, TIFFTAG_TILEWIDTH
, &tile_width
);
868 TIFFGetField(tif
, TIFFTAG_TILELENGTH
, &tile_height
);
869 if (tile_width
> w
|| tile_height
> h
||
870 (w
% tile_width
) != 0 || (h
% tile_height
!= 0)) {
872 * The tiles does not fit image width and height.
873 * Set up a clip rectangle for the image unit square.
875 fputs("0 0 1 1 rectclip\n", fd
);
877 if (tile_width
< w
) {
878 fputs("/im_x 0 def\n", fd
);
879 (void)strcpy(im_x
, "im_x neg");
881 if (tile_height
< h
) {
882 fputs("/im_y 0 def\n", fd
);
883 (void)sprintf(im_y
, "%lu im_y sub", (unsigned long) h
);
886 repeat_count
= tf_numberstrips
;
887 tile_height
= tf_rowsperstrip
;
890 if (repeat_count
> 1) {
891 fputs("/im_y 0 def\n", fd
);
892 fprintf(fd
, "/im_h %lu def\n",
893 (unsigned long) tile_height
);
894 (void)strcpy(im_h
, "im_h");
895 (void)sprintf(im_y
, "%lu im_y sub", (unsigned long) h
);
900 * Output start of exec block
902 fputs("{ % exec\n", fd
);
904 if (repeat_count
> 1)
905 fprintf(fd
, "%d { %% repeat\n", repeat_count
);
908 * Output filter options and image dictionary.
911 fputs(" /im_stream currentfile /ASCII85Decode filter def\n",
914 fputs(" /ImageType 1\n", fd
);
915 fprintf(fd
, " /Width %lu\n", (unsigned long) tile_width
);
917 * Workaround for some software that may crash when last strip
918 * of image contains fewer number of scanlines than specified
919 * by the `/Height' variable. So for stripped images with multiple
920 * strips we will set `/Height' as `im_h', because one is
921 * recalculated for each strip - including the (smaller) final strip.
922 * For tiled images and images with only one strip `/Height' will
923 * contain number of scanlines in tile (or image height in case of
924 * one-stripped image).
926 if (TIFFIsTiled(tif
) || tf_numberstrips
== 1)
927 fprintf(fd
, " /Height %lu\n", (unsigned long) tile_height
);
929 fprintf(fd
, " /Height im_h\n");
931 if (planarconfiguration
== PLANARCONFIG_SEPARATE
&& samplesperpixel
> 1)
932 fputs(" /MultipleDataSources true\n", fd
);
933 fprintf(fd
, " /ImageMatrix [ %lu 0 0 %ld %s %s ]\n",
934 (unsigned long) w
, - (long)h
, im_x
, im_y
);
935 fprintf(fd
, " /BitsPerComponent %d\n", bitspersample
);
936 fprintf(fd
, " /Interpolate %s\n", interpolate
? "true" : "false");
938 switch (samplesperpixel
- extrasamples
) {
940 switch (photometric
) {
941 case PHOTOMETRIC_MINISBLACK
:
942 fputs(" /Decode [0 1]\n", fd
);
944 case PHOTOMETRIC_MINISWHITE
:
945 switch (compression
) {
946 case COMPRESSION_CCITTRLE
:
947 case COMPRESSION_CCITTRLEW
:
948 case COMPRESSION_CCITTFAX3
:
949 case COMPRESSION_CCITTFAX4
:
951 * Manage inverting with /Blackis1 flag
952 * since there migth be uncompressed parts
954 fputs(" /Decode [0 1]\n", fd
);
960 fputs(" /Decode [1 0]\n", fd
);
964 case PHOTOMETRIC_PALETTE
:
965 TIFFGetFieldDefaulted(tif
, TIFFTAG_MINSAMPLEVALUE
,
967 TIFFGetFieldDefaulted(tif
, TIFFTAG_MAXSAMPLEVALUE
,
969 fprintf(fd
, " /Decode [%u %u]\n",
970 minsamplevalue
, maxsamplevalue
);
976 fputs(" /Decode [0 1]\n", fd
);
981 switch (photometric
) {
982 case PHOTOMETRIC_RGB
:
983 fputs(" /Decode [0 1 0 1 0 1]\n", fd
);
985 case PHOTOMETRIC_MINISWHITE
:
986 case PHOTOMETRIC_MINISBLACK
:
991 fputs(" /Decode [0 1 0 1 0 1]\n", fd
);
999 fputs(" /Decode [0 1 0 1 0 1 0 1]\n", fd
);
1002 fputs(" /DataSource", fd
);
1003 if (planarconfiguration
== PLANARCONFIG_SEPARATE
&&
1004 samplesperpixel
> 1)
1007 fputs(" im_stream", fd
);
1009 fputs(" currentfile /ASCIIHexDecode filter", fd
);
1012 switch (compression
) {
1013 case COMPRESSION_NONE
: /* 1: uncompressed */
1015 case COMPRESSION_CCITTRLE
: /* 2: CCITT modified Huffman RLE */
1016 case COMPRESSION_CCITTRLEW
: /* 32771: #1 w/ word alignment */
1017 case COMPRESSION_CCITTFAX3
: /* 3: CCITT Group 3 fax encoding */
1018 case COMPRESSION_CCITTFAX4
: /* 4: CCITT Group 4 fax encoding */
1019 fputs("\n\t<<\n", fd
);
1020 if (compression
== COMPRESSION_CCITTFAX3
) {
1023 fputs("\t /EndOfLine true\n", fd
);
1024 fputs("\t /EndOfBlock false\n", fd
);
1025 if (!TIFFGetField(tif
, TIFFTAG_GROUP3OPTIONS
,
1028 if (g3_options
& GROUP3OPT_2DENCODING
)
1029 fprintf(fd
, "\t /K %s\n", im_h
);
1030 if (g3_options
& GROUP3OPT_UNCOMPRESSED
)
1031 fputs("\t /Uncompressed true\n", fd
);
1032 if (g3_options
& GROUP3OPT_FILLBITS
)
1033 fputs("\t /EncodedByteAlign true\n", fd
);
1035 if (compression
== COMPRESSION_CCITTFAX4
) {
1038 fputs("\t /K -1\n", fd
);
1039 TIFFGetFieldDefaulted(tif
, TIFFTAG_GROUP4OPTIONS
,
1041 if (g4_options
& GROUP4OPT_UNCOMPRESSED
)
1042 fputs("\t /Uncompressed true\n", fd
);
1044 if (!(tile_width
== w
&& w
== 1728U))
1045 fprintf(fd
, "\t /Columns %lu\n",
1046 (unsigned long) tile_width
);
1047 fprintf(fd
, "\t /Rows %s\n", im_h
);
1048 if (compression
== COMPRESSION_CCITTRLE
||
1049 compression
== COMPRESSION_CCITTRLEW
) {
1050 fputs("\t /EncodedByteAlign true\n", fd
);
1051 fputs("\t /EndOfBlock false\n", fd
);
1053 if (photometric
== PHOTOMETRIC_MINISBLACK
)
1054 fputs("\t /BlackIs1 true\n", fd
);
1055 fprintf(fd
, "\t>> /CCITTFaxDecode filter");
1057 case COMPRESSION_LZW
: /* 5: Lempel-Ziv & Welch */
1058 TIFFGetFieldDefaulted(tif
, TIFFTAG_PREDICTOR
, &predictor
);
1059 if (predictor
== 2) {
1060 fputs("\n\t<<\n", fd
);
1061 fprintf(fd
, "\t /Predictor %u\n", predictor
);
1062 fprintf(fd
, "\t /Columns %lu\n",
1063 (unsigned long) tile_width
);
1064 fprintf(fd
, "\t /Colors %u\n", samplesperpixel
);
1065 fprintf(fd
, "\t /BitsPerComponent %u\n",
1069 fputs(" /LZWDecode filter", fd
);
1071 case COMPRESSION_DEFLATE
: /* 5: ZIP */
1072 case COMPRESSION_ADOBE_DEFLATE
:
1074 TIFFGetFieldDefaulted(tif
, TIFFTAG_PREDICTOR
, &predictor
);
1075 if (predictor
> 1) {
1076 fprintf(fd
, "\t %% PostScript Level 3 only.");
1077 fputs("\n\t<<\n", fd
);
1078 fprintf(fd
, "\t /Predictor %u\n", predictor
);
1079 fprintf(fd
, "\t /Columns %lu\n",
1080 (unsigned long) tile_width
);
1081 fprintf(fd
, "\t /Colors %u\n", samplesperpixel
);
1082 fprintf(fd
, "\t /BitsPerComponent %u\n",
1086 fputs(" /FlateDecode filter", fd
);
1088 use_rawdata
= FALSE
;
1091 case COMPRESSION_PACKBITS
: /* 32773: Macintosh RLE */
1092 fputs(" /RunLengthDecode filter", fd
);
1095 case COMPRESSION_OJPEG
: /* 6: !6.0 JPEG */
1096 case COMPRESSION_JPEG
: /* 7: %JPEG DCT compression */
1099 * Code not tested yet
1101 fputs(" /DCTDecode filter", fd
);
1104 use_rawdata
= FALSE
;
1107 case COMPRESSION_NEXT
: /* 32766: NeXT 2-bit RLE */
1108 case COMPRESSION_THUNDERSCAN
: /* 32809: ThunderScan RLE */
1109 case COMPRESSION_PIXARFILM
: /* 32908: Pixar companded 10bit LZW */
1110 case COMPRESSION_JBIG
: /* 34661: ISO JBIG */
1111 use_rawdata
= FALSE
;
1113 case COMPRESSION_SGILOG
: /* 34676: SGI LogL or LogLuv */
1114 case COMPRESSION_SGILOG24
: /* 34677: SGI 24-bit LogLuv */
1115 use_rawdata
= FALSE
;
1121 use_rawdata
= FALSE
;
1124 if (planarconfiguration
== PLANARCONFIG_SEPARATE
&&
1125 samplesperpixel
> 1) {
1129 * NOTE: This code does not work yet...
1131 for (i
= 1; i
< samplesperpixel
; i
++)
1136 fprintf( fd
, "\n >> %s\n", imageOp
);
1138 fputs(" im_stream status { im_stream flushfile } if\n", fd
);
1139 if (repeat_count
> 1) {
1140 if (tile_width
< w
) {
1141 fprintf(fd
, " /im_x im_x %lu add def\n",
1142 (unsigned long) tile_width
);
1143 if (tile_height
< h
) {
1144 fprintf(fd
, " im_x %lu ge {\n",
1146 fputs(" /im_x 0 def\n", fd
);
1147 fprintf(fd
, " /im_y im_y %lu add def\n",
1148 (unsigned long) tile_height
);
1149 fputs(" } if\n", fd
);
1152 if (tile_height
< h
) {
1153 if (tile_width
>= w
) {
1154 fprintf(fd
, " /im_y im_y %lu add def\n",
1155 (unsigned long) tile_height
);
1156 if (!TIFFIsTiled(tif
)) {
1157 fprintf(fd
, " /im_h %lu im_y sub",
1159 fprintf(fd
, " dup %lu gt { pop",
1160 (unsigned long) tile_height
);
1161 fprintf(fd
, " %lu } if def\n",
1162 (unsigned long) tile_height
);
1166 fputs("} repeat\n", fd
);
1169 * End of exec function
1173 return(use_rawdata
);
1179 PS_Lvl2page(FILE* fd
, TIFF
* tif
, uint32 w
, uint32 h
)
1182 int use_rawdata
, tiled_image
, breaklen
= MAXLINE
;
1183 uint32 chunk_no
, num_chunks
, *bc
;
1184 unsigned char *buf_data
, *cp
;
1185 tsize_t chunk_size
, byte_count
;
1187 #if defined( EXP_ASCII85ENCODER )
1188 int ascii85_l
; /* Length, in bytes, of ascii85_p[] data */
1189 uint8
* ascii85_p
= 0; /* Holds ASCII85 encoded data */
1192 PS_Lvl2colorspace(fd
, tif
);
1193 use_rawdata
= PS_Lvl2ImageDict(fd
, tif
, w
, h
);
1195 /* See http://bugzilla.remotesensing.org/show_bug.cgi?id=80 */
1196 #ifdef ENABLE_BROKEN_BEGINENDDATA
1197 fputs("%%BeginData:\n", fd
);
1199 fputs("exec\n", fd
);
1201 tiled_image
= TIFFIsTiled(tif
);
1203 num_chunks
= TIFFNumberOfTiles(tif
);
1204 TIFFGetField(tif
, TIFFTAG_TILEBYTECOUNTS
, &bc
);
1206 num_chunks
= TIFFNumberOfStrips(tif
);
1207 TIFFGetField(tif
, TIFFTAG_STRIPBYTECOUNTS
, &bc
);
1211 chunk_size
= (tsize_t
) bc
[0];
1212 for (chunk_no
= 1; chunk_no
< num_chunks
; chunk_no
++)
1213 if ((tsize_t
) bc
[chunk_no
] > chunk_size
)
1214 chunk_size
= (tsize_t
) bc
[chunk_no
];
1217 chunk_size
= TIFFTileSize(tif
);
1219 chunk_size
= TIFFStripSize(tif
);
1221 buf_data
= (unsigned char *)_TIFFmalloc(chunk_size
);
1223 TIFFError(filename
, "Can't alloc %u bytes for %s.",
1224 chunk_size
, tiled_image
? "tiles" : "strips");
1228 #if defined( EXP_ASCII85ENCODER )
1231 * Allocate a buffer to hold the ASCII85 encoded data. Note
1232 * that it is allocated with sufficient room to hold the
1233 * encoded data (5*chunk_size/4) plus the EOD marker (+8)
1234 * and formatting line breaks. The line breaks are more
1235 * than taken care of by using 6*chunk_size/4 rather than
1239 ascii85_p
= _TIFFmalloc( (chunk_size
+(chunk_size
/2)) + 8 );
1242 _TIFFfree( buf_data
);
1244 TIFFError( filename
, "Cannot allocate ASCII85 encoding buffer." );
1250 TIFFGetFieldDefaulted(tif
, TIFFTAG_FILLORDER
, &fillorder
);
1251 for (chunk_no
= 0; chunk_no
< num_chunks
; chunk_no
++) {
1258 byte_count
= TIFFReadRawTile(tif
, chunk_no
,
1259 buf_data
, chunk_size
);
1261 byte_count
= TIFFReadRawStrip(tif
, chunk_no
,
1262 buf_data
, chunk_size
);
1263 if (fillorder
== FILLORDER_LSB2MSB
)
1264 TIFFReverseBits(buf_data
, byte_count
);
1267 byte_count
= TIFFReadEncodedTile(tif
,
1271 byte_count
= TIFFReadEncodedStrip(tif
,
1275 if (byte_count
< 0) {
1276 TIFFError(filename
, "Can't read %s %d.",
1277 tiled_image
? "tile" : "strip", chunk_no
);
1279 Ascii85Put('\0', fd
);
1282 * For images with alpha, matte against a white background;
1283 * i.e. Cback * (1 - Aimage) where Cback = 1. We will fill the
1284 * lower part of the buffer with the modified values.
1286 * XXX: needs better solution
1289 int adjust
, i
, j
= 0;
1290 int ncomps
= samplesperpixel
- extrasamples
;
1291 for (i
= 0; i
< byte_count
; i
+=samplesperpixel
) {
1292 adjust
= 255 - buf_data
[i
+ ncomps
];
1295 buf_data
[j
++] = buf_data
[i
] + adjust
;
1298 buf_data
[j
++] = buf_data
[i
] + adjust
;
1299 buf_data
[j
++] = buf_data
[i
+1] + adjust
;
1302 buf_data
[j
++] = buf_data
[i
] + adjust
;
1303 buf_data
[j
++] = buf_data
[i
+1] + adjust
;
1304 buf_data
[j
++] = buf_data
[i
+2] + adjust
;
1312 #if defined( EXP_ASCII85ENCODER )
1313 ascii85_l
= Ascii85EncodeBlock(ascii85_p
, 1, buf_data
, byte_count
);
1315 if ( ascii85_l
> 0 )
1316 fwrite( ascii85_p
, ascii85_l
, 1, fd
);
1318 for (cp
= buf_data
; byte_count
> 0; byte_count
--)
1319 Ascii85Put(*cp
++, fd
);
1324 for (cp
= buf_data
; byte_count
> 0; byte_count
--) {
1325 putc(hex
[((*cp
)>>4)&0xf], fd
);
1326 putc(hex
[(*cp
)&0xf], fd
);
1329 if (--breaklen
<= 0) {
1337 if ( level2
|| level3
)
1341 #if !defined( EXP_ASCII85ENCODER )
1347 #if defined( EXP_ASCII85ENCODER )
1349 _TIFFfree( ascii85_p
);
1352 _TIFFfree(buf_data
);
1353 #ifdef ENABLE_BROKEN_BEGINENDDATA
1354 fputs("%%EndData\n", fd
);
1360 PSpage(FILE* fd
, TIFF
* tif
, uint32 w
, uint32 h
)
1362 char * imageOp
= "image";
1364 if ( useImagemask
&& (bitspersample
== 1) )
1365 imageOp
= "imagemask";
1367 if ((level2
|| level3
) && PS_Lvl2page(fd
, tif
, w
, h
))
1369 ps_bytesperrow
= tf_bytesperrow
- (extrasamples
* bitspersample
/ 8)*w
;
1370 switch (photometric
) {
1371 case PHOTOMETRIC_RGB
:
1372 if (planarconfiguration
== PLANARCONFIG_CONTIG
) {
1373 fprintf(fd
, "%s", RGBcolorimage
);
1374 PSColorContigPreamble(fd
, w
, h
, 3);
1375 PSDataColorContig(fd
, tif
, w
, h
, 3);
1377 PSColorSeparatePreamble(fd
, w
, h
, 3);
1378 PSDataColorSeparate(fd
, tif
, w
, h
, 3);
1381 case PHOTOMETRIC_SEPARATED
:
1382 /* XXX should emit CMYKcolorimage */
1383 if (planarconfiguration
== PLANARCONFIG_CONTIG
) {
1384 PSColorContigPreamble(fd
, w
, h
, 4);
1385 PSDataColorContig(fd
, tif
, w
, h
, 4);
1387 PSColorSeparatePreamble(fd
, w
, h
, 4);
1388 PSDataColorSeparate(fd
, tif
, w
, h
, 4);
1391 case PHOTOMETRIC_PALETTE
:
1392 fprintf(fd
, "%s", RGBcolorimage
);
1393 PhotoshopBanner(fd
, w
, h
, 1, 3, "false 3 colorimage");
1394 fprintf(fd
, "/scanLine %ld string def\n",
1395 (long) ps_bytesperrow
* 3L);
1396 fprintf(fd
, "%lu %lu 8\n",
1397 (unsigned long) w
, (unsigned long) h
);
1398 fprintf(fd
, "[%lu 0 0 -%lu 0 %lu]\n",
1399 (unsigned long) w
, (unsigned long) h
, (unsigned long) h
);
1400 fprintf(fd
, "{currentfile scanLine readhexstring pop} bind\n");
1401 fprintf(fd
, "false 3 colorimage\n");
1402 PSDataPalette(fd
, tif
, w
, h
);
1404 case PHOTOMETRIC_MINISBLACK
:
1405 case PHOTOMETRIC_MINISWHITE
:
1406 PhotoshopBanner(fd
, w
, h
, 1, 1, imageOp
);
1407 fprintf(fd
, "/scanLine %ld string def\n",
1408 (long) ps_bytesperrow
);
1409 fprintf(fd
, "%lu %lu %d\n",
1410 (unsigned long) w
, (unsigned long) h
, bitspersample
);
1411 fprintf(fd
, "[%lu 0 0 -%lu 0 %lu]\n",
1412 (unsigned long) w
, (unsigned long) h
, (unsigned long) h
);
1414 "{currentfile scanLine readhexstring pop} bind\n");
1415 fprintf(fd
, "%s\n", imageOp
);
1416 PSDataBW(fd
, tif
, w
, h
);
1423 PSColorContigPreamble(FILE* fd
, uint32 w
, uint32 h
, int nc
)
1425 ps_bytesperrow
= nc
* (tf_bytesperrow
/ samplesperpixel
);
1426 PhotoshopBanner(fd
, w
, h
, 1, nc
, "false %d colorimage");
1427 fprintf(fd
, "/line %ld string def\n", (long) ps_bytesperrow
);
1428 fprintf(fd
, "%lu %lu %d\n",
1429 (unsigned long) w
, (unsigned long) h
, bitspersample
);
1430 fprintf(fd
, "[%lu 0 0 -%lu 0 %lu]\n",
1431 (unsigned long) w
, (unsigned long) h
, (unsigned long) h
);
1432 fprintf(fd
, "{currentfile line readhexstring pop} bind\n");
1433 fprintf(fd
, "false %d colorimage\n", nc
);
1437 PSColorSeparatePreamble(FILE* fd
, uint32 w
, uint32 h
, int nc
)
1441 PhotoshopBanner(fd
, w
, h
, ps_bytesperrow
, nc
, "true %d colorimage");
1442 for (i
= 0; i
< nc
; i
++)
1443 fprintf(fd
, "/line%d %ld string def\n",
1444 i
, (long) ps_bytesperrow
);
1445 fprintf(fd
, "%lu %lu %d\n",
1446 (unsigned long) w
, (unsigned long) h
, bitspersample
);
1447 fprintf(fd
, "[%lu 0 0 -%lu 0 %lu] \n",
1448 (unsigned long) w
, (unsigned long) h
, (unsigned long) h
);
1449 for (i
= 0; i
< nc
; i
++)
1450 fprintf(fd
, "{currentfile line%d readhexstring pop}bind\n", i
);
1451 fprintf(fd
, "true %d colorimage\n", nc
);
1454 #define DOBREAK(len, howmany, fd) \
1455 if (((len) -= (howmany)) <= 0) { \
1457 (len) = MAXLINE-(howmany); \
1459 #define PUTHEX(c,fd) putc(hex[((c)>>4)&0xf],fd); putc(hex[(c)&0xf],fd)
1462 PSDataColorContig(FILE* fd
, TIFF
* tif
, uint32 w
, uint32 h
, int nc
)
1465 int breaklen
= MAXLINE
, cc
, es
= samplesperpixel
- nc
;
1466 unsigned char *tf_buf
;
1467 unsigned char *cp
, c
;
1470 tf_buf
= (unsigned char *) _TIFFmalloc(tf_bytesperrow
);
1471 if (tf_buf
== NULL
) {
1472 TIFFError(filename
, "No space for scanline buffer");
1475 for (row
= 0; row
< h
; row
++) {
1476 if (TIFFReadScanline(tif
, tf_buf
, row
, 0) < 0)
1482 for (; cc
< tf_bytesperrow
; cc
+= samplesperpixel
) {
1483 DOBREAK(breaklen
, nc
, fd
);
1485 * For images with alpha, matte against
1486 * a white background; i.e.
1487 * Cback * (1 - Aimage)
1490 adjust
= 255 - cp
[nc
];
1492 case 4: c
= *cp
++ + adjust
; PUTHEX(c
,fd
);
1493 case 3: c
= *cp
++ + adjust
; PUTHEX(c
,fd
);
1494 case 2: c
= *cp
++ + adjust
; PUTHEX(c
,fd
);
1495 case 1: c
= *cp
++ + adjust
; PUTHEX(c
,fd
);
1501 for (; cc
< tf_bytesperrow
; cc
+= samplesperpixel
) {
1502 DOBREAK(breaklen
, nc
, fd
);
1504 case 4: c
= *cp
++; PUTHEX(c
,fd
);
1505 case 3: c
= *cp
++; PUTHEX(c
,fd
);
1506 case 2: c
= *cp
++; PUTHEX(c
,fd
);
1507 case 1: c
= *cp
++; PUTHEX(c
,fd
);
1513 _TIFFfree((char *) tf_buf
);
1517 PSDataColorSeparate(FILE* fd
, TIFF
* tif
, uint32 w
, uint32 h
, int nc
)
1520 int breaklen
= MAXLINE
, cc
;
1522 unsigned char *tf_buf
;
1523 unsigned char *cp
, c
;
1526 tf_buf
= (unsigned char *) _TIFFmalloc(tf_bytesperrow
);
1527 if (tf_buf
== NULL
) {
1528 TIFFError(filename
, "No space for scanline buffer");
1531 maxs
= (samplesperpixel
> nc
? nc
: samplesperpixel
);
1532 for (row
= 0; row
< h
; row
++) {
1533 for (s
= 0; s
< maxs
; s
++) {
1534 if (TIFFReadScanline(tif
, tf_buf
, row
, s
) < 0)
1536 for (cp
= tf_buf
, cc
= 0; cc
< tf_bytesperrow
; cc
++) {
1537 DOBREAK(breaklen
, 1, fd
);
1543 _TIFFfree((char *) tf_buf
);
1546 #define PUTRGBHEX(c,fd) \
1547 PUTHEX(rmap[c],fd); PUTHEX(gmap[c],fd); PUTHEX(bmap[c],fd)
1550 PSDataPalette(FILE* fd
, TIFF
* tif
, uint32 w
, uint32 h
)
1552 uint16
*rmap
, *gmap
, *bmap
;
1554 int breaklen
= MAXLINE
, cc
, nc
;
1555 unsigned char *tf_buf
;
1556 unsigned char *cp
, c
;
1559 if (!TIFFGetField(tif
, TIFFTAG_COLORMAP
, &rmap
, &gmap
, &bmap
)) {
1560 TIFFError(filename
, "Palette image w/o \"Colormap\" tag");
1563 switch (bitspersample
) {
1564 case 8: case 4: case 2: case 1:
1567 TIFFError(filename
, "Depth %d not supported", bitspersample
);
1570 nc
= 3 * (8 / bitspersample
);
1571 tf_buf
= (unsigned char *) _TIFFmalloc(tf_bytesperrow
);
1572 if (tf_buf
== NULL
) {
1573 TIFFError(filename
, "No space for scanline buffer");
1576 if (checkcmap(tif
, 1<<bitspersample
, rmap
, gmap
, bmap
) == 16) {
1578 #define CVT(x) ((unsigned short) (((x) * 255) / ((1U<<16)-1)))
1579 for (i
= (1<<bitspersample
)-1; i
>= 0; i
--) {
1580 rmap
[i
] = CVT(rmap
[i
]);
1581 gmap
[i
] = CVT(gmap
[i
]);
1582 bmap
[i
] = CVT(bmap
[i
]);
1586 for (row
= 0; row
< h
; row
++) {
1587 if (TIFFReadScanline(tif
, tf_buf
, row
, 0) < 0)
1589 for (cp
= tf_buf
, cc
= 0; cc
< tf_bytesperrow
; cc
++) {
1590 DOBREAK(breaklen
, nc
, fd
);
1591 switch (bitspersample
) {
1593 c
= *cp
++; PUTRGBHEX(c
, fd
);
1596 c
= *cp
++; PUTRGBHEX(c
&0xf, fd
);
1597 c
>>= 4; PUTRGBHEX(c
, fd
);
1600 c
= *cp
++; PUTRGBHEX(c
&0x3, fd
);
1601 c
>>= 2; PUTRGBHEX(c
&0x3, fd
);
1602 c
>>= 2; PUTRGBHEX(c
&0x3, fd
);
1603 c
>>= 2; PUTRGBHEX(c
, fd
);
1606 c
= *cp
++; PUTRGBHEX(c
&0x1, fd
);
1607 c
>>= 1; PUTRGBHEX(c
&0x1, fd
);
1608 c
>>= 1; PUTRGBHEX(c
&0x1, fd
);
1609 c
>>= 1; PUTRGBHEX(c
&0x1, fd
);
1610 c
>>= 1; PUTRGBHEX(c
&0x1, fd
);
1611 c
>>= 1; PUTRGBHEX(c
&0x1, fd
);
1612 c
>>= 1; PUTRGBHEX(c
&0x1, fd
);
1613 c
>>= 1; PUTRGBHEX(c
, fd
);
1618 _TIFFfree((char *) tf_buf
);
1622 PSDataBW(FILE* fd
, TIFF
* tif
, uint32 w
, uint32 h
)
1624 int breaklen
= MAXLINE
;
1625 unsigned char* tf_buf
;
1627 tsize_t stripsize
= TIFFStripSize(tif
);
1630 #if defined( EXP_ASCII85ENCODER )
1631 int ascii85_l
; /* Length, in bytes, of ascii85_p[] data */
1632 uint8
*ascii85_p
= 0; /* Holds ASCII85 encoded data */
1636 tf_buf
= (unsigned char *) _TIFFmalloc(stripsize
);
1637 memset(tf_buf
, 0, stripsize
);
1638 if (tf_buf
== NULL
) {
1639 TIFFError(filename
, "No space for scanline buffer");
1643 #if defined( EXP_ASCII85ENCODER )
1646 * Allocate a buffer to hold the ASCII85 encoded data. Note
1647 * that it is allocated with sufficient room to hold the
1648 * encoded data (5*stripsize/4) plus the EOD marker (+8)
1649 * and formatting line breaks. The line breaks are more
1650 * than taken care of by using 6*stripsize/4 rather than
1654 ascii85_p
= _TIFFmalloc( (stripsize
+(stripsize
/2)) + 8 );
1657 _TIFFfree( tf_buf
);
1659 TIFFError( filename
, "Cannot allocate ASCII85 encoding buffer." );
1668 for (s
= 0; s
< TIFFNumberOfStrips(tif
); s
++) {
1669 int cc
= TIFFReadEncodedStrip(tif
, s
, tf_buf
, stripsize
);
1671 TIFFError(filename
, "Can't read strip");
1675 if (photometric
== PHOTOMETRIC_MINISWHITE
) {
1676 for (cp
+= cc
; --cp
>= tf_buf
;)
1681 #if defined( EXP_ASCII85ENCODER )
1684 for (i
= 0; i
< cc
; i
+=2) {
1685 adjust
= 255 - cp
[i
+ 1];
1686 cp
[i
/ 2] = cp
[i
] + adjust
;
1691 ascii85_l
= Ascii85EncodeBlock( ascii85_p
, 1, cp
, cc
);
1693 if ( ascii85_l
> 0 )
1694 fwrite( ascii85_p
, ascii85_l
, 1, fd
);
1697 Ascii85Put(*cp
++, fd
);
1698 #endif /* EXP_ASCII85_ENCODER */
1705 DOBREAK(breaklen
, 1, fd
);
1707 * For images with alpha, matte against
1708 * a white background; i.e.
1709 * Cback * (1 - Aimage)
1712 adjust
= 255 - cp
[1];
1713 c
= *cp
++ + adjust
; PUTHEX(c
,fd
);
1719 DOBREAK(breaklen
, 1, fd
);
1728 if ( level2
|| level3
)
1731 #if !defined( EXP_ASCII85ENCODER )
1736 _TIFFfree( ascii85_p
);
1743 PSRawDataBW(FILE* fd
, TIFF
* tif
, uint32 w
, uint32 h
)
1747 int breaklen
= MAXLINE
, cc
;
1749 unsigned char *tf_buf
;
1750 unsigned char *cp
, c
;
1753 #if defined( EXP_ASCII85ENCODER )
1754 int ascii85_l
; /* Length, in bytes, of ascii85_p[] data */
1755 uint8
* ascii85_p
= 0; /* Holds ASCII85 encoded data */
1759 TIFFGetFieldDefaulted(tif
, TIFFTAG_FILLORDER
, &fillorder
);
1760 TIFFGetField(tif
, TIFFTAG_STRIPBYTECOUNTS
, &bc
);
1763 * Find largest strip:
1768 for ( s
= 0; ++s
< (tstrip_t
)tf_numberstrips
; ) {
1769 if ( bc
[s
] > bufsize
)
1773 tf_buf
= (unsigned char*) _TIFFmalloc(bufsize
);
1774 if (tf_buf
== NULL
) {
1775 TIFFError(filename
, "No space for strip buffer");
1779 #if defined( EXP_ASCII85ENCODER )
1782 * Allocate a buffer to hold the ASCII85 encoded data. Note
1783 * that it is allocated with sufficient room to hold the
1784 * encoded data (5*bufsize/4) plus the EOD marker (+8)
1785 * and formatting line breaks. The line breaks are more
1786 * than taken care of by using 6*bufsize/4 rather than
1790 ascii85_p
= _TIFFmalloc( (bufsize
+(bufsize
/2)) + 8 );
1793 _TIFFfree( tf_buf
);
1795 TIFFError( filename
, "Cannot allocate ASCII85 encoding buffer." );
1801 for (s
= 0; s
< (tstrip_t
) tf_numberstrips
; s
++) {
1802 cc
= TIFFReadRawStrip(tif
, s
, tf_buf
, bc
[s
]);
1804 TIFFError(filename
, "Can't read strip");
1807 if (fillorder
== FILLORDER_LSB2MSB
)
1808 TIFFReverseBits(tf_buf
, cc
);
1810 for (cp
= tf_buf
; cc
> 0; cc
--) {
1811 DOBREAK(breaklen
, 1, fd
);
1819 #if defined( EXP_ASCII85ENCODER )
1820 ascii85_l
= Ascii85EncodeBlock( ascii85_p
, 1, tf_buf
, cc
);
1822 if ( ascii85_l
> 0 )
1823 fwrite( ascii85_p
, ascii85_l
, 1, fd
);
1825 for (cp
= tf_buf
; cc
> 0; cc
--)
1826 Ascii85Put(*cp
++, fd
);
1828 #endif /* EXP_ASCII85ENCODER */
1831 _TIFFfree((char *) tf_buf
);
1833 #if defined( EXP_ASCII85ENCODER )
1835 _TIFFfree( ascii85_p
);
1842 ascii85breaklen
= 2*MAXLINE
;
1847 Ascii85Encode(unsigned char* raw
)
1849 static char encoded
[6];
1852 word
= (((raw
[0]<<8)+raw
[1])<<16) + (raw
[2]<<8) + raw
[3];
1857 q
= word
/ (85L*85*85*85); /* actually only a byte */
1858 encoded
[0] = (char) (q
+ '!');
1860 word
-= q
* (85L*85*85*85); q
= word
/ (85L*85*85);
1861 encoded
[1] = (char) (q
+ '!');
1863 word
-= q
* (85L*85*85); q
= word
/ (85*85);
1864 encoded
[2] = (char) (q
+ '!');
1866 w1
= (uint16
) (word
- q
*(85L*85));
1867 encoded
[3] = (char) ((w1
/ 85) + '!');
1868 encoded
[4] = (char) ((w1
% 85) + '!');
1871 encoded
[0] = 'z', encoded
[1] = '\0';
1876 Ascii85Put(unsigned char code
, FILE* fd
)
1878 ascii85buf
[ascii85count
++] = code
;
1879 if (ascii85count
>= 4) {
1883 for (n
= ascii85count
, p
= ascii85buf
; n
>= 4; n
-= 4, p
+= 4) {
1885 for (cp
= Ascii85Encode(p
); *cp
; cp
++) {
1887 if (--ascii85breaklen
== 0) {
1889 ascii85breaklen
= 2*MAXLINE
;
1893 _TIFFmemcpy(ascii85buf
, p
, n
);
1899 Ascii85Flush(FILE* fd
)
1901 if (ascii85count
> 0) {
1903 _TIFFmemset(&ascii85buf
[ascii85count
], 0, 3);
1904 res
= Ascii85Encode(ascii85buf
);
1905 fwrite(res
[0] == 'z' ? "!!!!" : res
, ascii85count
+ 1, 1, fd
);
1909 #if defined( EXP_ASCII85ENCODER)
1911 #define A85BREAKCNTR ascii85breaklen
1912 #define A85BREAKLEN (2*MAXLINE)
1914 /*****************************************************************************
1916 * Name: Ascii85EncodeBlock( ascii85_p, f_eod, raw_p, raw_l )
1918 * Description: This routine will encode the raw data in the buffer described
1919 * by raw_p and raw_l into ASCII85 format and store the encoding
1920 * in the buffer given by ascii85_p.
1922 * Parameters: ascii85_p - A buffer supplied by the caller which will
1923 * contain the encoded ASCII85 data.
1924 * f_eod - Flag: Nz means to end the encoded buffer with
1925 * an End-Of-Data marker.
1926 * raw_p - Pointer to the buffer of data to be encoded
1927 * raw_l - Number of bytes in raw_p[] to be encoded
1929 * Returns: (int) < 0 Error, see errno
1930 * >= 0 Number of bytes written to ascii85_p[].
1932 * Notes: An external variable given by A85BREAKCNTR is used to
1933 * determine when to insert newline characters into the
1934 * encoded data. As each byte is placed into ascii85_p this
1935 * external is decremented. If the variable is decrement to
1936 * or past zero then a newline is inserted into ascii85_p
1937 * and the A85BREAKCNTR is then reset to A85BREAKLEN.
1938 * Note: for efficiency reasons the A85BREAKCNTR variable
1939 * is not actually checked on *every* character
1940 * placed into ascii85_p but often only for every
1943 * THE CALLER IS RESPONSIBLE FOR ENSURING THAT ASCII85_P[] IS
1944 * SUFFICIENTLY LARGE TO THE ENCODED DATA!
1945 * You will need at least 5 * (raw_l/4) bytes plus space for
1946 * newline characters and space for an EOD marker (if
1947 * requested). A safe calculation is to use 6*(raw_l/4) + 8
1948 * to size ascii85_p.
1950 *****************************************************************************/
1952 int Ascii85EncodeBlock( uint8
* ascii85_p
, unsigned f_eod
, const uint8
* raw_p
, int raw_l
)
1955 char ascii85
[5]; /* Encoded 5 tuple */
1956 int ascii85_l
; /* Number of bytes written to ascii85_p[] */
1957 int rc
; /* Return code */
1958 uint32 val32
; /* Unencoded 4 tuple */
1960 ascii85_l
= 0; /* Nothing written yet */
1964 --raw_p
; /* Prepare for pre-increment fetches */
1966 for ( ; raw_l
> 3; raw_l
-= 4 )
1968 val32
= *(++raw_p
) << 24;
1969 val32
+= *(++raw_p
) << 16;
1970 val32
+= *(++raw_p
) << 8;
1971 val32
+= *(++raw_p
);
1973 if ( val32
== 0 ) /* Special case */
1975 ascii85_p
[ascii85_l
] = 'z';
1981 ascii85
[4] = (char) ((val32
% 85) + 33);
1984 ascii85
[3] = (char) ((val32
% 85) + 33);
1987 ascii85
[2] = (char) ((val32
% 85) + 33);
1990 ascii85
[1] = (char) ((val32
% 85) + 33);
1991 ascii85
[0] = (char) ((val32
/ 85) + 33);
1993 _TIFFmemcpy( &ascii85_p
[ascii85_l
], ascii85
, sizeof(ascii85
) );
1994 rc
= sizeof(ascii85
);
1999 if ( (A85BREAKCNTR
-= rc
) <= 0 )
2001 ascii85_p
[ascii85_l
] = '\n';
2003 A85BREAKCNTR
= A85BREAKLEN
;
2008 * Output any straggler bytes:
2013 int len
; /* Output this many bytes */
2016 val32
= *++raw_p
<< 24; /* Prime the pump */
2018 if ( --raw_l
> 0 ) val32
+= *(++raw_p
) << 16;
2019 if ( --raw_l
> 0 ) val32
+= *(++raw_p
) << 8;
2023 ascii85
[3] = (char) ((val32
% 85) + 33);
2026 ascii85
[2] = (char) ((val32
% 85) + 33);
2029 ascii85
[1] = (char) ((val32
% 85) + 33);
2030 ascii85
[0] = (char) ((val32
/ 85) + 33);
2032 _TIFFmemcpy( &ascii85_p
[ascii85_l
], ascii85
, len
);
2038 * If requested add an ASCII85 End Of Data marker:
2043 ascii85_p
[ascii85_l
++] = '~';
2044 ascii85_p
[ascii85_l
++] = '>';
2045 ascii85_p
[ascii85_l
++] = '\n';
2048 return ( ascii85_l
);
2050 } /* Ascii85EncodeBlock() */
2052 #endif /* EXP_ASCII85ENCODER */
2056 "usage: tiff2ps [options] input.tif ...",
2057 "where options are:",
2058 " -1 generate PostScript Level 1 (default)",
2059 " -2 generate PostScript Level 2",
2060 " -3 generate PostScript Level 3",
2061 " -8 disable use of ASCII85 encoding with PostScript Level 2/3",
2062 " -a convert all directories in file (default is first)",
2063 " -b # set the bottom margin to # inches",
2064 " -c center image (-b and -l still add to this)",
2065 " -d # convert directory number #",
2066 " -D enable duplex printing (two pages per sheet of paper)",
2067 " -e generate Encapsulated PostScript (EPS) (implies -z)",
2068 " -h # assume printed page height is # inches (default 11)",
2069 " -w # assume printed page width is # inches (default 8.5)",
2070 " -H # split image if height is more than # inches",
2071 " -L # overLap split images by # inches",
2072 " -i # enable/disable (Nz/0) pixel interpolation (default: enable)",
2073 " -l # set the left margin to # inches",
2074 " -m use \"imagemask\" operator instead of \"image\"",
2075 " -o # convert directory at file offset #",
2076 " -O file write PostScript to file instead of standard output",
2077 " -p generate regular PostScript",
2078 " -r rotate by 180 degrees",
2079 " -s generate PostScript for a single image",
2080 " -T print pages for top edge binding",
2081 " -x override resolution units as centimeters",
2082 " -y override resolution units as inches",
2083 " -z enable printing in the deadzone (only for PostScript Level 2/3)",
2093 setbuf(stderr
, buf
);
2094 fprintf(stderr
, "%s\n\n", TIFFGetVersion());
2095 for (i
= 0; stuff
[i
] != NULL
; i
++)
2096 fprintf(stderr
, "%s\n", stuff
[i
]);
2100 /* vim: set ts=8 sts=8 sw=8 noet: */