]>
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 |
c40158e4 | 108 | #if defined(__WATCOMC__) && ( defined(__WXMSW__) || __WATCOMC__ > 1230 ) |
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 SB |
171 | // from pngerror.c |
172 | // so that the libpng doesn't send anything on stderr | |
173 | void | |
ce403057 | 174 | PNGLINKAGEMODE wx_png_error(png_structp WXUNUSED(png_ptr), png_const_charp message) |
deb2fec0 | 175 | { |
d10c19d6 | 176 | wxLogFatalError( wxString::FromAscii(message) ); |
deb2fec0 SB |
177 | } |
178 | ||
179 | void | |
bdffd806 | 180 | PNGLINKAGEMODE wx_png_warning(png_structp png_ptr, png_const_charp message) |
deb2fec0 | 181 | { |
d10c19d6 VZ |
182 | wxPNGInfoStruct *info = png_ptr ? WX_PNG_INFO(png_ptr) : NULL; |
183 | if ( !info || info->verbose ) | |
2b5f62a0 | 184 | wxLogWarning( wxString::FromAscii(message) ); |
deb2fec0 SB |
185 | } |
186 | ||
90350682 VZ |
187 | } // extern "C" |
188 | ||
27bb2b7c VZ |
189 | // ---------------------------------------------------------------------------- |
190 | // LoadFile() helpers | |
191 | // ---------------------------------------------------------------------------- | |
192 | ||
d26d9754 | 193 | // determine the kind of transparency we need for this image: if the only alpha |
c9fcf581 VZ |
194 | // values it has are 0 (transparent) and 0xff (opaque) then we can simply |
195 | // create a mask for it, we should be ok with a simple mask but otherwise we | |
196 | // need a full blown alpha channel in wxImage | |
d26d9754 VZ |
197 | // |
198 | // parameters: | |
36308e0e | 199 | // lines raw PNG data |
d26d9754 VZ |
200 | // x, y starting position |
201 | // w, h size of the image | |
202 | // numColBytes number of colour bytes (1 for grey scale, 3 for RGB) | |
203 | // (NB: alpha always follows the colour bytes) | |
27bb2b7c | 204 | Transparency |
36308e0e | 205 | CheckTransparency(unsigned char **lines, |
d26d9754 VZ |
206 | png_uint_32 x, png_uint_32 y, png_uint_32 w, png_uint_32 h, |
207 | size_t numColBytes) | |
27bb2b7c | 208 | { |
d26d9754 VZ |
209 | // suppose that a mask will suffice and check all the remaining alpha |
210 | // values to see if it does | |
36308e0e | 211 | for ( ; y < h; y++ ) |
27bb2b7c | 212 | { |
e45cc235 RD |
213 | // each pixel is numColBytes+1 bytes, offset into the current line by |
214 | // the current x position | |
215 | unsigned const char *ptr = lines[y] + (x * (numColBytes + 1)); | |
36308e0e | 216 | |
c9fcf581 | 217 | for ( png_uint_32 x2 = x; x2 < w; x2++ ) |
27bb2b7c | 218 | { |
c9fcf581 | 219 | // skip the grey or colour byte(s) |
36308e0e | 220 | ptr += numColBytes; |
27bb2b7c | 221 | |
36308e0e | 222 | unsigned char a2 = *ptr++; |
c9fcf581 VZ |
223 | |
224 | if ( !IsTransparent(a2) && !IsOpaque(a2) ) | |
27bb2b7c | 225 | { |
d26d9754 VZ |
226 | // not fully opaque nor fully transparent, hence need alpha |
227 | return Transparency_Alpha; | |
27bb2b7c | 228 | } |
27bb2b7c | 229 | } |
c9fcf581 VZ |
230 | |
231 | // during the next loop iteration check all the pixels in the row | |
232 | x = 0; | |
27bb2b7c VZ |
233 | } |
234 | ||
d26d9754 VZ |
235 | // mask will be enough |
236 | return Transparency_Mask; | |
27bb2b7c VZ |
237 | } |
238 | ||
239 | unsigned char *InitAlpha(wxImage *image, png_uint_32 x, png_uint_32 y) | |
240 | { | |
241 | // create alpha channel | |
242 | image->SetAlpha(); | |
243 | ||
244 | unsigned char *alpha = image->GetAlpha(); | |
245 | ||
246 | // set alpha for the pixels we had so far | |
c9fcf581 VZ |
247 | png_uint_32 end = y * image->GetWidth() + x; |
248 | for ( png_uint_32 i = 0; i < end; i++ ) | |
27bb2b7c | 249 | { |
c9fcf581 VZ |
250 | // all the previous pixels were opaque |
251 | *alpha++ = 0xff; | |
27bb2b7c VZ |
252 | } |
253 | ||
254 | return alpha; | |
255 | } | |
256 | ||
257 | void | |
258 | FindMaskColour(unsigned char **lines, png_uint_32 width, png_uint_32 height, | |
259 | unsigned char& rMask, unsigned char& gMask, unsigned char& bMask) | |
260 | { | |
261 | // choosing the colour for the mask is more | |
262 | // difficult: we need to iterate over the entire | |
263 | // image for this in order to choose an unused | |
264 | // colour (this is not very efficient but what else | |
265 | // can we do?) | |
266 | wxImageHistogram h; | |
267 | unsigned nentries = 0; | |
268 | unsigned char r2, g2, b2; | |
269 | for ( png_uint_32 y2 = 0; y2 < height; y2++ ) | |
270 | { | |
271 | const unsigned char *p = lines[y2]; | |
272 | for ( png_uint_32 x2 = 0; x2 < width; x2++ ) | |
273 | { | |
274 | r2 = *p++; | |
275 | g2 = *p++; | |
276 | b2 = *p++; | |
277 | ||
278 | wxImageHistogramEntry& | |
279 | entry = h[wxImageHistogram:: MakeKey(r2, g2, b2)]; | |
280 | ||
281 | if ( entry.value++ == 0 ) | |
282 | entry.index = nentries++; | |
283 | } | |
284 | } | |
285 | ||
f773e9b0 | 286 | if ( !h.FindFirstUnusedColour(&rMask, &gMask, &bMask) ) |
27bb2b7c VZ |
287 | { |
288 | wxLogWarning(_("Too many colours in PNG, the image may be slightly blurred.")); | |
289 | ||
290 | // use a fixed mask colour and we'll fudge | |
291 | // the real pixels with this colour (see | |
292 | // below) | |
293 | rMask = 0xfe; | |
294 | gMask = 0; | |
295 | bMask = 0xff; | |
296 | } | |
297 | } | |
298 | ||
299 | // ---------------------------------------------------------------------------- | |
300 | // reading PNGs | |
301 | // ---------------------------------------------------------------------------- | |
302 | ||
303 | bool wxPNGHandler::DoCanRead( wxInputStream& stream ) | |
304 | { | |
305 | unsigned char hdr[4]; | |
306 | ||
307 | if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) | |
7beb59f3 | 308 | return false; |
27bb2b7c VZ |
309 | |
310 | return memcmp(hdr, "\211PNG", WXSIZEOF(hdr)) == 0; | |
311 | } | |
312 | ||
313 | // convert data from RGB to wxImage format | |
314 | static | |
315 | void CopyDataFromPNG(wxImage *image, | |
316 | unsigned char **lines, | |
317 | png_uint_32 width, | |
318 | png_uint_32 height, | |
319 | int color_type) | |
320 | { | |
321 | Transparency transparency = Transparency_None; | |
322 | ||
323 | // only non NULL if transparency == Transparency_Alpha | |
324 | unsigned char *alpha = NULL; | |
325 | ||
326 | // RGB of the mask colour if transparency == Transparency_Mask | |
327 | // (but init them anyhow to avoid compiler warnings) | |
328 | unsigned char rMask = 0, | |
329 | gMask = 0, | |
330 | bMask = 0; | |
331 | ||
332 | unsigned char *ptrDst = image->GetData(); | |
333 | if ( !(color_type & PNG_COLOR_MASK_COLOR) ) | |
334 | { | |
335 | // grey image: GAGAGA... where G == grey component and A == alpha | |
336 | for ( png_uint_32 y = 0; y < height; y++ ) | |
337 | { | |
338 | const unsigned char *ptrSrc = lines[y]; | |
339 | for ( png_uint_32 x = 0; x < width; x++ ) | |
340 | { | |
341 | unsigned char g = *ptrSrc++; | |
342 | unsigned char a = *ptrSrc++; | |
343 | ||
344 | // the first time we encounter a transparent pixel we must | |
345 | // decide about what to do about them | |
c9fcf581 | 346 | if ( !IsOpaque(a) && transparency == Transparency_None ) |
27bb2b7c VZ |
347 | { |
348 | // we'll need at least the mask for this image and | |
349 | // maybe even full alpha channel info: the former is | |
350 | // only enough if we have alpha values of 0 and 0xff | |
351 | // only, otherwisewe need the latter | |
d26d9754 VZ |
352 | transparency = CheckTransparency |
353 | ( | |
36308e0e | 354 | lines, |
d26d9754 VZ |
355 | x, y, |
356 | width, height, | |
357 | 1 | |
358 | ); | |
27bb2b7c VZ |
359 | |
360 | if ( transparency == Transparency_Mask ) | |
361 | { | |
362 | // let's choose this colour for the mask: this is | |
363 | // not a problem here as all the other pixels are | |
364 | // grey, i.e. R == G == B which is not the case for | |
365 | // this one so no confusion is possible | |
366 | rMask = 0xff; | |
367 | gMask = 0; | |
368 | bMask = 0xff; | |
369 | } | |
370 | else // transparency == Transparency_Alpha | |
371 | { | |
372 | alpha = InitAlpha(image, x, y); | |
373 | } | |
374 | } | |
375 | ||
376 | switch ( transparency ) | |
377 | { | |
c9fcf581 VZ |
378 | case Transparency_Mask: |
379 | if ( IsTransparent(a) ) | |
380 | { | |
381 | *ptrDst++ = rMask; | |
382 | *ptrDst++ = bMask; | |
383 | *ptrDst++ = gMask; | |
384 | break; | |
385 | } | |
386 | // else: !transparent | |
387 | ||
388 | // must be opaque then as otherwise we shouldn't be | |
389 | // using the mask at all | |
390 | wxASSERT_MSG( IsOpaque(a), _T("logic error") ); | |
391 | ||
392 | // fall through | |
393 | ||
27bb2b7c | 394 | case Transparency_Alpha: |
c9fcf581 VZ |
395 | if ( alpha ) |
396 | *alpha++ = a; | |
27bb2b7c VZ |
397 | // fall through |
398 | ||
399 | case Transparency_None: | |
400 | *ptrDst++ = g; | |
401 | *ptrDst++ = g; | |
402 | *ptrDst++ = g; | |
403 | break; | |
27bb2b7c VZ |
404 | } |
405 | } | |
406 | } | |
407 | } | |
408 | else // colour image: RGBRGB... | |
409 | { | |
410 | for ( png_uint_32 y = 0; y < height; y++ ) | |
411 | { | |
412 | const unsigned char *ptrSrc = lines[y]; | |
413 | for ( png_uint_32 x = 0; x < width; x++ ) | |
414 | { | |
415 | unsigned char r = *ptrSrc++; | |
416 | unsigned char g = *ptrSrc++; | |
417 | unsigned char b = *ptrSrc++; | |
418 | unsigned char a = *ptrSrc++; | |
419 | ||
420 | // the logic here is the same as for the grey case except | |
421 | // where noted | |
c9fcf581 | 422 | if ( !IsOpaque(a) && transparency == Transparency_None ) |
27bb2b7c | 423 | { |
d26d9754 VZ |
424 | transparency = CheckTransparency |
425 | ( | |
36308e0e | 426 | lines, |
d26d9754 VZ |
427 | x, y, |
428 | width, height, | |
429 | 3 | |
430 | ); | |
27bb2b7c VZ |
431 | |
432 | if ( transparency == Transparency_Mask ) | |
433 | { | |
f773e9b0 RR |
434 | FindMaskColour(lines, width, height, |
435 | rMask, gMask, bMask); | |
27bb2b7c VZ |
436 | } |
437 | else // transparency == Transparency_Alpha | |
438 | { | |
439 | alpha = InitAlpha(image, x, y); | |
440 | } | |
441 | ||
442 | } | |
443 | ||
444 | switch ( transparency ) | |
445 | { | |
c9fcf581 VZ |
446 | case Transparency_Mask: |
447 | if ( IsTransparent(a) ) | |
448 | { | |
c9fcf581 VZ |
449 | *ptrDst++ = rMask; |
450 | *ptrDst++ = bMask; | |
451 | *ptrDst++ = gMask; | |
452 | break; | |
453 | } | |
999836aa VZ |
454 | else // !transparent |
455 | { | |
456 | // must be opaque then as otherwise we shouldn't be | |
457 | // using the mask at all | |
458 | wxASSERT_MSG( IsOpaque(a), _T("logic error") ); | |
459 | ||
460 | // if we couldn't find a unique colour for the | |
461 | // mask, we can have real pixels with the same | |
462 | // value as the mask and it's better to slightly | |
463 | // change their colour than to make them | |
464 | // transparent | |
465 | if ( r == rMask && g == gMask && b == bMask ) | |
466 | { | |
467 | r++; | |
468 | } | |
469 | } | |
c9fcf581 VZ |
470 | |
471 | // fall through | |
472 | ||
27bb2b7c | 473 | case Transparency_Alpha: |
c9fcf581 VZ |
474 | if ( alpha ) |
475 | *alpha++ = a; | |
27bb2b7c VZ |
476 | // fall through |
477 | ||
478 | case Transparency_None: | |
479 | *ptrDst++ = r; | |
480 | *ptrDst++ = g; | |
481 | *ptrDst++ = b; | |
482 | break; | |
27bb2b7c VZ |
483 | } |
484 | } | |
485 | } | |
486 | } | |
487 | ||
488 | if ( transparency == Transparency_Mask ) | |
489 | { | |
490 | image->SetMaskColour(rMask, gMask, bMask); | |
491 | } | |
492 | } | |
493 | ||
3ca6a5f0 BP |
494 | // temporarily disable the warning C4611 (interaction between '_setjmp' and |
495 | // C++ object destruction is non-portable) - I don't see any dtors here | |
496 | #ifdef __VISUALC__ | |
497 | #pragma warning(disable:4611) | |
498 | #endif /* VC++ */ | |
499 | ||
27bb2b7c VZ |
500 | bool |
501 | wxPNGHandler::LoadFile(wxImage *image, | |
502 | wxInputStream& stream, | |
503 | bool verbose, | |
504 | int WXUNUSED(index)) | |
e9c4b1a2 JS |
505 | { |
506 | // VZ: as this function uses setjmp() the only fool proof error handling | |
507 | // method is to use goto (setjmp is not really C++ dtors friendly...) | |
717b9bf2 | 508 | |
27bb2b7c VZ |
509 | unsigned char **lines = NULL; |
510 | png_infop info_ptr = (png_infop) NULL; | |
bdffd806 VS |
511 | wxPNGInfoStruct wxinfo; |
512 | ||
513 | wxinfo.verbose = verbose; | |
514 | wxinfo.stream.in = &stream; | |
717b9bf2 | 515 | |
e9c4b1a2 | 516 | image->Destroy(); |
717b9bf2 | 517 | |
d10c19d6 VZ |
518 | png_structp png_ptr = png_create_read_struct |
519 | ( | |
520 | PNG_LIBPNG_VER_STRING, | |
521 | (voidp) NULL, | |
522 | wx_png_error, | |
523 | wx_png_warning | |
524 | ); | |
e9c4b1a2 | 525 | if (!png_ptr) |
27bb2b7c | 526 | goto error; |
717b9bf2 | 527 | |
bdffd806 VS |
528 | // NB: please see the comment near wxPNGInfoStruct declaration for |
529 | // explanation why this line is mandatory | |
e9b964cf | 530 | png_set_read_fn( png_ptr, &wxinfo, wx_PNG_stream_reader); |
deb2fec0 | 531 | |
e9c4b1a2 JS |
532 | info_ptr = png_create_info_struct( png_ptr ); |
533 | if (!info_ptr) | |
27bb2b7c | 534 | goto error; |
717b9bf2 | 535 | |
bdffd806 | 536 | if (setjmp(wxinfo.jmpbuf)) |
27bb2b7c | 537 | goto error; |
717b9bf2 | 538 | |
27bb2b7c VZ |
539 | png_uint_32 i, width, height; |
540 | int bit_depth, color_type, interlace_type; | |
717b9bf2 | 541 | |
e9c4b1a2 JS |
542 | png_read_info( png_ptr, info_ptr ); |
543 | png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, (int*) NULL, (int*) NULL ); | |
717b9bf2 | 544 | |
e9c4b1a2 JS |
545 | if (color_type == PNG_COLOR_TYPE_PALETTE) |
546 | png_set_expand( png_ptr ); | |
717b9bf2 | 547 | |
2b5f62a0 VZ |
548 | // Fix for Bug [ 439207 ] Monochrome PNG images come up black |
549 | if (bit_depth < 8) | |
550 | png_set_expand( png_ptr ); | |
551 | ||
e9c4b1a2 JS |
552 | png_set_strip_16( png_ptr ); |
553 | png_set_packing( png_ptr ); | |
554 | if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS)) | |
555 | png_set_expand( png_ptr ); | |
556 | png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER ); | |
717b9bf2 | 557 | |
154e1ca1 | 558 | image->Create((int)width, (int)height, (bool) false /* no need to init pixels */); |
717b9bf2 | 559 | |
e9c4b1a2 | 560 | if (!image->Ok()) |
27bb2b7c | 561 | goto error; |
717b9bf2 | 562 | |
479cd5de | 563 | lines = (unsigned char **)malloc( (size_t)(height * sizeof(unsigned char *)) ); |
27bb2b7c VZ |
564 | if ( !lines ) |
565 | goto error; | |
717b9bf2 | 566 | |
e9c4b1a2 JS |
567 | for (i = 0; i < height; i++) |
568 | { | |
479cd5de | 569 | if ((lines[i] = (unsigned char *)malloc( (size_t)(width * (sizeof(unsigned char) * 4)))) == NULL) |
e9c4b1a2 JS |
570 | { |
571 | for ( unsigned int n = 0; n < i; n++ ) | |
572 | free( lines[n] ); | |
573 | goto error; | |
574 | } | |
575 | } | |
717b9bf2 | 576 | |
27bb2b7c VZ |
577 | png_read_image( png_ptr, lines ); |
578 | png_read_end( png_ptr, info_ptr ); | |
579 | png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL ); | |
717b9bf2 | 580 | |
27bb2b7c VZ |
581 | // loaded successfully, now init wxImage with this data |
582 | CopyDataFromPNG(image, lines, width, height, color_type); | |
717b9bf2 | 583 | |
27bb2b7c VZ |
584 | for ( i = 0; i < height; i++ ) |
585 | free( lines[i] ); | |
586 | free( lines ); | |
717b9bf2 | 587 | |
7beb59f3 | 588 | return true; |
95ee0ac8 | 589 | |
27bb2b7c | 590 | error: |
58c837a4 RR |
591 | if (verbose) |
592 | wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory.")); | |
717b9bf2 | 593 | |
e9c4b1a2 JS |
594 | if ( image->Ok() ) |
595 | { | |
596 | image->Destroy(); | |
597 | } | |
717b9bf2 | 598 | |
e9c4b1a2 JS |
599 | if ( lines ) |
600 | { | |
601 | free( lines ); | |
602 | } | |
717b9bf2 | 603 | |
e9c4b1a2 JS |
604 | if ( png_ptr ) |
605 | { | |
606 | if ( info_ptr ) | |
607 | { | |
608 | png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL ); | |
609 | free(info_ptr); | |
610 | } | |
611 | else | |
612 | png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL ); | |
613 | } | |
7beb59f3 | 614 | return false; |
e9c4b1a2 JS |
615 | } |
616 | ||
27bb2b7c VZ |
617 | // ---------------------------------------------------------------------------- |
618 | // writing PNGs | |
619 | // ---------------------------------------------------------------------------- | |
620 | ||
deb2fec0 | 621 | bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) |
e9c4b1a2 | 622 | { |
bdffd806 | 623 | wxPNGInfoStruct wxinfo; |
717b9bf2 | 624 | |
bdffd806 VS |
625 | wxinfo.verbose = verbose; |
626 | wxinfo.stream.out = &stream; | |
deb2fec0 | 627 | |
d10c19d6 VZ |
628 | png_structp png_ptr = png_create_write_struct |
629 | ( | |
630 | PNG_LIBPNG_VER_STRING, | |
631 | NULL, | |
632 | wx_png_error, | |
633 | wx_png_warning | |
634 | ); | |
bdffd806 VS |
635 | if (!png_ptr) |
636 | { | |
637 | if (verbose) | |
638 | wxLogError(_("Couldn't save PNG image.")); | |
7beb59f3 | 639 | return false; |
bdffd806 | 640 | } |
717b9bf2 | 641 | |
bdffd806 VS |
642 | png_infop info_ptr = png_create_info_struct(png_ptr); |
643 | if (info_ptr == NULL) | |
644 | { | |
645 | png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); | |
646 | if (verbose) | |
647 | wxLogError(_("Couldn't save PNG image.")); | |
7beb59f3 | 648 | return false; |
bdffd806 | 649 | } |
717b9bf2 | 650 | |
bdffd806 VS |
651 | if (setjmp(wxinfo.jmpbuf)) |
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 | // NB: please see the comment near wxPNGInfoStruct declaration for |
660 | // explanation why this line is mandatory | |
e9b964cf | 661 | png_set_write_fn( png_ptr, &wxinfo, wx_PNG_stream_writer, NULL); |
bdffd806 | 662 | |
a4efa721 VZ |
663 | const int iColorType = image->HasOption(wxIMAGE_OPTION_PNG_FORMAT) |
664 | ? image->GetOptionInt(wxIMAGE_OPTION_PNG_FORMAT) | |
665 | : wxPNG_TYPE_COLOUR; | |
666 | const int iBitDepth = image->HasOption(wxIMAGE_OPTION_PNG_BITDEPTH) | |
667 | ? image->GetOptionInt(wxIMAGE_OPTION_PNG_BITDEPTH) | |
668 | : 8; | |
669 | ||
670 | wxASSERT_MSG( iBitDepth == 8 || iBitDepth == 16, | |
671 | _T("PNG bit depth must be 8 or 16") ); | |
672 | ||
673 | bool bHasAlpha = image->HasAlpha(); | |
674 | bool bHasMask = image->HasMask(); | |
675 | bool bUseAlpha = bHasAlpha || bHasMask; | |
676 | ||
677 | int iPngColorType; | |
678 | if ( iColorType==wxPNG_TYPE_COLOUR ) | |
679 | { | |
680 | iPngColorType = bUseAlpha ? PNG_COLOR_TYPE_RGB_ALPHA | |
681 | : PNG_COLOR_TYPE_RGB; | |
682 | } | |
683 | else | |
684 | { | |
685 | iPngColorType = bUseAlpha ? PNG_COLOR_TYPE_GRAY_ALPHA | |
686 | : PNG_COLOR_TYPE_GRAY; | |
687 | } | |
bdffd806 | 688 | |
a4efa721 VZ |
689 | png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(), |
690 | iBitDepth, iPngColorType, | |
691 | PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, | |
692 | PNG_FILTER_TYPE_BASE); | |
693 | ||
694 | int iElements; | |
bdffd806 | 695 | png_color_8 sig_bit; |
a4efa721 VZ |
696 | |
697 | if ( iPngColorType & PNG_COLOR_MASK_COLOR ) | |
698 | { | |
699 | sig_bit.red = | |
700 | sig_bit.green = | |
701 | sig_bit.blue = (png_byte)iBitDepth; | |
702 | iElements = 3; | |
703 | } | |
704 | else // grey | |
705 | { | |
706 | sig_bit.gray = (png_byte)iBitDepth; | |
707 | iElements = 1; | |
708 | } | |
709 | ||
710 | if ( iPngColorType & PNG_COLOR_MASK_ALPHA ) | |
711 | { | |
712 | sig_bit.alpha = (png_byte)iBitDepth; | |
713 | iElements++; | |
714 | } | |
715 | ||
dc683654 VZ |
716 | if ( iBitDepth == 16 ) |
717 | iElements *= 2; | |
718 | ||
bdffd806 VS |
719 | png_set_sBIT( png_ptr, info_ptr, &sig_bit ); |
720 | png_write_info( png_ptr, info_ptr ); | |
721 | png_set_shift( png_ptr, &sig_bit ); | |
722 | png_set_packing( png_ptr ); | |
717b9bf2 | 723 | |
a4efa721 VZ |
724 | unsigned char * |
725 | data = (unsigned char *)malloc( image->GetWidth() * iElements ); | |
726 | if ( !data ) | |
bdffd806 VS |
727 | { |
728 | png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); | |
7beb59f3 | 729 | return false; |
bdffd806 | 730 | } |
717b9bf2 | 731 | |
a4efa721 VZ |
732 | unsigned char * |
733 | pAlpha = (unsigned char *)(bHasAlpha ? image->GetAlpha() : NULL); | |
734 | int iHeight = image->GetHeight(); | |
735 | int iWidth = image->GetWidth(); | |
736 | ||
4c2740ec WS |
737 | unsigned char uchMaskRed = 0, uchMaskGreen = 0, uchMaskBlue = 0; |
738 | ||
739 | if ( bHasMask ) | |
740 | { | |
741 | uchMaskRed = image->GetMaskRed(); | |
742 | uchMaskGreen = image->GetMaskGreen(); | |
743 | uchMaskBlue = image->GetMaskBlue(); | |
744 | } | |
745 | ||
a4efa721 VZ |
746 | unsigned char *pColors = image->GetData(); |
747 | ||
748 | for (int y = 0; y != iHeight; ++y) | |
bdffd806 | 749 | { |
a4efa721 VZ |
750 | unsigned char *pData = data; |
751 | for (int x = 0; x != iWidth; x++) | |
e9c4b1a2 | 752 | { |
a4efa721 VZ |
753 | unsigned char uchRed = *pColors++; |
754 | unsigned char uchGreen = *pColors++; | |
755 | unsigned char uchBlue = *pColors++; | |
e1e36a3e | 756 | |
a4efa721 | 757 | switch ( iColorType ) |
bdffd806 | 758 | { |
a4efa721 VZ |
759 | default: |
760 | wxFAIL_MSG( _T("unknown wxPNG_TYPE_XXX") ); | |
761 | // fall through | |
762 | ||
763 | case wxPNG_TYPE_COLOUR: | |
764 | *pData++ = uchRed; | |
dc683654 | 765 | if ( iBitDepth == 16 ) |
a4efa721 VZ |
766 | *pData++ = 0; |
767 | *pData++ = uchGreen; | |
dc683654 | 768 | if ( iBitDepth == 16 ) |
a4efa721 VZ |
769 | *pData++ = 0; |
770 | *pData++ = uchBlue; | |
dc683654 | 771 | if ( iBitDepth == 16 ) |
a4efa721 VZ |
772 | *pData++ = 0; |
773 | break; | |
774 | ||
775 | case wxPNG_TYPE_GREY: | |
776 | { | |
dc683654 VZ |
777 | // where do these coefficients come from? maybe we |
778 | // should have image options for them as well? | |
a4efa721 VZ |
779 | unsigned uiColor = |
780 | (unsigned) (76.544*(unsigned)uchRed + | |
781 | 150.272*(unsigned)uchGreen + | |
782 | 36.864*(unsigned)uchBlue); | |
dc683654 VZ |
783 | |
784 | *pData++ = (unsigned char)((uiColor >> 8) & 0xFF); | |
785 | if ( iBitDepth == 16 ) | |
a4efa721 | 786 | *pData++ = (unsigned char)(uiColor & 0xFF); |
a4efa721 VZ |
787 | } |
788 | break; | |
789 | ||
790 | case wxPNG_TYPE_GREY_RED: | |
791 | *pData++ = uchRed; | |
dc683654 | 792 | if ( iBitDepth == 16 ) |
a4efa721 VZ |
793 | *pData++ = 0; |
794 | break; | |
795 | } | |
796 | ||
797 | if ( bUseAlpha ) | |
798 | { | |
799 | unsigned char uchAlpha = 255; | |
800 | if ( bHasAlpha ) | |
801 | uchAlpha = *pAlpha++; | |
802 | ||
803 | if ( bHasMask ) | |
e1e36a3e | 804 | { |
a4efa721 VZ |
805 | if ( (uchRed == uchMaskRed) |
806 | && (uchGreen == uchMaskGreen) | |
807 | && (uchBlue == uchMaskBlue) ) | |
808 | uchAlpha = 0; | |
e1e36a3e | 809 | } |
a4efa721 VZ |
810 | |
811 | *pData++ = uchAlpha; | |
dc683654 | 812 | if ( iBitDepth == 16 ) |
a4efa721 | 813 | *pData++ = 0; |
e9c4b1a2 | 814 | } |
e9c4b1a2 | 815 | } |
a4efa721 | 816 | |
bdffd806 VS |
817 | png_bytep row_ptr = data; |
818 | png_write_rows( png_ptr, &row_ptr, 1 ); | |
e9c4b1a2 | 819 | } |
bdffd806 VS |
820 | |
821 | free(data); | |
822 | png_write_end( png_ptr, info_ptr ); | |
823 | png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr ); | |
824 | ||
7beb59f3 | 825 | return true; |
e9c4b1a2 | 826 | } |
e9c4b1a2 | 827 | |
3ca6a5f0 BP |
828 | #ifdef __VISUALC__ |
829 | #pragma warning(default:4611) | |
830 | #endif /* VC++ */ | |
831 | ||
9ab6ee85 | 832 | #endif // wxUSE_STREAMS |
e9c4b1a2 | 833 | |
9ab6ee85 | 834 | #endif // wxUSE_LIBPNG |