10 #ifndef PNGWRITER_HPP_
11 #define PNGWRITER_HPP_
26 template <
typename T1>
27 T
operator()(
const T1& x)
const {
return static_cast<T
>(x); }
42 template<
class Container2d,
typename T, std::
size_t Nb_components>
59 :
base(),outfile(NULL),png_ptr(NULL),info_ptr(NULL),row_stride(0),
60 finished(0),total_nb_row(0),initialized(false),output_image_width_(0),
61 output_image_height_(0)
72 PngWriter(std::string output_filename, std::size_t output_image_width, std::size_t output_image_height)
73 :
base(output_filename),outfile(NULL),png_ptr(NULL),info_ptr(NULL),row_stride(0),finished(0),total_nb_row(0),
74 initialized(false),output_image_width_(output_image_width),output_image_height_(output_image_height){
91 void initialize(std::size_t output_image_width, std::size_t output_image_height);
120 std::size_t total_nb_row;
122 std::size_t output_image_width_;
123 std::size_t output_image_height_;
132 template<
class Container2d,
typename T, std::
size_t Nb_components>
135 std::size_t output_image_height){
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(
"PngWriter::initialize()"), err.str());
153 if ((outfile = fopen(this->output_filename_.c_str(),
"wb")) == NULL) {
154 std::ostringstream err;
156 slip::slip_exception exc(std::string(
"slip"), std::string(
"PngWriter::initialize()"), err.str());
166 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
171 std::ostringstream err;
172 err <<
FILE_WRITE_ERROR << this->output_filename_ <<
" | Error in png_ptr initialization.";
173 slip::slip_exception exc(std::string(
"slip"), std::string(
"PngWriter::initialize()"), err.str());
178 info_ptr = png_create_info_struct(png_ptr);
179 if (info_ptr == NULL)
181 png_destroy_write_struct(&png_ptr, png_infopp_NULL);
183 std::ostringstream err;
184 err <<
FILE_WRITE_ERROR << this->output_filename_ <<
" | Error in info_ptr allocation.";
185 slip::slip_exception exc(std::string(
"slip"), std::string(
"PngWriter::initialize()"), err.str());
192 if (setjmp(png_jmpbuf(png_ptr)))
195 png_destroy_write_struct(&png_ptr, &info_ptr);
197 std::ostringstream err;
198 err <<
FILE_WRITE_ERROR << this->output_filename_ <<
" | PNG code has signaled an error.";
199 slip::slip_exception exc(std::string(
"slip"), std::string(
"PngWriter::initialize()"), err.str());
204 png_init_io(png_ptr, outfile);
214 if (Nb_components == 1){
215 png_set_IHDR(png_ptr, info_ptr, output_image_width_, output_image_height_,
216 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
218 png_set_IHDR(png_ptr, info_ptr, output_image_width_, output_image_height_,
219 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
223 png_write_info(png_ptr, info_ptr);
226 row_stride = output_image_width_ * Nb_components;
233 template<
class Container2d,
typename T, std::
size_t Nb_components>
237 std::ostringstream err;
238 err <<
FILE_WRITE_ERROR << this->output_filename_ <<
" | The writer to be initialized before writing.";
246 total_nb_row += data.height();
248 if(output_image_height_ < total_nb_row){
249 std::ostringstream err;
250 err <<
FILE_WRITE_ERROR << this->output_filename_ <<
" | Allocation problem during png writing.";
256 png_bytep row_pointer[1];
258 if (static_cast<png_uint_32>(output_image_height_) > PNG_UINT_32_MAX/png_sizeof(png_bytep))
259 png_error (png_ptr,
"Image is too tall to process in memory");
261 for (std::size_t j=0; j<data.height(); ++j){
262 row_pointer[0] =
new unsigned char[row_stride];
263 const T * data_ptr_j =
reinterpret_cast<const T *
>(data[j]);
264 std::transform(data_ptr_j, data_ptr_j + row_stride,
266 png_write_rows(png_ptr, &row_pointer[0], 1);
267 delete[] row_pointer[0];
270 if (total_nb_row == output_image_height_){
271 png_write_end(png_ptr, info_ptr);
278 template<
class Container2d,
typename T, std::
size_t Nb_components>
283 png_destroy_write_struct(&png_ptr, &info_ptr);
292 output_image_width_ = 0;
293 output_image_height_ = 0;
void initialize(std::size_t output_image_width, std::size_t output_image_height)
initialized the writing process. Has to be called before the first write() call of a given image...
PngWriter(std::string output_filename, std::size_t output_image_width, std::size_t output_image_height)
const std::string FILE_WRITE_ERROR
standard exception extension name have been changed to eliminate conflicts with QT ...
Contains the container writer base class.
Container2d container_type
T operator()(const T1 &x) const
Some definitions specific to libpng.
const std::string FILE_OPEN_ERROR
void release()
release the writing process. Has to be called when an image has been fully written.
PngWriter is the png image writer.
ContainerWriter< Container2d, T > base
Provides SLIP error messages.
ContainerWriter is the base class of the writer classes. All the writers are working the same way: ...
int write(const container_type &data)
write function. This function write the data within the output file after the previous one...