Skip to content

Commit 0d0b68a

Browse files
author
Ming
committed
fixed #151
1 parent a50b1b9 commit 0d0b68a

File tree

1 file changed

+52
-79
lines changed

1 file changed

+52
-79
lines changed

cocos2dx/platform/uphone/CCXUIImage_uphone.cpp

+52-79
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,6 @@ THE SOFTWARE.
3232
using namespace std;
3333
namespace cocos2d {
3434

35-
#ifdef _TRANZDA_VM_
36-
#define CCX_RGB(vr,vg,vb) \
37-
(ColorRefType)(((UInt32)(UInt8)(vb) << 16) | ((UInt32)(UInt8)(vg) << 8) | (UInt32)(UInt8)(vr) | ((UInt32)0xffL << 24))
38-
39-
#define CCX_RGB_APLHA(vr, vg, vb, va) \
40-
(ColorRefType)(((UInt32)((UInt8)(vr) * ((UInt8)(va) + 1)) >> 8) | \
41-
((UInt32)((UInt8)(vg) * ((UInt8)(va) + 1) >> 8) << 8) | \
42-
((UInt32)((UInt8)(vb) * ((UInt8)(va) + 1) >> 8) << 16) | \
43-
((UInt32)(UInt8)(va) << 24))
44-
45-
#define CCX_RGBA(vr,vg,vb,va) \
46-
(((va) == 0xff)?CCX_RGB((vr), (vg), (vb)):CCX_RGB_APLHA((vr), (vg), (vb), (va)))
47-
48-
#else
49-
#define CCX_RGB RGB
50-
#define CCX_RGBA RGBA
51-
#endif //_TRANZDA_VM_
52-
5335
typedef struct
5436
{
5537
unsigned char* data;
@@ -74,43 +56,6 @@ static void pngReadCallback(png_structp png_ptr, png_bytep data, png_size_t leng
7456
}
7557
}
7658

77-
static void copyImageData(tImageInfo &imageInfo, png_bytep* rowPointers)
78-
{
79-
// allocate memory
80-
imageInfo.data = new unsigned char[imageInfo.height * imageInfo.width * 4];
81-
if (! imageInfo.data)
82-
{
83-
return;
84-
}
85-
86-
// copy data
87-
if(imageInfo.hasAlpha) {
88-
unsigned int bytesPerRow = imageInfo.width * 4;
89-
unsigned int *tmp = (unsigned int *)imageInfo.data;
90-
for(unsigned int i = 0; i < imageInfo.height; i++)
91-
{
92-
for(unsigned int j = 0; j < bytesPerRow; j += 4)
93-
{
94-
*tmp++ = CCX_RGBA( rowPointers[i][j], rowPointers[i][j + 1],
95-
rowPointers[i][j + 2], rowPointers[i][j + 3] );
96-
}
97-
}
98-
}
99-
else
100-
{
101-
unsigned int bytesPerRow = imageInfo.width * 3;
102-
unsigned int *tmp = (unsigned int *)imageInfo.data;
103-
for(unsigned int i = 0; i < imageInfo.height; i++)
104-
{
105-
for(unsigned int j = 0; j < bytesPerRow; j += 3)
106-
{
107-
*tmp++ = CCX_RGB( rowPointers[i][j], rowPointers[i][j + 1],
108-
rowPointers[i][j + 2] );
109-
}
110-
}
111-
}
112-
}
113-
11459
UIImage::UIImage(void)
11560
{
11661
m_pBitmap = NULL;
@@ -267,12 +212,8 @@ bool UIImage::loadPngFromStream(unsigned char *data, int nLength)
267212
png_structp png_ptr;
268213
png_infop info_ptr;
269214
Int32 pos;
270-
Int32 bitDepth;
271-
png_uint_32 width;
272-
png_uint_32 height;
273215
Int32 interlaceType;
274216
png_bytep * rowPointers;
275-
Int32 colorType;
276217
tImageSource imageSource;
277218

278219
pos = 0;
@@ -316,35 +257,67 @@ bool UIImage::loadPngFromStream(unsigned char *data, int nLength)
316257
imageSource.offset = 0;
317258
png_set_read_fn(png_ptr, &imageSource, pngReadCallback);
318259

319-
320-
// read the data of the file
321-
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_GRAY_TO_RGB, 0);
322-
323-
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bitDepth, &colorType,
324-
&interlaceType, NULL, NULL);
325-
326-
if (setjmp(png_jmpbuf(png_ptr)))
327-
{
328-
png_destroy_read_struct(&png_ptr, NULL, NULL);
329-
png_destroy_info_struct(png_ptr, &info_ptr);
330-
return false;
331-
}
332-
333-
// get the image file data
334-
rowPointers = png_get_rows(png_ptr, info_ptr);
260+
// read png info
261+
png_read_info(png_ptr, info_ptr);
335262

336263
// init image info
337-
m_imageInfo.height = height;
338-
m_imageInfo.width = width;
339-
m_imageInfo.hasAlpha = info_ptr->color_type & PNG_COLOR_MASK_ALPHA;
264+
m_imageInfo.height = info_ptr->height;
265+
m_imageInfo.width = info_ptr->width;
340266
m_imageInfo.isPremultipliedAlpha = false;
341-
copyImageData(m_imageInfo, rowPointers);
342267
// we use CCX_RGA or CCX_RGB to save data
343268
// so the bitsPerComponet is 32, and it also
344269
// has the alpha data
345270
m_imageInfo.bitsPerComponent = 32;
346271
m_imageInfo.hasAlpha = true;
347272

273+
// convert to appropriate format, we now only support RGBA8888
274+
if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
275+
{
276+
png_set_packing(png_ptr);
277+
png_set_palette_to_rgb(png_ptr);
278+
}
279+
if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY && info_ptr->bit_depth < 8)
280+
{
281+
png_set_expand_gray_1_2_4_to_8(png_ptr);
282+
}
283+
if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
284+
{
285+
png_set_gray_to_rgb(png_ptr);
286+
}
287+
if (info_ptr->bit_depth == 16)
288+
{
289+
png_set_strip_16(png_ptr);
290+
}
291+
292+
// expand paletted or RGB images with transparency to full alpha channels so the data will be
293+
// available as RGBA quatets
294+
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
295+
{
296+
png_set_tRNS_to_alpha(png_ptr);
297+
}
298+
299+
// add the alpha channel if it has not
300+
if (info_ptr->color_type == PNG_COLOR_TYPE_RGB || info_ptr->color_type == PNG_COLOR_TYPE_GRAY)
301+
{
302+
png_set_add_alpha(png_ptr, 255, PNG_FILLER_AFTER);
303+
}
304+
305+
// allocate memory and read data
306+
m_imageInfo.data = new unsigned char[m_imageInfo.height * m_imageInfo.width * 4];
307+
rowPointers = (png_bytep*)png_mem_alloc(sizeof(png_bytep) * m_imageInfo.height);
308+
for (int i = 0; i < m_imageInfo.height; ++i)
309+
{
310+
rowPointers[i] = (png_bytep)png_mem_alloc(m_imageInfo.width * 4);
311+
}
312+
png_read_image(png_ptr, rowPointers);
313+
314+
// copy data to image info
315+
int bytesPerRow = m_imageInfo.width * 4;
316+
for (int j = 0; j < m_imageInfo.height; ++j)
317+
{
318+
memcpy(m_imageInfo.data + j * bytesPerRow, rowPointers[j], bytesPerRow);
319+
}
320+
348321
// release
349322
png_destroy_read_struct(&png_ptr, NULL, NULL);
350323
png_destroy_info_struct(png_ptr, &info_ptr);

0 commit comments

Comments
 (0)