From 6a216ad6a872ca3d4c3fef78bd41e33770507abf Mon Sep 17 00:00:00 2001 From: David Capello Date: Wed, 5 Jan 2011 00:16:39 -0300 Subject: [PATCH] Add support for 8-bit transparent PNG sprites (fixed issue #5). --- src/file/png_format.cpp | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/file/png_format.cpp b/src/file/png_format.cpp index 45e4e7cb0..c98ac4320 100644 --- a/src/file/png_format.cpp +++ b/src/file/png_format.cpp @@ -184,8 +184,10 @@ static bool load_PNG(FileOp *fop) return false; } - /* read palette */ + // Transparent palette entries + std::vector trans_entries(256, false); + // Read the palette if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE && png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette)) { int c; @@ -199,6 +201,20 @@ static bool load_PNG(FileOp *fop) for (; c < 256; c++) { fop_sequence_set_color(fop, c, 0, 0, 0); } + + // Read transparent entries + png_bytep trans = NULL; // Transparent palette entries + int num_trans = 0; + + png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, NULL); + + for (int i = 0; i < num_trans; ++i) { + int j = trans[i]; + if (j >= 0 && j < (int)trans_entries.size()) { + trans_entries[j] = true; + fop->seq.has_alpha = true; // Is a transparent sprite + } + } } /* Allocate the memory to hold the image using the fields of info_ptr. */ @@ -268,7 +284,15 @@ static bool load_PNG(FileOp *fop) for (x=0; xsprite->getBackgroundLayer() == NULL) { + png_bytep trans = (png_bytep)png_malloc(png_ptr, 1); + trans[0] = 0; // Entry 0 is transparent + + png_set_tRNS(png_ptr, info_ptr, trans, 1, NULL); + png_free(png_ptr, trans); + } } /* Write the file header information. */