10 #ifndef JPEGWRITER_HPP_
11 #define JPEGWRITER_HPP_
24 template <
typename T1>
26 return static_cast<T
>(x);
44 template<
class Container2d,
typename T, std::
size_t Nb_components>
60 :
base(),cinfo(),jerr(),outfile(NULL),row_pointer(NULL),row_stride(0),
61 finished(0),total_nb_row(0),initialized(false),output_image_width_(0),
62 output_image_height_(0){}
72 JpegWriter(std::string output_filename, std::size_t output_image_width, std::size_t output_image_height,
int quality = 90)
73 :
base(output_filename),cinfo(),jerr(),outfile(NULL),row_pointer(NULL),row_stride(0),
74 finished(0),total_nb_row(0),initialized(false),output_image_width_(output_image_width),
75 output_image_height_(output_image_height){
94 void initialize(std::size_t output_image_width, std::size_t output_image_height,
int quality = 90);
115 jpeg_compress_struct cinfo;
118 JSAMPROW* row_pointer;
121 std::size_t total_nb_row;
123 std::size_t output_image_width_;
124 std::size_t output_image_height_;
133 template<
class Container2d,
typename T, std::
size_t Nb_components>
136 output_image_width_ = output_image_width;
137 output_image_height_ = output_image_height;
142 template<
class Container2d,
typename T, std::
size_t Nb_components>
146 if (Nb_components != 1 && Nb_components != 3){
147 std::ostringstream err;
148 err <<
FILE_WRITE_ERROR << this->output_filename_ <<
" | Nb_components should be 1 or 3.";
149 slip::slip_exception exc(std::string(
"slip"), std::string(
"JpegWriter::initialize()"), err.str());
155 cinfo.err = jpeg_std_error(&jerr.pub);
156 jerr.pub.error_exit = my_error_exit;
159 if (setjmp(jerr.setjmp_buffer)) {
163 char buffer[JMSG_LENGTH_MAX];
165 (*cinfo.err->format_message) (reinterpret_cast<j_common_ptr>(&cinfo), buffer);
166 jpeg_destroy_compress(&cinfo);
169 std::ostringstream err;
170 err <<
FILE_WRITE_ERROR << this->output_filename_ <<
" | JPEG code has signaled the error : "
172 slip::slip_exception exc(std::string(
"slip"), std::string(
"JpegWriter::initialize()"), err.str());
177 jpeg_create_compress(&cinfo);
181 if ((outfile = fopen(this->output_filename_.c_str(),
"wb")) == NULL) {
182 std::ostringstream err;
184 slip::slip_exception exc(std::string(
"slip"), std::string(
"JpegWriter::initialize()"), err.str());
187 slip_jpeg_stdio_dest(&cinfo, outfile);
189 cinfo.image_width = output_image_width_;
190 cinfo.image_height = output_image_height_;
191 cinfo.input_components = Nb_components;
192 if (Nb_components == 3)
193 cinfo.in_color_space = JCS_RGB;
194 else if(Nb_components == 1)
195 cinfo.in_color_space = JCS_GRAYSCALE;
197 jpeg_set_defaults(&cinfo);
198 jpeg_set_quality(&cinfo, quality, TRUE );
201 jpeg_start_compress(&cinfo, TRUE);
204 row_stride = output_image_width_ * Nb_components;
205 row_pointer =
new JSAMPROW[1];
212 template<
class Container2d,
typename T, std::
size_t Nb_components>
216 std::ostringstream err;
217 err <<
FILE_WRITE_ERROR << this->output_filename_ <<
" | The writer to be initialized before writing.";
225 total_nb_row += data.height();
227 if(output_image_height_ < total_nb_row){
228 std::ostringstream err;
229 err <<
FILE_WRITE_ERROR << this->output_filename_ <<
" | Allocation problem during jpg writing.";
234 for(std::size_t j=0; j<data.height(); ++j){
235 row_pointer[0] =
new JSAMPLE[row_stride];
236 const T * data_ptr_j =
reinterpret_cast<const T *
>(data[j]);
237 std::transform(data_ptr_j, data_ptr_j + row_stride,
239 (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
240 delete[] row_pointer[0];
243 if (total_nb_row == output_image_height_)
249 template<
class Container2d,
typename T, std::
size_t Nb_components>
255 jpeg_finish_compress(&cinfo);
258 jpeg_destroy_compress(&cinfo);
261 delete[] row_pointer;
265 output_image_width_ = 0;
266 output_image_height_ = 0;
const std::string FILE_WRITE_ERROR
standard exception extension name have been changed to eliminate conflicts with QT ...
Contains the container writer base class.
JpegWriter(std::string output_filename, std::size_t output_image_width, std::size_t output_image_height, int quality=90)
int write(const container_type &data)
write function. This function write the data within the output file after the previous one...
Some definitions specific to libjpeg.
void initialize(std::size_t output_image_width, std::size_t output_image_height, int quality=90)
initialized the writing process. Has to be called before the first write() call of a given image...
Container2d container_type
const std::string FILE_OPEN_ERROR
Provides SLIP error messages.
T operator()(const T1 &x) const
ContainerWriter is the base class of the writer classes. All the writers are working the same way: ...
void release()
release the writing process. Has to be called when an image has been fully written.
ContainerWriter< Container2d, T > base
JpegWriter is the jpeg image writer.