[poppler] 2 commits - goo/PNGWriter.cc goo/PNGWriter.h goo/TiffWriter.cc goo/TiffWriter.h utils/HtmlOutputDev.cc

Pino Toscano pino at kemper.freedesktop.org
Sun Dec 9 13:41:58 PST 2012


 goo/PNGWriter.cc       |   98 ++++++++++++++++++++++++++++---------------------
 goo/PNGWriter.h        |   12 +-----
 goo/TiffWriter.cc      |   94 +++++++++++++++++++++++++++--------------------
 goo/TiffWriter.h       |   12 +-----
 utils/HtmlOutputDev.cc |    4 ++
 5 files changed, 120 insertions(+), 100 deletions(-)

New commits:
commit e306bad391a3ff49593f1f3bac0717d62599bd13
Author: Pino Toscano <pino at kde.org>
Date:   Sun Dec 9 22:40:27 2012 +0100

    pngwriter: move #include <png.h> into .cc file
    
    Move all the private data (including the libpng types) to a private class.
    
    This requires HtmlOutputDev.cc to include <png.h> on its own (which is correct, since it uses the libpng API directly).

diff --git a/goo/PNGWriter.cc b/goo/PNGWriter.cc
index 63ef7c7..1f35d8e 100644
--- a/goo/PNGWriter.cc
+++ b/goo/PNGWriter.cc
@@ -24,78 +24,94 @@
 #include "poppler/Error.h"
 #include "goo/gmem.h"
 
-PNGWriter::PNGWriter(Format formatA) : format(formatA)
+#include <png.h>
+
+struct PNGWriterPrivate {
+  PNGWriter::Format format;
+  png_structp png_ptr;
+  png_infop info_ptr;
+  unsigned char *icc_data;
+  int icc_data_size;
+  char *icc_name;
+  bool sRGB_profile;
+};
+
+PNGWriter::PNGWriter(Format formatA)
 {
-  icc_data = NULL;
-  icc_data_size = 0;
-  icc_name = NULL;
-  sRGB_profile = false;
+  priv = new PNGWriterPrivate;
+  priv->format = formatA;
+  priv->icc_data = NULL;
+  priv->icc_data_size = 0;
+  priv->icc_name = NULL;
+  priv->sRGB_profile = false;
 }
 
 PNGWriter::~PNGWriter()
 {
   /* cleanup heap allocation */
-  png_destroy_write_struct(&png_ptr, &info_ptr);
-  if (icc_data) {
-    gfree(icc_data);
-    free(icc_name);
+  png_destroy_write_struct(&priv->png_ptr, &priv->info_ptr);
+  if (priv->icc_data) {
+    gfree(priv->icc_data);
+    free(priv->icc_name);
   }
+
+  delete priv;
 }
 
 void PNGWriter::setICCProfile(const char *name, unsigned char *data, int size)
 {
-  icc_data = (unsigned char *)gmalloc(size);
-  memcpy(icc_data, data, size);
-  icc_data_size = size;
-  icc_name = strdup(name);
+  priv->icc_data = (unsigned char *)gmalloc(size);
+  memcpy(priv->icc_data, data, size);
+  priv->icc_data_size = size;
+  priv->icc_name = strdup(name);
 }
 
 void PNGWriter::setSRGBProfile()
 {
-  sRGB_profile = true;
+  priv->sRGB_profile = true;
 }
 
 bool PNGWriter::init(FILE *f, int width, int height, int hDPI, int vDPI)
 {
   /* libpng changed the png_set_iCCP() prototype in 1.5.0 */
 #if PNG_LIBPNG_VER < 10500
-  png_charp icc_data_ptr = (png_charp)icc_data;
+  png_charp icc_data_ptr = (png_charp)priv->icc_data;
 #else
-  png_const_bytep icc_data_ptr = (png_const_bytep)icc_data;
+  png_const_bytep icc_data_ptr = (png_const_bytep)priv->icc_data;
 #endif
 
   /* initialize stuff */
-  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
-  if (!png_ptr) {
+  priv->png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+  if (!priv->png_ptr) {
     error(errInternal, -1, "png_create_write_struct failed");
     return false;
   }
 
-  info_ptr = png_create_info_struct(png_ptr);
-  if (!info_ptr) {
+  priv->info_ptr = png_create_info_struct(priv->png_ptr);
+  if (!priv->info_ptr) {
     error(errInternal, -1, "png_create_info_struct failed");
     return false;
   }
 
-  if (setjmp(png_jmpbuf(png_ptr))) {
+  if (setjmp(png_jmpbuf(priv->png_ptr))) {
     error(errInternal, -1, "png_jmpbuf failed");
     return false;
   }
 
   /* write header */
-  png_init_io(png_ptr, f);
-  if (setjmp(png_jmpbuf(png_ptr))) {
+  png_init_io(priv->png_ptr, f);
+  if (setjmp(png_jmpbuf(priv->png_ptr))) {
     error(errInternal, -1, "Error during writing header");
     return false;
   }
 
   // Set up the type of PNG image and the compression level
-  png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
+  png_set_compression_level(priv->png_ptr, Z_BEST_COMPRESSION);
 
   // Silence silly gcc
   png_byte bit_depth = -1;
   png_byte color_type = -1;
-  switch (format) {
+  switch (priv->format) {
     case RGB:
       bit_depth = 8;
       color_type = PNG_COLOR_TYPE_RGB;
@@ -115,33 +131,33 @@ bool PNGWriter::init(FILE *f, int width, int height, int hDPI, int vDPI)
   }
   png_byte interlace_type = PNG_INTERLACE_NONE;
 
-  png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, interlace_type, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+  png_set_IHDR(priv->png_ptr, priv->info_ptr, width, height, bit_depth, color_type, interlace_type, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
 
-  png_set_pHYs(png_ptr, info_ptr, hDPI/0.0254, vDPI/0.0254, PNG_RESOLUTION_METER);
+  png_set_pHYs(priv->png_ptr, priv->info_ptr, hDPI/0.0254, vDPI/0.0254, PNG_RESOLUTION_METER);
 
-  if (icc_data)
-    png_set_iCCP(png_ptr, info_ptr, icc_name, PNG_COMPRESSION_TYPE_BASE, icc_data_ptr, icc_data_size);
-  else if (sRGB_profile)
-    png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_RELATIVE);
+  if (priv->icc_data)
+    png_set_iCCP(priv->png_ptr, priv->info_ptr, priv->icc_name, PNG_COMPRESSION_TYPE_BASE, icc_data_ptr, priv->icc_data_size);
+  else if (priv->sRGB_profile)
+    png_set_sRGB(priv->png_ptr, priv->info_ptr, PNG_sRGB_INTENT_RELATIVE);
 
-  png_write_info(png_ptr, info_ptr);
-  if (setjmp(png_jmpbuf(png_ptr))) {
+  png_write_info(priv->png_ptr, priv->info_ptr);
+  if (setjmp(png_jmpbuf(priv->png_ptr))) {
     error(errInternal, -1, "error during writing png info bytes");
     return false;
   }
 
   // pack 1 pixel/byte rows into 8 pixels/byte
-  if (format == MONOCHROME)
-    png_set_packing(png_ptr);
+  if (priv->format == MONOCHROME)
+    png_set_packing(priv->png_ptr);
 
   return true;
 }
 
 bool PNGWriter::writePointers(unsigned char **rowPointers, int rowCount)
 {
-  png_write_image(png_ptr, rowPointers);
+  png_write_image(priv->png_ptr, rowPointers);
   /* write bytes */
-  if (setjmp(png_jmpbuf(png_ptr))) {
+  if (setjmp(png_jmpbuf(priv->png_ptr))) {
     error(errInternal, -1, "Error during writing bytes");
     return false;
   }
@@ -152,8 +168,8 @@ bool PNGWriter::writePointers(unsigned char **rowPointers, int rowCount)
 bool PNGWriter::writeRow(unsigned char **row)
 {
   // Write the row to the file
-  png_write_rows(png_ptr, row, 1);
-  if (setjmp(png_jmpbuf(png_ptr))) {
+  png_write_rows(priv->png_ptr, row, 1);
+  if (setjmp(png_jmpbuf(priv->png_ptr))) {
     error(errInternal, -1, "error during png row write");
     return false;
   }
@@ -164,8 +180,8 @@ bool PNGWriter::writeRow(unsigned char **row)
 bool PNGWriter::close()
 {
   /* end write */
-  png_write_end(png_ptr, info_ptr);
-  if (setjmp(png_jmpbuf(png_ptr))) {
+  png_write_end(priv->png_ptr, priv->info_ptr);
+  if (setjmp(png_jmpbuf(priv->png_ptr))) {
     error(errInternal, -1, "Error during end of write");
     return false;
   }
diff --git a/goo/PNGWriter.h b/goo/PNGWriter.h
index dc905d7..67cd31e 100644
--- a/goo/PNGWriter.h
+++ b/goo/PNGWriter.h
@@ -19,10 +19,10 @@
 
 #ifdef ENABLE_LIBPNG
 
-#include <cstdio>
-#include <png.h>
 #include "ImgWriter.h"
 
+class PNGWriterPrivate;
+
 class PNGWriter : public ImgWriter
 {
 public:
@@ -52,13 +52,7 @@ private:
   PNGWriter(const PNGWriter &other);
   PNGWriter& operator=(const PNGWriter &other);
 
-  Format format;
-  png_structp png_ptr;
-  png_infop info_ptr;
-  unsigned char *icc_data;
-  int icc_data_size;
-  char *icc_name;
-  bool sRGB_profile;
+  PNGWriterPrivate *priv;
 };
 
 #endif
diff --git a/utils/HtmlOutputDev.cc b/utils/HtmlOutputDev.cc
index e4bd0b1..565c932 100644
--- a/utils/HtmlOutputDev.cc
+++ b/utils/HtmlOutputDev.cc
@@ -67,6 +67,10 @@
 #include "Outline.h"
 #include "PDFDoc.h"
 
+#ifdef ENABLE_LIBPNG
+#include <png.h>
+#endif
+
 #define DEBUG __FILE__ << ": " << __LINE__ << ": DEBUG: "
 
 class HtmlImage
commit cf338551e9d031cc00d56cea0d258ec5fd96e79a
Author: Pino Toscano <pino at kde.org>
Date:   Sun Dec 9 22:22:03 2012 +0100

    tiffwriter: move #include <tiffio.h> into .cc file
    
    Move all the private data (including the libtiff types) to a private class.

diff --git a/goo/TiffWriter.cc b/goo/TiffWriter.cc
index 048cea8..3e8af0d 100644
--- a/goo/TiffWriter.cc
+++ b/goo/TiffWriter.cc
@@ -16,24 +16,38 @@
 
 #include <string.h>
 
+extern "C" {
+#include <tiffio.h>
+}
+
+struct TiffWriterPrivate {
+  TIFF *f;				// LibTiff file context
+  int numRows;				// number of rows in the image
+  int curRow;				// number of rows written
+  const char *compressionString;	// compression type
+  TiffWriter::Format format;		// format of image data
+};
+
 TiffWriter::~TiffWriter()
 {
-  // no cleanup needed
+  delete priv;
 }
 
-TiffWriter::TiffWriter(Format formatA) : format(formatA)
+TiffWriter::TiffWriter(Format formatA)
 {
-  f = NULL;
-  numRows = 0;
-  curRow = 0;
-  compressionString = NULL;
+  priv = new TiffWriterPrivate;
+  priv->f = NULL;
+  priv->numRows = 0;
+  priv->curRow = 0;
+  priv->compressionString = NULL;
+  priv->format = formatA;
 }
 
 // Set the compression type
 
 void TiffWriter::setCompressionString(const char *compressionStringArg)
 {
-  compressionString = compressionStringArg;
+  priv->compressionString = compressionStringArg;
 }
 
 // Write a TIFF file.
@@ -72,29 +86,29 @@ bool TiffWriter::init(FILE *openedFile, int width, int height, int hDPI, int vDP
 
   // Initialize
 
-  f = NULL;
-  curRow = 0;
+  priv->f = NULL;
+  priv->curRow = 0;
 
   // Store the number of rows
 
-  numRows = height;
+  priv->numRows = height;
 
   // Set the compression
 
   compression = COMPRESSION_NONE;
 
-  if (compressionString == NULL || strcmp(compressionString, "") == 0) {
+  if (priv->compressionString == NULL || strcmp(priv->compressionString, "") == 0) {
     compression = COMPRESSION_NONE;
   } else {
     int i;
     for (i = 0; compressionList[i].compressionName != NULL; i++) {
-      if (strcmp(compressionString, compressionList[i].compressionName) == 0) {
+      if (strcmp(priv->compressionString, compressionList[i].compressionName) == 0) {
 	compression = compressionList[i].compressionCode;
 	break;
       }
     }
     if (compressionList[i].compressionName == NULL) {
-      fprintf(stderr, "TiffWriter: Unknown compression type '%.10s', using 'none'.\n", compressionString);
+      fprintf(stderr, "TiffWriter: Unknown compression type '%.10s', using 'none'.\n", priv->compressionString);
       fprintf(stderr, "Known compression types (the tiff library might not support every type)\n");
       for (i = 0; compressionList[i].compressionName != NULL; i++) {
 	fprintf(stderr, "%10s %s\n", compressionList[i].compressionName, compressionList[i].compressionDescription);
@@ -104,9 +118,9 @@ bool TiffWriter::init(FILE *openedFile, int width, int height, int hDPI, int vDP
 
   // Set bits per sample, samples per pixel, and photometric type from format
 
-  bitspersample = (format == MONOCHROME ? 1 : 8);
+  bitspersample = (priv->format == MONOCHROME ? 1 : 8);
 
-  switch (format) {
+  switch (priv->format) {
     case MONOCHROME:
     case GRAY:
       samplesperpixel = 1;
@@ -136,35 +150,35 @@ bool TiffWriter::init(FILE *openedFile, int width, int height, int hDPI, int vDP
     return false;
   }
 
-  f = TIFFFdOpen(fileno(openedFile), "-", "w");
+  priv->f = TIFFFdOpen(fileno(openedFile), "-", "w");
 
-  if (!f) {
+  if (!priv->f) {
     return false;
   }
 
   // Set TIFF tags
 
-  TIFFSetField(f, TIFFTAG_IMAGEWIDTH,  width);
-  TIFFSetField(f, TIFFTAG_IMAGELENGTH, height);
-  TIFFSetField(f, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
-  TIFFSetField(f, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
-  TIFFSetField(f, TIFFTAG_BITSPERSAMPLE, bitspersample);
-  TIFFSetField(f, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
-  TIFFSetField(f, TIFFTAG_PHOTOMETRIC, photometric);
-  TIFFSetField(f, TIFFTAG_COMPRESSION, (uint16) compression);
-  TIFFSetField(f, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(f, rowsperstrip));
-  TIFFSetField(f, TIFFTAG_XRESOLUTION, (double) hDPI);
-  TIFFSetField(f, TIFFTAG_YRESOLUTION, (double) vDPI);
-  TIFFSetField(f, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
-
-  if (format == RGBA_PREMULTIPLIED) {
+  TIFFSetField(priv->f, TIFFTAG_IMAGEWIDTH,  width);
+  TIFFSetField(priv->f, TIFFTAG_IMAGELENGTH, height);
+  TIFFSetField(priv->f, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
+  TIFFSetField(priv->f, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
+  TIFFSetField(priv->f, TIFFTAG_BITSPERSAMPLE, bitspersample);
+  TIFFSetField(priv->f, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
+  TIFFSetField(priv->f, TIFFTAG_PHOTOMETRIC, photometric);
+  TIFFSetField(priv->f, TIFFTAG_COMPRESSION, (uint16) compression);
+  TIFFSetField(priv->f, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(priv->f, rowsperstrip));
+  TIFFSetField(priv->f, TIFFTAG_XRESOLUTION, (double) hDPI);
+  TIFFSetField(priv->f, TIFFTAG_YRESOLUTION, (double) vDPI);
+  TIFFSetField(priv->f, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
+
+  if (priv->format == RGBA_PREMULTIPLIED) {
     uint16 extra = EXTRASAMPLE_ASSOCALPHA;
-    TIFFSetField(f, TIFFTAG_EXTRASAMPLES, 1, &extra);
+    TIFFSetField(priv->f, TIFFTAG_EXTRASAMPLES, 1, &extra);
   }
 
-  if (format == CMYK) {
-    TIFFSetField(f, TIFFTAG_INKSET, INKSET_CMYK);
-    TIFFSetField(f, TIFFTAG_NUMBEROFINKS, 4);
+  if (priv->format == CMYK) {
+    TIFFSetField(priv->f, TIFFTAG_INKSET, INKSET_CMYK);
+    TIFFSetField(priv->f, TIFFTAG_NUMBEROFINKS, 4);
   }
 
   return true;
@@ -175,7 +189,7 @@ bool TiffWriter::writePointers(unsigned char **rowPointers, int rowCount)
   // Write all rows to the file
 
   for (int row = 0; row < rowCount; row++) {
-    if (TIFFWriteScanline(f, rowPointers[row], row, 0) < 0) {
+    if (TIFFWriteScanline(priv->f, rowPointers[row], row, 0) < 0) {
       fprintf(stderr, "TiffWriter: Error writing tiff row %d\n", row);
       return false;
     }
@@ -188,12 +202,12 @@ bool TiffWriter::writeRow(unsigned char **rowData)
 {
   // Add a single row
 
-  if (TIFFWriteScanline(f, *rowData, curRow, 0) < 0) {
-    fprintf(stderr, "TiffWriter: Error writing tiff row %d\n", curRow);
+  if (TIFFWriteScanline(priv->f, *rowData, priv->curRow, 0) < 0) {
+    fprintf(stderr, "TiffWriter: Error writing tiff row %d\n", priv->curRow);
     return false;
   }
 
-  curRow++;
+  priv->curRow++;
 
   return true;
 }
@@ -202,7 +216,7 @@ bool TiffWriter::close()
 {
   // Close the file
 
-  TIFFClose(f);
+  TIFFClose(priv->f);
 
   return true;
 }
diff --git a/goo/TiffWriter.h b/goo/TiffWriter.h
index 848a24d..14b5e7a 100644
--- a/goo/TiffWriter.h
+++ b/goo/TiffWriter.h
@@ -19,11 +19,8 @@
 
 #include <sys/types.h>
 #include "ImgWriter.h"
-#include "splash/SplashTypes.h"
 
-extern "C" {
-#include "tiffio.h"
-}
+struct TiffWriterPrivate;
 
 class TiffWriter : public ImgWriter
 {
@@ -53,13 +50,8 @@ public:
 private:
   TiffWriter(const TiffWriter &other);
   TiffWriter& operator=(const TiffWriter &other);
-  
-  TIFF *f;				// LibTiff file context
-  int numRows;				// number of rows in the image
-  int curRow;				// number of rows written
-  const char *compressionString;	// compression type
-  Format format;			// format of image data
 
+  TiffWriterPrivate *priv;
 };
 
 #endif


More information about the poppler mailing list