]> git.saurik.com Git - wxWidgets.git/blob - src/common/imagpng.cpp
Tweaks for demos on MacOSX
[wxWidgets.git] / src / common / imagpng.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/imagepng.cpp
3 // Purpose: wxImage PNG handler
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ============================================================================
11 // declarations
12 // ============================================================================
13
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #ifndef WX_PRECOMP
26 #include "wx/defs.h"
27 #endif
28
29 #if wxUSE_IMAGE && wxUSE_LIBPNG
30
31 #include "wx/imagpng.h"
32 #include "wx/bitmap.h"
33 #include "wx/debug.h"
34 #include "wx/log.h"
35 #include "wx/app.h"
36 #include "png.h"
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
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
73 CheckTransparency(unsigned char **lines,
74 png_uint_32 x, png_uint_32 y, png_uint_32 w, png_uint_32 h,
75 size_t numColBytes);
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);
84
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
99 // ============================================================================
100 // wxPNGHandler implementation
101 // ============================================================================
102
103 IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler,wxImageHandler)
104
105 #if wxUSE_STREAMS
106
107 #ifndef PNGLINKAGEMODE
108 #if defined(__WATCOMC__) && ( defined(__WXMSW__) || defined(__WXMGL__) )
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
119 #endif
120
121
122 // VS: wxPNGInfoStruct declared below is a hack that needs some explanation.
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
126 // as DLL with another compiler than the main executable, it may not work
127 // (this is for example the case with wxMGL port and SciTech MGL library
128 // that provides custom runtime-loadable libpng implementation with jmpbuf
129 // disabled altogether). Luckily, it is still possible to use setjmp() &
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
134 // only for I/O routines and is set with png_set_read_fn or
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;
142
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
152 // ----------------------------------------------------------------------------
153 // helper functions
154 // ----------------------------------------------------------------------------
155
156 extern "C"
157 {
158
159 void PNGLINKAGEMODE wx_PNG_stream_reader( png_structp png_ptr, png_bytep data,
160 png_size_t length )
161 {
162 WX_PNG_INFO(png_ptr)->stream.in->Read(data, length);
163 }
164
165 void PNGLINKAGEMODE wx_PNG_stream_writer( png_structp png_ptr, png_bytep data,
166 png_size_t length )
167 {
168 WX_PNG_INFO(png_ptr)->stream.out->Write(data, length);
169 }
170
171 void
172 PNGLINKAGEMODE wx_png_warning(png_structp png_ptr, png_const_charp message)
173 {
174 wxPNGInfoStruct *info = png_ptr ? WX_PNG_INFO(png_ptr) : NULL;
175 if ( !info || info->verbose )
176 wxLogWarning( wxString::FromAscii(message) );
177 }
178
179 // from pngerror.c
180 // so that the libpng doesn't send anything on stderr
181 void
182 PNGLINKAGEMODE wx_png_error(png_structp WXUNUSED(png_ptr), png_const_charp message)
183 {
184 // JS: deliver it to wx_png_warning and don't perform any more actions:
185 // libpng will jump back to the calling function (LoadFile and SaveFile)
186 // and allow it to handle the error
187 wx_png_warning(NULL, message);
188 }
189
190 } // extern "C"
191
192 // ----------------------------------------------------------------------------
193 // LoadFile() helpers
194 // ----------------------------------------------------------------------------
195
196 // determine the kind of transparency we need for this image: if the only alpha
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
200 //
201 // parameters:
202 // lines raw PNG data
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)
207 Transparency
208 CheckTransparency(unsigned char **lines,
209 png_uint_32 x, png_uint_32 y, png_uint_32 w, png_uint_32 h,
210 size_t numColBytes)
211 {
212 // suppose that a mask will suffice and check all the remaining alpha
213 // values to see if it does
214 for ( ; y < h; y++ )
215 {
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));
219
220 for ( png_uint_32 x2 = x; x2 < w; x2++ )
221 {
222 // skip the grey or colour byte(s)
223 ptr += numColBytes;
224
225 unsigned char a2 = *ptr++;
226
227 if ( !IsTransparent(a2) && !IsOpaque(a2) )
228 {
229 // not fully opaque nor fully transparent, hence need alpha
230 return Transparency_Alpha;
231 }
232 }
233
234 // during the next loop iteration check all the pixels in the row
235 x = 0;
236 }
237
238 // mask will be enough
239 return Transparency_Mask;
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
250 png_uint_32 end = y * image->GetWidth() + x;
251 for ( png_uint_32 i = 0; i < end; i++ )
252 {
253 // all the previous pixels were opaque
254 *alpha++ = 0xff;
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
289 if ( !h.FindFirstUnusedColour(&rMask, &gMask, &bMask) )
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)) )
311 return false;
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
349 if ( !IsOpaque(a) && transparency == Transparency_None )
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
355 transparency = CheckTransparency
356 (
357 lines,
358 x, y,
359 width, height,
360 1
361 );
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 {
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
397 case Transparency_Alpha:
398 if ( alpha )
399 *alpha++ = a;
400 // fall through
401
402 case Transparency_None:
403 *ptrDst++ = g;
404 *ptrDst++ = g;
405 *ptrDst++ = g;
406 break;
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
425 if ( !IsOpaque(a) && transparency == Transparency_None )
426 {
427 transparency = CheckTransparency
428 (
429 lines,
430 x, y,
431 width, height,
432 3
433 );
434
435 if ( transparency == Transparency_Mask )
436 {
437 FindMaskColour(lines, width, height,
438 rMask, gMask, bMask);
439 }
440 else // transparency == Transparency_Alpha
441 {
442 alpha = InitAlpha(image, x, y);
443 }
444
445 }
446
447 switch ( transparency )
448 {
449 case Transparency_Mask:
450 if ( IsTransparent(a) )
451 {
452 *ptrDst++ = rMask;
453 *ptrDst++ = bMask;
454 *ptrDst++ = gMask;
455 break;
456 }
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 }
473
474 // fall through
475
476 case Transparency_Alpha:
477 if ( alpha )
478 *alpha++ = a;
479 // fall through
480
481 case Transparency_None:
482 *ptrDst++ = r;
483 *ptrDst++ = g;
484 *ptrDst++ = b;
485 break;
486 }
487 }
488 }
489 }
490
491 if ( transparency == Transparency_Mask )
492 {
493 image->SetMaskColour(rMask, gMask, bMask);
494 }
495 }
496
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
503 bool
504 wxPNGHandler::LoadFile(wxImage *image,
505 wxInputStream& stream,
506 bool verbose,
507 int WXUNUSED(index))
508 {
509 // VZ: as this function uses setjmp() the only fool proof error handling
510 // method is to use goto (setjmp is not really C++ dtors friendly...)
511
512 unsigned char **lines = NULL;
513 png_infop info_ptr = (png_infop) NULL;
514 wxPNGInfoStruct wxinfo;
515
516 wxinfo.verbose = verbose;
517 wxinfo.stream.in = &stream;
518
519 image->Destroy();
520
521 png_structp png_ptr = png_create_read_struct
522 (
523 PNG_LIBPNG_VER_STRING,
524 (voidp) NULL,
525 wx_png_error,
526 wx_png_warning
527 );
528 if (!png_ptr)
529 goto error;
530
531 // NB: please see the comment near wxPNGInfoStruct declaration for
532 // explanation why this line is mandatory
533 png_set_read_fn( png_ptr, &wxinfo, wx_PNG_stream_reader);
534
535 info_ptr = png_create_info_struct( png_ptr );
536 if (!info_ptr)
537 goto error;
538
539 if (setjmp(wxinfo.jmpbuf))
540 goto error;
541
542 png_uint_32 i, width, height;
543 int bit_depth, color_type, interlace_type;
544
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 );
547
548 if (color_type == PNG_COLOR_TYPE_PALETTE)
549 png_set_expand( png_ptr );
550
551 // Fix for Bug [ 439207 ] Monochrome PNG images come up black
552 if (bit_depth < 8)
553 png_set_expand( png_ptr );
554
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 );
560
561 image->Create((int)width, (int)height, (bool) false /* no need to init pixels */);
562
563 if (!image->Ok())
564 goto error;
565
566 lines = (unsigned char **)malloc( (size_t)(height * sizeof(unsigned char *)) );
567 if ( !lines )
568 goto error;
569
570 for (i = 0; i < height; i++)
571 {
572 if ((lines[i] = (unsigned char *)malloc( (size_t)(width * (sizeof(unsigned char) * 4)))) == NULL)
573 {
574 for ( unsigned int n = 0; n < i; n++ )
575 free( lines[n] );
576 goto error;
577 }
578 }
579
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 );
583
584 // loaded successfully, now init wxImage with this data
585 CopyDataFromPNG(image, lines, width, height, color_type);
586
587 for ( i = 0; i < height; i++ )
588 free( lines[i] );
589 free( lines );
590
591 return true;
592
593 error:
594 if (verbose)
595 wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory."));
596
597 if ( image->Ok() )
598 {
599 image->Destroy();
600 }
601
602 if ( lines )
603 {
604 free( lines );
605 }
606
607 if ( png_ptr )
608 {
609 if ( info_ptr )
610 {
611 png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL );
612 free(info_ptr);
613 }
614 else
615 png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL );
616 }
617 return false;
618 }
619
620 // ----------------------------------------------------------------------------
621 // writing PNGs
622 // ----------------------------------------------------------------------------
623
624 bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose )
625 {
626 wxPNGInfoStruct wxinfo;
627
628 wxinfo.verbose = verbose;
629 wxinfo.stream.out = &stream;
630
631 png_structp png_ptr = png_create_write_struct
632 (
633 PNG_LIBPNG_VER_STRING,
634 NULL,
635 wx_png_error,
636 wx_png_warning
637 );
638 if (!png_ptr)
639 {
640 if (verbose)
641 wxLogError(_("Couldn't save PNG image."));
642 return false;
643 }
644
645 png_infop info_ptr = png_create_info_struct(png_ptr);
646 if (info_ptr == NULL)
647 {
648 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
649 if (verbose)
650 wxLogError(_("Couldn't save PNG image."));
651 return false;
652 }
653
654 if (setjmp(wxinfo.jmpbuf))
655 {
656 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
657 if (verbose)
658 wxLogError(_("Couldn't save PNG image."));
659 return false;
660 }
661
662 // NB: please see the comment near wxPNGInfoStruct declaration for
663 // explanation why this line is mandatory
664 png_set_write_fn( png_ptr, &wxinfo, wx_PNG_stream_writer, NULL);
665
666 const int iColorType = image->HasOption(wxIMAGE_OPTION_PNG_FORMAT)
667 ? image->GetOptionInt(wxIMAGE_OPTION_PNG_FORMAT)
668 : wxPNG_TYPE_COLOUR;
669 const int iBitDepth = image->HasOption(wxIMAGE_OPTION_PNG_BITDEPTH)
670 ? image->GetOptionInt(wxIMAGE_OPTION_PNG_BITDEPTH)
671 : 8;
672
673 wxASSERT_MSG( iBitDepth == 8 || iBitDepth == 16,
674 _T("PNG bit depth must be 8 or 16") );
675
676 bool bHasAlpha = image->HasAlpha();
677 bool bHasMask = image->HasMask();
678 bool bUseAlpha = bHasAlpha || bHasMask;
679
680 int iPngColorType;
681 if ( iColorType==wxPNG_TYPE_COLOUR )
682 {
683 iPngColorType = bUseAlpha ? PNG_COLOR_TYPE_RGB_ALPHA
684 : PNG_COLOR_TYPE_RGB;
685 }
686 else
687 {
688 iPngColorType = bUseAlpha ? PNG_COLOR_TYPE_GRAY_ALPHA
689 : PNG_COLOR_TYPE_GRAY;
690 }
691
692 png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(),
693 iBitDepth, iPngColorType,
694 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
695 PNG_FILTER_TYPE_BASE);
696
697 int iElements;
698 png_color_8 sig_bit;
699
700 if ( iPngColorType & PNG_COLOR_MASK_COLOR )
701 {
702 sig_bit.red =
703 sig_bit.green =
704 sig_bit.blue = (png_byte)iBitDepth;
705 iElements = 3;
706 }
707 else // grey
708 {
709 sig_bit.gray = (png_byte)iBitDepth;
710 iElements = 1;
711 }
712
713 if ( iPngColorType & PNG_COLOR_MASK_ALPHA )
714 {
715 sig_bit.alpha = (png_byte)iBitDepth;
716 iElements++;
717 }
718
719 if ( iBitDepth == 16 )
720 iElements *= 2;
721
722 png_set_sBIT( png_ptr, info_ptr, &sig_bit );
723 png_write_info( png_ptr, info_ptr );
724 png_set_shift( png_ptr, &sig_bit );
725 png_set_packing( png_ptr );
726
727 unsigned char *
728 data = (unsigned char *)malloc( image->GetWidth() * iElements );
729 if ( !data )
730 {
731 png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
732 return false;
733 }
734
735 unsigned char *
736 pAlpha = (unsigned char *)(bHasAlpha ? image->GetAlpha() : NULL);
737 int iHeight = image->GetHeight();
738 int iWidth = image->GetWidth();
739
740 unsigned char uchMaskRed = 0, uchMaskGreen = 0, uchMaskBlue = 0;
741
742 if ( bHasMask )
743 {
744 uchMaskRed = image->GetMaskRed();
745 uchMaskGreen = image->GetMaskGreen();
746 uchMaskBlue = image->GetMaskBlue();
747 }
748
749 unsigned char *pColors = image->GetData();
750
751 for (int y = 0; y != iHeight; ++y)
752 {
753 unsigned char *pData = data;
754 for (int x = 0; x != iWidth; x++)
755 {
756 unsigned char uchRed = *pColors++;
757 unsigned char uchGreen = *pColors++;
758 unsigned char uchBlue = *pColors++;
759
760 switch ( iColorType )
761 {
762 default:
763 wxFAIL_MSG( _T("unknown wxPNG_TYPE_XXX") );
764 // fall through
765
766 case wxPNG_TYPE_COLOUR:
767 *pData++ = uchRed;
768 if ( iBitDepth == 16 )
769 *pData++ = 0;
770 *pData++ = uchGreen;
771 if ( iBitDepth == 16 )
772 *pData++ = 0;
773 *pData++ = uchBlue;
774 if ( iBitDepth == 16 )
775 *pData++ = 0;
776 break;
777
778 case wxPNG_TYPE_GREY:
779 {
780 // where do these coefficients come from? maybe we
781 // should have image options for them as well?
782 unsigned uiColor =
783 (unsigned) (76.544*(unsigned)uchRed +
784 150.272*(unsigned)uchGreen +
785 36.864*(unsigned)uchBlue);
786
787 *pData++ = (unsigned char)((uiColor >> 8) & 0xFF);
788 if ( iBitDepth == 16 )
789 *pData++ = (unsigned char)(uiColor & 0xFF);
790 }
791 break;
792
793 case wxPNG_TYPE_GREY_RED:
794 *pData++ = uchRed;
795 if ( iBitDepth == 16 )
796 *pData++ = 0;
797 break;
798 }
799
800 if ( bUseAlpha )
801 {
802 unsigned char uchAlpha = 255;
803 if ( bHasAlpha )
804 uchAlpha = *pAlpha++;
805
806 if ( bHasMask )
807 {
808 if ( (uchRed == uchMaskRed)
809 && (uchGreen == uchMaskGreen)
810 && (uchBlue == uchMaskBlue) )
811 uchAlpha = 0;
812 }
813
814 *pData++ = uchAlpha;
815 if ( iBitDepth == 16 )
816 *pData++ = 0;
817 }
818 }
819
820 png_bytep row_ptr = data;
821 png_write_rows( png_ptr, &row_ptr, 1 );
822 }
823
824 free(data);
825 png_write_end( png_ptr, info_ptr );
826 png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr );
827
828 return true;
829 }
830
831 #ifdef __VISUALC__
832 #pragma warning(default:4611)
833 #endif /* VC++ */
834
835 #endif // wxUSE_STREAMS
836
837 #endif // wxUSE_LIBPNG