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