--- xvpng.c.old	2007-05-14 09:53:28 +0900
+++ xvpng.c	2010-10-11 02:59:38 +0900
@@ -41,7 +41,7 @@
 #define COMPRESSION   6     /* default zlib compression level, not max
                                (Z_BEST_COMPRESSION) */
 
-#define HAVE_tRNS  (info_ptr->valid & PNG_INFO_tRNS)
+#define HAVE_tRNS  (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
 
 #define DWIDE    86
 #define DHIGH    104
@@ -84,6 +84,8 @@
 static CBUTT FdefCB, FnoneCB, FsubCB, FupCB, FavgCB, FPaethCB;
 
 
+static jmp_buf png_jmpbuf;
+
 #ifdef PNG_NO_STDIO
 /* NOTE:  Some sites configure their version of the PNG Library without
  *        Standard I/O Library interfaces in order to avoid unnecessary inter-
@@ -435,6 +437,7 @@
 {
   png_struct *png_ptr;
   png_info   *info_ptr;
+  int         png_bit_depth, png_color_type;
   png_color   palette[256];
   png_textp   text;
   byte        r1[256], g1[256], b1[256];  /* storage for deduped palette */
@@ -458,7 +461,7 @@
     FatalError(software);
   }
 
-  if (setjmp(png_ptr->jmpbuf)) {
+  if (setjmp(png_jmpbuf)) {
     png_destroy_write_struct(&png_ptr, &info_ptr);
     return -1;
   }
@@ -489,8 +492,6 @@
     png_set_filter(png_ptr, 0, filter);
   }
 
-  info_ptr->width = w;
-  info_ptr->height = h;
   if (w <= 0 || h <= 0) {
     SetISTR(ISTR_WARNING, "%s:  image dimensions out of range (%dx%d)",
       fbasename, w, h);
@@ -498,8 +499,6 @@
     return -1;
   }
 
-  info_ptr->interlace_type = interCB.val ? 1 : 0;
-
   linesize = 0;   /* quiet a compiler warning */
 
 
@@ -542,40 +541,37 @@
         png_destroy_write_struct(&png_ptr, &info_ptr);
         return -1;
       }
-      info_ptr->color_type = PNG_COLOR_TYPE_RGB;
-      info_ptr->bit_depth = 8;
+      png_color_type = PNG_COLOR_TYPE_RGB;
+      png_bit_depth = 8;
     } else /* ptype == PIC8 */ {
       linesize = w;
-      info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
+      png_color_type = PNG_COLOR_TYPE_PALETTE;
       if (numuniqcols <= 2)
-        info_ptr->bit_depth = 1;
+        png_bit_depth = 1;
       else
       if (numuniqcols <= 4)
-        info_ptr->bit_depth = 2;
+        png_bit_depth = 2;
       else
       if (numuniqcols <= 16)
-        info_ptr->bit_depth = 4;
+        png_bit_depth = 4;
       else
-        info_ptr->bit_depth = 8;
+        png_bit_depth = 8;
 
       for (i = 0; i < numuniqcols; i++) {
         palette[i].red   = r1[i];
         palette[i].green = g1[i];
         palette[i].blue  = b1[i];
       }
-      info_ptr->num_palette = numuniqcols;
-      info_ptr->palette = palette;
-      info_ptr->valid |= PNG_INFO_PLTE;
     }
   }
 
   else if (colorType == F_GREYSCALE || colorType == F_BWDITHER) {
-    info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
+    png_color_type = PNG_COLOR_TYPE_GRAY;
     if (colorType == F_BWDITHER) {
       /* shouldn't happen */
       if (ptype == PIC24) FatalError("PIC24 and B/W Stipple in WritePNG()");
 
-      info_ptr->bit_depth = 1;
+      png_bit_depth = 1;
       if (MONO(r1[0], g1[0], b1[0]) > MONO(r1[1], g1[1], b1[1])) {
         remap[0] = 1;
         remap[1] = 0;
@@ -595,7 +591,7 @@
           png_destroy_write_struct(&png_ptr, &info_ptr);
           return -1;
         }
-        info_ptr->bit_depth = 8;
+        png_bit_depth = 8;
       }
       else /* ptype == PIC8 */ {
         int low_precision;
@@ -617,7 +613,7 @@
         for (; i < 256; i++)
           remap[i]=0;  /* shouldn't be necessary, but... */
 
-        info_ptr->bit_depth = 8;
+        png_bit_depth = 8;
 
         /* Note that this fails most of the time because of gamma */
            /* (and that would be a bug:  GRR FIXME) */
@@ -636,7 +632,7 @@
           for (i = 0; i < numuniqcols; i++) {
             remap[i] &= 0xf;
           }
-          info_ptr->bit_depth = 4;
+          png_bit_depth = 4;
 
           /* try to adjust to 2-bit precision grayscale */
 
@@ -652,7 +648,7 @@
           for (i = 0; i < numuniqcols; i++) {
             remap[i] &= 3;
           }
-          info_ptr->bit_depth = 2;
+          png_bit_depth = 2;
 
           /* try to adjust to 1-bit precision grayscale */
 
@@ -668,7 +664,7 @@
           for (i = 0; i < numuniqcols; i++) {
             remap[i] &= 1;
           }
-          info_ptr->bit_depth = 1;
+          png_bit_depth = 1;
         }
       }
     }
@@ -677,6 +673,14 @@
   else
     png_error(png_ptr, "Unknown colorstyle in WritePNG");
 
+  png_set_IHDR(png_ptr, info_ptr, w, h, png_bit_depth, png_color_type,
+    interCB.val ? PNG_INTERLACE_ADAM7 : PNG_INTERLACE_NONE,
+    PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+
+  if (png_color_type == PNG_COLOR_TYPE_PALETTE) {
+    png_set_PLTE(png_ptr, info_ptr, palette, numuniqcols);
+  }
+
   if ((text = (png_textp)malloc(sizeof(png_text)))) {
     sprintf(software, "XV %s", REVDATE);
 
@@ -684,21 +688,17 @@
     text->key = "Software";
     text->text = software;
     text->text_length = strlen(text->text);
-
-    info_ptr->max_text = 1;
-    info_ptr->num_text = 1;
-    info_ptr->text = text;
+    png_set_text(png_ptr, info_ptr, text, 1);
   }
 
   Display_Gamma = gDial.val;  /* Save the current gamma for loading */
 
 // GRR FIXME:  add .Xdefaults option to omit writing gamma (size, cumulative errors when editing)--alternatively, modify save box to include "omit" checkbox
-  info_ptr->gamma = 1.0/gDial.val;
-  info_ptr->valid |= PNG_INFO_gAMA;
+  png_set_gAMA(png_ptr, info_ptr, 1.0/gDial.val);
 
   png_write_info(png_ptr, info_ptr);
 
-  if (info_ptr->bit_depth < 8)
+  if (png_bit_depth < 8)
     png_set_packing(png_ptr);
 
   pass=png_set_interlace_handling(png_ptr);
@@ -711,13 +711,13 @@
     int j;
     p = pic;
     for (j = 0; j < h; ++j) {
-      if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY) {
+      if (png_color_type == PNG_COLOR_TYPE_GRAY) {
         int k;
         for (k = 0; k < w; ++k)
           png_line[k] = ptype==PIC24 ? MONO(p[k*3], p[k*3+1], p[k*3+2]) :
                                        remap[pc2nc[p[k]]];
         png_write_row(png_ptr, png_line);
-      } else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
+      } else if (png_color_type == PNG_COLOR_TYPE_PALETTE) {
         int k;
         for (k = 0; k < w; ++k)
           png_line[k] = pc2nc[p[k]];
@@ -735,6 +735,7 @@
   savecmnt = NULL;   /* quiet a compiler warning */
 
   if (text) {
+    int num_text = 0;
     if (picComments && strlen(picComments) &&
         (savecmnt = (char *)malloc((strlen(picComments) + 1)*sizeof(char)))) {
       png_textp tp;
@@ -743,25 +744,19 @@
       strcpy(savecmnt, picComments);
       key = savecmnt;
       tp = text;
-      info_ptr->num_text = 0;
 
       comment = strchr(key, ':');
 
       do  {
-        /* Allocate a larger structure for comments if necessary */
-        if (info_ptr->num_text >= info_ptr->max_text)
+        if ((tp =
+            realloc(text, (num_text + 1)*sizeof(png_text))) == NULL)
         {
-          if ((tp =
-              realloc(text, (info_ptr->num_text + 2)*sizeof(png_text))) == NULL)
-          {
-            break;
-          }
-          else
-          {
-            text = tp;
-            tp = &text[info_ptr->num_text];
-            info_ptr->max_text += 2;
-          }
+          break;
+        }
+        else
+        {
+          text = tp;
+          tp = &text[num_text];
         }
 
         /* See if it looks like a PNG keyword from LoadPNG */
@@ -810,7 +805,7 @@
             }
 
             tp->compression = tp->text_length > 640 ? 0 : -1;
-            info_ptr->num_text++;
+            num_text++;
             tp++;
           }
         }
@@ -834,27 +829,25 @@
           tp->text = key;
           tp->text_length = q - key;
           tp->compression = tp->text_length > 750 ? 0 : -1;
-          info_ptr->num_text++;
+          num_text++;
           key = NULL;
         }
       } while (key && *key);
     }
-    else {
-      info_ptr->num_text = 0;
-    }
+    png_set_text(png_ptr, info_ptr, text, num_text);
   }
-  info_ptr->text = text;
 
-  png_convert_from_time_t(&(info_ptr->mod_time), time(NULL));
-  info_ptr->valid |= PNG_INFO_tIME;
+  {
+    png_time t;
+    png_convert_from_time_t(&t, time(NULL));
+    png_set_tIME(png_ptr, info_ptr, &t);
+  }
 
   png_write_end(png_ptr, info_ptr);
   fflush(fp);   /* just in case we core-dump before finishing... */
 
   if (text) {
     free(text);
-    /* must do this or png_destroy_write_struct() 0.97+ will free text again: */
-    info_ptr->text = (png_textp)NULL;
     if (savecmnt)
     {
       free(savecmnt);
@@ -886,6 +879,8 @@
   int pass;
   int gray_to_rgb;
   size_t commentsize;
+  int num_text;
+  png_textp text;
 
   fbasename = BaseName(fname);
 
@@ -921,7 +916,7 @@
     FatalError("malloc failure in LoadPNG");
   }
 
-  if (setjmp(png_ptr->jmpbuf)) {
+  if (setjmp(png_jmpbuf)) {
     fclose(fp);
     png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
     if (!read_anything) {
@@ -945,8 +940,8 @@
 #endif
   png_read_info(png_ptr, info_ptr);
 
-  pinfo->w = pinfo->normw = info_ptr->width;
-  pinfo->h = pinfo->normh = info_ptr->height;
+  pinfo->w = pinfo->normw = png_get_image_width(png_ptr, info_ptr);
+  pinfo->h = pinfo->normh = png_get_image_height(png_ptr, info_ptr);
   if (pinfo->w <= 0 || pinfo->h <= 0) {
     SetISTR(ISTR_WARNING, "%s:  image dimensions out of range (%dx%d)",
       fbasename, pinfo->w, pinfo->h);
@@ -957,9 +952,9 @@
   pinfo->frmType = F_PNG;
 
   sprintf(pinfo->fullInfo, "PNG, %d bit ",
-          info_ptr->bit_depth * info_ptr->channels);
+          png_get_bit_depth(png_ptr, info_ptr) * png_get_channels(png_ptr, info_ptr));
 
-  switch(info_ptr->color_type) {
+  switch(png_get_color_type(png_ptr, info_ptr)) {
     case PNG_COLOR_TYPE_PALETTE:
       strcat(pinfo->fullInfo, "palette color");
       break;
@@ -983,15 +978,18 @@
 
   sprintf(pinfo->fullInfo + strlen(pinfo->fullInfo),
 	  ", %sinterlaced. (%d bytes)",
-	  info_ptr->interlace_type ? "" : "non-", filesize);
+	  png_get_interlace_type(png_ptr, info_ptr) ? "" : "non-", filesize);
 
-  sprintf(pinfo->shrtInfo, "%lux%lu PNG", info_ptr->width, info_ptr->height);
+  sprintf(pinfo->shrtInfo, "%lux%lu PNG", (long)png_get_image_width(png_ptr, info_ptr), (long)png_get_image_height(png_ptr, info_ptr));
 
-  if (info_ptr->bit_depth < 8)
+  if (png_get_bit_depth(png_ptr, info_ptr) < 8)
       png_set_packing(png_ptr);
 
-  if (info_ptr->valid & PNG_INFO_gAMA)
-    png_set_gamma(png_ptr, Display_Gamma, info_ptr->gamma);
+  if (png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA)) {
+    double gamma;
+    png_get_gAMA(png_ptr, info_ptr, &gamma);
+    png_set_gamma(png_ptr, Display_Gamma, gamma);
+  }
 /*
  *else
  *  png_set_gamma(png_ptr, Display_Gamma, 0.45);
@@ -1000,7 +998,7 @@
   gray_to_rgb = 0;   /* quiet a compiler warning */
 
   if (have_imagebg) {
-    if (info_ptr->bit_depth == 16) {
+    if (png_get_bit_depth(png_ptr, info_ptr) == 16) {
       my_background.red   = imagebgR;
       my_background.green = imagebgG;
       my_background.blue  = imagebgB;
@@ -1013,8 +1011,8 @@
     }
     png_set_background(png_ptr, &my_background, PNG_BACKGROUND_GAMMA_SCREEN,
                        0, Display_Gamma);
-    if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
-         (info_ptr->color_type == PNG_COLOR_TYPE_GRAY && HAVE_tRNS)) &&
+    if ((png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY_ALPHA ||
+         (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY && HAVE_tRNS)) &&
         (imagebgR != imagebgG || imagebgR != imagebgB))  /* i.e., colored bg */
     {
       png_set_gray_to_rgb(png_ptr);
@@ -1022,8 +1020,10 @@
       gray_to_rgb = 1;
     }
   } else {
-    if (info_ptr->valid & PNG_INFO_bKGD) {
-      png_set_background(png_ptr, &info_ptr->background,
+    if (png_get_valid(png_ptr, info_ptr, PNG_INFO_bKGD)) {
+      png_color_16 *b;
+      png_get_bKGD(png_ptr, info_ptr, &b);
+      png_set_background(png_ptr, b,
                          PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
     } else {
       my_background.red = my_background.green = my_background.blue =
@@ -1033,13 +1033,13 @@
     }
   }
 
-  if (info_ptr->bit_depth == 16)
+  if (png_get_bit_depth(png_ptr, info_ptr) == 16)
     png_set_strip_16(png_ptr);
 
-  if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY ||
-      info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
+  if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY ||
+      png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY_ALPHA)
   {
-    if (info_ptr->bit_depth == 1)
+    if (png_get_bit_depth(png_ptr, info_ptr) == 1)
       pinfo->colType = F_BWDITHER;
     else
       pinfo->colType = F_GREYSCALE;
@@ -1050,8 +1050,8 @@
 
   png_read_update_info(png_ptr, info_ptr);
 
-  if (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
-     info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || gray_to_rgb)
+  if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGB ||
+     png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGB_ALPHA || gray_to_rgb)
   {
     linesize = 3 * pinfo->w;
     if (linesize/3 < pinfo->w) {   /* know pinfo->w > 0 (see above) */
@@ -1065,16 +1065,19 @@
   } else {
     linesize = pinfo->w;
     pinfo->type = PIC8;
-    if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY ||
-       info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
+    if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY ||
+       png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY_ALPHA) {
       for (i = 0; i < 256; i++)
         pinfo->r[i] = pinfo->g[i] = pinfo->b[i] = i;
     } else {
+      int num_palette;
+      png_color *palette;
+      png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
       pinfo->colType = F_FULLCOLOR;
-      for (i = 0; i < info_ptr->num_palette; i++) {
-        pinfo->r[i] = info_ptr->palette[i].red;
-        pinfo->g[i] = info_ptr->palette[i].green;
-        pinfo->b[i] = info_ptr->palette[i].blue;
+      for (i = 0; i < num_palette; i++) {
+        pinfo->r[i] = palette[i].red;
+        pinfo->g[i] = palette[i].green;
+        pinfo->b[i] = palette[i].blue;
       }
     }
   }
@@ -1106,22 +1109,23 @@
 
   png_read_end(png_ptr, info_ptr);
 
-  if (info_ptr->num_text > 0) {
+  png_get_text(png_ptr, info_ptr, &text, &num_text);
+  if (num_text > 0) {
     commentsize = 1;
 
-    for (i = 0; i < info_ptr->num_text; i++)
-      commentsize += strlen(info_ptr->text[i].key) + 2 +
-                     strlen(info_ptr->text[i].text) + 2;
+    for (i = 0; i < num_text; i++)
+      commentsize += strlen(text[i].key) + 2 +
+                     strlen(text[i].text) + 2;
 
     if ((pinfo->comment = malloc(commentsize)) == NULL) {
       png_warning(png_ptr,"can't allocate comment string");
     }
     else {
       pinfo->comment[0] = '\0';
-      for (i = 0; i < info_ptr->num_text; i++) {
-        strcat(pinfo->comment, info_ptr->text[i].key);
+      for (i = 0; i < num_text; i++) {
+        strcat(pinfo->comment, text[i].key);
         strcat(pinfo->comment, "::");
-        strcat(pinfo->comment, info_ptr->text[i].text);
+        strcat(pinfo->comment, text[i].text);
         strcat(pinfo->comment, "\n");
       }
     }
@@ -1143,7 +1147,7 @@
 {
   SetISTR(ISTR_WARNING,"%s:  libpng error: %s", fbasename, message);
 
-  longjmp(png_ptr->jmpbuf, 1);
+  longjmp(png_jmpbuf, 1);
 }
 
 
