[poppler] 2 commits - goo/ImgWriter.h goo/JpegWriter.cc goo/JpegWriter.h goo/PNGWriter.cc goo/PNGWriter.h goo/TiffWriter.cc goo/TiffWriter.h splash/SplashBitmap.cc utils/pdftocairo.cc
Adrian Johnson
ajohnson at kemper.freedesktop.org
Sat Dec 8 14:31:46 PST 2012
goo/ImgWriter.h | 20 ++--
goo/JpegWriter.cc | 224 ++++++++++++++++++++++++++++---------------------
goo/JpegWriter.h | 47 +++++-----
goo/PNGWriter.cc | 224 ++++++++++++++++++++++++-------------------------
goo/PNGWriter.h | 62 ++++++-------
goo/TiffWriter.cc | 40 ++++----
goo/TiffWriter.h | 58 ++++++------
splash/SplashBitmap.cc | 2
utils/pdftocairo.cc | 4
9 files changed, 360 insertions(+), 321 deletions(-)
New commits:
commit f6741d9242bf2d9c13c8d534c50c8e4d404afc7f
Author: Adrian Johnson <ajohnson at redneon.com>
Date: Sun Dec 2 09:34:35 2012 +1030
jpegwriter: move #include "jpeglib.h" into .cc file
On cygwin pdftocairo -v shows the wrong version due to
jpeglib.h defining PACKAGE_VERSION.
Avoid polluting our header files by moving libjpeg.h and
libjpeg types into JpegWriter.cc
Bug 57687
diff --git a/goo/JpegWriter.cc b/goo/JpegWriter.cc
index fc9557f..09f9b7b 100644
--- a/goo/JpegWriter.cc
+++ b/goo/JpegWriter.cc
@@ -15,8 +15,20 @@
#ifdef ENABLE_LIBJPEG
+extern "C" {
+#include <jpeglib.h>
+}
+
#include "poppler/Error.h"
+struct JpegWriterPrivate {
+ bool progressive;
+ int quality;
+ JpegWriter::Format format;
+ struct jpeg_compress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+};
+
void outputMessage(j_common_ptr cinfo)
{
char buffer[JMSG_LENGTH_MAX];
@@ -28,85 +40,103 @@ void outputMessage(j_common_ptr cinfo)
error(errInternal, -1, "{0:s}", buffer);
}
-JpegWriter::JpegWriter(int q, bool p, J_COLOR_SPACE cm)
- : progressive(p), quality(q), colorMode(cm)
+JpegWriter::JpegWriter(int q, bool p, Format formatA)
{
+ priv = new JpegWriterPrivate;
+ priv->progressive = p;
+ priv->quality = q;
+ priv->format = formatA;
}
-JpegWriter::JpegWriter(J_COLOR_SPACE cm)
- : progressive(false), quality(-1), colorMode(cm)
+JpegWriter::JpegWriter(Format formatA)
{
+ priv = new JpegWriterPrivate;
+ priv->progressive = false;
+ priv->quality = -1;
+ priv->format = formatA;
}
JpegWriter::~JpegWriter()
{
// cleanup
- jpeg_destroy_compress(&cinfo);
+ jpeg_destroy_compress(&priv->cinfo);
+ delete priv;
}
bool JpegWriter::init(FILE *f, int width, int height, int hDPI, int vDPI)
{
// Setup error handler
- cinfo.err = jpeg_std_error(&jerr);
- jerr.output_message = &outputMessage;
+ priv->cinfo.err = jpeg_std_error(&priv->jerr);
+ priv->jerr.output_message = &outputMessage;
// Initialize libjpeg
- jpeg_create_compress(&cinfo);
-
- // Set colorspace and initialise defaults
- cinfo.in_color_space = colorMode; /* colorspace of input image */
- jpeg_set_defaults(&cinfo);
+ jpeg_create_compress(&priv->cinfo);
+
+ // First set colorspace and call jpeg_set_defaults() since
+ // jpeg_set_defaults() sets default values for all fields in
+ // cinfo based on the colorspace.
+ switch (priv->format) {
+ case RGB:
+ priv->cinfo.in_color_space = JCS_RGB;
+ break;
+ case GRAY:
+ priv->cinfo.in_color_space = JCS_GRAYSCALE;
+ break;
+ case CMYK:
+ priv->cinfo.in_color_space = JCS_CMYK;
+ break;
+ default:
+ return false;
+ }
+ jpeg_set_defaults(&priv->cinfo);
// Set destination file
- jpeg_stdio_dest(&cinfo, f);
+ jpeg_stdio_dest(&priv->cinfo, f);
// Set libjpeg configuration
- cinfo.image_width = width;
- cinfo.image_height = height;
- cinfo.density_unit = 1; // dots per inch
- cinfo.X_density = hDPI;
- cinfo.Y_density = vDPI;
- /* # of color components per pixel */
- switch (colorMode) {
- case JCS_GRAYSCALE:
- cinfo.input_components = 1;
+ priv->cinfo.image_width = width;
+ priv->cinfo.image_height = height;
+ priv->cinfo.density_unit = 1; // dots per inch
+ priv->cinfo.X_density = hDPI;
+ priv->cinfo.Y_density = vDPI;
+ switch (priv->format) {
+ case GRAY:
+ priv->cinfo.input_components = 1;
break;
- case JCS_RGB:
- cinfo.input_components = 3;
+ case RGB:
+ priv->cinfo.input_components = 3;
break;
- case JCS_CMYK:
- cinfo.input_components = 4;
+ case CMYK:
+ priv->cinfo.input_components = 4;
+ jpeg_set_colorspace(&priv->cinfo, JCS_YCCK);
+ priv->cinfo.write_JFIF_header = TRUE;
break;
default:
return false;
}
- if (cinfo.in_color_space == JCS_CMYK) {
- jpeg_set_colorspace(&cinfo, JCS_YCCK);
- cinfo.write_JFIF_header = TRUE;
- }
// Set quality
- if( quality >= 0 && quality <= 100 ) {
- jpeg_set_quality(&cinfo, quality, true);
+ if (priv->quality >= 0 && priv->quality <= 100) {
+ jpeg_set_quality(&priv->cinfo, priv->quality, true);
}
// Use progressive mode
- if( progressive) {
- jpeg_simple_progression(&cinfo);
+ if (priv->progressive) {
+ jpeg_simple_progression(&priv->cinfo);
}
// Get ready for data
- jpeg_start_compress(&cinfo, TRUE);
+ jpeg_start_compress(&priv->cinfo, TRUE);
return true;
}
bool JpegWriter::writePointers(unsigned char **rowPointers, int rowCount)
{
- if (colorMode == JCS_CMYK) {
+ if (priv->format == CMYK) {
for (int y = 0; y < rowCount; y++) {
unsigned char *row = rowPointers[y];
- for (unsigned int x = 0; x < cinfo.image_width; x++) {
+ for (unsigned int x = 0; x < priv->cinfo.image_width; x++) {
for (int n = 0; n < 4; n++) {
*row = 0xff - *row;
row++;
@@ -115,16 +145,16 @@ bool JpegWriter::writePointers(unsigned char **rowPointers, int rowCount)
}
}
// Write all rows to the file
- jpeg_write_scanlines(&cinfo, rowPointers, rowCount);
+ jpeg_write_scanlines(&priv->cinfo, rowPointers, rowCount);
return true;
}
bool JpegWriter::writeRow(unsigned char **rowPointer)
{
- if (colorMode == JCS_CMYK) {
+ if (priv->format == CMYK) {
unsigned char *row = rowPointer[0];
- for (unsigned int x = 0; x < cinfo.image_width; x++) {
+ for (unsigned int x = 0; x < priv->cinfo.image_width; x++) {
for (int n = 0; n < 4; n++) {
*row = 0xff - *row;
row++;
@@ -132,16 +162,22 @@ bool JpegWriter::writeRow(unsigned char **rowPointer)
}
}
// Write the row to the file
- jpeg_write_scanlines(&cinfo, rowPointer, 1);
+ jpeg_write_scanlines(&priv->cinfo, rowPointer, 1);
return true;
}
bool JpegWriter::close()
{
- jpeg_finish_compress(&cinfo);
+ jpeg_finish_compress(&priv->cinfo);
return true;
}
+bool JpegWriter::supportCMYK()
+{
+ return priv->format == CMYK;
+}
+
+
#endif
diff --git a/goo/JpegWriter.h b/goo/JpegWriter.h
index d451bf3..66b8959 100644
--- a/goo/JpegWriter.h
+++ b/goo/JpegWriter.h
@@ -24,15 +24,19 @@
#include <sys/types.h>
#include "ImgWriter.h"
-extern "C" {
-#include <jpeglib.h>
-}
+struct JpegWriterPrivate;
class JpegWriter : public ImgWriter
{
public:
- JpegWriter(int quality, bool progressive, J_COLOR_SPACE colorMode = JCS_RGB);
- JpegWriter(J_COLOR_SPACE colorMode = JCS_RGB);
+ /* RGB - 3 bytes/pixel
+ * GRAY - 1 byte/pixel
+ * CMYK - 4 bytes/pixel
+ */
+ enum Format { RGB, GRAY, CMYK };
+
+ JpegWriter(int quality, bool progressive, Format format = RGB);
+ JpegWriter(Format format = RGB);
~JpegWriter();
bool init(FILE *f, int width, int height, int hDPI, int vDPI);
@@ -41,14 +45,13 @@ public:
bool writeRow(unsigned char **row);
bool close();
- bool supportCMYK() { return colorMode == JCS_CMYK; }
+ bool supportCMYK();
private:
- bool progressive;
- int quality;
- J_COLOR_SPACE colorMode;
- struct jpeg_compress_struct cinfo;
- struct jpeg_error_mgr jerr;
+ JpegWriter(const JpegWriter &other);
+ JpegWriter& operator=(const JpegWriter &other);
+
+ JpegWriterPrivate *priv;
};
#endif
diff --git a/splash/SplashBitmap.cc b/splash/SplashBitmap.cc
index ca1dee7..93d2da8 100644
--- a/splash/SplashBitmap.cc
+++ b/splash/SplashBitmap.cc
@@ -363,7 +363,7 @@ SplashError SplashBitmap::writeImgFile(SplashImageFileFormat format, FILE *f, in
#ifdef ENABLE_LIBJPEG
#ifdef SPLASH_CMYK
case splashFormatJpegCMYK:
- writer = new JpegWriter(JCS_CMYK);
+ writer = new JpegWriter(JpegWriter::CMYK);
break;
#endif
case splashFormatJpeg:
diff --git a/utils/pdftocairo.cc b/utils/pdftocairo.cc
index 258d939..192d295 100644
--- a/utils/pdftocairo.cc
+++ b/utils/pdftocairo.cc
@@ -293,9 +293,9 @@ void writePageImage(GooString *filename)
} else if (jpeg) {
#if ENABLE_LIBJPEG
if (gray)
- writer = new JpegWriter(JCS_GRAYSCALE);
+ writer = new JpegWriter(JpegWriter::GRAY);
else
- writer = new JpegWriter(JCS_RGB);
+ writer = new JpegWriter(JpegWriter::RGB);
#endif
} else if (tiff) {
#if ENABLE_LIBTIFF
commit e78dbb1b7dbd20c3ae547b02270ab0648c1bfc61
Author: Adrian Johnson <ajohnson at redneon.com>
Date: Sun Dec 2 09:10:32 2012 +1030
Reformat goo/*Writer files to poppler style
diff --git a/goo/ImgWriter.h b/goo/ImgWriter.h
index 185c230..8feb351 100644
--- a/goo/ImgWriter.h
+++ b/goo/ImgWriter.h
@@ -16,18 +16,18 @@
#define IMGWRITER_H
#include <stdio.h>
-
+
class ImgWriter
{
- public:
- virtual ~ImgWriter();
- virtual bool init(FILE *f, int width, int height, int hDPI, int vDPI) = 0;
-
- virtual bool writePointers(unsigned char **rowPointers, int rowCount) = 0;
- virtual bool writeRow(unsigned char **row) = 0;
-
- virtual bool close() = 0;
- virtual bool supportCMYK() { return false; }
+public:
+ virtual ~ImgWriter();
+ virtual bool init(FILE *f, int width, int height, int hDPI, int vDPI) = 0;
+
+ virtual bool writePointers(unsigned char **rowPointers, int rowCount) = 0;
+ virtual bool writeRow(unsigned char **row) = 0;
+
+ virtual bool close() = 0;
+ virtual bool supportCMYK() { return false; }
};
#endif
diff --git a/goo/JpegWriter.cc b/goo/JpegWriter.cc
index 2fe3d32..fc9557f 100644
--- a/goo/JpegWriter.cc
+++ b/goo/JpegWriter.cc
@@ -19,129 +19,129 @@
void outputMessage(j_common_ptr cinfo)
{
- char buffer[JMSG_LENGTH_MAX];
+ char buffer[JMSG_LENGTH_MAX];
- // Create the message
- (*cinfo->err->format_message) (cinfo, buffer);
+ // Create the message
+ (*cinfo->err->format_message) (cinfo, buffer);
- // Send it to poppler's error handler
- error(errInternal, -1, "{0:s}", buffer);
+ // Send it to poppler's error handler
+ error(errInternal, -1, "{0:s}", buffer);
}
JpegWriter::JpegWriter(int q, bool p, J_COLOR_SPACE cm)
-: progressive(p), quality(q), colorMode(cm)
+ : progressive(p), quality(q), colorMode(cm)
{
}
JpegWriter::JpegWriter(J_COLOR_SPACE cm)
-: progressive(false), quality(-1), colorMode(cm)
+ : progressive(false), quality(-1), colorMode(cm)
{
}
JpegWriter::~JpegWriter()
{
- // cleanup
- jpeg_destroy_compress(&cinfo);
+ // cleanup
+ jpeg_destroy_compress(&cinfo);
}
bool JpegWriter::init(FILE *f, int width, int height, int hDPI, int vDPI)
{
- // Setup error handler
- cinfo.err = jpeg_std_error(&jerr);
- jerr.output_message = &outputMessage;
-
- // Initialize libjpeg
- jpeg_create_compress(&cinfo);
-
- // Set colorspace and initialise defaults
- cinfo.in_color_space = colorMode; /* colorspace of input image */
- jpeg_set_defaults(&cinfo);
-
- // Set destination file
- jpeg_stdio_dest(&cinfo, f);
-
- // Set libjpeg configuration
- cinfo.image_width = width;
- cinfo.image_height = height;
- cinfo.density_unit = 1; // dots per inch
- cinfo.X_density = hDPI;
- cinfo.Y_density = vDPI;
- /* # of color components per pixel */
- switch (colorMode) {
- case JCS_GRAYSCALE:
- cinfo.input_components = 1;
- break;
- case JCS_RGB:
- cinfo.input_components = 3;
- break;
- case JCS_CMYK:
- cinfo.input_components = 4;
- break;
- default:
- return false;
- }
- if (cinfo.in_color_space == JCS_CMYK) {
- jpeg_set_colorspace(&cinfo, JCS_YCCK);
- cinfo.write_JFIF_header = TRUE;
- }
-
- // Set quality
- if( quality >= 0 && quality <= 100 ) {
- jpeg_set_quality(&cinfo, quality, true);
- }
-
- // Use progressive mode
- if( progressive) {
- jpeg_simple_progression(&cinfo);
- }
-
- // Get ready for data
- jpeg_start_compress(&cinfo, TRUE);
-
- return true;
+ // Setup error handler
+ cinfo.err = jpeg_std_error(&jerr);
+ jerr.output_message = &outputMessage;
+
+ // Initialize libjpeg
+ jpeg_create_compress(&cinfo);
+
+ // Set colorspace and initialise defaults
+ cinfo.in_color_space = colorMode; /* colorspace of input image */
+ jpeg_set_defaults(&cinfo);
+
+ // Set destination file
+ jpeg_stdio_dest(&cinfo, f);
+
+ // Set libjpeg configuration
+ cinfo.image_width = width;
+ cinfo.image_height = height;
+ cinfo.density_unit = 1; // dots per inch
+ cinfo.X_density = hDPI;
+ cinfo.Y_density = vDPI;
+ /* # of color components per pixel */
+ switch (colorMode) {
+ case JCS_GRAYSCALE:
+ cinfo.input_components = 1;
+ break;
+ case JCS_RGB:
+ cinfo.input_components = 3;
+ break;
+ case JCS_CMYK:
+ cinfo.input_components = 4;
+ break;
+ default:
+ return false;
+ }
+ if (cinfo.in_color_space == JCS_CMYK) {
+ jpeg_set_colorspace(&cinfo, JCS_YCCK);
+ cinfo.write_JFIF_header = TRUE;
+ }
+
+ // Set quality
+ if( quality >= 0 && quality <= 100 ) {
+ jpeg_set_quality(&cinfo, quality, true);
+ }
+
+ // Use progressive mode
+ if( progressive) {
+ jpeg_simple_progression(&cinfo);
+ }
+
+ // Get ready for data
+ jpeg_start_compress(&cinfo, TRUE);
+
+ return true;
}
bool JpegWriter::writePointers(unsigned char **rowPointers, int rowCount)
{
- if (colorMode == JCS_CMYK) {
- for (int y = 0; y < rowCount; y++) {
- unsigned char *row = rowPointers[y];
- for (unsigned int x = 0; x < cinfo.image_width; x++) {
- for (int n = 0; n < 4; n++) {
- *row = 0xff - *row;
- row++;
- }
- }
- }
+ if (colorMode == JCS_CMYK) {
+ for (int y = 0; y < rowCount; y++) {
+ unsigned char *row = rowPointers[y];
+ for (unsigned int x = 0; x < cinfo.image_width; x++) {
+ for (int n = 0; n < 4; n++) {
+ *row = 0xff - *row;
+ row++;
}
- // Write all rows to the file
- jpeg_write_scanlines(&cinfo, rowPointers, rowCount);
-
- return true;
+ }
+ }
+ }
+ // Write all rows to the file
+ jpeg_write_scanlines(&cinfo, rowPointers, rowCount);
+
+ return true;
}
bool JpegWriter::writeRow(unsigned char **rowPointer)
{
- if (colorMode == JCS_CMYK) {
- unsigned char *row = rowPointer[0];
- for (unsigned int x = 0; x < cinfo.image_width; x++) {
- for (int n = 0; n < 4; n++) {
- *row = 0xff - *row;
- row++;
- }
- }
- }
- // Write the row to the file
- jpeg_write_scanlines(&cinfo, rowPointer, 1);
-
- return true;
+ if (colorMode == JCS_CMYK) {
+ unsigned char *row = rowPointer[0];
+ for (unsigned int x = 0; x < cinfo.image_width; x++) {
+ for (int n = 0; n < 4; n++) {
+ *row = 0xff - *row;
+ row++;
+ }
+ }
+ }
+ // Write the row to the file
+ jpeg_write_scanlines(&cinfo, rowPointer, 1);
+
+ return true;
}
bool JpegWriter::close()
{
- jpeg_finish_compress(&cinfo);
-
- return true;
+ jpeg_finish_compress(&cinfo);
+
+ return true;
}
#endif
diff --git a/goo/JpegWriter.h b/goo/JpegWriter.h
index d076224..d451bf3 100644
--- a/goo/JpegWriter.h
+++ b/goo/JpegWriter.h
@@ -30,25 +30,25 @@ extern "C" {
class JpegWriter : public ImgWriter
{
- public:
- JpegWriter(int quality, bool progressive, J_COLOR_SPACE colorMode = JCS_RGB);
- JpegWriter(J_COLOR_SPACE colorMode = JCS_RGB);
- ~JpegWriter();
-
- bool init(FILE *f, int width, int height, int hDPI, int vDPI);
-
- bool writePointers(unsigned char **rowPointers, int rowCount);
- bool writeRow(unsigned char **row);
-
- bool close();
- bool supportCMYK() { return colorMode == JCS_CMYK; }
-
- private:
- bool progressive;
- int quality;
- J_COLOR_SPACE colorMode;
- struct jpeg_compress_struct cinfo;
- struct jpeg_error_mgr jerr;
+public:
+ JpegWriter(int quality, bool progressive, J_COLOR_SPACE colorMode = JCS_RGB);
+ JpegWriter(J_COLOR_SPACE colorMode = JCS_RGB);
+ ~JpegWriter();
+
+ bool init(FILE *f, int width, int height, int hDPI, int vDPI);
+
+ bool writePointers(unsigned char **rowPointers, int rowCount);
+ bool writeRow(unsigned char **row);
+
+ bool close();
+ bool supportCMYK() { return colorMode == JCS_CMYK; }
+
+private:
+ bool progressive;
+ int quality;
+ J_COLOR_SPACE colorMode;
+ struct jpeg_compress_struct cinfo;
+ struct jpeg_error_mgr jerr;
};
#endif
diff --git a/goo/PNGWriter.cc b/goo/PNGWriter.cc
index fe8b79e..63ef7c7 100644
--- a/goo/PNGWriter.cc
+++ b/goo/PNGWriter.cc
@@ -26,151 +26,151 @@
PNGWriter::PNGWriter(Format formatA) : format(formatA)
{
- icc_data = NULL;
- icc_data_size = 0;
- icc_name = NULL;
- sRGB_profile = false;
+ icc_data = NULL;
+ icc_data_size = 0;
+ icc_name = NULL;
+ 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);
- }
+ /* cleanup heap allocation */
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+ if (icc_data) {
+ gfree(icc_data);
+ free(icc_name);
+ }
}
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);
+ icc_data = (unsigned char *)gmalloc(size);
+ memcpy(icc_data, data, size);
+ icc_data_size = size;
+ icc_name = strdup(name);
}
void PNGWriter::setSRGBProfile()
{
- sRGB_profile = true;
+ 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)icc_data;
#else
- png_const_bytep icc_data_ptr = (png_const_bytep)icc_data;
+ png_const_bytep icc_data_ptr = (png_const_bytep)icc_data;
#endif
- /* initialize stuff */
- png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
- if (!png_ptr) {
- error(errInternal, -1, "png_create_write_struct failed");
- return false;
- }
-
- info_ptr = png_create_info_struct(png_ptr);
- if (!info_ptr) {
- error(errInternal, -1, "png_create_info_struct failed");
- return false;
- }
-
- if (setjmp(png_jmpbuf(png_ptr))) {
- error(errInternal, -1, "png_jmpbuf failed");
- return false;
- }
-
- /* write header */
- png_init_io(png_ptr, f);
- if (setjmp(png_jmpbuf(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);
-
- // Silence silly gcc
- png_byte bit_depth = -1;
- png_byte color_type = -1;
- switch (format) {
- case RGB:
- bit_depth = 8;
- color_type = PNG_COLOR_TYPE_RGB;
- break;
- case RGBA:
- bit_depth = 8;
- color_type = PNG_COLOR_TYPE_RGB_ALPHA;
- break;
- case GRAY:
- bit_depth = 8;
- color_type = PNG_COLOR_TYPE_GRAY;
- break;
- case MONOCHROME:
- bit_depth = 1;
- color_type = PNG_COLOR_TYPE_GRAY;
- break;
- }
- 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_pHYs(png_ptr, 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);
-
- png_write_info(png_ptr, info_ptr);
- if (setjmp(png_jmpbuf(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);
-
- return true;
+ /* initialize stuff */
+ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if (!png_ptr) {
+ error(errInternal, -1, "png_create_write_struct failed");
+ return false;
+ }
+
+ info_ptr = png_create_info_struct(png_ptr);
+ if (!info_ptr) {
+ error(errInternal, -1, "png_create_info_struct failed");
+ return false;
+ }
+
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ error(errInternal, -1, "png_jmpbuf failed");
+ return false;
+ }
+
+ /* write header */
+ png_init_io(png_ptr, f);
+ if (setjmp(png_jmpbuf(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);
+
+ // Silence silly gcc
+ png_byte bit_depth = -1;
+ png_byte color_type = -1;
+ switch (format) {
+ case RGB:
+ bit_depth = 8;
+ color_type = PNG_COLOR_TYPE_RGB;
+ break;
+ case RGBA:
+ bit_depth = 8;
+ color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+ break;
+ case GRAY:
+ bit_depth = 8;
+ color_type = PNG_COLOR_TYPE_GRAY;
+ break;
+ case MONOCHROME:
+ bit_depth = 1;
+ color_type = PNG_COLOR_TYPE_GRAY;
+ break;
+ }
+ 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_pHYs(png_ptr, 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);
+
+ png_write_info(png_ptr, info_ptr);
+ if (setjmp(png_jmpbuf(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);
+
+ return true;
}
bool PNGWriter::writePointers(unsigned char **rowPointers, int rowCount)
{
- png_write_image(png_ptr, rowPointers);
- /* write bytes */
- if (setjmp(png_jmpbuf(png_ptr))) {
- error(errInternal, -1, "Error during writing bytes");
- return false;
- }
-
- return true;
+ png_write_image(png_ptr, rowPointers);
+ /* write bytes */
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ error(errInternal, -1, "Error during writing bytes");
+ return false;
+ }
+
+ return true;
}
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))) {
- error(errInternal, -1, "error during png row write");
- return false;
- }
-
- return true;
+ // Write the row to the file
+ png_write_rows(png_ptr, row, 1);
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ error(errInternal, -1, "error during png row write");
+ return false;
+ }
+
+ return true;
}
bool PNGWriter::close()
{
- /* end write */
- png_write_end(png_ptr, info_ptr);
- if (setjmp(png_jmpbuf(png_ptr))) {
- error(errInternal, -1, "Error during end of write");
- return false;
- }
-
- return true;
+ /* end write */
+ png_write_end(png_ptr, info_ptr);
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ error(errInternal, -1, "Error during end of write");
+ return false;
+ }
+
+ return true;
}
#endif
diff --git a/goo/PNGWriter.h b/goo/PNGWriter.h
index 9e412b4..37df427 100644
--- a/goo/PNGWriter.h
+++ b/goo/PNGWriter.h
@@ -25,37 +25,37 @@
class PNGWriter : public ImgWriter
{
- public:
-
- /* RGB - 3 bytes/pixel
- * RGBA - 4 bytes/pixel
- * GRAY - 1 byte/pixel
- * MONOCHROME - 1 byte/pixel. PNGWriter will bitpack to 8 pixels/byte
- */
- enum Format { RGB, RGBA, GRAY, MONOCHROME };
-
- PNGWriter(Format format = RGB);
- ~PNGWriter();
-
- void setICCProfile(const char *name, unsigned char *data, int size);
- void setSRGBProfile();
-
-
- bool init(FILE *f, int width, int height, int hDPI, int vDPI);
-
- bool writePointers(unsigned char **rowPointers, int rowCount);
- bool writeRow(unsigned char **row);
-
- bool close();
-
- private:
- Format format;
- png_structp png_ptr;
- png_infop info_ptr;
- unsigned char *icc_data;
- int icc_data_size;
- char *icc_name;
- bool sRGB_profile;
+public:
+
+ /* RGB - 3 bytes/pixel
+ * RGBA - 4 bytes/pixel
+ * GRAY - 1 byte/pixel
+ * MONOCHROME - 1 byte/pixel. PNGWriter will bitpack to 8 pixels/byte
+ */
+ enum Format { RGB, RGBA, GRAY, MONOCHROME };
+
+ PNGWriter(Format format = RGB);
+ ~PNGWriter();
+
+ void setICCProfile(const char *name, unsigned char *data, int size);
+ void setSRGBProfile();
+
+
+ bool init(FILE *f, int width, int height, int hDPI, int vDPI);
+
+ bool writePointers(unsigned char **rowPointers, int rowCount);
+ bool writeRow(unsigned char **row);
+
+ bool close();
+
+private:
+ Format format;
+ png_structp png_ptr;
+ png_infop info_ptr;
+ unsigned char *icc_data;
+ int icc_data_size;
+ char *icc_name;
+ bool sRGB_profile;
};
#endif
diff --git a/goo/TiffWriter.cc b/goo/TiffWriter.cc
index 027cd9c..048cea8 100644
--- a/goo/TiffWriter.cc
+++ b/goo/TiffWriter.cc
@@ -107,26 +107,26 @@ bool TiffWriter::init(FILE *openedFile, int width, int height, int hDPI, int vDP
bitspersample = (format == MONOCHROME ? 1 : 8);
switch (format) {
- case MONOCHROME:
- case GRAY:
- samplesperpixel = 1;
- photometric = PHOTOMETRIC_MINISBLACK;
- break;
-
- case RGB:
- samplesperpixel = 3;
- photometric = PHOTOMETRIC_RGB;
- break;
-
- case RGBA_PREMULTIPLIED:
- samplesperpixel = 4;
- photometric = PHOTOMETRIC_RGB;
- break;
-
- case CMYK:
- samplesperpixel = 4;
- photometric = PHOTOMETRIC_SEPARATED;
- break;
+ case MONOCHROME:
+ case GRAY:
+ samplesperpixel = 1;
+ photometric = PHOTOMETRIC_MINISBLACK;
+ break;
+
+ case RGB:
+ samplesperpixel = 3;
+ photometric = PHOTOMETRIC_RGB;
+ break;
+
+ case RGBA_PREMULTIPLIED:
+ samplesperpixel = 4;
+ photometric = PHOTOMETRIC_RGB;
+ break;
+
+ case CMYK:
+ samplesperpixel = 4;
+ photometric = PHOTOMETRIC_SEPARATED;
+ break;
}
// Open the file
diff --git a/goo/TiffWriter.h b/goo/TiffWriter.h
index 26d0b76..0265c07 100644
--- a/goo/TiffWriter.h
+++ b/goo/TiffWriter.h
@@ -27,35 +27,35 @@ extern "C" {
class TiffWriter : public ImgWriter
{
- public:
- /* 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);
-
- bool init(FILE *openedFile, int width, int height, int hDPI, int vDPI);
-
- bool writePointers(unsigned char **rowPointers, int rowCount);
- bool writeRow(unsigned char **rowData);
-
- bool supportCMYK() { return true; }
-
- bool close();
-
- private:
- 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
+public:
+ /* 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);
+
+ bool init(FILE *openedFile, int width, int height, int hDPI, int vDPI);
+
+ bool writePointers(unsigned char **rowPointers, int rowCount);
+ bool writeRow(unsigned char **rowData);
+
+ bool supportCMYK() { return true; }
+
+ bool close();
+
+private:
+ 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
};
More information about the poppler
mailing list