1 //
2 // Copyright 2012 Olivier Tournaire
3 // Copyright 2007 Christian Henning
4 //
5 // Distributed under the Boost Software License, Version 1.0
6 // See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt
8 //
9 #ifndef BOOST_GIL_EXTENSION_IO_RAW_DETAIL_DEVICE_HPP
10 #define BOOST_GIL_EXTENSION_IO_RAW_DETAIL_DEVICE_HPP
11 
12 #include <boost/gil/extension/io/raw/tags.hpp>
13 
14 #include <boost/gil/io/base.hpp>
15 #include <boost/gil/io/device.hpp>
16 
17 #include <memory>
18 #include <string>
19 #include <type_traits>
20 
21 namespace boost { namespace gil { namespace detail {
22 
23 class raw_device_base
24 {
25 public:
26 
27     ///
28     /// Constructor
29     ///
raw_device_base()30     raw_device_base()
31     : _processor_ptr( new LibRaw )
32     {}
33 
34     // iparams getters
get_camera_manufacturer()35     std::string get_camera_manufacturer() { return std::string(_processor_ptr.get()->imgdata.idata.make);  }
get_camera_model()36     std::string get_camera_model()        { return std::string(_processor_ptr.get()->imgdata.idata.model); }
get_raw_count()37     unsigned get_raw_count()              { return _processor_ptr.get()->imgdata.idata.raw_count; }
get_dng_version()38     unsigned get_dng_version()            { return _processor_ptr.get()->imgdata.idata.dng_version; }
get_colors()39     int get_colors()                      { return _processor_ptr.get()->imgdata.idata.colors; }
get_filters()40     unsigned get_filters()                { return _processor_ptr.get()->imgdata.idata.filters; }
get_cdesc()41     std::string get_cdesc()               { return std::string(_processor_ptr.get()->imgdata.idata.cdesc); }
42 
43     // image_sizes getters
get_raw_width()44     unsigned short get_raw_width()    { return _processor_ptr.get()->imgdata.sizes.raw_width;  }
get_raw_height()45     unsigned short get_raw_height()   { return _processor_ptr.get()->imgdata.sizes.raw_height; }
get_image_width()46     unsigned short get_image_width()  { return _processor_ptr.get()->imgdata.sizes.width;  }
get_image_height()47     unsigned short get_image_height() { return _processor_ptr.get()->imgdata.sizes.height; }
get_top_margin()48     unsigned short get_top_margin()   { return _processor_ptr.get()->imgdata.sizes.top_margin;  }
get_left_margin()49     unsigned short get_left_margin()  { return _processor_ptr.get()->imgdata.sizes.left_margin; }
get_iwidth()50     unsigned short get_iwidth()       { return _processor_ptr.get()->imgdata.sizes.iwidth;  }
get_iheight()51     unsigned short get_iheight()      { return _processor_ptr.get()->imgdata.sizes.iheight; }
get_pixel_aspect()52     double get_pixel_aspect()         { return _processor_ptr.get()->imgdata.sizes.pixel_aspect;  }
get_flip()53     int get_flip()                    { return _processor_ptr.get()->imgdata.sizes.flip; }
54 
55     // colordata getters
56     // TODO
57 
58     // imgother getters
get_iso_speed()59     float get_iso_speed()     { return _processor_ptr.get()->imgdata.other.iso_speed; }
get_shutter()60     float get_shutter()       { return _processor_ptr.get()->imgdata.other.shutter; }
get_aperture()61     float get_aperture()      { return _processor_ptr.get()->imgdata.other.aperture; }
get_focal_len()62     float get_focal_len()     { return _processor_ptr.get()->imgdata.other.focal_len; }
get_timestamp()63     time_t get_timestamp()    { return _processor_ptr.get()->imgdata.other.timestamp; }
get_shot_order()64     unsigned int get_shot_order() { return _processor_ptr.get()->imgdata.other.shot_order; }
get_gpsdata()65     unsigned* get_gpsdata()   { return _processor_ptr.get()->imgdata.other.gpsdata; }
get_desc()66     std::string get_desc()    { return std::string(_processor_ptr.get()->imgdata.other.desc); }
get_artist()67     std::string get_artist()  { return std::string(_processor_ptr.get()->imgdata.other.artist); }
68 
get_version()69     std::string get_version()               { return std::string(_processor_ptr.get()->version()); }
get_unpack_function_name()70     std::string get_unpack_function_name()  { return std::string(_processor_ptr.get()->unpack_function_name()); }
71 
get_mem_image_format(int * widthp,int * heightp,int * colorsp,int * bpp)72     void get_mem_image_format(int *widthp, int *heightp, int *colorsp, int *bpp) { _processor_ptr.get()->get_mem_image_format(widthp, heightp, colorsp, bpp); }
73 
unpack()74     int unpack()                                                         { return _processor_ptr.get()->unpack(); }
dcraw_process()75     int dcraw_process()                                                  { return _processor_ptr.get()->dcraw_process(); }
dcraw_make_mem_image(int * error_code=nullptr)76     libraw_processed_image_t* dcraw_make_mem_image(int* error_code=nullptr) { return _processor_ptr.get()->dcraw_make_mem_image(error_code); }
77 
78 protected:
79 
80     using libraw_ptr_t = std::shared_ptr<LibRaw>;
81     libraw_ptr_t _processor_ptr;
82 };
83 
84 /*!
85  *
86  * file_stream_device specialization for raw images
87  */
88 template<>
89 class file_stream_device< raw_tag > : public raw_device_base
90 {
91 public:
92 
93     struct read_tag {};
94 
95     ///
96     /// Constructor
97     ///
file_stream_device(std::string const & file_name,read_tag=read_tag ())98     file_stream_device( std::string const& file_name
99                       , read_tag   = read_tag()
100                       )
101     {
102         io_error_if( _processor_ptr.get()->open_file( file_name.c_str() ) != LIBRAW_SUCCESS
103                    , "file_stream_device: failed to open file"
104                    );
105     }
106 
107     ///
108     /// Constructor
109     ///
file_stream_device(const char * file_name,read_tag=read_tag ())110     file_stream_device( const char* file_name
111                       , read_tag   = read_tag()
112                       )
113     {
114         io_error_if( _processor_ptr.get()->open_file( file_name ) != LIBRAW_SUCCESS
115                    , "file_stream_device: failed to open file"
116                    );
117     }
118 };
119 
120 template< typename FormatTag >
121 struct is_adaptable_input_device<FormatTag, LibRaw, void> : std::true_type
122 {
123     using device_type = file_stream_device<FormatTag>;
124 };
125 
126 
127 } // namespace detail
128 } // namespace gil
129 } // namespace boost
130 
131 #endif
132