1 //-------------------------------------
2 // PNGFILE.C -- Image File Functions
3 //-------------------------------------
5 // Copyright 2000, Willem van Schaik.
7 // This code is released under the libpng license.
8 // For conditions of distribution and use, see the disclaimer
9 // and license in png.h
20 define_exception_type(const char *);
21 extern struct exception_context the_exception_context[1];
22 struct exception_context the_exception_context[1];
25 static OPENFILENAME ofn;
27 static png_structp png_ptr = NULL;
28 static png_infop info_ptr = NULL;
34 png_cexcept_error(png_structp png_ptr, png_const_charp msg)
38 #ifdef PNG_CONSOLE_IO_SUPPORTED
39 fprintf(stderr, "libpng error: %s\n", msg);
46 // Windows open-file functions
48 void PngFileInitialize (HWND hwnd)
50 static TCHAR szFilter[] = TEXT ("PNG Files (*.PNG)\0*.png\0")
51 TEXT ("All Files (*.*)\0*.*\0\0");
53 ofn.lStructSize = sizeof (OPENFILENAME);
56 ofn.lpstrFilter = szFilter;
57 ofn.lpstrCustomFilter = NULL;
58 ofn.nMaxCustFilter = 0;
60 ofn.lpstrFile = NULL; // Set in Open and Close functions
61 ofn.nMaxFile = MAX_PATH;
62 ofn.lpstrFileTitle = NULL; // Set in Open and Close functions
63 ofn.nMaxFileTitle = MAX_PATH;
64 ofn.lpstrInitialDir = NULL;
65 ofn.lpstrTitle = NULL;
66 ofn.Flags = 0; // Set in Open and Close functions
68 ofn.nFileExtension = 0;
69 ofn.lpstrDefExt = TEXT ("png");
72 ofn.lpTemplateName = NULL;
75 BOOL PngFileOpenDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
78 ofn.lpstrFile = pstrFileName;
79 ofn.lpstrFileTitle = pstrTitleName;
80 ofn.Flags = OFN_HIDEREADONLY;
82 return GetOpenFileName (&ofn);
85 BOOL PngFileSaveDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
88 ofn.lpstrFile = pstrFileName;
89 ofn.lpstrFileTitle = pstrTitleName;
90 ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
92 return GetSaveFileName (&ofn);
95 // PNG image handler functions
97 BOOL PngLoadImage (PTSTR pstrFileName, png_byte **ppbImageData,
98 int *piWidth, int *piHeight, int *piChannels, png_color *pBkgColor)
105 png_color_16 *pBackground;
106 png_uint_32 ulChannels;
107 png_uint_32 ulRowBytes;
108 png_byte *pbImageData = *ppbImageData;
109 static png_byte **ppbRowPointers = NULL;
112 // open the PNG input file
116 *ppbImageData = pbImageData = NULL;
120 if (!(pfFile = fopen(pstrFileName, "rb")))
122 *ppbImageData = pbImageData = NULL;
126 // first check the eight byte PNG signature
128 fread(pbSig, 1, 8, pfFile);
129 if (png_sig_cmp(pbSig, 0, 8))
131 *ppbImageData = pbImageData = NULL;
135 // create the two png(-info) structures
137 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
138 (png_error_ptr)png_cexcept_error, (png_error_ptr)NULL);
141 *ppbImageData = pbImageData = NULL;
145 info_ptr = png_create_info_struct(png_ptr);
148 png_destroy_read_struct(&png_ptr, NULL, NULL);
149 *ppbImageData = pbImageData = NULL;
156 // initialize the png structure
158 #ifdef PNG_STDIO_SUPPORTED
159 png_init_io(png_ptr, pfFile);
161 png_set_read_fn(png_ptr, (png_voidp)pfFile, png_read_data);
164 png_set_sig_bytes(png_ptr, 8);
166 // read all PNG info up to image data
168 png_read_info(png_ptr, info_ptr);
170 // get width, height, bit-depth and color-type
172 png_get_IHDR(png_ptr, info_ptr, piWidth, piHeight, &iBitDepth,
173 &iColorType, NULL, NULL, NULL);
175 // expand images of all color-type and bit-depth to 3x8 bit RGB images
176 // let the library process things like alpha, transparency, background
179 png_set_strip_16(png_ptr);
180 if (iColorType == PNG_COLOR_TYPE_PALETTE)
181 png_set_expand(png_ptr);
183 png_set_expand(png_ptr);
184 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
185 png_set_expand(png_ptr);
186 if (iColorType == PNG_COLOR_TYPE_GRAY ||
187 iColorType == PNG_COLOR_TYPE_GRAY_ALPHA)
188 png_set_gray_to_rgb(png_ptr);
190 // set the background color to draw transparent and alpha images over.
191 if (png_get_bKGD(png_ptr, info_ptr, &pBackground))
193 png_set_background(png_ptr, pBackground, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
194 pBkgColor->red = (byte) pBackground->red;
195 pBkgColor->green = (byte) pBackground->green;
196 pBkgColor->blue = (byte) pBackground->blue;
203 // if required set gamma conversion
204 if (png_get_gAMA(png_ptr, info_ptr, &dGamma))
205 png_set_gamma(png_ptr, (double) 2.2, dGamma);
207 // after the transformations have been registered update info_ptr data
209 png_read_update_info(png_ptr, info_ptr);
211 // get again width, height and the new bit-depth and color-type
213 png_get_IHDR(png_ptr, info_ptr, piWidth, piHeight, &iBitDepth,
214 &iColorType, NULL, NULL, NULL);
217 // row_bytes is the width x number of channels
219 ulRowBytes = png_get_rowbytes(png_ptr, info_ptr);
220 ulChannels = png_get_channels(png_ptr, info_ptr);
222 *piChannels = ulChannels;
224 // now we can allocate memory to store the image
231 if ((pbImageData = (png_byte *) malloc(ulRowBytes * (*piHeight)
232 * sizeof(png_byte))) == NULL)
234 png_error(png_ptr, "Visual PNG: out of memory");
236 *ppbImageData = pbImageData;
238 // and allocate memory for an array of row-pointers
240 if ((ppbRowPointers = (png_bytepp) malloc((*piHeight)
241 * sizeof(png_bytep))) == NULL)
243 png_error(png_ptr, "Visual PNG: out of memory");
246 // set the individual row-pointers to point at the correct offsets
248 for (i = 0; i < (*piHeight); i++)
249 ppbRowPointers[i] = pbImageData + i * ulRowBytes;
251 // now we can go ahead and just read the whole image
253 png_read_image(png_ptr, ppbRowPointers);
255 // read the additional chunks in the PNG file (not really needed)
257 png_read_end(png_ptr, NULL);
261 free (ppbRowPointers);
262 ppbRowPointers = NULL;
269 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
271 *ppbImageData = pbImageData = NULL;
274 free (ppbRowPointers);
287 BOOL PngSaveImage (PTSTR pstrFileName, png_byte *pDiData,
288 int iWidth, int iHeight, png_color bkgColor)
290 const int ciBitDepth = 8;
291 const int ciChannels = 3;
294 png_uint_32 ulRowBytes;
295 static png_byte **ppbRowPointers = NULL;
298 // open the PNG output file
303 if (!(pfFile = fopen(pstrFileName, "wb")))
306 // prepare the standard PNG structures
308 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
309 (png_error_ptr)png_cexcept_error, (png_error_ptr)NULL);
316 info_ptr = png_create_info_struct(png_ptr);
319 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
325 // initialize the png structure
327 #ifdef PNG_STDIO_SUPPORTED
328 png_init_io(png_ptr, pfFile);
330 png_set_write_fn(png_ptr, (png_voidp)pfFile, png_write_data, png_flush);
333 // we're going to write a very simple 3x8 bit RGB image
335 png_set_IHDR(png_ptr, info_ptr, iWidth, iHeight, ciBitDepth,
336 PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
337 PNG_FILTER_TYPE_BASE);
339 // write the file header information
341 png_write_info(png_ptr, info_ptr);
343 // swap the BGR pixels in the DiData structure to RGB
345 png_set_bgr(png_ptr);
347 // row_bytes is the width x number of channels
349 ulRowBytes = iWidth * ciChannels;
351 // we can allocate memory for an array of row-pointers
353 if ((ppbRowPointers = (png_bytepp) malloc(iHeight * sizeof(png_bytep))) == NULL)
354 Throw "Visualpng: Out of memory";
356 // set the individual row-pointers to point at the correct offsets
358 for (i = 0; i < iHeight; i++)
359 ppbRowPointers[i] = pDiData + i * (((ulRowBytes + 3) >> 2) << 2);
361 // write out the entire image data in one call
363 png_write_image (png_ptr, ppbRowPointers);
365 // write the additional chunks to the PNG file (not really needed)
367 png_write_end(png_ptr, info_ptr);
371 free (ppbRowPointers);
372 ppbRowPointers = NULL;
374 // clean up after the write, and free any memory allocated
376 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
383 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
386 free (ppbRowPointers);
398 #ifndef PNG_STDIO_SUPPORTED
401 png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
405 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
406 * instead of an int, which is what fread() actually returns.
408 check = (png_size_t)fread(data, (png_size_t)1, length,
409 (FILE *)png_ptr->io_ptr);
413 png_error(png_ptr, "Read Error");
418 png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
422 check = fwrite(data, 1, length, (FILE *)(png_ptr->io_ptr));
425 png_error(png_ptr, "Write Error");
430 png_flush(png_structp png_ptr)
433 io_ptr = (FILE *)CVT_PTR((png_ptr->io_ptr));