Claw  1.7.3
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 #include <cstdio>
38 
39 extern "C"
40 {
41 #include <jpeglib.h>
42 }
43 
44 namespace claw
45 {
46  namespace graphic
47  {
52  class jpeg : public image
53  {
54  public:
55  /*--------------------------------------------------------------------*/
63  {
65  struct jpeg_error_mgr pub;
66 
68  jmp_buf setjmp_buffer;
69 
71  std::string error_string;
72 
73  }; // struct error_manager
74 
75  /*----------------------------------------------------------------------*/
80  class reader
81  {
82  // classes that need to be accessible from jpeg callbacks.
83  public:
84  /*--------------------------------------------------------------------*/
90  {
91  public:
92  source_manager( std::istream& is );
94 
95  boolean fill_input_buffer();
96  void skip_input_data(long num_bytes);
97 
98  public:
100  struct jpeg_source_mgr pub;
101 
102  private:
104  std::istream& m_input;
105 
107  const JOCTET* m_buffer;
108 
110  const unsigned int m_buffer_size;
111 
113  unsigned int m_stream_size;
114 
116  unsigned int m_stream_position;
117 
118  }; // struct source_manager
119 
120  private:
121  /*--------------------------------------------------------------------*/
125  class RGB_to_pixel32
126  {
127  public:
128  rgba_pixel_8 operator()( const JSAMPLE* pixel ) const;
129  }; // class RGB_to_pixel32
130 
131  /*--------------------------------------------------------------------*/
135  class grayscale_to_pixel32
136  {
137  public:
138  rgba_pixel_8 operator()( const JSAMPLE* pixel ) const;
139  }; // class grayscale_to_pixel32
140 
141  public:
142  reader( image& img );
143  reader( image& img, std::istream& f );
144 
145  void load( std::istream& f );
146 
147  private:
148  template<class Convert>
149  void read_data( jpeg_decompress_struct& cinfo,
150  const Convert& pixel_convert );
151 
152  void read_from_file( std::istream& f );
153  void decompress( std::istream& f, jpeg_decompress_struct& cinfo );
154 
155  void create_decompress_info( jpeg_decompress_struct& cinfo,
156  source_manager& infile ) const;
157  private:
159  image& m_image;
160 
161  }; // class reader
162 
163  /*----------------------------------------------------------------------*/
168  class writer
169  {
170  public:
174  struct options
175  {
176  public:
177  options();
178  options( unsigned char compression_quality_, bool progressive_ );
179 
180  public:
182  unsigned char quality;
183 
186 
187  }; // struct options
188 
189  // classes that need to be accessible from jpeg callbacks.
190 
191  /*--------------------------------------------------------------------*/
197  {
198  public:
199  destination_manager( std::ostream& os );
201 
202  void flush();
203  void term();
204 
205  public:
207  struct jpeg_destination_mgr pub;
208 
209  private:
211  std::ostream& m_output;
212 
214  JOCTET* m_buffer;
215 
217  const unsigned int m_buffer_size;
218 
219  }; // struct destination_manager
220 
221  public:
222  writer( const image& img );
223  writer( const image& img, std::ostream& f,
224  const options& opt = options() );
225 
226  void save( std::ostream& f, const options& opt = options() ) const;
227 
228  private:
229  void set_options( jpeg_compress_struct& cinfo,
230  const options& opt ) const;
231  void save_image( jpeg_compress_struct& cinfo ) const;
232 
233  void copy_pixel_line( JSAMPLE* data, unsigned int y ) const;
234 
235  void create_compress_info( jpeg_compress_struct& cinfo,
236  destination_manager& outfile ) const;
237 
238  private:
240  const image& m_image;
241 
244  static const unsigned int s_rgb_pixel_size;
245 
246  }; // class writer
247 
248  public:
249  jpeg( unsigned int w, unsigned int h );
250  jpeg( const image& that );
251  jpeg( std::istream& f );
252 
253  void save( std::ostream& os,
254  const writer::options& opt = writer::options() ) const;
255 
256  }; // class jpeg
257  } // namespace graphic
258 } // namespace claw
259 
260 #include <claw/impl/jpeg_reader.tpp>
261 
262 #endif // __CLAW_JPEG_HPP__