39 check = fread(data, 1, length, fp);
48 static png_struct *png_ptr;
49 static png_info *info_ptr;
54 png_uint_32 i_width, i_height;
55 int depth, color_type;
57 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, &jbuf,
NULL,
NULL);
61 info_ptr = png_create_info_struct(png_ptr);
65 if (setjmp(png_jmpbuf(png_ptr)))
68 input = fopen(
png.file_name,
"rb");
72 png_set_read_fn(png_ptr, input, read_data);
74 png_read_info(png_ptr, info_ptr);
76 png_get_IHDR(png_ptr, info_ptr, &i_width, &i_height, &depth, &color_type,
82 if (i_width != (
unsigned long)
png.width ||
83 i_height != (
unsigned long)
png.height)
84 G_fatal_error(_(
"Input PNG file has incorrect dimensions: expected: "
85 "%dx%d got: %lux%lu"),
86 png.width,
png.height, (
unsigned long)i_width,
87 (
unsigned long)i_height);
90 if (color_type != PNG_COLOR_TYPE_RGB_ALPHA)
94 if (color_type != PNG_COLOR_TYPE_PALETTE)
98 if (!
png.true_color &&
png.has_alpha) {
102 png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans,
NULL);
104 if (num_trans != 1 || trans[0] != 0)
109 png_set_invert_alpha(png_ptr);
115 png_get_PLTE(png_ptr, info_ptr, &png_pal, &num_palette);
117 if (num_palette > 256)
120 for (i = 0; i < num_palette; i++) {
121 png.palette[i][0] = png_pal[i].red;
122 png.palette[i][1] = png_pal[i].green;
123 png.palette[i][2] = png_pal[i].blue;
127 line = G_malloc(
png.width * 4);
129 for (y = 0, p =
png.grid; y <
png.height; y++) {
132 png_read_row(png_ptr, q,
NULL);
135 for (
x = 0;
x <
png.width;
x++, p++) {
145 for (
x = 0;
x <
png.width;
x++, p++, q++)
151 png_read_end(png_ptr,
NULL);
153 png_destroy_read_struct(&png_ptr, &info_ptr,
NULL);