00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef GIL_JPEG_DYNAMIC_IO_H
00014 #define GIL_JPEG_DYNAMIC_IO_H
00015
00023
00024 #include <stdio.h>
00025 #include <string>
00026 #include <boost/mpl/bool.hpp>
00027 #include <boost/shared_ptr.hpp>
00028 #include "../dynamic_image/dynamic_image_all.hpp"
00029 #include "io_error.hpp"
00030
00031 #include "jpeg_io.hpp"
00032 #include "jpeg_io_private.hpp"
00033 #include "dynamic_io.hpp"
00034
00035 namespace boost { namespace gil {
00036
00037 namespace detail {
00038
00039 struct jpeg_write_is_supported {
00040 template<typename View> struct apply
00041 : public mpl::bool_<jpeg_write_support<View>::is_supported> {};
00042 };
00043
00044 class jpeg_writer_dynamic : public jpeg_writer {
00045 int _quality;
00046 public:
00047 jpeg_writer_dynamic(FILE* file, int quality=100) : jpeg_writer(file) , _quality(quality) {}
00048 jpeg_writer_dynamic(const char* filename, int quality=100) : jpeg_writer(filename), _quality(quality) {}
00049
00050 template <typename Views>
00051 void write_view(const any_image_view<Views>& runtime_view) {
00052 dynamic_io_fnobj<jpeg_write_is_supported, jpeg_writer> op(this);
00053 apply_operation(runtime_view,op);
00054 }
00055 };
00056
00057 class jpeg_type_format_checker {
00058 J_COLOR_SPACE _color_type;
00059 public:
00060 jpeg_type_format_checker(J_COLOR_SPACE color_type_in) :
00061 _color_type(color_type_in) {}
00062 template <typename Image>
00063 bool apply() {
00064 return jpeg_read_support<typename Image::view_t>::color_type==_color_type;
00065 }
00066 };
00067
00068 struct jpeg_read_is_supported {
00069 template<typename View> struct apply
00070 : public mpl::bool_<jpeg_read_support<View>::is_supported> {};
00071 };
00072
00073 class jpeg_reader_dynamic : public jpeg_reader {
00074 public:
00075 jpeg_reader_dynamic(FILE* file) : jpeg_reader(file) {}
00076 jpeg_reader_dynamic(const char* filename) : jpeg_reader(filename){}
00077
00078 template <typename Images>
00079 void read_image(any_image<Images>& im) {
00080 if (!construct_matched(im,detail::jpeg_type_format_checker(_cinfo.out_color_space))) {
00081 io_error("jpeg_reader_dynamic::read_image(): no matching image type between those of the given any_image and that of the file");
00082 } else {
00083 im.recreate(get_dimensions());
00084 dynamic_io_fnobj<jpeg_read_is_supported, jpeg_reader> op(this);
00085 apply_operation(view(im),op);
00086 }
00087 }
00088 };
00089
00090 }
00091
00092
00098 template <typename Images>
00099 inline void jpeg_read_image(const char* filename,any_image<Images>& im) {
00100 detail::jpeg_reader_dynamic m(filename);
00101 m.read_image(im);
00102 }
00103
00106 template <typename Images>
00107 inline void jpeg_read_image(const std::string& filename,any_image<Images>& im) {
00108 jpeg_read_image(filename.c_str(),im);
00109 }
00110
00115 template <typename Views>
00116 inline void jpeg_write_view(const char* filename,const any_image_view<Views>& runtime_view) {
00117 detail::jpeg_writer_dynamic m(filename);
00118 m.write_view(runtime_view);
00119 }
00120
00123 template <typename Views>
00124 inline void jpeg_write_view(const std::string& filename,const any_image_view<Views>& runtime_view) {
00125 jpeg_write_view(filename.c_str(),runtime_view);
00126 }
00127
00128 } }
00129
00130 #endif