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