Claw
1.7.0
|
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 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 /* \brief For return to caller */ 00067 jmp_buf setjmp_buffer; 00068 00070 std::string error_string; 00071 00072 }; // struct error_manager 00073 00074 /*----------------------------------------------------------------------*/ 00079 class reader 00080 { 00081 // classes that need to be accessible from jpeg callbacks. 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 }; // struct source_manager 00118 00119 private: 00120 /*--------------------------------------------------------------------*/ 00124 class RGB_to_pixel32 00125 { 00126 public: 00127 rgba_pixel_8 operator()( const JSAMPLE* pixel ) const; 00128 }; // class RGB_to_pixel32 00129 00130 /*--------------------------------------------------------------------*/ 00134 class grayscale_to_pixel32 00135 { 00136 public: 00137 rgba_pixel_8 operator()( const JSAMPLE* pixel ) const; 00138 }; // class grayscale_to_pixel32 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 }; // class reader 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 }; // struct options 00187 00188 // classes that need to be accessible from jpeg callbacks. 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 }; // struct destination_manager 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 }; // class writer 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 }; // class jpeg 00256 } // namespace graphic 00257 } // namespace claw 00258 00259 #include <claw/impl/jpeg_reader.tpp> 00260 00261 #endif // __CLAW_JPEG_HPP__