]>
Commit | Line | Data |
---|---|---|
e9c4b1a2 | 1 | ///////////////////////////////////////////////////////////////////////////// |
27bb2b7c | 2 | // Name: src/common/imagepng.cpp |
e9c4b1a2 JS |
3 | // Purpose: wxImage PNG handler |
4 | // Author: Robert Roebling | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
27bb2b7c VZ |
10 | // ============================================================================ |
11 | // declarations | |
12 | // ============================================================================ | |
13 | ||
8f493002 VS |
14 | #ifdef __GNUG__ |
15 | #pragma implementation "imagpng.h" | |
16 | #endif | |
e9c4b1a2 | 17 | |
27bb2b7c VZ |
18 | // ---------------------------------------------------------------------------- |
19 | // headers | |
20 | // ---------------------------------------------------------------------------- | |
21 | ||
e9c4b1a2 JS |
22 | // For compilers that support precompilation, includes "wx.h". |
23 | #include "wx/wxprec.h" | |
24 | ||
25 | #ifdef __BORLANDC__ | |
ce4169a4 RR |
26 | #pragma hdrstop |
27 | #endif | |
28 | ||
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/defs.h" | |
e9c4b1a2 JS |
31 | #endif |
32 | ||
c96ea657 | 33 | #if wxUSE_IMAGE && wxUSE_LIBPNG |
ce4169a4 | 34 | |
8f493002 | 35 | #include "wx/imagpng.h" |
e9c4b1a2 JS |
36 | #include "wx/bitmap.h" |
37 | #include "wx/debug.h" | |
38 | #include "wx/log.h" | |
39 | #include "wx/app.h" | |
ce4169a4 | 40 | #include "png.h" |
e9c4b1a2 JS |
41 | #include "wx/filefn.h" |
42 | #include "wx/wfstream.h" | |
43 | #include "wx/intl.h" | |
44 | #include "wx/module.h" | |
45 | ||
46 | // For memcpy | |
47 | #include <string.h> | |
48 | ||
49 | #ifdef __SALFORDC__ | |
50 | #ifdef FAR | |
51 | #undef FAR | |
52 | #endif | |
53 | #endif | |
54 | ||
27bb2b7c VZ |
55 | // ---------------------------------------------------------------------------- |
56 | // constants | |
57 | // ---------------------------------------------------------------------------- | |
58 | ||
59 | // image can not have any transparent pixels at all, have only 100% opaque | |
60 | // and/or 100% transparent pixels in which case a simple mask is enough to | |
61 | // store this information in wxImage or have a real alpha channel in which case | |
62 | // we need to have it in wxImage as well | |
63 | enum Transparency | |
64 | { | |
65 | Transparency_None, | |
66 | Transparency_Mask, | |
67 | Transparency_Alpha | |
68 | }; | |
69 | ||
70 | // ---------------------------------------------------------------------------- | |
71 | // local functions | |
72 | // ---------------------------------------------------------------------------- | |
73 | ||
74 | // return the kind of transparency needed for this image assuming that it does | |
75 | // have transparent pixels, i.e. either Transparency_Alpha or Transparency_Mask | |
76 | static Transparency | |
36308e0e | 77 | CheckTransparency(unsigned char **lines, |
f7155274 VZ |
78 | png_uint_32 x, png_uint_32 y, png_uint_32 w, png_uint_32 h, |
79 | size_t numColBytes); | |
27bb2b7c VZ |
80 | |
81 | // init the alpha channel for the image and fill it with 1s up to (x, y) | |
82 | static unsigned char *InitAlpha(wxImage *image, png_uint_32 x, png_uint_32 y); | |
83 | ||
84 | // find a free colour for the mask in the PNG data array | |
85 | static void | |
86 | FindMaskColour(unsigned char **lines, png_uint_32 width, png_uint_32 height, | |
87 | unsigned char& rMask, unsigned char& gMask, unsigned char& bMask); | |
e9c4b1a2 | 88 | |
c9fcf581 VZ |
89 | // is the pixel with this value of alpha a fully opaque one? |
90 | static inline | |
91 | bool IsOpaque(unsigned char a) | |
92 | { | |
93 | return a == 0xff; | |
94 | } | |
95 | ||
96 | // is the pixel with this value of alpha a fully transparent one? | |
97 | static inline | |
98 | bool IsTransparent(unsigned char a) | |
99 | { | |
100 | return !a; | |
101 | } | |
102 | ||
27bb2b7c VZ |
103 | // ============================================================================ |
104 | // wxPNGHandler implementation | |
105 | // ============================================================================ | |
e9c4b1a2 | 106 | |
e9c4b1a2 | 107 | IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler,wxImageHandler) |
e9c4b1a2 | 108 | |
e30285ab | 109 | #if wxUSE_STREAMS |
9ab6ee85 | 110 | |
0729bd19 | 111 | #ifndef PNGLINKAGEMODE |
30e0e251 VZ |
112 | #ifdef __WATCOMC__ |
113 | // we need an explicit cdecl for Watcom, at least according to | |
114 | // | |
115 | // http://sf.net/tracker/index.php?func=detail&aid=651492&group_id=9863&atid=109863 | |
116 | // | |
117 | // more testing is needed for this however, please remove this comment | |
118 | // if you can confirm that my fix works with Watcom 11 | |
119 | #define PNGLINKAGEMODE cdecl | |
120 | #else | |
121 | #define PNGLINKAGEMODE LINKAGEMODE | |
122 | #endif | |
717b9bf2 DW |
123 | #endif |
124 | ||
bdffd806 VS |
125 | |
126 | // VS: wxPNGInfoStruct declared below is a hack that needs some explanation. | |
27bb2b7c VZ |
127 | // First, let me describe what's the problem: libpng uses jmp_buf in |
128 | // its png_struct structure. Unfortunately, this structure is | |
129 | // compiler-specific and may vary in size, so if you use libpng compiled | |
bdffd806 | 130 | // as DLL with another compiler than the main executable, it may not work |
27bb2b7c | 131 | // (this is for example the case with wxMGL port and SciTech MGL library |
bdffd806 | 132 | // that provides custom runtime-loadable libpng implementation with jmpbuf |
27bb2b7c | 133 | // disabled altogether). Luckily, it is still possible to use setjmp() & |
bdffd806 VS |
134 | // longjmp() as long as the structure is not part of png_struct. |
135 | // | |
136 | // Sadly, there's no clean way to attach user-defined data to png_struct. | |
137 | // There is only one customizable place, png_struct.io_ptr, which is meant | |
27bb2b7c | 138 | // only for I/O routines and is set with png_set_read_fn or |
bdffd806 VS |
139 | // png_set_write_fn. The hacky part is that we use io_ptr to store |
140 | // a pointer to wxPNGInfoStruct that holds I/O structures _and_ jmp_buf. | |
141 | ||
142 | struct wxPNGInfoStruct | |
143 | { | |
144 | jmp_buf jmpbuf; | |
145 | bool verbose; | |
27bb2b7c | 146 | |
bdffd806 VS |
147 | union |
148 | { | |
149 | wxInputStream *in; | |
150 | wxOutputStream *out; | |
151 | } stream; | |
152 | }; | |
153 | ||
154 | #define WX_PNG_INFO(png_ptr) ((wxPNGInfoStruct*)png_get_io_ptr(png_ptr)) | |
155 | ||
27bb2b7c VZ |
156 | // ---------------------------------------------------------------------------- |
157 | // helper functions | |
158 | // ---------------------------------------------------------------------------- | |
bdffd806 | 159 | |
90350682 VZ |
160 | extern "C" |
161 | { | |
162 | ||
163 | void PNGLINKAGEMODE _PNG_stream_reader( png_structp png_ptr, png_bytep data, png_size_t length ) | |
e9c4b1a2 | 164 | { |
bdffd806 | 165 | WX_PNG_INFO(png_ptr)->stream.in->Read(data, length); |
e9c4b1a2 JS |
166 | } |
167 | ||
90350682 | 168 | void PNGLINKAGEMODE _PNG_stream_writer( png_structp png_ptr, png_bytep data, png_size_t length ) |
e9c4b1a2 | 169 | { |
bdffd806 | 170 | WX_PNG_INFO(png_ptr)->stream.out->Write(data, length); |
e9c4b1a2 JS |
171 | } |
172 | ||
deb2fec0 SB |
173 | // from pngerror.c |
174 | // so that the libpng doesn't send anything on stderr | |
175 | void | |
bdffd806 | 176 | PNGLINKAGEMODE wx_png_error(png_structp png_ptr, png_const_charp message) |
deb2fec0 | 177 | { |
bdffd806 | 178 | wxPNGInfoStruct *info = WX_PNG_INFO(png_ptr); |
2b5f62a0 VZ |
179 | if (info->verbose) |
180 | wxLogError( wxString::FromAscii(message) ); | |
bdffd806 | 181 | |
deb2fec0 | 182 | #ifdef USE_FAR_KEYWORD |
bdffd806 VS |
183 | { |
184 | jmp_buf jmpbuf; | |
185 | png_memcpy(jmpbuf,info->jmpbuf,sizeof(jmp_buf)); | |
186 | longjmp(jmpbuf, 1); | |
187 | } | |
deb2fec0 | 188 | #else |
bdffd806 | 189 | longjmp(info->jmpbuf, 1); |
deb2fec0 SB |
190 | #endif |
191 | } | |
192 | ||
193 | void | |
bdffd806 | 194 | PNGLINKAGEMODE wx_png_warning(png_structp png_ptr, png_const_charp message) |
deb2fec0 | 195 | { |
bdffd806 | 196 | wxPNGInfoStruct *info = WX_PNG_INFO(png_ptr); |
2b5f62a0 VZ |
197 | if (info->verbose) |
198 | wxLogWarning( wxString::FromAscii(message) ); | |
deb2fec0 SB |
199 | } |
200 | ||
90350682 VZ |
201 | } // extern "C" |
202 | ||
27bb2b7c VZ |
203 | // ---------------------------------------------------------------------------- |
204 | // LoadFile() helpers | |
205 | // ---------------------------------------------------------------------------- | |
206 | ||
d26d9754 | 207 | // determine the kind of transparency we need for this image: if the only alpha |
c9fcf581 VZ |
208 | // values it has are 0 (transparent) and 0xff (opaque) then we can simply |
209 | // create a mask for it, we should be ok with a simple mask but otherwise we | |
210 | // need a full blown alpha channel in wxImage | |
d26d9754 VZ |
211 | // |
212 | // parameters: | |
36308e0e | 213 | // lines raw PNG data |
d26d9754 VZ |
214 | // x, y starting position |
215 | // w, h size of the image | |
216 | // numColBytes number of colour bytes (1 for grey scale, 3 for RGB) | |
217 | // (NB: alpha always follows the colour bytes) | |
27bb2b7c | 218 | Transparency |
36308e0e | 219 | CheckTransparency(unsigned char **lines, |
d26d9754 VZ |
220 | png_uint_32 x, png_uint_32 y, png_uint_32 w, png_uint_32 h, |
221 | size_t numColBytes) | |
27bb2b7c | 222 | { |
c9fcf581 VZ |
223 | // we start from (x + 1, y) |
224 | x++; | |
225 | ||
d26d9754 VZ |
226 | // suppose that a mask will suffice and check all the remaining alpha |
227 | // values to see if it does | |
36308e0e | 228 | for ( ; y < h; y++ ) |
27bb2b7c | 229 | { |
36308e0e VZ |
230 | unsigned const char *ptr = lines[y] + x; |
231 | ||
c9fcf581 | 232 | for ( png_uint_32 x2 = x; x2 < w; x2++ ) |
27bb2b7c | 233 | { |
c9fcf581 | 234 | // skip the grey or colour byte(s) |
36308e0e | 235 | ptr += numColBytes; |
27bb2b7c | 236 | |
36308e0e | 237 | unsigned char a2 = *ptr++; |
c9fcf581 VZ |
238 | |
239 | if ( !IsTransparent(a2) && !IsOpaque(a2) ) | |
27bb2b7c | 240 | { |
d26d9754 VZ |
241 | // not fully opaque nor fully transparent, hence need alpha |
242 | return Transparency_Alpha; | |
27bb2b7c | 243 | } |
27bb2b7c | 244 | } |
c9fcf581 VZ |
245 | |
246 | // during the next loop iteration check all the pixels in the row | |
247 | x = 0; | |
27bb2b7c VZ |
248 | } |
249 | ||
d26d9754 VZ |
250 | // mask will be enough |
251 | return Transparency_Mask; | |
27bb2b7c VZ |
252 | } |
253 | ||
254 | unsigned char *InitAlpha(wxImage *image, png_uint_32 x, png_uint_32 y) | |
255 | { | |
256 | // create alpha channel | |
257 | image->SetAlpha(); | |
258 | ||
259 | unsigned char *alpha = image->GetAlpha(); | |
260 | ||
261 | // set alpha for the pixels we had so far | |
c9fcf581 VZ |
262 | png_uint_32 end = y * image->GetWidth() + x; |
263 | for ( png_uint_32 i = 0; i < end; i++ ) | |
27bb2b7c | 264 | { |
c9fcf581 VZ |
265 | // all the previous pixels were opaque |
266 | *alpha++ = 0xff; | |
27bb2b7c VZ |
267 | } |
268 | ||
269 | return alpha; | |
270 | } | |
271 | ||
272 | void | |
273 | FindMaskColour(unsigned char **lines, png_uint_32 width, png_uint_32 height, | |
274 | unsigned char& rMask, unsigned char& gMask, unsigned char& bMask) | |
275 | { | |
276 | // choosing the colour for the mask is more | |
277 | // difficult: we need to iterate over the entire | |
278 | // image for this in order to choose an unused | |
279 | // colour (this is not very efficient but what else | |
280 | // can we do?) | |
281 | wxImageHistogram h; | |
282 | unsigned nentries = 0; | |
283 | unsigned char r2, g2, b2; | |
284 | for ( png_uint_32 y2 = 0; y2 < height; y2++ ) | |
285 | { | |
286 | const unsigned char *p = lines[y2]; | |
287 | for ( png_uint_32 x2 = 0; x2 < width; x2++ ) | |
288 | { | |
289 | r2 = *p++; | |
290 | g2 = *p++; | |
291 | b2 = *p++; | |
292 | ||
293 | wxImageHistogramEntry& | |
294 | entry = h[wxImageHistogram:: MakeKey(r2, g2, b2)]; | |
295 | ||
296 | if ( entry.value++ == 0 ) | |
297 | entry.index = nentries++; | |
298 | } | |
299 | } | |
300 | ||
301 | if ( !h.FindFirstUnusedColour(&rMask, &gMask, &bMask) ) | |
302 | { | |
303 | wxLogWarning(_("Too many colours in PNG, the image may be slightly blurred.")); | |
304 | ||
305 | // use a fixed mask colour and we'll fudge | |
306 | // the real pixels with this colour (see | |
307 | // below) | |
308 | rMask = 0xfe; | |
309 | gMask = 0; | |
310 | bMask = 0xff; | |
311 | } | |
312 | } | |
313 | ||
314 | // ---------------------------------------------------------------------------- | |
315 | // reading PNGs | |
316 | // ---------------------------------------------------------------------------- | |
317 | ||
318 | bool wxPNGHandler::DoCanRead( wxInputStream& stream ) | |
319 | { | |
320 | unsigned char hdr[4]; | |
321 | ||
322 | if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) | |
323 | return FALSE; | |
324 | ||
325 | return memcmp(hdr, "\211PNG", WXSIZEOF(hdr)) == 0; | |
326 | } | |
327 | ||
328 | // convert data from RGB to wxImage format | |
329 | static | |
330 | void CopyDataFromPNG(wxImage *image, | |
331 | unsigned char **lines, | |
332 | png_uint_32 width, | |
333 | png_uint_32 height, | |
334 | int color_type) | |
335 | { | |
336 | Transparency transparency = Transparency_None; | |
337 | ||
338 | // only non NULL if transparency == Transparency_Alpha | |
339 | unsigned char *alpha = NULL; | |
340 | ||
341 | // RGB of the mask colour if transparency == Transparency_Mask | |
342 | // (but init them anyhow to avoid compiler warnings) | |
343 | unsigned char rMask = 0, | |
344 | gMask = 0, | |
345 | bMask = 0; | |
346 | ||
347 | unsigned char *ptrDst = image->GetData(); | |
348 | if ( !(color_type & PNG_COLOR_MASK_COLOR) ) | |
349 | { | |
350 | // grey image: GAGAGA... where G == grey component and A == alpha | |
351 | for ( png_uint_32 y = 0; y < height; y++ ) | |
352 | { | |
353 | const unsigned char *ptrSrc = lines[y]; | |
354 | for ( png_uint_32 x = 0; x < width; x++ ) | |
355 | { | |
356 | unsigned char g = *ptrSrc++; | |
357 | unsigned char a = *ptrSrc++; | |
358 | ||
359 | // the first time we encounter a transparent pixel we must | |
360 | // decide about what to do about them | |
c9fcf581 | 361 | if ( !IsOpaque(a) && transparency == Transparency_None ) |
27bb2b7c VZ |
362 | { |
363 | // we'll need at least the mask for this image and | |
364 | // maybe even full alpha channel info: the former is | |
365 | // only enough if we have alpha values of 0 and 0xff | |
366 | // only, otherwisewe need the latter | |
d26d9754 VZ |
367 | transparency = CheckTransparency |
368 | ( | |
36308e0e | 369 | lines, |
d26d9754 VZ |
370 | x, y, |
371 | width, height, | |
372 | 1 | |
373 | ); | |
27bb2b7c VZ |
374 | |
375 | if ( transparency == Transparency_Mask ) | |
376 | { | |
377 | // let's choose this colour for the mask: this is | |
378 | // not a problem here as all the other pixels are | |
379 | // grey, i.e. R == G == B which is not the case for | |
380 | // this one so no confusion is possible | |
381 | rMask = 0xff; | |
382 | gMask = 0; | |
383 | bMask = 0xff; | |
384 | } | |
385 | else // transparency == Transparency_Alpha | |
386 | { | |
387 | alpha = InitAlpha(image, x, y); | |
388 | } | |
389 | } | |
390 | ||
391 | switch ( transparency ) | |
392 | { | |
c9fcf581 VZ |
393 | case Transparency_Mask: |
394 | if ( IsTransparent(a) ) | |
395 | { | |
396 | *ptrDst++ = rMask; | |
397 | *ptrDst++ = bMask; | |
398 | *ptrDst++ = gMask; | |
399 | break; | |
400 | } | |
401 | // else: !transparent | |
402 | ||
403 | // must be opaque then as otherwise we shouldn't be | |
404 | // using the mask at all | |
405 | wxASSERT_MSG( IsOpaque(a), _T("logic error") ); | |
406 | ||
407 | // fall through | |
408 | ||
27bb2b7c | 409 | case Transparency_Alpha: |
c9fcf581 VZ |
410 | if ( alpha ) |
411 | *alpha++ = a; | |
27bb2b7c VZ |
412 | // fall through |
413 | ||
414 | case Transparency_None: | |
415 | *ptrDst++ = g; | |
416 | *ptrDst++ = g; | |
417 | *ptrDst++ = g; | |
418 | break; | |
27bb2b7c VZ |
419 | } |
420 | } | |
421 | } | |
422 | } | |
423 | else // colour image: RGBRGB... | |
424 | { | |
425 | for ( png_uint_32 y = 0; y < height; y++ ) | |
426 | { | |
427 | const unsigned char *ptrSrc = lines[y]; | |
428 | for ( png_uint_32 x = 0; x < width; x++ ) | |
429 | { | |
430 | unsigned char r = *ptrSrc++; | |
431 | unsigned char g = *ptrSrc++; | |
432 | unsigned char b = *ptrSrc++; | |
433 | unsigned char a = *ptrSrc++; | |
434 | ||
435 | // the logic here is the same as for the grey case except | |
436 | // where noted | |
c9fcf581 | 437 | if ( !IsOpaque(a) && transparency == Transparency_None ) |
27bb2b7c | 438 | { |
d26d9754 VZ |
439 | transparency = CheckTransparency |
440 | ( | |
36308e0e | 441 | lines, |
d26d9754 VZ |
442 | x, y, |
443 | width, height, | |
444 | 3 | |
445 | ); | |
27bb2b7c VZ |
446 | |
447 | if ( transparency == Transparency_Mask ) | |
448 | { | |
449 | FindMaskColour(lines, width, height, | |
450 | rMask, gMask, bMask); | |
451 | } | |
452 | else // transparency == Transparency_Alpha | |
453 | { | |
454 | alpha = InitAlpha(image, x, y); | |
455 | } | |
456 | ||
457 | } | |
458 | ||
459 | switch ( transparency ) | |
460 | { | |
c9fcf581 VZ |
461 | case Transparency_Mask: |
462 | if ( IsTransparent(a) ) | |
463 | { | |
464 | // if we couldn't find a unique colour for the mask, we | |
465 | // can have real pixels with the same value as the mask | |
466 | // and it's better to slightly change their colour than | |
467 | // to make them transparent | |
468 | if ( r == rMask && g == gMask && b == bMask ) | |
469 | { | |
470 | r++; | |
471 | } | |
472 | ||
473 | *ptrDst++ = rMask; | |
474 | *ptrDst++ = bMask; | |
475 | *ptrDst++ = gMask; | |
476 | break; | |
477 | } | |
478 | // else: !transparent | |
479 | ||
480 | // must be opaque then as otherwise we shouldn't be | |
481 | // using the mask at all | |
482 | wxASSERT_MSG( IsOpaque(a), _T("logic error") ); | |
483 | ||
484 | // fall through | |
485 | ||
27bb2b7c | 486 | case Transparency_Alpha: |
c9fcf581 VZ |
487 | if ( alpha ) |
488 | *alpha++ = a; | |
27bb2b7c VZ |
489 | // fall through |
490 | ||
491 | case Transparency_None: | |
492 | *ptrDst++ = r; | |
493 | *ptrDst++ = g; | |
494 | *ptrDst++ = b; | |
495 | break; | |
27bb2b7c VZ |
496 | } |
497 | } | |
498 | } | |
499 | } | |
500 | ||
501 | if ( transparency == Transparency_Mask ) | |
502 | { | |
503 | image->SetMaskColour(rMask, gMask, bMask); | |
504 | } | |
505 | } | |
506 | ||
3ca6a5f0 BP |
507 | // temporarily disable the warning C4611 (interaction between '_setjmp' and |
508 | // C++ object destruction is non-portable) - I don't see any dtors here | |
509 | #ifdef __VISUALC__ | |
510 | #pragma warning(disable:4611) | |
511 | #endif /* VC++ */ | |
512 | ||
27bb2b7c VZ |
513 | bool |
514 | wxPNGHandler::LoadFile(wxImage *image, | |
515 | wxInputStream& stream, | |
516 | bool verbose, | |
517 | int WXUNUSED(index)) | |
e9c4b1a2 JS |
518 | { |
519 | // VZ: as this function uses setjmp() the only fool proof error handling | |
520 | // method is to use goto (setjmp is not really C++ dtors friendly...) | |
717b9bf2 | 521 | |
27bb2b7c VZ |
522 | unsigned char **lines = NULL; |
523 | png_infop info_ptr = (png_infop) NULL; | |
bdffd806 VS |
524 | wxPNGInfoStruct wxinfo; |
525 | ||
526 | wxinfo.verbose = verbose; | |
527 | wxinfo.stream.in = &stream; | |
717b9bf2 | 528 | |
e9c4b1a2 | 529 | image->Destroy(); |
717b9bf2 | 530 | |
e9c4b1a2 JS |
531 | png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, |
532 | (voidp) NULL, | |
533 | (png_error_ptr) NULL, | |
534 | (png_error_ptr) NULL ); | |
535 | if (!png_ptr) | |
27bb2b7c | 536 | goto error; |
717b9bf2 | 537 | |
bdffd806 VS |
538 | png_set_error_fn(png_ptr, (png_voidp)NULL, wx_png_error, wx_png_warning); |
539 | ||
540 | // NB: please see the comment near wxPNGInfoStruct declaration for | |
541 | // explanation why this line is mandatory | |
542 | png_set_read_fn( png_ptr, &wxinfo, _PNG_stream_reader); | |
deb2fec0 | 543 | |
e9c4b1a2 JS |
544 | info_ptr = png_create_info_struct( png_ptr ); |
545 | if (!info_ptr) | |
27bb2b7c | 546 | goto error; |
717b9bf2 | 547 | |
bdffd806 | 548 | if (setjmp(wxinfo.jmpbuf)) |
27bb2b7c | 549 | goto error; |
717b9bf2 | 550 | |
27bb2b7c VZ |
551 | png_uint_32 i, width, height; |
552 | int bit_depth, color_type, interlace_type; | |
717b9bf2 | 553 | |
e9c4b1a2 JS |
554 | png_read_info( png_ptr, info_ptr ); |
555 | png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, (int*) NULL, (int*) NULL ); | |
717b9bf2 | 556 | |
e9c4b1a2 JS |
557 | if (color_type == PNG_COLOR_TYPE_PALETTE) |
558 | png_set_expand( png_ptr ); | |
717b9bf2 | 559 | |
2b5f62a0 VZ |
560 | // Fix for Bug [ 439207 ] Monochrome PNG images come up black |
561 | if (bit_depth < 8) | |
562 | png_set_expand( png_ptr ); | |
563 | ||
e9c4b1a2 JS |
564 | png_set_strip_16( png_ptr ); |
565 | png_set_packing( png_ptr ); | |
566 | if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS)) | |
567 | png_set_expand( png_ptr ); | |
568 | png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER ); | |
717b9bf2 | 569 | |
479cd5de | 570 | image->Create( (int)width, (int)height ); |
717b9bf2 | 571 | |
e9c4b1a2 | 572 | if (!image->Ok()) |
27bb2b7c | 573 | goto error; |
717b9bf2 | 574 | |
479cd5de | 575 | lines = (unsigned char **)malloc( (size_t)(height * sizeof(unsigned char *)) ); |
27bb2b7c VZ |
576 | if ( !lines ) |
577 | goto error; | |
717b9bf2 | 578 | |
e9c4b1a2 JS |
579 | for (i = 0; i < height; i++) |
580 | { | |
479cd5de | 581 | if ((lines[i] = (unsigned char *)malloc( (size_t)(width * (sizeof(unsigned char) * 4)))) == NULL) |
e9c4b1a2 JS |
582 | { |
583 | for ( unsigned int n = 0; n < i; n++ ) | |
584 | free( lines[n] ); | |
585 | goto error; | |
586 | } | |
587 | } | |
717b9bf2 | 588 | |
27bb2b7c VZ |
589 | png_read_image( png_ptr, lines ); |
590 | png_read_end( png_ptr, info_ptr ); | |
591 | png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL ); | |
717b9bf2 | 592 | |
27bb2b7c VZ |
593 | // loaded successfully, now init wxImage with this data |
594 | CopyDataFromPNG(image, lines, width, height, color_type); | |
717b9bf2 | 595 | |
27bb2b7c VZ |
596 | for ( i = 0; i < height; i++ ) |
597 | free( lines[i] ); | |
598 | free( lines ); | |
717b9bf2 | 599 | |
e9c4b1a2 | 600 | return TRUE; |
95ee0ac8 | 601 | |
27bb2b7c | 602 | error: |
58c837a4 RR |
603 | if (verbose) |
604 | wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory.")); | |
717b9bf2 | 605 | |
e9c4b1a2 JS |
606 | if ( image->Ok() ) |
607 | { | |
608 | image->Destroy(); | |
609 | } | |
717b9bf2 | 610 | |
e9c4b1a2 JS |
611 | if ( lines ) |
612 | { | |
613 | free( lines ); | |
614 | } | |
717b9bf2 | 615 | |
e9c4b1a2 JS |
616 | if ( png_ptr ) |
617 | { | |
618 | if ( info_ptr ) | |
619 | { | |
620 | png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL ); | |
621 | free(info_ptr); | |
622 | } | |
623 | else | |
624 | png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL ); | |
625 | } | |
626 | return FALSE; | |
627 | } | |
628 | ||
27bb2b7c VZ |
629 | // ---------------------------------------------------------------------------- |
630 | // writing PNGs | |
631 | // ---------------------------------------------------------------------------- | |
632 | ||
deb2fec0 | 633 | bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) |
e9c4b1a2 | 634 | { |
bdffd806 | 635 | wxPNGInfoStruct wxinfo; |
717b9bf2 | 636 | |
bdffd806 VS |
637 | wxinfo.verbose = verbose; |
638 | wxinfo.stream.out = &stream; | |
deb2fec0 | 639 | |
bdffd806 VS |
640 | png_structp png_ptr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
641 | if (!png_ptr) | |
642 | { | |
643 | if (verbose) | |
644 | wxLogError(_("Couldn't save PNG image.")); | |
645 | return FALSE; | |
646 | } | |
717b9bf2 | 647 | |
bdffd806 | 648 | png_set_error_fn(png_ptr, (png_voidp)NULL, wx_png_error, wx_png_warning); |
717b9bf2 | 649 | |
bdffd806 VS |
650 | png_infop info_ptr = png_create_info_struct(png_ptr); |
651 | if (info_ptr == NULL) | |
652 | { | |
653 | png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); | |
654 | if (verbose) | |
655 | wxLogError(_("Couldn't save PNG image.")); | |
656 | return FALSE; | |
657 | } | |
717b9bf2 | 658 | |
bdffd806 VS |
659 | if (setjmp(wxinfo.jmpbuf)) |
660 | { | |
661 | png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); | |
662 | if (verbose) | |
663 | wxLogError(_("Couldn't save PNG image.")); | |
664 | return FALSE; | |
665 | } | |
717b9bf2 | 666 | |
bdffd806 VS |
667 | // NB: please see the comment near wxPNGInfoStruct declaration for |
668 | // explanation why this line is mandatory | |
669 | png_set_write_fn( png_ptr, &wxinfo, _PNG_stream_writer, NULL); | |
670 | ||
671 | png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(), 8, | |
672 | PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, | |
673 | PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); | |
674 | ||
675 | png_color_8 sig_bit; | |
676 | sig_bit.red = 8; | |
677 | sig_bit.green = 8; | |
678 | sig_bit.blue = 8; | |
679 | sig_bit.alpha = 8; | |
680 | png_set_sBIT( png_ptr, info_ptr, &sig_bit ); | |
681 | png_write_info( png_ptr, info_ptr ); | |
682 | png_set_shift( png_ptr, &sig_bit ); | |
683 | png_set_packing( png_ptr ); | |
717b9bf2 | 684 | |
bdffd806 VS |
685 | unsigned char *data = (unsigned char *)malloc( image->GetWidth()*4 ); |
686 | if (!data) | |
687 | { | |
688 | png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); | |
689 | return FALSE; | |
690 | } | |
717b9bf2 | 691 | |
bdffd806 VS |
692 | for (int y = 0; y < image->GetHeight(); y++) |
693 | { | |
694 | unsigned char *ptr = image->GetData() + (y * image->GetWidth() * 3); | |
695 | for (int x = 0; x < image->GetWidth(); x++) | |
e9c4b1a2 | 696 | { |
bdffd806 VS |
697 | data[(x << 2) + 0] = *ptr++; |
698 | data[(x << 2) + 1] = *ptr++; | |
699 | data[(x << 2) + 2] = *ptr++; | |
700 | if (( !image->HasMask() ) || \ | |
701 | (data[(x << 2) + 0] != image->GetMaskRed()) || \ | |
702 | (data[(x << 2) + 1] != image->GetMaskGreen()) || \ | |
703 | (data[(x << 2) + 2] != image->GetMaskBlue())) | |
e9c4b1a2 | 704 | { |
bdffd806 VS |
705 | data[(x << 2) + 3] = 255; |
706 | } | |
707 | else | |
708 | { | |
709 | data[(x << 2) + 3] = 0; | |
e9c4b1a2 | 710 | } |
e9c4b1a2 | 711 | } |
bdffd806 VS |
712 | png_bytep row_ptr = data; |
713 | png_write_rows( png_ptr, &row_ptr, 1 ); | |
e9c4b1a2 | 714 | } |
bdffd806 VS |
715 | |
716 | free(data); | |
717 | png_write_end( png_ptr, info_ptr ); | |
718 | png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr ); | |
719 | ||
e9c4b1a2 JS |
720 | return TRUE; |
721 | } | |
e9c4b1a2 | 722 | |
3ca6a5f0 BP |
723 | #ifdef __VISUALC__ |
724 | #pragma warning(default:4611) | |
725 | #endif /* VC++ */ | |
726 | ||
9ab6ee85 | 727 | #endif // wxUSE_STREAMS |
e9c4b1a2 | 728 | |
9ab6ee85 | 729 | #endif // wxUSE_LIBPNG |