@@ -231,76 +231,96 @@ static bool png_process_ihdr(struct png_ihdr *ihdr)
231231static void png_reverse_filter_copy_line_rgb (uint32_t * data ,
232232 const uint8_t * decoded , unsigned width , unsigned bpp )
233233{
234- uint32_t * data_ptr = NULL ;
234+ unsigned i ;
235235
236236 bpp /= 8 ;
237237
238- for (data_ptr = & data [ 0 ]; data_ptr < data + width ; data_ptr ++ )
238+ for (i = 0 ; i < width ; i ++ )
239239 {
240- uint32_t r = * (decoded );
241- uint32_t g = * (decoded + bpp );
242- uint32_t b = * (decoded + bpp + bpp );
243- decoded += (3 * bpp );
244- * data_ptr = (0xffu << 24 ) | (r << 16 ) | (g << 8 ) | (b << 0 );
240+ uint32_t r , g , b ;
241+
242+ r = * decoded ;
243+ decoded += bpp ;
244+ g = * decoded ;
245+ decoded += bpp ;
246+ b = * decoded ;
247+ decoded += bpp ;
248+ data [i ] = (0xffu << 24 ) | (r << 16 ) | (g << 8 ) | (b << 0 );
245249 }
246250}
247251
248252static void png_reverse_filter_copy_line_rgba (uint32_t * data ,
249253 const uint8_t * decoded , unsigned width , unsigned bpp )
250254{
251- uint32_t * data_ptr = NULL ;
255+ unsigned i ;
252256
253257 bpp /= 8 ;
254258
255- for (data_ptr = & data [ 0 ]; data_ptr < data + width ; data_ptr ++ )
259+ for (i = 0 ; i < width ; i ++ )
256260 {
257- uint32_t r = * (decoded );
258- uint32_t g = * (decoded + bpp );
259- uint32_t b = * (decoded + bpp + bpp );
260- uint32_t a = * (decoded + bpp + bpp + bpp );
261- decoded += (4 * bpp );
262- * data_ptr = (a << 24 ) | (r << 16 ) | (g << 8 ) | (b << 0 );
261+ uint32_t r , g , b , a ;
262+ r = * decoded ;
263+ decoded += bpp ;
264+ g = * decoded ;
265+ decoded += bpp ;
266+ b = * decoded ;
267+ decoded += bpp ;
268+ a = * decoded ;
269+ decoded += bpp ;
270+ data [i ] = (a << 24 ) | (r << 16 ) | (g << 8 ) | (b << 0 );
263271 }
264272}
265273
266274static void png_reverse_filter_copy_line_bw (uint32_t * data ,
267275 const uint8_t * decoded , unsigned width , unsigned depth )
268276{
269- unsigned i ;
270- static const unsigned
271- mul_table [] = { 0 , 0xff , 0x55 , 0 , 0x11 , 0 , 0 , 0 , 0x01 };
272- unsigned mul = mul_table [depth ];
273- unsigned mask = (1 << depth ) - 1 ;
274- unsigned bit = 0 ;
275- uint32_t * data_ptr = NULL ;
276-
277- for ( i = 0 , data_ptr = & data [0 ]
278- ; i < width
279- ; i ++ , data_ptr ++ , bit += depth )
277+ unsigned i , bit ;
278+ static const unsigned mul_table [] = { 0 , 0xff , 0x55 , 0 , 0x11 , 0 , 0 , 0 , 0x01 };
279+ unsigned mul , mask ;
280+
281+ if (depth == 16 )
282+ {
283+ for (i = 0 ; i < width ; i ++ )
284+ {
285+ uint32_t val = decoded [i << 1 ];
286+ data [i ] = (val * 0x010101 ) | (0xffu << 24 );
287+ }
288+ return ;
289+ }
290+
291+ mul = mul_table [depth ];
292+ mask = (1 << depth ) - 1 ;
293+ bit = 0 ;
294+
295+ for (i = 0 ; i < width ; i ++ , bit += depth )
280296 {
281297 unsigned byte = bit >> 3 ;
282298 unsigned val = decoded [byte ] >> (8 - depth - (bit & 7 ));
283299
284300 val &= mask ;
285301 val *= mul ;
286- * data_ptr = (val * 0x010101 ) | (0xffu << 24 );
302+ data [ i ] = (val * 0x010101 ) | (0xffu << 24 );
287303 }
288304}
289305
290306static void png_reverse_filter_copy_line_gray_alpha (uint32_t * data ,
291307 const uint8_t * decoded , unsigned width ,
292308 unsigned bpp )
293309{
294- uint32_t * data_ptr = NULL ;
310+ unsigned i ;
295311
296312 bpp /= 8 ;
297313
298- for (data_ptr = & data [ 0 ]; data_ptr < data + width ; data_ptr ++ )
314+ for (i = 0 ; i < width ; i ++ )
299315 {
300- uint32_t gray = * (decoded );
301- uint32_t alpha = * (decoded + bpp );
302- decoded += (2 * bpp );
303- * data_ptr = (gray * 0x010101 ) | (alpha << 24 );
316+ uint32_t gray , alpha ;
317+
318+ gray = * decoded ;
319+ decoded += bpp ;
320+ alpha = * decoded ;
321+ decoded += bpp ;
322+
323+ data [i ] = (gray * 0x010101 ) | (alpha << 24 );
304324 }
305325}
306326
@@ -460,13 +480,10 @@ static void png_reverse_filter_adam7_deinterlace_pass(uint32_t *data,
460480 for (y = 0 ; y < pass_height ;
461481 y ++ , data += ihdr -> width * pass -> stride_y , input += pass_width )
462482 {
463- uint32_t * out = data ;
464- const uint32_t * input_ptr = NULL ;
483+ uint32_t * out = data ;
465484
466- for ( input_ptr = & input [0 ]
467- ; input_ptr < input + pass_width
468- ; input_ptr ++ , out += pass -> stride_x )
469- * out = * input_ptr ;
485+ for (x = 0 ; x < pass_width ; x ++ , out += pass -> stride_x )
486+ * out = input [x ];
470487 }
471488}
472489
@@ -609,23 +626,7 @@ static int png_reverse_filter_copy_line(uint32_t *data, const struct png_ihdr *i
609626 switch (ihdr -> color_type )
610627 {
611628 case PNG_IHDR_COLOR_GRAY :
612- if (ihdr -> depth == 16 )
613- {
614- unsigned i ;
615- const uint8_t * decoded = pngp -> decoded_scanline ;
616- unsigned width = ihdr -> width ;
617- uint32_t * data_ptr = NULL ;
618-
619- for ( i = 0 , data_ptr = & data [0 ]
620- ; i < width
621- ; data_ptr ++ , i ++ )
622- {
623- uint32_t val = decoded [i << 1 ];
624- * data_ptr = (val * 0x010101 ) | (0xffu << 24 );
625- }
626- }
627- else
628- png_reverse_filter_copy_line_bw (data , pngp -> decoded_scanline , ihdr -> width , ihdr -> depth );
629+ png_reverse_filter_copy_line_bw (data , pngp -> decoded_scanline , ihdr -> width , ihdr -> depth );
629630 break ;
630631 case PNG_IHDR_COLOR_RGB :
631632 png_reverse_filter_copy_line_rgb (data , pngp -> decoded_scanline , ihdr -> width , ihdr -> depth );
@@ -832,17 +833,14 @@ static int rpng_load_image_argb_process_inflate_init(rpng_t *rpng, uint32_t **da
832833static bool png_read_plte (uint8_t * buf ,
833834 uint32_t * buffer , unsigned entries )
834835{
835- uint8_t * buf_ptr = NULL ;
836- uint32_t * buffer_ptr = NULL ;
836+ unsigned i ;
837837
838- for ( buf_ptr = & buf [0 ], buffer_ptr = & buffer [0 ]
839- ; buffer_ptr < buffer + entries
840- ; buf_ptr += 3 , buffer_ptr ++ )
838+ for (i = 0 ; i < entries ; i ++ )
841839 {
842- uint32_t r = * ( buf_ptr ) ;
843- uint32_t g = * ( buf_ptr + 1 ) ;
844- uint32_t b = * ( buf_ptr + 2 ) ;
845- * buffer_ptr = (r << 16 ) | (g << 8 ) | (b << 0 ) | (0xffu << 24 );
840+ uint32_t r = buf [ 3 * i + 0 ] ;
841+ uint32_t g = buf [ 3 * i + 1 ] ;
842+ uint32_t b = buf [ 3 * i + 2 ] ;
843+ buffer [ i ] = (r << 16 ) | (g << 8 ) | (b << 0 ) | (0xffu << 24 );
846844 }
847845
848846 return true;
@@ -958,15 +956,16 @@ static bool read_chunk_header(uint8_t *buf, uint8_t *buf_end,
958956{
959957 unsigned i ;
960958 uint8_t dword [4 ];
961- uint8_t * dword_ptr = NULL ;
962- uint8_t * buf_ptr = NULL ;
963959
964- dword [0 ] = '\0' ;
960+ dword [0 ] = '\0' ;
965961
966- for ( dword_ptr = & dword [0 ], buf_ptr = & buf [0 ]
967- ; dword_ptr < dword + 4
968- ; dword_ptr ++ , buf_ptr ++ )
969- * dword_ptr = * buf_ptr ;
962+ /* Check whether reading the header will overflow
963+ * the data buffer */
964+ if (buf_end - buf < 8 )
965+ return false;
966+
967+ for (i = 0 ; i < 4 ; i ++ )
968+ dword [i ] = buf [i ];
970969
971970 chunk -> size = dword_be (dword );
972971
@@ -1020,10 +1019,6 @@ bool rpng_iterate_image(rpng_t *rpng)
10201019 if (buf > rpng -> buff_end )
10211020 return false;
10221021
1023- /* Check whether reading the header will overflow
1024- * the data buffer */
1025- if (rpng -> buff_end - buf < 8 )
1026- return false;
10271022 if (!read_chunk_header (buf , rpng -> buff_end , & chunk ))
10281023 return false;
10291024
0 commit comments