From efa870dac2fdad4dd736b16e79b3c8e4e33b6055 Mon Sep 17 00:00:00 2001 From: "Matthew S. Wilson" Date: Mon, 4 Jul 2005 19:19:34 +0000 Subject: [PATCH] use the new tiff2ps interfaces 2005-07-04 Matthew S. Wilson * tiff/tiff-document.c (tiff_document_ps_export_begin) (tiff_document_ps_export_do_page, tiff_document_ps_export_end): use the new tiff2ps interfaces * tiff/tiff2ps.h (tiff2ps_context_new, tiff2ps_process_page) (tiff2ps_context_finalize): implement new interfaces for tiff2ps * tiff/tiff2ps.c: refactor tiff2ps code to use a context structure instead of global variables. Remove use of static variables in functions. --- ChangeLog | 13 + tiff/tiff-document.c | 24 +- tiff/tiff2ps.c | 1163 ++++++++++++++++++++++-------------------- tiff/tiff2ps.h | 29 +- 4 files changed, 650 insertions(+), 579 deletions(-) diff --git a/ChangeLog b/ChangeLog index d5f16234..019aaa1b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2005-07-04 Matthew S. Wilson + + * tiff/tiff-document.c (tiff_document_ps_export_begin) + (tiff_document_ps_export_do_page, tiff_document_ps_export_end): + use the new tiff2ps interfaces + + * tiff/tiff2ps.h (tiff2ps_context_new, tiff2ps_process_page) + (tiff2ps_context_finalize): implement new interfaces for tiff2ps + + * tiff/tiff2ps.c: refactor tiff2ps code to use a context + structure instead of global variables. Remove use of static + variables in functions. + 2005-07-04 Carlos Garcia Campos * shell/ev-page-action.c: unref the completion after setting it to the diff --git a/tiff/tiff-document.c b/tiff/tiff-document.c index 137833be..36847aa4 100644 --- a/tiff/tiff-document.c +++ b/tiff/tiff-document.c @@ -22,7 +22,6 @@ #include #include -#include #include "tiffio.h" #include "tiff2ps.h" @@ -43,8 +42,7 @@ struct _TiffDocument TIFF *tiff; gint n_pages; EvOrientation orientation; - FILE *ps_export_file; - gint ps_export_pages; + TIFF2PSContext *ps_export_ctx; }; typedef struct _TiffDocumentClass TiffDocumentClass; @@ -417,10 +415,7 @@ tiff_document_ps_export_begin (EvPSExporter *exporter, const char *filename, { TiffDocument *document = TIFF_DOCUMENT (exporter); - document->ps_export_file = g_fopen (filename, "w"); - if (document->ps_export_file == NULL) - return; - document->ps_export_pages = 0; + document->ps_export_ctx = tiff2ps_context_new(filename); } static void @@ -428,14 +423,12 @@ tiff_document_ps_export_do_page (EvPSExporter *exporter, int page) { TiffDocument *document = TIFF_DOCUMENT (exporter); - if (document->ps_export_file == NULL) + if (document->ps_export_ctx == NULL) return; if (TIFFSetDirectory (document->tiff, page) != 1) return; - TIFF2PS (document->ps_export_file, - document->tiff, - 0, 0, 0, 0, 0, - &document->ps_export_pages); + tiff2ps_process_page (document->ps_export_ctx, document->tiff, + 0, 0, 0, 0, 0); } static void @@ -443,12 +436,9 @@ tiff_document_ps_export_end (EvPSExporter *exporter) { TiffDocument *document = TIFF_DOCUMENT (exporter); - if (document->ps_export_file == NULL) + if (document->ps_export_ctx == NULL) return; - if (document->ps_export_pages) - TIFFPSTail (document->ps_export_file, - document->ps_export_pages); - fclose (document->ps_export_file); + tiff2ps_context_finalize(document->ps_export_ctx); } static void diff --git a/tiff/tiff2ps.c b/tiff/tiff2ps.c index 58f67daf..3e2bd32e 100644 --- a/tiff/tiff2ps.c +++ b/tiff/tiff2ps.c @@ -1,3 +1,4 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */ /* $Id$ */ /* @@ -29,7 +30,6 @@ * Matthew S. Wilson * Modifications Copyright (C) 2005 rpath, Inc. * - * FIXME: refactor to remove use of global variables */ #include @@ -39,6 +39,9 @@ #include #include +#include +#include + #include "tiff2ps.h" /* @@ -109,77 +112,110 @@ #define FALSE 0 #endif -static int ascii85 = FALSE; /* use ASCII85 encoding */ -static int interpolate = TRUE; /* interpolate level2 image */ -static int level2 = FALSE; /* generate PostScript level 2 */ -static int level3 = FALSE; /* generate PostScript level 3 */ -static int printAll = FALSE; /* print all images in file */ -static int generateEPSF = FALSE; /* generate Encapsulated PostScript */ -static int PSduplex = FALSE; /* enable duplex printing */ -static int PStumble = FALSE; /* enable top edge binding */ -static int PSavoiddeadzone = TRUE; /* enable avoiding printer deadzone */ -static double maxPageHeight = 0; /* maximum size to fit on page */ -static double splitOverlap = 0; /* amount for split pages to overlag */ -static int rotate = FALSE; /* rotate image by 180 degrees */ -static char *filename = NULL; /* input filename */ -static int useImagemask = FALSE; /* Use imagemask instead of image operator */ -static uint16 res_unit = 0; /* Resolution units: 2 - inches, 3 - cm */ -/* - * ASCII85 Encoding Support. - */ -static unsigned char ascii85buf[10]; -static int ascii85count; -static int ascii85breaklen; - -static void PSpage(FILE*, TIFF*, uint32, uint32); -static void PSColorContigPreamble(FILE*, uint32, uint32, int); -static void PSColorSeparatePreamble(FILE*, uint32, uint32, int); -static void PSDataColorContig(FILE*, TIFF*, uint32, uint32, int); -static void PSDataColorSeparate(FILE*, TIFF*, uint32, uint32, int); -static void PSDataPalette(FILE*, TIFF*, uint32, uint32); -static void PSDataBW(FILE*, TIFF*, uint32, uint32); -static void Ascii85Init(void); -static void Ascii85Put(unsigned char code, FILE* fd); -static void Ascii85Flush(FILE* fd); -static void PSHead(FILE*, TIFF*, uint32, uint32, double, double, double, double); +struct _TIFF2PSContext +{ + char *filename; /* input filename */ + FILE *fd; /* output file stream */ + int ascii85; /* use ASCII85 encoding */ + int interpolate; /* interpolate level2 image */ + int level2; /* generate PostScript level 2 */ + int level3; /* generate PostScript level 3 */ + int generateEPSF; /* generate Encapsulated PostScript */ + int PSduplex; /* enable duplex printing */ + int PStumble; /* enable top edge binding */ + int PSavoiddeadzone; /* enable avoiding printer deadzone */ + double maxPageHeight; /* maximum size to fit on page */ + double splitOverlap; /* amount for split pages to overlag */ + int rotate; /* rotate image by 180 degrees */ + int useImagemask; /* Use imagemask instead of image operator */ + uint16 res_unit; /* Resolution units: 2 - inches, 3 - cm */ + int npages; /* number of pages processed */ + + tsize_t tf_bytesperrow; + tsize_t ps_bytesperrow; + tsize_t tf_rowsperstrip; + tsize_t tf_numberstrips; -#if defined( EXP_ASCII85ENCODER) -static int Ascii85EncodeBlock( uint8 * ascii85_p, unsigned f_eod, const uint8 * raw_p, int raw_l ); + /* + * ASCII85 Encoding Support. + */ + unsigned char ascii85buf[10]; + int ascii85count; + int ascii85breaklen; + uint16 samplesperpixel; + uint16 bitspersample; + uint16 planarconfiguration; + uint16 photometric; + uint16 compression; + uint16 extrasamples; + int alpha; +}; + +static void PSpage(TIFF2PSContext*, TIFF*, uint32, uint32); +static void PSColorContigPreamble(TIFF2PSContext*, uint32, uint32, int); +static void PSColorSeparatePreamble(TIFF2PSContext*, uint32, uint32, int); +static void PSDataColorContig(TIFF2PSContext*, TIFF*, uint32, uint32, int); +static void PSDataColorSeparate(TIFF2PSContext*, TIFF*, uint32, uint32, int); +static void PSDataPalette(TIFF2PSContext*, TIFF*, uint32, uint32); +static void PSDataBW(TIFF2PSContext*, TIFF*, uint32, uint32); +static void Ascii85Init(TIFF2PSContext*); +static void Ascii85Put(TIFF2PSContext*, unsigned char); +static void Ascii85Flush(TIFF2PSContext*); +static void PSHead(TIFF2PSContext*, TIFF*, uint32, uint32, + double, double, double, double); +static void PSTail(TIFF2PSContext*); + +#if defined( EXP_ASCII85ENCODER ) +static int Ascii85EncodeBlock(TIFF2PSContext*, uint8 * ascii85_p, + unsigned f_eod, const uint8 * raw_p, int raw_l); #endif -static uint16 samplesperpixel; -static uint16 bitspersample; -static uint16 planarconfiguration; -static uint16 photometric; -static uint16 compression; -static uint16 extrasamples; -static int alpha; +TIFF2PSContext* tiff2ps_context_new(const gchar *filename) { + TIFF2PSContext* ctx; + + ctx = g_new0(TIFF2PSContext, 1); + ctx->filename = g_strdup(filename); + ctx->fd = g_fopen(ctx->filename, "w"); + if (ctx->fd == NULL) + return NULL; + ctx->interpolate = TRUE; /* interpolate level2 image */ + ctx->PSavoiddeadzone = TRUE; /* enable avoiding printer deadzone */ + return ctx; +} + +void tiff2ps_context_finalize(TIFF2PSContext *ctx) { + PSTail(ctx); + fclose(ctx->fd); + g_free(ctx->filename); + g_free(ctx); +} static int -checkImage(TIFF* tif) +checkImage(TIFF2PSContext *ctx, TIFF* tif) { - switch (photometric) { + switch (ctx->photometric) { case PHOTOMETRIC_YCBCR: - if ((compression == COMPRESSION_JPEG || compression == COMPRESSION_OJPEG) - && planarconfiguration == PLANARCONFIG_CONTIG) { + if ((ctx->compression == COMPRESSION_JPEG + || ctx->compression == COMPRESSION_OJPEG) + && ctx->planarconfiguration == PLANARCONFIG_CONTIG) { /* can rely on libjpeg to convert to RGB */ TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB); - photometric = PHOTOMETRIC_RGB; + ctx->photometric = PHOTOMETRIC_RGB; } else { - if (level2 || level3) + if (ctx->level2 || ctx->level3) break; - TIFFError(filename, "Can not handle image with %s", - "PhotometricInterpretation=YCbCr"); + TIFFError(ctx->filename, "Can not handle image with %s", + "Ctx->PhotometricInterpretation=YCbCr"); return (0); } /* fall thru... */ case PHOTOMETRIC_RGB: - if (alpha && bitspersample != 8) { - TIFFError(filename, - "Can not handle %d-bit/sample RGB image with alpha", - bitspersample); + if (ctx->alpha && ctx->bitspersample != 8) { + TIFFError(ctx->filename, + "Can not handle %d-bit/sample RGB image with ctx->alpha", + ctx->bitspersample); return (0); } /* fall thru... */ @@ -190,40 +226,41 @@ checkImage(TIFF* tif) break; case PHOTOMETRIC_LOGL: case PHOTOMETRIC_LOGLUV: - if (compression != COMPRESSION_SGILOG && - compression != COMPRESSION_SGILOG24) { - TIFFError(filename, - "Can not handle %s data with compression other than SGILog", - (photometric == PHOTOMETRIC_LOGL) ? + if (ctx->compression != COMPRESSION_SGILOG && + ctx->compression != COMPRESSION_SGILOG24) { + TIFFError(ctx->filename, + "Can not handle %s data with ctx->compression other than SGILog", + (ctx->photometric == PHOTOMETRIC_LOGL) ? "LogL" : "LogLuv" ); return (0); } /* rely on library to convert to RGB/greyscale */ TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT); - photometric = (photometric == PHOTOMETRIC_LOGL) ? + ctx->photometric = (ctx->photometric == PHOTOMETRIC_LOGL) ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB; - bitspersample = 8; + ctx->bitspersample = 8; break; case PHOTOMETRIC_CIELAB: /* fall thru... */ default: - TIFFError(filename, - "Can not handle image with PhotometricInterpretation=%d", - photometric); + TIFFError(ctx->filename, + "Can not handle image with Ctx->PhotometricInterpretation=%d", + ctx->photometric); return (0); } - switch (bitspersample) { + switch (ctx->bitspersample) { case 1: case 2: case 4: case 8: break; default: - TIFFError(filename, "Can not handle %d-bit/sample image", - bitspersample); + TIFFError(ctx->filename, "Can not handle %d-bit/sample image", + ctx->bitspersample); return (0); } - if (planarconfiguration == PLANARCONFIG_SEPARATE && extrasamples > 0) - TIFFWarning(filename, "Ignoring extra samples"); + if (ctx->planarconfiguration == PLANARCONFIG_SEPARATE && + ctx->extrasamples > 0) + TIFFWarning(ctx->filename, "Ignoring extra samples"); return (1); } @@ -260,12 +297,13 @@ static char RGBcolorimage[] = "\ * It is claimed to be part of some future revision of the EPS spec. */ static void -PhotoshopBanner(FILE* fd, uint32 w, uint32 h, int bs, int nc, char* startline) +PhotoshopBanner(TIFF2PSContext* ctx, uint32 w, uint32 h, int bs, int nc, + char* startline) { - fprintf(fd, "%%ImageData: %ld %ld %d %d 0 %d 2 \"", - (long) w, (long) h, bitspersample, nc, bs); - fprintf(fd, startline, nc); - fprintf(fd, "\"\n"); + fprintf(ctx->fd, "%%ImageData: %ld %ld %d %d 0 %d 2 \"", + (long) w, (long) h, ctx->bitspersample, nc, bs); + fprintf(ctx->fd, startline, nc); + fprintf(ctx->fd, "\"\n"); } /* @@ -275,14 +313,15 @@ PhotoshopBanner(FILE* fd, uint32 w, uint32 h, int bs, int nc, char* startline) * pprh : image height in PS units (72 dpi) */ static void -setupPageState(TIFF* tif, uint32* pw, uint32* ph, double* pprw, double* pprh) +setupPageState(TIFF2PSContext *ctx, TIFF* tif, uint32* pw, uint32* ph, + double* pprw, double* pprh) { float xres = 0.0F, yres = 0.0F; TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, pw); TIFFGetField(tif, TIFFTAG_IMAGELENGTH, ph); - if (res_unit == 0) - TIFFGetFieldDefaulted(tif, TIFFTAG_RESOLUTIONUNIT, &res_unit); + if (ctx->res_unit == 0) + TIFFGetFieldDefaulted(tif, TIFFTAG_RESOLUTIONUNIT, &ctx->res_unit); /* * Calculate printable area. */ @@ -292,7 +331,7 @@ setupPageState(TIFF* tif, uint32* pw, uint32* ph, double* pprw, double* pprh) if (!TIFFGetField(tif, TIFFTAG_YRESOLUTION, &yres) || fabs(yres) < 0.0000001) yres = PS_UNIT_SIZE; - switch (res_unit) { + switch (ctx->res_unit) { case RESUNIT_CENTIMETER: xres *= 2.54F, yres *= 2.54F; break; @@ -318,10 +357,6 @@ isCCITTCompression(TIFF* tif) compress == COMPRESSION_CCITTRLEW); } -static tsize_t tf_bytesperrow; -static tsize_t ps_bytesperrow; -static tsize_t tf_rowsperstrip; -static tsize_t tf_numberstrips; static char *hex = "0123456789abcdef"; /* @@ -329,7 +364,7 @@ static char *hex = "0123456789abcdef"; * pagewidth & pageheight are inches */ static int -PlaceImage(FILE *fp, double pagewidth, double pageheight, +PlaceImage(TIFF2PSContext *ctx, double pagewidth, double pageheight, double imagewidth, double imageheight, int splitpage, double lm, double bm, int cnt) { @@ -346,11 +381,11 @@ PlaceImage(FILE *fp, double pagewidth, double pageheight, pagewidth *= PS_UNIT_SIZE; pageheight *= PS_UNIT_SIZE; - if (maxPageHeight==0) + if (ctx->maxPageHeight==0) splitheight = 0; else - splitheight = maxPageHeight * PS_UNIT_SIZE; - overlap = splitOverlap * PS_UNIT_SIZE; + splitheight = ctx->maxPageHeight * PS_UNIT_SIZE; + overlap = ctx->splitOverlap * PS_UNIT_SIZE; /* * WIDTH: @@ -393,18 +428,18 @@ PlaceImage(FILE *fp, double pagewidth, double pageheight, bottom_offset += ytran / (cnt?2:1); if (cnt) left_offset += xtran / 2; - fprintf(fp, "%f %f translate\n", left_offset, bottom_offset); - fprintf(fp, "%f %f scale\n", xscale, yscale); - if (rotate) - fputs ("1 1 translate 180 rotate\n", fp); + fprintf(ctx->fd, "%f %f translate\n", left_offset, bottom_offset); + fprintf(ctx->fd, "%f %f scale\n", xscale, yscale); + if (ctx->rotate) + fputs ("1 1 translate 180 ctx->rotate\n", ctx->fd); return splitpage; } void -TIFF2PS(FILE* fd, TIFF* tif, - double pw, double ph, double lm, double bm, int cnt, int *npages) +tiff2ps_process_page(TIFF2PSContext* ctx, TIFF* tif, double pw, double ph, + double lm, double bm, gboolean cnt) { uint32 w, h; float ox, oy; @@ -412,7 +447,6 @@ TIFF2PS(FILE* fd, TIFF* tif, double scale = 1.0; double left_offset = lm * PS_UNIT_SIZE; double bottom_offset = bm * PS_UNIT_SIZE; - uint32 subfiletype; uint16* sampleinfo; int split; @@ -420,127 +454,122 @@ TIFF2PS(FILE* fd, TIFF* tif, ox = 0; if (!TIFFGetField(tif, TIFFTAG_YPOSITION, &oy)) oy = 0; - setupPageState(tif, &w, &h, &prw, &prh); - - do { - tf_numberstrips = TIFFNumberOfStrips(tif); - TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, - &tf_rowsperstrip); - setupPageState(tif, &w, &h, &prw, &prh); - if (!*npages) - PSHead(fd, tif, w, h, prw, prh, ox, oy); - TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, - &bitspersample); - TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, - &samplesperpixel); - TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, - &planarconfiguration); - TIFFGetField(tif, TIFFTAG_COMPRESSION, &compression); - TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, - &extrasamples, &sampleinfo); - alpha = (extrasamples == 1 && - sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA); - if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) { - switch (samplesperpixel - extrasamples) { - case 1: - if (isCCITTCompression(tif)) - photometric = PHOTOMETRIC_MINISWHITE; - else - photometric = PHOTOMETRIC_MINISBLACK; - break; - case 3: - photometric = PHOTOMETRIC_RGB; - break; - case 4: - photometric = PHOTOMETRIC_SEPARATED; - break; - } + setupPageState(ctx, tif, &w, &h, &prw, &prh); + + ctx->tf_numberstrips = TIFFNumberOfStrips(tif); + TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, + &ctx->tf_rowsperstrip); + setupPageState(ctx, tif, &w, &h, &prw, &prh); + if (!ctx->npages) + PSHead(ctx, tif, w, h, prw, prh, ox, oy); + TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, + &ctx->bitspersample); + TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, + &ctx->samplesperpixel); + TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, + &ctx->planarconfiguration); + TIFFGetField(tif, TIFFTAG_COMPRESSION, &ctx->compression); + TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES, + &ctx->extrasamples, &sampleinfo); + ctx->alpha = (ctx->extrasamples == 1 && + sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA); + if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &ctx->photometric)) { + switch (ctx->samplesperpixel - ctx->extrasamples) { + case 1: + if (isCCITTCompression(tif)) + ctx->photometric = PHOTOMETRIC_MINISWHITE; + else + ctx->photometric = PHOTOMETRIC_MINISBLACK; + break; + case 3: + ctx->photometric = PHOTOMETRIC_RGB; + break; + case 4: + ctx->photometric = PHOTOMETRIC_SEPARATED; + break; } - if (checkImage(tif)) { - tf_bytesperrow = TIFFScanlineSize(tif); - *npages = *npages + 1; - fprintf(fd, "%%%%Page: %d %d\n", *npages, *npages); - if (!generateEPSF && ( level2 || level3 )) { - double psw = 0.0, psh = 0.0; - if (psw != 0.0) { - psw = pw * PS_UNIT_SIZE; - if (res_unit == RESUNIT_CENTIMETER) - psw *= 2.54F; - } else - psw=rotate ? prh:prw; - if (psh != 0.0) { - psh = ph * PS_UNIT_SIZE; - if (res_unit == RESUNIT_CENTIMETER) - psh *= 2.54F; - } else - psh=rotate ? prw:prh; - fprintf(fd, - "1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n", - psw, psh); - fputs( - "<<\n /Policies <<\n /PageSize 3\n >>\n>> setpagedevice\n", - fd); - } - fprintf(fd, "gsave\n"); - fprintf(fd, "100 dict begin\n"); - if (pw != 0 || ph != 0) { - if (!pw) - pw = prw; - if (!ph) - ph = prh; - if (maxPageHeight) { /* used -H option */ - split = PlaceImage(fd,pw,ph,prw,prh, - 0,lm,bm,cnt); - while( split ) { - PSpage(fd, tif, w, h); - fprintf(fd, "end\n"); - fprintf(fd, "grestore\n"); - fprintf(fd, "showpage\n"); - *npages = *npages + 1; - fprintf(fd, "%%%%Page: %d %d\n", - *npages, *npages); - fprintf(fd, "gsave\n"); - fprintf(fd, "100 dict begin\n"); - split = PlaceImage(fd,pw,ph,prw,prh, - split,lm,bm,cnt); - } - } else { - pw *= PS_UNIT_SIZE; - ph *= PS_UNIT_SIZE; - - /* NB: maintain image aspect ratio */ - scale = pw/prw < ph/prh ? - pw/prw : ph/prh; - if (scale > 1.0) - scale = 1.0; - if (cnt) { - bottom_offset += - (ph - prh * scale) / 2; - left_offset += - (pw - prw * scale) / 2; - } - fprintf(fd, "%f %f translate\n", - left_offset, bottom_offset); - fprintf(fd, "%f %f scale\n", - prw * scale, prh * scale); - if (rotate) - fputs ("1 1 translate 180 rotate\n", fd); + } + if (checkImage(ctx, tif)) { + ctx->tf_bytesperrow = TIFFScanlineSize(tif); + ctx->npages++; + fprintf(ctx->fd, "%%%%Page: %d %d\n", ctx->npages, + ctx->npages); + if (!ctx->generateEPSF && ( ctx->level2 || ctx->level3 )) { + double psw = 0.0, psh = 0.0; + if (psw != 0.0) { + psw = pw * PS_UNIT_SIZE; + if (ctx->res_unit == RESUNIT_CENTIMETER) + psw *= 2.54F; + } else + psw=ctx->rotate ? prh:prw; + if (psh != 0.0) { + psh = ph * PS_UNIT_SIZE; + if (ctx->res_unit == RESUNIT_CENTIMETER) + psh *= 2.54F; + } else + psh=ctx->rotate ? prw:prh; + fprintf(ctx->fd, + "1 dict begin /PageSize [ %f %f ] def currentdict end setpagedevice\n", + psw, psh); + fputs( + "<<\n /Policies <<\n /PageSize 3\n >>\n>> setpagedevice\n", + ctx->fd); + } + fprintf(ctx->fd, "gsave\n"); + fprintf(ctx->fd, "100 dict begin\n"); + if (pw != 0 || ph != 0) { + if (!pw) + pw = prw; + if (!ph) + ph = prh; + if (ctx->maxPageHeight) { /* used -H option */ + split = PlaceImage(ctx,pw,ph,prw,prh, + 0,lm,bm,cnt); + while( split ) { + PSpage(ctx, tif, w, h); + fprintf(ctx->fd, "end\n"); + fprintf(ctx->fd, "grestore\n"); + fprintf(ctx->fd, "showpage\n"); + ctx->npages++; + fprintf(ctx->fd, "%%%%Page: %d %d\n", + ctx->npages, ctx->npages); + fprintf(ctx->fd, "gsave\n"); + fprintf(ctx->fd, "100 dict begin\n"); + split = PlaceImage(ctx,pw,ph,prw,prh, + split,lm,bm,cnt); } } else { - fprintf(fd, "%f %f scale\n", prw, prh); - if (rotate) - fputs ("1 1 translate 180 rotate\n", fd); + pw *= PS_UNIT_SIZE; + ph *= PS_UNIT_SIZE; + + /* NB: maintain image aspect ratio */ + scale = pw/prw < ph/prh ? + pw/prw : ph/prh; + if (scale > 1.0) + scale = 1.0; + if (cnt) { + bottom_offset += + (ph - prh * scale) / 2; + left_offset += + (pw - prw * scale) / 2; + } + fprintf(ctx->fd, "%f %f translate\n", + left_offset, bottom_offset); + fprintf(ctx->fd, "%f %f scale\n", + prw * scale, prh * scale); + if (ctx->rotate) + fputs ("1 1 translate 180 ctx->rotate\n", ctx->fd); } - PSpage(fd, tif, w, h); - fprintf(fd, "end\n"); - fprintf(fd, "grestore\n"); - fprintf(fd, "showpage\n"); + } else { + fprintf(ctx->fd, "%f %f scale\n", prw, prh); + if (ctx->rotate) + fputs ("1 1 translate 180 ctx->rotate\n", ctx->fd); } - if (generateEPSF) - break; - TIFFGetFieldDefaulted(tif, TIFFTAG_SUBFILETYPE, &subfiletype); - } while (((subfiletype & FILETYPE_PAGE) || printAll) && - TIFFReadDirectory(tif)); + PSpage(ctx, tif, w, h); + fprintf(ctx->fd, "end\n"); + fprintf(ctx->fd, "grestore\n"); + fprintf(ctx->fd, "showpage\n"); + } } @@ -577,61 +606,66 @@ gsave newpath clippath pathbbox grestore\n\ "; void -PSHead(FILE *fd, TIFF *tif, uint32 w, uint32 h, +PSHead(TIFF2PSContext *ctx, TIFF *tif, uint32 w, uint32 h, double pw, double ph, double ox, double oy) { time_t t; (void) tif; (void) w; (void) h; t = time(0); - fprintf(fd, "%%!PS-Adobe-3.0%s\n", generateEPSF ? " EPSF-3.0" : ""); - fprintf(fd, "%%%%Creator: Evince\n"); - fprintf(fd, "%%%%CreationDate: %s", ctime(&t)); - fprintf(fd, "%%%%DocumentData: Clean7Bit\n"); - fprintf(fd, "%%%%Origin: %ld %ld\n", (long) ox, (long) oy); + fprintf(ctx->fd, "%%!PS-Adobe-3.0%s\n", + ctx->generateEPSF ? " EPSF-3.0" : ""); + fprintf(ctx->fd, "%%%%Creator: Evince\n"); + fprintf(ctx->fd, "%%%%CreationDate: %s", ctime(&t)); + fprintf(ctx->fd, "%%%%DocumentData: Clean7Bit\n"); + fprintf(ctx->fd, "%%%%Origin: %ld %ld\n", (long) ox, (long) oy); /* NB: should use PageBoundingBox */ - fprintf(fd, "%%%%BoundingBox: 0 0 %ld %ld\n", - (long) ceil(pw), (long) ceil(ph)); - fprintf(fd, "%%%%LanguageLevel: %d\n", (level3 ? 3 : (level2 ? 2 : 1))); - fprintf(fd, "%%%%Pages: (atend)\n"); - fprintf(fd, "%%%%EndComments\n"); - fprintf(fd, "%%%%BeginSetup\n"); - if (PSduplex) - fprintf(fd, "%s", DuplexPreamble); - if (PStumble) - fprintf(fd, "%s", TumblePreamble); - if (PSavoiddeadzone && (level2 || level3)) - fprintf(fd, "%s", AvoidDeadZonePreamble); - fprintf(fd, "%%%%EndSetup\n"); + fprintf(ctx->fd, "%%%%BoundingBox: 0 0 %ld %ld\n", + (long) ceil(pw), (long) ceil(ph)); + fprintf(ctx->fd, "%%%%LanguageLevel: %d\n", + (ctx->level3 ? 3 : (ctx->level2 ? 2 : 1))); + fprintf(ctx->fd, "%%%%Pages: (atend)\n"); + fprintf(ctx->fd, "%%%%EndComments\n"); + fprintf(ctx->fd, "%%%%BeginSetup\n"); + if (ctx->PSduplex) + fprintf(ctx->fd, "%s", DuplexPreamble); + if (ctx->PStumble) + fprintf(ctx->fd, "%s", TumblePreamble); + if (ctx->PSavoiddeadzone && (ctx->level2 || ctx->level3)) + fprintf(ctx->fd, "%s", AvoidDeadZonePreamble); + fprintf(ctx->fd, "%%%%EndSetup\n"); } -void -TIFFPSTail(FILE *fd, int npages) +static void +PSTail(TIFF2PSContext *ctx) { - fprintf(fd, "%%%%Trailer\n"); - fprintf(fd, "%%%%Pages: %d\n", npages); - fprintf(fd, "%%%%EOF\n"); + if (!ctx->npages) + return; + fprintf(ctx->fd, "%%%%Trailer\n"); + fprintf(ctx->fd, "%%%%Pages: %d\n", ctx->npages); + fprintf(ctx->fd, "%%%%EOF\n"); } static int -checkcmap(TIFF* tif, int n, uint16* r, uint16* g, uint16* b) +checkcmap(TIFF2PSContext* ctx, TIFF* tif, int n, + uint16* r, uint16* g, uint16* b) { (void) tif; while (n-- > 0) if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256) return (16); - TIFFWarning(filename, "Assuming 8-bit colormap"); + TIFFWarning(ctx->filename, "Assuming 8-bit colormap"); return (8); } static void -PS_Lvl2colorspace(FILE* fd, TIFF* tif) +PS_Lvl2colorspace(TIFF2PSContext* ctx, TIFF* tif) { uint16 *rmap, *gmap, *bmap; int i, num_colors; const char * colorspace_p; - switch ( photometric ) + switch ( ctx->photometric ) { case PHOTOMETRIC_SEPARATED: colorspace_p = "CMYK"; @@ -649,25 +683,25 @@ PS_Lvl2colorspace(FILE* fd, TIFF* tif) * Set up PostScript Level 2 colorspace according to * section 4.8 in the PostScript refenence manual. */ - fputs("% PostScript Level 2 only.\n", fd); - if (photometric != PHOTOMETRIC_PALETTE) { - if (photometric == PHOTOMETRIC_YCBCR) { + fputs("% PostScript Level 2 only.\n", ctx->fd); + if (ctx->photometric != PHOTOMETRIC_PALETTE) { + if (ctx->photometric == PHOTOMETRIC_YCBCR) { /* MORE CODE HERE */ } - fprintf(fd, "/Device%s setcolorspace\n", colorspace_p ); + fprintf(ctx->fd, "/Device%s setcolorspace\n", colorspace_p ); return; } /* * Set up an indexed/palette colorspace */ - num_colors = (1 << bitspersample); + num_colors = (1 << ctx->bitspersample); if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) { - TIFFError(filename, + TIFFError(ctx->filename, "Palette image w/o \"Colormap\" tag"); return; } - if (checkcmap(tif, num_colors, rmap, gmap, bmap) == 16) { + if (checkcmap(ctx, tif, num_colors, rmap, gmap, bmap) == 16) { /* * Convert colormap to 8-bits values. */ @@ -679,33 +713,33 @@ PS_Lvl2colorspace(FILE* fd, TIFF* tif) } #undef CVT } - fprintf(fd, "[ /Indexed /DeviceRGB %d", num_colors - 1); - if (ascii85) { - Ascii85Init(); - fputs("\n<~", fd); - ascii85breaklen -= 2; + fprintf(ctx->fd, "[ /Indexed /DeviceRGB %d", num_colors - 1); + if (ctx->ascii85) { + Ascii85Init(ctx); + fputs("\n<~", ctx->fd); + ctx->ascii85breaklen -= 2; } else - fputs(" <", fd); + fputs(" <", ctx->fd); for (i = 0; i < num_colors; i++) { - if (ascii85) { - Ascii85Put((unsigned char)rmap[i], fd); - Ascii85Put((unsigned char)gmap[i], fd); - Ascii85Put((unsigned char)bmap[i], fd); + if (ctx->ascii85) { + Ascii85Put(ctx, (unsigned char)rmap[i]); + Ascii85Put(ctx, (unsigned char)gmap[i]); + Ascii85Put(ctx, (unsigned char)bmap[i]); } else { - fputs((i % 8) ? " " : "\n ", fd); - fprintf(fd, "%02x%02x%02x", + fputs((i % 8) ? " " : "\n ", ctx->fd); + fprintf(ctx->fd, "%02x%02x%02x", rmap[i], gmap[i], bmap[i]); } } - if (ascii85) - Ascii85Flush(fd); + if (ctx->ascii85) + Ascii85Flush(ctx); else - fputs(">\n", fd); - fputs("] setcolorspace\n", fd); + fputs(">\n", ctx->fd); + fputs("] setcolorspace\n", ctx->fd); } static int -PS_Lvl2ImageDict(FILE* fd, TIFF* tif, uint32 w, uint32 h) +PS_Lvl2ImageDict(TIFF2PSContext* ctx, TIFF* tif, uint32 w, uint32 h) { int use_rawdata; uint32 tile_width, tile_height; @@ -714,7 +748,7 @@ PS_Lvl2ImageDict(FILE* fd, TIFF* tif, uint32 w, uint32 h) char im_h[64], im_x[64], im_y[64]; char * imageOp = "image"; - if ( useImagemask && (bitspersample == 1) ) + if ( ctx->useImagemask && (ctx->bitspersample == 1) ) imageOp = "imagemask"; (void)strcpy(im_x, "0"); @@ -732,24 +766,24 @@ PS_Lvl2ImageDict(FILE* fd, TIFF* tif, uint32 w, uint32 h) * The tiles does not fit image width and height. * Set up a clip rectangle for the image unit square. */ - fputs("0 0 1 1 rectclip\n", fd); + fputs("0 0 1 1 rectclip\n", ctx->fd); } if (tile_width < w) { - fputs("/im_x 0 def\n", fd); + fputs("/im_x 0 def\n", ctx->fd); (void)strcpy(im_x, "im_x neg"); } if (tile_height < h) { - fputs("/im_y 0 def\n", fd); + fputs("/im_y 0 def\n", ctx->fd); (void)sprintf(im_y, "%lu im_y sub", (unsigned long) h); } } else { - repeat_count = tf_numberstrips; - tile_height = tf_rowsperstrip; + repeat_count = ctx->tf_numberstrips; + tile_height = ctx->tf_rowsperstrip; if (tile_height > h) tile_height = h; if (repeat_count > 1) { - fputs("/im_y 0 def\n", fd); - fprintf(fd, "/im_h %lu def\n", + fputs("/im_y 0 def\n", ctx->fd); + fprintf(ctx->fd, "/im_h %lu def\n", (unsigned long) tile_height); (void)strcpy(im_h, "im_h"); (void)sprintf(im_y, "%lu im_y sub", (unsigned long) h); @@ -759,20 +793,20 @@ PS_Lvl2ImageDict(FILE* fd, TIFF* tif, uint32 w, uint32 h) /* * Output start of exec block */ - fputs("{ % exec\n", fd); + fputs("{ % exec\n", ctx->fd); if (repeat_count > 1) - fprintf(fd, "%d { %% repeat\n", repeat_count); + fprintf(ctx->fd, "%d { %% repeat\n", repeat_count); /* * Output filter options and image dictionary. */ - if (ascii85) + if (ctx->ascii85) fputs(" /im_stream currentfile /ASCII85Decode filter def\n", - fd); - fputs(" <<\n", fd); - fputs(" /ImageType 1\n", fd); - fprintf(fd, " /Width %lu\n", (unsigned long) tile_width); + ctx->fd); + fputs(" <<\n", ctx->fd); + fputs(" /ImageType 1\n", ctx->fd); + fprintf(ctx->fd, " /Width %lu\n", (unsigned long) tile_width); /* * Workaround for some software that may crash when last strip * of image contains fewer number of scanlines than specified @@ -783,26 +817,26 @@ PS_Lvl2ImageDict(FILE* fd, TIFF* tif, uint32 w, uint32 h) * contain number of scanlines in tile (or image height in case of * one-stripped image). */ - if (TIFFIsTiled(tif) || tf_numberstrips == 1) - fprintf(fd, " /Height %lu\n", (unsigned long) tile_height); + if (TIFFIsTiled(tif) || ctx->tf_numberstrips == 1) + fprintf(ctx->fd, " /Height %lu\n", (unsigned long) tile_height); else - fprintf(fd, " /Height im_h\n"); + fprintf(ctx->fd, " /Height im_h\n"); - if (planarconfiguration == PLANARCONFIG_SEPARATE && samplesperpixel > 1) - fputs(" /MultipleDataSources true\n", fd); - fprintf(fd, " /ImageMatrix [ %lu 0 0 %ld %s %s ]\n", + if (ctx->planarconfiguration == PLANARCONFIG_SEPARATE && ctx->samplesperpixel > 1) + fputs(" /MultipleDataSources true\n", ctx->fd); + fprintf(ctx->fd, " /ImageMatrix [ %lu 0 0 %ld %s %s ]\n", (unsigned long) w, - (long)h, im_x, im_y); - fprintf(fd, " /BitsPerComponent %d\n", bitspersample); - fprintf(fd, " /Interpolate %s\n", interpolate ? "true" : "false"); + fprintf(ctx->fd, " /BitsPerComponent %d\n", ctx->bitspersample); + fprintf(ctx->fd, " /Ctx->Interpolate %s\n", ctx->interpolate ? "true" : "false"); - switch (samplesperpixel - extrasamples) { + switch (ctx->samplesperpixel - ctx->extrasamples) { case 1: - switch (photometric) { + switch (ctx->photometric) { case PHOTOMETRIC_MINISBLACK: - fputs(" /Decode [0 1]\n", fd); + fputs(" /Decode [0 1]\n", ctx->fd); break; case PHOTOMETRIC_MINISWHITE: - switch (compression) { + switch (ctx->compression) { case COMPRESSION_CCITTRLE: case COMPRESSION_CCITTRLEW: case COMPRESSION_CCITTFAX3: @@ -811,13 +845,13 @@ PS_Lvl2ImageDict(FILE* fd, TIFF* tif, uint32 w, uint32 h) * Manage inverting with /Blackis1 flag * since there migth be uncompressed parts */ - fputs(" /Decode [0 1]\n", fd); + fputs(" /Decode [0 1]\n", ctx->fd); break; default: /* * ERROR... */ - fputs(" /Decode [1 0]\n", fd); + fputs(" /Decode [1 0]\n", ctx->fd); break; } break; @@ -826,21 +860,21 @@ PS_Lvl2ImageDict(FILE* fd, TIFF* tif, uint32 w, uint32 h) &minsamplevalue); TIFFGetFieldDefaulted(tif, TIFFTAG_MAXSAMPLEVALUE, &maxsamplevalue); - fprintf(fd, " /Decode [%u %u]\n", + fprintf(ctx->fd, " /Decode [%u %u]\n", minsamplevalue, maxsamplevalue); break; default: /* * ERROR ? */ - fputs(" /Decode [0 1]\n", fd); + fputs(" /Decode [0 1]\n", ctx->fd); break; } break; case 3: - switch (photometric) { + switch (ctx->photometric) { case PHOTOMETRIC_RGB: - fputs(" /Decode [0 1 0 1 0 1]\n", fd); + fputs(" /Decode [0 1 0 1 0 1]\n", ctx->fd); break; case PHOTOMETRIC_MINISWHITE: case PHOTOMETRIC_MINISBLACK: @@ -848,7 +882,7 @@ PS_Lvl2ImageDict(FILE* fd, TIFF* tif, uint32 w, uint32 h) /* * ERROR?? */ - fputs(" /Decode [0 1 0 1 0 1]\n", fd); + fputs(" /Decode [0 1 0 1 0 1]\n", ctx->fd); break; } break; @@ -856,109 +890,109 @@ PS_Lvl2ImageDict(FILE* fd, TIFF* tif, uint32 w, uint32 h) /* * ERROR?? */ - fputs(" /Decode [0 1 0 1 0 1 0 1]\n", fd); + fputs(" /Decode [0 1 0 1 0 1 0 1]\n", ctx->fd); break; } - fputs(" /DataSource", fd); - if (planarconfiguration == PLANARCONFIG_SEPARATE && - samplesperpixel > 1) - fputs(" [", fd); - if (ascii85) - fputs(" im_stream", fd); + fputs(" /DataSource", ctx->fd); + if (ctx->planarconfiguration == PLANARCONFIG_SEPARATE && + ctx->samplesperpixel > 1) + fputs(" [", ctx->fd); + if (ctx->ascii85) + fputs(" im_stream", ctx->fd); else - fputs(" currentfile /ASCIIHexDecode filter", fd); + fputs(" currentfile /ASCIIHexDecode filter", ctx->fd); use_rawdata = TRUE; - switch (compression) { + switch (ctx->compression) { case COMPRESSION_NONE: /* 1: uncompressed */ break; case COMPRESSION_CCITTRLE: /* 2: CCITT modified Huffman RLE */ case COMPRESSION_CCITTRLEW: /* 32771: #1 w/ word alignment */ case COMPRESSION_CCITTFAX3: /* 3: CCITT Group 3 fax encoding */ case COMPRESSION_CCITTFAX4: /* 4: CCITT Group 4 fax encoding */ - fputs("\n\t<<\n", fd); - if (compression == COMPRESSION_CCITTFAX3) { + fputs("\n\t<<\n", ctx->fd); + if (ctx->compression == COMPRESSION_CCITTFAX3) { uint32 g3_options; - fputs("\t /EndOfLine true\n", fd); - fputs("\t /EndOfBlock false\n", fd); + fputs("\t /EndOfLine true\n", ctx->fd); + fputs("\t /EndOfBlock false\n", ctx->fd); if (!TIFFGetField(tif, TIFFTAG_GROUP3OPTIONS, &g3_options)) g3_options = 0; if (g3_options & GROUP3OPT_2DENCODING) - fprintf(fd, "\t /K %s\n", im_h); + fprintf(ctx->fd, "\t /K %s\n", im_h); if (g3_options & GROUP3OPT_UNCOMPRESSED) - fputs("\t /Uncompressed true\n", fd); + fputs("\t /Uncompressed true\n", ctx->fd); if (g3_options & GROUP3OPT_FILLBITS) - fputs("\t /EncodedByteAlign true\n", fd); + fputs("\t /EncodedByteAlign true\n", ctx->fd); } - if (compression == COMPRESSION_CCITTFAX4) { + if (ctx->compression == COMPRESSION_CCITTFAX4) { uint32 g4_options; - fputs("\t /K -1\n", fd); + fputs("\t /K -1\n", ctx->fd); TIFFGetFieldDefaulted(tif, TIFFTAG_GROUP4OPTIONS, &g4_options); if (g4_options & GROUP4OPT_UNCOMPRESSED) - fputs("\t /Uncompressed true\n", fd); + fputs("\t /Uncompressed true\n", ctx->fd); } if (!(tile_width == w && w == 1728U)) - fprintf(fd, "\t /Columns %lu\n", + fprintf(ctx->fd, "\t /Columns %lu\n", (unsigned long) tile_width); - fprintf(fd, "\t /Rows %s\n", im_h); - if (compression == COMPRESSION_CCITTRLE || - compression == COMPRESSION_CCITTRLEW) { - fputs("\t /EncodedByteAlign true\n", fd); - fputs("\t /EndOfBlock false\n", fd); + fprintf(ctx->fd, "\t /Rows %s\n", im_h); + if (ctx->compression == COMPRESSION_CCITTRLE || + ctx->compression == COMPRESSION_CCITTRLEW) { + fputs("\t /EncodedByteAlign true\n", ctx->fd); + fputs("\t /EndOfBlock false\n", ctx->fd); } - if (photometric == PHOTOMETRIC_MINISBLACK) - fputs("\t /BlackIs1 true\n", fd); - fprintf(fd, "\t>> /CCITTFaxDecode filter"); + if (ctx->photometric == PHOTOMETRIC_MINISBLACK) + fputs("\t /BlackIs1 true\n", ctx->fd); + fprintf(ctx->fd, "\t>> /CCITTFaxDecode filter"); break; case COMPRESSION_LZW: /* 5: Lempel-Ziv & Welch */ TIFFGetFieldDefaulted(tif, TIFFTAG_PREDICTOR, &predictor); if (predictor == 2) { - fputs("\n\t<<\n", fd); - fprintf(fd, "\t /Predictor %u\n", predictor); - fprintf(fd, "\t /Columns %lu\n", + fputs("\n\t<<\n", ctx->fd); + fprintf(ctx->fd, "\t /Predictor %u\n", predictor); + fprintf(ctx->fd, "\t /Columns %lu\n", (unsigned long) tile_width); - fprintf(fd, "\t /Colors %u\n", samplesperpixel); - fprintf(fd, "\t /BitsPerComponent %u\n", - bitspersample); - fputs("\t>>", fd); + fprintf(ctx->fd, "\t /Colors %u\n", ctx->samplesperpixel); + fprintf(ctx->fd, "\t /BitsPerComponent %u\n", + ctx->bitspersample); + fputs("\t>>", ctx->fd); } - fputs(" /LZWDecode filter", fd); + fputs(" /LZWDecode filter", ctx->fd); break; case COMPRESSION_DEFLATE: /* 5: ZIP */ case COMPRESSION_ADOBE_DEFLATE: - if ( level3 ) { + if ( ctx->level3 ) { TIFFGetFieldDefaulted(tif, TIFFTAG_PREDICTOR, &predictor); if (predictor > 1) { - fprintf(fd, "\t %% PostScript Level 3 only."); - fputs("\n\t<<\n", fd); - fprintf(fd, "\t /Predictor %u\n", predictor); - fprintf(fd, "\t /Columns %lu\n", + fprintf(ctx->fd, "\t %% PostScript Level 3 only."); + fputs("\n\t<<\n", ctx->fd); + fprintf(ctx->fd, "\t /Predictor %u\n", predictor); + fprintf(ctx->fd, "\t /Columns %lu\n", (unsigned long) tile_width); - fprintf(fd, "\t /Colors %u\n", samplesperpixel); - fprintf(fd, "\t /BitsPerComponent %u\n", - bitspersample); - fputs("\t>>", fd); + fprintf(ctx->fd, "\t /Colors %u\n", ctx->samplesperpixel); + fprintf(ctx->fd, "\t /BitsPerComponent %u\n", + ctx->bitspersample); + fputs("\t>>", ctx->fd); } - fputs(" /FlateDecode filter", fd); + fputs(" /FlateDecode filter", ctx->fd); } else { use_rawdata = FALSE ; } break; case COMPRESSION_PACKBITS: /* 32773: Macintosh RLE */ - fputs(" /RunLengthDecode filter", fd); + fputs(" /RunLengthDecode filter", ctx->fd); use_rawdata = TRUE; break; case COMPRESSION_OJPEG: /* 6: !6.0 JPEG */ - case COMPRESSION_JPEG: /* 7: %JPEG DCT compression */ + case COMPRESSION_JPEG: /* 7: %JPEG DCT ctx->compression */ #ifdef notdef /* * Code not tested yet */ - fputs(" /DCTDecode filter", fd); + fputs(" /DCTDecode filter", ctx->fd); use_rawdata = TRUE; #else use_rawdata = FALSE; @@ -981,54 +1015,54 @@ PS_Lvl2ImageDict(FILE* fd, TIFF* tif, uint32 w, uint32 h) use_rawdata = FALSE; break; } - if (planarconfiguration == PLANARCONFIG_SEPARATE && - samplesperpixel > 1) { + if (ctx->planarconfiguration == PLANARCONFIG_SEPARATE && + ctx->samplesperpixel > 1) { uint16 i; /* * NOTE: This code does not work yet... */ - for (i = 1; i < samplesperpixel; i++) - fputs(" dup", fd); - fputs(" ]", fd); + for (i = 1; i < ctx->samplesperpixel; i++) + fputs(" dup", ctx->fd); + fputs(" ]", ctx->fd); } - fprintf( fd, "\n >> %s\n", imageOp ); - if (ascii85) - fputs(" im_stream status { im_stream flushfile } if\n", fd); + fprintf( ctx->fd, "\n >> %s\n", imageOp ); + if (ctx->ascii85) + fputs(" im_stream status { im_stream flushfile } if\n", ctx->fd); if (repeat_count > 1) { if (tile_width < w) { - fprintf(fd, " /im_x im_x %lu add def\n", + fprintf(ctx->fd, " /im_x im_x %lu add def\n", (unsigned long) tile_width); if (tile_height < h) { - fprintf(fd, " im_x %lu ge {\n", + fprintf(ctx->fd, " im_x %lu ge {\n", (unsigned long) w); - fputs(" /im_x 0 def\n", fd); - fprintf(fd, " /im_y im_y %lu add def\n", + fputs(" /im_x 0 def\n", ctx->fd); + fprintf(ctx->fd, " /im_y im_y %lu add def\n", (unsigned long) tile_height); - fputs(" } if\n", fd); + fputs(" } if\n", ctx->fd); } } if (tile_height < h) { if (tile_width >= w) { - fprintf(fd, " /im_y im_y %lu add def\n", + fprintf(ctx->fd, " /im_y im_y %lu add def\n", (unsigned long) tile_height); if (!TIFFIsTiled(tif)) { - fprintf(fd, " /im_h %lu im_y sub", + fprintf(ctx->fd, " /im_h %lu im_y sub", (unsigned long) h); - fprintf(fd, " dup %lu gt { pop", + fprintf(ctx->fd, " dup %lu gt { pop", (unsigned long) tile_height); - fprintf(fd, " %lu } if def\n", + fprintf(ctx->fd, " %lu } if def\n", (unsigned long) tile_height); } } } - fputs("} repeat\n", fd); + fputs("} repeat\n", ctx->fd); } /* * End of exec function */ - fputs("}\n", fd); + fputs("}\n", ctx->fd); return(use_rawdata); } @@ -1036,7 +1070,7 @@ PS_Lvl2ImageDict(FILE* fd, TIFF* tif, uint32 w, uint32 h) #define MAXLINE 36 static int -PS_Lvl2page(FILE* fd, TIFF* tif, uint32 w, uint32 h) +PS_Lvl2page(TIFF2PSContext* ctx, TIFF* tif, uint32 w, uint32 h) { uint16 fillorder; int use_rawdata, tiled_image, breaklen = MAXLINE; @@ -1049,14 +1083,14 @@ PS_Lvl2page(FILE* fd, TIFF* tif, uint32 w, uint32 h) uint8 * ascii85_p = 0; /* Holds ASCII85 encoded data */ #endif - PS_Lvl2colorspace(fd, tif); - use_rawdata = PS_Lvl2ImageDict(fd, tif, w, h); + PS_Lvl2colorspace(ctx, tif); + use_rawdata = PS_Lvl2ImageDict(ctx, tif, w, h); /* See http://bugzilla.remotesensing.org/show_bug.cgi?id=80 */ #ifdef ENABLE_BROKEN_BEGINENDDATA - fputs("%%BeginData:\n", fd); + fputs("%%BeginData:\n", ctx->fd); #endif - fputs("exec\n", fd); + fputs("exec\n", ctx->fd); tiled_image = TIFFIsTiled(tif); if (tiled_image) { @@ -1080,13 +1114,13 @@ PS_Lvl2page(FILE* fd, TIFF* tif, uint32 w, uint32 h) } buf_data = (unsigned char *)_TIFFmalloc(chunk_size); if (!buf_data) { - TIFFError(filename, "Can't alloc %u bytes for %s.", + TIFFError(ctx->filename, "Can't alloc %u bytes for %s.", chunk_size, tiled_image ? "tiles" : "strips"); return(FALSE); } #if defined( EXP_ASCII85ENCODER ) - if ( ascii85 ) { + if ( ctx->ascii85 ) { /* * Allocate a buffer to hold the ASCII85 encoded data. Note * that it is allocated with sufficient room to hold the @@ -1101,7 +1135,8 @@ PS_Lvl2page(FILE* fd, TIFF* tif, uint32 w, uint32 h) if ( !ascii85_p ) { _TIFFfree( buf_data ); - TIFFError( filename, "Cannot allocate ASCII85 encoding buffer." ); + TIFFError( ctx->filename, + "Cannot allocate ASCII85 encoding buffer." ); return ( FALSE ); } } @@ -1109,8 +1144,8 @@ PS_Lvl2page(FILE* fd, TIFF* tif, uint32 w, uint32 h) TIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &fillorder); for (chunk_no = 0; chunk_no < num_chunks; chunk_no++) { - if (ascii85) - Ascii85Init(); + if (ctx->ascii85) + Ascii85Init(ctx); else breaklen = MAXLINE; if (use_rawdata) { @@ -1133,22 +1168,22 @@ PS_Lvl2page(FILE* fd, TIFF* tif, uint32 w, uint32 h) chunk_size); } if (byte_count < 0) { - TIFFError(filename, "Can't read %s %d.", + TIFFError(ctx->filename, "Can't read %s %d.", tiled_image ? "tile" : "strip", chunk_no); - if (ascii85) - Ascii85Put('\0', fd); + if (ctx->ascii85) + Ascii85Put(ctx, '\0'); } /* - * For images with alpha, matte against a white background; + * For images with ctx->alpha, matte against a white background; * i.e. Cback * (1 - Aimage) where Cback = 1. We will fill the * lower part of the buffer with the modified values. * * XXX: needs better solution */ - if (alpha) { + if (ctx->alpha) { int adjust, i, j = 0; - int ncomps = samplesperpixel - extrasamples; - for (i = 0; i < byte_count; i+=samplesperpixel) { + int ncomps = ctx->samplesperpixel - ctx->extrasamples; + for (i = 0; i < byte_count; i+=ctx->samplesperpixel) { adjust = 255 - buf_data[i + ncomps]; switch (ncomps) { case 1: @@ -1168,39 +1203,40 @@ PS_Lvl2page(FILE* fd, TIFF* tif, uint32 w, uint32 h) byte_count -= j; } - if (ascii85) { + if (ctx->ascii85) { #if defined( EXP_ASCII85ENCODER ) - ascii85_l = Ascii85EncodeBlock(ascii85_p, 1, buf_data, byte_count ); + ascii85_l = Ascii85EncodeBlock(ctx, ascii85_p, 1, + buf_data, byte_count); if ( ascii85_l > 0 ) - fwrite( ascii85_p, ascii85_l, 1, fd ); + fwrite( ascii85_p, ascii85_l, 1, ctx->fd ); #else for (cp = buf_data; byte_count > 0; byte_count--) - Ascii85Put(*cp++, fd); + Ascii85Put(ctx, *cp++); #endif } else { for (cp = buf_data; byte_count > 0; byte_count--) { - putc(hex[((*cp)>>4)&0xf], fd); - putc(hex[(*cp)&0xf], fd); + putc(hex[((*cp)>>4)&0xf], ctx->fd); + putc(hex[(*cp)&0xf], ctx->fd); cp++; if (--breaklen <= 0) { - putc('\n', fd); + putc('\n', ctx->fd); breaklen = MAXLINE; } } } - if ( !ascii85 ) { - if ( level2 || level3 ) - putc( '>', fd ); - putc('\n', fd); + if ( !ctx->ascii85 ) { + if ( ctx->level2 || ctx->level3 ) + putc( '>', ctx->fd ); + putc('\n', ctx->fd); } #if !defined( EXP_ASCII85ENCODER ) else - Ascii85Flush(fd); + Ascii85Flush(ctx); #endif } @@ -1210,104 +1246,106 @@ PS_Lvl2page(FILE* fd, TIFF* tif, uint32 w, uint32 h) #endif _TIFFfree(buf_data); #ifdef ENABLE_BROKEN_BEGINENDDATA - fputs("%%EndData\n", fd); + fputs("%%EndData\n", ctx->fd); #endif return(TRUE); } void -PSpage(FILE* fd, TIFF* tif, uint32 w, uint32 h) +PSpage(TIFF2PSContext* ctx, TIFF* tif, uint32 w, uint32 h) { - char * imageOp = "image"; + char *imageOp = "image"; - if ( useImagemask && (bitspersample == 1) ) + if ( ctx->useImagemask && (ctx->bitspersample == 1) ) imageOp = "imagemask"; - if ((level2 || level3) && PS_Lvl2page(fd, tif, w, h)) + if ((ctx->level2 || ctx->level3) && PS_Lvl2page(ctx, tif, w, h)) return; - ps_bytesperrow = tf_bytesperrow - (extrasamples * bitspersample / 8)*w; - switch (photometric) { + ctx->ps_bytesperrow = ctx->tf_bytesperrow - (ctx->extrasamples * ctx->bitspersample / 8)*w; + switch (ctx->photometric) { case PHOTOMETRIC_RGB: - if (planarconfiguration == PLANARCONFIG_CONTIG) { - fprintf(fd, "%s", RGBcolorimage); - PSColorContigPreamble(fd, w, h, 3); - PSDataColorContig(fd, tif, w, h, 3); + if (ctx->planarconfiguration == PLANARCONFIG_CONTIG) { + fprintf(ctx->fd, "%s", RGBcolorimage); + PSColorContigPreamble(ctx, w, h, 3); + PSDataColorContig(ctx, tif, w, h, 3); } else { - PSColorSeparatePreamble(fd, w, h, 3); - PSDataColorSeparate(fd, tif, w, h, 3); + PSColorSeparatePreamble(ctx, w, h, 3); + PSDataColorSeparate(ctx, tif, w, h, 3); } break; case PHOTOMETRIC_SEPARATED: /* XXX should emit CMYKcolorimage */ - if (planarconfiguration == PLANARCONFIG_CONTIG) { - PSColorContigPreamble(fd, w, h, 4); - PSDataColorContig(fd, tif, w, h, 4); + if (ctx->planarconfiguration == PLANARCONFIG_CONTIG) { + PSColorContigPreamble(ctx, w, h, 4); + PSDataColorContig(ctx, tif, w, h, 4); } else { - PSColorSeparatePreamble(fd, w, h, 4); - PSDataColorSeparate(fd, tif, w, h, 4); + PSColorSeparatePreamble(ctx, w, h, 4); + PSDataColorSeparate(ctx, tif, w, h, 4); } break; case PHOTOMETRIC_PALETTE: - fprintf(fd, "%s", RGBcolorimage); - PhotoshopBanner(fd, w, h, 1, 3, "false 3 colorimage"); - fprintf(fd, "/scanLine %ld string def\n", - (long) ps_bytesperrow * 3L); - fprintf(fd, "%lu %lu 8\n", - (unsigned long) w, (unsigned long) h); - fprintf(fd, "[%lu 0 0 -%lu 0 %lu]\n", - (unsigned long) w, (unsigned long) h, (unsigned long) h); - fprintf(fd, "{currentfile scanLine readhexstring pop} bind\n"); - fprintf(fd, "false 3 colorimage\n"); - PSDataPalette(fd, tif, w, h); + fprintf(ctx->fd, "%s", RGBcolorimage); + PhotoshopBanner(ctx, w, h, 1, 3, "false 3 colorimage"); + fprintf(ctx->fd, "/scanLine %ld string def\n", + (long) ctx->ps_bytesperrow * 3L); + fprintf(ctx->fd, "%lu %lu 8\n", + (unsigned long) w, (unsigned long) h); + fprintf(ctx->fd, "[%lu 0 0 -%lu 0 %lu]\n", + (unsigned long) w, (unsigned long) h, + (unsigned long) h); + fprintf(ctx->fd, + "{currentfile scanLine readhexstring pop} bind\n"); + fprintf(ctx->fd, "false 3 colorimage\n"); + PSDataPalette(ctx, tif, w, h); break; case PHOTOMETRIC_MINISBLACK: case PHOTOMETRIC_MINISWHITE: - PhotoshopBanner(fd, w, h, 1, 1, imageOp); - fprintf(fd, "/scanLine %ld string def\n", - (long) ps_bytesperrow); - fprintf(fd, "%lu %lu %d\n", - (unsigned long) w, (unsigned long) h, bitspersample); - fprintf(fd, "[%lu 0 0 -%lu 0 %lu]\n", + PhotoshopBanner(ctx, w, h, 1, 1, imageOp); + fprintf(ctx->fd, "/scanLine %ld string def\n", + (long) ctx->ps_bytesperrow); + fprintf(ctx->fd, "%lu %lu %d\n", + (unsigned long) w, (unsigned long) h, ctx->bitspersample); + fprintf(ctx->fd, "[%lu 0 0 -%lu 0 %lu]\n", (unsigned long) w, (unsigned long) h, (unsigned long) h); - fprintf(fd, + fprintf(ctx->fd, "{currentfile scanLine readhexstring pop} bind\n"); - fprintf(fd, "%s\n", imageOp); - PSDataBW(fd, tif, w, h); + fprintf(ctx->fd, "%s\n", imageOp); + PSDataBW(ctx, tif, w, h); break; } - putc('\n', fd); + putc('\n', ctx->fd); } void -PSColorContigPreamble(FILE* fd, uint32 w, uint32 h, int nc) +PSColorContigPreamble(TIFF2PSContext* ctx, uint32 w, uint32 h, int nc) { - ps_bytesperrow = nc * (tf_bytesperrow / samplesperpixel); - PhotoshopBanner(fd, w, h, 1, nc, "false %d colorimage"); - fprintf(fd, "/line %ld string def\n", (long) ps_bytesperrow); - fprintf(fd, "%lu %lu %d\n", - (unsigned long) w, (unsigned long) h, bitspersample); - fprintf(fd, "[%lu 0 0 -%lu 0 %lu]\n", + ctx->ps_bytesperrow = nc * (ctx->tf_bytesperrow / ctx->samplesperpixel); + PhotoshopBanner(ctx, w, h, 1, nc, "false %d colorimage"); + fprintf(ctx->fd, "/line %ld string def\n", (long) ctx->ps_bytesperrow); + fprintf(ctx->fd, "%lu %lu %d\n", + (unsigned long) w, (unsigned long) h, ctx->bitspersample); + fprintf(ctx->fd, "[%lu 0 0 -%lu 0 %lu]\n", (unsigned long) w, (unsigned long) h, (unsigned long) h); - fprintf(fd, "{currentfile line readhexstring pop} bind\n"); - fprintf(fd, "false %d colorimage\n", nc); + fprintf(ctx->fd, "{currentfile line readhexstring pop} bind\n"); + fprintf(ctx->fd, "false %d colorimage\n", nc); } void -PSColorSeparatePreamble(FILE* fd, uint32 w, uint32 h, int nc) +PSColorSeparatePreamble(TIFF2PSContext* ctx, uint32 w, uint32 h, int nc) { int i; - PhotoshopBanner(fd, w, h, ps_bytesperrow, nc, "true %d colorimage"); + PhotoshopBanner(ctx, w, h, ctx->ps_bytesperrow, nc, "true %d colorimage"); for (i = 0; i < nc; i++) - fprintf(fd, "/line%d %ld string def\n", - i, (long) ps_bytesperrow); - fprintf(fd, "%lu %lu %d\n", - (unsigned long) w, (unsigned long) h, bitspersample); - fprintf(fd, "[%lu 0 0 -%lu 0 %lu] \n", + fprintf(ctx->fd, "/line%d %ld string def\n", + i, (long) ctx->ps_bytesperrow); + fprintf(ctx->fd, "%lu %lu %d\n", + (unsigned long) w, (unsigned long) h, ctx->bitspersample); + fprintf(ctx->fd, "[%lu 0 0 -%lu 0 %lu] \n", (unsigned long) w, (unsigned long) h, (unsigned long) h); for (i = 0; i < nc; i++) - fprintf(fd, "{currentfile line%d readhexstring pop}bind\n", i); - fprintf(fd, "true %d colorimage\n", nc); + fprintf(ctx->fd, "{currentfile line%d readhexstring pop}bind\n", i); + fprintf(ctx->fd, "true %d colorimage\n", nc); } #define DOBREAK(len, howmany, fd) \ @@ -1318,52 +1356,52 @@ PSColorSeparatePreamble(FILE* fd, uint32 w, uint32 h, int nc) #define PUTHEX(c,fd) putc(hex[((c)>>4)&0xf],fd); putc(hex[(c)&0xf],fd) void -PSDataColorContig(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc) +PSDataColorContig(TIFF2PSContext* ctx, TIFF* tif, uint32 w, uint32 h, int nc) { uint32 row; - int breaklen = MAXLINE, cc, es = samplesperpixel - nc; + int breaklen = MAXLINE, cc, es = ctx->samplesperpixel - nc; unsigned char *tf_buf; unsigned char *cp, c; (void) w; - tf_buf = (unsigned char *) _TIFFmalloc(tf_bytesperrow); + tf_buf = (unsigned char *) _TIFFmalloc(ctx->tf_bytesperrow); if (tf_buf == NULL) { - TIFFError(filename, "No space for scanline buffer"); + TIFFError(ctx->filename, "No space for scanline buffer"); return; } for (row = 0; row < h; row++) { if (TIFFReadScanline(tif, tf_buf, row, 0) < 0) break; cp = tf_buf; - if (alpha) { + if (ctx->alpha) { int adjust; cc = 0; - for (; cc < tf_bytesperrow; cc += samplesperpixel) { - DOBREAK(breaklen, nc, fd); + for (; cc < ctx->tf_bytesperrow; cc += ctx->samplesperpixel) { + DOBREAK(breaklen, nc, ctx->fd); /* - * For images with alpha, matte against + * For images with ctx->alpha, matte against * a white background; i.e. * Cback * (1 - Aimage) * where Cback = 1. */ adjust = 255 - cp[nc]; switch (nc) { - case 4: c = *cp++ + adjust; PUTHEX(c,fd); - case 3: c = *cp++ + adjust; PUTHEX(c,fd); - case 2: c = *cp++ + adjust; PUTHEX(c,fd); - case 1: c = *cp++ + adjust; PUTHEX(c,fd); + case 4: c = *cp++ + adjust; PUTHEX(c,ctx->fd); + case 3: c = *cp++ + adjust; PUTHEX(c,ctx->fd); + case 2: c = *cp++ + adjust; PUTHEX(c,ctx->fd); + case 1: c = *cp++ + adjust; PUTHEX(c,ctx->fd); } cp += es; } } else { cc = 0; - for (; cc < tf_bytesperrow; cc += samplesperpixel) { - DOBREAK(breaklen, nc, fd); + for (; cc < ctx->tf_bytesperrow; cc += ctx->samplesperpixel) { + DOBREAK(breaklen, nc, ctx->fd); switch (nc) { - case 4: c = *cp++; PUTHEX(c,fd); - case 3: c = *cp++; PUTHEX(c,fd); - case 2: c = *cp++; PUTHEX(c,fd); - case 1: c = *cp++; PUTHEX(c,fd); + case 4: c = *cp++; PUTHEX(c,ctx->fd); + case 3: c = *cp++; PUTHEX(c,ctx->fd); + case 2: c = *cp++; PUTHEX(c,ctx->fd); + case 1: c = *cp++; PUTHEX(c,ctx->fd); } cp += es; } @@ -1373,7 +1411,7 @@ PSDataColorContig(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc) } void -PSDataColorSeparate(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc) +PSDataColorSeparate(TIFF2PSContext* ctx, TIFF* tif, uint32 w, uint32 h, int nc) { uint32 row; int breaklen = MAXLINE, cc; @@ -1382,20 +1420,20 @@ PSDataColorSeparate(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc) unsigned char *cp, c; (void) w; - tf_buf = (unsigned char *) _TIFFmalloc(tf_bytesperrow); + tf_buf = (unsigned char *) _TIFFmalloc(ctx->tf_bytesperrow); if (tf_buf == NULL) { - TIFFError(filename, "No space for scanline buffer"); + TIFFError(ctx->filename, "No space for scanline buffer"); return; } - maxs = (samplesperpixel > nc ? nc : samplesperpixel); + maxs = (ctx->samplesperpixel > nc ? nc : ctx->samplesperpixel); for (row = 0; row < h; row++) { for (s = 0; s < maxs; s++) { if (TIFFReadScanline(tif, tf_buf, row, s) < 0) break; - for (cp = tf_buf, cc = 0; cc < tf_bytesperrow; cc++) { - DOBREAK(breaklen, 1, fd); + for (cp = tf_buf, cc = 0; cc < ctx->tf_bytesperrow; cc++) { + DOBREAK(breaklen, 1, ctx->fd); c = *cp++; - PUTHEX(c,fd); + PUTHEX(c,ctx->fd); } } } @@ -1406,7 +1444,7 @@ PSDataColorSeparate(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc) PUTHEX(rmap[c],fd); PUTHEX(gmap[c],fd); PUTHEX(bmap[c],fd) void -PSDataPalette(FILE* fd, TIFF* tif, uint32 w, uint32 h) +PSDataPalette(TIFF2PSContext* ctx, TIFF* tif, uint32 w, uint32 h) { uint16 *rmap, *gmap, *bmap; uint32 row; @@ -1416,26 +1454,26 @@ PSDataPalette(FILE* fd, TIFF* tif, uint32 w, uint32 h) (void) w; if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) { - TIFFError(filename, "Palette image w/o \"Colormap\" tag"); + TIFFError(ctx->filename, "Palette image w/o \"Colormap\" tag"); return; } - switch (bitspersample) { + switch (ctx->bitspersample) { case 8: case 4: case 2: case 1: break; default: - TIFFError(filename, "Depth %d not supported", bitspersample); + TIFFError(ctx->filename, "Depth %d not supported", ctx->bitspersample); return; } - nc = 3 * (8 / bitspersample); - tf_buf = (unsigned char *) _TIFFmalloc(tf_bytesperrow); + nc = 3 * (8 / ctx->bitspersample); + tf_buf = (unsigned char *) _TIFFmalloc(ctx->tf_bytesperrow); if (tf_buf == NULL) { - TIFFError(filename, "No space for scanline buffer"); + TIFFError(ctx->filename, "No space for scanline buffer"); return; } - if (checkcmap(tif, 1<bitspersample, rmap, gmap, bmap) == 16) { int i; #define CVT(x) ((unsigned short) (((x) * 255) / ((1U<<16)-1))) - for (i = (1<= 0; i--) { + for (i = (1<bitspersample)-1; i >= 0; i--) { rmap[i] = CVT(rmap[i]); gmap[i] = CVT(gmap[i]); bmap[i] = CVT(bmap[i]); @@ -1445,31 +1483,31 @@ PSDataPalette(FILE* fd, TIFF* tif, uint32 w, uint32 h) for (row = 0; row < h; row++) { if (TIFFReadScanline(tif, tf_buf, row, 0) < 0) break; - for (cp = tf_buf, cc = 0; cc < tf_bytesperrow; cc++) { - DOBREAK(breaklen, nc, fd); - switch (bitspersample) { + for (cp = tf_buf, cc = 0; cc < ctx->tf_bytesperrow; cc++) { + DOBREAK(breaklen, nc, ctx->fd); + switch (ctx->bitspersample) { case 8: - c = *cp++; PUTRGBHEX(c, fd); + c = *cp++; PUTRGBHEX(c, ctx->fd); break; case 4: - c = *cp++; PUTRGBHEX(c&0xf, fd); - c >>= 4; PUTRGBHEX(c, fd); + c = *cp++; PUTRGBHEX(c&0xf, ctx->fd); + c >>= 4; PUTRGBHEX(c, ctx->fd); break; case 2: - c = *cp++; PUTRGBHEX(c&0x3, fd); - c >>= 2; PUTRGBHEX(c&0x3, fd); - c >>= 2; PUTRGBHEX(c&0x3, fd); - c >>= 2; PUTRGBHEX(c, fd); + c = *cp++; PUTRGBHEX(c&0x3, ctx->fd); + c >>= 2; PUTRGBHEX(c&0x3, ctx->fd); + c >>= 2; PUTRGBHEX(c&0x3, ctx->fd); + c >>= 2; PUTRGBHEX(c, ctx->fd); break; case 1: - c = *cp++; PUTRGBHEX(c&0x1, fd); - c >>= 1; PUTRGBHEX(c&0x1, fd); - c >>= 1; PUTRGBHEX(c&0x1, fd); - c >>= 1; PUTRGBHEX(c&0x1, fd); - c >>= 1; PUTRGBHEX(c&0x1, fd); - c >>= 1; PUTRGBHEX(c&0x1, fd); - c >>= 1; PUTRGBHEX(c&0x1, fd); - c >>= 1; PUTRGBHEX(c, fd); + c = *cp++; PUTRGBHEX(c&0x1, ctx->fd); + c >>= 1; PUTRGBHEX(c&0x1, ctx->fd); + c >>= 1; PUTRGBHEX(c&0x1, ctx->fd); + c >>= 1; PUTRGBHEX(c&0x1, ctx->fd); + c >>= 1; PUTRGBHEX(c&0x1, ctx->fd); + c >>= 1; PUTRGBHEX(c&0x1, ctx->fd); + c >>= 1; PUTRGBHEX(c&0x1, ctx->fd); + c >>= 1; PUTRGBHEX(c, ctx->fd); break; } } @@ -1478,7 +1516,7 @@ PSDataPalette(FILE* fd, TIFF* tif, uint32 w, uint32 h) } void -PSDataBW(FILE* fd, TIFF* tif, uint32 w, uint32 h) +PSDataBW(TIFF2PSContext* ctx, TIFF* tif, uint32 w, uint32 h) { int breaklen = MAXLINE; unsigned char* tf_buf; @@ -1495,12 +1533,12 @@ PSDataBW(FILE* fd, TIFF* tif, uint32 w, uint32 h) tf_buf = (unsigned char *) _TIFFmalloc(stripsize); memset(tf_buf, 0, stripsize); if (tf_buf == NULL) { - TIFFError(filename, "No space for scanline buffer"); + TIFFError(ctx->filename, "No space for scanline buffer"); return; } #if defined( EXP_ASCII85ENCODER ) - if ( ascii85 ) { + if ( ctx->ascii85 ) { /* * Allocate a buffer to hold the ASCII85 encoded data. Note * that it is allocated with sufficient room to hold the @@ -1515,30 +1553,31 @@ PSDataBW(FILE* fd, TIFF* tif, uint32 w, uint32 h) if ( !ascii85_p ) { _TIFFfree( tf_buf ); - TIFFError( filename, "Cannot allocate ASCII85 encoding buffer." ); + TIFFError( ctx->filename, + "Cannot allocate ASCII85 encoding buffer." ); return; } } #endif - if (ascii85) - Ascii85Init(); + if (ctx->ascii85) + Ascii85Init(ctx); for (s = 0; s < TIFFNumberOfStrips(tif); s++) { int cc = TIFFReadEncodedStrip(tif, s, tf_buf, stripsize); if (cc < 0) { - TIFFError(filename, "Can't read strip"); + TIFFError(ctx->filename, "Can't read strip"); break; } cp = tf_buf; - if (photometric == PHOTOMETRIC_MINISWHITE) { + if (ctx->photometric == PHOTOMETRIC_MINISWHITE) { for (cp += cc; --cp >= tf_buf;) *cp = ~*cp; cp++; } - if (ascii85) { + if (ctx->ascii85) { #if defined( EXP_ASCII85ENCODER ) - if (alpha) { + if (ctx->alpha) { int adjust, i; for (i = 0; i < cc; i+=2) { adjust = 255 - cp[i + 1]; @@ -1547,49 +1586,50 @@ PSDataBW(FILE* fd, TIFF* tif, uint32 w, uint32 h) cc /= 2; } - ascii85_l = Ascii85EncodeBlock( ascii85_p, 1, cp, cc ); + ascii85_l = Ascii85EncodeBlock(ctx, ascii85_p, 1, cp, + cc); if ( ascii85_l > 0 ) - fwrite( ascii85_p, ascii85_l, 1, fd ); + fwrite( ascii85_p, ascii85_l, 1, ctx->fd ); #else while (cc-- > 0) - Ascii85Put(*cp++, fd); + Ascii85Put(ctx, *cp++); #endif /* EXP_ASCII85_ENCODER */ } else { unsigned char c; - if (alpha) { + if (ctx->alpha) { int adjust; while (cc-- > 0) { - DOBREAK(breaklen, 1, fd); + DOBREAK(breaklen, 1, ctx->fd); /* - * For images with alpha, matte against + * For images with ctx->alpha, matte against * a white background; i.e. * Cback * (1 - Aimage) * where Cback = 1. */ adjust = 255 - cp[1]; - c = *cp++ + adjust; PUTHEX(c,fd); + c = *cp++ + adjust; PUTHEX(c,ctx->fd); cp++, cc--; } } else { while (cc-- > 0) { c = *cp++; - DOBREAK(breaklen, 1, fd); - PUTHEX(c, fd); + DOBREAK(breaklen, 1, ctx->fd); + PUTHEX(c, ctx->fd); } } } } - if ( !ascii85 ) + if ( !ctx->ascii85 ) { - if ( level2 || level3) - fputs(">\n", fd); + if ( ctx->level2 || ctx->level3) + fputs(">\n", ctx->fd); } #if !defined( EXP_ASCII85ENCODER ) else - Ascii85Flush(fd); + Ascii85Flush(ctx); #else if ( ascii85_p ) _TIFFfree( ascii85_p ); @@ -1599,16 +1639,15 @@ PSDataBW(FILE* fd, TIFF* tif, uint32 w, uint32 h) } static void -Ascii85Init(void) +Ascii85Init(TIFF2PSContext *ctx) { - ascii85breaklen = 2*MAXLINE; - ascii85count = 0; + ctx->ascii85breaklen = 2*MAXLINE; + ctx->ascii85count = 0; } -static char* -Ascii85Encode(unsigned char* raw) +static void +Ascii85Encode(unsigned char* raw, char *buf) { - static char encoded[6]; uint32 word; word = (((raw[0]<<8)+raw[1])<<16) + (raw[2]<<8) + raw[3]; @@ -1617,60 +1656,62 @@ Ascii85Encode(unsigned char* raw) uint16 w1; q = word / (85L*85*85*85); /* actually only a byte */ - encoded[0] = (char) (q + '!'); + buf[0] = (char) (q + '!'); word -= q * (85L*85*85*85); q = word / (85L*85*85); - encoded[1] = (char) (q + '!'); + buf[1] = (char) (q + '!'); word -= q * (85L*85*85); q = word / (85*85); - encoded[2] = (char) (q + '!'); + buf[2] = (char) (q + '!'); w1 = (uint16) (word - q*(85L*85)); - encoded[3] = (char) ((w1 / 85) + '!'); - encoded[4] = (char) ((w1 % 85) + '!'); - encoded[5] = '\0'; + buf[3] = (char) ((w1 / 85) + '!'); + buf[4] = (char) ((w1 % 85) + '!'); + buf[5] = '\0'; } else - encoded[0] = 'z', encoded[1] = '\0'; - return (encoded); + buf[0] = 'z', buf[1] = '\0'; } void -Ascii85Put(unsigned char code, FILE* fd) +Ascii85Put(TIFF2PSContext *ctx, unsigned char code) { - ascii85buf[ascii85count++] = code; - if (ascii85count >= 4) { + ctx->ascii85buf[ctx->ascii85count++] = code; + if (ctx->ascii85count >= 4) { unsigned char* p; int n; + char buf[6]; - for (n = ascii85count, p = ascii85buf; n >= 4; n -= 4, p += 4) { + for (n = ctx->ascii85count, p = ctx->ascii85buf; + n >= 4; n -= 4, p += 4) { char* cp; - for (cp = Ascii85Encode(p); *cp; cp++) { - putc(*cp, fd); - if (--ascii85breaklen == 0) { - putc('\n', fd); - ascii85breaklen = 2*MAXLINE; + Ascii85Encode(p, buf); + for (cp = buf; *cp; cp++) { + putc(*cp, ctx->fd); + if (--ctx->ascii85breaklen == 0) { + putc('\n', ctx->fd); + ctx->ascii85breaklen = 2*MAXLINE; } } } - _TIFFmemcpy(ascii85buf, p, n); - ascii85count = n; + _TIFFmemcpy(ctx->ascii85buf, p, n); + ctx->ascii85count = n; } } void -Ascii85Flush(FILE* fd) +Ascii85Flush(TIFF2PSContext* ctx) { - if (ascii85count > 0) { - char* res; - _TIFFmemset(&ascii85buf[ascii85count], 0, 3); - res = Ascii85Encode(ascii85buf); - fwrite(res[0] == 'z' ? "!!!!" : res, ascii85count + 1, 1, fd); + if (ctx->ascii85count > 0) { + char res[6]; + _TIFFmemset(&ctx->ascii85buf[ctx->ascii85count], 0, 3); + Ascii85Encode(ctx->ascii85buf, res); + fwrite(res[0] == 'z' ? "!!!!" : res, ctx->ascii85count + 1, 1, ctx->fd); } - fputs("~>\n", fd); + fputs("~>\n", ctx->fd); } #if defined( EXP_ASCII85ENCODER) -#define A85BREAKCNTR ascii85breaklen +#define A85BREAKCNTR ctx->ascii85breaklen #define A85BREAKLEN (2*MAXLINE) /***************************************************************************** @@ -1681,7 +1722,8 @@ Ascii85Flush(FILE* fd) * by raw_p and raw_l into ASCII85 format and store the encoding * in the buffer given by ascii85_p. * -* Parameters: ascii85_p - A buffer supplied by the caller which will +* Parameters: ctx - TIFF2PS context +* ascii85_p - A buffer supplied by the caller which will * contain the encoded ASCII85 data. * f_eod - Flag: Nz means to end the encoded buffer with * an End-Of-Data marker. @@ -1711,7 +1753,8 @@ Ascii85Flush(FILE* fd) * *****************************************************************************/ -int Ascii85EncodeBlock( uint8 * ascii85_p, unsigned f_eod, const uint8 * raw_p, int raw_l ) +int Ascii85EncodeBlock( TIFF2PSContext *ctx, uint8 * ascii85_p, + unsigned f_eod, const uint8 * raw_p, int raw_l ) { char ascii85[5]; /* Encoded 5 tuple */ diff --git a/tiff/tiff2ps.h b/tiff/tiff2ps.h index 0fde5a62..1944aacd 100644 --- a/tiff/tiff2ps.h +++ b/tiff/tiff2ps.h @@ -1,5 +1,30 @@ +/* + * Copyright (C) 2005 rpath, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include #include #include "tiffio.h" -void TIFF2PS(FILE*, TIFF*, double, double, double, double, int, int*); -void TIFFPSTail(FILE*, int); +typedef struct _TIFF2PSContext TIFF2PSContext; + +TIFF2PSContext *tiff2ps_context_new(const gchar *filename); +void tiff2ps_process_page(TIFF2PSContext* ctx, TIFF* tif, + double pagewidth, double pageheight, + double leftmargin, double bottommargin, + gboolean center); +void tiff2ps_context_finalize(TIFF2PSContext* ctx); -- 2.43.5