| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/common/imagpng.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 | #if wxUSE_IMAGE && wxUSE_LIBPNG |
| 26 | |
| 27 | #include "wx/imagpng.h" |
| 28 | #include "wx/versioninfo.h" |
| 29 | |
| 30 | #ifndef WX_PRECOMP |
| 31 | #include "wx/log.h" |
| 32 | #include "wx/intl.h" |
| 33 | #include "wx/palette.h" |
| 34 | #include "wx/stream.h" |
| 35 | #endif |
| 36 | |
| 37 | #include "png.h" |
| 38 | |
| 39 | // For memcpy |
| 40 | #include <string.h> |
| 41 | |
| 42 | // ---------------------------------------------------------------------------- |
| 43 | // constants |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | |
| 46 | // image cannot have any transparent pixels at all, have only 100% opaque |
| 47 | // and/or 100% transparent pixels in which case a simple mask is enough to |
| 48 | // store this information in wxImage or have a real alpha channel in which case |
| 49 | // we need to have it in wxImage as well |
| 50 | enum Transparency |
| 51 | { |
| 52 | Transparency_None, |
| 53 | Transparency_Mask, |
| 54 | Transparency_Alpha |
| 55 | }; |
| 56 | |
| 57 | // ---------------------------------------------------------------------------- |
| 58 | // local functions |
| 59 | // ---------------------------------------------------------------------------- |
| 60 | |
| 61 | // return the kind of transparency needed for this image assuming that it does |
| 62 | // have transparent pixels, i.e. either Transparency_Alpha or Transparency_Mask |
| 63 | static Transparency |
| 64 | CheckTransparency(unsigned char **lines, |
| 65 | png_uint_32 x, png_uint_32 y, png_uint_32 w, png_uint_32 h, |
| 66 | size_t numColBytes); |
| 67 | |
| 68 | // init the alpha channel for the image and fill it with 1s up to (x, y) |
| 69 | static unsigned char *InitAlpha(wxImage *image, png_uint_32 x, png_uint_32 y); |
| 70 | |
| 71 | // find a free colour for the mask in the PNG data array |
| 72 | static void |
| 73 | FindMaskColour(unsigned char **lines, png_uint_32 width, png_uint_32 height, |
| 74 | unsigned char& rMask, unsigned char& gMask, unsigned char& bMask); |
| 75 | |
| 76 | // is the pixel with this value of alpha a fully opaque one? |
| 77 | static inline |
| 78 | bool IsOpaque(unsigned char a) |
| 79 | { |
| 80 | return a == 0xff; |
| 81 | } |
| 82 | |
| 83 | // is the pixel with this value of alpha a fully transparent one? |
| 84 | static inline |
| 85 | bool IsTransparent(unsigned char a) |
| 86 | { |
| 87 | return !a; |
| 88 | } |
| 89 | |
| 90 | // ============================================================================ |
| 91 | // wxPNGHandler implementation |
| 92 | // ============================================================================ |
| 93 | |
| 94 | IMPLEMENT_DYNAMIC_CLASS(wxPNGHandler,wxImageHandler) |
| 95 | |
| 96 | #if wxUSE_STREAMS |
| 97 | |
| 98 | #ifndef PNGLINKAGEMODE |
| 99 | #ifdef PNGAPI |
| 100 | #define PNGLINKAGEMODE PNGAPI |
| 101 | #elif defined(__WATCOMC__) |
| 102 | // we need an explicit cdecl for Watcom, at least according to |
| 103 | // |
| 104 | // http://sf.net/tracker/index.php?func=detail&aid=651492&group_id=9863&atid=109863 |
| 105 | // |
| 106 | // more testing is needed for this however, please remove this comment |
| 107 | // if you can confirm that my fix works with Watcom 11 |
| 108 | #define PNGLINKAGEMODE cdecl |
| 109 | #else |
| 110 | #define PNGLINKAGEMODE LINKAGEMODE |
| 111 | #endif |
| 112 | #endif |
| 113 | |
| 114 | |
| 115 | // VS: wxPNGInfoStruct declared below is a hack that needs some explanation. |
| 116 | // First, let me describe what's the problem: libpng uses jmp_buf in |
| 117 | // its png_struct structure. Unfortunately, this structure is |
| 118 | // compiler-specific and may vary in size, so if you use libpng compiled |
| 119 | // as DLL with another compiler than the main executable, it may not work |
| 120 | // (this is for example the case with wxMGL port and SciTech MGL library |
| 121 | // that provides custom runtime-loadable libpng implementation with jmpbuf |
| 122 | // disabled altogether). Luckily, it is still possible to use setjmp() & |
| 123 | // longjmp() as long as the structure is not part of png_struct. |
| 124 | // |
| 125 | // Sadly, there's no clean way to attach user-defined data to png_struct. |
| 126 | // There is only one customizable place, png_struct.io_ptr, which is meant |
| 127 | // only for I/O routines and is set with png_set_read_fn or |
| 128 | // png_set_write_fn. The hacky part is that we use io_ptr to store |
| 129 | // a pointer to wxPNGInfoStruct that holds I/O structures _and_ jmp_buf. |
| 130 | |
| 131 | struct wxPNGInfoStruct |
| 132 | { |
| 133 | jmp_buf jmpbuf; |
| 134 | bool verbose; |
| 135 | |
| 136 | union |
| 137 | { |
| 138 | wxInputStream *in; |
| 139 | wxOutputStream *out; |
| 140 | } stream; |
| 141 | }; |
| 142 | |
| 143 | #define WX_PNG_INFO(png_ptr) ((wxPNGInfoStruct*)png_get_io_ptr(png_ptr)) |
| 144 | |
| 145 | // ---------------------------------------------------------------------------- |
| 146 | // helper functions |
| 147 | // ---------------------------------------------------------------------------- |
| 148 | |
| 149 | extern "C" |
| 150 | { |
| 151 | |
| 152 | static void PNGLINKAGEMODE wx_PNG_stream_reader( png_structp png_ptr, png_bytep data, |
| 153 | png_size_t length ) |
| 154 | { |
| 155 | WX_PNG_INFO(png_ptr)->stream.in->Read(data, length); |
| 156 | } |
| 157 | |
| 158 | static void PNGLINKAGEMODE wx_PNG_stream_writer( png_structp png_ptr, png_bytep data, |
| 159 | png_size_t length ) |
| 160 | { |
| 161 | WX_PNG_INFO(png_ptr)->stream.out->Write(data, length); |
| 162 | } |
| 163 | |
| 164 | static void |
| 165 | PNGLINKAGEMODE wx_png_warning(png_structp png_ptr, png_const_charp message) |
| 166 | { |
| 167 | wxPNGInfoStruct *info = png_ptr ? WX_PNG_INFO(png_ptr) : NULL; |
| 168 | if ( !info || info->verbose ) |
| 169 | { |
| 170 | wxLogWarning( wxString::FromAscii(message) ); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // from pngerror.c |
| 175 | // so that the libpng doesn't send anything on stderr |
| 176 | static void |
| 177 | PNGLINKAGEMODE wx_png_error(png_structp png_ptr, png_const_charp message) |
| 178 | { |
| 179 | wx_png_warning(NULL, message); |
| 180 | |
| 181 | // we're not using libpng built-in jump buffer (see comment before |
| 182 | // wxPNGInfoStruct above) so we have to return ourselves, otherwise libpng |
| 183 | // would just abort |
| 184 | longjmp(WX_PNG_INFO(png_ptr)->jmpbuf, 1); |
| 185 | } |
| 186 | |
| 187 | } // extern "C" |
| 188 | |
| 189 | // ---------------------------------------------------------------------------- |
| 190 | // LoadFile() helpers |
| 191 | // ---------------------------------------------------------------------------- |
| 192 | |
| 193 | // determine the kind of transparency we need for this image: if the only alpha |
| 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 |
| 197 | // |
| 198 | // parameters: |
| 199 | // lines raw PNG data |
| 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) |
| 204 | Transparency |
| 205 | CheckTransparency(unsigned char **lines, |
| 206 | png_uint_32 x, png_uint_32 y, png_uint_32 w, png_uint_32 h, |
| 207 | size_t numColBytes) |
| 208 | { |
| 209 | // suppose that a mask will suffice and check all the remaining alpha |
| 210 | // values to see if it does |
| 211 | for ( ; y < h; y++ ) |
| 212 | { |
| 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)); |
| 216 | |
| 217 | for ( png_uint_32 x2 = x; x2 < w; x2++ ) |
| 218 | { |
| 219 | // skip the grey or colour byte(s) |
| 220 | ptr += numColBytes; |
| 221 | |
| 222 | unsigned char a2 = *ptr++; |
| 223 | |
| 224 | if ( !IsTransparent(a2) && !IsOpaque(a2) ) |
| 225 | { |
| 226 | // not fully opaque nor fully transparent, hence need alpha |
| 227 | return Transparency_Alpha; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // during the next loop iteration check all the pixels in the row |
| 232 | x = 0; |
| 233 | } |
| 234 | |
| 235 | // mask will be enough |
| 236 | return Transparency_Mask; |
| 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 |
| 247 | png_uint_32 end = y * image->GetWidth() + x; |
| 248 | for ( png_uint_32 i = 0; i < end; i++ ) |
| 249 | { |
| 250 | // all the previous pixels were opaque |
| 251 | *alpha++ = 0xff; |
| 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 | ++p; // jump over alpha |
| 278 | |
| 279 | wxImageHistogramEntry& |
| 280 | entry = h[wxImageHistogram:: MakeKey(r2, g2, b2)]; |
| 281 | |
| 282 | if ( entry.value++ == 0 ) |
| 283 | entry.index = nentries++; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | if ( !h.FindFirstUnusedColour(&rMask, &gMask, &bMask) ) |
| 288 | { |
| 289 | wxLogWarning(_("Too many colours in PNG, the image may be slightly blurred.")); |
| 290 | |
| 291 | // use a fixed mask colour and we'll fudge |
| 292 | // the real pixels with this colour (see |
| 293 | // below) |
| 294 | rMask = 0xfe; |
| 295 | gMask = 0; |
| 296 | bMask = 0xff; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // ---------------------------------------------------------------------------- |
| 301 | // reading PNGs |
| 302 | // ---------------------------------------------------------------------------- |
| 303 | |
| 304 | bool wxPNGHandler::DoCanRead( wxInputStream& stream ) |
| 305 | { |
| 306 | unsigned char hdr[4]; |
| 307 | |
| 308 | if ( !stream.Read(hdr, WXSIZEOF(hdr)) ) // it's ok to modify the stream position here |
| 309 | return false; |
| 310 | |
| 311 | return memcmp(hdr, "\211PNG", WXSIZEOF(hdr)) == 0; |
| 312 | } |
| 313 | |
| 314 | // convert data from RGB to wxImage format |
| 315 | static |
| 316 | void CopyDataFromPNG(wxImage *image, |
| 317 | unsigned char **lines, |
| 318 | png_uint_32 width, |
| 319 | png_uint_32 height, |
| 320 | int color_type) |
| 321 | { |
| 322 | Transparency transparency = Transparency_None; |
| 323 | |
| 324 | // only non NULL if transparency == Transparency_Alpha |
| 325 | unsigned char *alpha = NULL; |
| 326 | |
| 327 | // RGB of the mask colour if transparency == Transparency_Mask |
| 328 | // (but init them anyhow to avoid compiler warnings) |
| 329 | unsigned char rMask = 0, |
| 330 | gMask = 0, |
| 331 | bMask = 0; |
| 332 | |
| 333 | unsigned char *ptrDst = image->GetData(); |
| 334 | if ( !(color_type & PNG_COLOR_MASK_COLOR) ) |
| 335 | { |
| 336 | // grey image: GAGAGA... where G == grey component and A == alpha |
| 337 | for ( png_uint_32 y = 0; y < height; y++ ) |
| 338 | { |
| 339 | const unsigned char *ptrSrc = lines[y]; |
| 340 | for ( png_uint_32 x = 0; x < width; x++ ) |
| 341 | { |
| 342 | unsigned char g = *ptrSrc++; |
| 343 | unsigned char a = *ptrSrc++; |
| 344 | |
| 345 | // the first time we encounter a transparent pixel we must |
| 346 | // decide about what to do about them |
| 347 | if ( !IsOpaque(a) && transparency == Transparency_None ) |
| 348 | { |
| 349 | // we'll need at least the mask for this image and |
| 350 | // maybe even full alpha channel info: the former is |
| 351 | // only enough if we have alpha values of 0 and 0xff |
| 352 | // only, otherwisewe need the latter |
| 353 | transparency = CheckTransparency |
| 354 | ( |
| 355 | lines, |
| 356 | x, y, |
| 357 | width, height, |
| 358 | 1 |
| 359 | ); |
| 360 | |
| 361 | if ( transparency == Transparency_Mask ) |
| 362 | { |
| 363 | // let's choose this colour for the mask: this is |
| 364 | // not a problem here as all the other pixels are |
| 365 | // grey, i.e. R == G == B which is not the case for |
| 366 | // this one so no confusion is possible |
| 367 | rMask = 0xff; |
| 368 | gMask = 0; |
| 369 | bMask = 0xff; |
| 370 | } |
| 371 | else // transparency == Transparency_Alpha |
| 372 | { |
| 373 | alpha = InitAlpha(image, x, y); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | switch ( transparency ) |
| 378 | { |
| 379 | case Transparency_Mask: |
| 380 | if ( IsTransparent(a) ) |
| 381 | { |
| 382 | *ptrDst++ = rMask; |
| 383 | *ptrDst++ = gMask; |
| 384 | *ptrDst++ = bMask; |
| 385 | break; |
| 386 | } |
| 387 | // else: !transparent |
| 388 | |
| 389 | // must be opaque then as otherwise we shouldn't be |
| 390 | // using the mask at all |
| 391 | wxASSERT_MSG( IsOpaque(a), wxT("logic error") ); |
| 392 | |
| 393 | // fall through |
| 394 | |
| 395 | case Transparency_Alpha: |
| 396 | if ( alpha ) |
| 397 | *alpha++ = a; |
| 398 | // fall through |
| 399 | |
| 400 | case Transparency_None: |
| 401 | *ptrDst++ = g; |
| 402 | *ptrDst++ = g; |
| 403 | *ptrDst++ = g; |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | else // colour image: RGBRGB... |
| 410 | { |
| 411 | for ( png_uint_32 y = 0; y < height; y++ ) |
| 412 | { |
| 413 | const unsigned char *ptrSrc = lines[y]; |
| 414 | for ( png_uint_32 x = 0; x < width; x++ ) |
| 415 | { |
| 416 | unsigned char r = *ptrSrc++; |
| 417 | unsigned char g = *ptrSrc++; |
| 418 | unsigned char b = *ptrSrc++; |
| 419 | unsigned char a = *ptrSrc++; |
| 420 | |
| 421 | // the logic here is the same as for the grey case except |
| 422 | // where noted |
| 423 | if ( !IsOpaque(a) && transparency == Transparency_None ) |
| 424 | { |
| 425 | transparency = CheckTransparency |
| 426 | ( |
| 427 | lines, |
| 428 | x, y, |
| 429 | width, height, |
| 430 | 3 |
| 431 | ); |
| 432 | |
| 433 | if ( transparency == Transparency_Mask ) |
| 434 | { |
| 435 | FindMaskColour(lines, width, height, |
| 436 | rMask, gMask, bMask); |
| 437 | } |
| 438 | else // transparency == Transparency_Alpha |
| 439 | { |
| 440 | alpha = InitAlpha(image, x, y); |
| 441 | } |
| 442 | |
| 443 | } |
| 444 | |
| 445 | switch ( transparency ) |
| 446 | { |
| 447 | case Transparency_Mask: |
| 448 | if ( IsTransparent(a) ) |
| 449 | { |
| 450 | *ptrDst++ = rMask; |
| 451 | *ptrDst++ = gMask; |
| 452 | *ptrDst++ = bMask; |
| 453 | break; |
| 454 | } |
| 455 | else // !transparent |
| 456 | { |
| 457 | // must be opaque then as otherwise we shouldn't be |
| 458 | // using the mask at all |
| 459 | wxASSERT_MSG( IsOpaque(a), wxT("logic error") ); |
| 460 | |
| 461 | // if we couldn't find a unique colour for the |
| 462 | // mask, we can have real pixels with the same |
| 463 | // value as the mask and it's better to slightly |
| 464 | // change their colour than to make them |
| 465 | // transparent |
| 466 | if ( r == rMask && g == gMask && b == bMask ) |
| 467 | { |
| 468 | r++; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | // fall through |
| 473 | |
| 474 | case Transparency_Alpha: |
| 475 | if ( alpha ) |
| 476 | *alpha++ = a; |
| 477 | // fall through |
| 478 | |
| 479 | case Transparency_None: |
| 480 | *ptrDst++ = r; |
| 481 | *ptrDst++ = g; |
| 482 | *ptrDst++ = b; |
| 483 | break; |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | if ( transparency == Transparency_Mask ) |
| 490 | { |
| 491 | image->SetMaskColour(rMask, gMask, bMask); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | // temporarily disable the warning C4611 (interaction between '_setjmp' and |
| 496 | // C++ object destruction is non-portable) - I don't see any dtors here |
| 497 | #ifdef __VISUALC__ |
| 498 | #pragma warning(disable:4611) |
| 499 | #endif /* VC++ */ |
| 500 | |
| 501 | bool |
| 502 | wxPNGHandler::LoadFile(wxImage *image, |
| 503 | wxInputStream& stream, |
| 504 | bool verbose, |
| 505 | int WXUNUSED(index)) |
| 506 | { |
| 507 | // VZ: as this function uses setjmp() the only fool-proof error handling |
| 508 | // method is to use goto (setjmp is not really C++ dtors friendly...) |
| 509 | |
| 510 | unsigned char **lines = NULL; |
| 511 | png_infop info_ptr = (png_infop) NULL; |
| 512 | wxPNGInfoStruct wxinfo; |
| 513 | |
| 514 | png_uint_32 i, width, height = 0; |
| 515 | int bit_depth, color_type, interlace_type; |
| 516 | |
| 517 | wxinfo.verbose = verbose; |
| 518 | wxinfo.stream.in = &stream; |
| 519 | |
| 520 | image->Destroy(); |
| 521 | |
| 522 | png_structp png_ptr = png_create_read_struct |
| 523 | ( |
| 524 | PNG_LIBPNG_VER_STRING, |
| 525 | NULL, |
| 526 | wx_png_error, |
| 527 | wx_png_warning |
| 528 | ); |
| 529 | if (!png_ptr) |
| 530 | goto error; |
| 531 | |
| 532 | // NB: please see the comment near wxPNGInfoStruct declaration for |
| 533 | // explanation why this line is mandatory |
| 534 | png_set_read_fn( png_ptr, &wxinfo, wx_PNG_stream_reader); |
| 535 | |
| 536 | info_ptr = png_create_info_struct( png_ptr ); |
| 537 | if (!info_ptr) |
| 538 | goto error; |
| 539 | |
| 540 | if (setjmp(wxinfo.jmpbuf)) |
| 541 | goto error; |
| 542 | |
| 543 | png_read_info( png_ptr, info_ptr ); |
| 544 | png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL ); |
| 545 | |
| 546 | if (color_type == PNG_COLOR_TYPE_PALETTE) |
| 547 | png_set_expand( png_ptr ); |
| 548 | |
| 549 | // Fix for Bug [ 439207 ] Monochrome PNG images come up black |
| 550 | if (bit_depth < 8) |
| 551 | png_set_expand( png_ptr ); |
| 552 | |
| 553 | png_set_strip_16( png_ptr ); |
| 554 | png_set_packing( png_ptr ); |
| 555 | if (png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS)) |
| 556 | png_set_expand( png_ptr ); |
| 557 | png_set_filler( png_ptr, 0xff, PNG_FILLER_AFTER ); |
| 558 | |
| 559 | image->Create((int)width, (int)height, (bool) false /* no need to init pixels */); |
| 560 | |
| 561 | if (!image->IsOk()) |
| 562 | goto error; |
| 563 | |
| 564 | // initialize all line pointers to NULL to ensure that they can be safely |
| 565 | // free()d if an error occurs before all of them could be allocated |
| 566 | lines = (unsigned char **)calloc(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 | goto error; |
| 574 | } |
| 575 | |
| 576 | png_read_image( png_ptr, lines ); |
| 577 | png_read_end( png_ptr, info_ptr ); |
| 578 | |
| 579 | #if wxUSE_PALETTE |
| 580 | if (color_type == PNG_COLOR_TYPE_PALETTE) |
| 581 | { |
| 582 | png_colorp palette = NULL; |
| 583 | int numPalette = 0; |
| 584 | |
| 585 | (void) png_get_PLTE(png_ptr, info_ptr, &palette, &numPalette); |
| 586 | |
| 587 | unsigned char* r = new unsigned char[numPalette]; |
| 588 | unsigned char* g = new unsigned char[numPalette]; |
| 589 | unsigned char* b = new unsigned char[numPalette]; |
| 590 | |
| 591 | for (int j = 0; j < numPalette; j++) |
| 592 | { |
| 593 | r[j] = palette[j].red; |
| 594 | g[j] = palette[j].green; |
| 595 | b[j] = palette[j].blue; |
| 596 | } |
| 597 | |
| 598 | image->SetPalette(wxPalette(numPalette, r, g, b)); |
| 599 | delete[] r; |
| 600 | delete[] g; |
| 601 | delete[] b; |
| 602 | } |
| 603 | #endif // wxUSE_PALETTE |
| 604 | |
| 605 | |
| 606 | // set the image resolution if it's available |
| 607 | png_uint_32 resX, resY; |
| 608 | int unitType; |
| 609 | if (png_get_pHYs(png_ptr, info_ptr, &resX, &resY, &unitType) |
| 610 | == PNG_INFO_pHYs) |
| 611 | { |
| 612 | wxImageResolution res = wxIMAGE_RESOLUTION_CM; |
| 613 | |
| 614 | switch (unitType) |
| 615 | { |
| 616 | default: |
| 617 | wxLogWarning(_("Unknown PNG resolution unit %d"), unitType); |
| 618 | // fall through |
| 619 | |
| 620 | case PNG_RESOLUTION_UNKNOWN: |
| 621 | image->SetOption(wxIMAGE_OPTION_RESOLUTIONX, resX); |
| 622 | image->SetOption(wxIMAGE_OPTION_RESOLUTIONY, resY); |
| 623 | |
| 624 | res = wxIMAGE_RESOLUTION_NONE; |
| 625 | break; |
| 626 | |
| 627 | case PNG_RESOLUTION_METER: |
| 628 | /* |
| 629 | Convert meters to centimeters. |
| 630 | Use a string to not lose precision (converting to cm and then |
| 631 | to inch would result in integer rounding error). |
| 632 | If an app wants an int, GetOptionInt will convert and round |
| 633 | down for them. |
| 634 | */ |
| 635 | image->SetOption(wxIMAGE_OPTION_RESOLUTIONX, |
| 636 | wxString::FromCDouble((double) resX / 100.0, 2)); |
| 637 | image->SetOption(wxIMAGE_OPTION_RESOLUTIONY, |
| 638 | wxString::FromCDouble((double) resY / 100.0, 2)); |
| 639 | break; |
| 640 | } |
| 641 | |
| 642 | image->SetOption(wxIMAGE_OPTION_RESOLUTIONUNIT, res); |
| 643 | } |
| 644 | |
| 645 | |
| 646 | png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL ); |
| 647 | |
| 648 | // loaded successfully, now init wxImage with this data |
| 649 | CopyDataFromPNG(image, lines, width, height, color_type); |
| 650 | |
| 651 | for ( i = 0; i < height; i++ ) |
| 652 | free( lines[i] ); |
| 653 | free( lines ); |
| 654 | |
| 655 | return true; |
| 656 | |
| 657 | error: |
| 658 | if (verbose) |
| 659 | { |
| 660 | wxLogError(_("Couldn't load a PNG image - file is corrupted or not enough memory.")); |
| 661 | } |
| 662 | |
| 663 | if ( image->IsOk() ) |
| 664 | { |
| 665 | image->Destroy(); |
| 666 | } |
| 667 | |
| 668 | if ( lines ) |
| 669 | { |
| 670 | for ( unsigned int n = 0; n < height; n++ ) |
| 671 | free( lines[n] ); |
| 672 | |
| 673 | free( lines ); |
| 674 | } |
| 675 | |
| 676 | if ( png_ptr ) |
| 677 | { |
| 678 | if ( info_ptr ) |
| 679 | { |
| 680 | png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL ); |
| 681 | free(info_ptr); |
| 682 | } |
| 683 | else |
| 684 | png_destroy_read_struct( &png_ptr, (png_infopp) NULL, (png_infopp) NULL ); |
| 685 | } |
| 686 | return false; |
| 687 | } |
| 688 | |
| 689 | // ---------------------------------------------------------------------------- |
| 690 | // SaveFile() palette helpers |
| 691 | // ---------------------------------------------------------------------------- |
| 692 | |
| 693 | typedef wxLongToLongHashMap PaletteMap; |
| 694 | |
| 695 | static unsigned long PaletteMakeKey(const png_color_8& clr) |
| 696 | { |
| 697 | return (wxImageHistogram::MakeKey(clr.red, clr.green, clr.blue) << 8) | clr.alpha; |
| 698 | } |
| 699 | |
| 700 | static long PaletteFind(const PaletteMap& palette, const png_color_8& clr) |
| 701 | { |
| 702 | unsigned long value = PaletteMakeKey(clr); |
| 703 | PaletteMap::const_iterator it = palette.find(value); |
| 704 | |
| 705 | return (it != palette.end()) ? it->second : wxNOT_FOUND; |
| 706 | } |
| 707 | |
| 708 | static long PaletteAdd(PaletteMap *palette, const png_color_8& clr) |
| 709 | { |
| 710 | unsigned long value = PaletteMakeKey(clr); |
| 711 | PaletteMap::const_iterator it = palette->find(value); |
| 712 | size_t index; |
| 713 | |
| 714 | if (it == palette->end()) |
| 715 | { |
| 716 | index = palette->size(); |
| 717 | (*palette)[value] = index; |
| 718 | } |
| 719 | else |
| 720 | { |
| 721 | index = it->second; |
| 722 | } |
| 723 | |
| 724 | return index; |
| 725 | } |
| 726 | |
| 727 | // ---------------------------------------------------------------------------- |
| 728 | // writing PNGs |
| 729 | // ---------------------------------------------------------------------------- |
| 730 | |
| 731 | bool wxPNGHandler::SaveFile( wxImage *image, wxOutputStream& stream, bool verbose ) |
| 732 | { |
| 733 | wxPNGInfoStruct wxinfo; |
| 734 | |
| 735 | wxinfo.verbose = verbose; |
| 736 | wxinfo.stream.out = &stream; |
| 737 | |
| 738 | png_structp png_ptr = png_create_write_struct |
| 739 | ( |
| 740 | PNG_LIBPNG_VER_STRING, |
| 741 | NULL, |
| 742 | wx_png_error, |
| 743 | wx_png_warning |
| 744 | ); |
| 745 | if (!png_ptr) |
| 746 | { |
| 747 | if (verbose) |
| 748 | { |
| 749 | wxLogError(_("Couldn't save PNG image.")); |
| 750 | } |
| 751 | return false; |
| 752 | } |
| 753 | |
| 754 | png_infop info_ptr = png_create_info_struct(png_ptr); |
| 755 | if (info_ptr == NULL) |
| 756 | { |
| 757 | png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); |
| 758 | if (verbose) |
| 759 | { |
| 760 | wxLogError(_("Couldn't save PNG image.")); |
| 761 | } |
| 762 | return false; |
| 763 | } |
| 764 | |
| 765 | if (setjmp(wxinfo.jmpbuf)) |
| 766 | { |
| 767 | png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); |
| 768 | if (verbose) |
| 769 | { |
| 770 | wxLogError(_("Couldn't save PNG image.")); |
| 771 | } |
| 772 | return false; |
| 773 | } |
| 774 | |
| 775 | // NB: please see the comment near wxPNGInfoStruct declaration for |
| 776 | // explanation why this line is mandatory |
| 777 | png_set_write_fn( png_ptr, &wxinfo, wx_PNG_stream_writer, NULL); |
| 778 | |
| 779 | const int iHeight = image->GetHeight(); |
| 780 | const int iWidth = image->GetWidth(); |
| 781 | |
| 782 | const bool bHasPngFormatOption |
| 783 | = image->HasOption(wxIMAGE_OPTION_PNG_FORMAT); |
| 784 | |
| 785 | int iColorType = bHasPngFormatOption |
| 786 | ? image->GetOptionInt(wxIMAGE_OPTION_PNG_FORMAT) |
| 787 | : wxPNG_TYPE_COLOUR; |
| 788 | |
| 789 | bool bHasAlpha = image->HasAlpha(); |
| 790 | bool bHasMask = image->HasMask(); |
| 791 | |
| 792 | bool bUsePalette = iColorType == wxPNG_TYPE_PALETTE |
| 793 | #if wxUSE_PALETTE |
| 794 | || (!bHasPngFormatOption && image->HasPalette() ) |
| 795 | #endif |
| 796 | ; |
| 797 | |
| 798 | png_color_8 mask; |
| 799 | |
| 800 | if (bHasMask) |
| 801 | { |
| 802 | mask.red = image->GetMaskRed(); |
| 803 | mask.green = image->GetMaskGreen(); |
| 804 | mask.blue = image->GetMaskBlue(); |
| 805 | mask.alpha = 0; |
| 806 | mask.gray = 0; |
| 807 | } |
| 808 | |
| 809 | PaletteMap palette; |
| 810 | |
| 811 | if (bUsePalette) |
| 812 | { |
| 813 | png_color png_rgb [PNG_MAX_PALETTE_LENGTH]; |
| 814 | png_byte png_trans[PNG_MAX_PALETTE_LENGTH]; |
| 815 | |
| 816 | const unsigned char *pColors = image->GetData(); |
| 817 | const unsigned char* pAlpha = image->GetAlpha(); |
| 818 | |
| 819 | if (bHasMask && !pAlpha) |
| 820 | { |
| 821 | // Mask must be first |
| 822 | PaletteAdd(&palette, mask); |
| 823 | } |
| 824 | |
| 825 | for (int y = 0; y < iHeight; y++) |
| 826 | { |
| 827 | for (int x = 0; x < iWidth; x++) |
| 828 | { |
| 829 | png_color_8 rgba; |
| 830 | |
| 831 | rgba.red = *pColors++; |
| 832 | rgba.green = *pColors++; |
| 833 | rgba.blue = *pColors++; |
| 834 | rgba.gray = 0; |
| 835 | rgba.alpha = (pAlpha && !bHasMask) ? *pAlpha++ : 0; |
| 836 | |
| 837 | // save in our palette |
| 838 | long index = PaletteAdd(&palette, rgba); |
| 839 | |
| 840 | if (index < PNG_MAX_PALETTE_LENGTH) |
| 841 | { |
| 842 | // save in libpng's palette |
| 843 | png_rgb[index].red = rgba.red; |
| 844 | png_rgb[index].green = rgba.green; |
| 845 | png_rgb[index].blue = rgba.blue; |
| 846 | png_trans[index] = rgba.alpha; |
| 847 | } |
| 848 | else |
| 849 | { |
| 850 | bUsePalette = false; |
| 851 | break; |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | if (bUsePalette) |
| 857 | { |
| 858 | png_set_PLTE(png_ptr, info_ptr, png_rgb, palette.size()); |
| 859 | |
| 860 | if (bHasMask && !pAlpha) |
| 861 | { |
| 862 | wxASSERT(PaletteFind(palette, mask) == 0); |
| 863 | png_trans[0] = 0; |
| 864 | png_set_tRNS(png_ptr, info_ptr, png_trans, 1, NULL); |
| 865 | } |
| 866 | else if (pAlpha && !bHasMask) |
| 867 | { |
| 868 | png_set_tRNS(png_ptr, info_ptr, png_trans, palette.size(), NULL); |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | /* |
| 874 | If saving palettised was requested but it was decided we can't use a |
| 875 | palette then reset the colour type to RGB. |
| 876 | */ |
| 877 | if (!bUsePalette && iColorType == wxPNG_TYPE_PALETTE) |
| 878 | { |
| 879 | iColorType = wxPNG_TYPE_COLOUR; |
| 880 | } |
| 881 | |
| 882 | bool bUseAlpha = !bUsePalette && (bHasAlpha || bHasMask); |
| 883 | |
| 884 | int iPngColorType; |
| 885 | |
| 886 | if (bUsePalette) |
| 887 | { |
| 888 | iPngColorType = PNG_COLOR_TYPE_PALETTE; |
| 889 | iColorType = wxPNG_TYPE_PALETTE; |
| 890 | } |
| 891 | else if ( iColorType==wxPNG_TYPE_COLOUR ) |
| 892 | { |
| 893 | iPngColorType = bUseAlpha ? PNG_COLOR_TYPE_RGB_ALPHA |
| 894 | : PNG_COLOR_TYPE_RGB; |
| 895 | } |
| 896 | else |
| 897 | { |
| 898 | iPngColorType = bUseAlpha ? PNG_COLOR_TYPE_GRAY_ALPHA |
| 899 | : PNG_COLOR_TYPE_GRAY; |
| 900 | } |
| 901 | |
| 902 | if (image->HasOption(wxIMAGE_OPTION_PNG_FILTER)) |
| 903 | png_set_filter( png_ptr, PNG_FILTER_TYPE_BASE, image->GetOptionInt(wxIMAGE_OPTION_PNG_FILTER) ); |
| 904 | |
| 905 | if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL)) |
| 906 | png_set_compression_level( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL) ); |
| 907 | |
| 908 | if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL)) |
| 909 | png_set_compression_mem_level( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL) ); |
| 910 | |
| 911 | if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY)) |
| 912 | png_set_compression_strategy( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY) ); |
| 913 | |
| 914 | if (image->HasOption(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE)) |
| 915 | png_set_compression_buffer_size( png_ptr, image->GetOptionInt(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE) ); |
| 916 | |
| 917 | int iBitDepth = !bUsePalette && image->HasOption(wxIMAGE_OPTION_PNG_BITDEPTH) |
| 918 | ? image->GetOptionInt(wxIMAGE_OPTION_PNG_BITDEPTH) |
| 919 | : 8; |
| 920 | |
| 921 | png_set_IHDR( png_ptr, info_ptr, image->GetWidth(), image->GetHeight(), |
| 922 | iBitDepth, iPngColorType, |
| 923 | PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, |
| 924 | PNG_FILTER_TYPE_BASE); |
| 925 | |
| 926 | int iElements; |
| 927 | png_color_8 sig_bit; |
| 928 | |
| 929 | if ( iPngColorType & PNG_COLOR_MASK_COLOR ) |
| 930 | { |
| 931 | sig_bit.red = |
| 932 | sig_bit.green = |
| 933 | sig_bit.blue = (png_byte)iBitDepth; |
| 934 | iElements = 3; |
| 935 | } |
| 936 | else // grey |
| 937 | { |
| 938 | sig_bit.gray = (png_byte)iBitDepth; |
| 939 | iElements = 1; |
| 940 | } |
| 941 | |
| 942 | if ( bUseAlpha ) |
| 943 | { |
| 944 | sig_bit.alpha = (png_byte)iBitDepth; |
| 945 | iElements++; |
| 946 | } |
| 947 | |
| 948 | if ( iBitDepth == 16 ) |
| 949 | iElements *= 2; |
| 950 | |
| 951 | // save the image resolution if we have it |
| 952 | int resX, resY; |
| 953 | switch ( GetResolutionFromOptions(*image, &resX, &resY) ) |
| 954 | { |
| 955 | case wxIMAGE_RESOLUTION_INCHES: |
| 956 | { |
| 957 | const double INCHES_IN_METER = 10000.0 / 254; |
| 958 | resX = int(resX * INCHES_IN_METER); |
| 959 | resY = int(resY * INCHES_IN_METER); |
| 960 | } |
| 961 | break; |
| 962 | |
| 963 | case wxIMAGE_RESOLUTION_CM: |
| 964 | resX *= 100; |
| 965 | resY *= 100; |
| 966 | break; |
| 967 | |
| 968 | case wxIMAGE_RESOLUTION_NONE: |
| 969 | break; |
| 970 | |
| 971 | default: |
| 972 | wxFAIL_MSG( wxT("unsupported image resolution units") ); |
| 973 | } |
| 974 | |
| 975 | if ( resX && resY ) |
| 976 | png_set_pHYs( png_ptr, info_ptr, resX, resY, PNG_RESOLUTION_METER ); |
| 977 | |
| 978 | png_set_sBIT( png_ptr, info_ptr, &sig_bit ); |
| 979 | png_write_info( png_ptr, info_ptr ); |
| 980 | png_set_shift( png_ptr, &sig_bit ); |
| 981 | png_set_packing( png_ptr ); |
| 982 | |
| 983 | unsigned char * |
| 984 | data = (unsigned char *)malloc( image->GetWidth() * iElements ); |
| 985 | if ( !data ) |
| 986 | { |
| 987 | png_destroy_write_struct( &png_ptr, (png_infopp)NULL ); |
| 988 | return false; |
| 989 | } |
| 990 | |
| 991 | const unsigned char * |
| 992 | pAlpha = (const unsigned char *)(bHasAlpha ? image->GetAlpha() : NULL); |
| 993 | |
| 994 | const unsigned char *pColors = image->GetData(); |
| 995 | |
| 996 | for (int y = 0; y != iHeight; ++y) |
| 997 | { |
| 998 | unsigned char *pData = data; |
| 999 | for (int x = 0; x != iWidth; x++) |
| 1000 | { |
| 1001 | png_color_8 clr; |
| 1002 | clr.red = *pColors++; |
| 1003 | clr.green = *pColors++; |
| 1004 | clr.blue = *pColors++; |
| 1005 | clr.gray = 0; |
| 1006 | clr.alpha = (bUsePalette && pAlpha) ? *pAlpha++ : 0; // use with wxPNG_TYPE_PALETTE only |
| 1007 | |
| 1008 | switch ( iColorType ) |
| 1009 | { |
| 1010 | default: |
| 1011 | wxFAIL_MSG( wxT("unknown wxPNG_TYPE_XXX") ); |
| 1012 | // fall through |
| 1013 | |
| 1014 | case wxPNG_TYPE_COLOUR: |
| 1015 | *pData++ = clr.red; |
| 1016 | if ( iBitDepth == 16 ) |
| 1017 | *pData++ = 0; |
| 1018 | *pData++ = clr.green; |
| 1019 | if ( iBitDepth == 16 ) |
| 1020 | *pData++ = 0; |
| 1021 | *pData++ = clr.blue; |
| 1022 | if ( iBitDepth == 16 ) |
| 1023 | *pData++ = 0; |
| 1024 | break; |
| 1025 | |
| 1026 | case wxPNG_TYPE_GREY: |
| 1027 | { |
| 1028 | // where do these coefficients come from? maybe we |
| 1029 | // should have image options for them as well? |
| 1030 | unsigned uiColor = |
| 1031 | (unsigned) (76.544*(unsigned)clr.red + |
| 1032 | 150.272*(unsigned)clr.green + |
| 1033 | 36.864*(unsigned)clr.blue); |
| 1034 | |
| 1035 | *pData++ = (unsigned char)((uiColor >> 8) & 0xFF); |
| 1036 | if ( iBitDepth == 16 ) |
| 1037 | *pData++ = (unsigned char)(uiColor & 0xFF); |
| 1038 | } |
| 1039 | break; |
| 1040 | |
| 1041 | case wxPNG_TYPE_GREY_RED: |
| 1042 | *pData++ = clr.red; |
| 1043 | if ( iBitDepth == 16 ) |
| 1044 | *pData++ = 0; |
| 1045 | break; |
| 1046 | |
| 1047 | case wxPNG_TYPE_PALETTE: |
| 1048 | *pData++ = (unsigned char) PaletteFind(palette, clr); |
| 1049 | break; |
| 1050 | } |
| 1051 | |
| 1052 | if ( bUseAlpha ) |
| 1053 | { |
| 1054 | unsigned char uchAlpha = 255; |
| 1055 | if ( bHasAlpha ) |
| 1056 | uchAlpha = *pAlpha++; |
| 1057 | |
| 1058 | if ( bHasMask ) |
| 1059 | { |
| 1060 | if ( (clr.red == mask.red) |
| 1061 | && (clr.green == mask.green) |
| 1062 | && (clr.blue == mask.blue) ) |
| 1063 | uchAlpha = 0; |
| 1064 | } |
| 1065 | |
| 1066 | *pData++ = uchAlpha; |
| 1067 | if ( iBitDepth == 16 ) |
| 1068 | *pData++ = 0; |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | png_bytep row_ptr = data; |
| 1073 | png_write_rows( png_ptr, &row_ptr, 1 ); |
| 1074 | } |
| 1075 | |
| 1076 | free(data); |
| 1077 | png_write_end( png_ptr, info_ptr ); |
| 1078 | png_destroy_write_struct( &png_ptr, (png_infopp)&info_ptr ); |
| 1079 | |
| 1080 | return true; |
| 1081 | } |
| 1082 | |
| 1083 | #ifdef __VISUALC__ |
| 1084 | #pragma warning(default:4611) |
| 1085 | #endif /* VC++ */ |
| 1086 | |
| 1087 | #endif // wxUSE_STREAMS |
| 1088 | |
| 1089 | /*static*/ wxVersionInfo wxPNGHandler::GetLibraryVersionInfo() |
| 1090 | { |
| 1091 | // The version string seems to always have a leading space and a trailing |
| 1092 | // new line, get rid of them both. |
| 1093 | wxString str = png_get_header_version(NULL) + 1; |
| 1094 | str.Replace("\n", ""); |
| 1095 | |
| 1096 | return wxVersionInfo("libpng", |
| 1097 | PNG_LIBPNG_VER_MAJOR, |
| 1098 | PNG_LIBPNG_VER_MINOR, |
| 1099 | PNG_LIBPNG_VER_RELEASE, |
| 1100 | str); |
| 1101 | } |
| 1102 | |
| 1103 | #endif // wxUSE_LIBPNG |