TIFF and LibTiff Mail List Archive

1998.10.08 20:34 "flexfax: tiff2ps patch for better output", by John Williams

Last week I asked about fixing tiff2ps so that short pages would be put at the top of the page instead of the bottom. I didn't get any responses, but I have fixed it myself, so others who are sending their faxes to a printer may find the changes useful as well.

Previously, tiff2ps had 2 modes. If no page height and width were specified (via -h and -w), the tiff image is simply bottom-left justified at the resolution specified in the tiff file. If a page size is given via -h and -w the tiff image is shrunk or expanded to exactly fill the page. This is nice for oversize pages, but results in terribly elongated stretching if the page is short.

To fix this behavior, I have modified the behavior when -h and -w are used to specify the page size. Short (or narrow) images are no longer stretched, instead they are simply positioned at the top-left on the page (previous behavior put it at the bottom-left). Long (or wide) images are shrunk to fit the page.

The new behavior only applies to Postscript level-1 output so far. (But I think level-2 output was already broken anyway.)

A patch for tiff2ps.c (against tiff-v3.4beta035) is attached. Feedback is appreciated, positive or negative. I would like to get this included in the tiff distribution, so if anyone is maintaining it, feel free to contact me.

~ John Williams

--- tiff2ps.c   1998/09/29 15:42:13     1.1
+++ tiff2ps.c   1998/10/08 16:56:50
@@ -56,9 +56,9 @@
 int    ascii85breaklen;

 int    TIFF2PS(FILE*, TIFF*, float, float);
-void   PSpage(FILE*, TIFF*, uint32, uint32);
-void   PSColorContigPreamble(FILE*, uint32, uint32, int);
-void   PSColorSeparatePreamble(FILE*, uint32, uint32, int);
+void   PSpage(FILE*, TIFF*, uint32, uint32, uint32);
+void   PSColorContigPreamble(FILE*, uint32, uint32, uint32, int);
+void   PSColorSeparatePreamble(FILE*, uint32, uint32, uint32, int);
 void   PSDataColorContig(FILE*, TIFF*, uint32, uint32, int);
 void   PSDataColorSeparate(FILE*, TIFF*, uint32, uint32, int);
 void   PSDataPalette(FILE*, TIFF*, uint32, uint32);
@@ -71,6 +71,8 @@
 void   PSTail(FILE*, int);

 static void usage(int);
+#define MIN(a,b)       ((a)<(b)?(a):(b))
+#define MAX(a,b)       ((a)>(b)?(a):(b))

 main(int argc, char* argv[])
@@ -263,10 +265,10 @@

 static void
-setupPageState(TIFF* tif, uint32* pw, uint32* ph, float* pprw, float* pprh)
+setupPageState(TIFF* tif, uint32* pw, uint32* ph, float* pprw, float* pprh, float *xres, float *yres)
        uint16 res_unit;
-       float xres, yres;
+       /*float xres, yres;*/

        TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, pw);
        TIFFGetField(tif, TIFFTAG_IMAGELENGTH, ph);
@@ -274,20 +276,20 @@
         * Calculate printable area.
-       if (!TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xres))
-               xres = PS_UNIT_SIZE;
-       if (!TIFFGetField(tif, TIFFTAG_YRESOLUTION, &yres))
-               yres = PS_UNIT_SIZE;
+       if (!TIFFGetField(tif, TIFFTAG_XRESOLUTION, xres))
+               *xres = PS_UNIT_SIZE;
+       if (!TIFFGetField(tif, TIFFTAG_YRESOLUTION, yres))
+               *yres = PS_UNIT_SIZE;
        switch (res_unit) {
-               xres /= 2.54, yres /= 2.54;
+               *xres /= 2.54, *yres /= 2.54;
        case RESUNIT_NONE:
-               xres *= PS_UNIT_SIZE, yres *= PS_UNIT_SIZE;
+               *xres *= PS_UNIT_SIZE, *yres *= PS_UNIT_SIZE;
-       *pprh = PSUNITS(*ph, yres);
-       *pprw = PSUNITS(*pw, xres);
+       *pprh = PSUNITS(*ph, *yres);
+       *pprw = PSUNITS(*pw, *xres);

 static int
@@ -317,18 +319,19 @@
        uint32 subfiletype;
        uint16* sampleinfo;
        static int npages = 0;
+       float xres,yres;

        if (!TIFFGetField(tif, TIFFTAG_XPOSITION, &ox))
                ox = 0;
        if (!TIFFGetField(tif, TIFFTAG_YPOSITION, &oy))
                oy = 0;
-       setupPageState(tif, &w, &h, &prw, &prh);
+       setupPageState(tif, &w, &h, &prw, &prh, &xres, &yres);

        do {
                tf_numberstrips = TIFFNumberOfStrips(tif);
                TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP,
-               setupPageState(tif, &w, &h, &prw, &prh);
+               setupPageState(tif, &w, &h, &prw, &prh, &xres, &yres);
                if (!npages)
                        PSHead(fd, tif, w, h, prw, prh, ox, oy);
                tf_bytesperrow = TIFFScanlineSize(tif);
@@ -361,12 +364,22 @@
                        fprintf(fd, "%%%%Page: %d %d\n", npages, npages);
                        fprintf(fd, "gsave\n");
                        fprintf(fd, "100 dict begin\n");
-                       if (pw != 0 && ph != 0)

-                               fprintf(fd, "%f %d