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