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