10 #ifndef JPEGREADER_HPP_
11 #define JPEGREADER_HPP_
44 template<
class Container2d,
typename T, std::
size_t Nb_components, std::
size_t Nb_block>
60 :
base(),cinfo(),jerr(),infile(NULL),buffer(),row_stride(0),
61 block_ind(1),block_size(0),last_block_size(0),last_it(0),finished(0),initialized(0){}
69 :
base(data_filename),cinfo(),jerr(),infile(NULL),buffer(),row_stride(0),
70 block_ind(1),block_size(0),last_block_size(0),last_it(0),finished(0),initialized(0)
106 int read(Container2d & in);
109 jpeg_decompress_struct cinfo;
126 template<
class Container2d,
typename T, std::
size_t Nb_components, std::
size_t Nb_block>
130 if (Nb_components != 1 && Nb_components != 3){
131 std::ostringstream err;
132 err <<
FILE_READ_ERROR << this->data_filename_ <<
" | Nb_components should be 1 or 3.";
133 slip::slip_exception exc(std::string(
"slip"), std::string(
"JpegReader::initialize()"), err.str());
139 if ((infile = fopen(this->data_filename_.c_str(),
"rb")) == NULL) {
140 std::ostringstream err;
142 slip::slip_exception exc(std::string(
"slip"), std::string(
"JpegReader::initialize()"), err.str());
148 cinfo.err = jpeg_std_error(&jerr.pub);
149 jerr.pub.error_exit = my_error_exit;
152 if (setjmp(jerr.setjmp_buffer)) {
156 char buffer[JMSG_LENGTH_MAX];
158 (*cinfo.err->format_message) (reinterpret_cast<j_common_ptr>(&cinfo), buffer);
159 jpeg_destroy_decompress(&cinfo);
161 std::ostringstream err;
162 err <<
FILE_READ_ERROR << this->data_filename_ <<
" | JPEG library has signaled the error : "
164 slip::slip_exception exc(std::string(
"slip"), std::string(
"JpegReader::initialize()"), err.str());
168 jpeg_create_decompress(&cinfo);
172 slip_jpeg_stdio_src(&cinfo, infile);
176 jpeg_read_header(&cinfo, TRUE);
186 jpeg_start_decompress(&cinfo);
189 if(cinfo.num_components != Nb_components){
191 std::ostringstream err;
192 err <<
FILE_READ_ERROR << this->data_filename_ <<
" | Nb_components does not match.";
193 slip::slip_exception exc(std::string(
"slip"), std::string(
"JpegReader::initialize()"), err.str());
198 row_stride = cinfo.output_width * cinfo.output_components;
200 buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
201 block_size = cinfo.output_height / Nb_block;
202 last_block_size = cinfo.output_height % Nb_block;
203 last_block_size = (last_block_size == 0 ? block_size : block_size+last_block_size);
204 last_it = (Nb_block == 1);
205 finished = (last_it && last_block_size == 0);
210 template<
class Container2d,
typename T, std::
size_t Nb_components, std::
size_t Nb_block>
213 std::ostringstream err;
214 err <<
FILE_READ_ERROR << this->data_filename_ <<
" | The reader needs to be initialized before reading.";
222 in.resize(last_block_size,cinfo.output_width,T());
224 in.resize(block_size,cinfo.output_width,T());
226 if(cinfo.output_height - cinfo.output_scanline < in.height()){
227 std::ostringstream err;
228 err <<
FILE_READ_ERROR << this->data_filename_ <<
" | Bad image dimensions.";
233 for(
unsigned int i=0; i < in.height(); ++i){
234 jpeg_read_scanlines(&cinfo, buffer, 1);
235 T * ptr_i =
reinterpret_cast<T *
>(in[i]);
236 std::copy(buffer[0],buffer[0]+row_stride,ptr_i);
243 last_it = (block_ind == Nb_block);
247 template<
class Container2d,
typename T, std::
size_t Nb_components, std::
size_t Nb_block>
252 (void) jpeg_finish_decompress(&cinfo);
260 jpeg_destroy_decompress(&cinfo);
void initialize()
initialized the reading process.
Container2d container_type
Contains the container reader base class.
standard exception extension name have been changed to eliminate conflicts with QT ...
Some definitions specific to libjpeg.
const std::string FILE_OPEN_ERROR
ContainerReader is the base class of the readers classes. All readers are working the same way: ...
JpegReader, inherited from ContainerReader, is a reader for jpeg images.
ContainerReader< Container2d, T, Nb_block > base
void copy(_II first, _II last, _OI output_first)
Copy algorithm optimized for slip iterators.
int read(Container2d &in)
virtual read function. If Nb_block is more than one, it reads only one container, and returns 0...
Provides SLIP error messages.
JpegReader(std::string data_filename)
void release()
release the reading process.
const std::string FILE_READ_ERROR