about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2011-04-16 21:22:04 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2011-04-16 21:22:04 +0000
commit99a59c83cf845bc2859589f59ca709b92268df11 (patch)
treede5579403a6be65149900247bf80174d3435a6f3
parent647dc5f9e0d5e89dc6ec3f75a9b107da50156a1e (diff)
downloadnetpbm-mirror-99a59c83cf845bc2859589f59ca709b92268df11.tar.gz
netpbm-mirror-99a59c83cf845bc2859589f59ca709b92268df11.tar.xz
netpbm-mirror-99a59c83cf845bc2859589f59ca709b92268df11.zip
Don't directly access private pnginfo members
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1474 9d0c8265-081b-0410-96cb-a4ca84ce46f8
-rw-r--r--converter/other/Makefile4
-rw-r--r--converter/other/pamrgbatopng.c56
-rw-r--r--converter/other/pngtxt.c34
-rw-r--r--converter/other/pngtxt.h10
-rw-r--r--converter/other/pngx.c157
-rw-r--r--converter/other/pngx.h78
-rw-r--r--converter/other/pnmtopng.c348
7 files changed, 434 insertions, 253 deletions
diff --git a/converter/other/Makefile b/converter/other/Makefile
index 4bcaa26e..3bad1996 100644
--- a/converter/other/Makefile
+++ b/converter/other/Makefile
@@ -169,8 +169,8 @@ pnmtopng: %: %.o pngx.o pngtxt.o $(NETPBMLIB) $(LIBOPT)
 	  $(shell $(LIBOPT) $(NETPBMLIB)) \
 	  $(PNGLIB_LIBOPTS) $(MATHLIB) $(LDFLAGS) $(LDLIBS) $(RPATH) $(LADD)
 
-pamrgbatopng: %: %.o $(NETPBMLIB) $(LIBOPT)
-	$(LD) -o $@ $@.o \
+pamrgbatopng: %: %.o pngx.o $(NETPBMLIB) $(LIBOPT)
+	$(LD) -o $@ $@.o pngx.o \
 	  $(shell $(LIBOPT) $(NETPBMLIB)) $(PNGLIB_LIBOPTS) \
 	  $(MATHLIB) $(LDFLAGS) $(LDLIBS) $(RPATH) $(LADD)
 
diff --git a/converter/other/pamrgbatopng.c b/converter/other/pamrgbatopng.c
index 7babf9f9..7a7a397a 100644
--- a/converter/other/pamrgbatopng.c
+++ b/converter/other/pamrgbatopng.c
@@ -1,10 +1,17 @@
-#include <png.h>
 #include <stdio.h>
 #include <stdlib.h>
+/* Due to poor design of libpng, you must not #include <setjmp.h> before
+<png.h>.  Compile failure results.
+*/
+#include <png.h>
 #include <setjmp.h>
 
-#include "pam.h"
+#include "pm_c_util.h"
 #include "mallocvar.h"
+#include "pam.h"
+#include "pngx.h"
+
+
 
 struct cmdlineInfo {
     const char * inputFileName;
@@ -49,7 +56,7 @@ convertPamToPng(const struct pam * const pamP,
 
 static void
 writeRaster(const struct pam * const pamP,
-            png_struct *       const pngP) {
+            struct pngx *      const pngxP) {
     
     tuple * tupleRow;
     png_byte * pngRow;
@@ -66,7 +73,7 @@ writeRaster(const struct pam * const pamP,
             
             convertPamToPng(pamP, tupleRow, pngRow);
             
-            png_write_row(pngP, pngRow);
+            png_write_row(pngxP->png_ptr, pngRow);
         }
         free(pngRow);
     }
@@ -76,46 +83,25 @@ writeRaster(const struct pam * const pamP,
 
 
 static void
-pngErrorHandler(png_struct * const pngP,
-                const char * const message) {
-
-    pm_error("Error generating PNG image.  libpng says: %s", message);
-}
-
-
-
-static void
 writePng(const struct pam * const pamP,
          FILE *             const ofP) {
 
-    png_struct * pngP;
-    png_info * infoP;
-
-    pngP = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
-    if (!pngP)
-        pm_error("Could not allocate png struct.");
+    struct pngx * pngxP;
 
-    png_set_error_fn(pngP, NULL, &pngErrorHandler, NULL);
-
-    infoP = png_create_info_struct(pngP);
-    if (!infoP)
-        pm_error("Could not allocate PNG info structure");
-    else {
-        infoP->width      = pamP->width;
-        infoP->height     = pamP->height;
-        infoP->bit_depth  = 8;
-        infoP->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+    pngx_create(&pngxP, PNGX_WRITE, NULL);
+    
+    pngx_setIhdr(pngxP, pamP->width, pamP->height,
+                 8, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0, 0);
         
-        png_init_io(pngP, ofP);
+    png_init_io(pngxP->png_ptr, ofP);
 
-        png_write_info(pngP, infoP);
+    pngx_writeInfo(pngxP);
         
-        writeRaster(pamP, pngP);
+    writeRaster(pamP, pngxP);
 
-        png_write_end(pngP, infoP);
+    pngx_writeEnd(pngxP);
         
-        png_destroy_write_struct(&pngP, &infoP);
-    }
+    pngx_destroy(pngxP);
 }
     
 
diff --git a/converter/other/pngtxt.c b/converter/other/pngtxt.c
index e35450d7..853edf0a 100644
--- a/converter/other/pngtxt.c
+++ b/converter/other/pngtxt.c
@@ -4,6 +4,7 @@
 #include <png.h>
 
 #include "nstring.h"
+#include "pngx.h"
 #include "pngtxt.h"
 #include "pm.h"
 #include "mallocvar.h"
@@ -240,25 +241,24 @@ handleArrayAllocation(png_text **    const arrayP,
 
 
 void 
-pnmpng_read_text (png_info * const info_ptr, 
-                  FILE *     const tfp, 
-                  bool       const ztxt,
-                  bool       const verbose) {
+pngtxt_read(struct pngx * const pngxP,
+            FILE *        const tfp, 
+            bool          const ztxt,
+            bool          const verbose) {
 
     const char * textline;
     unsigned int lineLength;
     unsigned int commentIdx;
     bool noCommentsYet;
     bool eof;
+    png_textp text;  /* An array; one line per element */
     unsigned int allocatedComments;
-        /* Number of entries currently allocated for the info_ptr->text
-           array 
-        */
+        /* Number of entries currently allocated for the PNG text array */
 
     allocatedComments = 256;  /* initial value */
 
-    MALLOCARRAY(info_ptr->text, allocatedComments);
-    if (info_ptr->text == NULL) 
+    MALLOCARRAY(text, allocatedComments);
+    if (text == NULL) 
         pm_error("unable to allocate memory for comment array");
 
     commentIdx = 0;
@@ -273,7 +273,7 @@ pnmpng_read_text (png_info * const info_ptr,
             if (lineLength == 0) {
                 /* skip this empty line */
             } else {
-                handleArrayAllocation(&info_ptr->text, &allocatedComments,
+                handleArrayAllocation(&text, &allocatedComments,
                                       commentIdx);
                 if ((textline[0] != ' ') && (textline[0] != '\t')) {
                     /* Line doesn't start with white space, which
@@ -285,7 +285,7 @@ pnmpng_read_text (png_info * const info_ptr,
                         ++commentIdx;
                     noCommentsYet = FALSE;
 
-                    startComment(&info_ptr->text[commentIdx], 
+                    startComment(&text[commentIdx], 
                                  textline, lineLength, ztxt);
                 } else {
                     /* Line starts with whitespace, which means it is
@@ -295,20 +295,20 @@ pnmpng_read_text (png_info * const info_ptr,
                         pm_error("Invalid comment file format: "
                                  "first line is a continuation line! "
                                  "(It starts with whitespace)");
-                    continueComment(&info_ptr->text[commentIdx], 
+                    continueComment(&text[commentIdx], 
                                     textline, lineLength);
                 }
             }
             pm_strfree(textline);
         }
     } 
-    if (noCommentsYet)
-        info_ptr->num_text = 0;
-    else
-        info_ptr->num_text = commentIdx + 1;
+    if (!noCommentsYet)
+        pngx_setText(pngxP, text, commentIdx + 1);
 
     if (verbose)
-        pm_message("%d comments placed in text chunk", info_ptr->num_text);
+        pm_message("%d comments placed in text chunk", commentIdx + 1);
+
+    free(text);
 }
 
 
diff --git a/converter/other/pngtxt.h b/converter/other/pngtxt.h
index ae7fe2c4..c83303df 100644
--- a/converter/other/pngtxt.h
+++ b/converter/other/pngtxt.h
@@ -4,10 +4,12 @@
 #include "pm_c_util.h"
 #include <png.h>
 
+struct pngx;
+
 void 
-pnmpng_read_text (png_info * const info_ptr, 
-                  FILE *     const tfp, 
-                  bool const ztxt,
-                  bool const verbose);
+pngtxt_read(struct pngx * const pngxP,
+            FILE *        const tfp, 
+            bool          const ztxt,
+            bool          const verbose);
 
 #endif
diff --git a/converter/other/pngx.c b/converter/other/pngx.c
index f9d2491f..f1afd39e 100644
--- a/converter/other/pngx.c
+++ b/converter/other/pngx.c
@@ -144,3 +144,160 @@ pngx_writeEnd(struct pngx * const pngxP) {
 
 
 
+png_byte
+pngx_colorType(struct pngx * const pngxP) {
+
+    return png_get_color_type(pngxP->png_ptr, pngxP->info_ptr);
+}
+
+
+
+void
+pngx_setGama(struct pngx * const pngxP,
+             float         const fileGamma) {
+
+    png_set_gAMA(pngxP->png_ptr, pngxP->info_ptr, fileGamma);
+}
+
+
+
+void
+pngx_setChrm(struct pngx *      const pngxP,
+             struct pngx_chroma const chroma) {
+
+    png_set_cHRM(pngxP->png_ptr, pngxP->info_ptr, 
+                 chroma.wx, chroma.wy,
+                 chroma.rx, chroma.ry,
+                 chroma.gx, chroma.gy,
+                 chroma.bx, chroma.by);
+}
+
+
+
+void
+pngx_setPhys(struct pngx *    const pngxP,
+             struct pngx_phys const phys) {
+
+    png_set_pHYs(pngxP->png_ptr, pngxP->info_ptr, 
+                 phys.x, phys.y, phys.unit);
+}
+
+
+
+void
+pngx_setTime(struct pngx * const pngxP,
+             png_time      const timeArg) {
+
+    png_time time;
+
+    time = timeArg;
+
+    png_set_tIME(pngxP->png_ptr, pngxP->info_ptr, &time);
+}
+
+
+
+void
+pngx_setSbit(struct pngx * const pngxP,
+             png_color_8   const sbitArg) {
+
+    png_color_8 sbit;
+
+    sbit = sbitArg;
+
+    png_set_sBIT(pngxP->png_ptr, pngxP->info_ptr, &sbit);
+}
+
+
+
+void
+pngx_setInterlaceHandling(struct pngx * const pngxP) {
+
+    png_set_interlace_handling(pngxP->png_ptr);
+}
+
+
+
+void
+pngx_setPlte(struct pngx * const pngxP,
+             png_color *   const palette,
+             unsigned int  const paletteSize) {
+
+    png_set_PLTE(pngxP->png_ptr, pngxP->info_ptr, palette, paletteSize);
+}
+
+
+
+void
+pngx_setTrnsPalette(struct pngx *    const pngxP,
+                    const png_byte * const transPalette,
+                    unsigned int     const paletteSize) {
+
+    png_set_tRNS(pngxP->png_ptr, pngxP->info_ptr,
+                 (png_byte *)transPalette, paletteSize, NULL);
+}
+
+
+
+void
+pngx_setTrnsValue(struct pngx * const pngxP,
+                  png_color_16  const transColorArg) {
+
+    png_color_16 transColor;
+
+    transColor = transColorArg;
+
+    png_set_tRNS(pngxP->png_ptr, pngxP->info_ptr,
+                 NULL, 0, &transColor);
+}
+
+
+
+void
+pngx_setHist(struct pngx * const pngxP,
+             png_uint_16 * const histogram) {
+
+    png_set_hIST(pngxP->png_ptr, pngxP->info_ptr, histogram);
+}
+
+
+
+struct pngx_trans
+pngx_getTrns(struct pngx * const pngxP) {
+
+    struct pngx_trans retval;
+
+    png_get_tRNS(pngxP->png_ptr, pngxP->info_ptr,
+                 &retval.trans, &retval.numTrans, &retval.transColorP);
+
+    return retval;
+}
+
+
+
+void
+pngx_setBkgdPalette(struct pngx * const pngxP,
+                    unsigned int  const backgroundIndex) {
+
+    png_color_16 background;
+
+    background.index = backgroundIndex;
+
+    png_set_bKGD(pngxP->png_ptr, pngxP->info_ptr, &background);
+}
+
+
+
+void
+pngx_setBkgdRgb(struct pngx * const pngxP,
+                png_color_16  const backgroundArg) {
+
+    png_color_16 background;
+
+    background = backgroundArg;
+
+    png_set_bKGD(pngxP->png_ptr, pngxP->info_ptr, &background);
+}
+
+
+
diff --git a/converter/other/pngx.h b/converter/other/pngx.h
index f9658c58..930b84c4 100644
--- a/converter/other/pngx.h
+++ b/converter/other/pngx.h
@@ -8,6 +8,29 @@
    the PNG library easier and cleaner.
 */
 
+struct pngx_chroma {
+    float wx;
+    float wy;
+    float rx;
+    float ry;
+    float gx;
+    float gy;
+    float bx;
+    float by;
+};
+
+struct pngx_phys {
+    int x;
+    int y;
+    int unit;
+};
+
+struct pngx_trans {
+    png_bytep trans;
+    int numTrans;
+    png_color_16 * transColorP;
+};
+
 typedef enum {PNGX_READ, PNGX_WRITE} pngx_rw;
 
 struct pngx {
@@ -29,6 +52,9 @@ bool
 pngx_chunkIsPresent(struct pngx * const pngxP,
                     uint32_t      const chunkType);
 
+png_byte
+pngx_colorType(struct pngx * const pngxP);
+
 void
 pngx_setText(struct pngx * const pngxP,
              png_textp     const textP,
@@ -45,6 +71,58 @@ pngx_setIhdr(struct pngx * const pngxP,
              int           const filterMethod);
 
 void
+pngx_setGama(struct pngx * const pngxP,
+             float         const fileGamma);
+
+void
+pngx_setChrm(struct pngx *      const pngxP,
+             struct pngx_chroma const chroma);
+
+void
+pngx_setPhys(struct pngx *    const pngxP,
+             struct pngx_phys const phys);
+
+void
+pngx_setTime(struct pngx * const pngxP,
+             png_time      const time);
+
+void
+pngx_setSbit(struct pngx * const pngxP,
+             png_color_8   const sbit);
+
+void
+pngx_setInterlaceHandling(struct pngx * const pngxP);
+
+void
+pngx_setPlte(struct pngx * const pngxP,
+             png_color *   const palette,
+             unsigned int  const paletteSize);
+
+void
+pngx_setTrnsPalette(struct pngx *    const pngxP,
+                    const png_byte * const transPalette,
+                    unsigned int     const paletteSize);
+
+void
+pngx_setTrnsValue(struct pngx * const pngxP,
+                  png_color_16  const transColorArg);
+
+void
+pngx_setHist(struct pngx * const pngxP,
+             png_uint_16 * const histogram);
+
+struct pngx_trans
+pngx_getTrns(struct pngx * const pngxP);
+
+void
+pngx_setBkgdPalette(struct pngx * const pngxP,
+                    unsigned int  const backgroundIndex);
+
+void
+pngx_setBkgdRgb(struct pngx * const pngxP,
+                png_color_16  const backgroundArg);
+
+void
 pngx_writeInfo(struct pngx * const pngxP);
 
 void
diff --git a/converter/other/pnmtopng.c b/converter/other/pnmtopng.c
index 5de3fdc1..e154c921 100644
--- a/converter/other/pnmtopng.c
+++ b/converter/other/pnmtopng.c
@@ -58,7 +58,12 @@
 #include <assert.h>
 #include <string.h> /* strcat() */
 #include <limits.h>
-#include <png.h>    /* includes zlib.h and setjmp.h */
+#include <png.h>
+/* Due to a design error in png.h, you must not #include <setjmp.h> before
+   <png.h>.  If you do, png.h won't compile.
+*/
+#include <setjmp.h> 
+#include <zlib.h>
 
 #include "pm_c_util.h"
 #include "pnm.h"
@@ -88,23 +93,6 @@ struct zlibCompression {
     unsigned int buffer_size;
 };
 
-struct chroma {
-    float wx;
-    float wy;
-    float rx;
-    float ry;
-    float gx;
-    float gy;
-    float bx;
-    float by;
-};
-
-struct phys {
-    int x;
-    int y;
-    int unit;
-};
-
 typedef struct cahitem {
     xel color;
     gray alpha;
@@ -129,9 +117,9 @@ struct cmdlineInfo {
     float         gamma;        /* Meaningless if !gammaSpec */
     unsigned int  hist;
     unsigned int  rgbSpec;
-    struct chroma rgb;          /* Meaningless if !rgbSpec */
+    struct pngx_chroma rgb;          /* Meaningless if !rgbSpec */
     unsigned int  sizeSpec;
-    struct phys   size;         /* Meaningless if !sizeSpec */
+    struct pngx_phys   size;         /* Meaningless if !sizeSpec */
     const char *  text;         /* NULL if none */
     const char *  ztxt;         /* NULL if none */
     unsigned int  modtimeSpec;
@@ -176,8 +164,8 @@ static int errorlevel;
 
 
 static void
-parseSizeOpt(const char *  const sizeOpt,
-             struct phys * const sizeP) {
+parseSizeOpt(const char *       const sizeOpt,
+             struct pngx_phys * const sizeP) {
 
     int count;
     
@@ -191,8 +179,8 @@ parseSizeOpt(const char *  const sizeOpt,
 
 
 static void
-parseRgbOpt(const char *    const rgbOpt,
-            struct chroma * const rgbP) {
+parseRgbOpt(const char *         const rgbOpt,
+            struct pngx_chroma * const rgbP) {
 
     int count;
     
@@ -2180,7 +2168,7 @@ makePngLine(png_byte *           const line,
             gray *               const alpha_mask,
             colorhash_table      const cht,
             coloralphahash_table const caht,
-            png_info *           const info_ptr,
+            struct pngx *        const pngxP,
             xelval               const png_maxval,
             unsigned int         const depth) {
             
@@ -2192,20 +2180,20 @@ makePngLine(png_byte *           const line,
         xel p_png;
         xel const p = xelrow[col];
         PPM_DEPTH(p_png, p, maxval, png_maxval);
-        if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY ||
-            info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
+        if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY ||
+            pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY_ALPHA) {
             if (depth == 16)
                 *pp++ = PNM_GET1(p_png) >> 8;
             *pp++ = PNM_GET1(p_png) & 0xff;
-        } else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
+        } else if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_PALETTE) {
             unsigned int paletteIndex;
             if (alpha)
                 paletteIndex = lookupColorAlpha(caht, &p, &alpha_mask[col]);
             else
                 paletteIndex = ppm_lookupcolor(cht, &p);
             *pp++ = paletteIndex;
-        } else if (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
-                   info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
+        } else if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB ||
+                   pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB_ALPHA) {
             if (depth == 16)
                 *pp++ = PPM_GETR(p_png) >> 8;
             *pp++ = PPM_GETR(p_png) & 0xff;
@@ -2218,7 +2206,7 @@ makePngLine(png_byte *           const line,
         } else
             pm_error("INTERNAL ERROR: undefined color_type");
                 
-        if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) {
+        if (pngx_colorType(pngxP) & PNG_COLOR_MASK_ALPHA) {
             int const png_alphaval = (int)
                 alpha_mask[col] * (float) png_maxval / maxval + 0.5;
             if (depth == 16)
@@ -2274,7 +2262,7 @@ writeRaster(struct pngx *        const pngxP,
             
             makePngLine(line, xelrow, cols, maxval,
                         alpha, alpha ? alpha_mask[row] : NULL,
-                        cht, caht, pngxP->info_ptr, png_maxval, depth);
+                        cht, caht, pngxP, png_maxval, depth);
 
             png_write_row(pngxP->png_ptr, line);
         }
@@ -2285,16 +2273,16 @@ writeRaster(struct pngx *        const pngxP,
 
 
 static void
-doHistChunk(bool         const histRequested,
-            pixel        const palettePnm[],
-            FILE *       const ifP,
-            pm_filepos   const rasterPos,
-            unsigned int const cols,
-            unsigned int const rows,
-            xelval       const maxval,
-            int          const format,
-            png_info *   const info_ptr,
-            bool         const verbose) {
+doHistChunk(struct pngx * const pngxP,
+            bool          const histRequested,
+            pixel         const palettePnm[],
+            FILE *        const ifP,
+            pm_filepos    const rasterPos,
+            unsigned int  const cols,
+            unsigned int  const rows,
+            xelval        const maxval,
+            int           const format,
+            bool          const verbose) {
 
     if (histRequested) {
         colorhist_vector chv;
@@ -2324,8 +2312,8 @@ doHistChunk(bool         const histRequested,
                         histogram[i] = chv[chvIndex].value;
                 }
             
-                info_ptr->valid |= PNG_INFO_hIST;
-                info_ptr->hist = histogram;
+                pngx_setHist(pngxP, histogram);
+
                 if (verbose)
                     pm_message("histogram created in PNG stream");
             }
@@ -2337,84 +2325,69 @@ doHistChunk(bool         const histRequested,
 
 
 static void
-setColorType(struct pngx * const pngxP,
-             bool          const colorMapped,
-             int           const pnmType,
-             bool          const alpha) {
+doIhdrChunk(struct pngx * const pngxP,
+            unsigned int  const width,
+            unsigned int  const height,
+            unsigned int  const depth,
+            bool          const colorMapped,
+            int           const pnmType,
+            bool          const alpha) {
+
+    int colorType;
 
     if (colorMapped)
-        pngxP->info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
+        colorType = PNG_COLOR_TYPE_PALETTE;
     else if (pnmType == PPM_TYPE)
-        pngxP->info_ptr->color_type = PNG_COLOR_TYPE_RGB;
+        colorType = PNG_COLOR_TYPE_RGB;
     else
-        pngxP->info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
+        colorType = PNG_COLOR_TYPE_GRAY;
+
+    if (alpha && pngx_colorType(pngxP) != PNG_COLOR_TYPE_PALETTE)
+        colorType |= PNG_COLOR_MASK_ALPHA;
 
-    if (alpha && pngxP->info_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
-        pngxP->info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
+    pngx_setIhdr(pngxP, width, height, depth, colorType, 0, 0, 0);
 }
 
 
 
 static void
 doGamaChunk(struct cmdlineInfo const cmdline,
-            png_info *         const info_ptr) {
+            struct pngx *      const pngxP) {
             
-    if (cmdline.gammaSpec) {
-        /* gAMA chunk */
-        info_ptr->valid |= PNG_INFO_gAMA;
-        info_ptr->gamma = cmdline.gamma;
-    }
+    if (cmdline.gammaSpec)
+        pngx_setGama(pngxP, cmdline.gamma);
 }
 
 
 
 static void
 doChrmChunk(struct cmdlineInfo const cmdline,
-            png_info *         const info_ptr) {
-
-    if (cmdline.rgbSpec) {
-        /* cHRM chunk */
-        info_ptr->valid |= PNG_INFO_cHRM;
-
-        info_ptr->x_white = cmdline.rgb.wx;
-        info_ptr->y_white = cmdline.rgb.wy;
-        info_ptr->x_red   = cmdline.rgb.rx;
-        info_ptr->y_red   = cmdline.rgb.ry;
-        info_ptr->x_green = cmdline.rgb.gx;
-        info_ptr->y_green = cmdline.rgb.gy;
-        info_ptr->x_blue  = cmdline.rgb.bx;
-        info_ptr->y_blue  = cmdline.rgb.by;
-    }
+            struct pngx *      const pngxP) {
+
+    if (cmdline.rgbSpec)
+        pngx_setChrm(pngxP, cmdline.rgb);
 }
 
 
 
 static void
 doPhysChunk(struct cmdlineInfo const cmdline,
-            png_info *         const info_ptr) {
-
-    if (cmdline.sizeSpec) {
-        /* pHYS chunk */
-        info_ptr->valid |= PNG_INFO_pHYs;
+            struct pngx *      const pngxP) {
 
-        info_ptr->x_pixels_per_unit = cmdline.size.x;
-        info_ptr->y_pixels_per_unit = cmdline.size.y;
-        info_ptr->phys_unit_type    = cmdline.size.unit;
-    }
+    if (cmdline.sizeSpec)
+        pngx_setPhys(pngxP, cmdline.size);
 }
 
 
 
-
 static void
 doTimeChunk(struct cmdlineInfo const cmdline,
-            png_info *         const info_ptr) {
+            struct pngx *      const pngxP) {
 
     if (cmdline.modtimeSpec) {
-        /* tIME chunk */
-        info_ptr->valid |= PNG_INFO_tIME;
-
-        png_convert_from_time_t(&info_ptr->mod_time, cmdline.modtime);
+        png_time pngTime;
+        png_convert_from_time_t(&pngTime, cmdline.modtime);
+        pngx_setTime(pngxP, pngTime);
     }
 }
 
@@ -2424,19 +2397,14 @@ static void
 reportTrans(struct pngx * const pngxP) {
 
     if (pngx_chunkIsPresent(pngxP, PNG_INFO_tRNS)) {
-        png_bytep trans;
-        int numTrans;
-        png_color_16 * transColorP;
-
-        png_get_tRNS(pngxP->png_ptr, pngxP->info_ptr,
-                     &trans, &numTrans, &transColorP);
-
+        struct pngx_trans const transInfo = pngx_getTrns(pngxP);
+        
         pm_message("Transparent color {gray, red, green, blue} = "
                    "{%d, %d, %d, %d}",
-                   transColorP->gray,
-                   transColorP->red,
-                   transColorP->green,
-                   transColorP->blue);
+                   transInfo.transColorP->gray,
+                   transInfo.transColorP->red,
+                   transInfo.transColorP->green,
+                   transInfo.transColorP->blue);
     } else
         pm_message("No transparent color");
 }
@@ -2451,23 +2419,17 @@ doTrnsChunk(struct pngx * const pngxP,
             xelval        const maxval,
             xelval        const pngMaxval) {
 
-    switch (pngxP->info_ptr->color_type) {
+    switch (pngx_colorType(pngxP)) {
     case PNG_COLOR_TYPE_PALETTE:
-        if (transPaletteSize > 0) {
-            png_set_tRNS(pngxP->png_ptr, pngxP->info_ptr,
-                         (png_byte *)transPalette,
-                         transPaletteSize /* omit opaque values */,
-                         0);
-        }
+        if (transPaletteSize > 0)
+            pngx_setTrnsPalette(pngxP, transPalette,
+                                transPaletteSize /* omit opaque values */);
         break;
     case PNG_COLOR_TYPE_GRAY:
     case PNG_COLOR_TYPE_RGB:
-        if (transparent > 0) {
-            png_color_16 pngTransColor = 
-                xelToPngColor_16(transColor, maxval, pngMaxval);
-            png_set_tRNS(pngxP->png_ptr, pngxP->info_ptr,
-                         NULL, 0, &pngTransColor);
-        }
+        if (transparent > 0)
+            pngx_setTrnsValue(pngxP,
+                              xelToPngColor_16(transColor, maxval, pngMaxval));
         break;
     default:
         /* This is PNG_COLOR_MASK_ALPHA.  Transparency will be handled
@@ -2482,28 +2444,28 @@ doTrnsChunk(struct pngx * const pngxP,
 
 
 static void
-doBkgdChunk(bool         const bkgdRequested,
-            png_info *   const info_ptr,
-            unsigned int const backgroundIndex,
-            pixel        const backColor,
-            xelval       const maxval,
-            xelval       const pngMaxval,
-            bool         const verbose) {
+doBkgdChunk(struct pngx * const pngxP,
+            bool          const bkgdRequested,
+            unsigned int  const backgroundIndex,
+            pixel         const backColor,
+            xelval        const maxval,
+            xelval        const pngMaxval,
+            bool          const verbose) {
     
     if (bkgdRequested) {
-        info_ptr->valid |= PNG_INFO_bKGD;
-        if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
-            info_ptr->background.index = backgroundIndex;
-        } else {
-            info_ptr->background = 
+        if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_PALETTE)
+            pngx_setBkgdPalette(pngxP, backgroundIndex);
+        else {
+            png_color_16 const pngBackground = 
                 xelToPngColor_16(backColor, maxval, pngMaxval);
+            pngx_setBkgdRgb(pngxP, pngBackground);
             if (verbose)
                 pm_message("Writing bKGD chunk with background color "
                            " {gray, red, green, blue} = {%d, %d, %d, %d}",
-                           info_ptr->background.gray, 
-                           info_ptr->background.red, 
-                           info_ptr->background.green, 
-                           info_ptr->background.blue ); 
+                           pngBackground.gray, 
+                           pngBackground.red, 
+                           pngBackground.green, 
+                           pngBackground.blue ); 
         }
     }
 }
@@ -2511,13 +2473,13 @@ doBkgdChunk(bool         const bkgdRequested,
 
 
 static void
-doSbitChunk(png_info * const pngInfoP,
-            xelval     const pngMaxval,
-            xelval     const maxval,
-            bool       const alpha,
-            xelval     const alphaMaxval) {
+doSbitChunk(struct pngx * const pngxP,
+            xelval        const pngMaxval,
+            xelval        const maxval,
+            bool          const alpha,
+            xelval        const alphaMaxval) {
 
-    if (pngInfoP->color_type != PNG_COLOR_TYPE_PALETTE &&
+    if (pngx_colorType(pngxP) != PNG_COLOR_TYPE_PALETTE &&
         (pngMaxval > maxval || (alpha && pngMaxval > alphaMaxval))) {
 
         /* We're writing in a bit depth that doesn't match the maxval
@@ -2536,26 +2498,28 @@ doSbitChunk(png_info * const pngInfoP,
            sBIT chunk.
         */
 
-        pngInfoP->valid |= PNG_INFO_sBIT;
-
         {
             int const sbitval = pm_maxvaltobits(MIN(maxval, pngMaxval));
 
-            if (pngInfoP->color_type & PNG_COLOR_MASK_COLOR) {
-                pngInfoP->sig_bit.red   = sbitval;
-                pngInfoP->sig_bit.green = sbitval;
-                pngInfoP->sig_bit.blue  = sbitval;
+            png_color_8 sbit;
+
+            if (pngx_colorType(pngxP) & PNG_COLOR_MASK_COLOR) {
+                sbit.red   = sbitval;
+                sbit.green = sbitval;
+                sbit.blue  = sbitval;
             } else
-                pngInfoP->sig_bit.gray = sbitval;
+                sbit.gray  = sbitval;
             
             if (verbose)
                 pm_message("Writing sBIT chunk with bits = %d", sbitval);
-        }
-        if (pngInfoP->color_type & PNG_COLOR_MASK_ALPHA) {
-            pngInfoP->sig_bit.alpha =
-                pm_maxvaltobits(MIN(alphaMaxval, pngMaxval));
-            if (verbose)
-                pm_message("  alpha bits = %d", pngInfoP->sig_bit.alpha);
+
+            if (pngx_colorType(pngxP) & PNG_COLOR_MASK_ALPHA) {
+                sbit.alpha = pm_maxvaltobits(MIN(alphaMaxval, pngMaxval));
+                if (verbose)
+                    pm_message("  alpha bits = %d", sbit.alpha);
+            }
+
+            pngx_setSbit(pngxP, sbit);
         }
     }
 }
@@ -2605,19 +2569,19 @@ convertpnm(struct cmdlineInfo const cmdline,
   struct pngx * pngxP;
 
   bool colorMapped;
-  pixel palette_pnm[MAXCOLORS];
+  pixel palettePnm[MAXCOLORS];
   png_color palette[MAXCOLORS];
       /* The color part of the color/alpha palette passed to the PNG
          compressor 
       */
-  unsigned int palette_size;
+  unsigned int paletteSize;
 
-  gray trans_pnm[MAXCOLORS];
+  gray transPnm[MAXCOLORS];
   png_byte  trans[MAXCOLORS];
       /* The alpha part of the color/alpha palette passed to the PNG
          compressor 
       */
-  unsigned int trans_size;
+  unsigned int transSize;
 
   colorhash_table cht;
   coloralphahash_table caht;
@@ -2625,7 +2589,7 @@ convertpnm(struct cmdlineInfo const cmdline,
   unsigned int background_index;
       /* Index into palette[] of the background color. */
 
-  gray alpha_maxval;
+  gray alphaMaxval;
   const char * noColormapReason;
       /* The reason that we shouldn't make a colormapped PNG, or NULL if
          we should.  malloc'ed null-terminated string.
@@ -2646,7 +2610,7 @@ convertpnm(struct cmdlineInfo const cmdline,
   xel *xelrow;    /* malloc'ed */
       /* The row of the input image currently being processed */
 
-  int pnm_type;
+  int pnmType;
   gray ** alpha_mask;
 
   /* We initialize these guys to quiet compiler warnings: */
@@ -2661,29 +2625,29 @@ convertpnm(struct cmdlineInfo const cmdline,
 
   pnm_readpnminit(ifP, &cols, &rows, &maxval, &format);
   pm_tell2(ifP, &rasterPos, sizeof(rasterPos));
-  pnm_type = PNM_FORMAT_TYPE(format);
+  pnmType = PNM_FORMAT_TYPE(format);
 
   xelrow = pnm_allocrow(cols);
 
   if (verbose) {
-    if (pnm_type == PBM_TYPE)    
-      pm_message ("reading a PBM file (maxval=%d)", maxval);
-    else if (pnm_type == PGM_TYPE)    
-      pm_message ("reading a PGM file (maxval=%d)", maxval);
-    else if (pnm_type == PPM_TYPE)    
-      pm_message ("reading a PPM file (maxval=%d)", maxval);
+      if (pnmType == PBM_TYPE)    
+          pm_message ("reading a PBM file");
+      else if (pnmType == PGM_TYPE)    
+          pm_message ("reading a PGM file (maxval=%d)", maxval);
+      else if (pnmType == PPM_TYPE)    
+          pm_message ("reading a PPM file (maxval=%d)", maxval);
   }
 
   determineTransparency(cmdline, ifP, rasterPos, cols, rows, maxval, format,
                         afP,
                         &alpha, &transparent, &transcolor, &transexact,
-                        &alpha_mask, &alpha_maxval);
+                        &alpha_mask, &alphaMaxval);
 
   determineBackground(cmdline, maxval, &backcolor);
 
   /* first of all, check if we have a grayscale image written as PPM */
 
-  if (pnm_type == PPM_TYPE && !cmdline.force) {
+  if (pnmType == PPM_TYPE && !cmdline.force) {
       unsigned int row;
       bool isgray;
 
@@ -2699,7 +2663,7 @@ convertpnm(struct cmdlineInfo const cmdline,
           }
       }
       if (isgray)
-          pnm_type = PGM_TYPE;
+          pnmType = PGM_TYPE;
   }
 
   /* handle `odd' maxvalues */
@@ -2716,8 +2680,8 @@ convertpnm(struct cmdlineInfo const cmdline,
                   cmdline.force, pfP,
                   alpha, transparent >= 0, transcolor, transexact, 
                   !!cmdline.background, backcolor,
-                  alpha_mask, alpha_maxval, pnm_meaningful_bits,
-                  palette_pnm, &palette_size, trans_pnm, &trans_size,
+                  alpha_mask, alphaMaxval, pnm_meaningful_bits,
+                  palettePnm, &paletteSize, transPnm, &transSize,
                   &background_index, &noColormapReason);
 
   if (noColormapReason) {
@@ -2732,18 +2696,18 @@ convertpnm(struct cmdlineInfo const cmdline,
   } else
       colorMapped = TRUE;
   
-  computeColorMapLookupTable(colorMapped, palette_pnm, palette_size,
-                             trans_pnm, trans_size, alpha, alpha_maxval,
+  computeColorMapLookupTable(colorMapped, palettePnm, paletteSize,
+                             transPnm, transSize, alpha, alphaMaxval,
                              &cht, &caht);
 
-  computeRasterWidth(colorMapped, palette_size, pnm_type, 
+  computeRasterWidth(colorMapped, paletteSize, pnmType, 
                      pnm_meaningful_bits, alpha,
                      &depth, &fulldepth);
   if (verbose)
     pm_message ("writing a%s %d-bit %s%s file%s",
                 fulldepth == 8 ? "n" : "", fulldepth,
                 colorMapped ? "palette": 
-                (pnm_type == PPM_TYPE ? "RGB" : "gray"),
+                (pnmType == PPM_TYPE ? "RGB" : "gray"),
                 alpha ? (colorMapped ? "+transparency" : "+alpha") : "",
                 cmdline.interlace ? " (interlaced)" : "");
 
@@ -2755,50 +2719,44 @@ convertpnm(struct cmdlineInfo const cmdline,
     pm_error ("setjmp returns error condition (2)");
   }
 
-  pngxP->info_ptr->width = cols;
-  pngxP->info_ptr->height = rows;
-  pngxP->info_ptr->bit_depth = depth;
-
-  setColorType(pngxP, colorMapped, pnm_type, alpha);
+  doIhdrChunk(pngxP, cols, rows, depth, colorMapped, pnmType, alpha);
 
-  pngxP->info_ptr->interlace_type = cmdline.interlace;
+  if (cmdline.interlace)
+      pngx_setInterlaceHandling(pngxP);
 
-  doGamaChunk(cmdline, pngxP->info_ptr);
+  doGamaChunk(cmdline, pngxP);
 
-  doChrmChunk(cmdline, pngxP->info_ptr);
+  doChrmChunk(cmdline, pngxP);
 
-  doPhysChunk(cmdline, pngxP->info_ptr);
+  doPhysChunk(cmdline, pngxP);
 
-  if (pngxP->info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
+  if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_PALETTE) {
 
-    /* creating PNG palette (Not counting the transparency palette) */
+      /* creating PNG palette (Not counting the transparency palette) */
 
-    createPngPalette(palette_pnm, palette_size, maxval,
-                     trans_pnm, trans_size, alpha_maxval, 
-                     palette, trans);
-    pngxP->info_ptr->valid |= PNG_INFO_PLTE;
-    pngxP->info_ptr->palette = palette;
-    pngxP->info_ptr->num_palette = palette_size;
+      createPngPalette(palettePnm, paletteSize, maxval,
+                       transPnm, transSize, alphaMaxval, 
+                       palette, trans);
+      pngx_setPlte(pngxP, palette, paletteSize);
 
-    doHistChunk(cmdline.hist, palette_pnm, ifP, rasterPos,
-                cols, rows, maxval, format,
-                pngxP->info_ptr, cmdline.verbose);
+      doHistChunk(pngxP, cmdline.hist, palettePnm, ifP, rasterPos,
+                  cols, rows, maxval, format, cmdline.verbose);
   }
 
-  doTrnsChunk(pngxP, trans, trans_size,
+  doTrnsChunk(pngxP, trans, transSize,
               transparent, transcolor, maxval, png_maxval);
 
-  doBkgdChunk(!!cmdline.background, pngxP->info_ptr,
+  doBkgdChunk(pngxP, !!cmdline.background,
               background_index, backcolor,
               maxval, png_maxval, cmdline.verbose);
 
-  doSbitChunk(pngxP->info_ptr, png_maxval, maxval, alpha, alpha_maxval);
+  doSbitChunk(pngxP, png_maxval, maxval, alpha, alphaMaxval);
 
   /* tEXT and zTXT chunks */
   if (cmdline.text || cmdline.ztxt)
-      pnmpng_read_text(pngxP->info_ptr, tfP, !!cmdline.ztxt, cmdline.verbose);
+      pngtxt_read(pngxP, tfP, !!cmdline.ztxt, cmdline.verbose);
 
-  doTimeChunk(cmdline, pngxP->info_ptr);
+  doTimeChunk(cmdline, pngxP);
 
   if (cmdline.filterSet != 0)
       png_set_filter(pngxP->png_ptr, 0, cmdline.filterSet);