24 #include <core/exception.h>
25 #include <fvutils/color/yuvrgb.h>
26 #include <fvutils/writers/png.h>
35 namespace firevision {
42 PNGWriter::PNGWriter() :
Writer(
"png")
68 throw Exception(
"Color space not supported, can only write YUV422_PLANAR images");
78 throw Exception(
"PNGWriter::write(): Illegal data, width==0 || height == 0 || filename=\"\".");
83 throw Exception(
"Could not open file for writing");
86 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
91 throw Exception(
"Could not create PNG write struct");
94 png_infop info_ptr = png_create_info_struct(png_ptr);
96 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
97 throw Exception(
"Could not create PNG info struct");
100 if (setjmp(png_jmpbuf(png_ptr))) {
101 png_destroy_write_struct(&png_ptr, &info_ptr);
103 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
104 throw Exception(
"Could not create setjmp");
108 png_init_io(png_ptr, fp);
113 png_set_IHDR(png_ptr,
120 PNG_COMPRESSION_TYPE_DEFAULT,
121 PNG_FILTER_TYPE_DEFAULT);
123 png_write_info(png_ptr, info_ptr);
127 png_byte row[
width * 3];
129 unsigned char *yp, *up, *vp;
130 unsigned char y1, y2, u = 0, v = 0;
136 for (
unsigned int i = 0; i <
height; ++i) {
137 if (colorspace_ == YUV422_PLANAR) {
140 for (
unsigned int j = 0; j < (
width / 2); ++j) {
145 pixel_yuv_to_rgb(y1, u, v, &row_p[0], &row_p[1], &row_p[2]);
147 pixel_yuv_to_rgb(y2, u, v, &row_p[0], &row_p[1], &row_p[2]);
151 if ((
width % 2) == 1) {
155 pixel_yuv_to_rgb(y1, u, v, &row_p[0], &row_p[1], &row_p[2]);
157 }
else if (colorspace_ == BGR) {
163 png_write_row(png_ptr, row);
166 png_write_end(png_ptr, info_ptr);
167 png_destroy_write_struct(&png_ptr, &info_ptr);