]> www.fi.muni.cz Git - evince.git/blob - tiff/tiff2ps.c
3e2bd32edfb042c220b83e038c4be80b6cd7fce4
[evince.git] / tiff / tiff2ps.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* $Id$ */
3
4 /*
5  * Copyright (c) 1988-1997 Sam Leffler
6  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and 
9  * its documentation for any purpose is hereby granted without fee, provided
10  * that (i) the above copyright notices and this permission notice appear in
11  * all copies of the software and related documentation, and (ii) the names of
12  * Sam Leffler and Silicon Graphics may not be used in any advertising or
13  * publicity relating to the software without the specific, prior written
14  * permission of Sam Leffler and Silicon Graphics.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
17  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
18  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
19  * 
20  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
21  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
22  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
23  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
24  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
25  * OF THIS SOFTWARE.
26  */
27
28 /*
29  * Modified for use as Evince TIFF ps exporter by
30  * Matthew S. Wilson <msw@rpath.com>
31  * Modifications Copyright (C) 2005 rpath, Inc.
32  *
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>                     /* for atof */
37 #include <math.h>
38 #include <time.h>
39 #include <string.h>
40 #include <unistd.h>
41
42 #include <glib.h>
43 #include <glib/gstdio.h>
44
45 #include "tiff2ps.h"
46
47 /*
48  * Revision history
49  *
50  * 2001-Mar-21
51  *    I (Bruce A. Mallett) added this revision history comment ;)
52  *
53  *    Fixed PS_Lvl2page() code which outputs non-ASCII85 raw
54  *    data.  Moved test for when to output a line break to
55  *    *after* the output of a character.  This just serves
56  *    to fix an eye-nuisance where the first line of raw
57  *    data was one character shorter than subsequent lines.
58  *
59  *    Added an experimental ASCII85 encoder which can be used
60  *    only when there is a single buffer of bytes to be encoded.
61  *    This version is much faster at encoding a straight-line
62  *    buffer of data because it can avoid alot of the loop
63  *    overhead of the byte-by-bye version.  To use this version
64  *    you need to define EXP_ASCII85ENCODER (experimental ...).
65  *
66  *    Added bug fix given by Michael Schmidt to PS_Lvl2page()
67  *    in which an end-of-data marker ('>') was not being output
68  *    when producing non-ASCII85 encoded PostScript Level 2
69  *    data.
70  *
71  *    Fixed PS_Lvl2colorspace() so that it no longer assumes that
72  *    a TIFF having more than 2 planes is a CMYK.  This routine
73  *    no longer looks at the samples per pixel but instead looks
74  *    at the "photometric" value.  This change allows support of
75  *    CMYK TIFFs.
76  *
77  *    Modified the PostScript L2 imaging loop so as to test if
78  *    the input stream is still open before attempting to do a
79  *    flushfile on it.  This was done because some RIPs close
80  *    the stream after doing the image operation.
81  *
82  *    Got rid of the realloc() being done inside a loop in the
83  *    PSRawDataBW() routine.  The code now walks through the
84  *    byte-size array outside the loop to determine the largest
85  *    size memory block that will be needed.
86  *
87  *    Added "-m" switch to ask tiff2ps to, where possible, use the
88  *    "imagemask" operator instead of the "image" operator.
89  *
90  *    Added the "-i #" switch to allow interpolation to be disabled.
91  *
92  *    Unrolled a loop or two to improve performance.
93  */
94
95 /*
96  * Define EXP_ASCII85ENCODER if you want to use an experimental
97  * version of the ASCII85 encoding routine.  The advantage of
98  * using this routine is that tiff2ps will convert to ASCII85
99  * encoding at between 3 and 4 times the speed as compared to
100  * using the old (non-experimental) encoder.  The disadvantage
101  * is that you will be using a new (and unproven) encoding
102  * routine.  So user beware, you have been warned!
103  */
104
105 #define EXP_ASCII85ENCODER
106
107 /*
108  * NB: this code assumes uint32 works with printf's %l[ud].
109  */
110 #ifndef TRUE
111 #define TRUE    1
112 #define FALSE   0
113 #endif
114
115
116 struct _TIFF2PSContext
117 {
118         char *filename;         /* input filename */
119         FILE *fd;               /* output file stream */
120         int ascii85;            /* use ASCII85 encoding */
121         int interpolate;        /* interpolate level2 image */
122         int level2;             /* generate PostScript level 2 */
123         int level3;             /* generate PostScript level 3 */
124         int generateEPSF;       /* generate Encapsulated PostScript */
125         int PSduplex;           /* enable duplex printing */
126         int PStumble;           /* enable top edge binding */
127         int PSavoiddeadzone;    /* enable avoiding printer deadzone */
128         double maxPageHeight;   /* maximum size to fit on page */
129         double splitOverlap;    /* amount for split pages to overlag */
130         int rotate;             /* rotate image by 180 degrees */
131         int useImagemask;       /* Use imagemask instead of image operator */
132         uint16 res_unit;        /* Resolution units: 2 - inches, 3 - cm */
133         int npages;             /* number of pages processed */
134
135         tsize_t tf_bytesperrow;
136         tsize_t ps_bytesperrow;
137         tsize_t tf_rowsperstrip;
138         tsize_t tf_numberstrips;
139
140         /*
141          * ASCII85 Encoding Support.
142          */
143         unsigned char ascii85buf[10];
144         int ascii85count;
145         int ascii85breaklen;
146         uint16 samplesperpixel;
147         uint16 bitspersample;
148         uint16 planarconfiguration;
149         uint16 photometric;
150         uint16 compression;
151         uint16 extrasamples;
152         int alpha;
153 };
154
155 static void PSpage(TIFF2PSContext*, TIFF*, uint32, uint32);
156 static void PSColorContigPreamble(TIFF2PSContext*, uint32, uint32, int);
157 static void PSColorSeparatePreamble(TIFF2PSContext*, uint32, uint32, int);
158 static void PSDataColorContig(TIFF2PSContext*, TIFF*, uint32, uint32, int);
159 static void PSDataColorSeparate(TIFF2PSContext*, TIFF*, uint32, uint32, int);
160 static void PSDataPalette(TIFF2PSContext*, TIFF*, uint32, uint32);
161 static void PSDataBW(TIFF2PSContext*, TIFF*, uint32, uint32);
162 static void Ascii85Init(TIFF2PSContext*);
163 static void Ascii85Put(TIFF2PSContext*, unsigned char);
164 static void Ascii85Flush(TIFF2PSContext*);
165 static void PSHead(TIFF2PSContext*, TIFF*, uint32, uint32,
166                    double, double, double, double);
167 static void PSTail(TIFF2PSContext*);
168
169 #if defined( EXP_ASCII85ENCODER )
170 static int Ascii85EncodeBlock(TIFF2PSContext*, uint8 * ascii85_p,
171                               unsigned f_eod, const uint8 * raw_p, int raw_l);
172 #endif
173
174 TIFF2PSContext* tiff2ps_context_new(const gchar *filename) {
175         TIFF2PSContext* ctx;
176
177         ctx = g_new0(TIFF2PSContext, 1);
178         ctx->filename = g_strdup(filename);
179         ctx->fd = g_fopen(ctx->filename, "w");
180         if (ctx->fd == NULL)
181                 return NULL;
182         ctx->interpolate = TRUE;     /* interpolate level2 image */
183         ctx->PSavoiddeadzone = TRUE; /* enable avoiding printer deadzone */
184         return ctx;
185 }
186
187 void tiff2ps_context_finalize(TIFF2PSContext *ctx) {
188         PSTail(ctx);
189         fclose(ctx->fd);
190         g_free(ctx->filename);
191         g_free(ctx);
192 }
193
194 static int
195 checkImage(TIFF2PSContext *ctx, TIFF* tif)
196 {
197         switch (ctx->photometric) {
198         case PHOTOMETRIC_YCBCR:
199                 if ((ctx->compression == COMPRESSION_JPEG
200                      || ctx->compression == COMPRESSION_OJPEG)
201                     && ctx->planarconfiguration == PLANARCONFIG_CONTIG) {
202                         /* can rely on libjpeg to convert to RGB */
203                         TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE,
204                                      JPEGCOLORMODE_RGB);
205                         ctx->photometric = PHOTOMETRIC_RGB;
206                 } else {
207                         if (ctx->level2 || ctx->level3)
208                                 break;
209                         TIFFError(ctx->filename, "Can not handle image with %s",
210                             "Ctx->PhotometricInterpretation=YCbCr");
211                         return (0);
212                 }
213                 /* fall thru... */
214         case PHOTOMETRIC_RGB:
215                 if (ctx->alpha && ctx->bitspersample != 8) {
216                         TIFFError(ctx->filename,
217                             "Can not handle %d-bit/sample RGB image with ctx->alpha",
218                             ctx->bitspersample);
219                         return (0);
220                 }
221                 /* fall thru... */
222         case PHOTOMETRIC_SEPARATED:
223         case PHOTOMETRIC_PALETTE:
224         case PHOTOMETRIC_MINISBLACK:
225         case PHOTOMETRIC_MINISWHITE:
226                 break;
227         case PHOTOMETRIC_LOGL:
228         case PHOTOMETRIC_LOGLUV:
229                 if (ctx->compression != COMPRESSION_SGILOG &&
230                     ctx->compression != COMPRESSION_SGILOG24) {
231                         TIFFError(ctx->filename,
232                     "Can not handle %s data with ctx->compression other than SGILog",
233                             (ctx->photometric == PHOTOMETRIC_LOGL) ?
234                                 "LogL" : "LogLuv"
235                         );
236                         return (0);
237                 }
238                 /* rely on library to convert to RGB/greyscale */
239                 TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
240                 ctx->photometric = (ctx->photometric == PHOTOMETRIC_LOGL) ?
241                     PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB;
242                 ctx->bitspersample = 8;
243                 break;
244         case PHOTOMETRIC_CIELAB:
245                 /* fall thru... */
246         default:
247                 TIFFError(ctx->filename,
248                     "Can not handle image with Ctx->PhotometricInterpretation=%d",
249                     ctx->photometric);
250                 return (0);
251         }
252         switch (ctx->bitspersample) {
253         case 1: case 2:
254         case 4: case 8:
255                 break;
256         default:
257                 TIFFError(ctx->filename, "Can not handle %d-bit/sample image",
258                     ctx->bitspersample);
259                 return (0);
260         }
261         if (ctx->planarconfiguration == PLANARCONFIG_SEPARATE &&
262             ctx->extrasamples > 0)
263                 TIFFWarning(ctx->filename, "Ignoring extra samples");
264         return (1);
265 }
266
267 #define PS_UNIT_SIZE    72.0F
268 #define PSUNITS(npix,res)       ((npix) * (PS_UNIT_SIZE / (res)))
269
270 static  char RGBcolorimage[] = "\
271 /bwproc {\n\
272     rgbproc\n\
273     dup length 3 idiv string 0 3 0\n\
274     5 -1 roll {\n\
275         add 2 1 roll 1 sub dup 0 eq {\n\
276             pop 3 idiv\n\
277             3 -1 roll\n\
278             dup 4 -1 roll\n\
279             dup 3 1 roll\n\
280             5 -1 roll put\n\
281             1 add 3 0\n\
282         } { 2 1 roll } ifelse\n\
283     } forall\n\
284     pop pop pop\n\
285 } def\n\
286 /colorimage where {pop} {\n\
287     /colorimage {pop pop /rgbproc exch def {bwproc} image} bind def\n\
288 } ifelse\n\
289 ";
290
291 /*
292  * Adobe Photoshop requires a comment line of the form:
293  *
294  * %ImageData: <cols> <rows> <depth>  <main channels> <pad channels>
295  *      <block size> <1 for binary|2 for hex> "data start"
296  *
297  * It is claimed to be part of some future revision of the EPS spec.
298  */
299 static void
300 PhotoshopBanner(TIFF2PSContext* ctx, uint32 w, uint32 h, int bs, int nc,
301                 char* startline)
302 {
303         fprintf(ctx->fd, "%%ImageData: %ld %ld %d %d 0 %d 2 \"",
304             (long) w, (long) h, ctx->bitspersample, nc, bs);
305         fprintf(ctx->fd, startline, nc);
306         fprintf(ctx->fd, "\"\n");
307 }
308
309 /*
310  *   pw : image width in pixels
311  *   ph : image height in pixels
312  * pprw : image width in PS units (72 dpi)
313  * pprh : image height in PS units (72 dpi)
314  */
315 static void
316 setupPageState(TIFF2PSContext *ctx, TIFF* tif, uint32* pw, uint32* ph,
317                double* pprw, double* pprh)
318 {
319         float xres = 0.0F, yres = 0.0F;
320
321         TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, pw);
322         TIFFGetField(tif, TIFFTAG_IMAGELENGTH, ph);
323         if (ctx->res_unit == 0)
324                 TIFFGetFieldDefaulted(tif, TIFFTAG_RESOLUTIONUNIT, &ctx->res_unit);
325         /*
326          * Calculate printable area.
327          */
328         if (!TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xres)
329             || fabs(xres) < 0.0000001)
330                 xres = PS_UNIT_SIZE;
331         if (!TIFFGetField(tif, TIFFTAG_YRESOLUTION, &yres)
332             || fabs(yres) < 0.0000001)
333                 yres = PS_UNIT_SIZE;
334         switch (ctx->res_unit) {
335         case RESUNIT_CENTIMETER:
336                 xres *= 2.54F, yres *= 2.54F;
337                 break;
338         case RESUNIT_INCH:
339                 break;
340         case RESUNIT_NONE:
341         default:
342                 xres *= PS_UNIT_SIZE, yres *= PS_UNIT_SIZE;
343                 break;
344         }
345         *pprh = PSUNITS(*ph, yres);
346         *pprw = PSUNITS(*pw, xres);
347 }
348
349 static int
350 isCCITTCompression(TIFF* tif)
351 {
352     uint16 compress;
353     TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
354     return (compress == COMPRESSION_CCITTFAX3 ||
355             compress == COMPRESSION_CCITTFAX4 ||
356             compress == COMPRESSION_CCITTRLE ||
357             compress == COMPRESSION_CCITTRLEW);
358 }
359
360 static  char *hex = "0123456789abcdef";
361
362 /*
363  * imagewidth & imageheight are 1/72 inches
364  * pagewidth & pageheight are inches
365  */
366 static int
367 PlaceImage(TIFF2PSContext *ctx, double pagewidth, double pageheight,
368            double imagewidth, double imageheight, int splitpage,
369            double lm, double bm, int cnt)
370 {
371         double xtran = 0;
372         double ytran = 0;
373         double xscale = 1;
374         double yscale = 1;
375         double left_offset = lm * PS_UNIT_SIZE;
376         double bottom_offset = bm * PS_UNIT_SIZE;
377         double subimageheight;
378         double splitheight;
379         double overlap;
380
381         pagewidth *= PS_UNIT_SIZE;
382         pageheight *= PS_UNIT_SIZE;
383
384         if (ctx->maxPageHeight==0)
385                 splitheight = 0;
386         else
387                 splitheight = ctx->maxPageHeight * PS_UNIT_SIZE;
388         overlap = ctx->splitOverlap * PS_UNIT_SIZE;
389
390         /*
391          * WIDTH:
392          *      if too wide, scrunch to fit
393          *      else leave it alone
394          */
395         if (imagewidth <= pagewidth) {
396                 xscale = imagewidth;
397         } else {
398                 xscale = pagewidth;
399         }
400
401         /* HEIGHT:
402          *      if too long, scrunch to fit
403          *      if too short, move to top of page
404          */
405         if (imageheight <= pageheight) {
406                 yscale = imageheight;
407                 ytran = pageheight - imageheight;
408         } else if (imageheight > pageheight &&
409                 (splitheight == 0 || imageheight <= splitheight)) {
410                 yscale = pageheight;
411         } else /* imageheight > splitheight */ {
412                 subimageheight = imageheight - (pageheight-overlap)*splitpage;
413                 if (subimageheight <= pageheight) {
414                         yscale = imageheight;
415                         ytran = pageheight - subimageheight;
416                         splitpage = 0;
417                 } else if ( subimageheight > pageheight && subimageheight <= splitheight) {
418                         yscale = imageheight * pageheight / subimageheight;
419                         ytran = 0;
420                         splitpage = 0;
421                 } else /* sumimageheight > splitheight */ {
422                         yscale = imageheight;
423                         ytran = pageheight - subimageheight;
424                         splitpage++;
425                 }
426         }
427
428         bottom_offset += ytran / (cnt?2:1);
429         if (cnt)
430             left_offset += xtran / 2;
431         fprintf(ctx->fd, "%f %f translate\n", left_offset, bottom_offset);
432         fprintf(ctx->fd, "%f %f scale\n", xscale, yscale);
433         if (ctx->rotate)
434             fputs ("1 1 translate 180 ctx->rotate\n", ctx->fd);
435
436         return splitpage;
437 }
438
439
440 void
441 tiff2ps_process_page(TIFF2PSContext* ctx, TIFF* tif, double pw, double ph,
442                      double lm, double bm, gboolean cnt)
443 {
444         uint32 w, h;
445         float ox, oy;
446         double prw, prh;
447         double scale = 1.0;
448         double left_offset = lm * PS_UNIT_SIZE;
449         double bottom_offset = bm * PS_UNIT_SIZE;
450         uint16* sampleinfo;
451         int split;
452
453         if (!TIFFGetField(tif, TIFFTAG_XPOSITION, &ox))
454                 ox = 0;
455         if (!TIFFGetField(tif, TIFFTAG_YPOSITION, &oy))
456                 oy = 0;
457         setupPageState(ctx, tif, &w, &h, &prw, &prh);
458
459         ctx->tf_numberstrips = TIFFNumberOfStrips(tif);
460         TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP,
461                               &ctx->tf_rowsperstrip);
462         setupPageState(ctx, tif, &w, &h, &prw, &prh);
463         if (!ctx->npages)
464                 PSHead(ctx, tif, w, h, prw, prh, ox, oy);
465         TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE,
466                               &ctx->bitspersample);
467         TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL,
468                               &ctx->samplesperpixel);
469         TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG,
470                               &ctx->planarconfiguration);
471         TIFFGetField(tif, TIFFTAG_COMPRESSION, &ctx->compression);
472         TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
473                               &ctx->extrasamples, &sampleinfo);
474         ctx->alpha = (ctx->extrasamples == 1 &&
475                       sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
476         if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &ctx->photometric)) {
477                 switch (ctx->samplesperpixel - ctx->extrasamples) {
478                 case 1:
479                         if (isCCITTCompression(tif))
480                                 ctx->photometric = PHOTOMETRIC_MINISWHITE;
481                         else
482                                 ctx->photometric = PHOTOMETRIC_MINISBLACK;
483                         break;
484                 case 3:
485                         ctx->photometric = PHOTOMETRIC_RGB;
486                         break;
487                 case 4:
488                         ctx->photometric = PHOTOMETRIC_SEPARATED;
489                         break;
490                 }
491         }
492         if (checkImage(ctx, tif)) {
493                 ctx->tf_bytesperrow = TIFFScanlineSize(tif);
494                 ctx->npages++;
495                 fprintf(ctx->fd, "%%%%Page: %d %d\n", ctx->npages,
496                         ctx->npages);
497                 if (!ctx->generateEPSF && ( ctx->level2 || ctx->level3 )) {
498                         double psw = 0.0, psh = 0.0;
499                         if (psw != 0.0) {
500                                 psw = pw * PS_UNIT_SIZE;
501                                 if (ctx->res_unit == RESUNIT_CENTIMETER)
502                                         psw *= 2.54F;
503                         } else
504                                 psw=ctx->rotate ? prh:prw;
505                         if (psh != 0.0) {
506                                 psh = ph * PS_UNIT_SIZE;
507                                 if (ctx->res_unit == RESUNIT_CENTIMETER)
508                                         psh *= 2.54F;
509                         } else
510                                 psh=ctx->rotate ? prw:prh;
511                         fprintf(ctx->fd,
512                                 "1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n",
513                                 psw, psh);
514                         fputs(
515                               "<<\n  /Policies <<\n    /PageSize 3\n  >>\n>> setpagedevice\n",
516                               ctx->fd);
517                 }
518                 fprintf(ctx->fd, "gsave\n");
519                 fprintf(ctx->fd, "100 dict begin\n");
520                 if (pw != 0 || ph != 0) {
521                         if (!pw)
522                                 pw = prw;
523                         if (!ph)
524                                 ph = prh;
525                         if (ctx->maxPageHeight) { /* used -H option */
526                                 split = PlaceImage(ctx,pw,ph,prw,prh,
527                                                    0,lm,bm,cnt);
528                                 while( split ) {
529                                         PSpage(ctx, tif, w, h);
530                                         fprintf(ctx->fd, "end\n");
531                                         fprintf(ctx->fd, "grestore\n");
532                                         fprintf(ctx->fd, "showpage\n");
533                                         ctx->npages++;
534                                         fprintf(ctx->fd, "%%%%Page: %d %d\n",
535                                                 ctx->npages, ctx->npages);
536                                         fprintf(ctx->fd, "gsave\n");
537                                         fprintf(ctx->fd, "100 dict begin\n");
538                                         split = PlaceImage(ctx,pw,ph,prw,prh,
539                                                            split,lm,bm,cnt);
540                                 }
541                         } else {
542                                 pw *= PS_UNIT_SIZE;
543                                 ph *= PS_UNIT_SIZE;
544
545                                 /* NB: maintain image aspect ratio */
546                                 scale = pw/prw < ph/prh ?
547                                         pw/prw : ph/prh;
548                                 if (scale > 1.0)
549                                         scale = 1.0;
550                                 if (cnt) {
551                                         bottom_offset +=
552                                                 (ph - prh * scale) / 2;
553                                         left_offset +=
554                                                 (pw - prw * scale) / 2;
555                                 }
556                                 fprintf(ctx->fd, "%f %f translate\n",
557                                         left_offset, bottom_offset);
558                                 fprintf(ctx->fd, "%f %f scale\n",
559                                         prw * scale, prh * scale);
560                                 if (ctx->rotate)
561                                         fputs ("1 1 translate 180 ctx->rotate\n", ctx->fd);
562                         }
563                 } else {
564                         fprintf(ctx->fd, "%f %f scale\n", prw, prh);
565                         if (ctx->rotate)
566                                 fputs ("1 1 translate 180 ctx->rotate\n", ctx->fd);
567                 }
568                 PSpage(ctx, tif, w, h);
569                 fprintf(ctx->fd, "end\n");
570                 fprintf(ctx->fd, "grestore\n");
571                 fprintf(ctx->fd, "showpage\n");
572         }
573 }
574
575
576 static char DuplexPreamble[] = "\
577 %%BeginFeature: *Duplex True\n\
578 systemdict begin\n\
579   /languagelevel where { pop languagelevel } { 1 } ifelse\n\
580   2 ge { 1 dict dup /Duplex true put setpagedevice }\n\
581   { statusdict /setduplex known { statusdict begin setduplex true end } if\n\
582   } ifelse\n\
583 end\n\
584 %%EndFeature\n\
585 ";
586
587 static char TumblePreamble[] = "\
588 %%BeginFeature: *Tumble True\n\
589 systemdict begin\n\
590   /languagelevel where { pop languagelevel } { 1 } ifelse\n\
591   2 ge { 1 dict dup /Tumble true put setpagedevice }\n\
592   { statusdict /settumble known { statusdict begin true settumble end } if\n\
593   } ifelse\n\
594 end\n\
595 %%EndFeature\n\
596 ";
597
598 static char AvoidDeadZonePreamble[] = "\
599 gsave newpath clippath pathbbox grestore\n\
600   4 2 roll 2 copy translate\n\
601   exch 3 1 roll sub 3 1 roll sub exch\n\
602   currentpagedevice /PageSize get aload pop\n\
603   exch 3 1 roll div 3 1 roll div abs exch abs\n\
604   2 copy gt { exch } if pop\n\
605   dup 1 lt { dup scale } { pop } ifelse\n\
606 ";
607
608 void
609 PSHead(TIFF2PSContext *ctx, TIFF *tif, uint32 w, uint32 h,
610        double pw, double ph, double ox, double oy)
611 {
612         time_t t;
613
614         (void) tif; (void) w; (void) h;
615         t = time(0);
616         fprintf(ctx->fd, "%%!PS-Adobe-3.0%s\n",
617                 ctx->generateEPSF ? " EPSF-3.0" : "");
618         fprintf(ctx->fd, "%%%%Creator: Evince\n");
619         fprintf(ctx->fd, "%%%%CreationDate: %s", ctime(&t));
620         fprintf(ctx->fd, "%%%%DocumentData: Clean7Bit\n");
621         fprintf(ctx->fd, "%%%%Origin: %ld %ld\n", (long) ox, (long) oy);
622         /* NB: should use PageBoundingBox */
623         fprintf(ctx->fd, "%%%%BoundingBox: 0 0 %ld %ld\n",
624                 (long) ceil(pw), (long) ceil(ph));
625         fprintf(ctx->fd, "%%%%LanguageLevel: %d\n",
626                 (ctx->level3 ? 3 : (ctx->level2 ? 2 : 1)));
627         fprintf(ctx->fd, "%%%%Pages: (atend)\n");
628         fprintf(ctx->fd, "%%%%EndComments\n");
629         fprintf(ctx->fd, "%%%%BeginSetup\n");
630         if (ctx->PSduplex)
631                 fprintf(ctx->fd, "%s", DuplexPreamble);
632         if (ctx->PStumble)
633                 fprintf(ctx->fd, "%s", TumblePreamble);
634         if (ctx->PSavoiddeadzone && (ctx->level2 || ctx->level3))
635                 fprintf(ctx->fd, "%s", AvoidDeadZonePreamble);
636         fprintf(ctx->fd, "%%%%EndSetup\n");
637 }
638
639 static void
640 PSTail(TIFF2PSContext *ctx)
641 {
642         if (!ctx->npages)
643                 return;
644         fprintf(ctx->fd, "%%%%Trailer\n");
645         fprintf(ctx->fd, "%%%%Pages: %d\n", ctx->npages);
646         fprintf(ctx->fd, "%%%%EOF\n");
647 }
648
649 static int
650 checkcmap(TIFF2PSContext* ctx, TIFF* tif, int n,
651           uint16* r, uint16* g, uint16* b)
652 {
653         (void) tif;
654         while (n-- > 0)
655                 if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
656                         return (16);
657         TIFFWarning(ctx->filename, "Assuming 8-bit colormap");
658         return (8);
659 }
660
661 static void
662 PS_Lvl2colorspace(TIFF2PSContext* ctx, TIFF* tif)
663 {
664         uint16 *rmap, *gmap, *bmap;
665         int i, num_colors;
666         const char * colorspace_p;
667
668         switch ( ctx->photometric )
669         {
670         case PHOTOMETRIC_SEPARATED:
671                 colorspace_p = "CMYK";
672                 break;
673
674         case PHOTOMETRIC_RGB:
675                 colorspace_p = "RGB";
676                 break;
677
678         default:
679                 colorspace_p = "Gray";
680         }
681
682         /*
683          * Set up PostScript Level 2 colorspace according to
684          * section 4.8 in the PostScript refenence manual.
685          */
686         fputs("% PostScript Level 2 only.\n", ctx->fd);
687         if (ctx->photometric != PHOTOMETRIC_PALETTE) {
688                 if (ctx->photometric == PHOTOMETRIC_YCBCR) {
689                     /* MORE CODE HERE */
690                 }
691                 fprintf(ctx->fd, "/Device%s setcolorspace\n", colorspace_p );
692                 return;
693         }
694
695         /*
696          * Set up an indexed/palette colorspace
697          */
698         num_colors = (1 << ctx->bitspersample);
699         if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) {
700                 TIFFError(ctx->filename,
701                         "Palette image w/o \"Colormap\" tag");
702                 return;
703         }
704         if (checkcmap(ctx, tif, num_colors, rmap, gmap, bmap) == 16) {
705                 /*
706                  * Convert colormap to 8-bits values.
707                  */
708 #define CVT(x)          (((x) * 255) / ((1L<<16)-1))
709                 for (i = 0; i < num_colors; i++) {
710                         rmap[i] = CVT(rmap[i]);
711                         gmap[i] = CVT(gmap[i]);
712                         bmap[i] = CVT(bmap[i]);
713                 }
714 #undef CVT
715         }
716         fprintf(ctx->fd, "[ /Indexed /DeviceRGB %d", num_colors - 1);
717         if (ctx->ascii85) {
718                 Ascii85Init(ctx);
719                 fputs("\n<~", ctx->fd);
720                 ctx->ascii85breaklen -= 2;
721         } else
722                 fputs(" <", ctx->fd);
723         for (i = 0; i < num_colors; i++) {
724                 if (ctx->ascii85) {
725                         Ascii85Put(ctx, (unsigned char)rmap[i]);
726                         Ascii85Put(ctx, (unsigned char)gmap[i]);
727                         Ascii85Put(ctx, (unsigned char)bmap[i]);
728                 } else {
729                         fputs((i % 8) ? " " : "\n  ", ctx->fd);
730                         fprintf(ctx->fd, "%02x%02x%02x",
731                             rmap[i], gmap[i], bmap[i]);
732                 }
733         }
734         if (ctx->ascii85)
735                 Ascii85Flush(ctx);
736         else
737                 fputs(">\n", ctx->fd);
738         fputs("] setcolorspace\n", ctx->fd);
739 }
740
741 static int
742 PS_Lvl2ImageDict(TIFF2PSContext* ctx, TIFF* tif, uint32 w, uint32 h)
743 {
744         int use_rawdata;
745         uint32 tile_width, tile_height;
746         uint16 predictor, minsamplevalue, maxsamplevalue;
747         int repeat_count;
748         char im_h[64], im_x[64], im_y[64];
749         char * imageOp = "image";
750
751         if ( ctx->useImagemask && (ctx->bitspersample == 1) )
752                 imageOp = "imagemask";
753
754         (void)strcpy(im_x, "0");
755         (void)sprintf(im_y, "%lu", (long) h);
756         (void)sprintf(im_h, "%lu", (long) h);
757         tile_width = w;
758         tile_height = h;
759         if (TIFFIsTiled(tif)) {
760                 repeat_count = TIFFNumberOfTiles(tif);
761                 TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tile_width);
762                 TIFFGetField(tif, TIFFTAG_TILELENGTH, &tile_height);
763                 if (tile_width > w || tile_height > h ||
764                     (w % tile_width) != 0 || (h % tile_height != 0)) {
765                         /*
766                          * The tiles does not fit image width and height.
767                          * Set up a clip rectangle for the image unit square.
768                          */
769                         fputs("0 0 1 1 rectclip\n", ctx->fd);
770                 }
771                 if (tile_width < w) {
772                         fputs("/im_x 0 def\n", ctx->fd);
773                         (void)strcpy(im_x, "im_x neg");
774                 }
775                 if (tile_height < h) {
776                         fputs("/im_y 0 def\n", ctx->fd);
777                         (void)sprintf(im_y, "%lu im_y sub", (unsigned long) h);
778                 }
779         } else {
780                 repeat_count = ctx->tf_numberstrips;
781                 tile_height = ctx->tf_rowsperstrip;
782                 if (tile_height > h)
783                         tile_height = h;
784                 if (repeat_count > 1) {
785                         fputs("/im_y 0 def\n", ctx->fd);
786                         fprintf(ctx->fd, "/im_h %lu def\n",
787                             (unsigned long) tile_height);
788                         (void)strcpy(im_h, "im_h");
789                         (void)sprintf(im_y, "%lu im_y sub", (unsigned long) h);
790                 }
791         }
792
793         /*
794          * Output start of exec block
795          */
796         fputs("{ % exec\n", ctx->fd);
797
798         if (repeat_count > 1)
799                 fprintf(ctx->fd, "%d { %% repeat\n", repeat_count);
800
801         /*
802          * Output filter options and image dictionary.
803          */
804         if (ctx->ascii85)
805                 fputs(" /im_stream currentfile /ASCII85Decode filter def\n",
806                       ctx->fd);
807         fputs(" <<\n", ctx->fd);
808         fputs("  /ImageType 1\n", ctx->fd);
809         fprintf(ctx->fd, "  /Width %lu\n", (unsigned long) tile_width);
810         /*
811          * Workaround for some software that may crash when last strip
812          * of image contains fewer number of scanlines than specified
813          * by the `/Height' variable. So for stripped images with multiple
814          * strips we will set `/Height' as `im_h', because one is 
815          * recalculated for each strip - including the (smaller) final strip.
816          * For tiled images and images with only one strip `/Height' will
817          * contain number of scanlines in tile (or image height in case of
818          * one-stripped image).
819          */
820         if (TIFFIsTiled(tif) || ctx->tf_numberstrips == 1)
821                 fprintf(ctx->fd, "  /Height %lu\n", (unsigned long) tile_height);
822         else
823                 fprintf(ctx->fd, "  /Height im_h\n");
824
825         if (ctx->planarconfiguration == PLANARCONFIG_SEPARATE && ctx->samplesperpixel > 1)
826                 fputs("  /MultipleDataSources true\n", ctx->fd);
827         fprintf(ctx->fd, "  /ImageMatrix [ %lu 0 0 %ld %s %s ]\n",
828             (unsigned long) w, - (long)h, im_x, im_y);
829         fprintf(ctx->fd, "  /BitsPerComponent %d\n", ctx->bitspersample);
830         fprintf(ctx->fd, "  /Ctx->Interpolate %s\n", ctx->interpolate ? "true" : "false");
831
832         switch (ctx->samplesperpixel - ctx->extrasamples) {
833         case 1:
834                 switch (ctx->photometric) {
835                 case PHOTOMETRIC_MINISBLACK:
836                         fputs("  /Decode [0 1]\n", ctx->fd);
837                         break;
838                 case PHOTOMETRIC_MINISWHITE:
839                         switch (ctx->compression) {
840                         case COMPRESSION_CCITTRLE:
841                         case COMPRESSION_CCITTRLEW:
842                         case COMPRESSION_CCITTFAX3:
843                         case COMPRESSION_CCITTFAX4:
844                                 /*
845                                  * Manage inverting with /Blackis1 flag
846                                  * since there migth be uncompressed parts
847                                  */
848                                 fputs("  /Decode [0 1]\n", ctx->fd);
849                                 break;
850                         default:
851                                 /*
852                                  * ERROR...
853                                  */
854                                 fputs("  /Decode [1 0]\n", ctx->fd);
855                                 break;
856                         }
857                         break;
858                 case PHOTOMETRIC_PALETTE:
859                         TIFFGetFieldDefaulted(tif, TIFFTAG_MINSAMPLEVALUE,
860                             &minsamplevalue);
861                         TIFFGetFieldDefaulted(tif, TIFFTAG_MAXSAMPLEVALUE,
862                             &maxsamplevalue);
863                         fprintf(ctx->fd, "  /Decode [%u %u]\n",
864                                     minsamplevalue, maxsamplevalue);
865                         break;
866                 default:
867                         /*
868                          * ERROR ?
869                          */
870                         fputs("  /Decode [0 1]\n", ctx->fd);
871                         break;
872                 }
873                 break;
874         case 3:
875                 switch (ctx->photometric) {
876                 case PHOTOMETRIC_RGB:
877                         fputs("  /Decode [0 1 0 1 0 1]\n", ctx->fd);
878                         break;
879                 case PHOTOMETRIC_MINISWHITE:
880                 case PHOTOMETRIC_MINISBLACK:
881                 default:
882                         /*
883                          * ERROR??
884                          */
885                         fputs("  /Decode [0 1 0 1 0 1]\n", ctx->fd);
886                         break;
887                 }
888                 break;
889         case 4:
890                 /*
891                  * ERROR??
892                  */
893                 fputs("  /Decode [0 1 0 1 0 1 0 1]\n", ctx->fd);
894                 break;
895         }
896         fputs("  /DataSource", ctx->fd);
897         if (ctx->planarconfiguration == PLANARCONFIG_SEPARATE &&
898             ctx->samplesperpixel > 1)
899                 fputs(" [", ctx->fd);
900         if (ctx->ascii85)
901                 fputs(" im_stream", ctx->fd);
902         else
903                 fputs(" currentfile /ASCIIHexDecode filter", ctx->fd);
904
905         use_rawdata = TRUE;
906         switch (ctx->compression) {
907         case COMPRESSION_NONE:          /* 1: uncompressed */
908                 break;
909         case COMPRESSION_CCITTRLE:      /* 2: CCITT modified Huffman RLE */
910         case COMPRESSION_CCITTRLEW:     /* 32771: #1 w/ word alignment */
911         case COMPRESSION_CCITTFAX3:     /* 3: CCITT Group 3 fax encoding */
912         case COMPRESSION_CCITTFAX4:     /* 4: CCITT Group 4 fax encoding */
913                 fputs("\n\t<<\n", ctx->fd);
914                 if (ctx->compression == COMPRESSION_CCITTFAX3) {
915                         uint32 g3_options;
916
917                         fputs("\t /EndOfLine true\n", ctx->fd);
918                         fputs("\t /EndOfBlock false\n", ctx->fd);
919                         if (!TIFFGetField(tif, TIFFTAG_GROUP3OPTIONS,
920                                             &g3_options))
921                                 g3_options = 0;
922                         if (g3_options & GROUP3OPT_2DENCODING)
923                                 fprintf(ctx->fd, "\t /K %s\n", im_h);
924                         if (g3_options & GROUP3OPT_UNCOMPRESSED)
925                                 fputs("\t /Uncompressed true\n", ctx->fd);
926                         if (g3_options & GROUP3OPT_FILLBITS)
927                                 fputs("\t /EncodedByteAlign true\n", ctx->fd);
928                 }
929                 if (ctx->compression == COMPRESSION_CCITTFAX4) {
930                         uint32 g4_options;
931
932                         fputs("\t /K -1\n", ctx->fd);
933                         TIFFGetFieldDefaulted(tif, TIFFTAG_GROUP4OPTIONS,
934                                                &g4_options);
935                         if (g4_options & GROUP4OPT_UNCOMPRESSED)
936                                 fputs("\t /Uncompressed true\n", ctx->fd);
937                 }
938                 if (!(tile_width == w && w == 1728U))
939                         fprintf(ctx->fd, "\t /Columns %lu\n",
940                             (unsigned long) tile_width);
941                 fprintf(ctx->fd, "\t /Rows %s\n", im_h);
942                 if (ctx->compression == COMPRESSION_CCITTRLE ||
943                     ctx->compression == COMPRESSION_CCITTRLEW) {
944                         fputs("\t /EncodedByteAlign true\n", ctx->fd);
945                         fputs("\t /EndOfBlock false\n", ctx->fd);
946                 }
947                 if (ctx->photometric == PHOTOMETRIC_MINISBLACK)
948                         fputs("\t /BlackIs1 true\n", ctx->fd);
949                 fprintf(ctx->fd, "\t>> /CCITTFaxDecode filter");
950                 break;
951         case COMPRESSION_LZW:   /* 5: Lempel-Ziv & Welch */
952                 TIFFGetFieldDefaulted(tif, TIFFTAG_PREDICTOR, &predictor);
953                 if (predictor == 2) {
954                         fputs("\n\t<<\n", ctx->fd);
955                         fprintf(ctx->fd, "\t /Predictor %u\n", predictor);
956                         fprintf(ctx->fd, "\t /Columns %lu\n",
957                             (unsigned long) tile_width);
958                         fprintf(ctx->fd, "\t /Colors %u\n", ctx->samplesperpixel);
959                         fprintf(ctx->fd, "\t /BitsPerComponent %u\n",
960                             ctx->bitspersample);
961                         fputs("\t>>", ctx->fd);
962                 }
963                 fputs(" /LZWDecode filter", ctx->fd);
964                 break;
965         case COMPRESSION_DEFLATE:       /* 5: ZIP */
966         case COMPRESSION_ADOBE_DEFLATE:
967                 if ( ctx->level3 ) {
968                          TIFFGetFieldDefaulted(tif, TIFFTAG_PREDICTOR, &predictor);
969                          if (predictor > 1) {
970                                 fprintf(ctx->fd, "\t %% PostScript Level 3 only.");
971                                 fputs("\n\t<<\n", ctx->fd);
972                                 fprintf(ctx->fd, "\t /Predictor %u\n", predictor);
973                                 fprintf(ctx->fd, "\t /Columns %lu\n",
974                                         (unsigned long) tile_width);
975                                 fprintf(ctx->fd, "\t /Colors %u\n", ctx->samplesperpixel);
976                                         fprintf(ctx->fd, "\t /BitsPerComponent %u\n",
977                                         ctx->bitspersample);
978                                 fputs("\t>>", ctx->fd);
979                          }
980                          fputs(" /FlateDecode filter", ctx->fd);
981                 } else {
982                         use_rawdata = FALSE ;
983                 }
984                 break;
985         case COMPRESSION_PACKBITS:      /* 32773: Macintosh RLE */
986                 fputs(" /RunLengthDecode filter", ctx->fd);
987                 use_rawdata = TRUE;
988             break;
989         case COMPRESSION_OJPEG:         /* 6: !6.0 JPEG */
990         case COMPRESSION_JPEG:          /* 7: %JPEG DCT ctx->compression */
991 #ifdef notdef
992                 /*
993                  * Code not tested yet
994                  */
995                 fputs(" /DCTDecode filter", ctx->fd);
996                 use_rawdata = TRUE;
997 #else
998                 use_rawdata = FALSE;
999 #endif
1000                 break;
1001         case COMPRESSION_NEXT:          /* 32766: NeXT 2-bit RLE */
1002         case COMPRESSION_THUNDERSCAN:   /* 32809: ThunderScan RLE */
1003         case COMPRESSION_PIXARFILM:     /* 32908: Pixar companded 10bit LZW */
1004         case COMPRESSION_JBIG:          /* 34661: ISO JBIG */
1005                 use_rawdata = FALSE;
1006                 break;
1007         case COMPRESSION_SGILOG:        /* 34676: SGI LogL or LogLuv */
1008         case COMPRESSION_SGILOG24:      /* 34677: SGI 24-bit LogLuv */
1009                 use_rawdata = FALSE;
1010                 break;
1011         default:
1012                 /*
1013                  * ERROR...
1014                  */
1015                 use_rawdata = FALSE;
1016                 break;
1017         }
1018         if (ctx->planarconfiguration == PLANARCONFIG_SEPARATE &&
1019             ctx->samplesperpixel > 1) {
1020                 uint16 i;
1021
1022                 /*
1023                  * NOTE: This code does not work yet...
1024                  */
1025                 for (i = 1; i < ctx->samplesperpixel; i++)
1026                         fputs(" dup", ctx->fd);
1027                 fputs(" ]", ctx->fd);
1028         }
1029
1030         fprintf( ctx->fd, "\n >> %s\n", imageOp );
1031         if (ctx->ascii85)
1032                 fputs(" im_stream status { im_stream flushfile } if\n", ctx->fd);
1033         if (repeat_count > 1) {
1034                 if (tile_width < w) {
1035                         fprintf(ctx->fd, " /im_x im_x %lu add def\n",
1036                             (unsigned long) tile_width);
1037                         if (tile_height < h) {
1038                                 fprintf(ctx->fd, " im_x %lu ge {\n",
1039                                     (unsigned long) w);
1040                                 fputs("  /im_x 0 def\n", ctx->fd);
1041                                 fprintf(ctx->fd, " /im_y im_y %lu add def\n",
1042                                     (unsigned long) tile_height);
1043                                 fputs(" } if\n", ctx->fd);
1044                         }
1045                 }
1046                 if (tile_height < h) {
1047                         if (tile_width >= w) {
1048                                 fprintf(ctx->fd, " /im_y im_y %lu add def\n",
1049                                     (unsigned long) tile_height);
1050                                 if (!TIFFIsTiled(tif)) {
1051                                         fprintf(ctx->fd, " /im_h %lu im_y sub",
1052                                             (unsigned long) h);
1053                                         fprintf(ctx->fd, " dup %lu gt { pop",
1054                                             (unsigned long) tile_height);
1055                                         fprintf(ctx->fd, " %lu } if def\n",
1056                                             (unsigned long) tile_height);
1057                                 }
1058                         }
1059                 }
1060                 fputs("} repeat\n", ctx->fd);
1061         }
1062         /*
1063          * End of exec function
1064          */
1065         fputs("}\n", ctx->fd);
1066
1067         return(use_rawdata);
1068 }
1069
1070 #define MAXLINE         36
1071
1072 static int
1073 PS_Lvl2page(TIFF2PSContext* ctx, TIFF* tif, uint32 w, uint32 h)
1074 {
1075         uint16 fillorder;
1076         int use_rawdata, tiled_image, breaklen = MAXLINE;
1077         uint32 chunk_no, num_chunks, *bc;
1078         unsigned char *buf_data, *cp;
1079         tsize_t chunk_size, byte_count;
1080
1081 #if defined( EXP_ASCII85ENCODER )
1082         int                     ascii85_l;      /* Length, in bytes, of ascii85_p[] data */
1083         uint8           *       ascii85_p = 0;  /* Holds ASCII85 encoded data */
1084 #endif
1085
1086         PS_Lvl2colorspace(ctx, tif);
1087         use_rawdata = PS_Lvl2ImageDict(ctx, tif, w, h);
1088
1089 /* See http://bugzilla.remotesensing.org/show_bug.cgi?id=80 */
1090 #ifdef ENABLE_BROKEN_BEGINENDDATA
1091         fputs("%%BeginData:\n", ctx->fd);
1092 #endif
1093         fputs("exec\n", ctx->fd);
1094
1095         tiled_image = TIFFIsTiled(tif);
1096         if (tiled_image) {
1097                 num_chunks = TIFFNumberOfTiles(tif);
1098                 TIFFGetField(tif, TIFFTAG_TILEBYTECOUNTS, &bc);
1099         } else {
1100                 num_chunks = TIFFNumberOfStrips(tif);
1101                 TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bc);
1102         }
1103
1104         if (use_rawdata) {
1105                 chunk_size = (tsize_t) bc[0];
1106                 for (chunk_no = 1; chunk_no < num_chunks; chunk_no++)
1107                         if ((tsize_t) bc[chunk_no] > chunk_size)
1108                                 chunk_size = (tsize_t) bc[chunk_no];
1109         } else {
1110                 if (tiled_image)
1111                         chunk_size = TIFFTileSize(tif);
1112                 else
1113                         chunk_size = TIFFStripSize(tif);
1114         }
1115         buf_data = (unsigned char *)_TIFFmalloc(chunk_size);
1116         if (!buf_data) {
1117                 TIFFError(ctx->filename, "Can't alloc %u bytes for %s.",
1118                         chunk_size, tiled_image ? "tiles" : "strips");
1119                 return(FALSE);
1120         }
1121
1122 #if defined( EXP_ASCII85ENCODER )
1123         if ( ctx->ascii85 ) {
1124             /*
1125              * Allocate a buffer to hold the ASCII85 encoded data.  Note
1126              * that it is allocated with sufficient room to hold the
1127              * encoded data (5*chunk_size/4) plus the EOD marker (+8)
1128              * and formatting line breaks.  The line breaks are more
1129              * than taken care of by using 6*chunk_size/4 rather than
1130              * 5*chunk_size/4.
1131              */
1132
1133             ascii85_p = _TIFFmalloc( (chunk_size+(chunk_size/2)) + 8 );
1134
1135             if ( !ascii85_p ) {
1136                 _TIFFfree( buf_data );
1137
1138                 TIFFError( ctx->filename,
1139                            "Cannot allocate ASCII85 encoding buffer." );
1140                 return ( FALSE );
1141             }
1142         }
1143 #endif
1144
1145         TIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &fillorder);
1146         for (chunk_no = 0; chunk_no < num_chunks; chunk_no++) {
1147                 if (ctx->ascii85)
1148                         Ascii85Init(ctx);
1149                 else
1150                         breaklen = MAXLINE;
1151                 if (use_rawdata) {
1152                         if (tiled_image)
1153                                 byte_count = TIFFReadRawTile(tif, chunk_no,
1154                                                   buf_data, chunk_size);
1155                         else
1156                                 byte_count = TIFFReadRawStrip(tif, chunk_no,
1157                                                   buf_data, chunk_size);
1158                         if (fillorder == FILLORDER_LSB2MSB)
1159                             TIFFReverseBits(buf_data, byte_count);
1160                 } else {
1161                         if (tiled_image)
1162                                 byte_count = TIFFReadEncodedTile(tif,
1163                                                 chunk_no, buf_data,
1164                                                 chunk_size);
1165                         else
1166                                 byte_count = TIFFReadEncodedStrip(tif,
1167                                                 chunk_no, buf_data,
1168                                                 chunk_size);
1169                 }
1170                 if (byte_count < 0) {
1171                         TIFFError(ctx->filename, "Can't read %s %d.",
1172                                 tiled_image ? "tile" : "strip", chunk_no);
1173                         if (ctx->ascii85)
1174                                 Ascii85Put(ctx, '\0');
1175                 }
1176                 /*
1177                  * For images with ctx->alpha, matte against a white background;
1178                  * i.e. Cback * (1 - Aimage) where Cback = 1. We will fill the
1179                  * lower part of the buffer with the modified values.
1180                  *
1181                  * XXX: needs better solution
1182                  */
1183                 if (ctx->alpha) {
1184                         int adjust, i, j = 0;
1185                         int ncomps = ctx->samplesperpixel - ctx->extrasamples;
1186                         for (i = 0; i < byte_count; i+=ctx->samplesperpixel) {
1187                                 adjust = 255 - buf_data[i + ncomps];
1188                                 switch (ncomps) {
1189                                         case 1:
1190                                                 buf_data[j++] = buf_data[i] + adjust;
1191                                                 break;
1192                                         case 2:
1193                                                 buf_data[j++] = buf_data[i] + adjust;
1194                                                 buf_data[j++] = buf_data[i+1] + adjust;
1195                                                 break;
1196                                         case 3:
1197                                                 buf_data[j++] = buf_data[i] + adjust;
1198                                                 buf_data[j++] = buf_data[i+1] + adjust;
1199                                                 buf_data[j++] = buf_data[i+2] + adjust;
1200                                                 break;
1201                                 }
1202                         }
1203                         byte_count -= j;
1204                 }
1205
1206                 if (ctx->ascii85) {
1207 #if defined( EXP_ASCII85ENCODER )
1208                         ascii85_l = Ascii85EncodeBlock(ctx, ascii85_p, 1,
1209                                                        buf_data, byte_count);
1210
1211                         if ( ascii85_l > 0 )
1212                                 fwrite( ascii85_p, ascii85_l, 1, ctx->fd );
1213 #else
1214                         for (cp = buf_data; byte_count > 0; byte_count--)
1215                                 Ascii85Put(ctx, *cp++);
1216 #endif
1217                 }
1218                 else
1219                 {
1220                         for (cp = buf_data; byte_count > 0; byte_count--) {
1221                                 putc(hex[((*cp)>>4)&0xf], ctx->fd);
1222                                 putc(hex[(*cp)&0xf], ctx->fd);
1223                                 cp++;
1224
1225                                 if (--breaklen <= 0) {
1226                                         putc('\n', ctx->fd);
1227                                         breaklen = MAXLINE;
1228                                 }
1229                         }
1230                 }
1231
1232                 if ( !ctx->ascii85 ) {
1233                         if ( ctx->level2 || ctx->level3 )
1234                                 putc( '>', ctx->fd );
1235                         putc('\n', ctx->fd);
1236                 }
1237 #if !defined( EXP_ASCII85ENCODER )
1238                 else
1239                         Ascii85Flush(ctx);
1240 #endif
1241         }
1242
1243 #if defined( EXP_ASCII85ENCODER )
1244         if ( ascii85_p )
1245             _TIFFfree( ascii85_p );
1246 #endif
1247         _TIFFfree(buf_data);
1248 #ifdef ENABLE_BROKEN_BEGINENDDATA
1249         fputs("%%EndData\n", ctx->fd);
1250 #endif
1251         return(TRUE);
1252 }
1253
1254 void
1255 PSpage(TIFF2PSContext* ctx, TIFF* tif, uint32 w, uint32 h)
1256 {
1257         char *imageOp = "image";
1258
1259         if ( ctx->useImagemask && (ctx->bitspersample == 1) )
1260                 imageOp = "imagemask";
1261
1262         if ((ctx->level2 || ctx->level3) && PS_Lvl2page(ctx, tif, w, h))
1263                 return;
1264         ctx->ps_bytesperrow = ctx->tf_bytesperrow - (ctx->extrasamples * ctx->bitspersample / 8)*w;
1265         switch (ctx->photometric) {
1266         case PHOTOMETRIC_RGB:
1267                 if (ctx->planarconfiguration == PLANARCONFIG_CONTIG) {
1268                         fprintf(ctx->fd, "%s", RGBcolorimage);
1269                         PSColorContigPreamble(ctx, w, h, 3);
1270                         PSDataColorContig(ctx, tif, w, h, 3);
1271                 } else {
1272                         PSColorSeparatePreamble(ctx, w, h, 3);
1273                         PSDataColorSeparate(ctx, tif, w, h, 3);
1274                 }
1275                 break;
1276         case PHOTOMETRIC_SEPARATED:
1277                 /* XXX should emit CMYKcolorimage */
1278                 if (ctx->planarconfiguration == PLANARCONFIG_CONTIG) {
1279                         PSColorContigPreamble(ctx, w, h, 4);
1280                         PSDataColorContig(ctx, tif, w, h, 4);
1281                 } else {
1282                         PSColorSeparatePreamble(ctx, w, h, 4);
1283                         PSDataColorSeparate(ctx, tif, w, h, 4);
1284                 }
1285                 break;
1286         case PHOTOMETRIC_PALETTE:
1287                 fprintf(ctx->fd, "%s", RGBcolorimage);
1288                 PhotoshopBanner(ctx, w, h, 1, 3, "false 3 colorimage");
1289                 fprintf(ctx->fd, "/scanLine %ld string def\n",
1290                         (long) ctx->ps_bytesperrow * 3L);
1291                 fprintf(ctx->fd, "%lu %lu 8\n",
1292                         (unsigned long) w, (unsigned long) h);
1293                 fprintf(ctx->fd, "[%lu 0 0 -%lu 0 %lu]\n",
1294                         (unsigned long) w, (unsigned long) h,
1295                         (unsigned long) h);
1296                 fprintf(ctx->fd,
1297                         "{currentfile scanLine readhexstring pop} bind\n");
1298                 fprintf(ctx->fd, "false 3 colorimage\n");
1299                 PSDataPalette(ctx, tif, w, h);
1300                 break;
1301         case PHOTOMETRIC_MINISBLACK:
1302         case PHOTOMETRIC_MINISWHITE:
1303                 PhotoshopBanner(ctx, w, h, 1, 1, imageOp);
1304                 fprintf(ctx->fd, "/scanLine %ld string def\n",
1305                     (long) ctx->ps_bytesperrow);
1306                 fprintf(ctx->fd, "%lu %lu %d\n",
1307                     (unsigned long) w, (unsigned long) h, ctx->bitspersample);
1308                 fprintf(ctx->fd, "[%lu 0 0 -%lu 0 %lu]\n",
1309                     (unsigned long) w, (unsigned long) h, (unsigned long) h);
1310                 fprintf(ctx->fd,
1311                     "{currentfile scanLine readhexstring pop} bind\n");
1312                 fprintf(ctx->fd, "%s\n", imageOp);
1313                 PSDataBW(ctx, tif, w, h);
1314                 break;
1315         }
1316         putc('\n', ctx->fd);
1317 }
1318
1319 void
1320 PSColorContigPreamble(TIFF2PSContext* ctx, uint32 w, uint32 h, int nc)
1321 {
1322         ctx->ps_bytesperrow = nc * (ctx->tf_bytesperrow / ctx->samplesperpixel);
1323         PhotoshopBanner(ctx, w, h, 1, nc, "false %d colorimage");
1324         fprintf(ctx->fd, "/line %ld string def\n", (long) ctx->ps_bytesperrow);
1325         fprintf(ctx->fd, "%lu %lu %d\n",
1326             (unsigned long) w, (unsigned long) h, ctx->bitspersample);
1327         fprintf(ctx->fd, "[%lu 0 0 -%lu 0 %lu]\n",
1328             (unsigned long) w, (unsigned long) h, (unsigned long) h);
1329         fprintf(ctx->fd, "{currentfile line readhexstring pop} bind\n");
1330         fprintf(ctx->fd, "false %d colorimage\n", nc);
1331 }
1332
1333 void
1334 PSColorSeparatePreamble(TIFF2PSContext* ctx, uint32 w, uint32 h, int nc)
1335 {
1336         int i;
1337
1338         PhotoshopBanner(ctx, w, h, ctx->ps_bytesperrow, nc, "true %d colorimage");
1339         for (i = 0; i < nc; i++)
1340                 fprintf(ctx->fd, "/line%d %ld string def\n",
1341                     i, (long) ctx->ps_bytesperrow);
1342         fprintf(ctx->fd, "%lu %lu %d\n",
1343             (unsigned long) w, (unsigned long) h, ctx->bitspersample);
1344         fprintf(ctx->fd, "[%lu 0 0 -%lu 0 %lu] \n",
1345             (unsigned long) w, (unsigned long) h, (unsigned long) h);
1346         for (i = 0; i < nc; i++)
1347                 fprintf(ctx->fd, "{currentfile line%d readhexstring pop}bind\n", i);
1348         fprintf(ctx->fd, "true %d colorimage\n", nc);
1349 }
1350
1351 #define DOBREAK(len, howmany, fd) \
1352         if (((len) -= (howmany)) <= 0) {        \
1353                 putc('\n', fd);                 \
1354                 (len) = MAXLINE-(howmany);      \
1355         }
1356 #define PUTHEX(c,fd)    putc(hex[((c)>>4)&0xf],fd); putc(hex[(c)&0xf],fd)
1357
1358 void
1359 PSDataColorContig(TIFF2PSContext* ctx, TIFF* tif, uint32 w, uint32 h, int nc)
1360 {
1361         uint32 row;
1362         int breaklen = MAXLINE, cc, es = ctx->samplesperpixel - nc;
1363         unsigned char *tf_buf;
1364         unsigned char *cp, c;
1365
1366         (void) w;
1367         tf_buf = (unsigned char *) _TIFFmalloc(ctx->tf_bytesperrow);
1368         if (tf_buf == NULL) {
1369                 TIFFError(ctx->filename, "No space for scanline buffer");
1370                 return;
1371         }
1372         for (row = 0; row < h; row++) {
1373                 if (TIFFReadScanline(tif, tf_buf, row, 0) < 0)
1374                         break;
1375                 cp = tf_buf;
1376                 if (ctx->alpha) {
1377                         int adjust;
1378                         cc = 0;
1379                         for (; cc < ctx->tf_bytesperrow; cc += ctx->samplesperpixel) {
1380                                 DOBREAK(breaklen, nc, ctx->fd);
1381                                 /*
1382                                  * For images with ctx->alpha, matte against
1383                                  * a white background; i.e.
1384                                  *    Cback * (1 - Aimage)
1385                                  * where Cback = 1.
1386                                  */
1387                                 adjust = 255 - cp[nc];
1388                                 switch (nc) {
1389                                 case 4: c = *cp++ + adjust; PUTHEX(c,ctx->fd);
1390                                 case 3: c = *cp++ + adjust; PUTHEX(c,ctx->fd);
1391                                 case 2: c = *cp++ + adjust; PUTHEX(c,ctx->fd);
1392                                 case 1: c = *cp++ + adjust; PUTHEX(c,ctx->fd);
1393                                 }
1394                                 cp += es;
1395                         }
1396                 } else {
1397                         cc = 0;
1398                         for (; cc < ctx->tf_bytesperrow; cc += ctx->samplesperpixel) {
1399                                 DOBREAK(breaklen, nc, ctx->fd);
1400                                 switch (nc) {
1401                                 case 4: c = *cp++; PUTHEX(c,ctx->fd);
1402                                 case 3: c = *cp++; PUTHEX(c,ctx->fd);
1403                                 case 2: c = *cp++; PUTHEX(c,ctx->fd);
1404                                 case 1: c = *cp++; PUTHEX(c,ctx->fd);
1405                                 }
1406                                 cp += es;
1407                         }
1408                 }
1409         }
1410         _TIFFfree((char *) tf_buf);
1411 }
1412
1413 void
1414 PSDataColorSeparate(TIFF2PSContext* ctx, TIFF* tif, uint32 w, uint32 h, int nc)
1415 {
1416         uint32 row;
1417         int breaklen = MAXLINE, cc;
1418         tsample_t s, maxs;
1419         unsigned char *tf_buf;
1420         unsigned char *cp, c;
1421
1422         (void) w;
1423         tf_buf = (unsigned char *) _TIFFmalloc(ctx->tf_bytesperrow);
1424         if (tf_buf == NULL) {
1425                 TIFFError(ctx->filename, "No space for scanline buffer");
1426                 return;
1427         }
1428         maxs = (ctx->samplesperpixel > nc ? nc : ctx->samplesperpixel);
1429         for (row = 0; row < h; row++) {
1430                 for (s = 0; s < maxs; s++) {
1431                         if (TIFFReadScanline(tif, tf_buf, row, s) < 0)
1432                                 break;
1433                         for (cp = tf_buf, cc = 0; cc < ctx->tf_bytesperrow; cc++) {
1434                                 DOBREAK(breaklen, 1, ctx->fd);
1435                                 c = *cp++;
1436                                 PUTHEX(c,ctx->fd);
1437                         }
1438                 }
1439         }
1440         _TIFFfree((char *) tf_buf);
1441 }
1442
1443 #define PUTRGBHEX(c,fd) \
1444         PUTHEX(rmap[c],fd); PUTHEX(gmap[c],fd); PUTHEX(bmap[c],fd)
1445
1446 void
1447 PSDataPalette(TIFF2PSContext* ctx, TIFF* tif, uint32 w, uint32 h)
1448 {
1449         uint16 *rmap, *gmap, *bmap;
1450         uint32 row;
1451         int breaklen = MAXLINE, cc, nc;
1452         unsigned char *tf_buf;
1453         unsigned char *cp, c;
1454
1455         (void) w;
1456         if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) {
1457                 TIFFError(ctx->filename, "Palette image w/o \"Colormap\" tag");
1458                 return;
1459         }
1460         switch (ctx->bitspersample) {
1461         case 8: case 4: case 2: case 1:
1462                 break;
1463         default:
1464                 TIFFError(ctx->filename, "Depth %d not supported", ctx->bitspersample);
1465                 return;
1466         }
1467         nc = 3 * (8 / ctx->bitspersample);
1468         tf_buf = (unsigned char *) _TIFFmalloc(ctx->tf_bytesperrow);
1469         if (tf_buf == NULL) {
1470                 TIFFError(ctx->filename, "No space for scanline buffer");
1471                 return;
1472         }
1473         if (checkcmap(ctx, tif, 1<<ctx->bitspersample, rmap, gmap, bmap) == 16) {
1474                 int i;
1475 #define CVT(x)          ((unsigned short) (((x) * 255) / ((1U<<16)-1)))
1476                 for (i = (1<<ctx->bitspersample)-1; i >= 0; i--) {
1477                         rmap[i] = CVT(rmap[i]);
1478                         gmap[i] = CVT(gmap[i]);
1479                         bmap[i] = CVT(bmap[i]);
1480                 }
1481 #undef CVT
1482         }
1483         for (row = 0; row < h; row++) {
1484                 if (TIFFReadScanline(tif, tf_buf, row, 0) < 0)
1485                         break;
1486                 for (cp = tf_buf, cc = 0; cc < ctx->tf_bytesperrow; cc++) {
1487                         DOBREAK(breaklen, nc, ctx->fd);
1488                         switch (ctx->bitspersample) {
1489                         case 8:
1490                                 c = *cp++; PUTRGBHEX(c, ctx->fd);
1491                                 break;
1492                         case 4:
1493                                 c = *cp++; PUTRGBHEX(c&0xf, ctx->fd);
1494                                 c >>= 4;   PUTRGBHEX(c, ctx->fd);
1495                                 break;
1496                         case 2:
1497                                 c = *cp++; PUTRGBHEX(c&0x3, ctx->fd);
1498                                 c >>= 2;   PUTRGBHEX(c&0x3, ctx->fd);
1499                                 c >>= 2;   PUTRGBHEX(c&0x3, ctx->fd);
1500                                 c >>= 2;   PUTRGBHEX(c, ctx->fd);
1501                                 break;
1502                         case 1:
1503                                 c = *cp++; PUTRGBHEX(c&0x1, ctx->fd);
1504                                 c >>= 1;   PUTRGBHEX(c&0x1, ctx->fd);
1505                                 c >>= 1;   PUTRGBHEX(c&0x1, ctx->fd);
1506                                 c >>= 1;   PUTRGBHEX(c&0x1, ctx->fd);
1507                                 c >>= 1;   PUTRGBHEX(c&0x1, ctx->fd);
1508                                 c >>= 1;   PUTRGBHEX(c&0x1, ctx->fd);
1509                                 c >>= 1;   PUTRGBHEX(c&0x1, ctx->fd);
1510                                 c >>= 1;   PUTRGBHEX(c, ctx->fd);
1511                                 break;
1512                         }
1513                 }
1514         }
1515         _TIFFfree((char *) tf_buf);
1516 }
1517
1518 void
1519 PSDataBW(TIFF2PSContext* ctx, TIFF* tif, uint32 w, uint32 h)
1520 {
1521         int breaklen = MAXLINE;
1522         unsigned char* tf_buf;
1523         unsigned char* cp;
1524         tsize_t stripsize = TIFFStripSize(tif);
1525         tstrip_t s;
1526
1527 #if defined( EXP_ASCII85ENCODER )
1528         int     ascii85_l;              /* Length, in bytes, of ascii85_p[] data */
1529         uint8   *ascii85_p = 0;         /* Holds ASCII85 encoded data */
1530 #endif
1531
1532         (void) w; (void) h;
1533         tf_buf = (unsigned char *) _TIFFmalloc(stripsize);
1534         memset(tf_buf, 0, stripsize);
1535         if (tf_buf == NULL) {
1536                 TIFFError(ctx->filename, "No space for scanline buffer");
1537                 return;
1538         }
1539
1540 #if defined( EXP_ASCII85ENCODER )
1541         if ( ctx->ascii85 ) {
1542             /*
1543              * Allocate a buffer to hold the ASCII85 encoded data.  Note
1544              * that it is allocated with sufficient room to hold the
1545              * encoded data (5*stripsize/4) plus the EOD marker (+8)
1546              * and formatting line breaks.  The line breaks are more
1547              * than taken care of by using 6*stripsize/4 rather than
1548              * 5*stripsize/4.
1549              */
1550
1551             ascii85_p = _TIFFmalloc( (stripsize+(stripsize/2)) + 8 );
1552
1553             if ( !ascii85_p ) {
1554                 _TIFFfree( tf_buf );
1555
1556                 TIFFError( ctx->filename,
1557                            "Cannot allocate ASCII85 encoding buffer." );
1558                 return;
1559             }
1560         }
1561 #endif
1562
1563         if (ctx->ascii85)
1564                 Ascii85Init(ctx);
1565
1566         for (s = 0; s < TIFFNumberOfStrips(tif); s++) {
1567                 int cc = TIFFReadEncodedStrip(tif, s, tf_buf, stripsize);
1568                 if (cc < 0) {
1569                         TIFFError(ctx->filename, "Can't read strip");
1570                         break;
1571                 }
1572                 cp = tf_buf;
1573                 if (ctx->photometric == PHOTOMETRIC_MINISWHITE) {
1574                         for (cp += cc; --cp >= tf_buf;)
1575                                 *cp = ~*cp;
1576                         cp++;
1577                 }
1578                 if (ctx->ascii85) {
1579 #if defined( EXP_ASCII85ENCODER )
1580                         if (ctx->alpha) {
1581                                 int adjust, i;
1582                                 for (i = 0; i < cc; i+=2) {
1583                                         adjust = 255 - cp[i + 1];
1584                                     cp[i / 2] = cp[i] + adjust;
1585                                 }
1586                                 cc /= 2;
1587                         }
1588
1589                         ascii85_l = Ascii85EncodeBlock(ctx, ascii85_p, 1, cp,
1590                                                        cc);
1591
1592                         if ( ascii85_l > 0 )
1593                             fwrite( ascii85_p, ascii85_l, 1, ctx->fd );
1594 #else
1595                         while (cc-- > 0)
1596                                 Ascii85Put(ctx, *cp++);
1597 #endif /* EXP_ASCII85_ENCODER */
1598                 } else {
1599                         unsigned char c;
1600
1601                         if (ctx->alpha) {
1602                                 int adjust;
1603                                 while (cc-- > 0) {
1604                                         DOBREAK(breaklen, 1, ctx->fd);
1605                                         /*
1606                                          * For images with ctx->alpha, matte against
1607                                          * a white background; i.e.
1608                                          *    Cback * (1 - Aimage)
1609                                          * where Cback = 1.
1610                                          */
1611                                         adjust = 255 - cp[1];
1612                                         c = *cp++ + adjust; PUTHEX(c,ctx->fd);
1613                                         cp++, cc--;
1614                                 }
1615                         } else {
1616                                 while (cc-- > 0) {
1617                                         c = *cp++;
1618                                         DOBREAK(breaklen, 1, ctx->fd);
1619                                         PUTHEX(c, ctx->fd);
1620                                 }
1621                         }
1622                 }
1623         }
1624
1625         if ( !ctx->ascii85 )
1626         {
1627             if ( ctx->level2 || ctx->level3)
1628                 fputs(">\n", ctx->fd);
1629         }
1630 #if !defined( EXP_ASCII85ENCODER )
1631         else
1632             Ascii85Flush(ctx);
1633 #else
1634         if ( ascii85_p )
1635             _TIFFfree( ascii85_p );
1636 #endif
1637
1638         _TIFFfree(tf_buf);
1639 }
1640
1641 static void
1642 Ascii85Init(TIFF2PSContext *ctx)
1643 {
1644         ctx->ascii85breaklen = 2*MAXLINE;
1645         ctx->ascii85count = 0;
1646 }
1647
1648 static void
1649 Ascii85Encode(unsigned char* raw, char *buf)
1650 {
1651         uint32 word;
1652
1653         word = (((raw[0]<<8)+raw[1])<<16) + (raw[2]<<8) + raw[3];
1654         if (word != 0L) {
1655                 uint32 q;
1656                 uint16 w1;
1657
1658                 q = word / (85L*85*85*85);      /* actually only a byte */
1659                 buf[0] = (char) (q + '!');
1660
1661                 word -= q * (85L*85*85*85); q = word / (85L*85*85);
1662                 buf[1] = (char) (q + '!');
1663
1664                 word -= q * (85L*85*85); q = word / (85*85);
1665                 buf[2] = (char) (q + '!');
1666
1667                 w1 = (uint16) (word - q*(85L*85));
1668                 buf[3] = (char) ((w1 / 85) + '!');
1669                 buf[4] = (char) ((w1 % 85) + '!');
1670                 buf[5] = '\0';
1671         } else
1672                 buf[0] = 'z', buf[1] = '\0';
1673 }
1674
1675 void
1676 Ascii85Put(TIFF2PSContext *ctx, unsigned char code)
1677 {
1678         ctx->ascii85buf[ctx->ascii85count++] = code;
1679         if (ctx->ascii85count >= 4) {
1680                 unsigned char* p;
1681                 int n;
1682                 char buf[6];
1683
1684                 for (n = ctx->ascii85count, p = ctx->ascii85buf;
1685                      n >= 4; n -= 4, p += 4) {
1686                         char* cp;
1687                         Ascii85Encode(p, buf);
1688                         for (cp = buf; *cp; cp++) {
1689                                 putc(*cp, ctx->fd);
1690                                 if (--ctx->ascii85breaklen == 0) {
1691                                         putc('\n', ctx->fd);
1692                                         ctx->ascii85breaklen = 2*MAXLINE;
1693                                 }
1694                         }
1695                 }
1696                 _TIFFmemcpy(ctx->ascii85buf, p, n);
1697                 ctx->ascii85count = n;
1698         }
1699 }
1700
1701 void
1702 Ascii85Flush(TIFF2PSContext* ctx)
1703 {
1704         if (ctx->ascii85count > 0) {
1705                 char res[6];
1706                 _TIFFmemset(&ctx->ascii85buf[ctx->ascii85count], 0, 3);
1707                 Ascii85Encode(ctx->ascii85buf, res);
1708                 fwrite(res[0] == 'z' ? "!!!!" : res, ctx->ascii85count + 1, 1, ctx->fd);
1709         }
1710         fputs("~>\n", ctx->fd);
1711 }
1712 #if     defined( EXP_ASCII85ENCODER)
1713 \f
1714 #define A85BREAKCNTR    ctx->ascii85breaklen
1715 #define A85BREAKLEN     (2*MAXLINE)
1716
1717 /*****************************************************************************
1718 *
1719 * Name:         Ascii85EncodeBlock( ascii85_p, f_eod, raw_p, raw_l )
1720 *
1721 * Description:  This routine will encode the raw data in the buffer described
1722 *               by raw_p and raw_l into ASCII85 format and store the encoding
1723 *               in the buffer given by ascii85_p.
1724 *
1725 * Parameters:   ctx         -   TIFF2PS context
1726 *               ascii85_p   -   A buffer supplied by the caller which will
1727 *                               contain the encoded ASCII85 data.
1728 *               f_eod       -   Flag: Nz means to end the encoded buffer with
1729 *                               an End-Of-Data marker.
1730 *               raw_p       -   Pointer to the buffer of data to be encoded
1731 *               raw_l       -   Number of bytes in raw_p[] to be encoded
1732 *
1733 * Returns:      (int)   <   0   Error, see errno
1734 *                       >=  0   Number of bytes written to ascii85_p[].
1735 *
1736 * Notes:        An external variable given by A85BREAKCNTR is used to
1737 *               determine when to insert newline characters into the
1738 *               encoded data.  As each byte is placed into ascii85_p this
1739 *               external is decremented.  If the variable is decrement to
1740 *               or past zero then a newline is inserted into ascii85_p
1741 *               and the A85BREAKCNTR is then reset to A85BREAKLEN.
1742 *                   Note:  for efficiency reasons the A85BREAKCNTR variable
1743 *                          is not actually checked on *every* character
1744 *                          placed into ascii85_p but often only for every
1745 *                          5 characters.
1746 *
1747 *               THE CALLER IS RESPONSIBLE FOR ENSURING THAT ASCII85_P[] IS
1748 *               SUFFICIENTLY LARGE TO THE ENCODED DATA!
1749 *                   You will need at least 5 * (raw_l/4) bytes plus space for
1750 *                   newline characters and space for an EOD marker (if
1751 *                   requested).  A safe calculation is to use 6*(raw_l/4) + 8
1752 *                   to size ascii85_p.
1753 *
1754 *****************************************************************************/
1755
1756 int Ascii85EncodeBlock( TIFF2PSContext *ctx, uint8 * ascii85_p,
1757                         unsigned f_eod, const uint8 * raw_p, int raw_l )
1758
1759 {
1760     char                        ascii85[5];     /* Encoded 5 tuple */
1761     int                         ascii85_l;      /* Number of bytes written to ascii85_p[] */
1762     int                         rc;             /* Return code */
1763     uint32                      val32;          /* Unencoded 4 tuple */
1764
1765     ascii85_l = 0;                              /* Nothing written yet */
1766
1767     if ( raw_p )
1768     {
1769         --raw_p;                                /* Prepare for pre-increment fetches */
1770
1771         for ( ; raw_l > 3; raw_l -= 4 )
1772         {
1773             val32  = *(++raw_p) << 24;
1774             val32 += *(++raw_p) << 16;
1775             val32 += *(++raw_p) <<  8;
1776             val32 += *(++raw_p);
1777
1778             if ( val32 == 0 )                   /* Special case */
1779             {
1780                 ascii85_p[ascii85_l] = 'z';
1781                 rc = 1;
1782             }
1783
1784             else
1785             {
1786                 ascii85[4] = (char) ((val32 % 85) + 33);
1787                 val32 /= 85;
1788
1789                 ascii85[3] = (char) ((val32 % 85) + 33);
1790                 val32 /= 85;
1791
1792                 ascii85[2] = (char) ((val32 % 85) + 33);
1793                 val32 /= 85;
1794
1795                 ascii85[1] = (char) ((val32 % 85) + 33);
1796                 ascii85[0] = (char) ((val32 / 85) + 33);
1797
1798                 _TIFFmemcpy( &ascii85_p[ascii85_l], ascii85, sizeof(ascii85) );
1799                 rc = sizeof(ascii85);
1800             }
1801
1802             ascii85_l += rc;
1803
1804             if ( (A85BREAKCNTR -= rc) <= 0 )
1805             {
1806                 ascii85_p[ascii85_l] = '\n';
1807                 ++ascii85_l;
1808                 A85BREAKCNTR = A85BREAKLEN;
1809             }
1810         }
1811
1812         /*
1813          * Output any straggler bytes:
1814          */
1815
1816         if ( raw_l > 0 )
1817         {
1818             int             len;                /* Output this many bytes */
1819
1820             len = raw_l + 1;
1821             val32 = *++raw_p << 24;             /* Prime the pump */
1822
1823             if ( --raw_l > 0 )  val32 += *(++raw_p) << 16;
1824             if ( --raw_l > 0 )  val32 += *(++raw_p) <<  8;
1825
1826             val32 /= 85;
1827
1828             ascii85[3] = (char) ((val32 % 85) + 33);
1829             val32 /= 85;
1830
1831             ascii85[2] = (char) ((val32 % 85) + 33);
1832             val32 /= 85;
1833
1834             ascii85[1] = (char) ((val32 % 85) + 33);
1835             ascii85[0] = (char) ((val32 / 85) + 33);
1836
1837             _TIFFmemcpy( &ascii85_p[ascii85_l], ascii85, len );
1838             ascii85_l += len;
1839         }
1840     }
1841
1842     /*
1843      * If requested add an ASCII85 End Of Data marker:
1844      */
1845
1846     if ( f_eod )
1847     {
1848         ascii85_p[ascii85_l++] = '~';
1849         ascii85_p[ascii85_l++] = '>';
1850         ascii85_p[ascii85_l++] = '\n';
1851     }
1852
1853     return ( ascii85_l );
1854
1855 }   /* Ascii85EncodeBlock() */
1856
1857 #endif  /* EXP_ASCII85ENCODER */
1858
1859 /* vim: set ts=8 sts=8 sw=8 noet: */