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 <previewhandler.h>
23 using namespace rtengine
;
24 using namespace rtengine::procparams
;
26 PreviewHandler::PreviewHandler () : image(NULL
) {
28 pih
= new PreviewHandlerIdleHelper
;
30 pih
->destroyed
= false;
34 PreviewHandler::~PreviewHandler () {
37 pih
->destroyed
= true;
42 //----------------previewimagelistener functions--------------------
46 PreviewHandlerIdleHelper
* pih
;
51 int iasetimage (void* data
) {
55 iaimgpar
* iap
= (iaimgpar
*)data
;
56 PreviewHandlerIdleHelper
* pih
= iap
->pih
;
59 if (pih
->pending
== 1)
68 if (pih
->phandler
->image
) {
69 IImage8
* temp
= pih
->phandler
->image
;
70 temp
->getMutex().lock ();
71 pih
->phandler
->image
= iap
->image
;
72 temp
->getMutex().unlock ();
75 pih
->phandler
->image
= iap
->image
;
76 pih
->phandler
->cropParams
= iap
->cp
;
77 pih
->phandler
->previewScale
= iap
->scale
;
86 void PreviewHandler::setImage (rtengine::IImage8
* i
, double scale
, rtengine::procparams::CropParams cp
) {
90 iaimgpar
* iap
= new iaimgpar
;
96 g_idle_add (iasetimage
, iap
);
99 int iadelimage (void* data
) {
101 gdk_threads_enter ();
103 iaimgpar
* iap
= (iaimgpar
*)data
;
104 PreviewHandlerIdleHelper
* pih
= iap
->pih
;
106 if (pih
->destroyed
) {
107 if (pih
->pending
== 1)
112 gdk_threads_leave ();
116 if (pih
->phandler
->image
) {
117 IImage8
* temp
= pih
->phandler
->image
;
118 temp
->getMutex().lock ();
119 pih
->phandler
->image
= NULL
;
120 temp
->getMutex().unlock ();
123 pih
->phandler
->previewImgMutex
.lock ();
124 pih
->phandler
->previewImg
.clear ();
125 pih
->phandler
->previewImgMutex
.unlock ();
130 gdk_threads_leave ();
135 void PreviewHandler::delImage (IImage8
* i
) {
139 iaimgpar
* iap
= new iaimgpar
;
143 g_idle_add (iadelimage
, iap
);
146 int imready (void* data
) {
148 gdk_threads_enter ();
150 iaimgpar
* iap
= (iaimgpar
*)data
;
151 PreviewHandlerIdleHelper
* pih
= iap
->pih
;
153 if (pih
->destroyed
) {
154 if (pih
->pending
== 1)
159 gdk_threads_leave ();
163 pih
->phandler
->previewImgMutex
.lock ();
164 pih
->phandler
->previewImg
= Gdk::Pixbuf::create_from_data (pih
->phandler
->image
->getData(), Gdk::COLORSPACE_RGB
, false, 8, pih
->phandler
->image
->getWidth(), pih
->phandler
->image
->getHeight(), 3*pih
->phandler
->image
->getWidth());
165 pih
->phandler
->previewImgMutex
.unlock ();
166 pih
->phandler
->cropParams
= iap
->cp
;
167 pih
->phandler
->previewImageChanged ();
171 gdk_threads_leave ();
176 void PreviewHandler::imageReady (CropParams cp
) {
179 iaimgpar
* iap
= new iaimgpar
;
182 g_idle_add (imready
, iap
);
185 Glib::RefPtr
<Gdk::Pixbuf
> PreviewHandler::getRoughImage (int x
, int y
, int w
, int h
, double zoom
) {
187 Glib::RefPtr
<Gdk::Pixbuf
> resPixbuf
;
188 previewImgMutex
.lock ();
190 double totalZoom
= zoom
*previewScale
;
191 if (w
>previewImg
->get_width()*totalZoom
)
192 w
= image
->getWidth()*totalZoom
;
193 if (h
>previewImg
->get_height()*totalZoom
)
194 h
= image
->getHeight()*totalZoom
;
201 if ((ix
+w
)/totalZoom
>previewImg
->get_width())
202 ix
= previewImg
->get_width()*totalZoom
- w
;
203 if ((iy
+h
)/totalZoom
>previewImg
->get_height())
204 iy
= previewImg
->get_height()*totalZoom
- h
;
206 resPixbuf
= Gdk::Pixbuf::create (Gdk::COLORSPACE_RGB
, false, 8, w
, h
);
207 previewImg
->scale (resPixbuf
, 0, 0, w
, h
, -ix
, -iy
, totalZoom
, totalZoom
, Gdk::INTERP_NEAREST
);
209 previewImgMutex
.unlock ();
213 Glib::RefPtr
<Gdk::Pixbuf
> PreviewHandler::getRoughImage (int desiredW
, int desiredH
, double& zoom_
) {
215 Glib::RefPtr
<Gdk::Pixbuf
> resPixbuf
;
216 previewImgMutex
.lock ();
218 double zoom1
= (double)desiredW
/ previewImg
->get_width();
219 double zoom2
= (double)desiredH
/ previewImg
->get_height();
220 double zoom
= zoom1
<zoom2
? zoom1
: zoom2
;
222 resPixbuf
= Gdk::Pixbuf::create (Gdk::COLORSPACE_RGB
, false, 8, image
->getWidth()*zoom
, image
->getHeight()*zoom
);
223 previewImg
->scale (resPixbuf
, 0, 0, previewImg
->get_width()*zoom
, previewImg
->get_height()*zoom
, 0, 0, zoom
, zoom
, Gdk::INTERP_NEAREST
);
224 zoom_
= zoom
/ previewScale
;
226 previewImgMutex
.unlock ();
230 void PreviewHandler::previewImageChanged () {
232 for (std::list
<PreviewListener
*>::iterator i
=listeners
.begin(); i
!=listeners
.end(); i
++)
233 (*i
)->previewImageChanged ();