aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/visupng/PngFile.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/visupng/PngFile.c')
-rw-r--r--libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/visupng/PngFile.c450
1 files changed, 450 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/visupng/PngFile.c b/libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/visupng/PngFile.c
new file mode 100644
index 0000000..f3e440a
--- /dev/null
+++ b/libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/visupng/PngFile.c
@@ -0,0 +1,450 @@
1/*-------------------------------------
2 * PNGFILE.C -- Image File Functions
3 *-------------------------------------
4 *
5 * Copyright 2000, Willem van Schaik.
6 *
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
10 */
11
12#include <windows.h>
13#include <commdlg.h>
14#include <stdio.h>
15#include <stdlib.h>
16
17#include "png.h"
18#include "pngfile.h"
19#include "cexcept.h"
20
21define_exception_type(const char *);
22extern struct exception_context the_exception_context[1];
23struct exception_context the_exception_context[1];
24png_const_charp msg;
25
26static OPENFILENAME ofn;
27
28static png_structp png_ptr = NULL;
29static png_infop info_ptr = NULL;
30
31
32/* cexcept interface */
33
34static void
35png_cexcept_error(png_structp png_ptr, png_const_charp msg)
36{
37 if(png_ptr)
38 ;
39#ifdef PNG_CONSOLE_IO_SUPPORTED
40 fprintf(stderr, "libpng error: %s\n", msg);
41#endif
42 {
43 Throw msg;
44 }
45}
46
47/* Windows open-file functions */
48
49void PngFileInitialize (HWND hwnd)
50{
51 static TCHAR szFilter[] = TEXT ("PNG Files (*.PNG)\0*.png\0")
52 TEXT ("All Files (*.*)\0*.*\0\0");
53
54 ofn.lStructSize = sizeof (OPENFILENAME);
55 ofn.hwndOwner = hwnd;
56 ofn.hInstance = NULL;
57 ofn.lpstrFilter = szFilter;
58 ofn.lpstrCustomFilter = NULL;
59 ofn.nMaxCustFilter = 0;
60 ofn.nFilterIndex = 0;
61 ofn.lpstrFile = NULL; /* Set in Open and Close functions */
62 ofn.nMaxFile = MAX_PATH;
63 ofn.lpstrFileTitle = NULL; /* Set in Open and Close functions */
64 ofn.nMaxFileTitle = MAX_PATH;
65 ofn.lpstrInitialDir = NULL;
66 ofn.lpstrTitle = NULL;
67 ofn.Flags = 0; /* Set in Open and Close functions */
68 ofn.nFileOffset = 0;
69 ofn.nFileExtension = 0;
70 ofn.lpstrDefExt = TEXT ("png");
71 ofn.lCustData = 0;
72 ofn.lpfnHook = NULL;
73 ofn.lpTemplateName = NULL;
74}
75
76BOOL PngFileOpenDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
77{
78 ofn.hwndOwner = hwnd;
79 ofn.lpstrFile = pstrFileName;
80 ofn.lpstrFileTitle = pstrTitleName;
81 ofn.Flags = OFN_HIDEREADONLY;
82
83 return GetOpenFileName (&ofn);
84}
85
86BOOL PngFileSaveDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
87{
88 ofn.hwndOwner = hwnd;
89 ofn.lpstrFile = pstrFileName;
90 ofn.lpstrFileTitle = pstrTitleName;
91 ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
92
93 return GetSaveFileName (&ofn);
94}
95
96/* PNG image handler functions */
97
98BOOL PngLoadImage (PTSTR pstrFileName, png_byte **ppbImageData,
99 int *piWidth, int *piHeight, int *piChannels, png_color *pBkgColor)
100{
101 static FILE *pfFile;
102 png_byte pbSig[8];
103 int iBitDepth;
104 int iColorType;
105 double dGamma;
106 png_color_16 *pBackground;
107 png_uint_32 ulChannels;
108 png_uint_32 ulRowBytes;
109 png_byte *pbImageData = *ppbImageData;
110 static png_byte **ppbRowPointers = NULL;
111 int i;
112
113 /* open the PNG input file */
114
115 if (!pstrFileName)
116 {
117 *ppbImageData = pbImageData = NULL;
118 return FALSE;
119 }
120
121 if (!(pfFile = fopen(pstrFileName, "rb")))
122 {
123 *ppbImageData = pbImageData = NULL;
124 return FALSE;
125 }
126
127 /* first check the eight byte PNG signature */
128
129 fread(pbSig, 1, 8, pfFile);
130 if (png_sig_cmp(pbSig, 0, 8))
131 {
132 *ppbImageData = pbImageData = NULL;
133 return FALSE;
134 }
135
136 /* create the two png(-info) structures */
137
138 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
139 (png_error_ptr)png_cexcept_error, (png_error_ptr)NULL);
140 if (!png_ptr)
141 {
142 *ppbImageData = pbImageData = NULL;
143 return FALSE;
144 }
145
146 info_ptr = png_create_info_struct(png_ptr);
147 if (!info_ptr)
148 {
149 png_destroy_read_struct(&png_ptr, NULL, NULL);
150 *ppbImageData = pbImageData = NULL;
151 return FALSE;
152 }
153
154 Try
155 {
156
157 /* initialize the png structure */
158
159#ifdef PNG_STDIO_SUPPORTED
160 png_init_io(png_ptr, pfFile);
161#else
162 png_set_read_fn(png_ptr, (png_voidp)pfFile, png_read_data);
163#endif
164
165 png_set_sig_bytes(png_ptr, 8);
166
167 /* read all PNG info up to image data */
168
169 png_read_info(png_ptr, info_ptr);
170
171 /* get width, height, bit-depth and color-type */
172
173 png_get_IHDR(png_ptr, info_ptr, piWidth, piHeight, &iBitDepth,
174 &iColorType, NULL, NULL, NULL);
175
176 /* expand images of all color-type and bit-depth to 3x8-bit RGB */
177 /* let the library process alpha, transparency, background, etc. */
178
179#ifdef PNG_READ_16_TO_8_SUPPORTED
180 if (iBitDepth == 16)
181# ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
182 png_set_scale_16(png_ptr);
183# else
184 png_set_strip_16(png_ptr);
185# endif
186#endif
187 if (iColorType == PNG_COLOR_TYPE_PALETTE)
188 png_set_expand(png_ptr);
189 if (iBitDepth < 8)
190 png_set_expand(png_ptr);
191 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
192 png_set_expand(png_ptr);
193 if (iColorType == PNG_COLOR_TYPE_GRAY ||
194 iColorType == PNG_COLOR_TYPE_GRAY_ALPHA)
195 png_set_gray_to_rgb(png_ptr);
196
197 /* set the background color to draw transparent and alpha images over */
198 if (png_get_bKGD(png_ptr, info_ptr, &pBackground))
199 {
200 png_set_background(png_ptr, pBackground, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
201 pBkgColor->red = (byte) pBackground->red;
202 pBkgColor->green = (byte) pBackground->green;
203 pBkgColor->blue = (byte) pBackground->blue;
204 }
205 else
206 {
207 pBkgColor = NULL;
208 }
209
210 /* if required set gamma conversion */
211 if (png_get_gAMA(png_ptr, info_ptr, &dGamma))
212 png_set_gamma(png_ptr, (double) 2.2, dGamma);
213
214 /* after the transformations are registered, update info_ptr data */
215
216 png_read_update_info(png_ptr, info_ptr);
217
218 /* get again width, height and the new bit-depth and color-type */
219
220 png_get_IHDR(png_ptr, info_ptr, piWidth, piHeight, &iBitDepth,
221 &iColorType, NULL, NULL, NULL);
222
223
224 /* row_bytes is the width x number of channels */
225
226 ulRowBytes = png_get_rowbytes(png_ptr, info_ptr);
227 ulChannels = png_get_channels(png_ptr, info_ptr);
228
229 *piChannels = ulChannels;
230
231 /* now we can allocate memory to store the image */
232
233 if (pbImageData)
234 {
235 free (pbImageData);
236 pbImageData = NULL;
237 }
238 if ((pbImageData = (png_byte *) malloc(ulRowBytes * (*piHeight)
239 * sizeof(png_byte))) == NULL)
240 {
241 png_error(png_ptr, "Visual PNG: out of memory");
242 }
243 *ppbImageData = pbImageData;
244
245 /* and allocate memory for an array of row-pointers */
246
247 if ((ppbRowPointers = (png_bytepp) malloc((*piHeight)
248 * sizeof(png_bytep))) == NULL)
249 {
250 png_error(png_ptr, "Visual PNG: out of memory");
251 }
252
253 /* set the individual row-pointers to point at the correct offsets */
254
255 for (i = 0; i < (*piHeight); i++)
256 ppbRowPointers[i] = pbImageData + i * ulRowBytes;
257
258 /* now we can go ahead and just read the whole image */
259
260 png_read_image(png_ptr, ppbRowPointers);
261
262 /* read the additional chunks in the PNG file (not really needed) */
263
264 png_read_end(png_ptr, NULL);
265
266 /* and we're done */
267
268 free (ppbRowPointers);
269 ppbRowPointers = NULL;
270
271 /* yepp, done */
272 }
273
274 Catch (msg)
275 {
276 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
277
278 *ppbImageData = pbImageData = NULL;
279
280 if(ppbRowPointers)
281 free (ppbRowPointers);
282
283 fclose(pfFile);
284
285 return FALSE;
286 }
287
288 fclose (pfFile);
289
290 return TRUE;
291}
292
293
294BOOL PngSaveImage (PTSTR pstrFileName, png_byte *pDiData,
295 int iWidth, int iHeight, png_color bkgColor)
296{
297 const int ciBitDepth = 8;
298 const int ciChannels = 3;
299
300 static FILE *pfFile;
301 png_uint_32 ulRowBytes;
302 static png_byte **ppbRowPointers = NULL;
303 int i;
304
305 /* open the PNG output file */
306
307 if (!pstrFileName)
308 return FALSE;
309
310 if (!(pfFile = fopen(pstrFileName, "wb")))
311 return FALSE;
312
313 /* prepare the standard PNG structures */
314
315 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
316 (png_error_ptr)png_cexcept_error, (png_error_ptr)NULL);
317 if (!png_ptr)
318 {
319 fclose(pfFile);
320 return FALSE;
321 }
322
323 info_ptr = png_create_info_struct(png_ptr);
324 if (!info_ptr) {
325 fclose(pfFile);
326 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
327 return FALSE;
328 }
329
330 Try
331 {
332 /* initialize the png structure */
333
334#ifdef PNG_STDIO_SUPPORTED
335 png_init_io(png_ptr, pfFile);
336#else
337 png_set_write_fn(png_ptr, (png_voidp)pfFile, png_write_data, png_flush);
338#endif
339
340 /* we're going to write a very simple 3x8-bit RGB image */
341
342 png_set_IHDR(png_ptr, info_ptr, iWidth, iHeight, ciBitDepth,
343 PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
344 PNG_FILTER_TYPE_BASE);
345
346 /* write the file header information */
347
348 png_write_info(png_ptr, info_ptr);
349
350 /* swap the BGR pixels in the DiData structure to RGB */
351
352 png_set_bgr(png_ptr);
353
354 /* row_bytes is the width x number of channels */
355
356 ulRowBytes = iWidth * ciChannels;
357
358 /* we can allocate memory for an array of row-pointers */
359
360 if ((ppbRowPointers = (png_bytepp) malloc(iHeight * sizeof(png_bytep))) == NULL)
361 Throw "Visualpng: Out of memory";
362
363 /* set the individual row-pointers to point at the correct offsets */
364
365 for (i = 0; i < iHeight; i++)
366 ppbRowPointers[i] = pDiData + i * (((ulRowBytes + 3) >> 2) << 2);
367
368 /* write out the entire image data in one call */
369
370 png_write_image (png_ptr, ppbRowPointers);
371
372 /* write the additional chunks to the PNG file (not really needed) */
373
374 png_write_end(png_ptr, info_ptr);
375
376 /* and we're done */
377
378 free (ppbRowPointers);
379 ppbRowPointers = NULL;
380
381 /* clean up after the write, and free any memory allocated */
382
383 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
384
385 /* yepp, done */
386 }
387
388 Catch (msg)
389 {
390 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
391
392 if(ppbRowPointers)
393 free (ppbRowPointers);
394
395 fclose(pfFile);
396
397 return FALSE;
398 }
399
400 fclose (pfFile);
401
402 return TRUE;
403}
404
405#ifndef PNG_STDIO_SUPPORTED
406
407static void
408png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
409{
410 png_size_t check;
411
412 /* fread() returns 0 on error, so it is OK to store this in a png_size_t
413 * instead of an int, which is what fread() actually returns.
414 */
415 check = (png_size_t)fread(data, (png_size_t)1, length,
416 (FILE *)png_ptr->io_ptr);
417
418 if (check != length)
419 {
420 png_error(png_ptr, "Read Error");
421 }
422}
423
424static void
425png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
426{
427 png_uint_32 check;
428
429 check = fwrite(data, 1, length, (FILE *)(png_ptr->io_ptr));
430 if (check != length)
431 {
432 png_error(png_ptr, "Write Error");
433 }
434}
435
436static void
437png_flush(png_structp png_ptr)
438{
439 FILE *io_ptr;
440 io_ptr = (FILE *)CVT_PTR((png_ptr->io_ptr));
441 if (io_ptr != NULL)
442 fflush(io_ptr);
443}
444
445#endif
446
447/*-----------------
448 * end of source
449 *-----------------
450 */