claw  1.9.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@stuff-o-matic.com
24 */
30 #ifndef __CLAW_JPEG_HPP__
31 #define __CLAW_JPEG_HPP__
32 
33 #include <claw/graphic/image.hpp>
34 
35 #include <cstdio>
36 #include <iostream>
37 #include <setjmp.h>
38 #include <string>
39 
40 extern "C"
41 {
42 #include <jpeglib.h>
43 }
44 
45 namespace claw
46 {
47  namespace graphic
48  {
53  class jpeg : public image
54  {
55  public:
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 
79  class reader
80  {
81  // classes that need to be accessible from jpeg callbacks.
82  public:
88  {
89  public:
90  source_manager(std::istream& is);
92 
93  boolean fill_input_buffer();
94  void skip_input_data(long num_bytes);
95 
96  public:
98  struct jpeg_source_mgr pub;
99 
100  private:
102  std::istream& m_input;
103 
105  const JOCTET* m_buffer;
106 
108  const unsigned int m_buffer_size;
109 
111  unsigned int m_stream_size;
112 
114  unsigned int m_stream_position;
115 
116  }; // struct source_manager
117 
118  private:
122  class RGB_to_pixel32
123  {
124  public:
125  rgba_pixel_8 operator()(const JSAMPLE* pixel) const;
126  }; // class RGB_to_pixel32
127 
131  class grayscale_to_pixel32
132  {
133  public:
134  rgba_pixel_8 operator()(const JSAMPLE* pixel) const;
135  }; // class grayscale_to_pixel32
136 
137  public:
138  reader(image& img);
139  reader(image& img, std::istream& f);
140 
141  void load(std::istream& f);
142 
143  private:
144  template <class Convert>
145  void read_data(jpeg_decompress_struct& cinfo,
146  const Convert& pixel_convert);
147 
148  void read_from_file(std::istream& f);
149  void decompress(std::istream& f, jpeg_decompress_struct& cinfo);
150 
151  void create_decompress_info(jpeg_decompress_struct& cinfo,
152  source_manager& infile) const;
153 
154  private:
156  image& m_image;
157 
158  }; // class reader
159 
164  class writer
165  {
166  public:
170  struct options
171  {
172  public:
173  options();
174  options(unsigned char compression_quality_, bool progressive_);
175 
176  public:
178  unsigned char quality;
179 
182 
183  }; // struct options
184 
185  // classes that need to be accessible from jpeg callbacks.
186 
192  {
193  public:
194  destination_manager(std::ostream& os);
196 
197  void flush();
198  void term();
199 
200  public:
202  struct jpeg_destination_mgr pub;
203 
204  private:
206  std::ostream& m_output;
207 
209  JOCTET* m_buffer;
210 
212  const unsigned int m_buffer_size;
213 
214  }; // struct destination_manager
215 
216  public:
217  writer(const image& img);
218  writer(const image& img, std::ostream& f,
219  const options& opt = options());
220 
221  void save(std::ostream& f, const options& opt = options()) const;
222 
223  private:
224  void set_options(jpeg_compress_struct& cinfo,
225  const options& opt) const;
226  void save_image(jpeg_compress_struct& cinfo) const;
227 
228  void copy_pixel_line(JSAMPLE* data, unsigned int y) const;
229 
230  void create_compress_info(jpeg_compress_struct& cinfo,
231  destination_manager& outfile) const;
232 
233  private:
235  const image& m_image;
236 
239  static const unsigned int s_rgb_pixel_size;
240 
241  }; // class writer
242 
243  public:
244  jpeg(unsigned int w, unsigned int h);
245  jpeg(const image& that);
246  jpeg(std::istream& f);
247 
248  void save(std::ostream& os,
249  const writer::options& opt = writer::options()) const;
250 
251  }; // class jpeg
252  }
253 }
254 
255 #include <claw/graphic/jpeg_reader.tpp>
256 
257 #endif // __CLAW_JPEG_HPP__
unsigned char quality
Quality level to use in the saved stream.
Definition: jpeg.hpp:178
void term()
Write the last pending bytes in the file.
void load(std::istream &f)
Load an image from a jpeg file.
boolean fill_input_buffer()
Fill the input buffer with new data.
source_manager(std::istream &is)
Constructor.
Definition: jpeg_reader.cpp:93
jpeg(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
Definition: jpeg.cpp:38
struct jpeg_error_mgr pub
"public" fields, needed by the jpeg library.
Definition: jpeg.hpp:65
void flush()
Write the content of the buffer in the file.
std::string error_string
A comprehensive description of the error.
Definition: jpeg.hpp:71
struct jpeg_destination_mgr pub
"public" fields, needed by the jpeg library.
Definition: jpeg.hpp:202
Destination manager that allow us to write in a std::ostream.
Definition: jpeg.hpp:191
writer(const image &img)
Constructor.
bool progressive
Tell if we save a progressive jpeg.
Definition: jpeg.hpp:181
jmp_buf setjmp_buffer
For return to caller.
Definition: jpeg.hpp:68
void save(std::ostream &os, const writer::options &opt=writer::options()) const
Save the image.
Definition: jpeg.cpp:64
This class write an image in a jpeg file.
Definition: jpeg.hpp:164
This class read data from a jpeg file and store it in an image.
Definition: jpeg.hpp:79
struct jpeg_source_mgr pub
"public" fields, needed by the jpeg library.
Definition: jpeg.hpp:98
destination_manager(std::ostream &os)
Constructor.
Definition: jpeg_writer.cpp:86
Error handler that throw an exception instead of exiting the program.
Definition: jpeg.hpp:62
Parameters of the writing algorithm.
Definition: jpeg.hpp:170
Source manager that allow us to read from a std::istream.
Definition: jpeg.hpp:87
A class for jpeg pictures.
Definition: jpeg.hpp:53
A class to deal with images.
Definition: image.hpp:50
void save(std::ostream &f, const options &opt=options()) const
Save an image in a jpeg file.
This is the main namespace.
Definition: application.hpp:49
reader(image &img)
Constructor.
void skip_input_data(long num_bytes)
Skip some bytes in the input buffer.
A class to deal with images.