Claw
1.7.3
|
00001 /* 00002 CLAW - a C++ Library Absolutely Wonderful 00003 00004 CLAW is a free library without any particular aim but being useful to 00005 anyone. 00006 00007 Copyright (C) 2005-2011 Julien Jorge 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00022 00023 contact: julien.jorge@gamned.org 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 #include <cstdio> 00038 00039 extern "C" 00040 { 00041 #include <jpeglib.h> 00042 } 00043 00044 namespace claw 00045 { 00046 namespace graphic 00047 { 00052 class jpeg : public image 00053 { 00054 public: 00055 /*--------------------------------------------------------------------*/ 00062 struct error_manager 00063 { 00065 struct jpeg_error_mgr pub; 00066 00068 jmp_buf setjmp_buffer; 00069 00071 std::string error_string; 00072 00073 }; // struct error_manager 00074 00075 /*----------------------------------------------------------------------*/ 00080 class reader 00081 { 00082 // classes that need to be accessible from jpeg callbacks. 00083 public: 00084 /*--------------------------------------------------------------------*/ 00089 struct source_manager 00090 { 00091 public: 00092 source_manager( std::istream& is ); 00093 ~source_manager(); 00094 00095 boolean fill_input_buffer(); 00096 void skip_input_data(long num_bytes); 00097 00098 public: 00100 struct jpeg_source_mgr pub; 00101 00102 private: 00104 std::istream& m_input; 00105 00107 const JOCTET* m_buffer; 00108 00110 const unsigned int m_buffer_size; 00111 00113 unsigned int m_stream_size; 00114 00116 unsigned int m_stream_position; 00117 00118 }; // struct source_manager 00119 00120 private: 00121 /*--------------------------------------------------------------------*/ 00125 class RGB_to_pixel32 00126 { 00127 public: 00128 rgba_pixel_8 operator()( const JSAMPLE* pixel ) const; 00129 }; // class RGB_to_pixel32 00130 00131 /*--------------------------------------------------------------------*/ 00135 class grayscale_to_pixel32 00136 { 00137 public: 00138 rgba_pixel_8 operator()( const JSAMPLE* pixel ) const; 00139 }; // class grayscale_to_pixel32 00140 00141 public: 00142 reader( image& img ); 00143 reader( image& img, std::istream& f ); 00144 00145 void load( std::istream& f ); 00146 00147 private: 00148 template<class Convert> 00149 void read_data( jpeg_decompress_struct& cinfo, 00150 const Convert& pixel_convert ); 00151 00152 void read_from_file( std::istream& f ); 00153 void decompress( std::istream& f, jpeg_decompress_struct& cinfo ); 00154 00155 void create_decompress_info( jpeg_decompress_struct& cinfo, 00156 source_manager& infile ) const; 00157 private: 00159 image& m_image; 00160 00161 }; // class reader 00162 00163 /*----------------------------------------------------------------------*/ 00168 class writer 00169 { 00170 public: 00174 struct options 00175 { 00176 public: 00177 options(); 00178 options( unsigned char compression_quality_, bool progressive_ ); 00179 00180 public: 00182 unsigned char quality; 00183 00185 bool progressive; 00186 00187 }; // struct options 00188 00189 // classes that need to be accessible from jpeg callbacks. 00190 00191 /*--------------------------------------------------------------------*/ 00196 struct destination_manager 00197 { 00198 public: 00199 destination_manager( std::ostream& os ); 00200 ~destination_manager(); 00201 00202 void flush(); 00203 void term(); 00204 00205 public: 00207 struct jpeg_destination_mgr pub; 00208 00209 private: 00211 std::ostream& m_output; 00212 00214 JOCTET* m_buffer; 00215 00217 const unsigned int m_buffer_size; 00218 00219 }; // struct destination_manager 00220 00221 public: 00222 writer( const image& img ); 00223 writer( const image& img, std::ostream& f, 00224 const options& opt = options() ); 00225 00226 void save( std::ostream& f, const options& opt = options() ) const; 00227 00228 private: 00229 void set_options( jpeg_compress_struct& cinfo, 00230 const options& opt ) const; 00231 void save_image( jpeg_compress_struct& cinfo ) const; 00232 00233 void copy_pixel_line( JSAMPLE* data, unsigned int y ) const; 00234 00235 void create_compress_info( jpeg_compress_struct& cinfo, 00236 destination_manager& outfile ) const; 00237 00238 private: 00240 const image& m_image; 00241 00244 static const unsigned int s_rgb_pixel_size; 00245 00246 }; // class writer 00247 00248 public: 00249 jpeg( unsigned int w, unsigned int h ); 00250 jpeg( const image& that ); 00251 jpeg( std::istream& f ); 00252 00253 void save( std::ostream& os, 00254 const writer::options& opt = writer::options() ) const; 00255 00256 }; // class jpeg 00257 } // namespace graphic 00258 } // namespace claw 00259 00260 #include <claw/impl/jpeg_reader.tpp> 00261 00262 #endif // __CLAW_JPEG_HPP__