1 /*
2  */
3 
4 /*
5 
6     Copyright (C) 2014 Ferrero Andrea
7 
8     This program is free software: you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21 
22  */
23 
24 /*
25 
26     These files are distributed with PhotoFlow - http://aferrero2707.github.io/PhotoFlow/
27 
28  */
29 
30 #ifndef PF_RAW_IMAGE_H
31 #define PF_RAW_IMAGE_H
32 
33 #include <string>
34 
35 #include <vips/vips.h>
36 
37 #include <glibmm.h>
38 
39 #include "../base/operation.hh"
40 #include "../base/processor.hh"
41 #include "../base/imagepyramid.hh"
42 #include "../base/photoflow.hh"
43 #include "../base/exif_data.hh"
44 #include "../base/array2d.hh"
45 
46 #include "fast_demosaic.hh"
47 
48 //#define PF_USE_LIBRAW
49 #define PF_USE_RAWSPEED
50 //#define PF_USE_DCRAW_RT
51 
52 #ifdef PF_USE_LIBRAW
53 #include <libraw/libraw.h>
54 
55 typedef libraw_data_t dcraw_data_t;
56 #endif
57 
58 #ifdef PF_USE_RAWSPEED
59 #include "rawspeed/src/librawspeed/RawSpeed-API.h"
60 
61 struct dcraw_iparams_t
62 {
63   unsigned int filters;
64   int xtrans_uncropped[6][6];
65   int xtrans[6][6];
66 };
67 
68 struct dcraw_color_data_t
69 {
70   unsigned black;
71   unsigned cblack[4];
72   float maximum;
73   float cam_mul[4];
74   float wb_mul[4];
75   float cam_xyz[4][3];
76   double ca_fitparams[3][2][16];
77 };
78 
79 struct dcraw_sizes_data_t
80 {
81   unsigned short int raw_height, raw_width;
82   unsigned short int height, width;
83   unsigned short int top_margin, left_margin;
84   int flip;
85 };
86 
87 struct dcraw_data_t
88 {
89   dcraw_iparams_t idata;
90 	dcraw_color_data_t color;
91 	dcraw_sizes_data_t sizes;
92 };
93 
94 class ImageMatrices
95 {
96 
97 public:
98     double rgb_cam[3][3] = {};
99     double cam_rgb[3][3] = {};
100     double xyz_cam[3][3] = {};
101     double cam_xyz[3][3] = {};
102 };
103 
104 
105 
106 #endif
107 
108 
109 namespace PF
110 {
111 
112 bool check_xtrans( unsigned filters );
113 dcraw_data_t* get_raw_data( VipsImage* image );
114 
115   class RawImage
116   {
117     int nref;
118     std::string file_name;
119     std::string file_name_real;
120 		std::string cache_file_name;
121 		std::string cache_file_name2;
122 
123     dcraw_data_t dcraw_data;
124     dcraw_data_t* pdata;
125 
126     int iwidth, iheight, crop_x, crop_y;
127 
128 	  float c_black[4];
129 
130 	  ImageMatrices imatrices;
131 
132 #ifdef PF_USE_RAWSPEED
133 		rawspeed::CameraMetaData *meta;
134 #endif
135 
136 		// VipsImages storing the raw data, one band for the pixel values and a nother for the colors
137     VipsImage* image;
138 		// VipsImage storing the dark frame data (if available)
139     VipsImage* df_image;
140 		// VipsImage storing the flat field data (if available)
141     VipsImage* ff_image;
142 		// Demosaiced image
143     VipsImage* demo_image;
144     PF::ProcessorBase* fast_demosaic;
145 
146     unsigned long int* raw_hist;
147 
148     exif_data_t exif_data;
149 
150     Array2D<float> rawData;  // holds preprocessed pixel values, rowData[i][j] corresponds to the ith row and jth column
151     //RawMatrix rawData;  // holds preprocessed pixel values, rowData[i][j] corresponds to the ith row and jth column
152     // Result of CA auto-correction
153     double fitparams[3][2][16];
154 
155     PF::ImagePyramid pyramid;
156 
157     int LinEqSolve(int nDim, double* pfMatr, double* pfVect, double* pfSolution);
158     void CA_correct_RT();
159     void CA_correct_RT_old();
160 
161     bool load_rawspeed();
162     bool load_rawtherapee();
163 
164   public:
165     RawImage( const std::string name );
166     ~RawImage();
167 
ref()168     void ref() { nref += 1; }
unref()169     void unref() { nref -= 1; }
get_nref()170     int get_nref() { return nref; }
171 
172     static void inverse33 (const double (*coeff)[3], double (*icoeff)[3]);
173 
is_xtrans()174     bool is_xtrans() { return check_xtrans( dcraw_data.idata.filters ); }
175 
get_file_name()176     std::string get_file_name() { return file_name_real; }
177 
BL(const int row,const int col)178     unsigned int BL(const int row, const int col)
179     {
180       return ((((row+dcraw_data.sizes.top_margin) & 1) << 1) + ((col+dcraw_data.sizes.left_margin) & 1));
181     }
182 
FC(unsigned row,unsigned col) const183     unsigned FC (unsigned row, unsigned col) const
184     {
185       //return( dcraw_data.idata.filters >> ((((row+dcraw_data.sizes.top_margin) << 1 & 14) +
186       //    ((col+dcraw_data.sizes.left_margin) & 1)) << 1) & 3 );
187       return( dcraw_data.idata.filters >> ((((row) << 1 & 14) + ((col) & 1)) << 1) & 3 );
188     }
189 
FC_xtrans(unsigned row,unsigned col) const190     unsigned FC_xtrans (unsigned row, unsigned col) const
191     {
192       return( dcraw_data.idata.xtrans[(row)%6][(col)%6] );
193     }
194 
195     VipsImage* get_image(unsigned int& level);
196 
197     void print_exif( PF::exif_data_t* data );
198     void print_exif();
199   };
200 
201 	extern std::map<std::string, PF::RawImage*> raw_images;
202 
203 }
204 
205 #endif
206