18 void abort_(
const char * s, ...)
22 vfprintf(stderr, s, args);
23 fprintf(stderr,
"\n");
36 png_bytep * row_pointers;
39 void read_png_file(
char* file_name)
44 FILE *fp = fopen(file_name,
"rb");
46 abort_(
"[read_png_file] File %s could not be opened for reading", file_name);
47 fread(header, 1, 8, fp);
48 if (png_sig_cmp(header, 0, 8))
49 abort_(
"[read_png_file] File %s is not recognized as a PNG file", file_name);
53 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
56 abort_(
"[read_png_file] png_create_read_struct failed");
58 info_ptr = png_create_info_struct(png_ptr);
60 abort_(
"[read_png_file] png_create_info_struct failed");
62 if (setjmp(png_jmpbuf(png_ptr)))
63 abort_(
"[read_png_file] Error during init_io");
65 png_init_io(png_ptr, fp);
66 png_set_sig_bytes(png_ptr, 8);
68 png_read_info(png_ptr, info_ptr);
70 width = info_ptr->width;
71 height = info_ptr->height;
72 color_type = info_ptr->color_type;
73 bit_depth = info_ptr->bit_depth;
75 number_of_passes = png_set_interlace_handling(png_ptr);
76 png_read_update_info(png_ptr, info_ptr);
78 printf(
"%d %d %d %d\n",width,height,color_type,bit_depth);
80 if (setjmp(png_jmpbuf(png_ptr)))
81 abort_(
"[read_png_file] Error during read_image");
83 row_pointers = (png_bytep*) malloc(
sizeof(png_bytep) * height);
84 for (y=0; y<height; y++)
85 row_pointers[y] = (png_byte*) malloc(info_ptr->rowbytes);
87 png_read_image(png_ptr, row_pointers);
93 void write_png_file(
char* file_name,png_bytep * row_pointers,
int ImWidth,
int ImHeight)
96 FILE *fp = fopen(file_name,
"wb");
98 abort_(
"[write_png_file] File %s could not be opened for writing", file_name);
102 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
105 abort_(
"[write_png_file] png_create_write_struct failed");
107 info_ptr = png_create_info_struct(png_ptr);
109 abort_(
"[write_png_file] png_create_info_struct failed");
111 if (setjmp(png_jmpbuf(png_ptr)))
112 abort_(
"[write_png_file] Error during init_io");
114 png_init_io(png_ptr, fp);
118 if (setjmp(png_jmpbuf(png_ptr)))
119 abort_(
"[write_png_file] Error during writing header");
121 png_set_IHDR(png_ptr, info_ptr, ImWidth, ImHeight,
122 bit_depth, color_type, PNG_INTERLACE_NONE,
123 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
125 png_write_info(png_ptr, info_ptr);
129 if (setjmp(png_jmpbuf(png_ptr)))
130 abort_(
"[write_png_file] Error during writing bytes");
132 png_write_image(png_ptr, row_pointers);
136 if (setjmp(png_jmpbuf(png_ptr)))
137 abort_(
"[write_png_file] Error during end of write");
139 png_write_end(png_ptr, NULL);
142 for (y=0; y<ImHeight; y++)
143 free(row_pointers[y]);
150 void process_file(
int height,
int width)
152 if (info_ptr->color_type != PNG_COLOR_TYPE_RGBA)
153 abort_(
"[process_file] color_type of input file must be PNG_COLOR_TYPE_RGBA (is %d)", info_ptr->color_type);
155 for (y=0; y<height; y++) {
156 png_byte* row = row_pointers[y];
157 for (x=0; x<width; x++) {
158 png_byte* ptr = &(row[x*4]);
170 int main(
int argc,
char **argv)
173 abort_(
"Usage: program_name <file_in> <file_out>");
175 read_png_file(argv[1]);
176 process_file(width,height);
177 write_png_file(argv[2],row_pointers,width,height);