24 #include <core/exception.h>
25 #include <fvutils/color/yuvrgb.h>
26 #include <fvutils/writers/jpeg.h>
39 namespace firevision {
49 JpegWriter::JpegWriter(
int quality) :
Writer(
"jpg")
53 this->quality = (quality > 0) ? quality : -quality;
66 this->quality = (quality > 0) ? quality : -quality;
77 if (
cspace == YUV422_PLANAR) {
80 throw Exception(
"Incompatible colorspace, can only hand YUV422_PLANAR images");
88 throw Exception(
"JpegWriter::read() error: buffer == NULL");
91 if ((outfile = fopen(
filename,
"wb")) == NULL) {
92 Exception e(
"Cannot open JPEG file for writing", errno);
98 struct jpeg_compress_struct cinfo;
99 struct jpeg_error_mgr jerr;
101 cinfo.err = jpeg_std_error(&jerr);
102 jpeg_create_compress(&cinfo);
103 jpeg_stdio_dest(&cinfo, outfile);
105 cinfo.image_width =
width;
106 cinfo.image_height =
height;
107 cinfo.input_components = 3;
108 cinfo.in_color_space = JCS_RGB;
110 jpeg_set_defaults(&cinfo);
111 jpeg_set_quality(&cinfo, quality,
true );
113 jpeg_start_compress(&cinfo,
true);
114 row_stride = cinfo.image_width * cinfo.input_components;
116 row_buffer = (
unsigned char *)malloc(row_stride);
118 while (cinfo.next_scanline < cinfo.image_height) {
119 convert_line_yuv422planar_to_rgb(
120 buffer, row_buffer, cinfo.image_width, cinfo.image_height, cinfo.next_scanline, 0);
121 jpeg_write_scanlines(&cinfo, &row_buffer, 1);
126 jpeg_finish_compress(&cinfo);
128 jpeg_destroy_compress(&cinfo);
Base class for exceptions in Fawkes.
void append(const char *format,...) noexcept
Append messages to the message list.
JpegWriter(int quality=80)
Constructor.
virtual ~JpegWriter()
Destructor.
virtual void write()
Write to file.
virtual void set_buffer(colorspace_t cspace, unsigned char *buffer)
Set image buffer.
Interface to write images.
unsigned int width
The width of the image.
colorspace_t cspace
The colorspace of the image.
virtual void set_filename(const char *filename)
Set filename.
unsigned char * buffer
The image-buffer.
unsigned int height
The height of the image.
char * filename
The complete filename.
Fawkes library namespace.