From 152b7bc52fccc78f547ec4604e0179482f4fc278 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Thu, 29 Nov 2012 23:49:24 +1030 Subject: [PATCH] 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 --- goo/JpegWriter.cc | 106 ++++++++++++++++++++++++++++++------------------ goo/JpegWriter.h | 21 ++++++---- splash/SplashBitmap.cc | 2 +- utils/pdftocairo.cc | 4 +- 4 files changed, 81 insertions(+), 52 deletions(-) diff --git a/goo/JpegWriter.cc b/goo/JpegWriter.cc index 2fe3d32..115fa39 100644 --- a/goo/JpegWriter.cc +++ b/goo/JpegWriter.cc @@ -15,8 +15,17 @@ #ifdef ENABLE_LIBJPEG +extern "C" { +#include +} + #include "poppler/Error.h" +struct JpegWriterPrivate { + struct jpeg_compress_struct cinfo; + struct jpeg_error_mgr jerr; +}; + void outputMessage(j_common_ptr cinfo) { char buffer[JMSG_LENGTH_MAX]; @@ -28,85 +37,102 @@ 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) +: progressive(p), quality(q), format(formatA) { + priv = new JpegWriterPrivate; } -JpegWriter::JpegWriter(J_COLOR_SPACE cm) -: progressive(false), quality(-1), colorMode(cm) +JpegWriter::JpegWriter(Format formatA) +: progressive(false), quality(-1), format(formatA) { + priv = new JpegWriterPrivate; } 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) { + if (!priv) + return false; + // 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 (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 (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); + jpeg_set_quality(&priv->cinfo, quality, true); } // Use progressive mode if( progressive) { - jpeg_simple_progression(&cinfo); + 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 (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 +141,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 (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,14 +158,14 @@ 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; } diff --git a/goo/JpegWriter.h b/goo/JpegWriter.h index d076224..3b7d6e0 100644 --- a/goo/JpegWriter.h +++ b/goo/JpegWriter.h @@ -24,15 +24,19 @@ #include #include "ImgWriter.h" -extern "C" { -#include -} +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 @@ class JpegWriter : public ImgWriter bool writeRow(unsigned char **row); bool close(); - bool supportCMYK() { return colorMode == JCS_CMYK; } + bool supportCMYK() { return format == CMYK; } private: bool progressive; int quality; - J_COLOR_SPACE colorMode; - struct jpeg_compress_struct cinfo; - struct jpeg_error_mgr jerr; + Format format; + JpegWriterPrivate *priv; }; #endif diff --git a/splash/SplashBitmap.cc b/splash/SplashBitmap.cc index f514dfa..1dc57c6 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 -- 1.7.10.4