]> git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
Remove wxRTTI macro for wxComboBox from Motif port.
[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 wxHAS_IMAGES_IN_RESOURCES
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_SAVEAS);
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 wxEmptyString,
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 #if wxUSE_XPM
235 wxT("X PixMap files (*.xpm)|*.xpm|")
236 #endif
237 wxT("ICO files (*.ico)|*.ico|")
238 wxT("CUR files (*.cur)|*.cur"),
239 wxFD_SAVE | wxFD_OVERWRITE_PROMPT,
240 this);
241
242 if ( savefilename.empty() )
243 return;
244
245 wxString extension;
246 wxFileName::SplitPath(savefilename, NULL, NULL, &extension);
247
248 bool saved = false;
249 if ( extension == wxT("bmp") )
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
263 const wxString bppchoices[] =
264 {
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")
273 };
274
275 int bppselection = wxGetSingleChoiceIndex(wxT("Set BMP BPP"),
276 wxT("Image sample: save file"),
277 WXSIZEOF(bppchoices),
278 bppchoices,
279 this);
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
292 delete[] cmap;
293 }
294 }
295 }
296 #if wxUSE_LIBPNG
297 else if ( extension == wxT("png") )
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
309 const wxString pngchoices[] =
310 {
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"),
317 };
318
319 int sel = wxGetSingleChoiceIndex(wxT("Set PNG format"),
320 wxT("Image sample: save file"),
321 WXSIZEOF(pngchoices),
322 pngchoices,
323 this);
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);
328
329 // these values are taken from OptiPNG with -o3 switch
330 const wxString compressionChoices[] =
331 {
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"),
344 };
345
346 int sel = wxGetSingleChoiceIndex(wxT("Select compression option (Cancel to use default)\n"),
347 wxT("PNG Compression Options"),
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 }
365 }
366 }
367 #endif // wxUSE_LIBPNG
368 else if ( extension == wxT("cur") )
369 {
370 image.Rescale(32,32);
371 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0);
372 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0);
373 // This shows how you can save an image with explicitly
374 // specified image format:
375 saved = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR);
376 }
377
378 if ( !saved )
379 {
380 // This one guesses image format from filename extension
381 // (it may fail if the extension is not recognized):
382 image.SaveFile(savefilename);
383 }
384 #endif // wxUSE_FILEDLG
385 }
386
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();
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();
408 }
409
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());
417 img = img.Rotate(angle, wxPoint(img.GetWidth() / 2, img.GetHeight() / 2));
418 if ( !img.IsOk() )
419 {
420 wxLogWarning(wxT("Rotation failed"));
421 return;
422 }
423
424 m_bitmap = wxBitmap(img);
425
426 UpdateStatusBar();
427 }
428
429 void UpdateStatusBar()
430 {
431 wxLogStatus(this, wxT("Image size: (%d, %d), zoom %.2f"),
432 m_bitmap.GetWidth(),
433 m_bitmap.GetHeight(),
434 m_zoom);
435 Refresh();
436 }
437
438 wxBitmap m_bitmap;
439 double m_zoom;
440
441 DECLARE_EVENT_TABLE()
442 };
443
444 #ifdef wxHAVE_RAW_BITMAP
445
446 #include "wx/rawbmp.h"
447
448 class MyRawBitmapFrame : public wxFrame
449 {
450 public:
451 enum
452 {
453 BORDER = 15,
454 SIZE = 150,
455 REAL_SIZE = SIZE - 2*BORDER
456 };
457
458 MyRawBitmapFrame(wxFrame *parent)
459 : wxFrame(parent, wxID_ANY, wxT("Raw bitmaps (how exciting)")),
460 m_bitmap(SIZE, SIZE, 24),
461 m_alphaBitmap(SIZE, SIZE, 32)
462 {
463 SetClientSize(SIZE, SIZE*2+25);
464
465 InitAlphaBitmap();
466 InitBitmap();
467
468 }
469
470 void InitAlphaBitmap()
471 {
472 // First, clear the whole bitmap by making it alpha
473 {
474 wxAlphaPixelData data( m_alphaBitmap, wxPoint(0,0), wxSize(SIZE, SIZE) );
475 if ( !data )
476 {
477 wxLogError(wxT("Failed to gain raw access to bitmap data"));
478 return;
479 }
480 wxAlphaPixelData::Iterator p(data);
481 for ( int y = 0; y < SIZE; ++y )
482 {
483 wxAlphaPixelData::Iterator rowStart = p;
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 }
493
494 // Then, draw colourful alpha-blended stripes
495 wxAlphaPixelData data(m_alphaBitmap, wxPoint(BORDER, BORDER),
496 wxSize(REAL_SIZE, REAL_SIZE));
497 if ( !data )
498 {
499 wxLogError(wxT("Failed to gain raw access to bitmap data"));
500 return;
501 }
502
503 wxAlphaPixelData::Iterator p(data);
504
505 for ( int y = 0; y < REAL_SIZE; ++y )
506 {
507 wxAlphaPixelData::Iterator rowStart = p;
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 {
515 // note that RGB must be premultiplied by alpha
516 unsigned a = (wxAlphaPixelData::Iterator::ChannelType)((x*255.)/REAL_SIZE);
517 p.Red() = r * a / 256;
518 p.Green() = g * a / 256;
519 p.Blue() = b * a / 256;
520 p.Alpha() = a;
521
522 ++p; // same as p.OffsetX(1)
523 }
524
525 p = rowStart;
526 p.OffsetY(data, 1);
527 }
528 }
529
530 void InitBitmap()
531 {
532 // draw some colourful stripes without alpha
533 wxNativePixelData data(m_bitmap);
534 if ( !data )
535 {
536 wxLogError(wxT("Failed to gain raw access to bitmap data"));
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
562 void OnPaint(wxPaintEvent& WXUNUSED(event))
563 {
564 wxPaintDC dc( this );
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);
568 dc.DrawBitmap( m_alphaBitmap, 0, 0, true /* use mask */ );
569
570 dc.DrawText(wxT("Raw bitmap access without alpha"), 0, SIZE+5);
571 dc.DrawBitmap( m_bitmap, 0, SIZE+5+dc.GetCharHeight());
572 }
573
574 private:
575 wxBitmap m_bitmap;
576 wxBitmap m_alphaBitmap;
577
578 DECLARE_EVENT_TABLE()
579 };
580
581 #endif // wxHAVE_RAW_BITMAP
582
583
584 // ============================================================================
585 // implementations
586 // ============================================================================
587
588 //-----------------------------------------------------------------------------
589 // MyImageFrame
590 //-----------------------------------------------------------------------------
591
592 BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
593 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground)
594 EVT_PAINT(MyImageFrame::OnPaint)
595
596 EVT_MENU(wxID_SAVEAS, MyImageFrame::OnSave)
597 EVT_MENU_RANGE(ID_ROTATE_LEFT, ID_ROTATE_RIGHT, MyImageFrame::OnRotate)
598 EVT_MENU(ID_RESIZE, MyImageFrame::OnResize)
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)
603 END_EVENT_TABLE()
604
605 //-----------------------------------------------------------------------------
606 // MyRawBitmapFrame
607 //-----------------------------------------------------------------------------
608
609 #ifdef wxHAVE_RAW_BITMAP
610
611 BEGIN_EVENT_TABLE(MyRawBitmapFrame, wxFrame)
612 EVT_PAINT(MyRawBitmapFrame::OnPaint)
613 END_EVENT_TABLE()
614
615 #endif // wxHAVE_RAW_BITMAP
616
617 //-----------------------------------------------------------------------------
618 // MyFrame
619 //-----------------------------------------------------------------------------
620
621 enum
622 {
623 ID_QUIT = wxID_EXIT,
624 ID_ABOUT = wxID_ABOUT,
625 ID_NEW = 100,
626 ID_INFO,
627 ID_SHOWRAW,
628 ID_GRAPHICS,
629 ID_SHOWTHUMBNAIL
630 };
631
632 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
633 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
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)
639 #ifdef wxHAVE_RAW_BITMAP
640 EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap)
641 #endif
642 #if wxUSE_GRAPHICS_CONTEXT
643 EVT_MENU (ID_GRAPHICS, MyFrame::OnTestGraphics)
644 #endif // wxUSE_GRAPHICS_CONTEXT
645 #if wxUSE_CLIPBOARD
646 EVT_MENU(wxID_COPY, MyFrame::OnCopy)
647 EVT_MENU(wxID_PASTE, MyFrame::OnPaste)
648 #endif // wxUSE_CLIPBOARD
649 END_EVENT_TABLE()
650
651 MyFrame::MyFrame()
652 : wxFrame( (wxFrame *)NULL, wxID_ANY, wxT("wxImage sample"),
653 wxPoint(20, 20), wxSize(950, 700) )
654 {
655 SetIcon(wxICON(sample));
656
657 wxMenuBar *menu_bar = new wxMenuBar();
658
659 wxMenu *menuImage = new wxMenu;
660 menuImage->Append( ID_NEW, wxT("&Show any image...\tCtrl-O"));
661 menuImage->Append( ID_INFO, wxT("Show image &information...\tCtrl-I"));
662 #ifdef wxHAVE_RAW_BITMAP
663 menuImage->AppendSeparator();
664 menuImage->Append( ID_SHOWRAW, wxT("Test &raw bitmap...\tCtrl-R"));
665 #endif
666 #if wxUSE_GRAPHICS_CONTEXT
667 menuImage->AppendSeparator();
668 menuImage->Append(ID_GRAPHICS, "Test &graphics context...\tCtrl-G");
669 #endif // wxUSE_GRAPHICS_CONTEXT
670 menuImage->AppendSeparator();
671 menuImage->Append( ID_SHOWTHUMBNAIL, wxT("Test &thumbnail...\tCtrl-T"),
672 "Test scaling the image during load (try with JPEG)");
673 menuImage->AppendSeparator();
674 menuImage->Append( ID_ABOUT, wxT("&About\tF1"));
675 menuImage->AppendSeparator();
676 menuImage->Append( ID_QUIT, wxT("E&xit\tCtrl-Q"));
677 menu_bar->Append(menuImage, wxT("&Image"));
678
679 #if wxUSE_CLIPBOARD
680 wxMenu *menuClipboard = new wxMenu;
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"));
684 #endif // wxUSE_CLIPBOARD
685
686 SetMenuBar( menu_bar );
687
688 #if wxUSE_STATUSBAR
689 CreateStatusBar(2);
690 int widths[] = { -1, 100 };
691 SetStatusWidths( 2, widths );
692 #endif // wxUSE_STATUSBAR
693
694 m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) );
695
696 // 500 width * 2750 height
697 m_canvas->SetScrollbars( 10, 10, 50, 275 );
698 m_canvas->SetCursor(wxImage("cursor.png"));
699 }
700
701 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
702 {
703 Close( true );
704 }
705
706 #if wxUSE_ZLIB && wxUSE_STREAMS
707 #include "wx/zstream.h"
708 #endif
709
710 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
711 {
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'),
735 "About wxImage Demo",
736 wxICON_INFORMATION | wxOK );
737 }
738
739 wxString MyFrame::LoadUserImage(wxImage& image)
740 {
741 wxString filename;
742
743 #if wxUSE_FILEDLG
744 filename = wxLoadFileSelector(wxT("image"), wxEmptyString);
745 if ( !filename.empty() )
746 {
747 if ( !image.LoadFile(filename) )
748 {
749 wxLogError(wxT("Couldn't load image from '%s'."), filename.c_str());
750
751 return wxEmptyString;
752 }
753 }
754 #endif // wxUSE_FILEDLG
755
756 return filename;
757 }
758
759 void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
760 {
761 wxImage image;
762 wxString filename = LoadUserImage(image);
763 if ( !filename.empty() )
764 new MyImageFrame(this, filename, image);
765 }
766
767 void MyFrame::OnImageInfo( wxCommandEvent &WXUNUSED(event) )
768 {
769 wxImage image;
770 if ( !LoadUserImage(image).empty() )
771 {
772 // TODO: show more information about the file
773 wxString info = wxString::Format("Image size: %dx%d",
774 image.GetWidth(),
775 image.GetHeight());
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
787
788 case wxIMAGE_RESOLUTION_NONE:
789 info += " in default units";
790 break;
791
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 }
804 }
805
806 #ifdef wxHAVE_RAW_BITMAP
807
808 void MyFrame::OnTestRawBitmap( wxCommandEvent &WXUNUSED(event) )
809 {
810 (new MyRawBitmapFrame(this))->Show();
811 }
812
813 #endif // wxHAVE_RAW_BITMAP
814
815 #if wxUSE_GRAPHICS_CONTEXT
816
817 class MyGraphicsFrame : public wxFrame
818 {
819 public:
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
864 private:
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);
872 gc->DrawText("Bitmap", 0, HEIGHT/2);
873 gc->DrawBitmap(m_bitmap, 0, 0, WIDTH, HEIGHT);
874
875 wxGraphicsFont gf = gc->CreateFont(wxNORMAL_FONT->GetPixelSize().y, "");
876 gc->SetFont(gf);
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
887 void MyFrame::OnTestGraphics(wxCommandEvent& WXUNUSED(event))
888 {
889 new MyGraphicsFrame(this);
890 }
891
892 #endif // wxUSE_GRAPHICS_CONTEXT
893
894 #if wxUSE_CLIPBOARD
895
896 void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
897 {
898 wxBitmapDataObject *dobjBmp = new wxBitmapDataObject;
899 dobjBmp->SetBitmap(m_canvas->my_horse_png);
900
901 wxTheClipboard->Open();
902
903 if ( !wxTheClipboard->SetData(dobjBmp) )
904 {
905 wxLogError(wxT("Failed to copy bitmap to clipboard"));
906 }
907
908 wxTheClipboard->Close();
909 }
910
911 void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
912 {
913 wxBitmapDataObject dobjBmp;
914
915 wxTheClipboard->Open();
916 if ( !wxTheClipboard->GetData(dobjBmp) )
917 {
918 wxLogMessage(wxT("No bitmap data in the clipboard"));
919 }
920 else
921 {
922 new MyImageFrame(this, wxT("Clipboard"), dobjBmp.GetBitmap());
923 }
924 wxTheClipboard->Close();
925 }
926
927 #endif // wxUSE_CLIPBOARD
928
929 void MyFrame::OnThumbnail( wxCommandEvent &WXUNUSED(event) )
930 {
931 #if wxUSE_FILEDLG
932 wxString filename = wxLoadFileSelector(wxT("image"), wxEmptyString, wxEmptyString, this);
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 {
946 wxLogError(wxT("Couldn't load image from '%s'."), filename.c_str());
947 return;
948 }
949
950 int origWidth = image.GetOptionInt( wxIMAGE_OPTION_ORIGINAL_WIDTH );
951 int origHeight = image.GetOptionInt( wxIMAGE_OPTION_ORIGINAL_HEIGHT );
952
953 const long loadTime = sw.Time();
954
955 MyImageFrame * const frame = new MyImageFrame(this, filename, image);
956 wxLogStatus(frame, "Loaded \"%s\" in %ldms; original size was (%d, %d)",
957 filename, loadTime, origWidth, origHeight);
958 #else
959 wxLogError( wxT("Couldn't create file selector dialog") );
960 return;
961 #endif // wxUSE_FILEDLG
962 }
963
964 //-----------------------------------------------------------------------------
965 // MyApp
966 //-----------------------------------------------------------------------------
967
968 IMPLEMENT_APP(MyApp)
969
970 bool 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 }