From d4e88b87a3dbc3e584bda0854afec1813a0578e8 Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Sun, 18 Aug 2013 16:08:02 +0930 Subject: [PATCH 2/5] Change PNGWriter monochrome format to be 8 pixels/byte to be consistent with TiffWriter and NetPBMWriter --- goo/PNGWriter.cc | 4 ---- goo/PNGWriter.h | 2 +- utils/HtmlOutputDev.cc | 31 +++++++++++++++++++------------ utils/ImageOutputDev.cc | 9 ++++++--- utils/pdftocairo.cc | 2 +- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/goo/PNGWriter.cc b/goo/PNGWriter.cc index b775600..4370f28 100644 --- a/goo/PNGWriter.cc +++ b/goo/PNGWriter.cc @@ -147,10 +147,6 @@ bool PNGWriter::init(FILE *f, int width, int height, int hDPI, int vDPI) return false; } - // pack 1 pixel/byte rows into 8 pixels/byte - if (priv->format == MONOCHROME) - png_set_packing(priv->png_ptr); - return true; } diff --git a/goo/PNGWriter.h b/goo/PNGWriter.h index ac8f95a..64b8833 100644 --- a/goo/PNGWriter.h +++ b/goo/PNGWriter.h @@ -31,7 +31,7 @@ public: /* RGB - 3 bytes/pixel * RGBA - 4 bytes/pixel * GRAY - 1 byte/pixel - * MONOCHROME - 1 byte/pixel. PNGWriter will bitpack to 8 pixels/byte + * MONOCHROME - 8 pixels/byte */ enum Format { RGB, RGBA, GRAY, MONOCHROME }; diff --git a/utils/HtmlOutputDev.cc b/utils/HtmlOutputDev.cc index 7926674..3926178 100644 --- a/utils/HtmlOutputDev.cc +++ b/utils/HtmlOutputDev.cc @@ -1413,32 +1413,39 @@ void HtmlOutputDev::drawPngImage(GfxState *state, Stream *str, int width, int he delete imgStr; } else { // isMask == true - ImageStream *imgStr = new ImageStream(str, width, 1, 1); - imgStr->reset(); + int size = (width + 7)/8; + + // PDF masks use 0 = draw current color, 1 = leave unchanged. + // We invert this to provide the standard interpretation of alpha + // (0 = transparent, 1 = opaque). If the colorMap already inverts + // the mask we leave the data unchanged. + int invert_bits = 0xff; + if (colorMap) { + GfxGray gray; + Guchar zero = 0; + colorMap->getGray(&zero, &gray); + if (colToByte(gray) == 0) + invert_bits = 0x00; + } - Guchar *png_row = (Guchar *)gmalloc( width ); + str->reset(); + Guchar *png_row = (Guchar *)gmalloc(size); for (int ri = 0; ri < height; ++ri) { - // read the row of the mask - Guchar *bit_row = imgStr->getLine(); - - // invert for PNG - for(int i = 0; i < width; i++) - png_row[i] = bit_row[i] ? 0xff : 0x00; + for(int i = 0; i < size; i++) + png_row[i] = str->getChar() ^ invert_bits; if (!writer->writeRow( &png_row )) { error(errIO, -1, "Failed to write into PNG '%s'", fName->getCString()); delete writer; fclose(f1); - delete imgStr; gfree(png_row); return; } } - imgStr->close(); - delete imgStr; + str->close(); gfree(png_row); } diff --git a/utils/ImageOutputDev.cc b/utils/ImageOutputDev.cc index 897c116..bd8dc35 100644 --- a/utils/ImageOutputDev.cc +++ b/utils/ImageOutputDev.cc @@ -309,7 +309,7 @@ void ImageOutputDev::writeImageFile(ImgWriter *writer, ImageFormat format, const GfxRGB rgb; GfxGray gray; Guchar zero = 0; - int invert_bits = 0xff; + int invert_bits; setFilename(ext); ++imgNum; @@ -335,8 +335,11 @@ void ImageOutputDev::writeImageFile(ImgWriter *writer, ImageFormat format, const row = (unsigned char *) gmallocn(width, sizeof(unsigned int)); - // if 0 comes out as 0 in the color map, the we _flip_ stream bits - // otherwise we pass through stream bits unmolested + // PDF masks use 0 = draw current color, 1 = leave unchanged. + // We invert this to provide the standard interpretation of alpha + // (0 = transparent, 1 = opaque). If the colorMap already inverts + // the mask we leave the data unchanged. + invert_bits = 0xff; if (colorMap) { colorMap->getGray(&zero, &gray); if (colToByte(gray) == 0) diff --git a/utils/pdftocairo.cc b/utils/pdftocairo.cc index 192d295..841c388 100644 --- a/utils/pdftocairo.cc +++ b/utils/pdftocairo.cc @@ -370,7 +370,7 @@ 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; - if (tiff && mono) { + if (mono) { if (bit == 7) *rowp = 0; if (y > 127) -- 1.8.1.2