1 /*
2  *  This file is part of RawTherapee.
3  *
4  *  Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
5  *
6  *  RawTherapee is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  RawTherapee is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with RawTherapee.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifndef __CROPHANDLER__
20 #define __CROPHANDLER__
21 
22 #include <atomic>
23 #include <vector>
24 
25 #include <gtkmm.h>
26 
27 #include "../rtengine/rtengine.h"
28 
29 #include "edit.h"
30 #include "lockablecolorpicker.h"
31 #include "threadutils.h"
32 
33 class CropDisplayHandler
34 {
35 
36 public:
~CropDisplayHandler()37     virtual ~CropDisplayHandler() {}
cropImageUpdated()38     virtual void cropImageUpdated    () {}
cropWindowChanged()39     virtual void cropWindowChanged   () {}
initialImageArrived()40     virtual void initialImageArrived () {}
setDisplayPosition(int x,int y)41     virtual void setDisplayPosition  (int x, int y) {}
42 };
43 
44 /**
45  *  This class handle the displayed part of the image, ask for the initial data and process it so it can display it.
46  *  Its position on the preview is handled not set by this class but by the CropHandlerListener (i.e. CropWindow) with which it works closely.
47  */
48 class CropHandler final :
49     public rtengine::DetailedCropListener,
50     public rtengine::SizeListener
51 {
52 public:
53     CropHandler ();
54     ~CropHandler () override;
55 
setDisplayHandler(CropDisplayHandler * l)56     void    setDisplayHandler (CropDisplayHandler* l)
57     {
58         displayHandler = l;
59     }
60     void    setEditSubscriber      (EditSubscriber* newSubscriber);
61 
62     void    newImage      (rtengine::StagedImageProcessor* ipc_, bool isDetailWindow);
63     void    setZoom       (int z, int centerx = -1, int centery = -1);
64     float   getZoomFactor ();
65     double  getFitZoom    ();
66     double  getFitCropZoom();
67     bool    isFullDisplay ();
68     void    setWSize      (int w, int h);
69     void    getWSize      (int& w, int &h);
70     void    getAnchorPosition (int& x, int& y);
71     void    setAnchorPosition (int x, int y, bool update = true);
72     void    moveAnchor    (int deltaX, int deltaY, bool update = true);
73     void    centerAnchor  (bool update = true);
74     void    getPosition   (int& x, int& y);
75     void    getSize       (int& w, int& h);
76     void    getFullImageSize (int& w, int& h);
77 
78     void    setEnabled (bool e);
79     bool    getEnabled ();
80 
81     void    colorPick (const rtengine::Coord &pickerPos, float &r, float &g, float &b, float &rpreview, float &gpreview, float &bpreview, LockableColorPicker::Size size);
82 
getCrop()83     rtengine::DetailedCrop* getCrop()
84     {
85         return crop;
86     }
87 
88     // DetailedCropListener interface
89     void setDetailedCrop(
90         rtengine::IImage8* im,
91         rtengine::IImage8* imworking,
92         const rtengine::procparams::ColorManagementParams& cmp,
93         const rtengine::procparams::CropParams& cp,
94         int cx,
95         int cy,
96         int cw,
97         int ch,
98         int skip
99     ) override;
100     void getWindow(int& cwx, int& cwy, int& cww, int& cwh, int& cskip) override;
101 
102     // SizeListener interface
103     void    sizeChanged  (int w, int h, int ow, int oh) override;
104 
105     void    update  ();
106 
107 
108     rtengine::procparams::CropParams cropParams;
109     rtengine::procparams::ColorManagementParams colorParams;
110     Glib::RefPtr<Gdk::Pixbuf> cropPixbuf;     // image displayed on monitor, using the monitor profile (i.e. lab to monitor profile)
111     Glib::RefPtr<Gdk::Pixbuf> cropPixbuftrue; // internal image in output color space for analysis (i.e. lab to either Working profile or Output profile, depending on options.rtSettings.HistogramWorking)
112 
113     MyMutex cimg;
114 
115 private:
116     void    compDim ();
117 
118     int zoom;               // scale factor (e.g. 5 if 1:5 scale) ; if 1:1 scale and bigger, factor is multiplied by 1000  (i.e. 1000 for 1:1 scale, 2000 for 2:1, etc...)
119     int ww, wh;             // size of the crop's canvas on the screen ; might be bigger than the displayed image, but not smaller
120     int cax, cay;           // clamped crop anchor's coordinate, i.e. point of the image that coincide to the center of the display area, expressed in image coordinates; cannot be outside the image's bounds; but if cax==cay==-1, designate the center of the image
121     int cx, cy, cw, ch;     // position and size of the requested crop ; position expressed in image coordinates, so cx and cy might be negative and cw and ch higher than the image's 1:1 size
122     int cropX, cropY, cropW, cropH; // cropPixbuf's displayed area (position and size), i.e. coordinates in 1:1 scale, i.e. cx, cy, cw & ch trimmed to the image's bounds
123     bool enabled;
124     std::vector<unsigned char> cropimg;
125     std::vector<unsigned char> cropimgtrue;
126     int cropimg_width, cropimg_height, cix, ciy, ciw, cih, cis;
127 
128     rtengine::StagedImageProcessor* ipc;
129     rtengine::DetailedCrop* crop;
130 
131     CropDisplayHandler* displayHandler;
132 
133     std::atomic<bool> redraw_needed;
134     std::atomic<bool> initial;
135 
136     IdleRegister idle_register;
137 
138     std::unique_ptr<Glib::ThreadPool> thread_pool_;
139     sigc::connection zoom_conn_;
140 };
141 
142 #endif
143