Claw  1.7.0
jpeg.hpp
Go to the documentation of this file.
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 */
30 #ifndef __CLAW_JPEG_HPP__
31 #define __CLAW_JPEG_HPP__
32 
33 #include <claw/image.hpp>
34 #include <setjmp.h>
35 #include <iostream>
36 #include <string>
37 
38 extern "C"
39 {
40 #include <jpeglib.h>
41 }
42 
43 namespace claw
44 {
45  namespace graphic
46  {
51  class jpeg : public image
52  {
53  public:
54  /*--------------------------------------------------------------------*/
62  {
64  struct jpeg_error_mgr pub;
65 
66  /* \brief For return to caller */
67  jmp_buf setjmp_buffer;
68 
70  std::string error_string;
71 
72  }; // struct error_manager
73 
74  /*----------------------------------------------------------------------*/
79  class reader
80  {
81  // classes that need to be accessible from jpeg callbacks.
82  public:
83  /*--------------------------------------------------------------------*/
89  {
90  public:
91  source_manager( std::istream& is );
93 
94  boolean fill_input_buffer();
95  void skip_input_data(long num_bytes);
96 
97  public:
99  struct jpeg_source_mgr pub;
100 
101  private:
103  std::istream& m_input;
104 
106  const JOCTET* m_buffer;
107 
109  const unsigned int m_buffer_size;
110 
112  unsigned int m_stream_size;
113 
115  unsigned int m_stream_position;
116 
117  }; // struct source_manager
118 
119  private:
120  /*--------------------------------------------------------------------*/
124  class RGB_to_pixel32
125  {
126  public:
127  rgba_pixel_8 operator()( const JSAMPLE* pixel ) const;
128  }; // class RGB_to_pixel32
129 
130  /*--------------------------------------------------------------------*/
134  class grayscale_to_pixel32
135  {
136  public:
137  rgba_pixel_8 operator()( const JSAMPLE* pixel ) const;
138  }; // class grayscale_to_pixel32
139 
140  public:
141  reader( image& img );
142  reader( image& img, std::istream& f );
143 
144  void load( std::istream& f );
145 
146  private:
147  template<class Convert>
148  void read_data( jpeg_decompress_struct& cinfo,
149  const Convert& pixel_convert );
150 
151  void read_from_file( std::istream& f );
152  void decompress( std::istream& f, jpeg_decompress_struct& cinfo );
153 
154  void create_decompress_info( jpeg_decompress_struct& cinfo,
155  source_manager& infile ) const;
156  private:
158  image& m_image;
159 
160  }; // class reader
161 
162  /*----------------------------------------------------------------------*/
167  class writer
168  {
169  public:
173  struct options
174  {
175  public:
176  options();
177  options( unsigned char compression_quality_, bool progressive_ );
178 
179  public:
181  unsigned char quality;
182 
185 
186  }; // struct options
187 
188  // classes that need to be accessible from jpeg callbacks.
189 
190  /*--------------------------------------------------------------------*/
196  {
197  public:
198  destination_manager( std::ostream& os );
200 
201  void flush();
202  void term();
203 
204  public:
206  struct jpeg_destination_mgr pub;
207 
208  private:
210  std::ostream& m_output;
211 
213  JOCTET* m_buffer;
214 
216  const unsigned int m_buffer_size;
217 
218  }; // struct destination_manager
219 
220  public:
221  writer( const image& img );
222  writer( const image& img, std::ostream& f,
223  const options& opt = options() );
224 
225  void save( std::ostream& f, const options& opt = options() ) const;
226 
227  private:
228  void set_options( jpeg_compress_struct& cinfo,
229  const options& opt ) const;
230  void save_image( jpeg_compress_struct& cinfo ) const;
231 
232  void copy_pixel_line( JSAMPLE* data, unsigned int y ) const;
233 
234  void create_compress_info( jpeg_compress_struct& cinfo,
235  destination_manager& outfile ) const;
236 
237  private:
239  const image& m_image;
240 
243  static const unsigned int s_rgb_pixel_size;
244 
245  }; // class writer
246 
247  public:
248  jpeg( unsigned int w, unsigned int h );
249  jpeg( const image& that );
250  jpeg( std::istream& f );
251 
252  void save( std::ostream& os,
253  const writer::options& opt = writer::options() ) const;
254 
255  }; // class jpeg
256  } // namespace graphic
257 } // namespace claw
258 
259 #include <claw/impl/jpeg_reader.tpp>
260 
261 #endif // __CLAW_JPEG_HPP__