Commit e78dbb1b authored by Adrian Johnson's avatar Adrian Johnson
Browse files

Reformat goo/*Writer files to poppler style

parent 1bfe4b22
......@@ -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
......@@ -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
......@@ -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
......
......@@ -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
......@@ -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);