Blame tools/tiff2ps.c

Packit 7838c8
/* $Id: tiff2ps.c,v 1.56 2017-04-27 15:46:22 erouault Exp $ */
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Copyright (c) 1988-1997 Sam Leffler
Packit 7838c8
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
Packit 7838c8
 *
Packit 7838c8
 * Permission to use, copy, modify, distribute, and sell this software and 
Packit 7838c8
 * its documentation for any purpose is hereby granted without fee, provided
Packit 7838c8
 * that (i) the above copyright notices and this permission notice appear in
Packit 7838c8
 * all copies of the software and related documentation, and (ii) the names of
Packit 7838c8
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
Packit 7838c8
 * publicity relating to the software without the specific, prior written
Packit 7838c8
 * permission of Sam Leffler and Silicon Graphics.
Packit 7838c8
 * 
Packit 7838c8
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
Packit 7838c8
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
Packit 7838c8
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
Packit 7838c8
 * 
Packit 7838c8
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
Packit 7838c8
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
Packit 7838c8
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
Packit 7838c8
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
Packit 7838c8
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
Packit 7838c8
 * OF THIS SOFTWARE.
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
#include "tif_config.h"
Packit 7838c8
Packit 7838c8
#include <stdio.h>
Packit 7838c8
#include <stdlib.h>			/* for atof */
Packit 7838c8
#include <math.h>
Packit 7838c8
#include <time.h>
Packit 7838c8
#include <string.h>
Packit 7838c8
Packit 7838c8
#ifdef HAVE_UNISTD_H
Packit 7838c8
# include <unistd.h>
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
#ifdef NEED_LIBPORT
Packit 7838c8
# include "libport.h"
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
#include "tiffio.h"
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Revision history
Packit 7838c8
 * 2013-Jan-21
Packit 7838c8
 *    Richard Nolde: Fix bug in auto rotate option code. Once a
Packit 7838c8
 *    rotation angle was set by the auto rotate check, it was
Packit 7838c8
 *    retained for all pages that followed instead of being
Packit 7838c8
 *    retested for each page.
Packit 7838c8
 *
Packit 7838c8
 * 2010-Sep-17
Packit 7838c8
 *    Richard Nolde: Reinstate code from Feb 2009 that never got
Packit 7838c8
 *    accepted into CVS with major modifications to handle -H and -W
Packit 7838c8
 *    options. Replaced original PlaceImage function with several
Packit 7838c8
 *    new functions that make support for multiple output pages
Packit 7838c8
 *    from a single image easier to understand. Added additional
Packit 7838c8
 *    warning messages for incompatible command line options.
Packit 7838c8
 *    Add new command line options to specify PageOrientation
Packit 7838c8
 *    Document Structuring Comment for landscape or portrait
Packit 7838c8
 *    and code to determine the values from ouput width and height
Packit 7838c8
 *    if not specified on the command line.
Packit 7838c8
 *    Add new command line option to specify document creator
Packit 7838c8
 *    as an alterntive to the string "tiff2ps" following model
Packit 7838c8
 *    of patch submitted by Thomas Jarosch for specifiying a
Packit 7838c8
 *    document title which is also supported now.
Packit 7838c8
 *
Packit 7838c8
 * 2009-Feb-11
Packit 7838c8
 *    Richard Nolde: Added support for rotations of 90, 180, 270
Packit 7838c8
 *    and auto using -r <90|180|270|auto>.  Auto picks the best
Packit 7838c8
 *    fit for the image on the specified paper size (eg portrait
Packit 7838c8
 *    or landscape) if -h or -w is specified. Rotation is in
Packit 7838c8
 *    degrees counterclockwise since that is how Postscript does
Packit 7838c8
 *    it. The auto opption rotates the image 90 degrees ccw to
Packit 7838c8
 *    produce landscape if that is a better fit than portait.
Packit 7838c8
 *
Packit 7838c8
 *    Cleaned up code in TIFF2PS and broke into smaller functions
Packit 7838c8
 *    to simplify rotations.
Packit 7838c8
 *
Packit 7838c8
 *    Identified incompatible options and returned errors, eg
Packit 7838c8
 *    -i for imagemask operator is only available for Level2 or
Packit 7838c8
 *    Level3 Postscript in the current implmentation since there
Packit 7838c8
 *    is a difference in the way the operands are called for Level1
Packit 7838c8
 *    and there is no function to provide the Level1 version.
Packit 7838c8
 *    -H was not handled properly if -h and/or -w were specified.
Packit 7838c8
 *    It should only clip the masked images if the scaled image
Packit 7838c8
 *    exceeds the maxPageHeight specified with -H.
Packit 7838c8
 *  
Packit 7838c8
 *    New design allows for all of the following combinations:
Packit 7838c8
 *    Conversion of TIFF to Postscript with optional rotations
Packit 7838c8
 *    of 90, 180, 270, or auto degrees counterclockwise
Packit 7838c8
 *    Conversion of TIFF to Postscript with entire image scaled
Packit 7838c8
 *    to maximum of values spedified with -h or -w while
Packit 7838c8
 *    maintaining aspect ratio. Same rotations apply.
Packit 7838c8
 *    Conversion of TIFF to Postscript with clipping of output
Packit 7838c8
 *    viewport to height specified with -H, producing multiple
Packit 7838c8
 *    pages at this height and original width as needed.
Packit 7838c8
 *    Same rotations apply.
Packit 7838c8
 *    Conversion of TIFF to Postscript with image scaled to 
Packit 7838c8
 *    maximum specified by -h and -w and the resulting scaled
Packit 7838c8
 *    image is presented in an output viewport clipped by -H height.
Packit 7838c8
 *    The same rotations apply.
Packit 7838c8
 *
Packit 7838c8
 *    Added maxPageWidth option using -W flag. MaxPageHeight and
Packit 7838c8
 *    MaxPageWidth are mutually exclusive since the aspect ratio
Packit 7838c8
 *    cannot be maintained if you set both.
Packit 7838c8
 *    Rewrote PlaceImage to allow maxPageHeight and maxPageWidth
Packit 7838c8
 *    options to work with values smaller or larger than the
Packit 7838c8
 *    physical paper size and still preserve the aspect ratio.
Packit 7838c8
 *    This is accomplished by creating multiple pages across
Packit 7838c8
 *    as well as down if need be.
Packit 7838c8
 *
Packit 7838c8
 * 2001-Mar-21
Packit 7838c8
 *    I (Bruce A. Mallett) added this revision history comment ;)
Packit 7838c8
 *
Packit 7838c8
 *    Fixed PS_Lvl2page() code which outputs non-ASCII85 raw
Packit 7838c8
 *    data.  Moved test for when to output a line break to
Packit 7838c8
 *    *after* the output of a character.  This just serves
Packit 7838c8
 *    to fix an eye-nuisance where the first line of raw
Packit 7838c8
 *    data was one character shorter than subsequent lines.
Packit 7838c8
 *
Packit 7838c8
 *    Added an experimental ASCII85 encoder which can be used
Packit 7838c8
 *    only when there is a single buffer of bytes to be encoded.
Packit 7838c8
 *    This version is much faster at encoding a straight-line
Packit 7838c8
 *    buffer of data because it can avoid a lot of the loop
Packit 7838c8
 *    overhead of the byte-by-byte version.  To use this version
Packit 7838c8
 *    you need to define EXP_ASCII85ENCODER (experimental ...).
Packit 7838c8
 *
Packit 7838c8
 *    Added bug fix given by Michael Schmidt to PS_Lvl2page()
Packit 7838c8
 *    in which an end-of-data marker ('>') was not being output
Packit 7838c8
 *    when producing non-ASCII85 encoded PostScript Level 2
Packit 7838c8
 *    data.
Packit 7838c8
 *
Packit 7838c8
 *    Fixed PS_Lvl2colorspace() so that it no longer assumes that
Packit 7838c8
 *    a TIFF having more than 2 planes is a CMYK.  This routine
Packit 7838c8
 *    no longer looks at the samples per pixel but instead looks
Packit 7838c8
 *    at the "photometric" value.  This change allows support of
Packit 7838c8
 *    CMYK TIFFs.
Packit 7838c8
 *
Packit 7838c8
 *    Modified the PostScript L2 imaging loop so as to test if
Packit 7838c8
 *    the input stream is still open before attempting to do a
Packit 7838c8
 *    flushfile on it.  This was done because some RIPs close
Packit 7838c8
 *    the stream after doing the image operation.
Packit 7838c8
 *
Packit 7838c8
 *    Got rid of the realloc() being done inside a loop in the
Packit 7838c8
 *    PSRawDataBW() routine.  The code now walks through the
Packit 7838c8
 *    byte-size array outside the loop to determine the largest
Packit 7838c8
 *    size memory block that will be needed.
Packit 7838c8
 *
Packit 7838c8
 *    Added "-m" switch to ask tiff2ps to, where possible, use the
Packit 7838c8
 *    "imagemask" operator instead of the "image" operator.
Packit 7838c8
 *
Packit 7838c8
 *    Added the "-i #" switch to allow interpolation to be disabled.
Packit 7838c8
 *
Packit 7838c8
 *    Unrolled a loop or two to improve performance.
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Define EXP_ASCII85ENCODER if you want to use an experimental
Packit 7838c8
 * version of the ASCII85 encoding routine.  The advantage of
Packit 7838c8
 * using this routine is that tiff2ps will convert to ASCII85
Packit 7838c8
 * encoding at between 3 and 4 times the speed as compared to
Packit 7838c8
 * using the old (non-experimental) encoder.  The disadvantage
Packit 7838c8
 * is that you will be using a new (and unproven) encoding
Packit 7838c8
 * routine.  So user beware, you have been warned!
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
#define	EXP_ASCII85ENCODER
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * NB: this code assumes uint32 works with printf's %l[ud].
Packit 7838c8
 */
Packit 7838c8
#ifndef TRUE
Packit 7838c8
#define	TRUE	1
Packit 7838c8
#define	FALSE	0
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
int	ascii85 = FALSE;		/* use ASCII85 encoding */
Packit 7838c8
int	interpolate = TRUE;		/* interpolate level2 image */
Packit 7838c8
int	level2 = FALSE;			/* generate PostScript level 2 */
Packit 7838c8
int	level3 = FALSE;			/* generate PostScript level 3 */
Packit 7838c8
int	printAll = FALSE;		/* print all images in file */
Packit 7838c8
int	generateEPSF = TRUE;		/* generate Encapsulated PostScript */
Packit 7838c8
int	PSduplex = FALSE;		/* enable duplex printing */
Packit 7838c8
int	PStumble = FALSE;		/* enable top edge binding */
Packit 7838c8
int	PSavoiddeadzone = TRUE;		/* enable avoiding printer deadzone */
Packit 7838c8
double	maxPageHeight = 0;		/* maximum height to select from image and print per page */
Packit 7838c8
double	maxPageWidth  = 0;		/* maximum width  to select from image and print per page */
Packit 7838c8
double	splitOverlap = 0;		/* amount for split pages to overlag */
Packit 7838c8
int	rotation = 0;                   /* optional value for rotation angle */
Packit 7838c8
int     auto_rotate = 0;                /* rotate image for best fit on the page */
Packit 7838c8
char	*filename = NULL;		/* input filename */
Packit 7838c8
char    *title = NULL;                  /* optional document title string */
Packit 7838c8
char    *creator = NULL;                /* optional document creator string */
Packit 7838c8
char    pageOrientation[12];            /* set optional PageOrientation DSC to Landscape or Portrait */
Packit 7838c8
int	useImagemask = FALSE;		/* Use imagemask instead of image operator */
Packit 7838c8
uint16	res_unit = 0;			/* Resolution units: 2 - inches, 3 - cm */
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * ASCII85 Encoding Support.
Packit 7838c8
 */
Packit 7838c8
unsigned char ascii85buf[10];
Packit 7838c8
int	ascii85count;
Packit 7838c8
int	ascii85breaklen;
Packit 7838c8
Packit 7838c8
int	TIFF2PS(FILE*, TIFF*, double, double, double, double, int);
Packit 7838c8
void	PSpage(FILE*, TIFF*, uint32, uint32);
Packit 7838c8
void	PSColorContigPreamble(FILE*, uint32, uint32, int);
Packit 7838c8
void	PSColorSeparatePreamble(FILE*, uint32, uint32, int);
Packit 7838c8
void	PSDataColorContig(FILE*, TIFF*, uint32, uint32, int);
Packit 7838c8
void	PSDataColorSeparate(FILE*, TIFF*, uint32, uint32, int);
Packit 7838c8
void	PSDataPalette(FILE*, TIFF*, uint32, uint32);
Packit 7838c8
void	PSDataBW(FILE*, TIFF*, uint32, uint32);
Packit 7838c8
void	PSRawDataBW(FILE*, TIFF*, uint32, uint32);
Packit 7838c8
void	Ascii85Init(void);
Packit 7838c8
void	Ascii85Put(unsigned char code, FILE* fd);
Packit 7838c8
void	Ascii85Flush(FILE* fd);
Packit 7838c8
void    PSHead(FILE*, double, double, double, double);
Packit 7838c8
void	PSTail(FILE*, int);
Packit 7838c8
int     psStart(FILE *, int, int, int *, double *, double, double, double,
Packit 7838c8
                double, double, double, double, double, double, double);
Packit 7838c8
int     psPageSize(FILE *, int, double, double, double, double, double, double);
Packit 7838c8
int     psRotateImage(FILE *, int, double, double, double, double);
Packit 7838c8
int     psMaskImage(FILE *, TIFF *, int, int, int *, double, double,
Packit 7838c8
		    double, double, double, double, double, double, double);
Packit 7838c8
int     psScaleImage(FILE *, double, int, int, double, double, double, double,
Packit 7838c8
                     double, double);
Packit 7838c8
int     get_viewport (double, double, double, double, double *, double *, int);
Packit 7838c8
int     exportMaskedImage(FILE *, double, double, double, double, int, int,
Packit 7838c8
			  double, double, double, int, int);
Packit 7838c8
Packit 7838c8
#if	defined( EXP_ASCII85ENCODER)
Packit 7838c8
tsize_t Ascii85EncodeBlock( uint8 * ascii85_p, unsigned f_eod, const uint8 * raw_p, tsize_t raw_l );
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
static	void usage(int);
Packit 7838c8
Packit 7838c8
int
Packit 7838c8
main(int argc, char* argv[])
Packit 7838c8
{
Packit 7838c8
	int dirnum = -1, c, np = 0;
Packit 7838c8
	int centered = 0;
Packit 7838c8
	double bottommargin = 0;
Packit 7838c8
	double leftmargin = 0;
Packit 7838c8
	double pageWidth = 0;
Packit 7838c8
	double pageHeight = 0;
Packit 7838c8
	uint32 diroff = 0;
Packit 7838c8
#if !HAVE_DECL_OPTARG
Packit 7838c8
	extern char *optarg;
Packit 7838c8
	extern int optind;
Packit 7838c8
#endif
Packit 7838c8
	FILE* output = stdout;
Packit 7838c8
Packit 7838c8
        pageOrientation[0] = '\0';
Packit 7838c8
Packit 7838c8
	while ((c = getopt(argc, argv, "b:d:h:H:W:L:i:w:l:o:O:P:C:r:t:acemxyzps1238DT")) != -1)
Packit 7838c8
		switch (c) {
Packit 7838c8
		case 'b':
Packit 7838c8
			bottommargin = atof(optarg);
Packit 7838c8
			break;
Packit 7838c8
		case 'c':
Packit 7838c8
			centered = 1;
Packit 7838c8
			break;
Packit 7838c8
		case 'C':
Packit 7838c8
			creator = optarg;
Packit 7838c8
			break;
Packit 7838c8
		case 'd': /* without -a, this only processes one image at this IFD */
Packit 7838c8
			dirnum = atoi(optarg);
Packit 7838c8
			break;
Packit 7838c8
		case 'D':
Packit 7838c8
			PSduplex = TRUE;
Packit 7838c8
			break;
Packit 7838c8
		case 'i':
Packit 7838c8
			interpolate = atoi(optarg) ? TRUE:FALSE;
Packit 7838c8
			break;
Packit 7838c8
		case 'T':
Packit 7838c8
			PStumble = TRUE;
Packit 7838c8
			break;
Packit 7838c8
		case 'e':
Packit 7838c8
                        PSavoiddeadzone = FALSE;
Packit 7838c8
			generateEPSF = TRUE;
Packit 7838c8
			break;
Packit 7838c8
		case 'h':
Packit 7838c8
			pageHeight = atof(optarg);
Packit 7838c8
			break;
Packit 7838c8
		case 'H':
Packit 7838c8
			maxPageHeight = atof(optarg);
Packit 7838c8
			break;
Packit 7838c8
		case 'W':
Packit 7838c8
			maxPageWidth = atof(optarg);
Packit 7838c8
			break;
Packit 7838c8
		case 'L':
Packit 7838c8
			splitOverlap = atof(optarg);
Packit 7838c8
			break;
Packit 7838c8
		case 'm':
Packit 7838c8
			useImagemask = TRUE;
Packit 7838c8
			break;
Packit 7838c8
		case 'o':
Packit 7838c8
		        switch (optarg[0])
Packit 7838c8
                          {
Packit 7838c8
                          case '0':
Packit 7838c8
                          case '1':
Packit 7838c8
                          case '2':
Packit 7838c8
                          case '3':
Packit 7838c8
                          case '4':
Packit 7838c8
                          case '5':
Packit 7838c8
                          case '6':
Packit 7838c8
                          case '7':
Packit 7838c8
                          case '8':
Packit 7838c8
                          case '9': diroff = (uint32) strtoul(optarg, NULL, 0);
Packit 7838c8
			          break;
Packit 7838c8
                          default: TIFFError ("-o", "Offset must be a numeric value.");
Packit 7838c8
			    exit (1);
Packit 7838c8
			  }
Packit 7838c8
			break;
Packit 7838c8
		case 'O':		/* XXX too bad -o is already taken */
Packit 7838c8
			output = fopen(optarg, "w");
Packit 7838c8
			if (output == NULL) {
Packit 7838c8
				fprintf(stderr,
Packit 7838c8
				    "%s: %s: Cannot open output file.\n",
Packit 7838c8
				    argv[0], optarg);
Packit 7838c8
				exit(-2);
Packit 7838c8
			}
Packit 7838c8
			break;
Packit 7838c8
		case 'P':
Packit 7838c8
                        switch (optarg[0])
Packit 7838c8
                          {
Packit 7838c8
                          case 'l':
Packit 7838c8
                          case 'L': strcpy (pageOrientation, "Landscape");
Packit 7838c8
			            break; 
Packit 7838c8
                          case 'p':
Packit 7838c8
                          case 'P': strcpy (pageOrientation, "Portrait");
Packit 7838c8
			            break; 
Packit 7838c8
                          default: TIFFError ("-P", "Page orientation must be Landscape or Portrait");
Packit 7838c8
			           exit (-1);
Packit 7838c8
			  }
Packit 7838c8
			break;
Packit 7838c8
		case 'l':
Packit 7838c8
			leftmargin = atof(optarg);
Packit 7838c8
			break;
Packit 7838c8
		case 'a': /* removed fall through to generate warning below, R Nolde 09-01-2010 */
Packit 7838c8
			printAll = TRUE;
Packit 7838c8
			break;
Packit 7838c8
		case 'p':
Packit 7838c8
			generateEPSF = FALSE;
Packit 7838c8
			break;
Packit 7838c8
		case 'r':
Packit 7838c8
                        if (strcmp (optarg, "auto") == 0)
Packit 7838c8
			  {
Packit 7838c8
                          rotation = 0;
Packit 7838c8
                          auto_rotate = TRUE;
Packit 7838c8
                          }
Packit 7838c8
                        else
Packit 7838c8
			  {
Packit 7838c8
 			  rotation = atoi(optarg);
Packit 7838c8
                          auto_rotate = FALSE;
Packit 7838c8
			  }
Packit 7838c8
                        switch (rotation)
Packit 7838c8
                          {
Packit 7838c8
			  case   0:
Packit 7838c8
                          case  90:
Packit 7838c8
                          case 180:
Packit 7838c8
                          case 270:
Packit 7838c8
			    break;
Packit 7838c8
			  default:
Packit 7838c8
                            fprintf (stderr, "Rotation angle must be 90, 180, 270 (degrees ccw) or auto\n");
Packit 7838c8
			    exit (-1);
Packit 7838c8
			  }
Packit 7838c8
			break;
Packit 7838c8
		case 's':
Packit 7838c8
			printAll = FALSE;
Packit 7838c8
			break;
Packit 7838c8
                case 't':
Packit 7838c8
                        title = optarg;
Packit 7838c8
                        break;
Packit 7838c8
		case 'w':
Packit 7838c8
			pageWidth = atof(optarg);
Packit 7838c8
			break;
Packit 7838c8
		case 'z':
Packit 7838c8
			PSavoiddeadzone = FALSE;
Packit 7838c8
			break;
Packit 7838c8
		case '1':
Packit 7838c8
			level2 = FALSE;
Packit 7838c8
			level3 = FALSE;
Packit 7838c8
			ascii85 = FALSE;
Packit 7838c8
			break;
Packit 7838c8
		case '2':
Packit 7838c8
			level2 = TRUE;
Packit 7838c8
			ascii85 = TRUE;			/* default to yes */
Packit 7838c8
			break;
Packit 7838c8
		case '3':
Packit 7838c8
			level3 = TRUE;
Packit 7838c8
			ascii85 = TRUE;			/* default to yes */
Packit 7838c8
			break;
Packit 7838c8
		case '8':
Packit 7838c8
			ascii85 = FALSE;
Packit 7838c8
			break;
Packit 7838c8
		case 'x':
Packit 7838c8
			res_unit = RESUNIT_CENTIMETER;
Packit 7838c8
			break;
Packit 7838c8
		case 'y':
Packit 7838c8
			res_unit = RESUNIT_INCH;
Packit 7838c8
			break;
Packit 7838c8
		case '?':
Packit 7838c8
			usage(-1);
Packit 7838c8
		}
Packit 7838c8
Packit 7838c8
        if (useImagemask == TRUE)
Packit 7838c8
          {
Packit 7838c8
	  if ((level2 == FALSE) && (level3 == FALSE))
Packit 7838c8
            {
Packit 7838c8
	    TIFFError ("-m "," imagemask operator requres Postscript Level2 or Level3");
Packit 7838c8
	    exit (1);
Packit 7838c8
            }
Packit 7838c8
          }
Packit 7838c8
Packit 7838c8
        if (pageWidth && (maxPageWidth > pageWidth))
Packit 7838c8
	  {
Packit 7838c8
	  TIFFError ("-W", "Max viewport width cannot exceed page width");
Packit 7838c8
	  exit (1);
Packit 7838c8
          }
Packit 7838c8
Packit 7838c8
        /* auto rotate requires a specified page width and height */
Packit 7838c8
        if (auto_rotate == TRUE)
Packit 7838c8
          {
Packit 7838c8
	    /*
Packit 7838c8
	  if ((pageWidth == 0) || (pageHeight == 0))
Packit 7838c8
	    TIFFWarning ("-r auto", " requires page height and width specified with -h and -w");
Packit 7838c8
	    */
Packit 7838c8
          if ((maxPageWidth > 0) || (maxPageHeight > 0))
Packit 7838c8
            {
Packit 7838c8
	    TIFFError ("-r auto", " is incompatible with maximum page width/height specified by -H or -W");
Packit 7838c8
            exit (1);
Packit 7838c8
            }
Packit 7838c8
          }
Packit 7838c8
        if ((maxPageWidth > 0) && (maxPageHeight > 0))
Packit 7838c8
            {
Packit 7838c8
	    TIFFError ("-H and -W", " Use only one of -H or -W to define a viewport");
Packit 7838c8
            exit (1);
Packit 7838c8
            }
Packit 7838c8
Packit 7838c8
        if ((generateEPSF == TRUE) && (printAll == TRUE))
Packit 7838c8
          {
Packit 7838c8
	  TIFFError(" -e and -a", "Warning: Cannot generate Encapsulated Postscript for multiple images");
Packit 7838c8
	  generateEPSF = FALSE;
Packit 7838c8
          }
Packit 7838c8
Packit 7838c8
        if ((generateEPSF == TRUE) && (PSduplex == TRUE))
Packit 7838c8
          {
Packit 7838c8
	  TIFFError(" -e and -D", "Warning: Encapsulated Postscript does not support Duplex option");
Packit 7838c8
	  PSduplex = FALSE;
Packit 7838c8
          }
Packit 7838c8
Packit 7838c8
        if ((generateEPSF == TRUE) && (PStumble == TRUE))
Packit 7838c8
          {
Packit 7838c8
	  TIFFError(" -e and -T", "Warning: Encapsulated Postscript does not support Top Edge Binding option");
Packit 7838c8
	  PStumble = FALSE;
Packit 7838c8
          }
Packit 7838c8
Packit 7838c8
        if ((generateEPSF == TRUE) && (PSavoiddeadzone == TRUE))
Packit 7838c8
	  PSavoiddeadzone = FALSE;
Packit 7838c8
Packit 7838c8
	for (; argc - optind > 0; optind++) {
Packit 7838c8
		TIFF* tif = TIFFOpen(filename = argv[optind], "r");
Packit 7838c8
		if (tif != NULL) {
Packit 7838c8
			if (dirnum != -1
Packit 7838c8
                            && !TIFFSetDirectory(tif, (tdir_t)dirnum))
Packit 7838c8
                        {
Packit 7838c8
                                TIFFClose(tif);
Packit 7838c8
				return (-1);
Packit 7838c8
                        }
Packit 7838c8
			else if (diroff != 0 &&
Packit 7838c8
			    !TIFFSetSubDirectory(tif, diroff))
Packit 7838c8
                        {
Packit 7838c8
                                TIFFClose(tif);
Packit 7838c8
				return (-1);
Packit 7838c8
                        }
Packit 7838c8
			np = TIFF2PS(output, tif, pageWidth, pageHeight,
Packit 7838c8
				     leftmargin, bottommargin, centered);
Packit 7838c8
                        if (np < 0)
Packit 7838c8
                          {
Packit 7838c8
			  TIFFError("Error", "Unable to process %s", filename);
Packit 7838c8
                          }
Packit 7838c8
			TIFFClose(tif);
Packit 7838c8
		}
Packit 7838c8
	}
Packit 7838c8
	if (np)
Packit 7838c8
		PSTail(output, np);
Packit 7838c8
	else
Packit 7838c8
		usage(-1);
Packit 7838c8
	if (output != stdout)
Packit 7838c8
		fclose(output);
Packit 7838c8
	return (0);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static	uint16 samplesperpixel;
Packit 7838c8
static	uint16 bitspersample;
Packit 7838c8
static	uint16 planarconfiguration;
Packit 7838c8
static	uint16 photometric;
Packit 7838c8
static	uint16 compression;
Packit 7838c8
static	uint16 extrasamples;
Packit 7838c8
static	int alpha;
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
checkImage(TIFF* tif)
Packit 7838c8
{
Packit 7838c8
	switch (photometric) {
Packit 7838c8
	case PHOTOMETRIC_YCBCR:
Packit 7838c8
		if ((compression == COMPRESSION_JPEG || compression == COMPRESSION_OJPEG)
Packit 7838c8
			&& planarconfiguration == PLANARCONFIG_CONTIG) {
Packit 7838c8
			/* can rely on libjpeg to convert to RGB */
Packit 7838c8
			TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE,
Packit 7838c8
				     JPEGCOLORMODE_RGB);
Packit 7838c8
			photometric = PHOTOMETRIC_RGB;
Packit 7838c8
		} else {
Packit 7838c8
			if (level2 || level3)
Packit 7838c8
				break;
Packit 7838c8
			TIFFError(filename, "Can not handle image with %s",
Packit 7838c8
			    "PhotometricInterpretation=YCbCr");
Packit 7838c8
			return (0);
Packit 7838c8
		}
Packit 7838c8
		/* fall thru... */
Packit 7838c8
	case PHOTOMETRIC_RGB:
Packit 7838c8
		if (alpha && bitspersample != 8) {
Packit 7838c8
			TIFFError(filename,
Packit 7838c8
			    "Can not handle %d-bit/sample RGB image with alpha",
Packit 7838c8
			    bitspersample);
Packit 7838c8
			return (0);
Packit 7838c8
		}
Packit 7838c8
		/* fall thru... */
Packit 7838c8
	case PHOTOMETRIC_SEPARATED:
Packit 7838c8
	case PHOTOMETRIC_PALETTE:
Packit 7838c8
	case PHOTOMETRIC_MINISBLACK:
Packit 7838c8
	case PHOTOMETRIC_MINISWHITE:
Packit 7838c8
		break;
Packit 7838c8
	case PHOTOMETRIC_LOGL:
Packit 7838c8
	case PHOTOMETRIC_LOGLUV:
Packit 7838c8
		if (compression != COMPRESSION_SGILOG &&
Packit 7838c8
		    compression != COMPRESSION_SGILOG24) {
Packit 7838c8
			TIFFError(filename,
Packit 7838c8
		    "Can not handle %s data with compression other than SGILog",
Packit 7838c8
			    (photometric == PHOTOMETRIC_LOGL) ?
Packit 7838c8
				"LogL" : "LogLuv"
Packit 7838c8
			);
Packit 7838c8
			return (0);
Packit 7838c8
		}
Packit 7838c8
		/* rely on library to convert to RGB/greyscale */
Packit 7838c8
		TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
Packit 7838c8
		photometric = (photometric == PHOTOMETRIC_LOGL) ?
Packit 7838c8
		    PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB;
Packit 7838c8
		bitspersample = 8;
Packit 7838c8
		break;
Packit 7838c8
	case PHOTOMETRIC_CIELAB:
Packit 7838c8
		/* fall thru... */
Packit 7838c8
	default:
Packit 7838c8
		TIFFError(filename,
Packit 7838c8
		    "Can not handle image with PhotometricInterpretation=%d",
Packit 7838c8
		    photometric);
Packit 7838c8
		return (0);
Packit 7838c8
	}
Packit 7838c8
	switch (bitspersample) {
Packit 7838c8
	case 1: case 2:
Packit 7838c8
	case 4: case 8:
Packit 7838c8
	case 16:
Packit 7838c8
		break;
Packit 7838c8
	default:
Packit 7838c8
		TIFFError(filename, "Can not handle %d-bit/sample image",
Packit 7838c8
		    bitspersample);
Packit 7838c8
		return (0);
Packit 7838c8
	}
Packit 7838c8
	if (planarconfiguration == PLANARCONFIG_SEPARATE && extrasamples > 0)
Packit 7838c8
		TIFFWarning(filename, "Ignoring extra samples");
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
#define PS_UNIT_SIZE	72.0F
Packit 7838c8
#define	PSUNITS(npix,res)	((npix) * (PS_UNIT_SIZE / (res)))
Packit 7838c8
Packit 7838c8
static	char RGBcolorimage[] = "\
Packit 7838c8
/bwproc {\n\
Packit 7838c8
    rgbproc\n\
Packit 7838c8
    dup length 3 idiv string 0 3 0\n\
Packit 7838c8
    5 -1 roll {\n\
Packit 7838c8
	add 2 1 roll 1 sub dup 0 eq {\n\
Packit 7838c8
	    pop 3 idiv\n\
Packit 7838c8
	    3 -1 roll\n\
Packit 7838c8
	    dup 4 -1 roll\n\
Packit 7838c8
	    dup 3 1 roll\n\
Packit 7838c8
	    5 -1 roll put\n\
Packit 7838c8
	    1 add 3 0\n\
Packit 7838c8
	} { 2 1 roll } ifelse\n\
Packit 7838c8
    } forall\n\
Packit 7838c8
    pop pop pop\n\
Packit 7838c8
} def\n\
Packit 7838c8
/colorimage where {pop} {\n\
Packit 7838c8
    /colorimage {pop pop /rgbproc exch def {bwproc} image} bind def\n\
Packit 7838c8
} ifelse\n\
Packit 7838c8
";
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Adobe Photoshop requires a comment line of the form:
Packit 7838c8
 *
Packit 7838c8
 * %ImageData: <cols> <rows> <depth>  <main channels> <pad channels>
Packit 7838c8
 *	<block size> <1 for binary|2 for hex> "data start"
Packit 7838c8
 *
Packit 7838c8
 * It is claimed to be part of some future revision of the EPS spec.
Packit 7838c8
 */
Packit 7838c8
static void
Packit 7838c8
PhotoshopBanner(FILE* fd, uint32 w, uint32 h, int bs, int nc, char* startline)
Packit 7838c8
{
Packit 7838c8
	fprintf(fd, "%%ImageData: %ld %ld %d %d 0 %d 2 \"",
Packit 7838c8
	    (long) w, (long) h, bitspersample, nc, bs);
Packit 7838c8
	fprintf(fd, startline, nc);
Packit 7838c8
	fprintf(fd, "\"\n");
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/*   Convert pixel width and height pw, ph, to points pprw, pprh 
Packit 7838c8
 *   using image resolution and resolution units from TIFF tags.
Packit 7838c8
 *   pw : image width in pixels
Packit 7838c8
 *   ph : image height in pixels
Packit 7838c8
 * pprw : image width in PS units (72 dpi)
Packit 7838c8
 * pprh : image height in PS units (72 dpi)
Packit 7838c8
 */
Packit 7838c8
static void
Packit 7838c8
setupPageState(TIFF* tif, uint32* pw, uint32* ph, double* pprw, double* pprh)
Packit 7838c8
{
Packit 7838c8
	float xres = 0.0F, yres = 0.0F;
Packit 7838c8
Packit 7838c8
	TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, pw);
Packit 7838c8
	TIFFGetField(tif, TIFFTAG_IMAGELENGTH, ph);
Packit 7838c8
	if (res_unit == 0)	/* Not specified as command line option */
Packit 7838c8
		if (!TIFFGetFieldDefaulted(tif, TIFFTAG_RESOLUTIONUNIT, &res_unit))
Packit 7838c8
			res_unit = RESUNIT_INCH;
Packit 7838c8
	/*
Packit 7838c8
	 * Calculate printable area.
Packit 7838c8
	 */
Packit 7838c8
	if (!TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xres)
Packit 7838c8
            || fabs(xres) < 0.0000001)
Packit 7838c8
		xres = PS_UNIT_SIZE;
Packit 7838c8
	if (!TIFFGetField(tif, TIFFTAG_YRESOLUTION, &yres)
Packit 7838c8
            || fabs(yres) < 0.0000001)
Packit 7838c8
		yres = PS_UNIT_SIZE;
Packit 7838c8
	switch (res_unit) {
Packit 7838c8
	case RESUNIT_CENTIMETER:
Packit 7838c8
		xres *= 2.54F, yres *= 2.54F;
Packit 7838c8
		break;
Packit 7838c8
	case RESUNIT_INCH:
Packit 7838c8
		break;
Packit 7838c8
	case RESUNIT_NONE:	/* Subsequent code assumes we have converted to inches! */
Packit 7838c8
		res_unit = RESUNIT_INCH;
Packit 7838c8
		break;
Packit 7838c8
	default:	/* Last ditch guess for unspecified RESUNIT case
Packit 7838c8
			 * check that the resolution is not inches before scaling it.
Packit 7838c8
			 * Moved to end of function with additional check, RJN, 08-31-2010
Packit 7838c8
			 * if (xres != PS_UNIT_SIZE || yres != PS_UNIT_SIZE)
Packit 7838c8
			 * xres *= PS_UNIT_SIZE, yres *= PS_UNIT_SIZE;
Packit 7838c8
			 */
Packit 7838c8
		break;
Packit 7838c8
	}
Packit 7838c8
	/* This is a hack to deal with images that have no meaningful Resolution Size
Packit 7838c8
	 * but may have x and/or y resolutions of 1 pixel per undefined unit.
Packit 7838c8
	 */
Packit 7838c8
	if ((xres > 1.0) && (xres != PS_UNIT_SIZE))
Packit 7838c8
		*pprw = PSUNITS(*pw, xres);
Packit 7838c8
	else
Packit 7838c8
		*pprw = PSUNITS(*pw, PS_UNIT_SIZE);
Packit 7838c8
	if ((yres > 1.0) && (yres != PS_UNIT_SIZE))
Packit 7838c8
		*pprh = PSUNITS(*ph, yres);
Packit 7838c8
	else
Packit 7838c8
		*pprh = PSUNITS(*ph, PS_UNIT_SIZE);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
isCCITTCompression(TIFF* tif)
Packit 7838c8
{
Packit 7838c8
    uint16 compress;
Packit 7838c8
    TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
Packit 7838c8
    return (compress == COMPRESSION_CCITTFAX3 ||
Packit 7838c8
	    compress == COMPRESSION_CCITTFAX4 ||
Packit 7838c8
	    compress == COMPRESSION_CCITTRLE ||
Packit 7838c8
	    compress == COMPRESSION_CCITTRLEW);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static	tsize_t tf_bytesperrow;
Packit 7838c8
static	tsize_t ps_bytesperrow;
Packit 7838c8
static	tsize_t	tf_rowsperstrip;
Packit 7838c8
static	tsize_t	tf_numberstrips;
Packit 7838c8
static	char *hex = "0123456789abcdef";
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Pagewidth and pageheight are the output size in points,
Packit 7838c8
 * may refer to values specified with -h and -w, or to
Packit 7838c8
 * values read from the image if neither -h nor -w are used.
Packit 7838c8
 * Imagewidth and imageheight are image size in points.
Packit 7838c8
 * Ximages and Yimages are number of pages across and down.
Packit 7838c8
 * Only one of maxPageHeight or maxPageWidth can be used.
Packit 7838c8
 * These are global variables unfortunately.
Packit 7838c8
 */
Packit 7838c8
int get_subimage_count(double pagewidth,  double pageheight,
Packit 7838c8
		       double imagewidth, double imageheight,
Packit 7838c8
		       int *ximages, int *yimages,
Packit 7838c8
		       int rotation, double scale)
Packit 7838c8
{
Packit 7838c8
	int pages = 1;
Packit 7838c8
	double splitheight = 0;  /* Requested Max Height in points */
Packit 7838c8
	double splitwidth  = 0;  /* Requested Max Width in points */
Packit 7838c8
	double overlap     = 0;  /* Repeated edge width in points */
Packit 7838c8
Packit 7838c8
	splitheight = maxPageHeight * PS_UNIT_SIZE;
Packit 7838c8
	splitwidth  = maxPageWidth  * PS_UNIT_SIZE;
Packit 7838c8
	overlap     = splitOverlap  * PS_UNIT_SIZE;
Packit 7838c8
	pagewidth  *= PS_UNIT_SIZE;
Packit 7838c8
	pageheight *= PS_UNIT_SIZE;
Packit 7838c8
Packit 7838c8
	if ((imagewidth < 1.0) || (imageheight < 1.0))
Packit 7838c8
	{
Packit 7838c8
		TIFFError("get_subimage_count", "Invalid image width or height");
Packit 7838c8
		return (0);
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
  switch (rotation)
Packit 7838c8
    {
Packit 7838c8
    case 0:
Packit 7838c8
    case 180: if (splitheight > 0) /* -H maxPageHeight */
Packit 7838c8
                {
Packit 7838c8
               if (imageheight > splitheight) /* More than one vertical image segment */
Packit 7838c8
                 {
Packit 7838c8
                 if (pagewidth)
Packit 7838c8
                   *ximages = (int)ceil((scale * imagewidth)  / (pagewidth - overlap));
Packit 7838c8
                  else
Packit 7838c8
                   *ximages = 1;
Packit 7838c8
                 *yimages = (int)ceil((scale * imageheight) / (splitheight - overlap)); /* Max vert pages needed */
Packit 7838c8
                 }
Packit 7838c8
                else
Packit 7838c8
                 {
Packit 7838c8
                 if (pagewidth)
Packit 7838c8
                   *ximages = (int)ceil((scale * imagewidth) / (pagewidth - overlap));    /* Max horz pages needed */
Packit 7838c8
                  else
Packit 7838c8
                   *ximages = 1;
Packit 7838c8
                 *yimages = 1;                                                     /* Max vert pages needed */
Packit 7838c8
                 }
Packit 7838c8
               }
Packit 7838c8
              else
Packit 7838c8
               {
Packit 7838c8
                if (splitwidth > 0) /* -W maxPageWidth */
Packit 7838c8
                 {
Packit 7838c8
                 if (imagewidth >splitwidth)
Packit 7838c8
                   {
Packit 7838c8
                   *ximages = (int)ceil((scale * imagewidth)  / (splitwidth - overlap));   /* Max horz pages needed */
Packit 7838c8
                    if (pageheight)
Packit 7838c8
                     *yimages = (int)ceil((scale * imageheight) / (pageheight - overlap)); /* Max vert pages needed */
Packit 7838c8
                    else
Packit 7838c8
                     *yimages = 1;
Packit 7838c8
                   }
Packit 7838c8
                  else
Packit 7838c8
                   {
Packit 7838c8
                   *ximages = 1;                                                     /* Max vert pages needed */
Packit 7838c8
                    if (pageheight)
Packit 7838c8
                     *yimages = (int)ceil((scale * imageheight) / (pageheight - overlap)); /* Max vert pages needed */
Packit 7838c8
                    else
Packit 7838c8
                     *yimages = 1;
Packit 7838c8
                   }
Packit 7838c8
                 }
Packit 7838c8
                else
Packit 7838c8
                 {
Packit 7838c8
                 *ximages = 1;
Packit 7838c8
                 *yimages = 1;
Packit 7838c8
                 }
Packit 7838c8
               }
Packit 7838c8
             break;
Packit 7838c8
    case 90:
Packit 7838c8
    case 270: if (splitheight > 0) /* -H maxPageHeight */
Packit 7838c8
                {
Packit 7838c8
               if (imagewidth > splitheight) /* More than one vertical image segment */
Packit 7838c8
                 {
Packit 7838c8
                 *yimages = (int)ceil((scale * imagewidth) / (splitheight - overlap)); /* Max vert pages needed */
Packit 7838c8
                  if (pagewidth)
Packit 7838c8
                   *ximages = (int)ceil((scale * imageheight) / (pagewidth - overlap));   /* Max horz pages needed */
Packit 7838c8
                  else
Packit 7838c8
                   *ximages = 1;
Packit 7838c8
                 }
Packit 7838c8
                else
Packit 7838c8
                 {
Packit 7838c8
                 *yimages = 1;                                                     /* Max vert pages needed */
Packit 7838c8
                  if (pagewidth)
Packit 7838c8
                   *ximages = (int)ceil((scale * imageheight) / (pagewidth - overlap));    /* Max horz pages needed */
Packit 7838c8
                  else
Packit 7838c8
                   *ximages = 1;
Packit 7838c8
                 }
Packit 7838c8
               }
Packit 7838c8
              else
Packit 7838c8
               {
Packit 7838c8
                if (splitwidth > 0) /* -W maxPageWidth */
Packit 7838c8
                 {
Packit 7838c8
                 if (imageheight > splitwidth)
Packit 7838c8
                   {
Packit 7838c8
                   if (pageheight)
Packit 7838c8
                     *yimages = (int)ceil((scale * imagewidth) / (pageheight - overlap)); /* Max vert pages needed */
Packit 7838c8
                    else
Packit 7838c8
                     *yimages = 1;
Packit 7838c8
                   *ximages = (int)ceil((scale * imageheight)  / (splitwidth - overlap));   /* Max horz pages needed */
Packit 7838c8
                   }
Packit 7838c8
                  else
Packit 7838c8
                   {
Packit 7838c8
                   if (pageheight)
Packit 7838c8
                     *yimages = (int)ceil((scale * imagewidth) / (pageheight - overlap));  /* Max horz pages needed */
Packit 7838c8
                    else
Packit 7838c8
                     *yimages = 1;
Packit 7838c8
                   *ximages = 1;                                                     /* Max vert pages needed */
Packit 7838c8
                   }
Packit 7838c8
                 }
Packit 7838c8
                else
Packit 7838c8
                 {
Packit 7838c8
                 *ximages = 1;
Packit 7838c8
                 *yimages = 1;
Packit 7838c8
                 }
Packit 7838c8
               }
Packit 7838c8
             break;
Packit 7838c8
    default:  *ximages = 1;
Packit 7838c8
             *yimages = 1;
Packit 7838c8
  }
Packit 7838c8
  pages = (*ximages) * (*yimages);
Packit 7838c8
  return (pages);
Packit 7838c8
  }
Packit 7838c8
Packit 7838c8
/* New version of PlaceImage that handles only the translation and rotation
Packit 7838c8
 * for a single output page.
Packit 7838c8
 */
Packit 7838c8
int exportMaskedImage(FILE *fp, double pagewidth, double pageheight,
Packit 7838c8
                     double imagewidth, double imageheight,
Packit 7838c8
                      int row, int column,
Packit 7838c8
                      double left_offset, double bott_offset,
Packit 7838c8
                     double scale, int center, int rotation)
Packit 7838c8
  {
Packit 7838c8
  double xtran = 0.0;
Packit 7838c8
  double ytran = 0.0;
Packit 7838c8
Packit 7838c8
  double xscale = 1.0;
Packit 7838c8
  double yscale = 1.0;
Packit 7838c8
Packit 7838c8
  double splitheight    = 0;  /* Requested Max Height in points */
Packit 7838c8
  double splitwidth     = 0;  /* Requested Max Width in points */
Packit 7838c8
  double overlap        = 0;  /* Repeated edge width in points */
Packit 7838c8
  double subimage_height = 0.0;
Packit 7838c8
Packit 7838c8
  splitheight = maxPageHeight * PS_UNIT_SIZE;
Packit 7838c8
  splitwidth  = maxPageWidth  * PS_UNIT_SIZE;
Packit 7838c8
  overlap     = splitOverlap  * PS_UNIT_SIZE;
Packit 7838c8
  xscale = scale * imagewidth;
Packit 7838c8
  yscale = scale * imageheight;
Packit 7838c8
Packit 7838c8
  if ((xscale < 0.0) || (yscale < 0.0))
Packit 7838c8
    {
Packit 7838c8
    TIFFError("exportMaskedImage", "Invalid parameters.");
Packit 7838c8
    return (-1);
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
  /* If images are cropped to a vewport with -H or -W, the output pages are shifted to
Packit 7838c8
   * the top of each output page rather than the Postscript default lower edge.
Packit 7838c8
   */
Packit 7838c8
  switch (rotation)
Packit 7838c8
    {
Packit 7838c8
    case 0:
Packit 7838c8
    case 180: if (splitheight > 0) /* -H maxPageHeight */
Packit 7838c8
                {
Packit 7838c8
               if (splitheight < imageheight) /* More than one vertical image segments */
Packit 7838c8
                 {
Packit 7838c8
                 /* Intra2net: Keep correct apspect ratio */
Packit 7838c8
                 xscale = (imagewidth + overlap) * (pageheight / splitheight) * scale;
Packit 7838c8
Packit 7838c8
                 xtran = -1.0 * column * (pagewidth - overlap);
Packit 7838c8
                  subimage_height = imageheight - ((splitheight - overlap) * row);
Packit 7838c8
                 ytran  = pageheight - subimage_height * (pageheight / splitheight);
Packit 7838c8
                  }
Packit 7838c8
                else  /* Only one page in vertical direction */
Packit 7838c8
                 {
Packit 7838c8
                 xtran = -1.0 * column * (pagewidth - overlap);
Packit 7838c8
                  ytran = splitheight - imageheight;
Packit 7838c8
                 }
Packit 7838c8
               }
Packit 7838c8
              else
Packit 7838c8
               {
Packit 7838c8
                if (splitwidth > 0) /* maxPageWidth */
Packit 7838c8
                 {
Packit 7838c8
                 if (splitwidth < imagewidth)
Packit 7838c8
                   {
Packit 7838c8
                   xtran = -1.0  * column * splitwidth;
Packit 7838c8
                   ytran = -1.0 * row * (pageheight - overlap);
Packit 7838c8
                    }
Packit 7838c8
                  else /* Only one page in horizontal direction */
Packit 7838c8
                   {
Packit 7838c8
                    ytran = -1.0 * row * (pageheight - overlap);
Packit 7838c8
                    xtran = 0;
Packit 7838c8
                   }
Packit 7838c8
                 }
Packit 7838c8
                else    /* Simple case, no splitting */
Packit 7838c8
                 {
Packit 7838c8
                 ytran = pageheight - imageheight;
Packit 7838c8
                 xtran = 0;
Packit 7838c8
                  }
Packit 7838c8
                }
Packit 7838c8
Packit 7838c8
            if (imagewidth <= pagewidth) {
Packit 7838c8
                /* Intra2net: Crop page at the bottom instead of the top (-> output starts at the top).
Packit 7838c8
                     Only do this in non-page-split mode */
Packit 7838c8
                if (imageheight <= splitheight) {
Packit 7838c8
                    ytran = pageheight - imageheight; /* Note: Will be negative for images longer than page size */
Packit 7838c8
                }
Packit 7838c8
            }
Packit 7838c8
              bott_offset += ytran / (center ? 2 : 1);
Packit 7838c8
              left_offset += xtran / (center ? 2 : 1);
Packit 7838c8
              break;
Packit 7838c8
    case  90:
Packit 7838c8
    case 270:  if (splitheight > 0) /* -H maxPageHeight */
Packit 7838c8
                {
Packit 7838c8
               if (splitheight < imagewidth) /* More than one vertical image segments */
Packit 7838c8
                 {
Packit 7838c8
                 xtran = -1.0 * column * (pageheight - overlap);
Packit 7838c8
                 /* Commented code places image at bottom of page instead of top.
Packit 7838c8
                     ytran = -1.0 * row * splitheight;
Packit 7838c8
                   */
Packit 7838c8
                  if (row == 0)
Packit 7838c8
                    ytran = -1.0 * (imagewidth - splitheight);
Packit 7838c8
                  else
Packit 7838c8
                    ytran = -1.0 * (imagewidth - (splitheight - overlap) * (row + 1));
Packit 7838c8
                  }
Packit 7838c8
                else  /* Only one page in vertical direction */
Packit 7838c8
                 {
Packit 7838c8
                  xtran = -1.0 * column * (pageheight - overlap);
Packit 7838c8
                  ytran = splitheight - imagewidth;
Packit 7838c8
                 }
Packit 7838c8
		}
Packit 7838c8
              else
Packit 7838c8
               {
Packit 7838c8
                if (splitwidth > 0) /* maxPageWidth */
Packit 7838c8
                 {
Packit 7838c8
                 if (splitwidth < imageheight)
Packit 7838c8
                   {
Packit 7838c8
                    xtran = -1.0  * column * splitwidth;
Packit 7838c8
                    ytran = -1.0 * row * (pagewidth - overlap);
Packit 7838c8
                    }
Packit 7838c8
                  else /* Only one page in horizontal direction */
Packit 7838c8
                   {
Packit 7838c8
                    ytran = -1.0 * row * (pagewidth - overlap);
Packit 7838c8
                    xtran = 0;
Packit 7838c8
                   }
Packit 7838c8
                 }
Packit 7838c8
                else    /* Simple case, no splitting */
Packit 7838c8
                 {
Packit 7838c8
                 ytran = pageheight - imageheight;
Packit 7838c8
                 xtran = 0; /* pagewidth  - imagewidth; */
Packit 7838c8
                  }
Packit 7838c8
                }
Packit 7838c8
              bott_offset += ytran / (center ? 2 : 1);
Packit 7838c8
              left_offset += xtran / (center ? 2 : 1);
Packit 7838c8
              break;
Packit 7838c8
    default:  xtran = 0;
Packit 7838c8
             ytran = 0;
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
  switch (rotation)
Packit 7838c8
    {
Packit 7838c8
    case   0: fprintf(fp, "%f %f translate\n", left_offset, bott_offset);
Packit 7838c8
              fprintf(fp, "%f %f scale\n", xscale, yscale);
Packit 7838c8
             break;
Packit 7838c8
    case 180: fprintf(fp, "%f %f translate\n", left_offset, bott_offset);
Packit 7838c8
              fprintf(fp, "%f %f scale\n1 1 translate 180 rotate\n",  xscale, yscale);
Packit 7838c8
              break;
Packit 7838c8
    case  90: fprintf(fp, "%f %f translate\n", left_offset, bott_offset);
Packit 7838c8
              fprintf(fp, "%f %f scale\n1 0 translate 90 rotate\n", yscale, xscale);
Packit 7838c8
              break;
Packit 7838c8
    case 270: fprintf(fp, "%f %f translate\n", left_offset, bott_offset);
Packit 7838c8
              fprintf(fp, "%f %f scale\n0 1 translate 270 rotate\n", yscale, xscale);
Packit 7838c8
              break;
Packit 7838c8
    default:  TIFFError ("exportMaskedImage", "Unsupported rotation angle %d. No rotation", rotation);
Packit 7838c8
             fprintf( fp, "%f %f scale\n", xscale, yscale);
Packit 7838c8
              break;
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
  return (0);
Packit 7838c8
  }
Packit 7838c8
Packit 7838c8
/* Rotate an image without scaling or clipping */
Packit 7838c8
int  psRotateImage (FILE * fd, int rotation, double pswidth, double psheight,
Packit 7838c8
                    double left_offset, double bottom_offset)
Packit 7838c8
  {
Packit 7838c8
  if ((left_offset != 0.0) || (bottom_offset != 0))
Packit 7838c8
    fprintf (fd, "%f %f translate\n", left_offset, bottom_offset);
Packit 7838c8
Packit 7838c8
  /* Exchange width and height for 90/270 rotations */
Packit 7838c8
  switch (rotation)
Packit 7838c8
    {
Packit 7838c8
    case   0: fprintf (fd, "%f %f scale\n", pswidth, psheight);
Packit 7838c8
              break;
Packit 7838c8
    case  90: fprintf (fd, "%f %f scale\n1 0 translate 90 rotate\n", psheight, pswidth);
Packit 7838c8
              break;
Packit 7838c8
    case 180: fprintf (fd, "%f %f scale\n1 1 translate 180 rotate\n", pswidth, psheight);
Packit 7838c8
              break;
Packit 7838c8
    case 270: fprintf (fd, "%f %f scale\n0 1 translate 270 rotate\n", psheight, pswidth);
Packit 7838c8
              break;
Packit 7838c8
    default:  TIFFError ("psRotateImage", "Unsupported rotation %d.", rotation);
Packit 7838c8
             fprintf( fd, "%f %f scale\n", pswidth, psheight);
Packit 7838c8
              return (1);
Packit 7838c8
    }
Packit 7838c8
  return (0);
Packit 7838c8
  }
Packit 7838c8
Packit 7838c8
/* Scale and rotate an image to a single output page. */
Packit 7838c8
int psScaleImage(FILE * fd, double scale, int rotation, int center,
Packit 7838c8
                 double reqwidth, double reqheight, double pswidth, double psheight,
Packit 7838c8
                 double left_offset, double bottom_offset)
Packit 7838c8
  {
Packit 7838c8
  double hcenter = 0.0, vcenter = 0.0;
Packit 7838c8
Packit 7838c8
  /* Adjust offsets for centering */
Packit 7838c8
  if (center)
Packit 7838c8
    {
Packit 7838c8
    switch (rotation)
Packit 7838c8
      {
Packit 7838c8
      case   90: vcenter = (reqheight - pswidth * scale) / 2;
Packit 7838c8
                hcenter = (reqwidth - psheight * scale) / 2;
Packit 7838c8
                 fprintf (fd, "%f %f translate\n", hcenter, vcenter);
Packit 7838c8
                 fprintf (fd, "%f %f scale\n1 0 translate 90 rotate\n", psheight * scale, pswidth * scale);
Packit 7838c8
                 break;
Packit 7838c8
      case  180: hcenter = (reqwidth - pswidth * scale) / 2;
Packit 7838c8
                vcenter = (reqheight - psheight * scale) / 2;
Packit 7838c8
                 fprintf (fd, "%f %f translate\n", hcenter, vcenter);
Packit 7838c8
                 fprintf (fd, "%f %f scale\n1 1 translate 180 rotate\n", pswidth * scale, psheight * scale);
Packit 7838c8
                 break;
Packit 7838c8
      case  270: vcenter = (reqheight - pswidth * scale) / 2;
Packit 7838c8
                hcenter = (reqwidth - psheight * scale) / 2;
Packit 7838c8
                 fprintf (fd, "%f %f translate\n", hcenter, vcenter);
Packit 7838c8
                 fprintf (fd, "%f %f scale\n0 1 translate 270 rotate\n", psheight * scale, pswidth * scale);
Packit 7838c8
                 break;
Packit 7838c8
      case    0:
Packit 7838c8
      default:   hcenter = (reqwidth - pswidth * scale) / 2;
Packit 7838c8
                vcenter = (reqheight - psheight * scale) / 2;
Packit 7838c8
                 fprintf (fd, "%f %f translate\n", hcenter, vcenter);
Packit 7838c8
                 fprintf (fd, "%f %f scale\n", pswidth * scale, psheight * scale);
Packit 7838c8
                 break;
Packit 7838c8
      }
Packit 7838c8
    }
Packit 7838c8
  else  /* Not centered */
Packit 7838c8
    {
Packit 7838c8
    switch (rotation)
Packit 7838c8
      {
Packit 7838c8
      case 0:   fprintf (fd, "%f %f translate\n", left_offset ? left_offset : 0.0,
Packit 7838c8
                         bottom_offset ? bottom_offset : reqheight - (psheight * scale));
Packit 7838c8
                fprintf (fd, "%f %f scale\n", pswidth * scale, psheight * scale);
Packit 7838c8
                break;
Packit 7838c8
      case 90:  fprintf (fd, "%f %f translate\n", left_offset ? left_offset : 0.0,
Packit 7838c8
                         bottom_offset ? bottom_offset : reqheight - (pswidth * scale));
Packit 7838c8
                fprintf (fd, "%f %f scale\n1 0 translate 90 rotate\n", psheight * scale, pswidth * scale);
Packit 7838c8
                break;
Packit 7838c8
      case 180: fprintf (fd, "%f %f translate\n", left_offset ? left_offset : 0.0,
Packit 7838c8
                         bottom_offset ? bottom_offset : reqheight - (psheight * scale));
Packit 7838c8
                fprintf (fd, "%f %f scale\n1 1 translate 180 rotate\n", pswidth * scale, psheight * scale);
Packit 7838c8
                break;
Packit 7838c8
      case 270: fprintf (fd, "%f %f translate\n", left_offset ? left_offset : 0.0,
Packit 7838c8
                         bottom_offset ? bottom_offset : reqheight - (pswidth * scale));
Packit 7838c8
                fprintf (fd, "%f %f scale\n0 1 translate 270 rotate\n", psheight * scale, pswidth * scale);
Packit 7838c8
                break;
Packit 7838c8
      default:  TIFFError ("psScaleImage", "Unsupported rotation  %d", rotation);
Packit 7838c8
               fprintf (fd, "%f %f scale\n", pswidth * scale, psheight * scale);
Packit 7838c8
                return (1);
Packit 7838c8
      }
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
  return (0);
Packit 7838c8
  }
Packit 7838c8
Packit 7838c8
/* This controls the visible portion of the page which is displayed.
Packit 7838c8
 * N.B. Setting maxPageHeight no longer sets pageheight if not set explicitly
Packit 7838c8
 */
Packit 7838c8
int psPageSize (FILE * fd, int rotation, double pgwidth, double pgheight,
Packit 7838c8
                double reqwidth, double reqheight, double pswidth, double psheight)
Packit 7838c8
  {
Packit 7838c8
  double xscale = 1.0, yscale = 1.0, scale = 1.0;
Packit 7838c8
  double splitheight;
Packit 7838c8
  double splitwidth;
Packit 7838c8
  double new_width;
Packit 7838c8
  double new_height;
Packit 7838c8
Packit 7838c8
  splitheight = maxPageHeight * PS_UNIT_SIZE;
Packit 7838c8
  splitwidth  = maxPageWidth  * PS_UNIT_SIZE;
Packit 7838c8
Packit 7838c8
  switch (rotation)
Packit 7838c8
    {
Packit 7838c8
    case   0:
Packit 7838c8
    case 180: if ((splitheight > 0) || (splitwidth > 0))
Packit 7838c8
                {
Packit 7838c8
               if (pgwidth != 0 || pgheight != 0)
Packit 7838c8
                  {
Packit 7838c8
                 xscale = reqwidth / (splitwidth ? splitwidth : pswidth);
Packit 7838c8
                 yscale = reqheight / (splitheight ? splitheight : psheight);
Packit 7838c8
                  scale = (xscale < yscale) ? xscale : yscale;
Packit 7838c8
                  }
Packit 7838c8
                new_width = splitwidth ? splitwidth : scale * pswidth;
Packit 7838c8
                new_height = splitheight ? splitheight : scale * psheight;
Packit 7838c8
                if (strlen(pageOrientation))
Packit 7838c8
                  fprintf (fd, "%%%%PageOrientation: %s\n", pageOrientation);
Packit 7838c8
                else
Packit 7838c8
                  fprintf (fd, "%%%%PageOrientation: %s\n", (new_width > new_height) ? "Landscape" : "Portrait");
Packit 7838c8
                fprintf (fd, "%%%%PageBoundingBox: 0 0 %ld %ld\n", (long)new_width, (long)new_height);
Packit 7838c8
                fprintf (fd, "1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n",
Packit 7838c8
                       new_width, new_height);
Packit 7838c8
                }
Packit 7838c8
             else /* No viewport defined with -H or -W */
Packit 7838c8
                {
Packit 7838c8
                if ((pgwidth == 0) && (pgheight == 0)) /* Image not scaled */
Packit 7838c8
                  {
Packit 7838c8
                  if (strlen(pageOrientation))
Packit 7838c8
                    fprintf (fd, "%%%%PageOrientation: %s\n", pageOrientation);
Packit 7838c8
                  else
Packit 7838c8
                    fprintf (fd, "%%%%PageOrientation: %s\n", (pswidth > psheight) ? "Landscape" : "Portrait");
Packit 7838c8
                 fprintf (fd, "%%%%PageBoundingBox: 0 0 %ld %ld\n", (long)pswidth, (long)psheight);
Packit 7838c8
                  fprintf(fd, "1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n",
Packit 7838c8
                          pswidth, psheight);
Packit 7838c8
                  }
Packit 7838c8
               else /* Image scaled */
Packit 7838c8
                  {
Packit 7838c8
                  if (strlen(pageOrientation))
Packit 7838c8
                    fprintf (fd, "%%%%PageOrientation: %s\n", pageOrientation);
Packit 7838c8
                  else
Packit 7838c8
                    fprintf (fd, "%%%%PageOrientation: %s\n", (reqwidth > reqheight) ? "Landscape" : "Portrait");
Packit 7838c8
                 fprintf (fd, "%%%%PageBoundingBox: 0 0 %ld %ld\n", (long)reqwidth, (long)reqheight);
Packit 7838c8
                  fprintf(fd, "1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n",
Packit 7838c8
                           reqwidth, reqheight);
Packit 7838c8
                  }
Packit 7838c8
                }
Packit 7838c8
             break;
Packit 7838c8
    case  90:
Packit 7838c8
    case 270: if ((splitheight > 0) || (splitwidth > 0))
Packit 7838c8
               {
Packit 7838c8
               if (pgwidth != 0 || pgheight != 0)
Packit 7838c8
                  {
Packit 7838c8
                 xscale = reqwidth / (splitwidth ? splitwidth : pswidth);
Packit 7838c8
                 yscale = reqheight / (splitheight ? splitheight : psheight);
Packit 7838c8
                  scale = (xscale < yscale) ? xscale : yscale;
Packit 7838c8
                  }
Packit 7838c8
                new_width = splitwidth ? splitwidth : scale * psheight;
Packit 7838c8
                new_height = splitheight ? splitheight : scale * pswidth;
Packit 7838c8
Packit 7838c8
                if (strlen(pageOrientation))
Packit 7838c8
                  fprintf (fd, "%%%%PageOrientation: %s\n", pageOrientation);
Packit 7838c8
                else
Packit 7838c8
                  fprintf (fd, "%%%%PageOrientation: %s\n", (new_width > new_height) ? "Landscape" : "Portrait");
Packit 7838c8
                fprintf (fd, "%%%%PageBoundingBox: 0 0 %ld %ld\n", (long)new_width, (long)new_height);
Packit 7838c8
                fprintf (fd, "1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n",
Packit 7838c8
                       new_width, new_height);
Packit 7838c8
                }
Packit 7838c8
              else
Packit 7838c8
                {
Packit 7838c8
                if ((pgwidth == 0) && (pgheight == 0)) /* Image not scaled */
Packit 7838c8
                  {
Packit 7838c8
                  if (strlen(pageOrientation))
Packit 7838c8
                    fprintf (fd, "%%%%PageOrientation: %s\n", pageOrientation);
Packit 7838c8
                  else
Packit 7838c8
                    fprintf (fd, "%%%%PageOrientation: %s\n", (psheight > pswidth) ? "Landscape" : "Portrait");
Packit 7838c8
                 fprintf (fd, "%%%%PageBoundingBox: 0 0 %ld %ld\n", (long)psheight, (long)pswidth);
Packit 7838c8
                  fprintf(fd, "1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n",
Packit 7838c8
                         psheight, pswidth);
Packit 7838c8
                  }
Packit 7838c8
               else /* Image scaled */
Packit 7838c8
                  {
Packit 7838c8
                  if (strlen(pageOrientation))
Packit 7838c8
                    fprintf (fd, "%%%%PageOrientation: %s\n", pageOrientation);
Packit 7838c8
                  else
Packit 7838c8
                    fprintf (fd, "%%%%PageOrientation: %s\n", (reqwidth > reqheight) ? "Landscape" : "Portrait");
Packit 7838c8
                 fprintf (fd, "%%%%PageBoundingBox: 0 0 %ld %ld\n", (long)reqwidth, (long)reqheight);
Packit 7838c8
                  fprintf(fd, "1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n",
Packit 7838c8
                          reqwidth, reqheight);
Packit 7838c8
                  }
Packit 7838c8
               }
Packit 7838c8
             break;
Packit 7838c8
    default:  TIFFError ("psPageSize", "Invalid rotation %d", rotation);
Packit 7838c8
      return (1);
Packit 7838c8
    }
Packit 7838c8
  fputs("<<\n  /Policies <<\n    /PageSize 3\n  >>\n>> setpagedevice\n", fd);
Packit 7838c8
Packit 7838c8
  return (0);
Packit 7838c8
  } /* end psPageSize */
Packit 7838c8
Packit 7838c8
/* Mask an image as a series of pages, each only showing a section defined
Packit 7838c8
 * by the maxPageHeight or maxPageWidth options.
Packit 7838c8
 */
Packit 7838c8
int psMaskImage(FILE *fd, TIFF *tif, int rotation, int center,
Packit 7838c8
                int *npages, double pixwidth, double pixheight,
Packit 7838c8
               double left_margin, double bottom_margin,
Packit 7838c8
                double pgwidth, double pgheight,
Packit 7838c8
               double pswidth, double psheight, double scale)
Packit 7838c8
  {
Packit 7838c8
  int i, j;
Packit 7838c8
  int ximages = 1, yimages = 1;
Packit 7838c8
  int pages = *npages;
Packit 7838c8
  double view_width = 0;
Packit 7838c8
  double view_height = 0;
Packit 7838c8
Packit 7838c8
  if (get_viewport (pgwidth, pgheight, pswidth, psheight, &view_width, &view_height, rotation))
Packit 7838c8
    {
Packit 7838c8
    TIFFError ("get_viewport", "Unable to set image viewport");
Packit 7838c8
    return (-1);
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
  if (get_subimage_count(pgwidth, pgheight, pswidth, psheight,
Packit 7838c8
                        &ximages, &yimages, rotation, scale) < 1)
Packit 7838c8
    {
Packit 7838c8
    TIFFError("get_subimage_count", "Invalid image count: %d columns, %d rows", ximages, yimages);
Packit 7838c8
    return (-1);
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
  for (i = 0; i < yimages; i++)
Packit 7838c8
    {
Packit 7838c8
    for (j = 0; j < ximages; j++)
Packit 7838c8
       {
Packit 7838c8
       pages++;
Packit 7838c8
       *npages = pages;
Packit 7838c8
       fprintf(fd, "%%%%Page: %d %d\n", pages, pages);
Packit 7838c8
Packit 7838c8
       /* Write out the PageSize info for non EPS files */
Packit 7838c8
       if (!generateEPSF && ( level2 || level3 ))
Packit 7838c8
         {
Packit 7838c8
         if (psPageSize(fd, rotation, pgwidth, pgheight,
Packit 7838c8
                        view_width, view_height, pswidth, psheight))
Packit 7838c8
           return (-1);
Packit 7838c8
        }
Packit 7838c8
       fprintf(fd, "gsave\n");
Packit 7838c8
       fprintf(fd, "100 dict begin\n");
Packit 7838c8
       if (exportMaskedImage(fd, view_width, view_height, pswidth, psheight,
Packit 7838c8
                            i, j, left_margin, bottom_margin,
Packit 7838c8
                            scale, center, rotation))
Packit 7838c8
        {
Packit 7838c8
        TIFFError("exportMaskedImage", "Invalid image parameters.");
Packit 7838c8
        return (-1);
Packit 7838c8
        }
Packit 7838c8
       PSpage(fd, tif, pixwidth, pixheight);
Packit 7838c8
       fprintf(fd, "end\n");
Packit 7838c8
       fprintf(fd, "grestore\n");
Packit 7838c8
       fprintf(fd, "showpage\n");
Packit 7838c8
       }
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
  return (pages);
Packit 7838c8
  }
Packit 7838c8
Packit 7838c8
/* Compute scale factor and write out file header */
Packit 7838c8
int psStart(FILE *fd, int npages, int auto_rotate, int *rotation, double *scale,
Packit 7838c8
            double ox, double oy, double pgwidth, double pgheight,
Packit 7838c8
           double reqwidth, double reqheight, double pswidth, double psheight,
Packit 7838c8
           double left_offset, double bottom_offset)
Packit 7838c8
  {
Packit 7838c8
  double maxsource = 0.0;    /* Used for auto rotations */
Packit 7838c8
  double maxtarget = 0.0;
Packit 7838c8
  double xscale = 1.0, yscale = 1.0;
Packit 7838c8
  double splitheight;
Packit 7838c8
  double splitwidth;
Packit 7838c8
  double view_width = 0.0, view_height = 0.0;
Packit 7838c8
  double page_width = 0.0, page_height = 0.0;
Packit 7838c8
Packit 7838c8
  /* Splitheight and splitwidth are in inches */
Packit 7838c8
  splitheight = maxPageHeight * PS_UNIT_SIZE;
Packit 7838c8
  splitwidth  = maxPageWidth * PS_UNIT_SIZE;
Packit 7838c8
Packit 7838c8
  page_width = pgwidth * PS_UNIT_SIZE;
Packit 7838c8
  page_height = pgheight * PS_UNIT_SIZE;
Packit 7838c8
Packit 7838c8
  /* If user has specified a page width and height and requested the
Packit 7838c8
   * image to be auto-rotated to fit on that media, we match the
Packit 7838c8
   * longest dimension of the image to the longest dimension of the
Packit 7838c8
   * target media but we have to ignore auto rotate if user specified
Packit 7838c8
   * maxPageHeight since this makes life way too complicated. */
Packit 7838c8
  if (auto_rotate)
Packit 7838c8
    {
Packit 7838c8
    if ((splitheight != 0) || (splitwidth != 0))
Packit 7838c8
      {
Packit 7838c8
      TIFFError ("psStart", "Auto-rotate is incompatible with page splitting ");
Packit 7838c8
      return (1);
Packit 7838c8
      }
Packit 7838c8
Packit 7838c8
    /* Find longest edges in image and output media */
Packit 7838c8
    maxsource = (pswidth >= psheight) ? pswidth : psheight;
Packit 7838c8
    maxtarget = (reqwidth >= reqheight) ? reqwidth : reqheight;
Packit 7838c8
Packit 7838c8
    if (((maxsource == pswidth) && (maxtarget != reqwidth)) ||
Packit 7838c8
        ((maxsource == psheight) && (maxtarget != reqheight)))
Packit 7838c8
      {  /* optimal orientaion does not match input orientation */
Packit 7838c8
      *rotation = 90;
Packit 7838c8
      xscale = (reqwidth - left_offset)/psheight;
Packit 7838c8
      yscale = (reqheight - bottom_offset)/pswidth;
Packit 7838c8
      }
Packit 7838c8
    else /* optimal orientaion matches input orientation */
Packit 7838c8
      {
Packit 7838c8
      xscale = (reqwidth - left_offset)/pswidth;
Packit 7838c8
      yscale = (reqheight - bottom_offset)/psheight;
Packit 7838c8
      }
Packit 7838c8
    *scale = (xscale < yscale) ? xscale : yscale;
Packit 7838c8
Packit 7838c8
    /* Do not scale image beyound original size */
Packit 7838c8
    if (*scale > 1.0)
Packit 7838c8
      *scale = 1.0;
Packit 7838c8
Packit 7838c8
    /* Set the size of the displayed image to requested page size
Packit 7838c8
     * and optimal orientation.
Packit 7838c8
     */
Packit 7838c8
    if (!npages)
Packit 7838c8
      PSHead(fd, reqwidth, reqheight, ox, oy);
Packit 7838c8
Packit 7838c8
    return (0);
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
  /* N.B. If pgwidth or pgheight are set from maxPageHeight/Width,
Packit 7838c8
   * we have a problem with the tests below under splitheight.
Packit 7838c8
   */
Packit 7838c8
Packit 7838c8
  switch (*rotation)  /* Auto rotate has NOT been specified */
Packit 7838c8
    {
Packit 7838c8
    case   0:
Packit 7838c8
    case 180: if ((splitheight != 0)  || (splitwidth != 0))
Packit 7838c8
                {  /* Viewport clipped to maxPageHeight or maxPageWidth */
Packit 7838c8
                if ((page_width != 0) || (page_height != 0)) /* Image scaled */
Packit 7838c8
                  {
Packit 7838c8
                 xscale = (reqwidth  - left_offset) / (page_width ? page_width : pswidth);
Packit 7838c8
                 yscale = (reqheight - bottom_offset) / (page_height ? page_height : psheight);
Packit 7838c8
                  *scale = (xscale < yscale) ? xscale : yscale;
Packit 7838c8
                  /*
Packit 7838c8
                  if (*scale > 1.0)
Packit 7838c8
                    *scale = 1.0;
Packit 7838c8
                   */
Packit 7838c8
                 }
Packit 7838c8
                else       /* Image clipped but not scaled */
Packit 7838c8
                 *scale = 1.0;
Packit 7838c8
Packit 7838c8
                view_width = splitwidth ? splitwidth : *scale * pswidth;
Packit 7838c8
                view_height = splitheight ? splitheight: *scale * psheight;
Packit 7838c8
               }
Packit 7838c8
              else   /* Viewport not clipped to maxPageHeight or maxPageWidth */
Packit 7838c8
                {
Packit 7838c8
                if ((page_width != 0) || (page_height != 0))
Packit 7838c8
                  {   /* Image scaled  */
Packit 7838c8
                  xscale = (reqwidth - left_offset) / pswidth;
Packit 7838c8
                  yscale = (reqheight - bottom_offset) / psheight;
Packit 7838c8
Packit 7838c8
                  view_width = reqwidth;
Packit 7838c8
                  view_height = reqheight;
Packit 7838c8
                 }
Packit 7838c8
                else
Packit 7838c8
                  {  /* Image not scaled  */
Packit 7838c8
                  xscale = (pswidth - left_offset)/pswidth;
Packit 7838c8
                  yscale = (psheight - bottom_offset)/psheight;
Packit 7838c8
Packit 7838c8
                  view_width = pswidth;
Packit 7838c8
                  view_height = psheight;
Packit 7838c8
                 }
Packit 7838c8
               }
Packit 7838c8
             break;
Packit 7838c8
    case  90:
Packit 7838c8
    case 270: if ((splitheight != 0) || (splitwidth != 0))
Packit 7838c8
                {  /* Viewport clipped to maxPageHeight or maxPageWidth */
Packit 7838c8
                if ((page_width != 0) || (page_height != 0)) /* Image scaled */
Packit 7838c8
                  {
Packit 7838c8
                 xscale = (reqwidth - left_offset)/ psheight;
Packit 7838c8
                 yscale = (reqheight - bottom_offset)/ pswidth;
Packit 7838c8
                  *scale = (xscale < yscale) ? xscale : yscale;
Packit 7838c8
                  /*
Packit 7838c8
                  if (*scale > 1.0)
Packit 7838c8
                    *scale = 1.0;
Packit 7838c8
                 */
Packit 7838c8
                 }
Packit 7838c8
                else  /* Image clipped but not scaled */
Packit 7838c8
                 *scale = 1.0;
Packit 7838c8
                view_width = splitwidth ? splitwidth : *scale * psheight;
Packit 7838c8
                view_height = splitheight ? splitheight : *scale * pswidth;
Packit 7838c8
               }
Packit 7838c8
              else /* Viewport not clipped to maxPageHeight or maxPageWidth */
Packit 7838c8
                {
Packit 7838c8
                if ((page_width != 0) || (page_height != 0)) /* Image scaled */
Packit 7838c8
                  {
Packit 7838c8
                  xscale = (reqwidth - left_offset) / psheight;
Packit 7838c8
                  yscale = (reqheight - bottom_offset) / pswidth;
Packit 7838c8
Packit 7838c8
                 view_width = reqwidth;
Packit 7838c8
                 view_height = reqheight;
Packit 7838c8
                 }
Packit 7838c8
                else
Packit 7838c8
                  {
Packit 7838c8
                  xscale = (pswidth  - left_offset)/ psheight;
Packit 7838c8
                 yscale = (psheight  - bottom_offset)/ pswidth;
Packit 7838c8
Packit 7838c8
                 view_width = psheight;
Packit 7838c8
                 view_height = pswidth;
Packit 7838c8
                  }
Packit 7838c8
                }
Packit 7838c8
              break;
Packit 7838c8
    default:  TIFFError ("psPageSize", "Invalid rotation %d", *rotation);
Packit 7838c8
              return (1);
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
  if (!npages)
Packit 7838c8
    PSHead(fd, (page_width ? page_width : view_width), (page_height ? page_height : view_height), ox, oy);
Packit 7838c8
Packit 7838c8
  *scale = (xscale < yscale) ? xscale : yscale;
Packit 7838c8
  if (*scale > 1.0)
Packit 7838c8
    *scale = 1.0;
Packit 7838c8
Packit 7838c8
  return (0);
Packit 7838c8
  }
Packit 7838c8
Packit 7838c8
int get_viewport (double pgwidth, double pgheight, double pswidth, double psheight,
Packit 7838c8
                  double *view_width, double *view_height, int rotation)
Packit 7838c8
  {
Packit 7838c8
  /* Only one of maxPageHeight or maxPageWidth can be specified */
Packit 7838c8
  if (maxPageHeight != 0)   /* Clip the viewport to maxPageHeight on each page */
Packit 7838c8
    {
Packit 7838c8
    if (pgheight != 0 && pgheight < maxPageHeight)
Packit 7838c8
      *view_height = pgheight * PS_UNIT_SIZE;
Packit 7838c8
    else
Packit 7838c8
      *view_height = maxPageHeight * PS_UNIT_SIZE;
Packit 7838c8
    /*
Packit 7838c8
     * if (res_unit == RESUNIT_CENTIMETER)
Packit 7838c8
     * *view_height /= 2.54F;
Packit 7838c8
     */
Packit 7838c8
    }
Packit 7838c8
  else
Packit 7838c8
    {
Packit 7838c8
    if (pgheight != 0) /* User has set PageHeight with -h flag */
Packit 7838c8
      {
Packit 7838c8
      *view_height = pgheight * PS_UNIT_SIZE; /* Postscript size for Page Height in inches */
Packit 7838c8
      /* if (res_unit == RESUNIT_CENTIMETER)
Packit 7838c8
       *  *view_height /= 2.54F;
Packit 7838c8
       */
Packit 7838c8
      }
Packit 7838c8
    else /* If no width or height are specified, use the original size from image */
Packit 7838c8
      switch (rotation)
Packit 7838c8
        {
Packit 7838c8
        default:
Packit 7838c8
        case   0:
Packit 7838c8
        case 180: *view_height = psheight;
Packit 7838c8
                 break;
Packit 7838c8
        case  90:
Packit 7838c8
        case 270: *view_height = pswidth;
Packit 7838c8
                 break;
Packit 7838c8
       }
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
  if (maxPageWidth != 0)   /* Clip the viewport to maxPageWidth on each page */
Packit 7838c8
    {
Packit 7838c8
    if (pgwidth != 0 && pgwidth < maxPageWidth)
Packit 7838c8
      *view_width = pgwidth * PS_UNIT_SIZE;
Packit 7838c8
    else
Packit 7838c8
      *view_width = maxPageWidth * PS_UNIT_SIZE;
Packit 7838c8
    /* if (res_unit == RESUNIT_CENTIMETER)
Packit 7838c8
     *  *view_width /= 2.54F;
Packit 7838c8
     */
Packit 7838c8
    }
Packit 7838c8
  else
Packit 7838c8
    {
Packit 7838c8
    if (pgwidth != 0)  /* User has set PageWidth with -w flag */
Packit 7838c8
      {
Packit 7838c8
      *view_width = pgwidth * PS_UNIT_SIZE; /* Postscript size for Page Width in inches */
Packit 7838c8
      /* if (res_unit == RESUNIT_CENTIMETER)
Packit 7838c8
       * *view_width /= 2.54F;
Packit 7838c8
       */
Packit 7838c8
      }
Packit 7838c8
    else  /* If no width or height are specified, use the original size from image */
Packit 7838c8
      switch (rotation)
Packit 7838c8
        {
Packit 7838c8
        default:
Packit 7838c8
        case   0:
Packit 7838c8
        case 180: *view_width = pswidth;
Packit 7838c8
                 break;
Packit 7838c8
        case  90:
Packit 7838c8
        case 270: *view_width = psheight; /* (*view_height / psheight) * psheight; */
Packit 7838c8
                 break;
Packit 7838c8
       }
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
  return (0);
Packit 7838c8
  }
Packit 7838c8
Packit 7838c8
/* pgwidth and pgheight specify page width and height in inches from -h and -w flags
Packit 7838c8
 * lm and bm are the LeftMargin and BottomMargin in inches
Packit 7838c8
 * center causes the image to be centered on the page if the paper size is
Packit 7838c8
 * larger than the image size
Packit 7838c8
 * returns the sequence number of the page processed or -1 on error
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
int TIFF2PS(FILE* fd, TIFF* tif, double pgwidth, double pgheight, double lm, double bm, int center)
Packit 7838c8
  {
Packit 7838c8
  uint32 pixwidth = 0, pixheight = 0;  /* Image width and height in pixels */
Packit 7838c8
  double ox = 0.0, oy = 0.0;  /* Offset from current Postscript origin */
Packit 7838c8
  double pswidth, psheight;   /* Original raw image width and height in points */
Packit 7838c8
  double view_width, view_height; /* Viewport width and height in points */
Packit 7838c8
  double scale = 1.0;
Packit 7838c8
  double left_offset = lm * PS_UNIT_SIZE;
Packit 7838c8
  double bottom_offset = bm * PS_UNIT_SIZE;
Packit 7838c8
  uint32 subfiletype;
Packit 7838c8
  uint16* sampleinfo;
Packit 7838c8
  static int npages = 0;
Packit 7838c8
Packit 7838c8
  if (!TIFFGetField(tif, TIFFTAG_XPOSITION, &ox))
Packit 7838c8
     ox = 0;
Packit 7838c8
  if (!TIFFGetField(tif, TIFFTAG_YPOSITION, &oy))
Packit 7838c8
     oy = 0;
Packit 7838c8
Packit 7838c8
  /* Consolidated all the tag information into one code segment, Richard Nolde */
Packit 7838c8
  do {
Packit 7838c8
     tf_numberstrips = TIFFNumberOfStrips(tif);
Packit 7838c8
     TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &tf_rowsperstrip);
Packit 7838c8
     TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &bitspersample);
Packit 7838c8
     TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
Packit 7838c8
     TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfiguration);
Packit 7838c8
     TIFFGetField(tif, TIFFTAG_COMPRESSION, &compression);
Packit 7838c8
     TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, &extrasamples, &sampleinfo);
Packit 7838c8
     alpha = (extrasamples == 1 && sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
Packit 7838c8
     if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric))
Packit 7838c8
       {
Packit 7838c8
       switch (samplesperpixel - extrasamples)
Packit 7838c8
             {
Packit 7838c8
            case 1: if (isCCITTCompression(tif))
Packit 7838c8
                      photometric = PHOTOMETRIC_MINISWHITE;
Packit 7838c8
                    else
Packit 7838c8
                       photometric = PHOTOMETRIC_MINISBLACK;
Packit 7838c8
                    break;
Packit 7838c8
            case 3: photometric = PHOTOMETRIC_RGB;
Packit 7838c8
                    break;
Packit 7838c8
            case 4: photometric = PHOTOMETRIC_SEPARATED;
Packit 7838c8
                    break;
Packit 7838c8
            }
Packit 7838c8
       }
Packit 7838c8
Packit 7838c8
     /* Read image tags for width and height in pixels pixwidth, pixheight,
Packit 7838c8
      * and convert to points pswidth, psheight
Packit 7838c8
      */
Packit 7838c8
     setupPageState(tif, &pixwidth, &pixheight, &pswidth, &psheight);
Packit 7838c8
     view_width = pswidth;
Packit 7838c8
     view_height = psheight;
Packit 7838c8
Packit 7838c8
     if (get_viewport (pgwidth, pgheight, pswidth, psheight, &view_width, &view_height, rotation))
Packit 7838c8
       {
Packit 7838c8
       TIFFError("get_viewport", "Unable to set image viewport");
Packit 7838c8
       return (1);
Packit 7838c8
       }
Packit 7838c8
Packit 7838c8
     /* Write the Postscript file header with Bounding Box and Page Size definitions */
Packit 7838c8
     if (psStart(fd, npages, auto_rotate, &rotation, &scale, ox, oy,
Packit 7838c8
                pgwidth, pgheight, view_width, view_height, pswidth, psheight,
Packit 7838c8
                 left_offset, bottom_offset))
Packit 7838c8
       return (-1);
Packit 7838c8
Packit 7838c8
     if (checkImage(tif))  /* Aborts if unsupported image parameters */
Packit 7838c8
       {
Packit 7838c8
       tf_bytesperrow = TIFFScanlineSize(tif);
Packit 7838c8
Packit 7838c8
       /* Set viewport clipping and scaling options */
Packit 7838c8
       if ((maxPageHeight) || (maxPageWidth)  || (pgwidth != 0) || (pgheight != 0))
Packit 7838c8
         {
Packit 7838c8
        if ((maxPageHeight) || (maxPageWidth)) /* used -H or -W  option */
Packit 7838c8
           {
Packit 7838c8
          if (psMaskImage(fd, tif, rotation, center, &npages, pixwidth, pixheight,
Packit 7838c8
                          left_offset, bottom_offset, pgwidth, pgheight,
Packit 7838c8
                           pswidth, psheight, scale) < 0)
Packit 7838c8
            return (-1);
Packit 7838c8
          }
Packit 7838c8
         else  /* N.B. Setting maxPageHeight no longer sets pgheight */
Packit 7838c8
           {
Packit 7838c8
           if (pgwidth != 0 || pgheight != 0)
Packit 7838c8
             {
Packit 7838c8
             /* User did not specify a maxium page height or width using -H or -W flag
Packit 7838c8
              * but did use -h or -w flag to scale to a specific size page.
Packit 7838c8
              */
Packit 7838c8
             npages++;
Packit 7838c8
             fprintf(fd, "%%%%Page: %d %d\n", npages, npages);
Packit 7838c8
Packit 7838c8
             if (!generateEPSF && ( level2 || level3 ))
Packit 7838c8
               {
Packit 7838c8
              /* Write out the PageSize info for non EPS files */
Packit 7838c8
              if (psPageSize(fd, rotation, pgwidth, pgheight,
Packit 7838c8
                              view_width, view_height, pswidth, psheight))
Packit 7838c8
                return (-1);
Packit 7838c8
               }
Packit 7838c8
             fprintf(fd, "gsave\n");
Packit 7838c8
             fprintf(fd, "100 dict begin\n");
Packit 7838c8
             if (psScaleImage(fd, scale, rotation, center, view_width, view_height,
Packit 7838c8
                              pswidth, psheight, left_offset, bottom_offset))
Packit 7838c8
              return (-1);
Packit 7838c8
Packit 7838c8
             PSpage(fd, tif, pixwidth, pixheight);
Packit 7838c8
             fprintf(fd, "end\n");
Packit 7838c8
             fprintf(fd, "grestore\n");
Packit 7838c8
             fprintf(fd, "showpage\n");
Packit 7838c8
            }
Packit 7838c8
          }
Packit 7838c8
        }
Packit 7838c8
       else  /* Simple rotation: user did not use -H, -W, -h or -w */
Packit 7838c8
         {
Packit 7838c8
         npages++;
Packit 7838c8
         fprintf(fd, "%%%%Page: %d %d\n", npages, npages);
Packit 7838c8
Packit 7838c8
         if (!generateEPSF && ( level2 || level3 ))
Packit 7838c8
           {
Packit 7838c8
          /* Write out the PageSize info for non EPS files */
Packit 7838c8
          if (psPageSize(fd, rotation, pgwidth, pgheight,
Packit 7838c8
                          view_width, view_height, pswidth, psheight))
Packit 7838c8
           return (-1);
Packit 7838c8
         }
Packit 7838c8
         fprintf(fd, "gsave\n");
Packit 7838c8
         fprintf(fd, "100 dict begin\n");
Packit 7838c8
        if (psRotateImage(fd, rotation, pswidth, psheight, left_offset, bottom_offset))
Packit 7838c8
           return (-1);
Packit 7838c8
Packit 7838c8
         PSpage(fd, tif, pixwidth, pixheight);
Packit 7838c8
         fprintf(fd, "end\n");
Packit 7838c8
         fprintf(fd, "grestore\n");
Packit 7838c8
         fprintf(fd, "showpage\n");
Packit 7838c8
         }
Packit 7838c8
       }
Packit 7838c8
  if (generateEPSF)
Packit 7838c8
    break;
Packit 7838c8
  if (auto_rotate)
Packit 7838c8
    rotation = 0.0;
Packit 7838c8
  TIFFGetFieldDefaulted(tif, TIFFTAG_SUBFILETYPE, &subfiletype);
Packit 7838c8
  } while (((subfiletype & FILETYPE_PAGE) || printAll) && TIFFReadDirectory(tif));
Packit 7838c8
Packit 7838c8
return(npages);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static char DuplexPreamble[] = "\
Packit 7838c8
%%BeginFeature: *Duplex True\n\
Packit 7838c8
systemdict begin\n\
Packit 7838c8
  /languagelevel where { pop languagelevel } { 1 } ifelse\n\
Packit 7838c8
  2 ge { 1 dict dup /Duplex true put setpagedevice }\n\
Packit 7838c8
  { statusdict /setduplex known { statusdict begin setduplex true end } if\n\
Packit 7838c8
  } ifelse\n\
Packit 7838c8
end\n\
Packit 7838c8
%%EndFeature\n\
Packit 7838c8
";
Packit 7838c8
Packit 7838c8
static char TumblePreamble[] = "\
Packit 7838c8
%%BeginFeature: *Tumble True\n\
Packit 7838c8
systemdict begin\n\
Packit 7838c8
  /languagelevel where { pop languagelevel } { 1 } ifelse\n\
Packit 7838c8
  2 ge { 1 dict dup /Tumble true put setpagedevice }\n\
Packit 7838c8
  { statusdict /settumble known { statusdict begin true settumble end } if\n\
Packit 7838c8
  } ifelse\n\
Packit 7838c8
end\n\
Packit 7838c8
%%EndFeature\n\
Packit 7838c8
";
Packit 7838c8
Packit 7838c8
static char AvoidDeadZonePreamble[] = "\
Packit 7838c8
gsave newpath clippath pathbbox grestore\n\
Packit 7838c8
  4 2 roll 2 copy translate\n\
Packit 7838c8
  exch 3 1 roll sub 3 1 roll sub exch\n\
Packit 7838c8
  currentpagedevice /PageSize get aload pop\n\
Packit 7838c8
  exch 3 1 roll div 3 1 roll div abs exch abs\n\
Packit 7838c8
  2 copy gt { exch } if pop\n\
Packit 7838c8
  dup 1 lt { dup scale } { pop } ifelse\n\
Packit 7838c8
";
Packit 7838c8
Packit 7838c8
void
Packit 7838c8
PSHead(FILE *fd, double pagewidth, double pageheight, double xoff, double yoff)
Packit 7838c8
{
Packit 7838c8
	time_t t;
Packit 7838c8
Packit 7838c8
	t = time(0);
Packit 7838c8
	fprintf(fd, "%%!PS-Adobe-3.0%s\n", generateEPSF ? " EPSF-3.0" : "");
Packit 7838c8
	fprintf(fd, "%%%%Creator: %s\n", creator ? creator : "tiff2ps");
Packit 7838c8
        fprintf(fd, "%%%%Title: %s\n", title ? title : filename);
Packit 7838c8
	fprintf(fd, "%%%%CreationDate: %s", ctime(&t);;
Packit 7838c8
	fprintf(fd, "%%%%DocumentData: Clean7Bit\n");
Packit 7838c8
	/* NB: should use PageBoundingBox for each page instead of BoundingBox *
Packit 7838c8
         * PageBoundingBox DSC added in PSPageSize function, R Nolde 09-01-2010
Packit 7838c8
         */
Packit 7838c8
	fprintf(fd, "%%%%Origin: %ld %ld\n", (long) xoff, (long) yoff);
Packit 7838c8
        fprintf(fd, "%%%%BoundingBox: 0 0 %ld %ld\n",
Packit 7838c8
	       (long) ceil(pagewidth), (long) ceil(pageheight));
Packit 7838c8
Packit 7838c8
	fprintf(fd, "%%%%LanguageLevel: %d\n", (level3 ? 3 : (level2 ? 2 : 1)));
Packit 7838c8
        if (generateEPSF == TRUE)
Packit 7838c8
	  fprintf(fd, "%%%%Pages: 1 1\n");
Packit 7838c8
        else
Packit 7838c8
	  fprintf(fd, "%%%%Pages: (atend)\n");
Packit 7838c8
	fprintf(fd, "%%%%EndComments\n");
Packit 7838c8
        if (generateEPSF == FALSE)
Packit 7838c8
          {
Packit 7838c8
  	  fprintf(fd, "%%%%BeginSetup\n");
Packit 7838c8
	  if (PSduplex)
Packit 7838c8
		fprintf(fd, "%s", DuplexPreamble);
Packit 7838c8
	  if (PStumble)
Packit 7838c8
		fprintf(fd, "%s", TumblePreamble);
Packit 7838c8
	  if (PSavoiddeadzone && (level2 || level3))
Packit 7838c8
		fprintf(fd, "%s", AvoidDeadZonePreamble);
Packit 7838c8
	  fprintf(fd, "%%%%EndSetup\n");
Packit 7838c8
	  }
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
void
Packit 7838c8
PSTail(FILE *fd, int npages)
Packit 7838c8
{
Packit 7838c8
	fprintf(fd, "%%%%Trailer\n");
Packit 7838c8
        if (generateEPSF == FALSE)
Packit 7838c8
	  fprintf(fd, "%%%%Pages: %d\n", npages);
Packit 7838c8
	fprintf(fd, "%%%%EOF\n");
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
checkcmap(TIFF* tif, int n, uint16* r, uint16* g, uint16* b)
Packit 7838c8
{
Packit 7838c8
	(void) tif;
Packit 7838c8
	while (n-- > 0)
Packit 7838c8
		if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
Packit 7838c8
			return (16);
Packit 7838c8
	TIFFWarning(filename, "Assuming 8-bit colormap");
Packit 7838c8
	return (8);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static void
Packit 7838c8
PS_Lvl2colorspace(FILE* fd, TIFF* tif)
Packit 7838c8
{
Packit 7838c8
	uint16 *rmap, *gmap, *bmap;
Packit 7838c8
	int i, num_colors;
Packit 7838c8
	const char * colorspace_p;
Packit 7838c8
Packit 7838c8
	switch ( photometric )
Packit 7838c8
	{
Packit 7838c8
	case PHOTOMETRIC_SEPARATED:
Packit 7838c8
		colorspace_p = "CMYK";
Packit 7838c8
		break;
Packit 7838c8
Packit 7838c8
	case PHOTOMETRIC_RGB:
Packit 7838c8
		colorspace_p = "RGB";
Packit 7838c8
		break;
Packit 7838c8
Packit 7838c8
	default:
Packit 7838c8
		colorspace_p = "Gray";
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	/*
Packit 7838c8
	 * Set up PostScript Level 2 colorspace according to
Packit 7838c8
	 * section 4.8 in the PostScript refenence manual.
Packit 7838c8
	 */
Packit 7838c8
	fputs("% PostScript Level 2 only.\n", fd);
Packit 7838c8
	if (photometric != PHOTOMETRIC_PALETTE) {
Packit 7838c8
		if (photometric == PHOTOMETRIC_YCBCR) {
Packit 7838c8
		    /* MORE CODE HERE */
Packit 7838c8
		}
Packit 7838c8
		fprintf(fd, "/Device%s setcolorspace\n", colorspace_p );
Packit 7838c8
		return;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	/*
Packit 7838c8
	 * Set up an indexed/palette colorspace
Packit 7838c8
	 */
Packit 7838c8
	num_colors = (1 << bitspersample);
Packit 7838c8
	if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) {
Packit 7838c8
		TIFFError(filename,
Packit 7838c8
			"Palette image w/o \"Colormap\" tag");
Packit 7838c8
		return;
Packit 7838c8
	}
Packit 7838c8
	if (checkcmap(tif, num_colors, rmap, gmap, bmap) == 16) {
Packit 7838c8
		/*
Packit 7838c8
		 * Convert colormap to 8-bits values.
Packit 7838c8
		 */
Packit 7838c8
#define	CVT(x)		(((x) * 255) / ((1L<<16)-1))
Packit 7838c8
		for (i = 0; i < num_colors; i++) {
Packit 7838c8
			rmap[i] = CVT(rmap[i]);
Packit 7838c8
			gmap[i] = CVT(gmap[i]);
Packit 7838c8
			bmap[i] = CVT(bmap[i]);
Packit 7838c8
		}
Packit 7838c8
#undef CVT
Packit 7838c8
	}
Packit 7838c8
	fprintf(fd, "[ /Indexed /DeviceRGB %d", num_colors - 1);
Packit 7838c8
	if (ascii85) {
Packit 7838c8
		Ascii85Init();
Packit 7838c8
		fputs("\n<~", fd);
Packit 7838c8
		ascii85breaklen -= 2;
Packit 7838c8
	} else
Packit 7838c8
		fputs(" <", fd);
Packit 7838c8
	for (i = 0; i < num_colors; i++) {
Packit 7838c8
		if (ascii85) {
Packit 7838c8
			Ascii85Put((unsigned char)rmap[i], fd);
Packit 7838c8
			Ascii85Put((unsigned char)gmap[i], fd);
Packit 7838c8
			Ascii85Put((unsigned char)bmap[i], fd);
Packit 7838c8
		} else {
Packit 7838c8
			fputs((i % 8) ? " " : "\n  ", fd);
Packit 7838c8
			fprintf(fd, "%02x%02x%02x",
Packit 7838c8
			    rmap[i], gmap[i], bmap[i]);
Packit 7838c8
		}
Packit 7838c8
	}
Packit 7838c8
	if (ascii85)
Packit 7838c8
		Ascii85Flush(fd);
Packit 7838c8
	else
Packit 7838c8
		fputs(">\n", fd);
Packit 7838c8
	fputs("] setcolorspace\n", fd);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static int
Packit 7838c8
PS_Lvl2ImageDict(FILE* fd, TIFF* tif, uint32 w, uint32 h)
Packit 7838c8
{
Packit 7838c8
	int use_rawdata;
Packit 7838c8
	uint32 tile_width, tile_height;
Packit 7838c8
	uint16 predictor, minsamplevalue, maxsamplevalue;
Packit 7838c8
	int repeat_count;
Packit 7838c8
	char im_h[64], im_x[64], im_y[64];
Packit 7838c8
	char * imageOp = "image";
Packit 7838c8
Packit 7838c8
	if ( useImagemask && (bitspersample == 1) )
Packit 7838c8
		imageOp = "imagemask";
Packit 7838c8
Packit 7838c8
	(void)strcpy(im_x, "0");
Packit 7838c8
	(void)snprintf(im_y, sizeof(im_y), "%lu", (long) h);
Packit 7838c8
	(void)snprintf(im_h, sizeof(im_h), "%lu", (long) h);
Packit 7838c8
	tile_width = w;
Packit 7838c8
	tile_height = h;
Packit 7838c8
	if (TIFFIsTiled(tif)) {
Packit 7838c8
		repeat_count = TIFFNumberOfTiles(tif);
Packit 7838c8
		TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tile_width);
Packit 7838c8
		TIFFGetField(tif, TIFFTAG_TILELENGTH, &tile_height);
Packit 7838c8
		if (tile_width > w || tile_height > h ||
Packit 7838c8
		    (w % tile_width) != 0 || (h % tile_height != 0)) {
Packit 7838c8
			/*
Packit 7838c8
			 * The tiles does not fit image width and height.
Packit 7838c8
			 * Set up a clip rectangle for the image unit square.
Packit 7838c8
			 */
Packit 7838c8
			fputs("0 0 1 1 rectclip\n", fd);
Packit 7838c8
		}
Packit 7838c8
		if (tile_width < w) {
Packit 7838c8
			fputs("/im_x 0 def\n", fd);
Packit 7838c8
			(void)strcpy(im_x, "im_x neg");
Packit 7838c8
		}
Packit 7838c8
		if (tile_height < h) {
Packit 7838c8
			fputs("/im_y 0 def\n", fd);
Packit 7838c8
			(void)snprintf(im_y, sizeof(im_y), "%lu im_y sub", (unsigned long) h);
Packit 7838c8
		}
Packit 7838c8
	} else {
Packit 7838c8
		repeat_count = tf_numberstrips;
Packit 7838c8
		tile_height = tf_rowsperstrip;
Packit 7838c8
		if (tile_height > h)
Packit 7838c8
			tile_height = h;
Packit 7838c8
		if (repeat_count > 1) {
Packit 7838c8
			fputs("/im_y 0 def\n", fd);
Packit 7838c8
			fprintf(fd, "/im_h %lu def\n",
Packit 7838c8
			    (unsigned long) tile_height);
Packit 7838c8
			(void)strcpy(im_h, "im_h");
Packit 7838c8
			(void)snprintf(im_y, sizeof(im_y), "%lu im_y sub", (unsigned long) h);
Packit 7838c8
		}
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	/*
Packit 7838c8
	 * Output start of exec block
Packit 7838c8
	 */
Packit 7838c8
	fputs("{ % exec\n", fd);
Packit 7838c8
Packit 7838c8
	if (repeat_count > 1)
Packit 7838c8
		fprintf(fd, "%d { %% repeat\n", repeat_count);
Packit 7838c8
Packit 7838c8
	/*
Packit 7838c8
	 * Output filter options and image dictionary.
Packit 7838c8
	 */
Packit 7838c8
	if (ascii85)
Packit 7838c8
		fputs(" /im_stream currentfile /ASCII85Decode filter def\n",
Packit 7838c8
		    fd);
Packit 7838c8
	fputs(" <<\n", fd);
Packit 7838c8
	fputs("  /ImageType 1\n", fd);
Packit 7838c8
	fprintf(fd, "  /Width %lu\n", (unsigned long) tile_width);
Packit 7838c8
	/*
Packit 7838c8
	 * Workaround for some software that may crash when last strip
Packit 7838c8
	 * of image contains fewer number of scanlines than specified
Packit 7838c8
	 * by the `/Height' variable. So for stripped images with multiple
Packit 7838c8
	 * strips we will set `/Height' as `im_h', because one is 
Packit 7838c8
	 * recalculated for each strip - including the (smaller) final strip.
Packit 7838c8
	 * For tiled images and images with only one strip `/Height' will
Packit 7838c8
	 * contain number of scanlines in tile (or image height in case of
Packit 7838c8
	 * one-stripped image).
Packit 7838c8
	 */
Packit 7838c8
	if (TIFFIsTiled(tif) || tf_numberstrips == 1)
Packit 7838c8
		fprintf(fd, "  /Height %lu\n", (unsigned long) tile_height);
Packit 7838c8
	else
Packit 7838c8
		fprintf(fd, "  /Height im_h\n");
Packit 7838c8
	
Packit 7838c8
	if (planarconfiguration == PLANARCONFIG_SEPARATE && samplesperpixel > 1)
Packit 7838c8
		fputs("  /MultipleDataSources true\n", fd);
Packit 7838c8
	fprintf(fd, "  /ImageMatrix [ %lu 0 0 %ld %s %s ]\n",
Packit 7838c8
	    (unsigned long) w, - (long)h, im_x, im_y);
Packit 7838c8
	fprintf(fd, "  /BitsPerComponent %d\n", bitspersample);
Packit 7838c8
	fprintf(fd, "  /Interpolate %s\n", interpolate ? "true" : "false");
Packit 7838c8
Packit 7838c8
	switch (samplesperpixel - extrasamples) {
Packit 7838c8
	case 1:
Packit 7838c8
		switch (photometric) {
Packit 7838c8
		case PHOTOMETRIC_MINISBLACK:
Packit 7838c8
			fputs("  /Decode [0 1]\n", fd);
Packit 7838c8
			break;
Packit 7838c8
		case PHOTOMETRIC_MINISWHITE:
Packit 7838c8
			switch (compression) {
Packit 7838c8
			case COMPRESSION_CCITTRLE:
Packit 7838c8
			case COMPRESSION_CCITTRLEW:
Packit 7838c8
			case COMPRESSION_CCITTFAX3:
Packit 7838c8
			case COMPRESSION_CCITTFAX4:
Packit 7838c8
				/*
Packit 7838c8
				 * Manage inverting with /Blackis1 flag
Packit 7838c8
				 * since there migth be uncompressed parts
Packit 7838c8
				 */
Packit 7838c8
				fputs("  /Decode [0 1]\n", fd);
Packit 7838c8
				break;
Packit 7838c8
			default:
Packit 7838c8
				/*
Packit 7838c8
				 * ERROR...
Packit 7838c8
				 */
Packit 7838c8
				fputs("  /Decode [1 0]\n", fd);
Packit 7838c8
				break;
Packit 7838c8
			}
Packit 7838c8
			break;
Packit 7838c8
		case PHOTOMETRIC_PALETTE:
Packit 7838c8
			TIFFGetFieldDefaulted(tif, TIFFTAG_MINSAMPLEVALUE,
Packit 7838c8
			    &minsamplevalue);
Packit 7838c8
			TIFFGetFieldDefaulted(tif, TIFFTAG_MAXSAMPLEVALUE,
Packit 7838c8
			    &maxsamplevalue);
Packit 7838c8
			fprintf(fd, "  /Decode [%u %u]\n",
Packit 7838c8
				    minsamplevalue, maxsamplevalue);
Packit 7838c8
			break;
Packit 7838c8
		default:
Packit 7838c8
			/*
Packit 7838c8
			 * ERROR ?
Packit 7838c8
			 */
Packit 7838c8
			fputs("  /Decode [0 1]\n", fd);
Packit 7838c8
			break;
Packit 7838c8
		}
Packit 7838c8
		break;
Packit 7838c8
	case 3:
Packit 7838c8
		switch (photometric) {
Packit 7838c8
		case PHOTOMETRIC_RGB:
Packit 7838c8
			fputs("  /Decode [0 1 0 1 0 1]\n", fd);
Packit 7838c8
			break;
Packit 7838c8
		case PHOTOMETRIC_MINISWHITE:
Packit 7838c8
		case PHOTOMETRIC_MINISBLACK:
Packit 7838c8
		default:
Packit 7838c8
			/*
Packit 7838c8
			 * ERROR??
Packit 7838c8
			 */
Packit 7838c8
			fputs("  /Decode [0 1 0 1 0 1]\n", fd);
Packit 7838c8
			break;
Packit 7838c8
		}
Packit 7838c8
		break;
Packit 7838c8
	case 4:
Packit 7838c8
		/*
Packit 7838c8
		 * ERROR??
Packit 7838c8
		 */
Packit 7838c8
		fputs("  /Decode [0 1 0 1 0 1 0 1]\n", fd);
Packit 7838c8
		break;
Packit 7838c8
	}
Packit 7838c8
	fputs("  /DataSource", fd);
Packit 7838c8
	if (planarconfiguration == PLANARCONFIG_SEPARATE &&
Packit 7838c8
	    samplesperpixel > 1)
Packit 7838c8
		fputs(" [", fd);
Packit 7838c8
	if (ascii85)
Packit 7838c8
		fputs(" im_stream", fd);
Packit 7838c8
	else
Packit 7838c8
		fputs(" currentfile /ASCIIHexDecode filter", fd);
Packit 7838c8
Packit 7838c8
	use_rawdata = TRUE;
Packit 7838c8
	switch (compression) {
Packit 7838c8
	case COMPRESSION_NONE:		/* 1: uncompressed */
Packit 7838c8
		break;
Packit 7838c8
	case COMPRESSION_CCITTRLE:	/* 2: CCITT modified Huffman RLE */
Packit 7838c8
	case COMPRESSION_CCITTRLEW:	/* 32771: #1 w/ word alignment */
Packit 7838c8
	case COMPRESSION_CCITTFAX3:	/* 3: CCITT Group 3 fax encoding */
Packit 7838c8
	case COMPRESSION_CCITTFAX4:	/* 4: CCITT Group 4 fax encoding */
Packit 7838c8
		fputs("\n\t<<\n", fd);
Packit 7838c8
		if (compression == COMPRESSION_CCITTFAX3) {
Packit 7838c8
			uint32 g3_options;
Packit 7838c8
Packit 7838c8
			fputs("\t /EndOfLine true\n", fd);
Packit 7838c8
			fputs("\t /EndOfBlock false\n", fd);
Packit 7838c8
			if (!TIFFGetField(tif, TIFFTAG_GROUP3OPTIONS,
Packit 7838c8
					    &g3_options))
Packit 7838c8
				g3_options = 0;
Packit 7838c8
			if (g3_options & GROUP3OPT_2DENCODING)
Packit 7838c8
				fprintf(fd, "\t /K %s\n", im_h);
Packit 7838c8
			if (g3_options & GROUP3OPT_UNCOMPRESSED)
Packit 7838c8
				fputs("\t /Uncompressed true\n", fd);
Packit 7838c8
			if (g3_options & GROUP3OPT_FILLBITS)
Packit 7838c8
				fputs("\t /EncodedByteAlign true\n", fd);
Packit 7838c8
		}
Packit 7838c8
		if (compression == COMPRESSION_CCITTFAX4) {
Packit 7838c8
			uint32 g4_options;
Packit 7838c8
Packit 7838c8
			fputs("\t /K -1\n", fd);
Packit 7838c8
			TIFFGetFieldDefaulted(tif, TIFFTAG_GROUP4OPTIONS,
Packit 7838c8
					       &g4_options);
Packit 7838c8
			if (g4_options & GROUP4OPT_UNCOMPRESSED)
Packit 7838c8
				fputs("\t /Uncompressed true\n", fd);
Packit 7838c8
		}
Packit 7838c8
		if (!(tile_width == w && w == 1728U))
Packit 7838c8
			fprintf(fd, "\t /Columns %lu\n",
Packit 7838c8
			    (unsigned long) tile_width);
Packit 7838c8
		fprintf(fd, "\t /Rows %s\n", im_h);
Packit 7838c8
		if (compression == COMPRESSION_CCITTRLE ||
Packit 7838c8
		    compression == COMPRESSION_CCITTRLEW) {
Packit 7838c8
			fputs("\t /EncodedByteAlign true\n", fd);
Packit 7838c8
			fputs("\t /EndOfBlock false\n", fd);
Packit 7838c8
		}
Packit 7838c8
		if (photometric == PHOTOMETRIC_MINISBLACK)
Packit 7838c8
			fputs("\t /BlackIs1 true\n", fd);
Packit 7838c8
		fprintf(fd, "\t>> /CCITTFaxDecode filter");
Packit 7838c8
		break;
Packit 7838c8
	case COMPRESSION_LZW:	/* 5: Lempel-Ziv & Welch */
Packit 7838c8
		TIFFGetFieldDefaulted(tif, TIFFTAG_PREDICTOR, &predictor);
Packit 7838c8
		if (predictor == 2) {
Packit 7838c8
			fputs("\n\t<<\n", fd);
Packit 7838c8
			fprintf(fd, "\t /Predictor %u\n", predictor);
Packit 7838c8
			fprintf(fd, "\t /Columns %lu\n",
Packit 7838c8
			    (unsigned long) tile_width);
Packit 7838c8
			fprintf(fd, "\t /Colors %u\n", samplesperpixel);
Packit 7838c8
			fprintf(fd, "\t /BitsPerComponent %u\n",
Packit 7838c8
			    bitspersample);
Packit 7838c8
			fputs("\t>>", fd);
Packit 7838c8
		}
Packit 7838c8
		fputs(" /LZWDecode filter", fd);
Packit 7838c8
		break;
Packit 7838c8
	case COMPRESSION_DEFLATE:	/* 5: ZIP */
Packit 7838c8
	case COMPRESSION_ADOBE_DEFLATE:
Packit 7838c8
		if ( level3 ) {
Packit 7838c8
			 TIFFGetFieldDefaulted(tif, TIFFTAG_PREDICTOR, &predictor);
Packit 7838c8
			 if (predictor > 1) {
Packit 7838c8
				fprintf(fd, "\t %% PostScript Level 3 only.");
Packit 7838c8
				fputs("\n\t<<\n", fd);
Packit 7838c8
				fprintf(fd, "\t /Predictor %u\n", predictor);
Packit 7838c8
				fprintf(fd, "\t /Columns %lu\n",
Packit 7838c8
					(unsigned long) tile_width);
Packit 7838c8
				fprintf(fd, "\t /Colors %u\n", samplesperpixel);
Packit 7838c8
					fprintf(fd, "\t /BitsPerComponent %u\n",
Packit 7838c8
					bitspersample);
Packit 7838c8
				fputs("\t>>", fd);
Packit 7838c8
			 }
Packit 7838c8
			 fputs(" /FlateDecode filter", fd);
Packit 7838c8
		} else {
Packit 7838c8
			use_rawdata = FALSE ;
Packit 7838c8
		}
Packit 7838c8
		break;
Packit 7838c8
	case COMPRESSION_PACKBITS:	/* 32773: Macintosh RLE */
Packit 7838c8
		fputs(" /RunLengthDecode filter", fd);
Packit 7838c8
		use_rawdata = TRUE;
Packit 7838c8
	    break;
Packit 7838c8
	case COMPRESSION_OJPEG:		/* 6: !6.0 JPEG */
Packit 7838c8
	case COMPRESSION_JPEG:		/* 7: %JPEG DCT compression */
Packit 7838c8
#ifdef notdef
Packit 7838c8
		/*
Packit 7838c8
		 * Code not tested yet
Packit 7838c8
		 */
Packit 7838c8
		fputs(" /DCTDecode filter", fd);
Packit 7838c8
		use_rawdata = TRUE;
Packit 7838c8
#else
Packit 7838c8
		use_rawdata = FALSE;
Packit 7838c8
#endif
Packit 7838c8
		break;
Packit 7838c8
	case COMPRESSION_NEXT:		/* 32766: NeXT 2-bit RLE */
Packit 7838c8
	case COMPRESSION_THUNDERSCAN:	/* 32809: ThunderScan RLE */
Packit 7838c8
	case COMPRESSION_PIXARFILM:	/* 32908: Pixar companded 10bit LZW */
Packit 7838c8
	case COMPRESSION_JBIG:		/* 34661: ISO JBIG */
Packit 7838c8
		use_rawdata = FALSE;
Packit 7838c8
		break;
Packit 7838c8
	case COMPRESSION_SGILOG:	/* 34676: SGI LogL or LogLuv */
Packit 7838c8
	case COMPRESSION_SGILOG24:	/* 34677: SGI 24-bit LogLuv */
Packit 7838c8
		use_rawdata = FALSE;
Packit 7838c8
		break;
Packit 7838c8
	default:
Packit 7838c8
		/*
Packit 7838c8
		 * ERROR...
Packit 7838c8
		 */
Packit 7838c8
		use_rawdata = FALSE;
Packit 7838c8
		break;
Packit 7838c8
	}
Packit 7838c8
	if (planarconfiguration == PLANARCONFIG_SEPARATE &&
Packit 7838c8
	    samplesperpixel > 1) {
Packit 7838c8
		uint16 i;
Packit 7838c8
Packit 7838c8
		/*
Packit 7838c8
		 * NOTE: This code does not work yet...
Packit 7838c8
		 */
Packit 7838c8
		for (i = 1; i < samplesperpixel; i++)
Packit 7838c8
			fputs(" dup", fd);
Packit 7838c8
		fputs(" ]", fd);
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	fprintf( fd, "\n >> %s\n", imageOp );
Packit 7838c8
	if (ascii85)
Packit 7838c8
		fputs(" im_stream status { im_stream flushfile } if\n", fd);
Packit 7838c8
	if (repeat_count > 1) {
Packit 7838c8
		if (tile_width < w) {
Packit 7838c8
			fprintf(fd, " /im_x im_x %lu add def\n",
Packit 7838c8
			    (unsigned long) tile_width);
Packit 7838c8
			if (tile_height < h) {
Packit 7838c8
				fprintf(fd, " im_x %lu ge {\n",
Packit 7838c8
				    (unsigned long) w);
Packit 7838c8
				fputs("  /im_x 0 def\n", fd);
Packit 7838c8
				fprintf(fd, " /im_y im_y %lu add def\n",
Packit 7838c8
				    (unsigned long) tile_height);
Packit 7838c8
				fputs(" } if\n", fd);
Packit 7838c8
			}
Packit 7838c8
		}
Packit 7838c8
		if (tile_height < h) {
Packit 7838c8
			if (tile_width >= w) {
Packit 7838c8
				fprintf(fd, " /im_y im_y %lu add def\n",
Packit 7838c8
				    (unsigned long) tile_height);
Packit 7838c8
				if (!TIFFIsTiled(tif)) {
Packit 7838c8
					fprintf(fd, " /im_h %lu im_y sub",
Packit 7838c8
					    (unsigned long) h);
Packit 7838c8
					fprintf(fd, " dup %lu gt { pop",
Packit 7838c8
					    (unsigned long) tile_height);
Packit 7838c8
					fprintf(fd, " %lu } if def\n",
Packit 7838c8
					    (unsigned long) tile_height);
Packit 7838c8
				}
Packit 7838c8
			}
Packit 7838c8
		}
Packit 7838c8
		fputs("} repeat\n", fd);
Packit 7838c8
	}
Packit 7838c8
	/*
Packit 7838c8
	 * End of exec function
Packit 7838c8
	 */
Packit 7838c8
	fputs("}\n", fd);
Packit 7838c8
Packit 7838c8
	return(use_rawdata);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/* Flip the byte order of buffers with 16 bit samples */
Packit 7838c8
static void
Packit 7838c8
PS_FlipBytes(unsigned char* buf, tsize_t count)
Packit 7838c8
{
Packit 7838c8
	int i;
Packit 7838c8
	unsigned char temp;
Packit 7838c8
Packit 7838c8
	if (count <= 0 || bitspersample <= 8) {
Packit 7838c8
		return;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	count--;
Packit 7838c8
Packit 7838c8
	for (i = 0; i < count; i += 2) {
Packit 7838c8
		temp = buf[i];
Packit 7838c8
		buf[i] = buf[i + 1];
Packit 7838c8
		buf[i + 1] = temp;
Packit 7838c8
	}
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
#define MAXLINE		36
Packit 7838c8
Packit 7838c8
int
Packit 7838c8
PS_Lvl2page(FILE* fd, TIFF* tif, uint32 w, uint32 h)
Packit 7838c8
{
Packit 7838c8
	uint16 fillorder;
Packit 7838c8
	int use_rawdata, tiled_image, breaklen = MAXLINE;
Packit 7838c8
	uint32 chunk_no, num_chunks;
Packit 7838c8
        uint64 *bc;
Packit 7838c8
	unsigned char *buf_data, *cp;
Packit 7838c8
	tsize_t chunk_size, byte_count;
Packit 7838c8
Packit 7838c8
#if defined( EXP_ASCII85ENCODER )
Packit 7838c8
	tsize_t			ascii85_l;	/* Length, in bytes, of ascii85_p[] data */
Packit 7838c8
	uint8		*	ascii85_p = 0;	/* Holds ASCII85 encoded data */
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
	PS_Lvl2colorspace(fd, tif);
Packit 7838c8
	use_rawdata = PS_Lvl2ImageDict(fd, tif, w, h);
Packit 7838c8
Packit 7838c8
/* See http://bugzilla.remotesensing.org/show_bug.cgi?id=80 */
Packit 7838c8
#ifdef ENABLE_BROKEN_BEGINENDDATA
Packit 7838c8
	fputs("%%BeginData:\n", fd);
Packit 7838c8
#endif
Packit 7838c8
	fputs("exec\n", fd);
Packit 7838c8
Packit 7838c8
	tiled_image = TIFFIsTiled(tif);
Packit 7838c8
	if (tiled_image) {
Packit 7838c8
		num_chunks = TIFFNumberOfTiles(tif);
Packit 7838c8
		TIFFGetField(tif, TIFFTAG_TILEBYTECOUNTS, &bc);
Packit 7838c8
	} else {
Packit 7838c8
		num_chunks = TIFFNumberOfStrips(tif);
Packit 7838c8
		TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bc);
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	if (use_rawdata) {
Packit 7838c8
		chunk_size = (tsize_t) bc[0];
Packit 7838c8
		for (chunk_no = 1; chunk_no < num_chunks; chunk_no++)
Packit 7838c8
			if ((tsize_t) bc[chunk_no] > chunk_size)
Packit 7838c8
				chunk_size = (tsize_t) bc[chunk_no];
Packit 7838c8
	} else {
Packit 7838c8
		if (tiled_image)
Packit 7838c8
			chunk_size = TIFFTileSize(tif);
Packit 7838c8
		else
Packit 7838c8
			chunk_size = TIFFStripSize(tif);
Packit 7838c8
	}
Packit 7838c8
	buf_data = (unsigned char *)_TIFFmalloc(chunk_size);
Packit 7838c8
	if (!buf_data) {
Packit 7838c8
		TIFFError(filename, "Can't alloc %lu bytes for %s.",
Packit 7838c8
			(unsigned long) chunk_size, tiled_image ? "tiles" : "strips");
Packit 7838c8
		return(FALSE);
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
#if defined( EXP_ASCII85ENCODER )
Packit 7838c8
	if ( ascii85 ) {
Packit 7838c8
	    /*
Packit 7838c8
	     * Allocate a buffer to hold the ASCII85 encoded data.  Note
Packit 7838c8
	     * that it is allocated with sufficient room to hold the
Packit 7838c8
	     * encoded data (5*chunk_size/4) plus the EOD marker (+8)
Packit 7838c8
	     * and formatting line breaks.  The line breaks are more
Packit 7838c8
	     * than taken care of by using 6*chunk_size/4 rather than
Packit 7838c8
	     * 5*chunk_size/4.
Packit 7838c8
	     */
Packit 7838c8
Packit 7838c8
	    ascii85_p = _TIFFmalloc( (chunk_size+(chunk_size/2)) + 8 );
Packit 7838c8
Packit 7838c8
	    if ( !ascii85_p ) {
Packit 7838c8
		_TIFFfree( buf_data );
Packit 7838c8
Packit 7838c8
		TIFFError( filename, "Cannot allocate ASCII85 encoding buffer." );
Packit 7838c8
		return ( FALSE );
Packit 7838c8
	    }
Packit 7838c8
	}
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
	TIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &fillorder);
Packit 7838c8
	for (chunk_no = 0; chunk_no < num_chunks; chunk_no++) {
Packit 7838c8
		if (ascii85)
Packit 7838c8
			Ascii85Init();
Packit 7838c8
		else
Packit 7838c8
			breaklen = MAXLINE;
Packit 7838c8
		if (use_rawdata) {
Packit 7838c8
			if (tiled_image)
Packit 7838c8
				byte_count = TIFFReadRawTile(tif, chunk_no,
Packit 7838c8
						  buf_data, chunk_size);
Packit 7838c8
			else
Packit 7838c8
				byte_count = TIFFReadRawStrip(tif, chunk_no,
Packit 7838c8
						  buf_data, chunk_size);
Packit 7838c8
			if (fillorder == FILLORDER_LSB2MSB)
Packit 7838c8
			    TIFFReverseBits(buf_data, byte_count);
Packit 7838c8
		} else {
Packit 7838c8
			if (tiled_image)
Packit 7838c8
				byte_count = TIFFReadEncodedTile(tif,
Packit 7838c8
						chunk_no, buf_data,
Packit 7838c8
						chunk_size);
Packit 7838c8
			else
Packit 7838c8
				byte_count = TIFFReadEncodedStrip(tif,
Packit 7838c8
						chunk_no, buf_data,
Packit 7838c8
						chunk_size);
Packit 7838c8
		}
Packit 7838c8
		if (byte_count < 0) {
Packit 7838c8
			TIFFError(filename, "Can't read %s %d.",
Packit 7838c8
				tiled_image ? "tile" : "strip", chunk_no);
Packit 7838c8
			if (ascii85)
Packit 7838c8
				Ascii85Put('\0', fd);
Packit 7838c8
		}
Packit 7838c8
		/*
Packit 7838c8
		 * for 16 bits, the two bytes must be most significant
Packit 7838c8
		 * byte first
Packit 7838c8
		 */
Packit 7838c8
		if (bitspersample == 16 && !TIFFIsBigEndian(tif)) {
Packit 7838c8
			PS_FlipBytes(buf_data, byte_count);
Packit 7838c8
		}
Packit 7838c8
		/*
Packit 7838c8
		 * For images with alpha, matte against a white background;
Packit 7838c8
		 * i.e. Cback * (1 - Aimage) where Cback = 1. We will fill the
Packit 7838c8
		 * lower part of the buffer with the modified values.
Packit 7838c8
		 *
Packit 7838c8
		 * XXX: needs better solution
Packit 7838c8
		 */
Packit 7838c8
		if (alpha) {
Packit 7838c8
			int adjust, i, j = 0;
Packit 7838c8
			int ncomps = samplesperpixel - extrasamples;
Packit 7838c8
			for (i = 0; i < byte_count; i+=samplesperpixel) {
Packit 7838c8
				adjust = 255 - buf_data[i + ncomps];
Packit 7838c8
				switch (ncomps) {
Packit 7838c8
					case 1:
Packit 7838c8
						buf_data[j++] = buf_data[i] + adjust;
Packit 7838c8
						break;
Packit 7838c8
					case 2:
Packit 7838c8
						buf_data[j++] = buf_data[i] + adjust;
Packit 7838c8
						buf_data[j++] = buf_data[i+1] + adjust;
Packit 7838c8
						break;
Packit 7838c8
					case 3:
Packit 7838c8
						buf_data[j++] = buf_data[i] + adjust;
Packit 7838c8
						buf_data[j++] = buf_data[i+1] + adjust;
Packit 7838c8
						buf_data[j++] = buf_data[i+2] + adjust;
Packit 7838c8
						break;
Packit 7838c8
				}
Packit 7838c8
			}
Packit 7838c8
			byte_count -= j;
Packit 7838c8
		}
Packit 7838c8
Packit 7838c8
		if (ascii85) {
Packit 7838c8
#if defined( EXP_ASCII85ENCODER )
Packit 7838c8
			ascii85_l = Ascii85EncodeBlock(ascii85_p, 1, buf_data, byte_count );
Packit 7838c8
Packit 7838c8
			if ( ascii85_l > 0 )
Packit 7838c8
				fwrite( ascii85_p, ascii85_l, 1, fd );
Packit 7838c8
#else
Packit 7838c8
			for (cp = buf_data; byte_count > 0; byte_count--)
Packit 7838c8
				Ascii85Put(*cp++, fd);
Packit 7838c8
#endif
Packit 7838c8
		}
Packit 7838c8
		else
Packit 7838c8
		{
Packit 7838c8
			for (cp = buf_data; byte_count > 0; byte_count--) {
Packit 7838c8
				putc(hex[((*cp)>>4)&0xf], fd);
Packit 7838c8
				putc(hex[(*cp)&0xf], fd);
Packit 7838c8
				cp++;
Packit 7838c8
Packit 7838c8
				if (--breaklen <= 0) {
Packit 7838c8
					putc('\n', fd);
Packit 7838c8
					breaklen = MAXLINE;
Packit 7838c8
				}
Packit 7838c8
			}
Packit 7838c8
		}
Packit 7838c8
Packit 7838c8
		if ( !ascii85 ) {
Packit 7838c8
			if ( level2 || level3 )
Packit 7838c8
				putc( '>', fd );
Packit 7838c8
			putc('\n', fd);
Packit 7838c8
		}
Packit 7838c8
#if !defined( EXP_ASCII85ENCODER )
Packit 7838c8
		else
Packit 7838c8
			Ascii85Flush(fd);
Packit 7838c8
#endif
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
#if defined( EXP_ASCII85ENCODER )
Packit 7838c8
	if ( ascii85_p )
Packit 7838c8
	    _TIFFfree( ascii85_p );
Packit 7838c8
#endif
Packit 7838c8
       
Packit 7838c8
	_TIFFfree(buf_data);
Packit 7838c8
#ifdef ENABLE_BROKEN_BEGINENDDATA
Packit 7838c8
	fputs("%%EndData\n", fd);
Packit 7838c8
#endif
Packit 7838c8
	return(TRUE);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
void
Packit 7838c8
PSpage(FILE* fd, TIFF* tif, uint32 w, uint32 h)
Packit 7838c8
{
Packit 7838c8
	char	*	imageOp = "image";
Packit 7838c8
Packit 7838c8
	if ( useImagemask && (bitspersample == 1) )
Packit 7838c8
		imageOp = "imagemask";
Packit 7838c8
Packit 7838c8
	if ((level2 || level3) && PS_Lvl2page(fd, tif, w, h))
Packit 7838c8
		return;
Packit 7838c8
	ps_bytesperrow = tf_bytesperrow - (extrasamples * bitspersample / 8)*w;
Packit 7838c8
	switch (photometric) {
Packit 7838c8
	case PHOTOMETRIC_RGB:
Packit 7838c8
		if (planarconfiguration == PLANARCONFIG_CONTIG) {
Packit 7838c8
			fprintf(fd, "%s", RGBcolorimage);
Packit 7838c8
			PSColorContigPreamble(fd, w, h, 3);
Packit 7838c8
			PSDataColorContig(fd, tif, w, h, 3);
Packit 7838c8
		} else {
Packit 7838c8
			PSColorSeparatePreamble(fd, w, h, 3);
Packit 7838c8
			PSDataColorSeparate(fd, tif, w, h, 3);
Packit 7838c8
		}
Packit 7838c8
		break;
Packit 7838c8
	case PHOTOMETRIC_SEPARATED:
Packit 7838c8
		/* XXX should emit CMYKcolorimage */
Packit 7838c8
		if (planarconfiguration == PLANARCONFIG_CONTIG) {
Packit 7838c8
			PSColorContigPreamble(fd, w, h, 4);
Packit 7838c8
			PSDataColorContig(fd, tif, w, h, 4);
Packit 7838c8
		} else {
Packit 7838c8
			PSColorSeparatePreamble(fd, w, h, 4);
Packit 7838c8
			PSDataColorSeparate(fd, tif, w, h, 4);
Packit 7838c8
		}
Packit 7838c8
		break;
Packit 7838c8
	case PHOTOMETRIC_PALETTE:
Packit 7838c8
		fprintf(fd, "%s", RGBcolorimage);
Packit 7838c8
		PhotoshopBanner(fd, w, h, 1, 3, "false 3 colorimage");
Packit 7838c8
		fprintf(fd, "/scanLine %ld string def\n",
Packit 7838c8
		    (long) ps_bytesperrow * 3L);
Packit 7838c8
		fprintf(fd, "%lu %lu 8\n",
Packit 7838c8
		    (unsigned long) w, (unsigned long) h);
Packit 7838c8
		fprintf(fd, "[%lu 0 0 -%lu 0 %lu]\n",
Packit 7838c8
		    (unsigned long) w, (unsigned long) h, (unsigned long) h);
Packit 7838c8
		fprintf(fd, "{currentfile scanLine readhexstring pop} bind\n");
Packit 7838c8
		fprintf(fd, "false 3 colorimage\n");
Packit 7838c8
		PSDataPalette(fd, tif, w, h);
Packit 7838c8
		break;
Packit 7838c8
	case PHOTOMETRIC_MINISBLACK:
Packit 7838c8
	case PHOTOMETRIC_MINISWHITE:
Packit 7838c8
		PhotoshopBanner(fd, w, h, 1, 1, imageOp);
Packit 7838c8
		fprintf(fd, "/scanLine %ld string def\n",
Packit 7838c8
		    (long) ps_bytesperrow);
Packit 7838c8
		fprintf(fd, "%lu %lu %d\n",
Packit 7838c8
		    (unsigned long) w, (unsigned long) h, bitspersample);
Packit 7838c8
		fprintf(fd, "[%lu 0 0 -%lu 0 %lu]\n",
Packit 7838c8
		    (unsigned long) w, (unsigned long) h, (unsigned long) h);
Packit 7838c8
		fprintf(fd,
Packit 7838c8
		    "{currentfile scanLine readhexstring pop} bind\n");
Packit 7838c8
		fprintf(fd, "%s\n", imageOp);
Packit 7838c8
		PSDataBW(fd, tif, w, h);
Packit 7838c8
		break;
Packit 7838c8
	}
Packit 7838c8
	putc('\n', fd);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
void
Packit 7838c8
PSColorContigPreamble(FILE* fd, uint32 w, uint32 h, int nc)
Packit 7838c8
{
Packit 7838c8
	ps_bytesperrow = nc * (tf_bytesperrow / samplesperpixel);
Packit 7838c8
	PhotoshopBanner(fd, w, h, 1, nc, "false %d colorimage");
Packit 7838c8
	fprintf(fd, "/line %ld string def\n", (long) ps_bytesperrow);
Packit 7838c8
	fprintf(fd, "%lu %lu %d\n",
Packit 7838c8
	    (unsigned long) w, (unsigned long) h, bitspersample);
Packit 7838c8
	fprintf(fd, "[%lu 0 0 -%lu 0 %lu]\n",
Packit 7838c8
	    (unsigned long) w, (unsigned long) h, (unsigned long) h);
Packit 7838c8
	fprintf(fd, "{currentfile line readhexstring pop} bind\n");
Packit 7838c8
	fprintf(fd, "false %d colorimage\n", nc);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
void
Packit 7838c8
PSColorSeparatePreamble(FILE* fd, uint32 w, uint32 h, int nc)
Packit 7838c8
{
Packit 7838c8
	int i;
Packit 7838c8
Packit 7838c8
	PhotoshopBanner(fd, w, h, ps_bytesperrow, nc, "true %d colorimage");
Packit 7838c8
	for (i = 0; i < nc; i++)
Packit 7838c8
		fprintf(fd, "/line%d %ld string def\n",
Packit 7838c8
		    i, (long) ps_bytesperrow);
Packit 7838c8
	fprintf(fd, "%lu %lu %d\n",
Packit 7838c8
	    (unsigned long) w, (unsigned long) h, bitspersample);
Packit 7838c8
	fprintf(fd, "[%lu 0 0 -%lu 0 %lu] \n",
Packit 7838c8
	    (unsigned long) w, (unsigned long) h, (unsigned long) h);
Packit 7838c8
	for (i = 0; i < nc; i++)
Packit 7838c8
		fprintf(fd, "{currentfile line%d readhexstring pop}bind\n", i);
Packit 7838c8
	fprintf(fd, "true %d colorimage\n", nc);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
#define	DOBREAK(len, howmany, fd) \
Packit 7838c8
	if (((len) -= (howmany)) <= 0) {	\
Packit 7838c8
		putc('\n', fd);			\
Packit 7838c8
		(len) = MAXLINE-(howmany);	\
Packit 7838c8
	}
Packit 7838c8
#define	PUTHEX(c,fd)	putc(hex[((c)>>4)&0xf],fd); putc(hex[(c)&0xf],fd)
Packit 7838c8
Packit 7838c8
void
Packit 7838c8
PSDataColorContig(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc)
Packit 7838c8
{
Packit 7838c8
	uint32 row;
Packit 7838c8
	int breaklen = MAXLINE, es = samplesperpixel - nc;
Packit 7838c8
	tsize_t cc;
Packit 7838c8
	unsigned char *tf_buf;
Packit 7838c8
	unsigned char *cp, c;
Packit 7838c8
Packit 7838c8
	(void) w;
Packit 7838c8
        if( es <= 0 )
Packit 7838c8
        {
Packit 7838c8
            TIFFError(filename, "Inconsistent value of es: %d", es);
Packit 7838c8
            return;
Packit 7838c8
        }
Packit 7838c8
	tf_buf = (unsigned char *) _TIFFmalloc(tf_bytesperrow);
Packit 7838c8
	if (tf_buf == NULL) {
Packit 7838c8
		TIFFError(filename, "No space for scanline buffer");
Packit 7838c8
		return;
Packit 7838c8
	}
Packit 7838c8
	for (row = 0; row < h; row++) {
Packit 7838c8
		if (TIFFReadScanline(tif, tf_buf, row, 0) < 0)
Packit 7838c8
			break;
Packit 7838c8
		cp = tf_buf;
Packit 7838c8
		/*
Packit 7838c8
		 * for 16 bits, the two bytes must be most significant
Packit 7838c8
		 * byte first
Packit 7838c8
		 */
Packit 7838c8
		if (bitspersample == 16 && !HOST_BIGENDIAN) {
Packit 7838c8
			PS_FlipBytes(cp, tf_bytesperrow);
Packit 7838c8
		}
Packit 7838c8
		if (alpha) {
Packit 7838c8
			int adjust;
Packit 7838c8
			cc = 0;
Packit 7838c8
			for (; cc < tf_bytesperrow; cc += samplesperpixel) {
Packit 7838c8
				DOBREAK(breaklen, nc, fd);
Packit 7838c8
				/*
Packit 7838c8
				 * For images with alpha, matte against
Packit 7838c8
				 * a white background; i.e.
Packit 7838c8
				 *    Cback * (1 - Aimage)
Packit 7838c8
				 * where Cback = 1.
Packit 7838c8
				 */
Packit 7838c8
				adjust = 255 - cp[nc];
Packit 7838c8
				switch (nc) {
Packit 7838c8
				case 4: c = *cp++ + adjust; PUTHEX(c,fd);
Packit 7838c8
				case 3: c = *cp++ + adjust; PUTHEX(c,fd);
Packit 7838c8
				case 2: c = *cp++ + adjust; PUTHEX(c,fd);
Packit 7838c8
				case 1: c = *cp++ + adjust; PUTHEX(c,fd);
Packit 7838c8
				}
Packit 7838c8
				cp += es;
Packit 7838c8
			}
Packit 7838c8
		} else {
Packit 7838c8
			cc = 0;
Packit 7838c8
			for (; cc < tf_bytesperrow; cc += samplesperpixel) {
Packit 7838c8
				DOBREAK(breaklen, nc, fd);
Packit 7838c8
				switch (nc) {
Packit 7838c8
				case 4: c = *cp++; PUTHEX(c,fd);
Packit 7838c8
				case 3: c = *cp++; PUTHEX(c,fd);
Packit 7838c8
				case 2: c = *cp++; PUTHEX(c,fd);
Packit 7838c8
				case 1: c = *cp++; PUTHEX(c,fd);
Packit 7838c8
				}
Packit 7838c8
				cp += es;
Packit 7838c8
			}
Packit 7838c8
		}
Packit 7838c8
	}
Packit 7838c8
	_TIFFfree((char *) tf_buf);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
void
Packit 7838c8
PSDataColorSeparate(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc)
Packit 7838c8
{
Packit 7838c8
	uint32 row;
Packit 7838c8
	int breaklen = MAXLINE;
Packit 7838c8
	tsize_t cc;
Packit 7838c8
	tsample_t s, maxs;
Packit 7838c8
	unsigned char *tf_buf;
Packit 7838c8
	unsigned char *cp, c;
Packit 7838c8
Packit 7838c8
	(void) w;
Packit 7838c8
	tf_buf = (unsigned char *) _TIFFmalloc(tf_bytesperrow);
Packit 7838c8
	if (tf_buf == NULL) {
Packit 7838c8
		TIFFError(filename, "No space for scanline buffer");
Packit 7838c8
		return;
Packit 7838c8
	}
Packit 7838c8
	maxs = (samplesperpixel > nc ? nc : samplesperpixel);
Packit 7838c8
	for (row = 0; row < h; row++) {
Packit 7838c8
		for (s = 0; s < maxs; s++) {
Packit 7838c8
			if (TIFFReadScanline(tif, tf_buf, row, s) < 0)
Packit 7838c8
				break;
Packit 7838c8
			for (cp = tf_buf, cc = 0; cc < tf_bytesperrow; cc++) {
Packit 7838c8
				DOBREAK(breaklen, 1, fd);
Packit 7838c8
				c = *cp++;
Packit 7838c8
				PUTHEX(c,fd);
Packit 7838c8
			}
Packit 7838c8
		}
Packit 7838c8
	}
Packit 7838c8
	_TIFFfree((char *) tf_buf);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
#define	PUTRGBHEX(c,fd) \
Packit 7838c8
	PUTHEX(rmap[c],fd); PUTHEX(gmap[c],fd); PUTHEX(bmap[c],fd)
Packit 7838c8
Packit 7838c8
void
Packit 7838c8
PSDataPalette(FILE* fd, TIFF* tif, uint32 w, uint32 h)
Packit 7838c8
{
Packit 7838c8
	uint16 *rmap, *gmap, *bmap;
Packit 7838c8
	uint32 row;
Packit 7838c8
	int breaklen = MAXLINE, nc;
Packit 7838c8
	tsize_t cc;
Packit 7838c8
	unsigned char *tf_buf;
Packit 7838c8
	unsigned char *cp, c;
Packit 7838c8
Packit 7838c8
	(void) w;
Packit 7838c8
	if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) {
Packit 7838c8
		TIFFError(filename, "Palette image w/o \"Colormap\" tag");
Packit 7838c8
		return;
Packit 7838c8
	}
Packit 7838c8
	switch (bitspersample) {
Packit 7838c8
	case 8:	case 4: case 2: case 1:
Packit 7838c8
		break;
Packit 7838c8
	default:
Packit 7838c8
		TIFFError(filename, "Depth %d not supported", bitspersample);
Packit 7838c8
		return;
Packit 7838c8
	}
Packit 7838c8
	nc = 3 * (8 / bitspersample);
Packit 7838c8
	tf_buf = (unsigned char *) _TIFFmalloc(tf_bytesperrow);
Packit 7838c8
	if (tf_buf == NULL) {
Packit 7838c8
		TIFFError(filename, "No space for scanline buffer");
Packit 7838c8
		return;
Packit 7838c8
	}
Packit 7838c8
	if (checkcmap(tif, 1<
Packit 7838c8
		int i;
Packit 7838c8
#define	CVT(x)		((unsigned short) (((x) * 255) / ((1U<<16)-1)))
Packit 7838c8
		for (i = (1<<bitspersample)-1; i >= 0; i--) {
Packit 7838c8
			rmap[i] = CVT(rmap[i]);
Packit 7838c8
			gmap[i] = CVT(gmap[i]);
Packit 7838c8
			bmap[i] = CVT(bmap[i]);
Packit 7838c8
		}
Packit 7838c8
#undef CVT
Packit 7838c8
	}
Packit 7838c8
	for (row = 0; row < h; row++) {
Packit 7838c8
		if (TIFFReadScanline(tif, tf_buf, row, 0) < 0)
Packit 7838c8
			break;
Packit 7838c8
		for (cp = tf_buf, cc = 0; cc < tf_bytesperrow; cc++) {
Packit 7838c8
			DOBREAK(breaklen, nc, fd);
Packit 7838c8
			switch (bitspersample) {
Packit 7838c8
			case 8:
Packit 7838c8
				c = *cp++; PUTRGBHEX(c, fd);
Packit 7838c8
				break;
Packit 7838c8
			case 4:
Packit 7838c8
				c = *cp++; PUTRGBHEX(c&0xf, fd);
Packit 7838c8
				c >>= 4;   PUTRGBHEX(c, fd);
Packit 7838c8
				break;
Packit 7838c8
			case 2:
Packit 7838c8
				c = *cp++; PUTRGBHEX(c&0x3, fd);
Packit 7838c8
				c >>= 2;   PUTRGBHEX(c&0x3, fd);
Packit 7838c8
				c >>= 2;   PUTRGBHEX(c&0x3, fd);
Packit 7838c8
				c >>= 2;   PUTRGBHEX(c, fd);
Packit 7838c8
				break;
Packit 7838c8
			case 1:
Packit 7838c8
				c = *cp++; PUTRGBHEX(c&0x1, fd);
Packit 7838c8
				c >>= 1;   PUTRGBHEX(c&0x1, fd);
Packit 7838c8
				c >>= 1;   PUTRGBHEX(c&0x1, fd);
Packit 7838c8
				c >>= 1;   PUTRGBHEX(c&0x1, fd);
Packit 7838c8
				c >>= 1;   PUTRGBHEX(c&0x1, fd);
Packit 7838c8
				c >>= 1;   PUTRGBHEX(c&0x1, fd);
Packit 7838c8
				c >>= 1;   PUTRGBHEX(c&0x1, fd);
Packit 7838c8
				c >>= 1;   PUTRGBHEX(c, fd);
Packit 7838c8
				break;
Packit 7838c8
			}
Packit 7838c8
		}
Packit 7838c8
	}
Packit 7838c8
	_TIFFfree((char *) tf_buf);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
void
Packit 7838c8
PSDataBW(FILE* fd, TIFF* tif, uint32 w, uint32 h)
Packit 7838c8
{
Packit 7838c8
	int breaklen = MAXLINE;
Packit 7838c8
	unsigned char* tf_buf;
Packit 7838c8
	unsigned char* cp;
Packit 7838c8
	tsize_t stripsize = TIFFStripSize(tif);
Packit 7838c8
	tstrip_t s;
Packit 7838c8
Packit 7838c8
#if defined( EXP_ASCII85ENCODER )
Packit 7838c8
	tsize_t	ascii85_l;		/* Length, in bytes, of ascii85_p[] data */
Packit 7838c8
	uint8	*ascii85_p = 0;		/* Holds ASCII85 encoded data */
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
	(void) w; (void) h;
Packit 7838c8
	tf_buf = (unsigned char *) _TIFFmalloc(stripsize);
Packit 7838c8
	if (tf_buf == NULL) {
Packit 7838c8
		TIFFError(filename, "No space for scanline buffer");
Packit 7838c8
		return;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	// FIXME
Packit 7838c8
	memset(tf_buf, 0, stripsize);
Packit 7838c8
Packit 7838c8
#if defined( EXP_ASCII85ENCODER )
Packit 7838c8
	if ( ascii85 ) {
Packit 7838c8
	    /*
Packit 7838c8
	     * Allocate a buffer to hold the ASCII85 encoded data.  Note
Packit 7838c8
	     * that it is allocated with sufficient room to hold the
Packit 7838c8
	     * encoded data (5*stripsize/4) plus the EOD marker (+8)
Packit 7838c8
	     * and formatting line breaks.  The line breaks are more
Packit 7838c8
	     * than taken care of by using 6*stripsize/4 rather than
Packit 7838c8
	     * 5*stripsize/4.
Packit 7838c8
	     */
Packit 7838c8
Packit 7838c8
	    ascii85_p = _TIFFmalloc( (stripsize+(stripsize/2)) + 8 );
Packit 7838c8
Packit 7838c8
	    if ( !ascii85_p ) {
Packit 7838c8
		_TIFFfree( tf_buf );
Packit 7838c8
Packit 7838c8
		TIFFError( filename, "Cannot allocate ASCII85 encoding buffer." );
Packit 7838c8
		return;
Packit 7838c8
	    }
Packit 7838c8
	}
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
	if (ascii85)
Packit 7838c8
		Ascii85Init();
Packit 7838c8
Packit 7838c8
	for (s = 0; s < TIFFNumberOfStrips(tif); s++) {
Packit 7838c8
		tmsize_t cc = TIFFReadEncodedStrip(tif, s, tf_buf, stripsize);
Packit 7838c8
		if (cc < 0) {
Packit 7838c8
			TIFFError(filename, "Can't read strip");
Packit 7838c8
			break;
Packit 7838c8
		}
Packit 7838c8
		cp = tf_buf;
Packit 7838c8
		if (photometric == PHOTOMETRIC_MINISWHITE) {
Packit 7838c8
			for (cp += cc; --cp >= tf_buf;)
Packit 7838c8
				*cp = ~*cp;
Packit 7838c8
			cp++;
Packit 7838c8
		}
Packit 7838c8
		/*
Packit 7838c8
		 * for 16 bits, the two bytes must be most significant
Packit 7838c8
		 * byte first
Packit 7838c8
		 */
Packit 7838c8
		if (bitspersample == 16 && !HOST_BIGENDIAN) {
Packit 7838c8
			PS_FlipBytes(cp, cc);
Packit 7838c8
		}
Packit 7838c8
		if (ascii85) {
Packit 7838c8
#if defined( EXP_ASCII85ENCODER )
Packit 7838c8
			if (alpha) {
Packit 7838c8
				int adjust, i;
Packit 7838c8
				for (i = 0; i < cc; i+=2) {
Packit 7838c8
					adjust = 255 - cp[i + 1];
Packit 7838c8
				    cp[i / 2] = cp[i] + adjust;
Packit 7838c8
				}
Packit 7838c8
				cc /= 2;
Packit 7838c8
			}
Packit 7838c8
Packit 7838c8
			ascii85_l = Ascii85EncodeBlock( ascii85_p, 1, cp, cc );
Packit 7838c8
Packit 7838c8
			if ( ascii85_l > 0 )
Packit 7838c8
			    fwrite( ascii85_p, ascii85_l, 1, fd );
Packit 7838c8
#else
Packit 7838c8
			while (cc-- > 0)
Packit 7838c8
				Ascii85Put(*cp++, fd);
Packit 7838c8
#endif /* EXP_ASCII85_ENCODER */
Packit 7838c8
		} else {
Packit 7838c8
			unsigned char c;
Packit 7838c8
Packit 7838c8
			if (alpha) {
Packit 7838c8
				int adjust;
Packit 7838c8
				while (cc-- > 1) {
Packit 7838c8
					DOBREAK(breaklen, 1, fd);
Packit 7838c8
					/*
Packit 7838c8
					 * For images with alpha, matte against
Packit 7838c8
					 * a white background; i.e.
Packit 7838c8
					 *    Cback * (1 - Aimage)
Packit 7838c8
					 * where Cback = 1.
Packit 7838c8
					 */
Packit 7838c8
					adjust = 255 - cp[1];
Packit 7838c8
					c = *cp++ + adjust; PUTHEX(c,fd);
Packit 7838c8
					cp++, cc--;
Packit 7838c8
				}
Packit 7838c8
			} else {
Packit 7838c8
				while (cc-- > 0) {
Packit 7838c8
					c = *cp++;
Packit 7838c8
					DOBREAK(breaklen, 1, fd);
Packit 7838c8
					PUTHEX(c, fd);
Packit 7838c8
				}
Packit 7838c8
			}
Packit 7838c8
		}
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	if ( !ascii85 )
Packit 7838c8
	{
Packit 7838c8
	    if ( level2 || level3)
Packit 7838c8
		fputs(">\n", fd);
Packit 7838c8
	}
Packit 7838c8
#if !defined( EXP_ASCII85ENCODER )
Packit 7838c8
	else
Packit 7838c8
	    Ascii85Flush(fd);
Packit 7838c8
#else
Packit 7838c8
	if ( ascii85_p )
Packit 7838c8
	    _TIFFfree( ascii85_p );
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
	_TIFFfree(tf_buf);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
void
Packit 7838c8
PSRawDataBW(FILE* fd, TIFF* tif, uint32 w, uint32 h)
Packit 7838c8
{
Packit 7838c8
	uint64 *bc;
Packit 7838c8
	uint32 bufsize;
Packit 7838c8
	int breaklen = MAXLINE;
Packit 7838c8
	tmsize_t cc;
Packit 7838c8
	uint16 fillorder;
Packit 7838c8
	unsigned char *tf_buf;
Packit 7838c8
	unsigned char *cp, c;
Packit 7838c8
	tstrip_t s;
Packit 7838c8
Packit 7838c8
#if defined( EXP_ASCII85ENCODER )
Packit 7838c8
	tsize_t 		ascii85_l;		/* Length, in bytes, of ascii85_p[] data */
Packit 7838c8
	uint8		*	ascii85_p = 0;		/* Holds ASCII85 encoded data */
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
	(void) w; (void) h;
Packit 7838c8
	TIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &fillorder);
Packit 7838c8
	TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bc);
Packit 7838c8
Packit 7838c8
	/*
Packit 7838c8
	 * Find largest strip:
Packit 7838c8
	 */
Packit 7838c8
Packit 7838c8
	bufsize = (uint32) bc[0];
Packit 7838c8
Packit 7838c8
	for ( s = 0; ++s < (tstrip_t)tf_numberstrips; ) {
Packit 7838c8
		if ( bc[s] > bufsize )
Packit 7838c8
			bufsize = (uint32) bc[s];
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	tf_buf = (unsigned char*) _TIFFmalloc(bufsize);
Packit 7838c8
	if (tf_buf == NULL) {
Packit 7838c8
		TIFFError(filename, "No space for strip buffer");
Packit 7838c8
		return;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
#if defined( EXP_ASCII85ENCODER )
Packit 7838c8
	if ( ascii85 ) {
Packit 7838c8
	    /*
Packit 7838c8
	     * Allocate a buffer to hold the ASCII85 encoded data.  Note
Packit 7838c8
	     * that it is allocated with sufficient room to hold the
Packit 7838c8
	     * encoded data (5*bufsize/4) plus the EOD marker (+8)
Packit 7838c8
	     * and formatting line breaks.  The line breaks are more
Packit 7838c8
	     * than taken care of by using 6*bufsize/4 rather than
Packit 7838c8
	     * 5*bufsize/4.
Packit 7838c8
	     */
Packit 7838c8
Packit 7838c8
	    ascii85_p = _TIFFmalloc( (bufsize+(bufsize/2)) + 8 );
Packit 7838c8
Packit 7838c8
	    if ( !ascii85_p ) {
Packit 7838c8
		_TIFFfree( tf_buf );
Packit 7838c8
Packit 7838c8
		TIFFError( filename, "Cannot allocate ASCII85 encoding buffer." );
Packit 7838c8
		return;
Packit 7838c8
	    }
Packit 7838c8
	}
Packit 7838c8
#endif
Packit 7838c8
Packit 7838c8
	for (s = 0; s < (tstrip_t) tf_numberstrips; s++) {
Packit 7838c8
		cc = TIFFReadRawStrip(tif, s, tf_buf, (tmsize_t) bc[s]);
Packit 7838c8
		if (cc < 0) {
Packit 7838c8
			TIFFError(filename, "Can't read strip");
Packit 7838c8
			break;
Packit 7838c8
		}
Packit 7838c8
		if (fillorder == FILLORDER_LSB2MSB)
Packit 7838c8
			TIFFReverseBits(tf_buf, cc);
Packit 7838c8
		if (!ascii85) {
Packit 7838c8
			for (cp = tf_buf; cc > 0; cc--) {
Packit 7838c8
				DOBREAK(breaklen, 1, fd);
Packit 7838c8
				c = *cp++;
Packit 7838c8
				PUTHEX(c, fd);
Packit 7838c8
			}
Packit 7838c8
			fputs(">\n", fd);
Packit 7838c8
			breaklen = MAXLINE;
Packit 7838c8
		} else {
Packit 7838c8
			Ascii85Init();
Packit 7838c8
#if defined( EXP_ASCII85ENCODER )
Packit 7838c8
			ascii85_l = Ascii85EncodeBlock( ascii85_p, 1, tf_buf, cc );
Packit 7838c8
Packit 7838c8
			if ( ascii85_l > 0 )
Packit 7838c8
				fwrite( ascii85_p, ascii85_l, 1, fd );
Packit 7838c8
#else
Packit 7838c8
			for (cp = tf_buf; cc > 0; cc--)
Packit 7838c8
				Ascii85Put(*cp++, fd);
Packit 7838c8
			Ascii85Flush(fd);
Packit 7838c8
#endif	/* EXP_ASCII85ENCODER */
Packit 7838c8
		}
Packit 7838c8
	}
Packit 7838c8
	_TIFFfree((char *) tf_buf);
Packit 7838c8
Packit 7838c8
#if defined( EXP_ASCII85ENCODER )
Packit 7838c8
	if ( ascii85_p )
Packit 7838c8
		_TIFFfree( ascii85_p );
Packit 7838c8
#endif
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
void
Packit 7838c8
Ascii85Init(void)
Packit 7838c8
{
Packit 7838c8
	ascii85breaklen = 2*MAXLINE;
Packit 7838c8
	ascii85count = 0;
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static char*
Packit 7838c8
Ascii85Encode(unsigned char* raw)
Packit 7838c8
{
Packit 7838c8
	static char encoded[6];
Packit 7838c8
	uint32 word;
Packit 7838c8
Packit 7838c8
	word = (((raw[0]<<8)+raw[1])<<16) + (raw[2]<<8) + raw[3];
Packit 7838c8
	if (word != 0L) {
Packit 7838c8
		uint32 q;
Packit 7838c8
		uint16 w1;
Packit 7838c8
Packit 7838c8
		q = word / (85L*85*85*85);	/* actually only a byte */
Packit 7838c8
		encoded[0] = (char) (q + '!');
Packit 7838c8
Packit 7838c8
		word -= q * (85L*85*85*85); q = word / (85L*85*85);
Packit 7838c8
		encoded[1] = (char) (q + '!');
Packit 7838c8
Packit 7838c8
		word -= q * (85L*85*85); q = word / (85*85);
Packit 7838c8
		encoded[2] = (char) (q + '!');
Packit 7838c8
Packit 7838c8
		w1 = (uint16) (word - q*(85L*85));
Packit 7838c8
		encoded[3] = (char) ((w1 / 85) + '!');
Packit 7838c8
		encoded[4] = (char) ((w1 % 85) + '!');
Packit 7838c8
		encoded[5] = '\0';
Packit 7838c8
	} else
Packit 7838c8
		encoded[0] = 'z', encoded[1] = '\0';
Packit 7838c8
	return (encoded);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
void
Packit 7838c8
Ascii85Put(unsigned char code, FILE* fd)
Packit 7838c8
{
Packit 7838c8
	ascii85buf[ascii85count++] = code;
Packit 7838c8
	if (ascii85count >= 4) {
Packit 7838c8
		unsigned char* p;
Packit 7838c8
		int n;
Packit 7838c8
Packit 7838c8
		for (n = ascii85count, p = ascii85buf; n >= 4; n -= 4, p += 4) {
Packit 7838c8
			char* cp;
Packit 7838c8
			for (cp = Ascii85Encode(p); *cp; cp++) {
Packit 7838c8
				putc(*cp, fd);
Packit 7838c8
				if (--ascii85breaklen == 0) {
Packit 7838c8
					putc('\n', fd);
Packit 7838c8
					ascii85breaklen = 2*MAXLINE;
Packit 7838c8
				}
Packit 7838c8
			}
Packit 7838c8
		}
Packit 7838c8
		_TIFFmemcpy(ascii85buf, p, n);
Packit 7838c8
		ascii85count = n;
Packit 7838c8
	}
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
void
Packit 7838c8
Ascii85Flush(FILE* fd)
Packit 7838c8
{
Packit 7838c8
	if (ascii85count > 0) {
Packit 7838c8
		char* res;
Packit 7838c8
		_TIFFmemset(&ascii85buf[ascii85count], 0, 3);
Packit 7838c8
		res = Ascii85Encode(ascii85buf);
Packit 7838c8
		fwrite(res[0] == 'z' ? "!!!!" : res, ascii85count + 1, 1, fd);
Packit 7838c8
	}
Packit 7838c8
	fputs("~>\n", fd);
Packit 7838c8
}
Packit 7838c8
#if	defined( EXP_ASCII85ENCODER)
Packit 7838c8

Packit 7838c8
#define A85BREAKCNTR    ascii85breaklen
Packit 7838c8
#define A85BREAKLEN     (2*MAXLINE)
Packit 7838c8
Packit 7838c8
/*****************************************************************************
Packit 7838c8
*
Packit 7838c8
* Name:         Ascii85EncodeBlock( ascii85_p, f_eod, raw_p, raw_l )
Packit 7838c8
*
Packit 7838c8
* Description:  This routine will encode the raw data in the buffer described
Packit 7838c8
*               by raw_p and raw_l into ASCII85 format and store the encoding
Packit 7838c8
*               in the buffer given by ascii85_p.
Packit 7838c8
*
Packit 7838c8
* Parameters:   ascii85_p   -   A buffer supplied by the caller which will
Packit 7838c8
*                               contain the encoded ASCII85 data.
Packit 7838c8
*               f_eod       -   Flag: Nz means to end the encoded buffer with
Packit 7838c8
*                               an End-Of-Data marker.
Packit 7838c8
*               raw_p       -   Pointer to the buffer of data to be encoded
Packit 7838c8
*               raw_l       -   Number of bytes in raw_p[] to be encoded
Packit 7838c8
*
Packit 7838c8
* Returns:      (int)   <   0   Error, see errno
Packit 7838c8
*                       >=  0   Number of bytes written to ascii85_p[].
Packit 7838c8
*
Packit 7838c8
* Notes:        An external variable given by A85BREAKCNTR is used to
Packit 7838c8
*               determine when to insert newline characters into the
Packit 7838c8
*               encoded data.  As each byte is placed into ascii85_p this
Packit 7838c8
*               external is decremented.  If the variable is decrement to
Packit 7838c8
*               or past zero then a newline is inserted into ascii85_p
Packit 7838c8
*               and the A85BREAKCNTR is then reset to A85BREAKLEN.
Packit 7838c8
*                   Note:  for efficiency reasons the A85BREAKCNTR variable
Packit 7838c8
*                          is not actually checked on *every* character
Packit 7838c8
*                          placed into ascii85_p but often only for every
Packit 7838c8
*                          5 characters.
Packit 7838c8
*
Packit 7838c8
*               THE CALLER IS RESPONSIBLE FOR ENSURING THAT ASCII85_P[] IS
Packit 7838c8
*               SUFFICIENTLY LARGE TO THE ENCODED DATA!
Packit 7838c8
*                   You will need at least 5 * (raw_l/4) bytes plus space for
Packit 7838c8
*                   newline characters and space for an EOD marker (if
Packit 7838c8
*                   requested).  A safe calculation is to use 6*(raw_l/4) + 8
Packit 7838c8
*                   to size ascii85_p.
Packit 7838c8
*
Packit 7838c8
*****************************************************************************/
Packit 7838c8
Packit 7838c8
tsize_t Ascii85EncodeBlock( uint8 * ascii85_p, unsigned f_eod, const uint8 * raw_p, tsize_t raw_l )
Packit 7838c8
Packit 7838c8
{
Packit 7838c8
    char                        ascii85[5];     /* Encoded 5 tuple */
Packit 7838c8
    tsize_t                     ascii85_l;      /* Number of bytes written to ascii85_p[] */
Packit 7838c8
    int                         rc;             /* Return code */
Packit 7838c8
    uint32                      val32;          /* Unencoded 4 tuple */
Packit 7838c8
Packit 7838c8
    ascii85_l = 0;                              /* Nothing written yet */
Packit 7838c8
Packit 7838c8
    if ( raw_p )
Packit 7838c8
    {
Packit 7838c8
        --raw_p;                                /* Prepare for pre-increment fetches */
Packit 7838c8
Packit 7838c8
        for ( ; raw_l > 3; raw_l -= 4 )
Packit 7838c8
        {
Packit 7838c8
            val32  = *(++raw_p) << 24;
Packit 7838c8
            val32 += *(++raw_p) << 16;
Packit 7838c8
            val32 += *(++raw_p) <<  8;
Packit 7838c8
            val32 += *(++raw_p);
Packit 7838c8
    
Packit 7838c8
            if ( val32 == 0 )                   /* Special case */
Packit 7838c8
            {
Packit 7838c8
                ascii85_p[ascii85_l] = 'z';
Packit 7838c8
                rc = 1;
Packit 7838c8
            }
Packit 7838c8
    
Packit 7838c8
            else
Packit 7838c8
            {
Packit 7838c8
                ascii85[4] = (char) ((val32 % 85) + 33);
Packit 7838c8
                val32 /= 85;
Packit 7838c8
    
Packit 7838c8
                ascii85[3] = (char) ((val32 % 85) + 33);
Packit 7838c8
                val32 /= 85;
Packit 7838c8
    
Packit 7838c8
                ascii85[2] = (char) ((val32 % 85) + 33);
Packit 7838c8
                val32 /= 85;
Packit 7838c8
    
Packit 7838c8
                ascii85[1] = (char) ((val32 % 85) + 33);
Packit 7838c8
                ascii85[0] = (char) ((val32 / 85) + 33);
Packit 7838c8
Packit 7838c8
                _TIFFmemcpy( &ascii85_p[ascii85_l], ascii85, sizeof(ascii85) );
Packit 7838c8
                rc = sizeof(ascii85);
Packit 7838c8
            }
Packit 7838c8
    
Packit 7838c8
            ascii85_l += rc;
Packit 7838c8
    
Packit 7838c8
            if ( (A85BREAKCNTR -= rc) <= 0 )
Packit 7838c8
            {
Packit 7838c8
                ascii85_p[ascii85_l] = '\n';
Packit 7838c8
                ++ascii85_l;
Packit 7838c8
                A85BREAKCNTR = A85BREAKLEN;
Packit 7838c8
            }
Packit 7838c8
        }
Packit 7838c8
    
Packit 7838c8
        /*
Packit 7838c8
         * Output any straggler bytes:
Packit 7838c8
         */
Packit 7838c8
    
Packit 7838c8
        if ( raw_l > 0 )
Packit 7838c8
        {
Packit 7838c8
            tsize_t         len;                /* Output this many bytes */
Packit 7838c8
    
Packit 7838c8
            len = raw_l + 1;
Packit 7838c8
            val32 = *++raw_p << 24;             /* Prime the pump */
Packit 7838c8
    
Packit 7838c8
            if ( --raw_l > 0 )  val32 += *(++raw_p) << 16;
Packit 7838c8
            if ( --raw_l > 0 )  val32 += *(++raw_p) <<  8;
Packit 7838c8
    
Packit 7838c8
            val32 /= 85;
Packit 7838c8
    
Packit 7838c8
            ascii85[3] = (char) ((val32 % 85) + 33);
Packit 7838c8
            val32 /= 85;
Packit 7838c8
    
Packit 7838c8
            ascii85[2] = (char) ((val32 % 85) + 33);
Packit 7838c8
            val32 /= 85;
Packit 7838c8
    
Packit 7838c8
            ascii85[1] = (char) ((val32 % 85) + 33);
Packit 7838c8
            ascii85[0] = (char) ((val32 / 85) + 33);
Packit 7838c8
    
Packit 7838c8
            _TIFFmemcpy( &ascii85_p[ascii85_l], ascii85, len );
Packit 7838c8
            ascii85_l += len;
Packit 7838c8
        }
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
    /*
Packit 7838c8
     * If requested add an ASCII85 End Of Data marker:
Packit 7838c8
     */
Packit 7838c8
Packit 7838c8
    if ( f_eod )
Packit 7838c8
    {
Packit 7838c8
        ascii85_p[ascii85_l++] = '~';
Packit 7838c8
        ascii85_p[ascii85_l++] = '>';
Packit 7838c8
        ascii85_p[ascii85_l++] = '\n';
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
    return ( ascii85_l );
Packit 7838c8
Packit 7838c8
}   /* Ascii85EncodeBlock() */
Packit 7838c8
Packit 7838c8
#endif	/* EXP_ASCII85ENCODER */
Packit 7838c8
Packit 7838c8
Packit 7838c8
char* stuff[] = {
Packit 7838c8
"usage: tiff2ps [options] input.tif ...",
Packit 7838c8
"where options are:",
Packit 7838c8
" -1            generate PostScript Level 1 (default)",
Packit 7838c8
" -2            generate PostScript Level 2",
Packit 7838c8
" -3            generate PostScript Level 3",
Packit 7838c8
" -8            disable use of ASCII85 encoding with PostScript Level 2/3",
Packit 7838c8
" -a            convert all directories in file (default is first), Not EPS",
Packit 7838c8
" -b #          set the bottom margin to # inches",
Packit 7838c8
" -c            center image (-b and -l still add to this)",
Packit 7838c8
" -C name       set postscript document creator name",
Packit 7838c8
" -d #          set initial directory to # counting from zero",
Packit 7838c8
" -D            enable duplex printing (two pages per sheet of paper)",
Packit 7838c8
" -e            generate Encapsulated PostScript (EPS) (implies -z)",
Packit 7838c8
" -h #          set printed page height to # inches (no default)",
Packit 7838c8
" -w #          set printed page width to # inches (no default)",
Packit 7838c8
" -H #          split image if height is more than # inches",
Packit 7838c8
" -W #          split image if width is more than # inches",
Packit 7838c8
" -L #          overLap split images by # inches",
Packit 7838c8
" -i #          enable/disable (Nz/0) pixel interpolation (default: enable)",
Packit 7838c8
" -l #          set the left margin to # inches",
Packit 7838c8
" -m            use \"imagemask\" operator instead of \"image\"",
Packit 7838c8
" -o #          convert directory at file offset # bytes",
Packit 7838c8
" -O file       write PostScript to file instead of standard output",
Packit 7838c8
" -p            generate regular (non-encapsulated) PostScript",
Packit 7838c8
" -P L or P     set optional PageOrientation DSC comment to Landscape or Portrait",
Packit 7838c8
" -r # or auto  rotate by 90, 180, 270 degrees or auto",
Packit 7838c8
" -s            generate PostScript for a single image",
Packit 7838c8
" -t name       set postscript document title. Otherwise the filename is used",
Packit 7838c8
" -T            print pages for top edge binding",
Packit 7838c8
" -x            override resolution units as centimeters",
Packit 7838c8
" -y            override resolution units as inches",
Packit 7838c8
" -z            enable printing in the deadzone (only for PostScript Level 2/3)",
Packit 7838c8
NULL
Packit 7838c8
};
Packit 7838c8
Packit 7838c8
static void
Packit 7838c8
usage(int code)
Packit 7838c8
{
Packit 7838c8
	char buf[BUFSIZ];
Packit 7838c8
	int i;
Packit 7838c8
Packit 7838c8
	setbuf(stderr, buf);
Packit 7838c8
        fprintf(stderr, "%s\n\n", TIFFGetVersion());
Packit 7838c8
	for (i = 0; stuff[i] != NULL; i++)
Packit 7838c8
		fprintf(stderr, "%s\n", stuff[i]);
Packit 7838c8
	exit(code);
Packit 7838c8
}
Packit 7838c8