[poppler] 2 commits - goo/TiffWriter.cc goo/TiffWriter.h splash/SplashBitmap.cc utils/pdftocairo.1 utils/pdftocairo.cc

Adrian Johnson ajohnson at kemper.freedesktop.org
Fri Nov 16 15:45:36 PST 2012


 goo/TiffWriter.cc      |   47 ++++++++++----------------
 goo/TiffWriter.h       |   13 +++++--
 splash/SplashBitmap.cc |   23 +++++++++++-
 utils/pdftocairo.1     |   23 +++++++-----
 utils/pdftocairo.cc    |   88 ++++++++++++++++++++++++++++++++++++++-----------
 5 files changed, 133 insertions(+), 61 deletions(-)

New commits:
commit d1c509c48fc422649d0c2c2d70af5e13e3472874
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Sat Nov 17 10:04:55 2012 +1030

    pdftocairo: add tiff support
    
    Bug 57006

diff --git a/utils/pdftocairo.1 b/utils/pdftocairo.1
index 19b62ef..7d17eef 100644
--- a/utils/pdftocairo.1
+++ b/utils/pdftocairo.1
@@ -1,6 +1,6 @@
 .TH pdftoppm 1
 .SH NAME
-pdftocairo \- Portable Document Format (PDF) to PNG/JPEG/PDF/PS/EPS/SVG using cairo
+pdftocairo \- Portable Document Format (PDF) to PNG/JPEG/TIFF/PDF/PS/EPS/SVG using cairo
 .SH SYNOPSIS
 .B pdftocairo
 [options]
@@ -14,6 +14,8 @@ Portable Network Graphics (PNG)
 .IP \(bu
 JPEG Interchange Format (JPEG)
 .IP \(bu
+Tagged Image File Format (TIFF)
+.IP \(bu
 Portable Document Format (PDF)
 .IP \(bu
 PostScript (PS)
@@ -27,7 +29,7 @@ reads the PDF file,
 .IR PDF-file ,
 and writes to
 .IR output-file .
-The image formats (PNG and JPEG) generate one file per page with the page number and file type appended to
+The image formats (PNG, JPEG, and TIFF) generate one file per page with the page number and file type appended to
 .IR output-file
 (except when \-singlefile is used).
 When the output format is a vector format (PDF, PS, EPS, and SVG) or when \-singlefile is used,
@@ -46,7 +48,7 @@ is not used, the output filename will be derived from the
 .IR PDF-file
 filename.
 .PP
-Not all options are valid with all output formats. One (and only one) of the output format options (\-png, \-jpeg, \-pdf, \-ps, \-eps, or \-svg) must be used.
+Not all options are valid with all output formats. One (and only one) of the output format options (\-png, \-jpeg, \-tiff, \-pdf, \-ps, \-eps, or \-svg) must be used.
 .PP
 The resolution options (\-r, \-rx, \-ry) set the resolution of the
 image output formats. The image dimensions will depend on the PDF page
@@ -72,6 +74,9 @@ Generates a PNG file(s)
 .BI \-jpeg
 Generates a JPEG file(s)
 .TP
+.BI \-tiff
+Generates a TIFF file(s)
+.TP
 .BI \-pdf
 Generates a PDF file
 .TP
@@ -114,17 +119,17 @@ Specifies the Y resolution, in pixels per inch of image files (or rasterized reg
 .BI \-scale-to " number"
 Scales the long side of each page (width for landscape pages, height
 for portrait pages) to fit in scale-to pixels. The size of the short
-side will be determined by the aspect ratio of the page (PNG/JPEG only).
+side will be determined by the aspect ratio of the page (PNG/JPEG/TIFF only).
 .TP
 .BI \-scale-to-x " number"
 Scales each page horizontally to fit in scale-to-x pixels. If
 scale-to-y is set to -1, the vertical size will determined by the
-aspect ratio of the page (PNG/JPEG only).
+aspect ratio of the page (PNG/JPEG/TIFF only).
 .TP
 .BI \-scale-to-y " number"
 Scales each page vertically to fit in scale-to-y pixels. If scale-to-x
 is set to -1, the horizontal size will determined by the aspect ratio
-of the page (PNG/JPEG only).
+of the page (PNG/JPEG/TIFF only).
 .TP
 .BI \-x " number"
 Specifies the x-coordinate of the crop area top left corner in pixels (image output) or points (vector output)
@@ -145,13 +150,13 @@ Specifies the size of crop square in pixels (image output) or points (vector out
 Uses the crop box rather than media box when generating the files
 .TP
 .B \-mono
-Generate a monochrome file (PNG only).
+Generate a monochrome file (PNG and TIFF only).
 .TP
 .B \-gray
-Generate a grayscale file (PNG and JPEG only).
+Generate a grayscale file (PNG, JPEG, and TIFF only).
 .TP
 .B \-transp
-Use a transparent page color instead of white (PNG only).
+Use a transparent page color instead of white (PNG and TIFF only).
 .TP
 .BI \-icc " icc-file"
 Use the specified ICC file as the output profile (PNG only). The profile will be embedded in the PNG file.
diff --git a/utils/pdftocairo.cc b/utils/pdftocairo.cc
index 8d13e31..258d939 100644
--- a/utils/pdftocairo.cc
+++ b/utils/pdftocairo.cc
@@ -45,6 +45,7 @@
 #include "goo/ImgWriter.h"
 #include "goo/JpegWriter.h"
 #include "goo/PNGWriter.h"
+#include "goo/TiffWriter.h"
 #include "GlobalParams.h"
 #include "Object.h"
 #include "PDFDoc.h"
@@ -75,6 +76,7 @@ static GBool ps = gFalse;
 static GBool eps = gFalse;
 static GBool pdf = gFalse;
 static GBool svg = gFalse;
+static GBool tiff = gFalse;
 
 static int firstPage = 1;
 static int lastPage = 0;
@@ -109,6 +111,7 @@ static GBool expand = gFalse;
 static GBool noShrink = gFalse;
 static GBool noCenter = gFalse;
 static GBool duplex = gFalse;
+static char tiffCompressionStr[16] = "";
 
 static char ownerPassword[33] = "";
 static char userPassword[33] = "";
@@ -125,6 +128,12 @@ static const ArgDesc argDesc[] = {
   {"-jpeg",   argFlag,     &jpeg,           0,
    "generate a JPEG file"},
 #endif
+#if ENABLE_LIBTIFF
+  {"-tiff",    argFlag,     &tiff,           0,
+   "generate a TIFF file"},
+  {"-tiffcompression", argString, tiffCompressionStr, sizeof(tiffCompressionStr),
+   "set TIFF compression: none, packbits, jpeg, lzw, deflate"},
+#endif
 #if CAIRO_HAS_PS_SURFACE
   {"-ps",     argFlag,     &ps,            0,
    "generate PostScript file"},
@@ -288,6 +297,18 @@ void writePageImage(GooString *filename)
     else
       writer = new JpegWriter(JCS_RGB);
 #endif
+  } else if (tiff) {
+#if ENABLE_LIBTIFF
+    if (transp)
+      writer = new TiffWriter(TiffWriter::RGBA_PREMULTIPLIED);
+    else if (gray)
+      writer = new TiffWriter(TiffWriter::GRAY);
+    else if (mono)
+      writer = new TiffWriter(TiffWriter::MONOCHROME);
+    else
+      writer = new TiffWriter(TiffWriter::RGB);
+    static_cast<TiffWriter*>(writer)->setCompressionString(tiffCompressionStr);
+#endif
   }
   if (!writer)
     return;
@@ -316,21 +337,30 @@ void writePageImage(GooString *filename)
   for (int y = 0; y < height; y++ ) {
     uint32_t *pixel = (uint32_t *) (data + y*stride);
     unsigned char *rowp = row;
+    int bit = 7;
     for (int x = 0; x < width; x++, pixel++) {
       if (transp) {
+        if (tiff) {
+          // RGBA premultipled format
+          *rowp++ = (*pixel &   0xff0000) >> 16;
+          *rowp++ = (*pixel &   0x00ff00) >>  8;
+          *rowp++ = (*pixel &   0x0000ff) >>  0;
+          *rowp++ = (*pixel & 0xff000000) >> 24;
+        } else {
 	// unpremultiply into RGBA format
-	uint8_t a;
-	a = (*pixel & 0xff000000) >> 24;
-	if (a == 0) {
-	  *rowp++ = 0;
-	  *rowp++ = 0;
-	  *rowp++ = 0;
-	} else {
-	  *rowp++ = (((*pixel & 0xff0000) >> 16) * 255 + a / 2) / a;
-	  *rowp++ = (((*pixel & 0x00ff00) >>  8) * 255 + a / 2) / a;
-	  *rowp++ = (((*pixel & 0x0000ff) >>  0) * 255 + a / 2) / a;
-	}
-	*rowp++ = a;
+          uint8_t a;
+          a = (*pixel & 0xff000000) >> 24;
+          if (a == 0) {
+            *rowp++ = 0;
+            *rowp++ = 0;
+            *rowp++ = 0;
+          } else {
+            *rowp++ = (((*pixel & 0xff0000) >> 16) * 255 + a / 2) / a;
+            *rowp++ = (((*pixel & 0x00ff00) >>  8) * 255 + a / 2) / a;
+            *rowp++ = (((*pixel & 0x0000ff) >>  0) * 255 + a / 2) / a;
+          }
+          *rowp++ = a;
+        }
       } else if (gray || mono) {
 	// convert to gray
         // The PDF Reference specifies the DeviceRGB to DeviceGray conversion as
@@ -340,7 +370,19 @@ void writePageImage(GooString *filename)
 	int b = (*pixel & 0x000000ff) >>  0;
 	// an arbitrary integer approximation of .3*r + .59*g + .11*b
 	int y = (r*19661+g*38666+b*7209 + 32829)>>16;
-	*rowp++ = y;
+        if (tiff && mono) {
+          if (bit == 7)
+            *rowp = 0;
+          if (y > 127)
+            *rowp |= (1 << bit);
+          bit--;
+          if (bit < 0) {
+            bit = 7;
+            rowp++;
+          }
+        } else {
+          *rowp++ = y;
+        }
       } else {
 	// copy into RGB format
 	*rowp++ = (*pixel & 0x00ff0000) >> 16;
@@ -630,6 +672,8 @@ static GooString *getImageFileName(GooString *outputFileName, int numDigits, int
     imageName->append(".png");
   else if (jpeg)
     imageName->append(".jpg");
+  else if (tiff)
+    imageName->append(".tif");
 
   return imageName;
 }
@@ -702,7 +746,7 @@ static GooString *getOutputFileName(GooString *fileName, GooString *outputName)
 static void checkInvalidPrintOption(GBool option, const char *option_name)
 {
   if (option) {
-    fprintf(stderr, "Error: %s may only be used with the -png or -jpeg output options.\n", option_name);
+    fprintf(stderr, "Error: %s may only be used with the -png, -jpeg, or -tiff output options.\n", option_name);
     exit(99);
   }
 }
@@ -752,6 +796,7 @@ int main(int argc, char *argv[]) {
 
   num_outputs = (png ? 1 : 0) +
                 (jpeg ? 1 : 0) +
+                (tiff ? 1 : 0) +
                 (ps ? 1 : 0) +
                 (eps ? 1 : 0) +
                 (pdf ? 1 : 0) +
@@ -764,7 +809,7 @@ int main(int argc, char *argv[]) {
     fprintf(stderr, "Error: use only one of the output format options (-png, -jpeg, -ps, -eps, -pdf, -svg).\n");
     exit(99);
   }
-  if (png || jpeg)
+  if (png || jpeg || tiff)
     printing = gFalse;
   else
     printing = gTrue;
@@ -794,8 +839,8 @@ int main(int argc, char *argv[]) {
     exit(99);
   }
 
-  if (transp && !png) {
-    fprintf(stderr, "Error: -transp may only be used with png output.\n");
+  if (transp && !(png || tiff)) {
+    fprintf(stderr, "Error: -transp may only be used with png or tiff output.\n");
     exit(99);
   }
 
@@ -804,8 +849,13 @@ int main(int argc, char *argv[]) {
     exit(99);
   }
 
-  if (mono && !png) {
-    fprintf(stderr, "Error: -mono may only be used with png output.\n");
+  if (mono && !(png || tiff)) {
+    fprintf(stderr, "Error: -mono may only be used with png or tiff output.\n");
+    exit(99);
+  }
+
+  if (strlen(tiffCompressionStr) > 0 && !tiff) {
+    fprintf(stderr, "Error: -tiffcompression may only be used with tiff output.\n");
     exit(99);
   }
 
commit 6d3fe48cf30b298b4436462904d781ea441c1f3d
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Sat Nov 17 10:03:22 2012 +1030

    tiff: remove splash dependency and add RGBA support
    
    Bug 57006

diff --git a/goo/TiffWriter.cc b/goo/TiffWriter.cc
index 9c134f9..660764c 100644
--- a/goo/TiffWriter.cc
+++ b/goo/TiffWriter.cc
@@ -20,13 +20,12 @@ TiffWriter::~TiffWriter()
   // no cleanup needed
 }
 
-TiffWriter::TiffWriter()
+TiffWriter::TiffWriter(Format formatA) : format(formatA)
 {
   f = NULL;
   numRows = 0;
   curRow = 0;
   compressionString = NULL;
-  splashMode = splashModeRGB8;
 }
 
 // Set the compression type
@@ -36,13 +35,6 @@ void TiffWriter::setCompressionString(const char *compressionStringArg)
   compressionString = compressionStringArg;
 }
 
-// Set the bitmap mode
-
-void TiffWriter::setSplashMode(SplashColorMode splashModeArg)
-{
-  splashMode = splashModeArg;
-}
-
 // Write a TIFF file.
 
 bool TiffWriter::init(FILE *openedFile, int width, int height, int hDPI, int vDPI)
@@ -109,35 +101,31 @@ bool TiffWriter::init(FILE *openedFile, int width, int height, int hDPI, int vDP
     }
   }
 
-  // Set bits per sample, samples per pixel, and photometric type from the splash mode
+  // Set bits per sample, samples per pixel, and photometric type from format
 
-  bitspersample = (splashMode == splashModeMono1? 1: 8);
+  bitspersample = (format == MONOCHROME ? 1 : 8);
 
-  switch (splashMode) {
-
-  case splashModeMono1:
-  case splashModeMono8:
+  switch (format) {
+  case MONOCHROME:
+  case GRAY:
     samplesperpixel = 1;
     photometric = PHOTOMETRIC_MINISBLACK;
     break;
 
-  case splashModeRGB8:
-  case splashModeBGR8:
+  case RGB:
     samplesperpixel = 3;
     photometric = PHOTOMETRIC_RGB;
     break;
 
-#if SPLASH_CMYK
-  case splashModeCMYK8:
-  case splashModeDeviceN8:
+  case RGBA_PREMULTIPLIED:
     samplesperpixel = 4;
-    photometric = PHOTOMETRIC_SEPARATED;
+    photometric = PHOTOMETRIC_RGB;
     break;
-#endif
 
-  default:
-    fprintf(stderr, "TiffWriter: Mode %d not supported\n", splashMode);
-    return false;
+  case CMYK:
+    samplesperpixel = 4;
+    photometric = PHOTOMETRIC_SEPARATED;
+    break;
   }
 
   // Open the file
@@ -168,12 +156,15 @@ bool TiffWriter::init(FILE *openedFile, int width, int height, int hDPI, int vDP
   TIFFSetField(f, TIFFTAG_YRESOLUTION, (double) vDPI);
   TIFFSetField(f, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
 
-#if SPLASH_CMYK
-  if (splashMode == splashModeCMYK8 || splashMode == splashModeDeviceN8) {
+  if (format == RGBA_PREMULTIPLIED) {
+    uint16 extra = EXTRASAMPLE_ASSOCALPHA;
+    TIFFSetField(f, TIFFTAG_EXTRASAMPLES, 1, &extra);
+  }
+
+  if (format == CMYK) {
     TIFFSetField(f, TIFFTAG_INKSET, INKSET_CMYK);
     TIFFSetField(f, TIFFTAG_NUMBEROFINKS, 4);
   }
-#endif
 
   return true;
 }
diff --git a/goo/TiffWriter.h b/goo/TiffWriter.h
index bffd17a..831b03b 100644
--- a/goo/TiffWriter.h
+++ b/goo/TiffWriter.h
@@ -27,11 +27,18 @@ extern "C" {
 class TiffWriter : public ImgWriter
 {
 	public:
-		TiffWriter();
+		/* RGB                 - 3 bytes/pixel
+		 * RGBA_PREMULTIPLIED  - 4 bytes/pixel premultiplied by alpha
+		 * GRAY                - 1 byte/pixel
+		 * MONOCHROME          - 8 pixels/byte
+		 * CMYK                - 4 bytes/pixel
+		 */
+		enum Format { RGB, RGBA_PREMULTIPLIED, GRAY, MONOCHROME, CMYK };
+
+		TiffWriter(Format format = RGB);
 		~TiffWriter();
 		
 		void setCompressionString(const char *compressionStringArg);
-		void setSplashMode(SplashColorMode splashModeArg);
 
 		bool init(FILE *openedFile, int width, int height, int hDPI, int vDPI);
 		
@@ -47,7 +54,7 @@ class TiffWriter : public ImgWriter
 		int numRows;				// number of rows in the image
 		int curRow;				// number of rows written
 		const char *compressionString;		// compression type
-		SplashColorMode splashMode;		// format of image data
+		Format format;				// format of image data
 
 };
 
diff --git a/splash/SplashBitmap.cc b/splash/SplashBitmap.cc
index 996f0aa..f514dfa 100644
--- a/splash/SplashBitmap.cc
+++ b/splash/SplashBitmap.cc
@@ -373,10 +373,29 @@ SplashError SplashBitmap::writeImgFile(SplashImageFileFormat format, FILE *f, in
 	
     #ifdef ENABLE_LIBTIFF
     case splashFormatTiff:
-      writer = new TiffWriter();
+      switch (mode) {
+      case splashModeMono1:
+        writer = new TiffWriter(TiffWriter::MONOCHROME);
+        break;
+      case splashModeMono8:
+        writer = new TiffWriter(TiffWriter::GRAY);
+        break;
+      case splashModeRGB8:
+      case splashModeBGR8:
+        writer = new TiffWriter(TiffWriter::RGB);
+        break;
+#if SPLASH_CMYK
+      case splashModeCMYK8:
+      case splashModeDeviceN8:
+        writer = new TiffWriter(TiffWriter::CMYK);
+        break;
+#endif
+      default:
+        fprintf(stderr, "TiffWriter: Mode %d not supported\n", mode);
+        writer = new TiffWriter();
+      }
       if (writer) {
         ((TiffWriter *)writer)->setCompressionString(compressionString);
-        ((TiffWriter *)writer)->setSplashMode(mode);
       }
       break;
     #endif


More information about the poppler mailing list