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