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  e.Skip();
100 }
101 
103 {
104  if (m_image.GetWidth() == 0)
105  {
106  return;
107  }
108  wxSize realSize = m_image.GetSize();
109  if (m_fitToWindow)
110  {
112  };
113 
114  //scaling image to screen size
115  wxImage img;
116  if (getScaleFactor()!=1.0)
117  {
118  realSize.SetWidth(scale(m_image.GetWidth()));
119  realSize.SetHeight(scale(m_image.GetHeight()));
120  wxImageResizeQuality resizeQuality = wxIMAGE_QUALITY_NORMAL;
121  if (std::max(m_image.GetWidth(), m_image.GetHeight()) > (ULONG_MAX >> 16))
122  {
123  // wxIMAGE_QUALITY_NORMAL resizes the image with ResampleNearest
124  // this algorithm works only if image dimensions are smaller then
125  // ULONG_MAX >> 16 (actual size of unsigned long differ from system
126  // to system)
127  resizeQuality = wxIMAGE_QUALITY_BOX_AVERAGE;
128  };
129  img=m_image.Scale(realSize.GetWidth(), realSize.GetHeight(), resizeQuality);
130  }
131  else
132  {
133  //therefore we need to create a copy to work on it
134  img=m_image.Copy();
135  };
136  // do color correction only if input image has icc profile or if we found a monitor profile
137  if (!m_iccProfile.empty() || wxGetApp().GetToolboxFrame()->HasMonitorProfile())
138  {
139  HuginBase::Color::CorrectImage(img, m_iccProfile, wxGetApp().GetToolboxFrame()->GetMonitorProfile());
140  };
141  m_bitmap=wxBitmap(img);
142  SetVirtualSize(realSize);
143  SetScrollRate(1, 1);
144  Refresh(true);
145 }
146 
147 void PreviewWindow::setScale(double factor)
148 {
149  if (factor == 0)
150  {
151  m_fitToWindow = true;
152  factor = calcAutoScaleFactor(m_image.GetSize());
153  }
154  else
155  {
156  m_fitToWindow = false;
157  }
158  // update if factor changed
159  if (factor != m_scaleFactor)
160  {
161  m_scaleFactor = factor;
162  // keep existing scale focussed.
163  rescaleImage();
164  }
165 }
166 
168 {
169  return m_fitToWindow ? 0 : m_scaleFactor;
170 }
171 
172 
174 {
175  const int w = size.GetWidth();
176  const int h = size.GetHeight();
177  const wxSize csize = GetSize();
178  const double s1 = (double)csize.GetWidth() / w;
179  const double s2 = (double)csize.GetHeight() / h;
180  return s1 < s2 ? s1 : s2;
181 }
182 
184 {
185  return m_scaleFactor;
186 }
187 
188 int PreviewWindow::scale(int x) const
189 {
190  return (int) (x * getScaleFactor() + 0.5);
191 }
192 
193 double PreviewWindow::scale(double x) const
194 {
195  return x * getScaleFactor();
196 }
197 
198 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