00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #ifndef __CLAW_JPEG_HPP__
00031 #define __CLAW_JPEG_HPP__
00032
00033 #include <claw/image.hpp>
00034 #include <setjmp.h>
00035 #include <iostream>
00036 #include <string>
00037
00038 extern "C"
00039 {
00040 #include <jpeglib.h>
00041 }
00042
00043 namespace claw
00044 {
00045 namespace graphic
00046 {
00051 class jpeg : public image
00052 {
00053 public:
00054
00061 struct error_manager
00062 {
00064 struct jpeg_error_mgr pub;
00065
00066
00067 jmp_buf setjmp_buffer;
00068
00070 std::string error_string;
00071
00072 };
00073
00074
00079 class reader
00080 {
00081
00082 public:
00083
00088 struct source_manager
00089 {
00090 public:
00091 source_manager( std::istream& is );
00092 ~source_manager();
00093
00094 boolean fill_input_buffer();
00095 void skip_input_data(long num_bytes);
00096
00097 public:
00099 struct jpeg_source_mgr pub;
00100
00101 private:
00103 std::istream& m_input;
00104
00106 const JOCTET* m_buffer;
00107
00109 const unsigned int m_buffer_size;
00110
00112 unsigned int m_stream_size;
00113
00115 unsigned int m_stream_position;
00116
00117 };
00118
00119 private:
00120
00124 class RGB_to_pixel32
00125 {
00126 public:
00127 rgba_pixel_8 operator()( const JSAMPLE* pixel ) const;
00128 };
00129
00130
00134 class grayscale_to_pixel32
00135 {
00136 public:
00137 rgba_pixel_8 operator()( const JSAMPLE* pixel ) const;
00138 };
00139
00140 public:
00141 reader( image& img );
00142 reader( image& img, std::istream& f );
00143
00144 void load( std::istream& f );
00145
00146 private:
00147 template<class Convert>
00148 void read_data( jpeg_decompress_struct& cinfo,
00149 const Convert& pixel_convert );
00150
00151 void read_from_file( std::istream& f );
00152 void decompress( std::istream& f, jpeg_decompress_struct& cinfo );
00153
00154 void create_decompress_info( jpeg_decompress_struct& cinfo,
00155 source_manager& infile ) const;
00156 private:
00158 image& m_image;
00159
00160 };
00161
00162
00167 class writer
00168 {
00169 public:
00173 struct options
00174 {
00175 public:
00176 options();
00177 options( unsigned char compression_quality_, bool progressive_ );
00178
00179 public:
00181 unsigned char quality;
00182
00184 bool progressive;
00185
00186 };
00187
00188
00189
00190
00195 struct destination_manager
00196 {
00197 public:
00198 destination_manager( std::ostream& os );
00199 ~destination_manager();
00200
00201 void flush();
00202 void term();
00203
00204 public:
00206 struct jpeg_destination_mgr pub;
00207
00208 private:
00210 std::ostream& m_output;
00211
00213 JOCTET* m_buffer;
00214
00216 const unsigned int m_buffer_size;
00217
00218 };
00219
00220 public:
00221 writer( const image& img );
00222 writer( const image& img, std::ostream& f,
00223 const options& opt = options() );
00224
00225 void save( std::ostream& f, const options& opt = options() ) const;
00226
00227 private:
00228 void set_options( jpeg_compress_struct& cinfo,
00229 const options& opt ) const;
00230 void save_image( jpeg_compress_struct& cinfo ) const;
00231
00232 void copy_pixel_line( JSAMPLE* data, unsigned int y ) const;
00233
00234 void create_compress_info( jpeg_compress_struct& cinfo,
00235 destination_manager& outfile ) const;
00236
00237 private:
00239 const image& m_image;
00240
00243 static const unsigned int s_rgb_pixel_size;
00244
00245 };
00246
00247 public:
00248 jpeg( unsigned int w, unsigned int h );
00249 jpeg( const image& that );
00250 jpeg( std::istream& f );
00251
00252 void save( std::ostream& os,
00253 const writer::options& opt = writer::options() ) const;
00254
00255 };
00256 }
00257 }
00258
00259 #include <claw/impl/jpeg_reader.tpp>
00260
00261 #endif // __CLAW_JPEG_HPP__