Skip to content

Commit 85e2bfd

Browse files
author
Ming
committed
fixed #163
1 parent 29dba05 commit 85e2bfd

File tree

1 file changed

+12
-52
lines changed

1 file changed

+12
-52
lines changed

cocos2dx/platform/uphone/CCXUIImage_uphone.cpp

+12-52
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,9 @@ bool UIImage::loadPngFromStream(unsigned char *data, int nLength)
222222
char header[8];
223223
png_structp png_ptr;
224224
png_infop info_ptr;
225-
Int32 pos;
226225
png_bytep * rowPointers;
227226
tImageSource imageSource;
228-
229-
pos = 0;
227+
int color_type;
230228

231229
// read 8 bytes from the beginning of stream
232230
unsigned char *tmp = data;
@@ -267,64 +265,28 @@ bool UIImage::loadPngFromStream(unsigned char *data, int nLength)
267265
imageSource.offset = 0;
268266
png_set_read_fn(png_ptr, &imageSource, pngReadCallback);
269267

270-
// read png info
271-
png_read_info(png_ptr, info_ptr);
268+
// read png
269+
// PNG_TRANSFORM_EXPAND: perform set_expand()
270+
// PNG_TRANSFORM_PACKING: expand 1, 2 and 4-bit samples to bytes
271+
// PNG_TRANSFORM_STRIP_16: strip 16-bit samples to 8 bits
272+
// PNG_TRANSFORM_GRAY_TO_RGB: expand grayscale samples to RGB (or GA to RGBA)
273+
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_GRAY_TO_RGB, NULL);
274+
275+
png_get_IHDR(png_ptr, info_ptr, &m_imageInfo.width, &m_imageInfo.height, &m_imageInfo.bitsPerComponent, &color_type,
276+
NULL, NULL, NULL);
272277

273278
// init image info
274-
m_imageInfo.height = info_ptr->height;
275-
m_imageInfo.width = info_ptr->width;
276279
m_imageInfo.isPremultipliedAlpha = false;
277-
m_imageInfo.bitsPerComponent = info_ptr->bit_depth;
278280
m_imageInfo.hasAlpha = info_ptr->color_type & PNG_COLOR_MASK_ALPHA;
279281

280-
// convert to appropriate format, we now only support RGBA8888
281-
if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
282-
{
283-
png_set_packing(png_ptr);
284-
png_set_palette_to_rgb(png_ptr);
285-
}
286-
if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY && info_ptr->bit_depth < 8)
287-
{
288-
png_set_expand_gray_1_2_4_to_8(png_ptr);
289-
}
290-
if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
291-
{
292-
png_set_gray_to_rgb(png_ptr);
293-
}
294-
if (info_ptr->bit_depth == 16)
295-
{
296-
png_set_strip_16(png_ptr);
297-
}
298-
299-
// adds a full alpha channel if there is transparency information
300-
// in a tRNS chunk
301-
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
302-
{
303-
png_set_tRNS_to_alpha(png_ptr);
304-
m_imageInfo.hasAlpha = true;
305-
}
306-
307-
// add the alpha channel if it has not
308-
if (info_ptr->color_type == PNG_COLOR_TYPE_RGB || info_ptr->color_type == PNG_COLOR_TYPE_GRAY)
309-
{
310-
png_set_add_alpha(png_ptr, 255, PNG_FILLER_AFTER);
311-
m_imageInfo.hasAlpha = true;
312-
}
313-
314282
// allocate memory and read data
315283
int bytesPerComponent = 3;
316284
if (m_imageInfo.hasAlpha)
317285
{
318286
bytesPerComponent = 4;
319287
}
320-
321288
m_imageInfo.data = new unsigned char[m_imageInfo.height * m_imageInfo.width * bytesPerComponent];
322-
rowPointers = (png_bytep*)png_mem_alloc(sizeof(png_bytep) * m_imageInfo.height);
323-
for (unsigned int i = 0; i < m_imageInfo.height; ++i)
324-
{
325-
rowPointers[i] = (png_bytep)png_mem_alloc(m_imageInfo.width * bytesPerComponent);
326-
}
327-
png_read_image(png_ptr, rowPointers);
289+
rowPointers = png_get_rows(png_ptr, info_ptr);
328290

329291
// copy data to image info
330292
int bytesPerRow = m_imageInfo.width * bytesPerComponent;
@@ -355,8 +317,7 @@ bool UIImage::loadJpg(const char *strFileName)
355317
if ( !infile )
356318
{
357319
return false;
358-
}
359-
/*jpeg_stdio_src(&cinfo, infile);*/
320+
}
360321

361322
/* here we set up the standard libjpeg error handler */
362323
cinfo.err = jpeg_std_error( &jerr );
@@ -395,7 +356,6 @@ bool UIImage::loadJpg(const char *strFileName)
395356
m_imageInfo.data = new unsigned char[cinfo.output_width*cinfo.output_height*cinfo.output_components];
396357

397358
/* now actually read the jpeg into the raw buffer */
398-
/*row_pointer[0] = (unsigned char *)malloc( cinfo.output_width*cinfo.num_components );*/
399359
row_pointer[0] = new unsigned char[cinfo.output_width*cinfo.output_components];
400360

401361
/* read one scan line at a time */

0 commit comments

Comments
 (0)