]>
Commit | Line | Data |
---|---|---|
5a566d89 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: samples/image/image.cpp | |
3 | // Purpose: sample showing operations with wxImage | |
4 | // Author: Robert Roebling | |
09ddabf7 | 5 | // Modified by: Francesco Montorsi |
5a566d89 VZ |
6 | // Created: 1998 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998-2005 Robert Roebling | |
a2bbedf0 | 9 | // (c) 2005-2009 Vadim Zeitlin |
5a566d89 VZ |
10 | // License: wxWindows licence |
11 | /////////////////////////////////////////////////////////////////////////////// | |
01111366 | 12 | |
3d05544e JS |
13 | // For compilers that support precompilation, includes "wx/wx.h". |
14 | #include "wx/wxprec.h" | |
15 | ||
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #ifndef WX_PRECOMP | |
9061c15c | 21 | #include "wx/wx.h" |
3d05544e JS |
22 | #endif |
23 | ||
01111366 | 24 | #include "wx/image.h" |
a23fd0e1 | 25 | #include "wx/file.h" |
5a566d89 | 26 | #include "wx/filename.h" |
a8d4e3ac VS |
27 | #include "wx/mstream.h" |
28 | #include "wx/wfstream.h" | |
1971d23c | 29 | #include "wx/quantize.h" |
95706ac1 | 30 | #include "wx/stopwatch.h" |
a23fd0e1 | 31 | |
da9df1f5 VZ |
32 | #if wxUSE_CLIPBOARD |
33 | #include "wx/dataobj.h" | |
34 | #include "wx/clipbrd.h" | |
35 | #endif // wxUSE_CLIPBOARD | |
36 | ||
284f2b59 | 37 | #if defined(__WXMSW__) |
481721e8 | 38 | #ifdef wxHAVE_RAW_BITMAP |
9b24b388 | 39 | #include "wx/rawbmp.h" |
481721e8 | 40 | #endif |
c698eae5 | 41 | #endif |
a8d4e3ac | 42 | |
284f2b59 RR |
43 | #if defined(__WXMAC__) || defined(__WXGTK__) |
44 | #define wxHAVE_RAW_BITMAP | |
45 | #include "wx/rawbmp.h" | |
46 | #endif | |
47 | ||
09ddabf7 | 48 | #include "canvas.h" |
01111366 | 49 | |
41f02b9a FM |
50 | #ifndef __WXMSW__ |
51 | #include "../sample.xpm" | |
52 | #endif | |
01111366 | 53 | |
09ddabf7 FM |
54 | // ============================================================================ |
55 | // declarations | |
56 | // ============================================================================ | |
01111366 | 57 | |
09ddabf7 FM |
58 | //----------------------------------------------------------------------------- |
59 | // MyApp | |
60 | //----------------------------------------------------------------------------- | |
61 | ||
62 | class MyApp: public wxApp | |
01111366 | 63 | { |
1d5b7a0b | 64 | public: |
09ddabf7 | 65 | virtual bool OnInit(); |
01111366 RR |
66 | }; |
67 | ||
09ddabf7 | 68 | // ---------------------------------------------------------------------------- |
01111366 | 69 | // MyFrame |
09ddabf7 | 70 | // ---------------------------------------------------------------------------- |
18134a1c | 71 | |
01111366 RR |
72 | class MyFrame: public wxFrame |
73 | { | |
1d5b7a0b VZ |
74 | public: |
75 | MyFrame(); | |
01111366 | 76 | |
01111366 | 77 | void OnAbout( wxCommandEvent &event ); |
ab0f0386 | 78 | void OnNewFrame( wxCommandEvent &event ); |
37ba70a5 | 79 | void OnImageInfo( wxCommandEvent &event ); |
95706ac1 VZ |
80 | void OnThumbnail( wxCommandEvent &event ); |
81 | ||
b3f04dc2 VZ |
82 | #ifdef wxHAVE_RAW_BITMAP |
83 | void OnTestRawBitmap( wxCommandEvent &event ); | |
84 | #endif // wxHAVE_RAW_BITMAP | |
01111366 | 85 | void OnQuit( wxCommandEvent &event ); |
1d5b7a0b | 86 | |
dd38c875 | 87 | #if wxUSE_CLIPBOARD |
da9df1f5 VZ |
88 | void OnCopy(wxCommandEvent& event); |
89 | void OnPaste(wxCommandEvent& event); | |
90 | #endif // wxUSE_CLIPBOARD | |
91 | ||
01111366 | 92 | MyCanvas *m_canvas; |
1d5b7a0b | 93 | |
60a41aee | 94 | private: |
37ba70a5 VZ |
95 | // ask user for the file name and try to load an image from it |
96 | // | |
97 | // return the file path on success, empty string if we failed to load the | |
98 | // image or were cancelled by user | |
99 | static wxString LoadUserImage(wxImage& image); | |
100 | ||
101 | ||
1d5b7a0b VZ |
102 | DECLARE_DYNAMIC_CLASS(MyFrame) |
103 | DECLARE_EVENT_TABLE() | |
01111366 RR |
104 | }; |
105 | ||
1d8acb7d VZ |
106 | // ---------------------------------------------------------------------------- |
107 | // Frame used for showing a standalone image | |
108 | // ---------------------------------------------------------------------------- | |
109 | ||
110 | enum | |
111 | { | |
8b6264b4 | 112 | ID_ROTATE_LEFT = wxID_HIGHEST+1, |
05a8831a | 113 | ID_ROTATE_RIGHT, |
8b6264b4 FM |
114 | ID_RESIZE, |
115 | ID_PAINT_BG | |
1d8acb7d VZ |
116 | }; |
117 | ||
ab0f0386 VZ |
118 | class MyImageFrame : public wxFrame |
119 | { | |
120 | public: | |
85fcb94f VZ |
121 | MyImageFrame(wxFrame *parent, const wxString& desc, const wxImage& image) |
122 | { | |
123 | Create(parent, desc, wxBitmap(image), image.GetImageCount(desc)); | |
124 | } | |
125 | ||
1d8acb7d | 126 | MyImageFrame(wxFrame *parent, const wxString& desc, const wxBitmap& bitmap) |
ab0f0386 | 127 | { |
85fcb94f VZ |
128 | Create(parent, desc, bitmap); |
129 | } | |
130 | ||
a2bbedf0 | 131 | private: |
85fcb94f VZ |
132 | bool Create(wxFrame *parent, |
133 | const wxString& desc, | |
134 | const wxBitmap& bitmap, | |
135 | int numImages = 1) | |
136 | { | |
137 | if ( !wxFrame::Create(parent, wxID_ANY, | |
9a83f860 | 138 | wxString::Format(wxT("Image from %s"), desc), |
85fcb94f VZ |
139 | wxDefaultPosition, wxDefaultSize, |
140 | wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE) ) | |
141 | return false; | |
142 | ||
143 | m_bitmap = bitmap; | |
a2bbedf0 | 144 | m_zoom = 1.; |
85fcb94f | 145 | |
1d8acb7d VZ |
146 | wxMenu *menu = new wxMenu; |
147 | menu->Append(wxID_SAVE); | |
148 | menu->AppendSeparator(); | |
9a83f860 | 149 | menu->AppendCheckItem(ID_PAINT_BG, wxT("&Paint background"), |
437950ce | 150 | "Uncheck this for transparent images"); |
8b6264b4 | 151 | menu->AppendSeparator(); |
9a83f860 | 152 | menu->Append(ID_RESIZE, wxT("&Fit to window\tCtrl-F")); |
a2bbedf0 VZ |
153 | menu->Append(wxID_ZOOM_IN, "Zoom &in\tCtrl-+"); |
154 | menu->Append(wxID_ZOOM_OUT, "Zoom &out\tCtrl--"); | |
155 | menu->Append(wxID_ZOOM_100, "Reset zoom to &100%\tCtrl-1"); | |
05a8831a | 156 | menu->AppendSeparator(); |
9a83f860 VZ |
157 | menu->Append(ID_ROTATE_LEFT, wxT("Rotate &left\tCtrl-L")); |
158 | menu->Append(ID_ROTATE_RIGHT, wxT("Rotate &right\tCtrl-R")); | |
1d8acb7d VZ |
159 | |
160 | wxMenuBar *mbar = new wxMenuBar; | |
9a83f860 | 161 | mbar->Append(menu, wxT("&Image")); |
1d8acb7d VZ |
162 | SetMenuBar(mbar); |
163 | ||
437950ce VZ |
164 | mbar->Check(ID_PAINT_BG, true); |
165 | ||
85fcb94f VZ |
166 | CreateStatusBar(2); |
167 | if ( numImages != 1 ) | |
168 | SetStatusText(wxString::Format("%d images", numImages), 1); | |
05a8831a | 169 | |
ab0f0386 | 170 | SetClientSize(bitmap.GetWidth(), bitmap.GetHeight()); |
05a8831a VZ |
171 | |
172 | UpdateStatusBar(); | |
8b6264b4 | 173 | |
85fcb94f VZ |
174 | Show(); |
175 | ||
176 | return true; | |
ab0f0386 VZ |
177 | } |
178 | ||
0c543b7a VZ |
179 | void OnEraseBackground(wxEraseEvent& WXUNUSED(event)) |
180 | { | |
181 | // do nothing here to be able to see how transparent images are shown | |
182 | } | |
183 | ||
ab0f0386 VZ |
184 | void OnPaint(wxPaintEvent& WXUNUSED(event)) |
185 | { | |
05a8831a | 186 | wxPaintDC dc(this); |
8b6264b4 | 187 | |
9b2f7ad5 SC |
188 | #ifndef __WXOSX__ |
189 | // on OSX the immediate Update from within ClearBackground leads to a recursion | |
437950ce | 190 | if ( GetMenuBar()->IsChecked(ID_PAINT_BG) ) |
8b6264b4 | 191 | ClearBackground(); |
9b2f7ad5 | 192 | #endif |
8b6264b4 | 193 | |
a2bbedf0 VZ |
194 | dc.SetUserScale(m_zoom, m_zoom); |
195 | ||
05a8831a | 196 | const wxSize size = GetClientSize(); |
a2bbedf0 VZ |
197 | dc.DrawBitmap |
198 | ( | |
199 | m_bitmap, | |
200 | dc.DeviceToLogicalX((size.x - m_zoom*m_bitmap.GetWidth())/2), | |
201 | dc.DeviceToLogicalY((size.y - m_zoom*m_bitmap.GetHeight())/2), | |
202 | true /* use mask */ | |
203 | ); | |
ab0f0386 VZ |
204 | } |
205 | ||
1d8acb7d | 206 | void OnSave(wxCommandEvent& WXUNUSED(event)) |
1971d23c | 207 | { |
71307412 | 208 | #if wxUSE_FILEDLG |
368d59f0 | 209 | wxImage image = m_bitmap.ConvertToImage(); |
1971d23c | 210 | |
4693b20c | 211 | wxString savefilename = wxFileSelector( wxT("Save Image"), |
71307412 WS |
212 | wxEmptyString, |
213 | wxEmptyString, | |
4693b20c | 214 | (const wxChar *)NULL, |
d0ee33f5 | 215 | wxT("BMP files (*.bmp)|*.bmp|") |
c8688139 | 216 | #if wxUSE_LIBPNG |
d0ee33f5 | 217 | wxT("PNG files (*.png)|*.png|") |
c8688139 VZ |
218 | #endif |
219 | #if wxUSE_LIBJPEG | |
d0ee33f5 | 220 | wxT("JPEG files (*.jpg)|*.jpg|") |
c8688139 VZ |
221 | #endif |
222 | #if wxUSE_GIF | |
d0ee33f5 | 223 | wxT("GIF files (*.gif)|*.gif|") |
c8688139 VZ |
224 | #endif |
225 | #if wxUSE_LIBTIFF | |
d0ee33f5 | 226 | wxT("TIFF files (*.tif)|*.tif|") |
c8688139 VZ |
227 | #endif |
228 | #if wxUSE_PCX | |
d0ee33f5 | 229 | wxT("PCX files (*.pcx)|*.pcx|") |
c8688139 | 230 | #endif |
d0ee33f5 WS |
231 | wxT("ICO files (*.ico)|*.ico|") |
232 | wxT("CUR files (*.cur)|*.cur"), | |
ff3e84ff | 233 | wxFD_SAVE, |
47f797bd | 234 | this); |
1971d23c VZ |
235 | |
236 | if ( savefilename.empty() ) | |
237 | return; | |
238 | ||
5a566d89 VZ |
239 | wxString extension; |
240 | wxFileName::SplitPath(savefilename, NULL, NULL, &extension); | |
1971d23c | 241 | |
5a566d89 | 242 | bool saved = false; |
9a83f860 | 243 | if ( extension == wxT("bmp") ) |
5a566d89 VZ |
244 | { |
245 | static const int bppvalues[] = | |
246 | { | |
247 | wxBMP_1BPP, | |
248 | wxBMP_1BPP_BW, | |
249 | wxBMP_4BPP, | |
250 | wxBMP_8BPP, | |
251 | wxBMP_8BPP_GREY, | |
252 | wxBMP_8BPP_RED, | |
253 | wxBMP_8BPP_PALETTE, | |
254 | wxBMP_24BPP | |
255 | }; | |
256 | ||
24207dfc | 257 | const wxString bppchoices[] = |
5a566d89 | 258 | { |
9a83f860 VZ |
259 | wxT("1 bpp color"), |
260 | wxT("1 bpp B&W"), | |
261 | wxT("4 bpp color"), | |
262 | wxT("8 bpp color"), | |
263 | wxT("8 bpp greyscale"), | |
264 | wxT("8 bpp red"), | |
265 | wxT("8 bpp own palette"), | |
266 | wxT("24 bpp") | |
5a566d89 VZ |
267 | }; |
268 | ||
9a83f860 VZ |
269 | int bppselection = wxGetSingleChoiceIndex(wxT("Set BMP BPP"), |
270 | wxT("Image sample: save file"), | |
09ddabf7 FM |
271 | WXSIZEOF(bppchoices), |
272 | bppchoices, | |
273 | this); | |
5a566d89 VZ |
274 | if ( bppselection != -1 ) |
275 | { | |
276 | int format = bppvalues[bppselection]; | |
277 | image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, format); | |
278 | ||
279 | if ( format == wxBMP_8BPP_PALETTE ) | |
280 | { | |
281 | unsigned char *cmap = new unsigned char [256]; | |
282 | for ( int i = 0; i < 256; i++ ) | |
283 | cmap[i] = (unsigned char)i; | |
284 | image.SetPalette(wxPalette(256, cmap, cmap, cmap)); | |
285 | ||
d0ee33f5 | 286 | delete[] cmap; |
5a566d89 VZ |
287 | } |
288 | } | |
1971d23c | 289 | } |
c8688139 | 290 | #if wxUSE_LIBPNG |
9a83f860 | 291 | else if ( extension == wxT("png") ) |
5a566d89 VZ |
292 | { |
293 | static const int pngvalues[] = | |
294 | { | |
295 | wxPNG_TYPE_COLOUR, | |
296 | wxPNG_TYPE_COLOUR, | |
297 | wxPNG_TYPE_GREY, | |
298 | wxPNG_TYPE_GREY, | |
299 | wxPNG_TYPE_GREY_RED, | |
300 | wxPNG_TYPE_GREY_RED, | |
301 | }; | |
302 | ||
24207dfc | 303 | const wxString pngchoices[] = |
5a566d89 | 304 | { |
9a83f860 VZ |
305 | wxT("Colour 8bpp"), |
306 | wxT("Colour 16bpp"), | |
307 | wxT("Grey 8bpp"), | |
308 | wxT("Grey 16bpp"), | |
309 | wxT("Grey red 8bpp"), | |
310 | wxT("Grey red 16bpp"), | |
5a566d89 VZ |
311 | }; |
312 | ||
9a83f860 VZ |
313 | int sel = wxGetSingleChoiceIndex(wxT("Set PNG format"), |
314 | wxT("Image sample: save file"), | |
09ddabf7 FM |
315 | WXSIZEOF(pngchoices), |
316 | pngchoices, | |
317 | this); | |
5a566d89 VZ |
318 | if ( sel != -1 ) |
319 | { | |
320 | image.SetOption(wxIMAGE_OPTION_PNG_FORMAT, pngvalues[sel]); | |
321 | image.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH, sel % 2 ? 16 : 8); | |
d19ce8c4 FM |
322 | |
323 | // these values are taken from OptiPNG with -o3 switch | |
324 | const wxString compressionChoices[] = | |
325 | { | |
9a83f860 VZ |
326 | wxT("compression = 9, memory = 8, strategy = 0, filter = 0"), |
327 | wxT("compression = 9, memory = 9, strategy = 0, filter = 0"), | |
328 | wxT("compression = 9, memory = 8, strategy = 1, filter = 0"), | |
329 | wxT("compression = 9, memory = 9, strategy = 1, filter = 0"), | |
330 | wxT("compression = 1, memory = 8, strategy = 2, filter = 0"), | |
331 | wxT("compression = 1, memory = 9, strategy = 2, filter = 0"), | |
332 | wxT("compression = 9, memory = 8, strategy = 0, filter = 5"), | |
333 | wxT("compression = 9, memory = 9, strategy = 0, filter = 5"), | |
334 | wxT("compression = 9, memory = 8, strategy = 1, filter = 5"), | |
335 | wxT("compression = 9, memory = 9, strategy = 1, filter = 5"), | |
336 | wxT("compression = 1, memory = 8, strategy = 2, filter = 5"), | |
337 | wxT("compression = 1, memory = 9, strategy = 2, filter = 5"), | |
d19ce8c4 FM |
338 | }; |
339 | ||
9a83f860 VZ |
340 | int sel = wxGetSingleChoiceIndex(wxT("Select compression option (Cancel to use default)\n"), |
341 | wxT("PNG Compression Options"), | |
d19ce8c4 FM |
342 | WXSIZEOF(compressionChoices), |
343 | compressionChoices, | |
344 | this); | |
345 | if (sel != -1) | |
346 | { | |
347 | const int zc[] = {9, 9, 9, 9, 1, 1, 9, 9, 9, 9, 1, 1}; | |
348 | const int zm[] = {8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9}; | |
349 | const int zs[] = {0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2}; | |
350 | const int f[] = {0x08, 0x08, 0x08, 0x08, 0x08, 0x08, | |
351 | 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8}; | |
352 | ||
353 | image.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL , zc[sel]); | |
354 | image.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL , zm[sel]); | |
355 | image.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY , zs[sel]); | |
356 | image.SetOption(wxIMAGE_OPTION_PNG_FILTER , f[sel]); | |
357 | image.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE, 1048576); // 1 MB | |
358 | } | |
5a566d89 VZ |
359 | } |
360 | } | |
c8688139 | 361 | #endif // wxUSE_LIBPNG |
9a83f860 | 362 | else if ( extension == wxT("cur") ) |
45647dcf | 363 | { |
51b07644 VZ |
364 | image.Rescale(32,32); |
365 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0); | |
45647dcf VS |
366 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0); |
367 | // This shows how you can save an image with explicitly | |
368 | // specified image format: | |
5a566d89 | 369 | saved = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR); |
45647dcf | 370 | } |
5a566d89 VZ |
371 | |
372 | if ( !saved ) | |
45647dcf VS |
373 | { |
374 | // This one guesses image format from filename extension | |
375 | // (it may fail if the extension is not recognized): | |
5a566d89 | 376 | image.SaveFile(savefilename); |
45647dcf | 377 | } |
71307412 | 378 | #endif // wxUSE_FILEDLG |
1971d23c VZ |
379 | } |
380 | ||
05a8831a VZ |
381 | void OnResize(wxCommandEvent& WXUNUSED(event)) |
382 | { | |
383 | wxImage img(m_bitmap.ConvertToImage()); | |
384 | ||
385 | const wxSize size = GetClientSize(); | |
386 | img.Rescale(size.x, size.y, wxIMAGE_QUALITY_HIGH); | |
387 | m_bitmap = wxBitmap(img); | |
388 | ||
389 | UpdateStatusBar(); | |
a2bbedf0 VZ |
390 | } |
391 | ||
392 | void OnZoom(wxCommandEvent& event) | |
393 | { | |
394 | if ( event.GetId() == wxID_ZOOM_IN ) | |
395 | m_zoom *= 1.2; | |
396 | else if ( event.GetId() == wxID_ZOOM_OUT ) | |
397 | m_zoom /= 1.2; | |
398 | else // wxID_ZOOM_100 | |
399 | m_zoom = 1.; | |
400 | ||
401 | UpdateStatusBar(); | |
05a8831a VZ |
402 | } |
403 | ||
1d8acb7d VZ |
404 | void OnRotate(wxCommandEvent& event) |
405 | { | |
406 | double angle = 5; | |
407 | if ( event.GetId() == ID_ROTATE_LEFT ) | |
408 | angle = -angle; | |
409 | ||
410 | wxImage img(m_bitmap.ConvertToImage()); | |
05a8831a | 411 | img = img.Rotate(angle, wxPoint(img.GetWidth() / 2, img.GetHeight() / 2)); |
1d8acb7d VZ |
412 | if ( !img.Ok() ) |
413 | { | |
9a83f860 | 414 | wxLogWarning(wxT("Rotation failed")); |
1d8acb7d VZ |
415 | return; |
416 | } | |
417 | ||
418 | m_bitmap = wxBitmap(img); | |
05a8831a VZ |
419 | |
420 | UpdateStatusBar(); | |
1d8acb7d VZ |
421 | } |
422 | ||
05a8831a VZ |
423 | void UpdateStatusBar() |
424 | { | |
9a83f860 | 425 | wxLogStatus(this, wxT("Image size: (%d, %d), zoom %.2f"), |
05a8831a | 426 | m_bitmap.GetWidth(), |
a2bbedf0 VZ |
427 | m_bitmap.GetHeight(), |
428 | m_zoom); | |
429 | Refresh(); | |
05a8831a VZ |
430 | } |
431 | ||
ab0f0386 | 432 | wxBitmap m_bitmap; |
a2bbedf0 | 433 | double m_zoom; |
ab0f0386 VZ |
434 | |
435 | DECLARE_EVENT_TABLE() | |
436 | }; | |
437 | ||
b3f04dc2 VZ |
438 | #ifdef wxHAVE_RAW_BITMAP |
439 | ||
440 | #include "wx/rawbmp.h" | |
441 | ||
442 | class MyRawBitmapFrame : public wxFrame | |
443 | { | |
444 | public: | |
445 | enum | |
446 | { | |
447 | BORDER = 15, | |
448 | SIZE = 150, | |
449 | REAL_SIZE = SIZE - 2*BORDER | |
450 | }; | |
451 | ||
452 | MyRawBitmapFrame(wxFrame *parent) | |
9a83f860 | 453 | : wxFrame(parent, wxID_ANY, wxT("Raw bitmaps (how exciting)")), |
09ddabf7 FM |
454 | m_bitmap(SIZE, SIZE, 24), |
455 | m_alphaBitmap(SIZE, SIZE, 32) | |
b3f04dc2 | 456 | { |
8ef08948 | 457 | SetClientSize(SIZE, SIZE*2+25); |
b3f04dc2 | 458 | |
8ef08948 RD |
459 | InitAlphaBitmap(); |
460 | InitBitmap(); | |
66df4ec6 | 461 | |
8ef08948 RD |
462 | } |
463 | ||
464 | void InitAlphaBitmap() | |
465 | { | |
6e5551ad RR |
466 | // First, clear the whole bitmap by making it alpha |
467 | { | |
8ef08948 | 468 | wxAlphaPixelData data( m_alphaBitmap, wxPoint(0,0), wxSize(SIZE, SIZE) ); |
6e5551ad RR |
469 | if ( !data ) |
470 | { | |
9a83f860 | 471 | wxLogError(wxT("Failed to gain raw access to bitmap data")); |
6e5551ad RR |
472 | return; |
473 | } | |
8ef08948 | 474 | wxAlphaPixelData::Iterator p(data); |
6e5551ad RR |
475 | for ( int y = 0; y < SIZE; ++y ) |
476 | { | |
8ef08948 | 477 | wxAlphaPixelData::Iterator rowStart = p; |
6e5551ad RR |
478 | for ( int x = 0; x < SIZE; ++x ) |
479 | { | |
480 | p.Alpha() = 0; | |
481 | ++p; // same as p.OffsetX(1) | |
482 | } | |
483 | p = rowStart; | |
484 | p.OffsetY(data, 1); | |
485 | } | |
486 | } | |
095980e1 | 487 | |
6e5551ad | 488 | // Then, draw colourful alpha-blended stripes |
8ef08948 | 489 | wxAlphaPixelData data(m_alphaBitmap, wxPoint(BORDER, BORDER), |
09ddabf7 | 490 | wxSize(REAL_SIZE, REAL_SIZE)); |
b3f04dc2 VZ |
491 | if ( !data ) |
492 | { | |
9a83f860 | 493 | wxLogError(wxT("Failed to gain raw access to bitmap data")); |
b3f04dc2 VZ |
494 | return; |
495 | } | |
496 | ||
8ef08948 | 497 | wxAlphaPixelData::Iterator p(data); |
b3f04dc2 | 498 | |
b3f04dc2 VZ |
499 | for ( int y = 0; y < REAL_SIZE; ++y ) |
500 | { | |
8ef08948 | 501 | wxAlphaPixelData::Iterator rowStart = p; |
b3f04dc2 VZ |
502 | |
503 | int r = y < REAL_SIZE/3 ? 255 : 0, | |
504 | g = (REAL_SIZE/3 <= y) && (y < 2*(REAL_SIZE/3)) ? 255 : 0, | |
505 | b = 2*(REAL_SIZE/3) <= y ? 255 : 0; | |
506 | ||
507 | for ( int x = 0; x < REAL_SIZE; ++x ) | |
508 | { | |
b068dfe2 | 509 | // note that RGB must be premultiplied by alpha |
8ef08948 | 510 | unsigned a = (wxAlphaPixelData::Iterator::ChannelType)((x*255.)/REAL_SIZE); |
5c99abbe MW |
511 | p.Red() = r * a / 256; |
512 | p.Green() = g * a / 256; | |
513 | p.Blue() = b * a / 256; | |
b068dfe2 | 514 | p.Alpha() = a; |
b3f04dc2 VZ |
515 | |
516 | ++p; // same as p.OffsetX(1) | |
517 | } | |
518 | ||
519 | p = rowStart; | |
1d2f48b6 | 520 | p.OffsetY(data, 1); |
b3f04dc2 VZ |
521 | } |
522 | } | |
523 | ||
8ef08948 RD |
524 | void InitBitmap() |
525 | { | |
526 | // draw some colourful stripes without alpha | |
527 | wxNativePixelData data(m_bitmap); | |
528 | if ( !data ) | |
529 | { | |
9a83f860 | 530 | wxLogError(wxT("Failed to gain raw access to bitmap data")); |
8ef08948 RD |
531 | return; |
532 | } | |
533 | ||
534 | wxNativePixelData::Iterator p(data); | |
535 | for ( int y = 0; y < SIZE; ++y ) | |
536 | { | |
537 | wxNativePixelData::Iterator rowStart = p; | |
538 | ||
539 | int r = y < SIZE/3 ? 255 : 0, | |
540 | g = (SIZE/3 <= y) && (y < 2*(SIZE/3)) ? 255 : 0, | |
541 | b = 2*(SIZE/3) <= y ? 255 : 0; | |
542 | ||
543 | for ( int x = 0; x < SIZE; ++x ) | |
544 | { | |
545 | p.Red() = r; | |
546 | p.Green() = g; | |
547 | p.Blue() = b; | |
548 | ++p; // same as p.OffsetX(1) | |
549 | } | |
550 | ||
551 | p = rowStart; | |
552 | p.OffsetY(data, 1); | |
553 | } | |
554 | } | |
555 | ||
b3f04dc2 VZ |
556 | void OnPaint(wxPaintEvent& WXUNUSED(event)) |
557 | { | |
558 | wxPaintDC dc( this ); | |
9a83f860 VZ |
559 | dc.DrawText(wxT("This is alpha and raw bitmap test"), 0, BORDER); |
560 | dc.DrawText(wxT("This is alpha and raw bitmap test"), 0, SIZE/2 - BORDER); | |
561 | dc.DrawText(wxT("This is alpha and raw bitmap test"), 0, SIZE - 2*BORDER); | |
8ef08948 RD |
562 | dc.DrawBitmap( m_alphaBitmap, 0, 0, true /* use mask */ ); |
563 | ||
9a83f860 | 564 | dc.DrawText(wxT("Raw bitmap access without alpha"), 0, SIZE+5); |
8ef08948 | 565 | dc.DrawBitmap( m_bitmap, 0, SIZE+5+dc.GetCharHeight()); |
b3f04dc2 VZ |
566 | } |
567 | ||
568 | private: | |
569 | wxBitmap m_bitmap; | |
8ef08948 | 570 | wxBitmap m_alphaBitmap; |
b3f04dc2 VZ |
571 | |
572 | DECLARE_EVENT_TABLE() | |
573 | }; | |
574 | ||
575 | #endif // wxHAVE_RAW_BITMAP | |
576 | ||
01111366 | 577 | |
09ddabf7 FM |
578 | // ============================================================================ |
579 | // implementations | |
580 | // ============================================================================ | |
01111366 | 581 | |
09ddabf7 FM |
582 | //----------------------------------------------------------------------------- |
583 | // MyImageFrame | |
584 | //----------------------------------------------------------------------------- | |
01111366 | 585 | |
ab0f0386 | 586 | BEGIN_EVENT_TABLE(MyImageFrame, wxFrame) |
0c543b7a VZ |
587 | EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground) |
588 | EVT_PAINT(MyImageFrame::OnPaint) | |
1d8acb7d VZ |
589 | |
590 | EVT_MENU(wxID_SAVE, MyImageFrame::OnSave) | |
591 | EVT_MENU_RANGE(ID_ROTATE_LEFT, ID_ROTATE_RIGHT, MyImageFrame::OnRotate) | |
05a8831a | 592 | EVT_MENU(ID_RESIZE, MyImageFrame::OnResize) |
a2bbedf0 VZ |
593 | |
594 | EVT_MENU(wxID_ZOOM_IN, MyImageFrame::OnZoom) | |
595 | EVT_MENU(wxID_ZOOM_OUT, MyImageFrame::OnZoom) | |
596 | EVT_MENU(wxID_ZOOM_100, MyImageFrame::OnZoom) | |
ab0f0386 VZ |
597 | END_EVENT_TABLE() |
598 | ||
09ddabf7 FM |
599 | //----------------------------------------------------------------------------- |
600 | // MyRawBitmapFrame | |
601 | //----------------------------------------------------------------------------- | |
602 | ||
b3f04dc2 VZ |
603 | #ifdef wxHAVE_RAW_BITMAP |
604 | ||
605 | BEGIN_EVENT_TABLE(MyRawBitmapFrame, wxFrame) | |
606 | EVT_PAINT(MyRawBitmapFrame::OnPaint) | |
607 | END_EVENT_TABLE() | |
608 | ||
609 | #endif // wxHAVE_RAW_BITMAP | |
610 | ||
09ddabf7 | 611 | //----------------------------------------------------------------------------- |
01111366 | 612 | // MyFrame |
09ddabf7 | 613 | //----------------------------------------------------------------------------- |
01111366 | 614 | |
b3f04dc2 VZ |
615 | enum |
616 | { | |
91b07357 JS |
617 | ID_QUIT = wxID_EXIT, |
618 | ID_ABOUT = wxID_ABOUT, | |
619 | ID_NEW = 100, | |
37ba70a5 | 620 | ID_INFO, |
95706ac1 VZ |
621 | ID_SHOWRAW, |
622 | ID_SHOWTHUMBNAIL | |
b3f04dc2 | 623 | }; |
01111366 RR |
624 | |
625 | IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) | |
37ba70a5 | 626 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
09ddabf7 FM |
627 | EVT_MENU (ID_ABOUT, MyFrame::OnAbout) |
628 | EVT_MENU (ID_QUIT, MyFrame::OnQuit) | |
629 | EVT_MENU (ID_NEW, MyFrame::OnNewFrame) | |
630 | EVT_MENU (ID_INFO, MyFrame::OnImageInfo) | |
631 | EVT_MENU (ID_SHOWTHUMBNAIL, MyFrame::OnThumbnail) | |
b3f04dc2 | 632 | #ifdef wxHAVE_RAW_BITMAP |
09ddabf7 | 633 | EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap) |
b3f04dc2 | 634 | #endif |
dd38c875 | 635 | #if wxUSE_CLIPBOARD |
da9df1f5 VZ |
636 | EVT_MENU(wxID_COPY, MyFrame::OnCopy) |
637 | EVT_MENU(wxID_PASTE, MyFrame::OnPaste) | |
638 | #endif // wxUSE_CLIPBOARD | |
01111366 RR |
639 | END_EVENT_TABLE() |
640 | ||
1d5b7a0b | 641 | MyFrame::MyFrame() |
9a83f860 | 642 | : wxFrame( (wxFrame *)NULL, wxID_ANY, wxT("wxImage sample"), |
09ddabf7 | 643 | wxPoint(20, 20), wxSize(950, 700) ) |
01111366 | 644 | { |
41f02b9a FM |
645 | SetIcon(wxICON(sample)); |
646 | ||
09ddabf7 | 647 | wxMenuBar *menu_bar = new wxMenuBar(); |
da9df1f5 | 648 | |
09ddabf7 | 649 | wxMenu *menuImage = new wxMenu; |
9a83f860 VZ |
650 | menuImage->Append( ID_NEW, wxT("&Show any image...\tCtrl-O")); |
651 | menuImage->Append( ID_INFO, wxT("Show image &information...\tCtrl-I")); | |
b3f04dc2 | 652 | #ifdef wxHAVE_RAW_BITMAP |
09ddabf7 | 653 | menuImage->AppendSeparator(); |
9a83f860 | 654 | menuImage->Append( ID_SHOWRAW, wxT("Test &raw bitmap...\tCtrl-R")); |
b3f04dc2 | 655 | #endif |
09ddabf7 | 656 | menuImage->AppendSeparator(); |
9a83f860 | 657 | menuImage->Append( ID_SHOWTHUMBNAIL, wxT("Test &thumbnail...\tCtrl-T"), |
09ddabf7 FM |
658 | "Test scaling the image during load (try with JPEG)"); |
659 | menuImage->AppendSeparator(); | |
9a83f860 | 660 | menuImage->Append( ID_ABOUT, wxT("&About...")); |
09ddabf7 | 661 | menuImage->AppendSeparator(); |
9a83f860 VZ |
662 | menuImage->Append( ID_QUIT, wxT("E&xit\tCtrl-Q")); |
663 | menu_bar->Append(menuImage, wxT("&Image")); | |
da9df1f5 | 664 | |
dd38c875 | 665 | #if wxUSE_CLIPBOARD |
09ddabf7 | 666 | wxMenu *menuClipboard = new wxMenu; |
9a83f860 VZ |
667 | menuClipboard->Append(wxID_COPY, wxT("&Copy test image\tCtrl-C")); |
668 | menuClipboard->Append(wxID_PASTE, wxT("&Paste image\tCtrl-V")); | |
669 | menu_bar->Append(menuClipboard, wxT("&Clipboard")); | |
da9df1f5 | 670 | #endif // wxUSE_CLIPBOARD |
3d05544e | 671 | |
09ddabf7 | 672 | SetMenuBar( menu_bar ); |
1d5b7a0b | 673 | |
8520f137 | 674 | #if wxUSE_STATUSBAR |
09ddabf7 FM |
675 | CreateStatusBar(2); |
676 | int widths[] = { -1, 100 }; | |
677 | SetStatusWidths( 2, widths ); | |
8520f137 | 678 | #endif // wxUSE_STATUSBAR |
1d5b7a0b | 679 | |
09ddabf7 | 680 | m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) ); |
cbd4be25 | 681 | |
09ddabf7 FM |
682 | // 500 width * 2750 height |
683 | m_canvas->SetScrollbars( 10, 10, 50, 275 ); | |
01111366 RR |
684 | } |
685 | ||
686 | void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) | |
687 | { | |
09ddabf7 | 688 | Close( true ); |
01111366 RR |
689 | } |
690 | ||
691 | void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) | |
692 | { | |
a2bbedf0 VZ |
693 | (void)wxMessageBox( "wxImage demo\n" |
694 | "(c) Robert Roebling 1998-2005" | |
695 | "(c) Vadim Zeitlin 2005-2009", | |
696 | "About wxImage Demo", | |
697 | wxICON_INFORMATION | wxOK ); | |
fb1585ae RR |
698 | } |
699 | ||
37ba70a5 | 700 | wxString MyFrame::LoadUserImage(wxImage& image) |
ab0f0386 | 701 | { |
37ba70a5 VZ |
702 | wxString filename; |
703 | ||
71307412 | 704 | #if wxUSE_FILEDLG |
9a83f860 | 705 | filename = wxLoadFileSelector(wxT("image"), wxEmptyString); |
37ba70a5 VZ |
706 | if ( !filename.empty() ) |
707 | { | |
708 | if ( !image.LoadFile(filename) ) | |
709 | { | |
9a83f860 | 710 | wxLogError(wxT("Couldn't load image from '%s'."), filename.c_str()); |
f6bcfd97 | 711 | |
37ba70a5 VZ |
712 | return wxEmptyString; |
713 | } | |
714 | } | |
715 | #endif // wxUSE_FILEDLG | |
716 | ||
717 | return filename; | |
718 | } | |
719 | ||
720 | void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) ) | |
721 | { | |
f6bcfd97 | 722 | wxImage image; |
37ba70a5 VZ |
723 | wxString filename = LoadUserImage(image); |
724 | if ( !filename.empty() ) | |
85fcb94f | 725 | new MyImageFrame(this, filename, image); |
37ba70a5 VZ |
726 | } |
727 | ||
728 | void MyFrame::OnImageInfo( wxCommandEvent &WXUNUSED(event) ) | |
729 | { | |
730 | wxImage image; | |
731 | if ( !LoadUserImage(image).empty() ) | |
f6bcfd97 | 732 | { |
37ba70a5 VZ |
733 | // TODO: show more information about the file |
734 | wxString info = wxString::Format("Image size: %dx%d", | |
09ddabf7 FM |
735 | image.GetWidth(), |
736 | image.GetHeight()); | |
37ba70a5 VZ |
737 | |
738 | int xres = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX), | |
739 | yres = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY); | |
740 | if ( xres || yres ) | |
741 | { | |
742 | info += wxString::Format("\nResolution: %dx%d", xres, yres); | |
743 | switch ( image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT) ) | |
744 | { | |
745 | default: | |
746 | wxFAIL_MSG( "unknown image resolution units" ); | |
747 | // fall through | |
f6bcfd97 | 748 | |
37ba70a5 VZ |
749 | case wxIMAGE_RESOLUTION_NONE: |
750 | info += " in default units"; | |
751 | break; | |
f6bcfd97 | 752 | |
37ba70a5 VZ |
753 | case wxIMAGE_RESOLUTION_INCHES: |
754 | info += " in"; | |
755 | break; | |
756 | ||
757 | case wxIMAGE_RESOLUTION_CM: | |
758 | info += " cm"; | |
759 | break; | |
760 | } | |
761 | } | |
762 | ||
763 | wxLogMessage("%s", info); | |
764 | } | |
ab0f0386 VZ |
765 | } |
766 | ||
b3f04dc2 VZ |
767 | #ifdef wxHAVE_RAW_BITMAP |
768 | ||
481721e8 | 769 | void MyFrame::OnTestRawBitmap( wxCommandEvent &WXUNUSED(event) ) |
b3f04dc2 VZ |
770 | { |
771 | (new MyRawBitmapFrame(this))->Show(); | |
772 | } | |
773 | ||
774 | #endif // wxHAVE_RAW_BITMAP | |
775 | ||
dd38c875 | 776 | #if wxUSE_CLIPBOARD |
da9df1f5 VZ |
777 | |
778 | void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
779 | { | |
780 | wxBitmapDataObject *dobjBmp = new wxBitmapDataObject; | |
bd981f27 | 781 | dobjBmp->SetBitmap(m_canvas->my_horse_png); |
da9df1f5 | 782 | |
dd38c875 MB |
783 | wxTheClipboard->Open(); |
784 | ||
da9df1f5 VZ |
785 | if ( !wxTheClipboard->SetData(dobjBmp) ) |
786 | { | |
9a83f860 | 787 | wxLogError(wxT("Failed to copy bitmap to clipboard")); |
da9df1f5 | 788 | } |
dd38c875 MB |
789 | |
790 | wxTheClipboard->Close(); | |
da9df1f5 VZ |
791 | } |
792 | ||
793 | void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
794 | { | |
795 | wxBitmapDataObject dobjBmp; | |
dd38c875 MB |
796 | |
797 | wxTheClipboard->Open(); | |
da9df1f5 VZ |
798 | if ( !wxTheClipboard->GetData(dobjBmp) ) |
799 | { | |
9a83f860 | 800 | wxLogMessage(wxT("No bitmap data in the clipboard")); |
da9df1f5 VZ |
801 | } |
802 | else | |
803 | { | |
9a83f860 | 804 | new MyImageFrame(this, wxT("Clipboard"), dobjBmp.GetBitmap()); |
da9df1f5 | 805 | } |
dd38c875 | 806 | wxTheClipboard->Close(); |
da9df1f5 VZ |
807 | } |
808 | ||
809 | #endif // wxUSE_CLIPBOARD | |
810 | ||
95706ac1 VZ |
811 | void MyFrame::OnThumbnail( wxCommandEvent &WXUNUSED(event) ) |
812 | { | |
813 | #if wxUSE_FILEDLG | |
9a83f860 | 814 | wxString filename = wxLoadFileSelector(wxT("image"), wxEmptyString, wxEmptyString, this); |
95706ac1 VZ |
815 | if ( filename.empty() ) |
816 | return; | |
817 | ||
818 | static const int THUMBNAIL_WIDTH = 320; | |
819 | static const int THUMBNAIL_HEIGHT = 240; | |
820 | ||
821 | wxImage image; | |
822 | image.SetOption(wxIMAGE_OPTION_MAX_WIDTH, THUMBNAIL_WIDTH); | |
823 | image.SetOption(wxIMAGE_OPTION_MAX_HEIGHT, THUMBNAIL_HEIGHT); | |
824 | ||
825 | wxStopWatch sw; | |
826 | if ( !image.LoadFile(filename) ) | |
827 | { | |
9a83f860 | 828 | wxLogError(wxT("Couldn't load image from '%s'."), filename.c_str()); |
95706ac1 VZ |
829 | return; |
830 | } | |
831 | ||
832 | const long loadTime = sw.Time(); | |
833 | ||
85fcb94f | 834 | MyImageFrame * const frame = new MyImageFrame(this, filename, image); |
95706ac1 VZ |
835 | wxLogStatus(frame, "Loaded \"%s\" in %ldms", filename, loadTime); |
836 | #else | |
9a83f860 | 837 | wxLogError( wxT("Couldn't create file selector dialog") ); |
95706ac1 VZ |
838 | return; |
839 | #endif // wxUSE_FILEDLG | |
840 | } | |
841 | ||
09ddabf7 FM |
842 | //----------------------------------------------------------------------------- |
843 | // MyApp | |
844 | //----------------------------------------------------------------------------- | |
845 | ||
846 | IMPLEMENT_APP(MyApp) | |
847 | ||
848 | bool MyApp::OnInit() | |
849 | { | |
850 | if ( !wxApp::OnInit() ) | |
851 | return false; | |
852 | ||
853 | wxInitAllImageHandlers(); | |
854 | ||
855 | wxFrame *frame = new MyFrame(); | |
856 | frame->Show( true ); | |
857 | ||
858 | return true; | |
859 | } |