2 // source code for the ImageLib PNG functions
4 // Copyright (C) 2001 M. Scott Heiman
7 // You may use the software for any purpose you see fit. You may modify
8 // it, incorporate it in a commercial application, use it for school,
9 // even turn it in as homework. You must keep the Copyright in the
10 // header and source files. This software is not in the "Public Domain".
11 // You may use this software at your own risk. I have made a reasonable
12 // effort to verify that this software works in the manner I expect it to;
15 // THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" AND
16 // WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING
17 // WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A
18 // PARTICULAR PURPOSE. IN NO EVENT SHALL MICHAEL S. HEIMAN BE LIABLE TO
19 // YOU OR ANYONE ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT OR
20 // CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING
21 // WITHOUT LIMITATION, LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE,
22 // OR THE CLAIMS OF THIRD PARTIES, WHETHER OR NOT MICHAEL S. HEIMAN HAS
23 // BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
24 // ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
25 // POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
32 #ifdef _BMG_LIBPNG_STANDALONE
33 #include "BMGLibPNG.h"
40 # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
44 /* this stuff is necessary because the normal png_init_io() method crashes in Win32 */
45 static void user_read_data(png_structp png_read, png_bytep data, png_size_t length)
47 FILE *fPtr = (FILE *) png_get_io_ptr(png_read);
48 if (fread(data, 1, length, fPtr) != length)
49 fprintf(stderr, "Failed to read %i bytes from PNG file.\n", (int) length);
52 static void user_write_data(png_structp png_write, png_bytep data, png_size_t length)
54 FILE *fPtr = (FILE *) png_get_io_ptr(png_write);
55 if (fwrite(data, 1, length, fPtr) != length)
56 fprintf(stderr, "Failed to write %i bytes to PNG file.\n", (int) length);
59 static void user_flush_data(png_structp png_read)
61 FILE *fPtr = (FILE *) png_get_io_ptr(png_read);
66 ReadPNG - Reads the contents of a PNG file and stores the contents into
70 filename - the name of the file to be opened
73 img - the BMGImageStruct containing the image data
76 BMGError - if the file could not be read or a resource error occurred
77 BMG_OK - if the file was read and the data was stored in img
83 2-bit images are converted to 4-bit images.
84 16-bit images are converted to 8-bit images.
85 gray scale images with alpha components are converted to 32-bit images
87 BMGError ReadPNG( const char *filename,
88 struct BMGImageStruct * volatile img )
93 FILE * volatile file = NULL;
97 unsigned char signature[8];
98 png_structp volatile png_ptr = NULL;
99 png_infop volatile info_ptr = NULL;
100 png_infop volatile end_info = NULL;
101 png_color_16 *ImageBackground = NULL;
102 png_bytep trns = NULL;
105 png_color_16p TransColors = NULL;
106 png_uint_32 Width, Height;
109 unsigned char** volatile rows = NULL;
114 error = setjmp( err_jmp );
117 if (end_info != NULL)
118 png_destroy_read_struct((png_structp *) &png_ptr, (png_infop *) &info_ptr, (png_infop *) &end_info);
119 else if (info_ptr != NULL)
120 png_destroy_read_struct((png_structp *) &png_ptr, (png_infop *) &info_ptr, NULL);
121 else if (png_ptr != NULL)
122 png_destroy_read_struct((png_structp *) &png_ptr, NULL, NULL);
133 SetLastBMGError((BMGError) error);
134 return (BMGError) error;
138 longjmp ( err_jmp, (int)errInvalidBMGImage );
140 file = fopen( filename, "rb" );
141 if ( !file || fread( signature, 1, 8, file ) != 8)
142 longjmp ( err_jmp, (int)errFileOpen );
144 /* check the signature */
145 if ( png_sig_cmp( signature, 0, 8 ) != 0 )
146 longjmp( err_jmp, (int)errUnsupportedFileFormat );
148 /* create a pointer to the png read structure */
149 png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
151 longjmp( err_jmp, (int)errMemoryAllocation );
153 /* create a pointer to the png info structure */
154 info_ptr = png_create_info_struct( png_ptr );
156 longjmp( err_jmp, (int)errMemoryAllocation );
158 /* create a pointer to the png end-info structure */
159 end_info = png_create_info_struct(png_ptr);
161 longjmp( err_jmp, (int)errMemoryAllocation );
163 /* bamboozle the PNG longjmp buffer */
164 /*generic PNG error handler*/
165 /* error will always == 1 which == errLib */
166 // error = png_setjmp(png_ptr);
167 error = setjmp( png_jmpbuf( png_ptr ) );
169 longjmp( err_jmp, error );
171 /* set function pointers in the PNG library, for read callbacks */
172 png_set_read_fn(png_ptr, (png_voidp) file, user_read_data);
174 /*let the read functions know that we have already read the 1st 8 bytes */
175 png_set_sig_bytes( png_ptr, 8 );
177 /* read all PNG data up to the image data */
178 png_read_info( png_ptr, info_ptr );
180 /* extract the data we need to form the HBITMAP from the PNG header */
181 png_get_IHDR( png_ptr, info_ptr, &Width, &Height, &BitDepth, &ColorType,
182 &InterlaceType, NULL, NULL);
184 img->width = (unsigned int) Width;
185 img->height = (unsigned int) Height;
187 img->bits_per_pixel = (unsigned char)32;
188 img->scan_width = Width * 4;
190 /* convert 16-bit images to 8-bit images */
192 png_set_strip_16(png_ptr);
194 /* These are not really required per Rice format spec,
195 * but is done just in case someone uses them.
197 /* convert palette color to rgb color */
198 if (ColorType == PNG_COLOR_TYPE_PALETTE) {
199 png_set_palette_to_rgb(png_ptr);
200 ColorType = PNG_COLOR_TYPE_RGB;
203 /* expand 1,2,4 bit gray scale to 8 bit gray scale */
204 if (ColorType == PNG_COLOR_TYPE_GRAY && BitDepth < 8)
205 png_set_expand_gray_1_2_4_to_8(png_ptr);
207 /* convert gray scale or gray scale + alpha to rgb color */
208 if (ColorType == PNG_COLOR_TYPE_GRAY ||
209 ColorType == PNG_COLOR_TYPE_GRAY_ALPHA) {
210 png_set_gray_to_rgb(png_ptr);
211 ColorType = PNG_COLOR_TYPE_RGB;
214 /* add alpha channel if any */
215 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
216 png_set_tRNS_to_alpha(png_ptr);
217 ColorType = PNG_COLOR_TYPE_RGB_ALPHA;
220 /* convert rgb to rgba */
221 if (ColorType == PNG_COLOR_TYPE_RGB) {
222 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
223 ColorType = PNG_COLOR_TYPE_RGB_ALPHA;
226 png_set_bgr(png_ptr);
228 /* set the background color if one is found */
229 if ( png_get_valid(png_ptr, info_ptr, PNG_INFO_bKGD) )
230 png_get_bKGD(png_ptr, info_ptr, &ImageBackground);
232 /* get the transparent color if one is there */
233 if ( png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS ) )
234 png_get_tRNS( png_ptr, info_ptr, &trns, &NumTrans, &TransColors );
236 img->palette_size = (unsigned short)0;
237 img->bytes_per_palette_entry = 4U;
239 tmp = AllocateBMGImage( img );
241 longjmp( err_jmp, (int)tmp );
243 png_read_update_info( png_ptr, info_ptr );
245 /* create buffer to read data to */
246 rows = (unsigned char **)malloc(Height*sizeof(unsigned char *));
248 longjmp( err_jmp, (int)errMemoryAllocation );
250 k = png_get_rowbytes( png_ptr, info_ptr );
251 rows[0] = (unsigned char *)malloc( Height*k*sizeof(char));
253 longjmp( err_jmp, (int)errMemoryAllocation );
255 for ( i = 1; i < (int)Height; i++ )
256 rows[i] = rows[i-1] + k;
258 /* read the entire image into rows */
259 png_read_image( png_ptr, rows );
261 bits = img->bits + (Height - 1) * img->scan_width;
262 for ( i = 0; i < (int)Height; i++ )
264 memcpy(bits, rows[i], 4*Width);
265 bits -= img->scan_width;
270 png_read_end( png_ptr, info_ptr );
271 png_destroy_read_struct((png_structp *) &png_ptr, (png_infop *) &info_ptr, (png_infop *) &end_info);
277 BMGError ReadPNGInfo( const char *filename,
278 struct BMGImageStruct * volatile img )
283 FILE * volatile file = NULL;
287 unsigned char signature[8];
288 png_structp volatile png_ptr = NULL;
289 png_infop volatile info_ptr = NULL;
290 png_infop volatile end_info = NULL;
291 png_uint_32 Width, Height;
294 error = setjmp( err_jmp );
297 if (end_info != NULL)
298 png_destroy_read_struct((png_structp *) &png_ptr, (png_infop *) &info_ptr, (png_infop *) &end_info);
299 else if (info_ptr != NULL)
300 png_destroy_read_struct((png_structp *) &png_ptr, (png_infop *) &info_ptr, NULL);
301 else if (png_ptr != NULL)
302 png_destroy_read_struct((png_structp *) &png_ptr, NULL, NULL);
307 SetLastBMGError((BMGError) error);
308 return (BMGError) error;
312 longjmp ( err_jmp, (int)errInvalidBMGImage );
314 file = fopen( filename, "rb" );
315 if ( !file || fread( signature, 1, 8, file ) != 8)
316 longjmp ( err_jmp, (int)errFileOpen );
318 /* check the signature */
319 if ( png_sig_cmp( signature, 0, 8 ) != 0 )
320 longjmp( err_jmp, (int)errUnsupportedFileFormat );
322 /* create a pointer to the png read structure */
323 png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
325 longjmp( err_jmp, (int)errMemoryAllocation );
327 /* create a pointer to the png info structure */
328 info_ptr = png_create_info_struct( png_ptr );
330 longjmp( err_jmp, (int)errMemoryAllocation );
332 /* create a pointer to the png end-info structure */
333 end_info = png_create_info_struct(png_ptr);
335 longjmp( err_jmp, (int)errMemoryAllocation );
337 /* bamboozle the PNG longjmp buffer */
338 /*generic PNG error handler*/
339 /* error will always == 1 which == errLib */
340 // error = png_setjmp(png_ptr);
341 error = setjmp( png_jmpbuf( png_ptr ) );
343 longjmp( err_jmp, error );
345 /* set function pointers in the PNG library, for read callbacks */
346 png_set_read_fn(png_ptr, (png_voidp) file, user_read_data);
348 /*let the read functions know that we have already read the 1st 8 bytes */
349 png_set_sig_bytes( png_ptr, 8 );
351 /* read all PNG data up to the image data */
352 png_read_info( png_ptr, info_ptr );
354 /* extract the data we need to form the HBITMAP from the PNG header */
355 png_get_IHDR( png_ptr, info_ptr, &Width, &Height, &BitDepth, &ColorType,
356 &InterlaceType, NULL, NULL);
358 img->width = (unsigned int) Width;
359 img->height = (unsigned int) Height;
361 img->bits_per_pixel = (unsigned char)32;
362 img->scan_width = Width * 4;
364 img->palette_size = (unsigned short)0;
365 img->bytes_per_palette_entry = 4U;
368 png_destroy_read_struct((png_structp *) &png_ptr, (png_infop *) &info_ptr, (png_infop *) &end_info);
375 WritePNG - writes the contents of a BMGImageStruct to a PNG file.
378 filename - the name of the file to be opened
379 img - the BMGImageStruct containing the image data
382 0 - if the file could not be written or a resource error occurred
383 1 - if the file was written
386 16-BPP BMG Images are converted to 24-BPP images
389 Color Type is limited to PNG_COLOR_TYPE_GRAY, PNG_COLOR_TYPE_RGB_ALPHA,
390 PNG_COLOR_TYPE_RGB, & PNG_COLOR_TYPE_PALETTE;
392 BMGError WritePNG( const char *filename, struct BMGImageStruct img )
398 png_structp png_ptr = NULL;
399 png_infop info_ptr = NULL;
400 png_colorp PNGPalette = NULL;
402 unsigned char *bits, *p, *q;
403 unsigned char **rows = NULL;
404 volatile int GrayScale, NumColors; // mark as volatile so GCC won't throw warning with -Wclobbered
407 FILE *outfile = NULL;
412 error = setjmp( err_jmp );
413 fprintf(stderr,"Writing PNG file %s.\n", filename);
416 if ( png_ptr != NULL )
417 png_destroy_write_struct( &png_ptr, NULL );
432 SetLastBMGError( (BMGError)error );
433 return (BMGError)error;
436 SetLastBMGError( BMG_OK );
438 if ((outfile = fopen(filename, "wb")) == NULL)
440 fprintf(stderr, "Error opening %s for reading.\n", filename);
441 longjmp( err_jmp, (int)errFileOpen );
444 /* 16 BPP DIBS do not have palettes. libPNG expects 16 BPP images to have
445 a palette. To correct this situation we must convert 16 BPP images to
446 24 BPP images before saving the data to the file */
447 if ( img.bits_per_pixel == 16 )
449 tmp = Convert16to24( &img );
451 longjmp( err_jmp, (int)tmp );
456 if (img.bits_per_pixel <= 8) // has palette
458 NumColors = img.palette_size;
459 /* if this is a grayscale image then set the flag and delete the palette*/
462 while ( i < NumColors && bits[0] == bits[1] && bits[0] == bits[2] )
465 bits += img.bytes_per_palette_entry;
467 GrayScale = (i == NumColors);
471 DIBScanWidth = ( img.width * img.bits_per_pixel + 7 ) / 8;
473 /* create the png pointer */
474 png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
476 longjmp( err_jmp, (int)errMemoryAllocation );
478 /* create the info pointer */
479 info_ptr = png_create_info_struct( png_ptr );
481 longjmp( err_jmp, (int)errMemoryAllocation );
483 /* bamboozle the png error handler */
484 /* error will always == 1 which equals errLib */
485 // error = png_setjmp(png_ptr);
486 error = setjmp( png_jmpbuf( png_ptr ) );
488 longjmp( err_jmp, error );
490 /* set function pointers in the PNG library, for write callbacks */
491 png_set_write_fn(png_ptr, (png_voidp) outfile, user_write_data, user_flush_data);
493 /* prepare variables needed to create PNG header */
494 BitDepth = img.bits_per_pixel < 8 ? img.bits_per_pixel : 8;
496 /* determine color type */
498 ColorType = PNG_COLOR_TYPE_GRAY;
499 else if ( img.bits_per_pixel == 32 )
500 ColorType = PNG_COLOR_TYPE_RGB_ALPHA;
501 else if ( img.bits_per_pixel == 24 )
502 ColorType = PNG_COLOR_TYPE_RGB;
504 ColorType = PNG_COLOR_TYPE_PALETTE;
506 /* create the PNG header */
507 png_set_IHDR( png_ptr, info_ptr, img.width, img.height, BitDepth, ColorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE );
509 /* store the palette information if there is any */
510 if ( img.palette != NULL && !GrayScale )
512 PNGPalette = (png_colorp)png_malloc( png_ptr,
513 NumColors*sizeof(png_color));
517 for ( i = 0; i < NumColors; i++, bits += img.bytes_per_palette_entry )
519 PNGPalette[i].red = bits[2];
520 PNGPalette[i].green = bits[1];
521 PNGPalette[i].blue = bits[0];
523 png_set_PLTE( png_ptr, info_ptr, PNGPalette, NumColors );
526 longjmp( err_jmp, (int)errMemoryAllocation );
529 /* write the file header information */
530 png_write_info( png_ptr, info_ptr );
532 /* create array to store data in */
533 rows = (unsigned char **)malloc(sizeof(unsigned char*));
535 longjmp( err_jmp, (int)errMemoryAllocation );
536 rows[0] = (unsigned char *)malloc( DIBScanWidth * sizeof(unsigned char));
538 longjmp( err_jmp, (int)errMemoryAllocation );
540 /* point to the bottom row of the DIB data. DIBs are stored bottom-to-top,
541 PNGs are stored top-to-bottom. */
542 bits = img.bits + (img.height - 1) * img.scan_width;
545 for ( i = 0; i < (int)img.height; i++ )
547 switch ( img.bits_per_pixel )
552 memcpy( (void *)rows[0], (void *)bits, DIBScanWidth );
556 for ( p = rows[0]; p < rows[0] + DIBScanWidth; p += 3, q += 3 )
565 for ( p = rows[0]; p < rows[0] + DIBScanWidth; p += 4, q += 4 )
575 png_write_rows( png_ptr, rows, 1 );
576 bits -= img.scan_width;
579 /* finish writing the rest of the file */
580 png_write_end( png_ptr, info_ptr );
582 /* clean up and exit */
587 png_destroy_write_struct( &png_ptr, NULL );
593 #ifdef _BMG_LIBPNG_STANDALONE
594 #pragma message ("Creating BMGLibPNG functions")
597 /* saves the contents of an HBITMAP to a file. returns 1 if successfull,
599 BMGError SaveBitmapToPNGFile( HBITMAP hBitmap, /* bitmap to be saved */
600 const char *filename) /* name of output file */
602 struct BMGImageStruct img;
603 char msg[256], ext[4], *period;
604 BMGError out = BMG_OK;
606 InitBMGImage( &img );
608 /* determine the file type by using the extension */
609 strcpy( msg, filename );
610 period = strrchr( msg, '.' );
611 if ( period != NULL )
614 strcpy( ext, period );
615 ext[0] = toupper( ext[0] );
616 ext[1] = toupper( ext[1] );
617 ext[2] = toupper( ext[2] );
622 strcat( msg, ".PNG" );
623 strcpy( ext, "PNG" );
626 if ( strcmp( ext, "PNG" ) == 0 )
628 /* extract data from the bitmap. We assume that 32 bit images have been
629 // blended with the background (unless this is a DDB - see GetDataFromBitmap
630 // for more details) */
631 out = GetDataFromBitmap( hBitmap, &img, 1 );
634 out = WritePNG( msg, img );
636 FreeBMGImage( &img );
640 out = errInvalidFileExtension;
643 SetLastBMGError( out );
648 /* Creates an HBITMAP to an image file. returns an HBITMAP if successfull,
650 HBITMAP CreateBitmapFromPNGFile( const char *filename,
653 char ext[4], msg[256];
655 BMGError out = BMG_OK;
656 struct BMGImageStruct img;
657 HBITMAP hBitmap = NULL;
659 InitBMGImage( &img );
662 strcpy( msg, filename );
663 period = strrchr( msg, '.' );
664 if ( period != NULL )
667 strncpy( ext, period, 3 );
668 ext[0] = toupper( ext[0] );
669 ext[1] = toupper( ext[1] );
670 ext[2] = toupper( ext[2] );
675 strcat( msg, ".PNG" );
676 strcpy( ext, "PNG" );
679 if ( strcmp( ext, "PNG" ) == 0 )
681 out = ReadPNG( msg, &img );
684 hBitmap = CreateBitmapFromData( img, blend );
686 FreeBMGImage( &img );
690 out = errInvalidFileExtension;
693 SetLastBMGError( out );