Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PreviewWindow.cpp
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
10 /* This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This software is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this software. If not, see
22  * <http://www.gnu.org/licenses/>.
23  *
24  */
25 
26 #include "PreviewWindow.h"
27 #include <wx/dcbuffer.h>
28 #include <wx/dcclient.h>
29 #include "ToolboxApp.h"
30 #include "base_wx/platform.h"
31 
32 // our image control
33 bool PreviewWindow::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name)
34 {
35  wxScrolledWindow::Create(parent, id, pos, size, style, name);
36  m_scaleFactor = 1.0;
37  m_fitToWindow = true;
38  SetBackgroundStyle(wxBG_STYLE_PAINT);
39  // bind event handler
40  Bind(wxEVT_SIZE, &PreviewWindow::OnSize, this);
41  Bind(wxEVT_PAINT, &PreviewWindow::OnPaint, this);
42 
43  return true;
44 }
45 
46 void PreviewWindow::setImage(const wxString& file)
47 {
48  if(!file.IsEmpty() && wxFileExists(file))
49  {
50  m_iccProfile.clear();
51  try
52  {
53  m_image.LoadFile(file);
54  // load also icc profile
55  vigra::ImageImportInfo info(file.mb_str(HUGIN_CONV_FILENAME));
56  m_iccProfile = info.getICCProfile();
57  }
58  catch (...)
59  {
60  m_image.Destroy();
61  SetVirtualSize(100, 100);
62  Refresh(true);
63  return;
64  }
65  rescaleImage();
66  Refresh();
67  }
68  else
69  {
70  m_image.Destroy();
71  SetVirtualSize(100,100);
72  Refresh(true);
73  }
74 }
75 
76 void PreviewWindow::OnPaint(wxPaintEvent& e)
77 {
78  wxAutoBufferedPaintDC dc(this);
79  PrepareDC(dc);
80  dc.SetBackground(GetBackgroundColour());
81  dc.Clear();
82  if (m_bitmap.IsOk())
83  {
84  // draw bitmap
85  dc.DrawBitmap(m_bitmap, 0, 0);
86  };
87 }
88 
89 void PreviewWindow::OnSize(wxSizeEvent &e)
90 {
91  // rescale m_bitmap if needed.
92  if (m_image.IsOk())
93  {
94  if (m_fitToWindow)
95  {
96  setScale(0);
97  }
98  }
99 }
100 
102 {
103  if (m_image.GetWidth() == 0)
104  {
105  return;
106  }
107  wxSize realSize = m_image.GetSize();
108  if (m_fitToWindow)
109  {
111  };
112 
113  //scaling image to screen size
114  wxImage img;
115  if (getScaleFactor()!=1.0)
116  {
117  realSize.SetWidth(scale(m_image.GetWidth()));
118  realSize.SetHeight(scale(m_image.GetHeight()));
119  wxImageResizeQuality resizeQuality = wxIMAGE_QUALITY_NORMAL;
120  if (std::max(m_image.GetWidth(), m_image.GetHeight()) > (ULONG_MAX >> 16))
121  {
122  // wxIMAGE_QUALITY_NORMAL resizes the image with ResampleNearest
123  // this algorithm works only if image dimensions are smaller then
124  // ULONG_MAX >> 16 (actual size of unsigned long differ from system
125  // to system)
126  resizeQuality = wxIMAGE_QUALITY_BOX_AVERAGE;
127  };
128  img=m_image.Scale(realSize.GetWidth(), realSize.GetHeight(), resizeQuality);
129  }
130  else
131  {
132  //therefore we need to create a copy to work on it
133  img=m_image.Copy();
134  };
135  // do color correction only if input image has icc profile or if we found a monitor profile
136  if (!m_iccProfile.empty() || wxGetApp().GetToolboxFrame()->HasMonitorProfile())
137  {
138  HuginBase::Color::CorrectImage(img, m_iccProfile, wxGetApp().GetToolboxFrame()->GetMonitorProfile());
139  };
140  m_bitmap=wxBitmap(img);
141  SetVirtualSize(realSize);
142  SetScrollRate(1, 1);
143  Refresh(true);
144 }
145 
146 void PreviewWindow::setScale(double factor)
147 {
148  if (factor == 0)
149  {
150  m_fitToWindow = true;
151  factor = calcAutoScaleFactor(m_image.GetSize());
152  }
153  else
154  {
155  m_fitToWindow = false;
156  }
157  // update if factor changed
158  if (factor != m_scaleFactor)
159  {
160  m_scaleFactor = factor;
161  // keep existing scale focussed.
162  rescaleImage();
163  }
164 }
165 
167 {
168  return m_fitToWindow ? 0 : m_scaleFactor;
169 }
170 
171 
173 {
174  const int w = size.GetWidth();
175  const int h = size.GetHeight();
176  const wxSize csize = GetSize();
177  const double s1 = (double)csize.GetWidth() / w;
178  const double s2 = (double)csize.GetHeight() / h;
179  return s1 < s2 ? s1 : s2;
180 }
181 
183 {
184  return m_scaleFactor;
185 }
186 
187 int PreviewWindow::scale(int x) const
188 {
189  return (int) (x * getScaleFactor() + 0.5);
190 }
191 
192 double PreviewWindow::scale(double x) const
193 {
194  return x * getScaleFactor();
195 }
196 
197 IMPLEMENT_DYNAMIC_CLASS(PreviewWindow, wxScrolledWindow)
void OnPaint(wxPaintEvent &e)
drawing routine
implementation of huginApp Class
double m_scaleFactor
store current scale factor
Definition: PreviewWindow.h:72
double calcAutoScaleFactor(wxSize size)
calculate new scale factor for this image
#define HUGIN_CONV_FILENAME
Definition: platform.h:40
double getScaleFactor() const
get scale factor (calculates factor when fit to window is active)
int scale(int x) const
helper function to scale of width/height
vigra::ImageImportInfo::ICCProfile m_iccProfile
remember icc profile of currently loaded file
Definition: PreviewWindow.h:75
void GetMonitorProfile(wxString &profileName, cmsHPROFILE &profile)
Definition: wxcms.cpp:195
void setImage(const wxString &filename)
set the current image and mask list, this loads also the image from cache
void CorrectImage(wxImage &image, const vigra::ImageImportInfo::ICCProfile &iccProfile, const cmsHPROFILE &monitorProfile)
apply color correction to given image using input iccProfile and monitor profile
Definition: wxcms.cpp:218
bool Create(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxTAB_TRAVERSAL, const wxString &name="panel")
create the control
IMPLEMENT_DYNAMIC_CLASS(wxTreeListHeaderWindow, wxWindow)
IMPEX double h[25][1024]
Definition: emor.cpp:169
void OnSize(wxSizeEvent &e)
handler called when size of control was changed
wxBitmap m_bitmap
Definition: PreviewWindow.h:67
void setScale(double factor)
set the scaling factor f.
static T max(T x, T y)
Definition: svm.cpp:65
preview window
Definition: PreviewWindow.h:38
static void info(const char *fmt,...)
Definition: svm.cpp:95
void rescaleImage()
rescale the image
double getScale()
return scale factor, 0 for autoscale
declaration of PreviewWindow class
wxImage m_image
Definition: PreviewWindow.h:65