Skip to content

Commit 29dba05

Browse files
author
Ming
committed
fixed #163
1 parent 5b82f4d commit 29dba05

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

cocos2dx/platform/uphone/CCXUIImage_uphone.cpp

+14-6
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ bool UIImage::loadPngFromStream(unsigned char *data, int nLength)
275275
m_imageInfo.width = info_ptr->width;
276276
m_imageInfo.isPremultipliedAlpha = false;
277277
m_imageInfo.bitsPerComponent = info_ptr->bit_depth;
278-
m_imageInfo.hasAlpha = true;
278+
m_imageInfo.hasAlpha = info_ptr->color_type & PNG_COLOR_MASK_ALPHA;
279279

280280
// convert to appropriate format, we now only support RGBA8888
281281
if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
@@ -296,30 +296,38 @@ bool UIImage::loadPngFromStream(unsigned char *data, int nLength)
296296
png_set_strip_16(png_ptr);
297297
}
298298

299-
// expand paletted or RGB images with transparency to full alpha channels so the data will be
300-
// available as RGBA quatets
299+
// adds a full alpha channel if there is transparency information
300+
// in a tRNS chunk
301301
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
302302
{
303303
png_set_tRNS_to_alpha(png_ptr);
304+
m_imageInfo.hasAlpha = true;
304305
}
305306

306307
// add the alpha channel if it has not
307308
if (info_ptr->color_type == PNG_COLOR_TYPE_RGB || info_ptr->color_type == PNG_COLOR_TYPE_GRAY)
308309
{
309310
png_set_add_alpha(png_ptr, 255, PNG_FILLER_AFTER);
311+
m_imageInfo.hasAlpha = true;
310312
}
311313

312314
// allocate memory and read data
313-
m_imageInfo.data = new unsigned char[m_imageInfo.height * m_imageInfo.width * 4];
315+
int bytesPerComponent = 3;
316+
if (m_imageInfo.hasAlpha)
317+
{
318+
bytesPerComponent = 4;
319+
}
320+
321+
m_imageInfo.data = new unsigned char[m_imageInfo.height * m_imageInfo.width * bytesPerComponent];
314322
rowPointers = (png_bytep*)png_mem_alloc(sizeof(png_bytep) * m_imageInfo.height);
315323
for (unsigned int i = 0; i < m_imageInfo.height; ++i)
316324
{
317-
rowPointers[i] = (png_bytep)png_mem_alloc(m_imageInfo.width * 4);
325+
rowPointers[i] = (png_bytep)png_mem_alloc(m_imageInfo.width * bytesPerComponent);
318326
}
319327
png_read_image(png_ptr, rowPointers);
320328

321329
// copy data to image info
322-
int bytesPerRow = m_imageInfo.width * 4;
330+
int bytesPerRow = m_imageInfo.width * bytesPerComponent;
323331
for (unsigned int j = 0; j < m_imageInfo.height; ++j)
324332
{
325333
memcpy(m_imageInfo.data + j * bytesPerRow, rowPointers[j], bytesPerRow);

0 commit comments

Comments
 (0)