@@ -222,11 +222,9 @@ bool UIImage::loadPngFromStream(unsigned char *data, int nLength)
222
222
char header[8 ];
223
223
png_structp png_ptr;
224
224
png_infop info_ptr;
225
- Int32 pos;
226
225
png_bytep * rowPointers;
227
226
tImageSource imageSource;
228
-
229
- pos = 0 ;
227
+ int color_type;
230
228
231
229
// read 8 bytes from the beginning of stream
232
230
unsigned char *tmp = data;
@@ -267,64 +265,28 @@ bool UIImage::loadPngFromStream(unsigned char *data, int nLength)
267
265
imageSource.offset = 0 ;
268
266
png_set_read_fn (png_ptr, &imageSource, pngReadCallback);
269
267
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 );
272
277
273
278
// init image info
274
- m_imageInfo.height = info_ptr->height ;
275
- m_imageInfo.width = info_ptr->width ;
276
279
m_imageInfo.isPremultipliedAlpha = false ;
277
- m_imageInfo.bitsPerComponent = info_ptr->bit_depth ;
278
280
m_imageInfo.hasAlpha = info_ptr->color_type & PNG_COLOR_MASK_ALPHA;
279
281
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
-
314
282
// allocate memory and read data
315
283
int bytesPerComponent = 3 ;
316
284
if (m_imageInfo.hasAlpha )
317
285
{
318
286
bytesPerComponent = 4 ;
319
287
}
320
-
321
288
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);
328
290
329
291
// copy data to image info
330
292
int bytesPerRow = m_imageInfo.width * bytesPerComponent;
@@ -355,8 +317,7 @@ bool UIImage::loadJpg(const char *strFileName)
355
317
if ( !infile )
356
318
{
357
319
return false ;
358
- }
359
- /* jpeg_stdio_src(&cinfo, infile);*/
320
+ }
360
321
361
322
/* here we set up the standard libjpeg error handler */
362
323
cinfo.err = jpeg_std_error ( &jerr );
@@ -395,7 +356,6 @@ bool UIImage::loadJpg(const char *strFileName)
395
356
m_imageInfo.data = new unsigned char [cinfo.output_width *cinfo.output_height *cinfo.output_components ];
396
357
397
358
/* now actually read the jpeg into the raw buffer */
398
- /* row_pointer[0] = (unsigned char *)malloc( cinfo.output_width*cinfo.num_components );*/
399
359
row_pointer[0 ] = new unsigned char [cinfo.output_width *cinfo.output_components ];
400
360
401
361
/* read one scan line at a time */
0 commit comments