Claw  1.7.3
jpeg_reader.tpp
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
25 /**
26  * \file jpeg_reader.tpp
27  * \brief Implementation of the template methods of the
28  * claw::graphic::jpeg::reader class.
29  * \author Julien Jorge
30  */
31 #include <claw/jpeg_error_manager.hpp>
32 #include <claw/exception.hpp>
33 
34 /*----------------------------------------------------------------------------*/
35 /**
36  * \brief Decompress a RGB jpeg image.
37  * \param cinfo Informations about the decompression process.
38  * \param pixel_convert Operator to use to convert pixels from input.
39  */
40 template<class Convert>
41 void claw::graphic::jpeg::reader::read_data
42 ( jpeg_decompress_struct& cinfo, const Convert& pixel_convert )
43 {
44  const unsigned int pixel_size = cinfo.output_components;
45  JSAMPLE* buffer = new JSAMPLE[cinfo.output_width * pixel_size];
46 
47  error_manager jerr;
48  jpeg_error_mgr* jerr_saved = cinfo.err;
49 
50  cinfo.err = jpeg_std_error(&jerr.pub);
51  jerr.pub.error_exit = jpeg__error_manager__error_exit;
52 
53  if ( setjmp(jerr.setjmp_buffer) )
54  {
55  delete[] buffer;
56  throw CLAW_EXCEPTION(jerr.error_string);
57  }
58 
59  while (cinfo.output_scanline < cinfo.output_height)
60  {
61  jpeg_read_scanlines(&cinfo, &buffer, 1);
62 
63  scanline::iterator pixel = m_image[cinfo.output_scanline-1].begin();
64 
65  for ( unsigned int i=0; i!=pixel_size*m_image.width();
66  i+=pixel_size, ++pixel )
67  *pixel = pixel_convert( &buffer[i] );
68  }
69 
70  delete[] buffer;
71  cinfo.err = jerr_saved;
72 } // jpeg::reader::read_data()