24 #include <core/exception.h> 25 #include <fvutils/writers/png.h> 26 #include <fvutils/color/yuvrgb.h> 45 PNGWriter::PNGWriter()
72 if( cspace != BGR && cspace != RGB && cspace != YUV422_PLANAR) {
73 throw Exception(
"Color space not supported, can only write YUV422_PLANAR images");
86 throw Exception(
"PNGWriter::write(): Illegal data, width==0 || height == 0 || filename=\"\".");
91 throw Exception(
"Could not open file for writing");
94 png_structp png_ptr = png_create_write_struct
95 (PNG_LIBPNG_VER_STRING,(png_voidp)NULL,
96 (png_error_ptr)NULL, (png_error_ptr)NULL);
98 throw Exception(
"Could not create PNG write struct");
101 png_infop info_ptr = png_create_info_struct(png_ptr);
103 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
104 throw Exception(
"Could not create PNG info struct");
107 if (setjmp(png_jmpbuf(png_ptr))) {
108 png_destroy_write_struct(&png_ptr, &info_ptr);
110 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
111 throw Exception(
"Could not create setjmp");
115 png_init_io(png_ptr, fp);
121 8 , PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
122 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
124 png_write_info(png_ptr, info_ptr);
128 png_byte row[
width*3];
130 unsigned char *yp, *up, *vp;
131 unsigned char y1, y2, u = 0, v = 0;
138 for (
unsigned int i = 0; i <
height; ++i) {
139 if( colorspace_ == YUV422_PLANAR ) {
142 for (
unsigned int j = 0; j < (
width / 2); ++j) {
147 pixel_yuv_to_rgb(y1, u, v, &row_p[0], &row_p[1], &row_p[2]);
149 pixel_yuv_to_rgb(y2, u, v, &row_p[0], &row_p[1], &row_p[2]);
153 if ( (
width % 2) == 1 ) {
157 pixel_yuv_to_rgb(y1, u, v, &row_p[0], &row_p[1], &row_p[2]);
159 }
else if (colorspace_ == BGR) {
166 png_write_row(png_ptr, row);
169 png_write_end(png_ptr, info_ptr);
170 png_destroy_write_struct(&png_ptr, &info_ptr);
colorspace_t cspace
The colorspace of the image.
Fawkes library namespace.
Interface to write images.
virtual void write()
Write to file.
Base class for exceptions in Fawkes.
virtual void set_filename(const char *filename)
Set filename.
unsigned int width
The width of the image.
unsigned char * buffer
The image-buffer.
virtual void set_buffer(colorspace_t cspace, unsigned char *buffer)
Set image buffer.
unsigned int height
The height of the image.
char * filename
The complete filename.