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