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 LMMSE_DEMOSAIC_H
31 #define LMMSE_DEMOSAIC_H
32 
33 #include <string>
34 
35 //#include <libraw/libraw.h>
36 
37 #include "../base/operation.hh"
38 #include "../rt/rtengine/rawimagesource.hh"
39 
40 
41 
42 namespace PF
43 {
44 
45   class LMMSEDemosaicPar: public OpParBase
46   {
47   public:
48     LMMSEDemosaicPar();
has_intensity()49     bool has_intensity() { return false; }
has_opacity()50     bool has_opacity() { return false; }
needs_input()51     bool needs_input() { return true; }
52 
set_image_hints(VipsImage * img)53     void set_image_hints( VipsImage* img )
54     {
55       if( !img ) return;
56       OpParBase::set_image_hints( img );
57       rgb_image( get_xsize(), get_ysize() );
58     }
59 
60     /* Function to derive the output area from the input area
61      */
transform(const VipsRect * rin,VipsRect * rout,int)62     virtual void transform(const VipsRect* rin, VipsRect* rout, int /*id*/)
63     {
64       int border = 10;
65       rout->left = rin->left+border;
66       rout->top = rin->top+border;
67       rout->width = rin->width-border*2;
68       rout->height = rin->height-border*2;
69     }
70 
71     /* Function to derive the area to be read from input images,
72        based on the requested output area
73     */
transform_inv(const VipsRect * rout,VipsRect * rin,int)74     virtual void transform_inv(const VipsRect* rout, VipsRect* rin, int /*id*/)
75     {
76       int border = 10;
77 			// Output region aligned to the Bayer pattern
78 			int raw_left = (rout->left/2)*2;
79 			//if( raw_left < border ) raw_left = 0;
80 
81 			int raw_top = (rout->top/2)*2;
82       //if( raw_top < border ) raw_top = 0;
83 
84       int raw_right = rout->left+rout->width-1;
85 			//if( raw_right > (in->Xsize-border-1) ) raw_right = in->Xsize-1;
86 
87 			int raw_bottom = rout->top+rout->height-1;
88       //if( raw_bottom > (in->Ysize-border-1) ) raw_bottom = in->Ysize-1;
89 
90 			int raw_width = raw_right-raw_left+1;
91 			int raw_height = raw_bottom-raw_top+1;
92 
93       rin->left = raw_left-border;
94       rin->top = raw_top-border;
95       rin->width = raw_width+border*2;
96       rin->height = raw_height+border*2;
97 
98       if( (rin->width%2) ) rin->width += 1;
99       if( (rin->height%2) ) rin->height += 1;
100     }
101 
102 
103 
104     VipsImage* build(std::vector<VipsImage*>& in, int first,
105 										 VipsImage* imap, VipsImage* omap,
106 										 unsigned int& level);
107   };
108 
109 
110 
111 
112   template < OP_TEMPLATE_DEF >
113   class LMMSEDemosaicProc
114   {
115   public:
render(VipsRegion ** in,int n,int in_first,VipsRegion * imap,VipsRegion * omap,VipsRegion * out,LMMSEDemosaicPar * par)116     void render(VipsRegion** in, int n, int in_first,
117 								VipsRegion* imap, VipsRegion* omap,
118 								VipsRegion* out, LMMSEDemosaicPar* par)
119     {
120       //fast_demosaic( in, n, in_first,
121       //	     imap, omap, out, par );
122     }
123   };
124 
125 
126   template < OP_TEMPLATE_DEF_TYPE_SPEC >
127   class LMMSEDemosaicProc< OP_TEMPLATE_IMP_TYPE_SPEC(float) >
128   {
129   public:
render(VipsRegion ** in,int n,int in_first,VipsRegion * imap,VipsRegion * omap,VipsRegion * out,LMMSEDemosaicPar * par)130     void render(VipsRegion** in, int n, int in_first,
131 								VipsRegion* imap, VipsRegion* omap,
132 								VipsRegion* out, LMMSEDemosaicPar* par)
133     {
134 			rtengine::RawImageSource rawimg;
135 			rawimg.lmmse_demosaic( in[0], out );
136     }
137   };
138 
139 
140   ProcessorBase* new_lmmse_demosaic();
141 }
142 
143 
144 #endif
145