1 //-------------------------------------
2 // PNGFILE.C -- Image File Functions
3 //-------------------------------------
5 // Copyright 2000, Willem van Schaik. For conditions of distribution and
6 // use, see the copyright/license/disclaimer notice in png.h
17 define_exception_type(const char *);
18 extern struct exception_context the_exception_context[1];
19 struct exception_context the_exception_context[1];
22 static OPENFILENAME ofn;
24 static png_structp png_ptr = NULL;
25 static png_infop info_ptr = NULL;
31 png_cexcept_error(png_structp png_ptr, png_const_charp msg)
35 #ifndef PNG_NO_CONSOLE_IO
36 fprintf(stderr, "libpng error: %s\n", msg);
43 // Windows open-file functions
45 void PngFileInitialize (HWND hwnd)
47 static TCHAR szFilter[] = TEXT ("PNG Files (*.PNG)\0*.png\0")
48 TEXT ("All Files (*.*)\0*.*\0\0");
50 ofn.lStructSize = sizeof (OPENFILENAME);
53 ofn.lpstrFilter = szFilter;
54 ofn.lpstrCustomFilter = NULL;
55 ofn.nMaxCustFilter = 0;
57 ofn.lpstrFile = NULL; // Set in Open and Close functions
58 ofn.nMaxFile = MAX_PATH;
59 ofn.lpstrFileTitle = NULL; // Set in Open and Close functions
60 ofn.nMaxFileTitle = MAX_PATH;
61 ofn.lpstrInitialDir = NULL;
62 ofn.lpstrTitle = NULL;
63 ofn.Flags = 0; // Set in Open and Close functions
65 ofn.nFileExtension = 0;
66 ofn.lpstrDefExt = TEXT ("png");
69 ofn.lpTemplateName = NULL;
72 BOOL PngFileOpenDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
75 ofn.lpstrFile = pstrFileName;
76 ofn.lpstrFileTitle = pstrTitleName;
77 ofn.Flags = OFN_HIDEREADONLY;
79 return GetOpenFileName (&ofn);
82 BOOL PngFileSaveDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
85 ofn.lpstrFile = pstrFileName;
86 ofn.lpstrFileTitle = pstrTitleName;
87 ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
89 return GetSaveFileName (&ofn);
92 // PNG image handler functions
94 BOOL PngLoadImage (PTSTR pstrFileName, png_byte **ppbImageData,
95 int *piWidth, int *piHeight, int *piChannels, png_color *pBkgColor)
102 png_color_16 *pBackground;
103 png_uint_32 ulChannels;
104 png_uint_32 ulRowBytes;
105 png_byte *pbImageData = *ppbImageData;
106 static png_byte **ppbRowPointers = NULL;
109 // open the PNG input file
113 *ppbImageData = pbImageData = NULL;
117 if (!(pfFile = fopen(pstrFileName, "rb")))
119 *ppbImageData = pbImageData = NULL;
123 // first check the eight byte PNG signature
125 fread(pbSig, 1, 8, pfFile);
126 if (!png_check_sig(pbSig, 8))
128 *ppbImageData = pbImageData = NULL;
132 // create the two png(-info) structures
134 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
135 (png_error_ptr)png_cexcept_error, (png_error_ptr)NULL);
138 *ppbImageData = pbImageData = NULL;
142 info_ptr = png_create_info_struct(png_ptr);
145 png_destroy_read_struct(&png_ptr, NULL, NULL);
146 *ppbImageData = pbImageData = NULL;
153 // initialize the png structure
155 #if !defined(PNG_NO_STDIO)
156 png_init_io(png_ptr, pfFile);
158 png_set_read_fn(png_ptr, (png_voidp)pfFile, png_read_data);
161 png_set_sig_bytes(png_ptr, 8);
163 // read all PNG info up to image data
165 png_read_info(png_ptr, info_ptr);
167 // get width, height, bit-depth and color-type
169 png_get_IHDR(png_ptr, info_ptr, piWidth, piHeight, &iBitDepth,
170 &iColorType, NULL, NULL, NULL);
172 // expand images of all color-type and bit-depth to 3x8 bit RGB images
173 // let the library process things like alpha, transparency, background
176 png_set_strip_16(png_ptr);
177 if (iColorType == PNG_COLOR_TYPE_PALETTE)
178 png_set_expand(png_ptr);
180 png_set_expand(png_ptr);
181 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
182 png_set_expand(png_ptr);
183 if (iColorType == PNG_COLOR_TYPE_GRAY ||
184 iColorType == PNG_COLOR_TYPE_GRAY_ALPHA)
185 png_set_gray_to_rgb(png_ptr);
187 // set the background color to draw transparent and alpha images over.
188 if (png_get_bKGD(png_ptr, info_ptr, &pBackground))
190 png_set_background(png_ptr, pBackground, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
191 pBkgColor->red = (byte) pBackground->red;
192 pBkgColor->green = (byte) pBackground->green;
193 pBkgColor->blue = (byte) pBackground->blue;
200 // if required set gamma conversion
201 if (png_get_gAMA(png_ptr, info_ptr, &dGamma))
202 png_set_gamma(png_ptr, (double) 2.2, dGamma);
204 // after the transformations have been registered update info_ptr data
206 png_read_update_info(png_ptr, info_ptr);
208 // get again width, height and the new bit-depth and color-type
210 png_get_IHDR(png_ptr, info_ptr, piWidth, piHeight, &iBitDepth,
211 &iColorType, NULL, NULL, NULL);
214 // row_bytes is the width x number of channels
216 ulRowBytes = png_get_rowbytes(png_ptr, info_ptr);
217 ulChannels = png_get_channels(png_ptr, info_ptr);
219 *piChannels = ulChannels;
221 // now we can allocate memory to store the image
228 if ((pbImageData = (png_byte *) malloc(ulRowBytes * (*piHeight)
229 * sizeof(png_byte))) == NULL)
231 png_error(png_ptr, "Visual PNG: out of memory");
233 *ppbImageData = pbImageData;
235 // and allocate memory for an array of row-pointers
237 if ((ppbRowPointers = (png_bytepp) malloc((*piHeight)
238 * sizeof(png_bytep))) == NULL)
240 png_error(png_ptr, "Visual PNG: out of memory");
243 // set the individual row-pointers to point at the correct offsets
245 for (i = 0; i < (*piHeight); i++)
246 ppbRowPointers[i] = pbImageData + i * ulRowBytes;
248 // now we can go ahead and just read the whole image
250 png_read_image(png_ptr, ppbRowPointers);
252 // read the additional chunks in the PNG file (not really needed)
254 png_read_end(png_ptr, NULL);
258 free (ppbRowPointers);
259 ppbRowPointers = NULL;
266 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
268 *ppbImageData = pbImageData = NULL;
271 free (ppbRowPointers);
284 BOOL PngSaveImage (PTSTR pstrFileName, png_byte *pDiData,
285 int iWidth, int iHeight, png_color bkgColor)
287 const int ciBitDepth = 8;
288 const int ciChannels = 3;
291 png_uint_32 ulRowBytes;
292 static png_byte **ppbRowPointers = NULL;
295 // open the PNG output file
300 if (!(pfFile = fopen(pstrFileName, "wb")))
303 // prepare the standard PNG structures
305 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
306 (png_error_ptr)png_cexcept_error, (png_error_ptr)NULL);
313 info_ptr = png_create_info_struct(png_ptr);
316 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
322 // initialize the png structure
324 #if !defined(PNG_NO_STDIO)
325 png_init_io(png_ptr, pfFile);
327 png_set_write_fn(png_ptr, (png_voidp)pfFile, png_write_data, png_flush);
330 // we're going to write a very simple 3x8 bit RGB image
332 png_set_IHDR(png_ptr, info_ptr, iWidth, iHeight, ciBitDepth,
333 PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
334 PNG_FILTER_TYPE_BASE);
336 // write the file header information
338 png_write_info(png_ptr, info_ptr);
340 // swap the BGR pixels in the DiData structure to RGB
342 png_set_bgr(png_ptr);
344 // row_bytes is the width x number of channels
346 ulRowBytes = iWidth * ciChannels;
348 // we can allocate memory for an array of row-pointers
350 if ((ppbRowPointers = (png_bytepp) malloc(iHeight * sizeof(png_bytep))) == NULL)
351 Throw "Visualpng: Out of memory";
353 // set the individual row-pointers to point at the correct offsets
355 for (i = 0; i < iHeight; i++)
356 ppbRowPointers[i] = pDiData + i * (((ulRowBytes + 3) >> 2) << 2);
358 // write out the entire image data in one call
360 png_write_image (png_ptr, ppbRowPointers);
362 // write the additional chunks to the PNG file (not really needed)
364 png_write_end(png_ptr, info_ptr);
368 free (ppbRowPointers);
369 ppbRowPointers = NULL;
371 // clean up after the write, and free any memory allocated
373 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
380 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
383 free (ppbRowPointers);
398 png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
402 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
403 * instead of an int, which is what fread() actually returns.
405 check = (png_size_t)fread(data, (png_size_t)1, length,
406 (FILE *)png_ptr->io_ptr);
410 png_error(png_ptr, "Read Error");
415 png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
419 check = fwrite(data, 1, length, (FILE *)(png_ptr->io_ptr));
422 png_error(png_ptr, "Write Error");
427 png_flush(png_structp png_ptr)
430 io_ptr = (FILE *)CVT_PTR((png_ptr->io_ptr));