[PATCH 3/9] png: Add additional pixel formats

Adrian Johnson ajohnson at redneon.com
Wed Aug 10 02:18:15 PDT 2011


RGBA is required for images with transparency. GRAY and MONOCHROME
allow PNGWriter write more compact PNG files when the images is known
to be all gray or monochrome.
---
 goo/PNGWriter.cc |   30 ++++++++++++++++++++++++++----
 goo/PNGWriter.h  |   13 ++++++++++++-
 2 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/goo/PNGWriter.cc b/goo/PNGWriter.cc
index 400c515..7883c56 100644
--- a/goo/PNGWriter.cc
+++ b/goo/PNGWriter.cc
@@ -21,7 +21,7 @@
 
 #include "poppler/Error.h"
 
-PNGWriter::PNGWriter()
+PNGWriter::PNGWriter(Format formatA) : format(formatA)
 {
 }
 
@@ -61,8 +61,26 @@ bool PNGWriter::init(FILE *f, int width, int height, int hDPI, int vDPI)
 	// Set up the type of PNG image and the compression level
 	png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
 
-	png_byte bit_depth = 8;
-	png_byte color_type = PNG_COLOR_TYPE_RGB;
+	png_byte bit_depth;
+	png_byte color_type;
+	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);
@@ -74,7 +92,11 @@ bool PNGWriter::init(FILE *f, int width, int height, int hDPI, int vDPI)
 		error(-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;
 }
 
diff --git a/goo/PNGWriter.h b/goo/PNGWriter.h
index 0f79bf9..dc06e1c 100644
--- a/goo/PNGWriter.h
+++ b/goo/PNGWriter.h
@@ -23,10 +23,20 @@
 #include <png.h>
 #include "ImgWriter.h"
 
+
+
 class PNGWriter : public ImgWriter
 {
 	public:
-		PNGWriter();
+
+		/* 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();
 		
 		bool init(FILE *f, int width, int height, int hDPI, int vDPI);
@@ -37,6 +47,7 @@ class PNGWriter : public ImgWriter
 		bool close();
 	
 	private:
+		Format format;
 		png_structp png_ptr;
 		png_infop info_ptr;
 };
-- 
1.7.4.1


--------------010201060309050309080107
Content-Type: text/x-patch;
 name="0004-png-add-support-for-embedding-ICC-profile.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="0004-png-add-support-for-embedding-ICC-profile.patch"



More information about the poppler mailing list