Proper check for rawzor libraries.
[rawtherapee-fixes.git] / rtgui / guiutils.cc
blobe1f757e0efab6b476680308b9105b2f40b324b41
1 /*
2 * This file is part of RawTherapee.
4 * Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
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.
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.
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/>.
19 #include <guiutils.h>
20 #include <options.h>
21 #include <utils.h>
23 bool removeIfThere (Gtk::Container* cont, Gtk::Widget* w, bool increference) {
25 Glib::ListHandle<Gtk::Widget*> list = cont->get_children ();
26 Glib::ListHandle<Gtk::Widget*>::iterator i = list.begin ();
27 for (; i!=list.end() && *i!=w; i++);
28 if (i!=list.end()) {
29 if (increference)
30 w->reference ();
31 cont->remove (*w);
32 return true;
34 else
35 return false;
38 void thumbInterp (const unsigned char* src, int sw, int sh, unsigned char* dst, int dw, int dh) {
40 if (options.thumbInterp==0)
41 rtengine::nearestInterp (src, sw, sh, dst, dw, dh);
42 else if (options.thumbInterp==1)
43 rtengine::bilinearInterp (src, sw, sh, dst, dw, dh);
46 Glib::ustring removeExtension (const Glib::ustring& filename) {
48 Glib::ustring bname = Glib::path_get_basename(filename);
49 int lastdot = bname.find_last_of ('.');
50 if (lastdot!=bname.npos)
51 return filename.substr (0, filename.size()-(bname.size()-lastdot));
52 else
53 return filename;
56 Glib::ustring getExtension (const Glib::ustring& filename) {
58 Glib::ustring bname = Glib::path_get_basename(filename);
59 int lastdot = bname.find_last_of ('.');
60 if (lastdot!=bname.npos)
61 return filename.substr (filename.size()-(bname.size()-lastdot)+1, filename.npos);
62 else
63 return "";
66 void drawCrop (Cairo::RefPtr<Cairo::Context> cr, int imx, int imy, int imw, int imh, int startx, int starty, double scale, const rtengine::procparams::CropParams& cparams) {
68 cr->set_line_width (1.0);
69 cr->rectangle (imx+0.5, imy+0.5, imw, imh);
70 cr->clip ();
72 double c1x = (cparams.x-startx)*scale;
73 double c1y = (cparams.y-starty)*scale;
74 double c2x = (cparams.x+cparams.w-1-startx)*scale;
75 double c2y = (cparams.y+cparams.h-1-starty)*scale;
77 cr->set_source_rgba (0, 0, 0, 2.0/3.0);
78 cr->rectangle (imx+0.5, imy+0.5, imw, c1y);
79 cr->rectangle (imx+0.5, imy+0.5+c2y, imw, imh-c2y);
80 cr->rectangle (imx+0.5, imy+0.5+c1y, c1x, c2y-c1y+1);
81 cr->rectangle (imx+0.5+c2x, imy+0.5+c1y, imw-c2x, c2y-c1y+1);
82 cr->fill ();
84 // rectangle around the cropped area and guides
85 if (cparams.guide!="None") {
86 double rectx1 = c1x + imx + 0.5;
87 double recty1 = c1y + imy + 0.5;
88 double rectx2 = c2x + imx + 0.5;
89 double recty2 = c2y + imy + 0.5;
90 cr->set_line_width (1.0);
91 cr->set_source_rgb (1.0, 1.0, 1.0);
92 cr->move_to (rectx1, recty1);
93 cr->line_to (rectx2, recty1);
94 cr->line_to (rectx2, recty2);
95 cr->line_to (rectx1, recty2);
96 cr->line_to (rectx1, recty1);
97 cr->stroke ();
98 cr->set_source_rgb (0.0, 0.0, 0.0);
99 std::valarray<double> ds (1);
100 ds[0] = 4;
101 cr->set_dash (ds, 0);
102 cr->move_to (rectx1, recty1);
103 cr->line_to (rectx2, recty1);
104 cr->line_to (rectx2, recty2);
105 cr->line_to (rectx1, recty2);
106 cr->line_to (rectx1, recty1);
107 cr->stroke ();
108 ds.resize (0);
109 cr->set_dash (ds, 0);
111 if (cparams.guide!="Rule of diagonals") {
112 // draw guide lines
113 std::vector<double> horiz_ratios;
114 std::vector<double> vert_ratios;
116 if (cparams.guide=="Rule of thirds") {
117 horiz_ratios.push_back (1.0/3.0);
118 horiz_ratios.push_back (2.0/3.0);
119 vert_ratios.push_back (1.0/3.0);
120 vert_ratios.push_back (2.0/3.0);
122 else if (cparams.guide=="Harmonic means 1") {
123 horiz_ratios.push_back (1.0-0.618);
124 vert_ratios.push_back (1.0-0.618);
126 else if (cparams.guide=="Harmonic means 2") {
127 horiz_ratios.push_back (0.618);
128 vert_ratios.push_back (1.0-0.618);
130 else if (cparams.guide=="Harmonic means 3") {
131 horiz_ratios.push_back (1.0-0.618);
132 vert_ratios.push_back (0.618);
134 else if (cparams.guide=="Harmonic means 4") {
135 horiz_ratios.push_back (0.618);
136 vert_ratios.push_back (0.618);
138 for (int i=0; i<vert_ratios.size(); i++) {
139 cr->set_source_rgb (1.0, 1.0, 1.0);
140 cr->move_to (rectx1 + (rectx2-rectx1) * vert_ratios[i], recty1);
141 cr->line_to (rectx1 + (rectx2-rectx1) * vert_ratios[i], recty2);
142 cr->move_to (rectx1, recty1 + (recty2-recty1) * horiz_ratios[i]);
143 cr->line_to (rectx2, recty1 + (recty2-recty1) * horiz_ratios[i]);
144 cr->stroke ();
145 cr->set_source_rgb (0.0, 0.0, 0.0);
146 std::valarray<double> ds (1);
147 ds[0] = 4;
148 cr->set_dash (ds, 0);
149 cr->move_to (rectx1 + (rectx2-rectx1) * vert_ratios[i], recty1);
150 cr->line_to (rectx1 + (rectx2-rectx1) * vert_ratios[i], recty2);
151 cr->move_to (rectx1, recty1 + (recty2-recty1) * horiz_ratios[i]);
152 cr->line_to (rectx2, recty1 + (recty2-recty1) * horiz_ratios[i]);
153 cr->stroke ();
154 ds.resize (0);
155 cr->set_dash (ds, 0);
158 else {
159 int corners_from[4][2];
160 int corners_to[4][2];
161 int mindim = MIN (rectx2-rectx1+1, recty2-recty1+1);
162 corners_from[0][0] = rectx1;
163 corners_from[0][1] = recty1;
164 corners_to[0][0] = rectx1 + mindim;
165 corners_to[0][1] = recty1 + mindim;
166 corners_from[1][0] = rectx1;
167 corners_from[1][1] = recty2;
168 corners_to[1][0] = rectx1 + mindim;
169 corners_to[1][1] = recty2 - mindim;
170 corners_from[2][0] = rectx2;
171 corners_from[2][1] = recty1;
172 corners_to[2][0] = rectx2 - mindim;
173 corners_to[2][1] = recty1 + mindim;
174 corners_from[3][0] = rectx2;
175 corners_from[3][1] = recty2;
176 corners_to[3][0] = rectx2 - mindim;
177 corners_to[3][1] = recty2 - mindim;
178 for (int i=0; i<4; i++) {
179 cr->set_source_rgb (1.0, 1.0, 1.0);
180 cr->move_to (corners_from[i][0], corners_from[i][1]);
181 cr->line_to (corners_to[i][0], corners_to[i][1]);
182 cr->stroke ();
183 cr->set_source_rgb (0.0, 0.0, 0.0);
184 std::valarray<double> ds (1);
185 ds[0] = 4;
186 cr->set_dash (ds, 0);
187 cr->move_to (corners_from[i][0], corners_from[i][1]);
188 cr->line_to (corners_to[i][0], corners_to[i][1]);
189 cr->stroke ();
190 ds.resize (0);
191 cr->set_dash (ds, 0);
195 cr->reset_clip ();