aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/png.c36
1 files changed, 19 insertions, 17 deletions
diff --git a/src/png.c b/src/png.c
index f10ba64..47e4f0e 100644
--- a/src/png.c
+++ b/src/png.c
@@ -13,13 +13,11 @@ struct png_t{
int numpasses;
};
-static unsigned char png_head[8] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
-
static int ispng(FILE *f){
unsigned char buf[8];
if(fread(buf, 1, 8, f) != 8)
return 0;
- return !memcmp(buf, png_head, 8);
+ return png_check_sig(buf, 8);
}
struct image *png_open(FILE *f){
@@ -56,19 +54,19 @@ struct image *png_open(FILE *f){
png_read_info(p->png_ptr, p->info_ptr);
{
- png_byte color_type = png_get_color_type(p->png_ptr, p->info_ptr);
- if(color_type == PNG_COLOR_TYPE_PALETTE)
- png_set_palette_to_rgb(p->png_ptr);
- if(color_type == PNG_COLOR_TYPE_GRAY && png_get_bit_depth(p->png_ptr, p->info_ptr) < 8)
- png_set_gray_1_2_4_to_8(p->png_ptr);
+ int color_type = png_get_color_type(p->png_ptr, p->info_ptr);
+ int bit_depth = png_get_bit_depth(p->png_ptr, p->info_ptr);
+ if (color_type == PNG_COLOR_TYPE_PALETTE)
+ png_set_expand(p->png_ptr);
+ if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
+ png_set_expand(p->png_ptr);
if (png_get_valid(p->png_ptr, p->info_ptr, PNG_INFO_tRNS))
- png_set_tRNS_to_alpha(p->png_ptr);
- if (png_get_bit_depth(p->png_ptr, p->info_ptr) == 16)
+ png_set_expand(p->png_ptr);
+ if (bit_depth == 16)
png_set_strip_16(p->png_ptr);
- if(color_type & PNG_COLOR_MASK_ALPHA)
- png_set_strip_alpha(p->png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(p->png_ptr);
+ png_set_strip_alpha(p->png_ptr);
if(png_get_interlace_type(p->png_ptr, p->info_ptr) == PNG_INTERLACE_ADAM7)
p->numpasses = png_set_interlace_handling(p->png_ptr);
else
@@ -84,16 +82,20 @@ struct image *png_open(FILE *f){
int png_read(struct image *img){
unsigned int y;
+ png_bytepp row_pointers;
+
struct png_t *p = (struct png_t *)img;
if(setjmp(png_jmpbuf(p->png_ptr))){
png_destroy_read_struct(&p->png_ptr, &p->info_ptr, &p->end_info);
return 1;
}
- while(p->numpasses--){
- for(y = 0; y < img->height; y++){
- png_read_row(p->png_ptr, &img->buf[y * img->width * 3], NULL);
- }
- }
+
+ row_pointers = (png_bytepp)malloc(img->height * sizeof(png_bytep));
+ for(y = 0; y < img->height; y++)
+ row_pointers[y] = img->buf + y * img->width * 3;
+
+ png_read_image(p->png_ptr, row_pointers);
+
png_destroy_read_struct(&p->png_ptr, &p->info_ptr, &p->end_info);
return 0;
}