$NetBSD: patch-ai,v 1.1 2006/04/19 19:21:28 wiz Exp $

Fix build with png-1.5.

--- src/readPNG.c.orig	2000-12-08 18:00:45.000000000 +0000
+++ src/readPNG.c
@@ -35,8 +35,8 @@ unsigned char * ReadPNG(FILE *infile,int
 	unsigned char *pixmap;
 	unsigned char *p;
 	png_byte *q;
-	png_struct *png_ptr;
-	png_info *info_ptr;
+	png_structp png_ptr;
+	png_infop info_ptr;
 	double screen_gamma;
 	png_byte *png_pixels=NULL, **row_pointers=NULL;
 	int i, j;
@@ -60,40 +60,36 @@ unsigned char * ReadPNG(FILE *infile,int
 	rewind(infile);
 
         /* allocate the structures */
-	png_ptr = (png_struct *)calloc(1,sizeof(png_struct));
-	if(!png_ptr)
-		return 0;
-
-	info_ptr = (png_info *)calloc(1,sizeof(png_info));
-	if(!info_ptr) {
-		free(png_ptr);
-		return 0;
+	png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
+	    NULL, NULL, NULL);
+	if (!png_ptr)
+		return (0);
+
+	info_ptr = png_create_info_struct(png_ptr);
+	if (!info_ptr) {
+		png_destroy_read_struct(&png_ptr, (png_infopp)NULL,
+		    (png_infopp)NULL);
+		return (0);
 	}
 
         /* Establish the setjmp return context for png_error to use. */
-	if (setjmp(png_ptr->jmpbuf)) {
+	if (setjmp(png_jmpbuf(png_ptr))) {
         
         	if (mMosaicSrcTrace) {
             		fprintf(stderr, "\n!!!libpng read error!!!\n");
         	}
-		png_read_destroy(png_ptr, info_ptr, (png_info *)0); 
+		png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
 
 		if(png_pixels != NULL)
 			free((char *)png_pixels);
 		if(row_pointers != NULL)
 			free((png_byte **)row_pointers);
-		free((char *)png_ptr);
-		free((char *)info_ptr);
 		return 0;
 	}
 
     /* SWP -- Hopefully to fix cores on bad PNG files */
     /*####png_set_message_fn(png_ptr,png_get_msg_ptr(png_ptr),NULL,NULL); */
 
-        /* initialize the structures */
-	png_info_init(info_ptr);
-	png_read_init(png_ptr);
-    
         /* set up the input control */
 	png_init_io(png_ptr, infile);
     
@@ -102,19 +98,19 @@ unsigned char * ReadPNG(FILE *infile,int
     
         /* setup other stuff using the fields of png_info. */
     
-	*width = (int)png_ptr->width;
-	*height = (int)png_ptr->height;
+	*width = (int)png_get_image_width(png_ptr, info_ptr);
+	*height = (int)png_get_image_height(png_ptr, info_ptr);
 
 #ifdef DEBUG_PNG
-	fprintf(stderr,"\n\nBEFORE\nheight = %d\n", (int)png_ptr->width);
-	fprintf(stderr,"width = %d\n", (int)png_ptr->height);
-	fprintf(stderr,"bit depth = %d\n", info_ptr->bit_depth);
-	fprintf(stderr,"color type = %d\n", info_ptr->color_type);
+	fprintf(stderr,"\n\nBEFORE\nheight = %d\n", (int)png_get_image_width(png_ptr, info_ptr));
+	fprintf(stderr,"width = %d\n", (int)png_get_image_height(png_ptr, info_ptr));
+	fprintf(stderr,"bit depth = %d\n", png_get_bit_depth(png_ptr, info_ptr));
+	fprintf(stderr,"color type = %d\n", png_get_color_type(png_ptr, info_ptr));
 	fprintf(stderr,"compression type = %d\n", info_ptr->compression_type);
 	fprintf(stderr,"filter type = %d\n", info_ptr->filter_type);
 	fprintf(stderr,"interlace type = %d\n", info_ptr->interlace_type);
 	fprintf(stderr,"num colors = %d\n",info_ptr->num_palette);
-	fprintf(stderr,"rowbytes = %d\n", info_ptr->rowbytes);
+	fprintf(stderr,"rowbytes = %d\n", png_get_rowbytes(png_ptr, info_ptr));
 #endif
 #if 0
         /* This handles alpha and transparency by replacing it with 
@@ -133,15 +129,15 @@ unsigned char * ReadPNG(FILE *infile,int
 #endif
 
         /* strip pixels in 16-bit images down to 8 bits */
-	if (info_ptr->bit_depth == 16)
+	if (png_get_bit_depth(png_ptr, info_ptr) == 16)
 		png_set_strip_16(png_ptr);
 
 /* If it is a color image then check if it has a palette. If not
 then dither the image to 256 colors, and make up a palette */
-	if (info_ptr->color_type==PNG_COLOR_TYPE_RGB ||
-	    info_ptr->color_type==PNG_COLOR_TYPE_RGB_ALPHA) {
+	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) {
 
-		if(! (info_ptr->valid & PNG_INFO_PLTE)) {
+		if(! (png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE))) {
 #ifdef DEBUG_PNG
                 	fprintf(stderr,"dithering (RGB->palette)...\n");
 #endif
@@ -155,38 +151,45 @@ then dither the image to 256 colors, and
 
                 /* this should probably be dithering to 
                    Rdata.colors_per_inlined_image colors */
-			png_set_dither(png_ptr, std_color_cube, 216, 216, NULL, 1);
+			png_set_quantize(png_ptr, std_color_cube, 216, 216, NULL, 1);
 		} else {
+			png_colorp palette;
+			int num_palette;
+			png_uint_16p hist;
+			png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
+			png_get_hIST(png_ptr, info_ptr, &hist);
 #ifdef DEBUG_PNG
                 	fprintf(stderr,"dithering (RGB->file supplied palette)...\n");
 #endif
-			png_set_dither(png_ptr, info_ptr->palette, 
-					info_ptr->num_palette,
+			png_set_quantize(png_ptr, palette, 
+					num_palette,
 					mMosaicAppData.colors_per_inlined_image,
-					info_ptr->hist, 1);
+					hist, 1);
 		}
 	}
 /* PNG files pack pixels of bit depths 1, 2, and 4 into bytes as
            small as they can. This expands pixels to 1 pixel per byte, and
            if a transparency value is supplied, an alpha channel is
            built.*/
-	if (info_ptr->bit_depth < 8)
+	if (png_get_bit_depth(png_ptr, info_ptr) < 8)
 		png_set_packing(png_ptr);
 
 /* have libpng handle the gamma conversion */
 
 	if (mMosaicAppData.use_screen_gamma) { /*SWP*/
-		if (info_ptr->bit_depth != 16) {  /* temporary .. glennrp */
+		if (png_get_bit_depth(png_ptr, info_ptr) != 16) {  /* temporary .. glennrp */
 			screen_gamma=(double)(mMosaicAppData.screen_gamma);
             
 #ifdef DEBUG_PNG
         fprintf(stderr,"screen gamma=%f\n",screen_gamma);
 #endif
-			if (info_ptr->valid & PNG_INFO_gAMA) {
+			if (png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA)) {
+				double gamma;
+				png_get_gAMA(png_ptr, info_ptr, &gamma);
 #ifdef DEBUG_PNG
-				printf("setting gamma=%f\n",info_ptr->gamma);
+				printf("setting gamma=%f\n",gamma);
 #endif
-				png_set_gamma(png_ptr, screen_gamma, (double)info_ptr->gamma);
+				png_set_gamma(png_ptr, screen_gamma, (double)gamma);
 			} else {
 #ifdef DEBUG_PNG
 				fprintf(stderr,"setting gamma=%f\n",0.45);
@@ -196,41 +199,44 @@ then dither the image to 256 colors, and
 		}
 	}
     
-	if (info_ptr->interlace_type)
+	if (png_get_interlace_type(png_ptr, info_ptr))
 		png_set_interlace_handling(png_ptr);
 
 	png_read_update_info(png_ptr, info_ptr);
     
 #ifdef DEBUG_PNG
-	fprintf(stderr,"\n\nAFTER\nheight = %d\n", (int)png_ptr->width);
-	fprintf(stderr,"width = %d\n", (int)png_ptr->height);
-	fprintf(stderr,"bit depth = %d\n", info_ptr->bit_depth);
-	fprintf(stderr,"color type = %d\n", info_ptr->color_type);
+	fprintf(stderr,"\n\nAFTER\nheight = %d\n", (int)png_get_image_width(png_ptr, info_ptr));
+	fprintf(stderr,"width = %d\n", (int)png_get_image_height(png_ptr, info_ptr));
+	fprintf(stderr,"bit depth = %d\n", png_get_bit_depth(png_ptr, info_ptr));
+	fprintf(stderr,"color type = %d\n", png_get_color_type(png_ptr, info_ptr));
 	fprintf(stderr,"compression type = %d\n", info_ptr->compression_type);
 	fprintf(stderr,"filter type = %d\n", info_ptr->filter_type);
 	fprintf(stderr,"interlace type = %d\n", info_ptr->interlace_type);
 	fprintf(stderr,"num colors = %d\n",info_ptr->num_palette);
-	fprintf(stderr,"rowbytes = %d\n", info_ptr->rowbytes);
+	fprintf(stderr,"rowbytes = %d\n", png_get_rowbytes(png_ptr, info_ptr));
 #endif
 /* allocate the pixel grid which we will need to send to png_read_image(). */
-	png_pixels = (png_byte *)malloc(info_ptr->rowbytes * 
+	png_pixels = (png_byte *)malloc(png_get_rowbytes(png_ptr, info_ptr) * 
 			(*height) * sizeof(png_byte));
     
 	row_pointers = (png_byte **) calloc((*height) , sizeof(png_byte *));
 	for (i=0; i < *height; i++)
-		row_pointers[i]=png_pixels+(info_ptr->rowbytes*i);
+		row_pointers[i]=png_pixels+(png_get_rowbytes(png_ptr, info_ptr)*i);
 
 /* FINALLY - read the darn thing. */
 	png_read_image(png_ptr, row_pointers);
     
 /* now that we have the (transformed to 8-bit RGB) image, we have
 to copy the resulting palette to our colormap. */
-	if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) {
-		if (info_ptr->valid & PNG_INFO_PLTE) {
-			for (i=0; i < info_ptr->num_palette; i++) {
-				colrs[i].red = info_ptr->palette[i].red << 8;
-				colrs[i].green = info_ptr->palette[i].green << 8;
-				colrs[i].blue = info_ptr->palette[i].blue << 8;
+	if (png_get_color_type(png_ptr, info_ptr) & PNG_COLOR_MASK_COLOR) {
+		if (png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE)) {
+			png_colorp palette;
+			int num_palette; 
+			png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
+			for (i=0; i < num_palette; i++) {
+				colrs[i].red = palette[i].red << 8;
+				colrs[i].green = palette[i].green << 8;
+				colrs[i].blue = palette[i].blue << 8;
 				colrs[i].pixel = i;
 				colrs[i].flags = DoRed|DoGreen|DoBlue;
 			}
@@ -257,7 +263,7 @@ to copy the resulting palette to our col
 	p = pixmap; q = png_pixels;
 /* if there is an alpha channel, we have to get rid of it in the
 pixmap, since I don't do anything with it yet */
-	if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) {
+	if (png_get_color_type(png_ptr, info_ptr) & PNG_COLOR_MASK_ALPHA) {
 #ifdef DEBUG_PNG
        		fprintf(stderr,"Getting rid of alpha channel\n");
 #endif
@@ -283,10 +289,7 @@ pixmap, since I don't do anything with i
 	}
 	free((png_byte **)row_pointers);
 /* clean up after the read, and free any memory allocated */
-	png_read_destroy(png_ptr, info_ptr, (png_info *)0);
-/* free the structures */
-	free((char *)png_ptr);
-	free((char *)info_ptr);
+	png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
 	return pixmap;
 }
 #endif
