1 /* -*- C++ -*-
2  *
3  *  This file is part of RawTherapee.
4  *
5  *  Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
6  *
7  *  RawTherapee is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  RawTherapee is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with RawTherapee.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 #ifndef _IMPROCFUN_H_
21 #define _IMPROCFUN_H_
22 
23 #include "imagefloat.h"
24 #include "image16.h"
25 #include "image8.h"
26 #include "procparams.h"
27 #include "coord2d.h"
28 #include "color.h"
29 #include "labimage.h"
30 #include "LUT.h"
31 #include "lcp.h"
32 #include "dcp.h"
33 #include "curves.h"
34 #include "cplx_wavelet_dec.h"
35 #include "pipettebuffer.h"
36 #include "gamutwarning.h"
37 
38 namespace rtengine {
39 
40 using namespace procparams;
41 
42 struct ImProcData {
43     const ProcParams *params;
44     double scale;
45     bool multiThread;
46 
47     explicit ImProcData(const ProcParams *p=nullptr, double s=1.0, bool m=true):
paramsImProcData48         params(p), scale(s), multiThread(m) {}
49 };
50 
51 
52 class ImProcFunctions {
53 public:
54     //----------------------------------------------------------------------
55     // constructor/destructor and initialization/state manipulation
56     //----------------------------------------------------------------------
57     ImProcFunctions(const ProcParams* iparams, bool imultiThread=true);
58     ~ImProcFunctions();
59 
60     void setScale(double iscale);
61 
62     void updateColorProfiles(const Glib::ustring& monitorProfile, RenderingIntent monitorIntent, bool softProof, bool gamutCheck);
setMonitorTransform(cmsHTRANSFORM xform)63     void setMonitorTransform(cmsHTRANSFORM xform) { monitorTransform = xform; }
64 
setDCPProfile(DCPProfile * dcp,const DCPProfile::ApplyState & as)65     void setDCPProfile(DCPProfile *dcp, const DCPProfile::ApplyState &as)
66     {
67         dcpProf = dcp;
68         dcpApplyState = &as;
69     }
70 
setPipetteBuffer(PipetteBuffer * pb)71     void setPipetteBuffer(PipetteBuffer *pb)
72     {
73         pipetteBuffer = pb;
74     }
75 
76     void setProgressListener(ProgressListener *pl, int num_previews);
77     //----------------------------------------------------------------------
78 
79     //----------------------------------------------------------------------
80     // pipeline management
81     //----------------------------------------------------------------------
82     enum class Stage {
83         STAGE_0,
84         STAGE_1,
85         STAGE_2,
86         STAGE_3
87     };
88     enum class Pipeline {
89         THUMBNAIL,
90         NAVIGATOR,
91         PREVIEW,
92         OUTPUT
93     };
94     bool process(Pipeline pipeline, Stage stage, Imagefloat *img);
95 
96     void setViewport(int ox, int oy, int fw, int fh);
97     void setOutputHistograms(LUTu *histToneCurve, LUTu *histCCurve, LUTu *histLCurve);
98     void setShowSharpeningMask(bool yes);
99     //----------------------------------------------------------------------
100 
101     //----------------------------------------------------------------------
102     // image processing operations
103     //----------------------------------------------------------------------
104     void firstAnalysis(const Imagefloat* const working, const ProcParams &params, LUTu & vhist16);
105 
106     void labAdjustments(Imagefloat *rgb);
107     bool doSharpening(Imagefloat *rgb, const SharpeningParams &sharpenParam, bool showMask);
108     bool sharpening(Imagefloat *img);
109     bool prsharpening(Imagefloat *img);
110     void transform(Imagefloat* original, Imagefloat* transformed, int cx, int cy, int sx, int sy, int oW, int oH, int fW, int fH, const FramesMetaData *metadata, int rawRotationDeg, bool highQuality);
111     void resize(Imagefloat* src, Imagefloat* dst, float dScale);
112     void Lanczos(Imagefloat *src, Imagefloat *dst, float scale);
113     void impulsedenoise(Imagefloat *rgb);   //Emil's impulse denoise
114     bool textureBoost(Imagefloat *rgb);
115 
116     struct DenoiseInfoStore {
DenoiseInfoStoreDenoiseInfoStore117         DenoiseInfoStore () : chM (0), max_r{}, max_b{}, ch_M{}, valid (false)  {}
118         float chM;
119         float max_r[9];
120         float max_b[9];
121         float ch_M[9];
122         bool valid;
123     };
124     void denoiseComputeParams(ImageSource *imgsrc, const ColorTemp &currWB, DenoiseInfoStore &store, procparams::DenoiseParams &dnparams);
125     void denoise(ImageSource *imgsrc, const ColorTemp &currWB, Imagefloat *img, const DenoiseInfoStore &store, const procparams::DenoiseParams &dnparams);
126 
127     void defringe(Imagefloat *rgb);
128     void dehaze(Imagefloat *rgb);
129     void dynamicRangeCompression(Imagefloat *rgb);
130     bool localContrast(Imagefloat *rgb);
131     bool toneEqualizer(Imagefloat *rgb);
132     void softLight(Imagefloat *rgb);
133     bool colorCorrection(Imagefloat *rgb);
134     void logEncoding(Imagefloat *rgb);
135     void filmGrain(Imagefloat *rgb);
136     bool guidedSmoothing(Imagefloat *rgb);
137     void hslEqualizer(Imagefloat *rgb);
138     void channelMixer(Imagefloat *rgb);
139     void exposure(Imagefloat *rgb);
140     void rgbCurves(Imagefloat *rgb);
141     void blackAndWhite(Imagefloat *rgb);
142     void toneCurve(Imagefloat *img);
143     void saturationVibrance(Imagefloat *img);
144     void filmSimulation(Imagefloat *img);
145     void creativeGradients(Imagefloat *img);
146     //----------------------------------------------------------------------
147 
148     //----------------------------------------------------------------------
149     // Lab/RGB conversion
150     //----------------------------------------------------------------------
151     void rgb2monitor(Imagefloat *img, Image8* image);
152 
153     Image8 *rgb2out(Imagefloat *img, int cx, int cy, int cw, int ch, const procparams::ColorManagementParams &icm, bool consider_histogram_settings = true);
154 
155     Imagefloat *rgb2out(Imagefloat *img, const procparams::ColorManagementParams &icm);
156 
157     void rgb2lab(Imagefloat &src, LabImage &dst, const Glib::ustring &workingSpace);
rgb2lab(Imagefloat & src,LabImage & dst)158     void rgb2lab(Imagefloat &src, LabImage &dst) { rgb2lab(src, dst, params->icm.workingProfile); }
159 
160     void lab2rgb(const LabImage &src, Imagefloat &dst, const Glib::ustring &workingSpace);
lab2rgb(const LabImage & src,Imagefloat & dst)161     void lab2rgb(const LabImage &src, Imagefloat &dst) { lab2rgb(src, dst, params->icm.workingProfile); }
162     //----------------------------------------------------------------------
163 
164     //----------------------------------------------------------------------
165     // misc helper functions for image processing ops
166     //----------------------------------------------------------------------
167     bool needsLuminanceOnly();
168     bool needsTransform();
169     bool needsPCVignetting();
170 
171     float resizeScale(const ProcParams* params, int fw, int fh, int &imw, int &imh);
172 
173     void getAutoLog(ImageSource *imgsrc, procparams::LogEncodingParams &params);
174 
175     static void getAutoExp(const LUTu & histogram, int histcompr, double clip, double& expcomp, int& bright, int& contr, int& black, int& hlcompr, int& hlcomprthresh);
176 
177     static double getAutoDistor(const Glib::ustring& fname, int thumb_size);
178     bool transCoord(int W, int H, int x, int y, int w, int h, int& xv, int& yv, int& wv, int& hv, double ascaleDef = -1, const LensCorrection *pLCPMap = nullptr);
179     bool transCoord(int W, int H, const std::vector<Coord2D> &src, std::vector<Coord2D> &red,  std::vector<Coord2D> &green, std::vector<Coord2D> &blue, double ascaleDef = -1, const LensCorrection *pLCPMap = nullptr);
180     double getTransformAutoFill(int oW, int oH, const LensCorrection *pLCPMap = nullptr);
181     //----------------------------------------------------------------------
182 
183     class DeltaEData {
184     public:
185         bool ok;
186         float L;
187         float C;
188         float H;
189         double x;
190         double y;
191 
DeltaEData()192         DeltaEData():
193             ok(false), L(0), C(0), H(0), x(-1), y(-1) {}
194     };
195     DeltaEData deltaE;
196     int setDeltaEData(EditUniqueID id, double x, double y);
197 
198     // Spot Removal Tool
199     void removeSpots (rtengine::Imagefloat* img, rtengine::ImageSource* imgsrc, const std::vector<procparams::SpotEntry> &entries, const PreviewProps &pp, const rtengine::ColorTemp &currWB, const procparams::ColorManagementParams *cmp, int tr);
200 
201 private:
202     cmsHPROFILE monitor;
203     cmsHTRANSFORM monitorTransform;
204     std::unique_ptr<GamutWarning> gamutWarning;
205 
206     const ProcParams* params;
207     double scale;
208     bool multiThread;
209     Pipeline cur_pipeline;
210 
211     DCPProfile *dcpProf;
212     const DCPProfile::ApplyState *dcpApplyState;
213 
214     PipetteBuffer *pipetteBuffer;
215 
216     double lumimul[3];
217 
218     int offset_x;
219     int offset_y;
220     int full_width;
221     int full_height;
222 
223     LUTu *histToneCurve;
224     LUTu *histCCurve;
225     LUTu *histLCurve;
226 
227     bool show_sharpening_mask;
228 
229     ProgressListener *plistener;
230     int progress_step;
231     int progress_end;
232 
233 
234 private:
235     void transformLuminanceOnly(Imagefloat* original, Imagefloat* transformed, int cx, int cy, int oW, int oH, int fW, int fH, bool creative);
236     void transformGeneral(bool highQuality, Imagefloat *original, Imagefloat *transformed, int cx, int cy, int sx, int sy, int oW, int oH, int fW, int fH, const LensCorrection *pLCPMap);
237     void transformLCPCAOnly(Imagefloat *original, Imagefloat *transformed, int cx, int cy, const LensCorrection *pLCPMap);
238 
239     bool needsCA();
240     bool needsDistortion();
241     bool needsRotation();
242     bool needsPerspective();
243     bool needsGradient();
244     bool needsVignetting();
245     bool needsLCP();
246     bool needsLensfun();
247 
248     template <class Ret, class Method>
249     Ret apply(Method op, Imagefloat *img);
250 };
251 
252 
253 } // namespace rtengine
254 
255 #endif
256