fix background repainting under wxMSW (not sure this is the best way to do it - but...
[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:
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 #include "smile.xbm"
37 #include "smile.xpm"
38
39 #if defined(__WXMSW__)
40 #ifdef wxHAVE_RAW_BITMAP
41 #include "wx/rawbmp.h"
42 #endif
43 #endif
44
45 #if defined(__WXMAC__) || defined(__WXGTK__)
46 #define wxHAVE_RAW_BITMAP
47 #include "wx/rawbmp.h"
48 #endif
49
50 // derived classes
51
52 class MyFrame;
53 class MyApp;
54
55 // MyCanvas
56
57 class MyCanvas: public wxScrolledWindow
58 {
59 public:
60 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
61 ~MyCanvas();
62 void OnPaint( wxPaintEvent &event );
63 void CreateAntiAliasedBitmap();
64
65 wxBitmap my_horse_png;
66 wxBitmap my_horse_jpeg;
67 wxBitmap my_horse_gif;
68 wxBitmap my_horse_bmp;
69 wxBitmap my_horse_bmp2;
70 wxBitmap my_horse_pcx;
71 wxBitmap my_horse_pnm;
72 wxBitmap my_horse_tiff;
73 wxBitmap my_horse_tga;
74 wxBitmap my_horse_xpm;
75 wxBitmap my_horse_ico32;
76 wxBitmap my_horse_ico16;
77 wxBitmap my_horse_ico;
78 wxBitmap my_horse_cur;
79
80 wxBitmap my_smile_xbm;
81 wxBitmap my_square;
82 wxBitmap my_anti;
83
84 wxBitmap my_horse_asciigrey_pnm;
85 wxBitmap my_horse_rawgrey_pnm;
86
87 wxBitmap colorized_horse_jpeg;
88 wxBitmap my_cmyk_jpeg;
89
90 wxBitmap my_toucan;
91 wxBitmap my_toucan_flipped_horiz;
92 wxBitmap my_toucan_flipped_vert;
93 wxBitmap my_toucan_flipped_both;
94 wxBitmap my_toucan_grey;
95 wxBitmap my_toucan_head;
96 wxBitmap my_toucan_scaled_normal;
97 wxBitmap my_toucan_scaled_high;
98 wxBitmap my_toucan_blur;
99
100 int xH, yH ;
101 int m_ani_images;
102 wxBitmap *my_horse_ani;
103
104 private:
105 wxBitmap m_bmpSmileXpm;
106 wxIcon m_iconSmileXpm;
107
108 DECLARE_EVENT_TABLE()
109 };
110
111
112 // MyFrame
113
114
115 class MyFrame: public wxFrame
116 {
117 public:
118 MyFrame();
119
120 void OnAbout( wxCommandEvent &event );
121 void OnNewFrame( wxCommandEvent &event );
122 void OnImageInfo( wxCommandEvent &event );
123 void OnThumbnail( wxCommandEvent &event );
124
125 #ifdef wxHAVE_RAW_BITMAP
126 void OnTestRawBitmap( wxCommandEvent &event );
127 #endif // wxHAVE_RAW_BITMAP
128 void OnQuit( wxCommandEvent &event );
129
130 #if wxUSE_CLIPBOARD
131 void OnCopy(wxCommandEvent& event);
132 void OnPaste(wxCommandEvent& event);
133 #endif // wxUSE_CLIPBOARD
134
135 MyCanvas *m_canvas;
136
137 private:
138 // ask user for the file name and try to load an image from it
139 //
140 // return the file path on success, empty string if we failed to load the
141 // image or were cancelled by user
142 static wxString LoadUserImage(wxImage& image);
143
144
145 DECLARE_DYNAMIC_CLASS(MyFrame)
146 DECLARE_EVENT_TABLE()
147 };
148
149 // ----------------------------------------------------------------------------
150 // Frame used for showing a standalone image
151 // ----------------------------------------------------------------------------
152
153 enum
154 {
155 ID_ROTATE_LEFT = 100,
156 ID_ROTATE_RIGHT,
157 ID_RESIZE
158 };
159
160 class MyImageFrame : public wxFrame
161 {
162 public:
163 MyImageFrame(wxFrame *parent, const wxString& desc, const wxBitmap& bitmap)
164 : wxFrame(parent, wxID_ANY,
165 wxString::Format(_T("Image from %s"), desc.c_str()),
166 wxDefaultPosition, wxDefaultSize,
167 wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE),
168 m_bitmap(bitmap)
169 {
170 wxMenu *menu = new wxMenu;
171 menu->Append(wxID_SAVE);
172 menu->AppendSeparator();
173 menu->Append(ID_RESIZE, _T("&Fit to window\tCtrl-F"));
174 menu->AppendSeparator();
175 menu->Append(ID_ROTATE_LEFT, _T("Rotate &left\tCtrl-L"));
176 menu->Append(ID_ROTATE_RIGHT, _T("Rotate &right\tCtrl-R"));
177
178 wxMenuBar *mbar = new wxMenuBar;
179 mbar->Append(menu, _T("&Image"));
180 SetMenuBar(mbar);
181
182 CreateStatusBar();
183
184 SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
185
186 UpdateStatusBar();
187
188 SetBackgroundColour(*wxWHITE);
189 }
190
191 void OnEraseBackground(wxEraseEvent& WXUNUSED(event))
192 {
193 // do nothing here to be able to see how transparent images are shown
194 }
195
196 void OnPaint(wxPaintEvent& WXUNUSED(event))
197 {
198 wxPaintDC dc(this);
199
200 ClearBackground();
201
202 const wxSize size = GetClientSize();
203 dc.DrawBitmap(m_bitmap,
204 (size.x - m_bitmap.GetWidth())/2,
205 (size.y - m_bitmap.GetHeight())/2,
206 true /* use mask */);
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 wxT("PNG files (*.png)|*.png|")
220 wxT("JPEG files (*.jpg)|*.jpg|")
221 wxT("GIF files (*.gif)|*.gif|")
222 wxT("TIFF files (*.tif)|*.tif|")
223 wxT("PCX files (*.pcx)|*.pcx|")
224 wxT("ICO files (*.ico)|*.ico|")
225 wxT("CUR files (*.cur)|*.cur"),
226 wxFD_SAVE,
227 this);
228
229 if ( savefilename.empty() )
230 return;
231
232 wxString extension;
233 wxFileName::SplitPath(savefilename, NULL, NULL, &extension);
234
235 bool saved = false;
236 if ( extension == _T("bmp") )
237 {
238 static const int bppvalues[] =
239 {
240 wxBMP_1BPP,
241 wxBMP_1BPP_BW,
242 wxBMP_4BPP,
243 wxBMP_8BPP,
244 wxBMP_8BPP_GREY,
245 wxBMP_8BPP_RED,
246 wxBMP_8BPP_PALETTE,
247 wxBMP_24BPP
248 };
249
250 const wxString bppchoices[] =
251 {
252 _T("1 bpp color"),
253 _T("1 bpp B&W"),
254 _T("4 bpp color"),
255 _T("8 bpp color"),
256 _T("8 bpp greyscale"),
257 _T("8 bpp red"),
258 _T("8 bpp own palette"),
259 _T("24 bpp")
260 };
261
262 int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"),
263 _T("Image sample: save file"),
264 WXSIZEOF(bppchoices),
265 bppchoices,
266 this);
267 if ( bppselection != -1 )
268 {
269 int format = bppvalues[bppselection];
270 image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, format);
271
272 if ( format == wxBMP_8BPP_PALETTE )
273 {
274 unsigned char *cmap = new unsigned char [256];
275 for ( int i = 0; i < 256; i++ )
276 cmap[i] = (unsigned char)i;
277 image.SetPalette(wxPalette(256, cmap, cmap, cmap));
278
279 delete[] cmap;
280 }
281 }
282 }
283 else if ( extension == _T("png") )
284 {
285 static const int pngvalues[] =
286 {
287 wxPNG_TYPE_COLOUR,
288 wxPNG_TYPE_COLOUR,
289 wxPNG_TYPE_GREY,
290 wxPNG_TYPE_GREY,
291 wxPNG_TYPE_GREY_RED,
292 wxPNG_TYPE_GREY_RED,
293 };
294
295 const wxString pngchoices[] =
296 {
297 _T("Colour 8bpp"),
298 _T("Colour 16bpp"),
299 _T("Grey 8bpp"),
300 _T("Grey 16bpp"),
301 _T("Grey red 8bpp"),
302 _T("Grey red 16bpp"),
303 };
304
305 int sel = wxGetSingleChoiceIndex(_T("Set PNG format"),
306 _T("Image sample: save file"),
307 WXSIZEOF(pngchoices),
308 pngchoices,
309 this);
310 if ( sel != -1 )
311 {
312 image.SetOption(wxIMAGE_OPTION_PNG_FORMAT, pngvalues[sel]);
313 image.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH, sel % 2 ? 16 : 8);
314 }
315 }
316 else if ( extension == _T("cur") )
317 {
318 image.Rescale(32,32);
319 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0);
320 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0);
321 // This shows how you can save an image with explicitly
322 // specified image format:
323 saved = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR);
324 }
325
326 if ( !saved )
327 {
328 // This one guesses image format from filename extension
329 // (it may fail if the extension is not recognized):
330 image.SaveFile(savefilename);
331 }
332 #endif // wxUSE_FILEDLG
333 }
334
335 void OnResize(wxCommandEvent& WXUNUSED(event))
336 {
337 wxImage img(m_bitmap.ConvertToImage());
338
339 const wxSize size = GetClientSize();
340 img.Rescale(size.x, size.y, wxIMAGE_QUALITY_HIGH);
341 m_bitmap = wxBitmap(img);
342
343 UpdateStatusBar();
344 Refresh();
345 }
346
347 void OnRotate(wxCommandEvent& event)
348 {
349 double angle = 5;
350 if ( event.GetId() == ID_ROTATE_LEFT )
351 angle = -angle;
352
353 wxImage img(m_bitmap.ConvertToImage());
354 img = img.Rotate(angle, wxPoint(img.GetWidth() / 2, img.GetHeight() / 2));
355 if ( !img.Ok() )
356 {
357 wxLogWarning(_T("Rotation failed"));
358 return;
359 }
360
361 m_bitmap = wxBitmap(img);
362
363 UpdateStatusBar();
364 Refresh();
365 }
366
367 private:
368 void UpdateStatusBar()
369 {
370 wxLogStatus(this, _T("Image size: (%d, %d)"),
371 m_bitmap.GetWidth(),
372 m_bitmap.GetHeight());
373 }
374
375 wxBitmap m_bitmap;
376
377 DECLARE_EVENT_TABLE()
378 };
379
380 #ifdef wxHAVE_RAW_BITMAP
381
382 #include "wx/rawbmp.h"
383
384 class MyRawBitmapFrame : public wxFrame
385 {
386 public:
387 enum
388 {
389 BORDER = 15,
390 SIZE = 150,
391 REAL_SIZE = SIZE - 2*BORDER
392 };
393
394 MyRawBitmapFrame(wxFrame *parent)
395 : wxFrame(parent, wxID_ANY, _T("Raw bitmaps (how exciting)")),
396 m_bitmap(SIZE, SIZE, 24),
397 m_alphaBitmap(SIZE, SIZE, 32)
398 {
399 SetClientSize(SIZE, SIZE*2+25);
400
401 InitAlphaBitmap();
402 InitBitmap();
403
404 }
405
406 void InitAlphaBitmap()
407 {
408 // First, clear the whole bitmap by making it alpha
409 {
410 wxAlphaPixelData data( m_alphaBitmap, wxPoint(0,0), wxSize(SIZE, SIZE) );
411 if ( !data )
412 {
413 wxLogError(_T("Failed to gain raw access to bitmap data"));
414 return;
415 }
416 wxAlphaPixelData::Iterator p(data);
417 for ( int y = 0; y < SIZE; ++y )
418 {
419 wxAlphaPixelData::Iterator rowStart = p;
420 for ( int x = 0; x < SIZE; ++x )
421 {
422 p.Alpha() = 0;
423 ++p; // same as p.OffsetX(1)
424 }
425 p = rowStart;
426 p.OffsetY(data, 1);
427 }
428 }
429
430 // Then, draw colourful alpha-blended stripes
431 wxAlphaPixelData data(m_alphaBitmap, wxPoint(BORDER, BORDER),
432 wxSize(REAL_SIZE, REAL_SIZE));
433 if ( !data )
434 {
435 wxLogError(_T("Failed to gain raw access to bitmap data"));
436 return;
437 }
438
439 wxAlphaPixelData::Iterator p(data);
440
441 for ( int y = 0; y < REAL_SIZE; ++y )
442 {
443 wxAlphaPixelData::Iterator rowStart = p;
444
445 int r = y < REAL_SIZE/3 ? 255 : 0,
446 g = (REAL_SIZE/3 <= y) && (y < 2*(REAL_SIZE/3)) ? 255 : 0,
447 b = 2*(REAL_SIZE/3) <= y ? 255 : 0;
448
449 for ( int x = 0; x < REAL_SIZE; ++x )
450 {
451 // note that RGB must be premultiplied by alpha
452 unsigned a = (wxAlphaPixelData::Iterator::ChannelType)((x*255.)/REAL_SIZE);
453 p.Red() = r * a / 256;
454 p.Green() = g * a / 256;
455 p.Blue() = b * a / 256;
456 p.Alpha() = a;
457
458 ++p; // same as p.OffsetX(1)
459 }
460
461 p = rowStart;
462 p.OffsetY(data, 1);
463 }
464 }
465
466 void InitBitmap()
467 {
468 // draw some colourful stripes without alpha
469 wxNativePixelData data(m_bitmap);
470 if ( !data )
471 {
472 wxLogError(_T("Failed to gain raw access to bitmap data"));
473 return;
474 }
475
476 wxNativePixelData::Iterator p(data);
477 for ( int y = 0; y < SIZE; ++y )
478 {
479 wxNativePixelData::Iterator rowStart = p;
480
481 int r = y < SIZE/3 ? 255 : 0,
482 g = (SIZE/3 <= y) && (y < 2*(SIZE/3)) ? 255 : 0,
483 b = 2*(SIZE/3) <= y ? 255 : 0;
484
485 for ( int x = 0; x < SIZE; ++x )
486 {
487 p.Red() = r;
488 p.Green() = g;
489 p.Blue() = b;
490 ++p; // same as p.OffsetX(1)
491 }
492
493 p = rowStart;
494 p.OffsetY(data, 1);
495 }
496 }
497
498 void OnPaint(wxPaintEvent& WXUNUSED(event))
499 {
500 wxPaintDC dc( this );
501 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER);
502 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE/2 - BORDER);
503 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE - 2*BORDER);
504 dc.DrawBitmap( m_alphaBitmap, 0, 0, true /* use mask */ );
505
506 dc.DrawText(_T("Raw bitmap access without alpha"), 0, SIZE+5);
507 dc.DrawBitmap( m_bitmap, 0, SIZE+5+dc.GetCharHeight());
508 }
509
510 private:
511 wxBitmap m_bitmap;
512 wxBitmap m_alphaBitmap;
513
514 DECLARE_EVENT_TABLE()
515 };
516
517 #endif // wxHAVE_RAW_BITMAP
518
519 // MyApp
520
521 class MyApp: public wxApp
522 {
523 public:
524 virtual bool OnInit();
525 };
526
527 // main program
528
529 IMPLEMENT_APP(MyApp)
530
531 // MyCanvas
532
533 BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
534 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground)
535 EVT_PAINT(MyImageFrame::OnPaint)
536
537 EVT_MENU(wxID_SAVE, MyImageFrame::OnSave)
538 EVT_MENU_RANGE(ID_ROTATE_LEFT, ID_ROTATE_RIGHT, MyImageFrame::OnRotate)
539 EVT_MENU(ID_RESIZE, MyImageFrame::OnResize)
540 END_EVENT_TABLE()
541
542 #ifdef wxHAVE_RAW_BITMAP
543
544 BEGIN_EVENT_TABLE(MyRawBitmapFrame, wxFrame)
545 EVT_PAINT(MyRawBitmapFrame::OnPaint)
546 END_EVENT_TABLE()
547
548 #endif // wxHAVE_RAW_BITMAP
549
550 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
551 EVT_PAINT(MyCanvas::OnPaint)
552 END_EVENT_TABLE()
553
554 MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
555 const wxPoint &pos, const wxSize &size )
556 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
557 , m_bmpSmileXpm(smile_xpm)
558 , m_iconSmileXpm(smile_xpm)
559 {
560 my_horse_ani = NULL;
561 m_ani_images = 0 ;
562
563 SetBackgroundColour(* wxWHITE);
564
565 wxBitmap bitmap( 100, 100 );
566
567 wxMemoryDC dc;
568 dc.SelectObject( bitmap );
569 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
570 dc.SetPen( *wxBLACK_PEN );
571 dc.DrawRectangle( 0, 0, 100, 100 );
572 dc.SetBrush( *wxWHITE_BRUSH );
573 dc.DrawRectangle( 20, 20, 60, 60 );
574 dc.SelectObject( wxNullBitmap );
575
576 // try to find the directory with our images
577 wxString dir;
578 if ( wxFile::Exists(wxT("./horse.png")) )
579 dir = wxT("./");
580 else if ( wxFile::Exists(wxT("../horse.png")) )
581 dir = wxT("../");
582 else
583 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
584
585 wxImage image = bitmap.ConvertToImage();
586
587 #if wxUSE_LIBPNG
588 if ( !image.SaveFile( dir + _T("test.png"), wxBITMAP_TYPE_PNG ))
589 wxLogError(wxT("Can't save file"));
590
591 image.Destroy();
592
593 if ( image.LoadFile( dir + _T("test.png") ) )
594 my_square = wxBitmap( image );
595
596 image.Destroy();
597
598 if ( !image.LoadFile( dir + _T("horse.png")) )
599 wxLogError(wxT("Can't load PNG image"));
600 else
601 my_horse_png = wxBitmap( image );
602
603 if ( !image.LoadFile( dir + _T("toucan.png")) )
604 wxLogError(wxT("Can't load PNG image"));
605 else
606 my_toucan = wxBitmap(image);
607
608 my_toucan_flipped_horiz = wxBitmap(image.Mirror(true));
609 my_toucan_flipped_vert = wxBitmap(image.Mirror(false));
610 my_toucan_flipped_both = wxBitmap(image.Mirror(true).Mirror(false));
611 my_toucan_grey = wxBitmap(image.ConvertToGreyscale());
612 my_toucan_head = wxBitmap(image.GetSubImage(wxRect(40, 7, 80, 60)));
613 my_toucan_scaled_normal = wxBitmap(image.Scale(110,90,wxIMAGE_QUALITY_NORMAL));
614 my_toucan_scaled_high = wxBitmap(image.Scale(110,90,wxIMAGE_QUALITY_HIGH));
615 my_toucan_blur = wxBitmap(image.Blur(10));
616
617 #endif // wxUSE_LIBPNG
618
619 #if wxUSE_LIBJPEG
620 image.Destroy();
621
622 if ( !image.LoadFile( dir + _T("horse.jpg")) )
623 wxLogError(wxT("Can't load JPG image"));
624 else
625 {
626 my_horse_jpeg = wxBitmap( image );
627
628 // Colorize by rotating green hue to red
629 wxImage::HSVValue greenHSV = wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0));
630 wxImage::HSVValue redHSV = wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0));
631 image.RotateHue(redHSV.hue - greenHSV.hue);
632 colorized_horse_jpeg = wxBitmap( image );
633 }
634
635 if ( !image.LoadFile( dir + _T("cmyk.jpg")) )
636 wxLogError(_T("Can't load CMYK JPG image"));
637 else
638 my_cmyk_jpeg = wxBitmap(image);
639 #endif // wxUSE_LIBJPEG
640
641 #if wxUSE_GIF
642 image.Destroy();
643
644 if ( !image.LoadFile( dir + _T("horse.gif" )) )
645 wxLogError(wxT("Can't load GIF image"));
646 else
647 my_horse_gif = wxBitmap( image );
648 #endif
649
650 #if wxUSE_PCX
651 image.Destroy();
652
653 if ( !image.LoadFile( dir + _T("horse.pcx"), wxBITMAP_TYPE_PCX ) )
654 wxLogError(wxT("Can't load PCX image"));
655 else
656 my_horse_pcx = wxBitmap( image );
657 #endif
658
659 image.Destroy();
660
661 if ( !image.LoadFile( dir + _T("horse.bmp"), wxBITMAP_TYPE_BMP ) )
662 wxLogError(wxT("Can't load BMP image"));
663 else
664 my_horse_bmp = wxBitmap( image );
665
666 #if wxUSE_XPM
667 image.Destroy();
668
669 if ( !image.LoadFile( dir + _T("horse.xpm"), wxBITMAP_TYPE_XPM ) )
670 wxLogError(wxT("Can't load XPM image"));
671 else
672 my_horse_xpm = wxBitmap( image );
673
674 if ( !image.SaveFile( dir + _T("test.xpm"), wxBITMAP_TYPE_XPM ))
675 wxLogError(wxT("Can't save file"));
676 #endif
677
678 #if wxUSE_PNM
679 image.Destroy();
680
681 if ( !image.LoadFile( dir + _T("horse.pnm"), wxBITMAP_TYPE_PNM ) )
682 wxLogError(wxT("Can't load PNM image"));
683 else
684 my_horse_pnm = wxBitmap( image );
685
686 image.Destroy();
687
688 if ( !image.LoadFile( dir + _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM ) )
689 wxLogError(wxT("Can't load PNM image"));
690 else
691 my_horse_asciigrey_pnm = wxBitmap( image );
692
693 image.Destroy();
694
695 if ( !image.LoadFile( dir + _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM ) )
696 wxLogError(wxT("Can't load PNM image"));
697 else
698 my_horse_rawgrey_pnm = wxBitmap( image );
699 #endif
700
701 #if wxUSE_LIBTIFF
702 image.Destroy();
703
704 if ( !image.LoadFile( dir + _T("horse.tif"), wxBITMAP_TYPE_TIF ) )
705 wxLogError(wxT("Can't load TIFF image"));
706 else
707 my_horse_tiff = wxBitmap( image );
708 #endif
709
710 #if wxUSE_LIBTIFF
711 image.Destroy();
712
713 if ( !image.LoadFile( dir + _T("horse.tga"), wxBITMAP_TYPE_TGA ) )
714 wxLogError(wxT("Can't load TGA image"));
715 else
716 my_horse_tga = wxBitmap( image );
717 #endif
718
719 CreateAntiAliasedBitmap();
720
721 my_smile_xbm = wxBitmap( (const char*)smile_bits, smile_width,
722 smile_height, 1 );
723
724 // demonstrates XPM automatically using the mask when saving
725 if ( m_bmpSmileXpm.Ok() )
726 m_bmpSmileXpm.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM);
727
728 #if wxUSE_ICO_CUR
729 image.Destroy();
730
731 if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 0 ) )
732 wxLogError(wxT("Can't load first ICO image"));
733 else
734 my_horse_ico32 = wxBitmap( image );
735
736 image.Destroy();
737
738 if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 1 ) )
739 wxLogError(wxT("Can't load second ICO image"));
740 else
741 my_horse_ico16 = wxBitmap( image );
742
743 image.Destroy();
744
745 if ( !image.LoadFile( dir + _T("horse.ico") ) )
746 wxLogError(wxT("Can't load best ICO image"));
747 else
748 my_horse_ico = wxBitmap( image );
749
750 image.Destroy();
751
752 if ( !image.LoadFile( dir + _T("horse.cur"), wxBITMAP_TYPE_CUR ) )
753 wxLogError(wxT("Can't load best ICO image"));
754 else
755 {
756 my_horse_cur = wxBitmap( image );
757 xH = 30 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) ;
758 yH = 2420 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ;
759 }
760
761 m_ani_images = wxImage::GetImageCount ( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI );
762 if (m_ani_images==0)
763 wxLogError(wxT("No ANI-format images found"));
764 else
765 my_horse_ani = new wxBitmap [m_ani_images];
766 int i ;
767 for (i=0; i < m_ani_images; i++)
768 {
769 image.Destroy();
770 if (!image.LoadFile( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI, i ))
771 {
772 wxString tmp = wxT("Can't load image number ");
773 tmp << i ;
774 wxLogError(tmp);
775 }
776 else
777 my_horse_ani [i] = wxBitmap( image );
778 }
779 #endif // wxUSE_ICO_CUR
780
781 image.Destroy();
782
783 // test image loading from stream
784 wxFile file(dir + _T("horse.bmp"));
785 if ( file.IsOpened() )
786 {
787 wxFileOffset len = file.Length();
788 size_t dataSize = (size_t)len;
789 void *data = malloc(dataSize);
790 if ( file.Read(data, dataSize) != len )
791 wxLogError(_T("Reading bitmap file failed"));
792 else
793 {
794 wxMemoryInputStream mis(data, dataSize);
795 if ( !image.LoadFile(mis) )
796 wxLogError(wxT("Can't load BMP image from stream"));
797 else
798 my_horse_bmp2 = wxBitmap( image );
799 }
800
801 free(data);
802 }
803 }
804
805 MyCanvas::~MyCanvas()
806 {
807 delete [] my_horse_ani;
808 }
809
810 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
811 {
812 wxPaintDC dc( this );
813 PrepareDC( dc );
814
815 dc.DrawText( _T("Loaded image"), 30, 10 );
816 if (my_square.Ok())
817 dc.DrawBitmap( my_square, 30, 30 );
818
819 dc.DrawText( _T("Drawn directly"), 150, 10 );
820 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
821 dc.SetPen( *wxBLACK_PEN );
822 dc.DrawRectangle( 150, 30, 100, 100 );
823 dc.SetBrush( *wxWHITE_BRUSH );
824 dc.DrawRectangle( 170, 50, 60, 60 );
825
826 if (my_anti.Ok())
827 dc.DrawBitmap( my_anti, 280, 30 );
828
829 dc.DrawText( _T("PNG handler"), 30, 135 );
830 if (my_horse_png.Ok())
831 {
832 dc.DrawBitmap( my_horse_png, 30, 150 );
833 wxRect rect(0,0,100,100);
834 wxBitmap sub( my_horse_png.GetSubBitmap(rect) );
835 dc.DrawText( _T("GetSubBitmap()"), 280, 175 );
836 dc.DrawBitmap( sub, 280, 195 );
837 }
838
839 dc.DrawText( _T("JPEG handler"), 30, 365 );
840 if (my_horse_jpeg.Ok())
841 dc.DrawBitmap( my_horse_jpeg, 30, 380 );
842
843 dc.DrawText( _T("Green rotated to red"), 280, 365 );
844 if (colorized_horse_jpeg.Ok())
845 dc.DrawBitmap( colorized_horse_jpeg, 280, 380 );
846
847 dc.DrawText( _T("CMYK JPEG image"), 530, 365 );
848 if (my_cmyk_jpeg.Ok())
849 dc.DrawBitmap( my_cmyk_jpeg, 530, 380 );
850
851 dc.DrawText( _T("GIF handler"), 30, 595 );
852 if (my_horse_gif.Ok())
853 dc.DrawBitmap( my_horse_gif, 30, 610 );
854
855 dc.DrawText( _T("PCX handler"), 30, 825 );
856 if (my_horse_pcx.Ok())
857 dc.DrawBitmap( my_horse_pcx, 30, 840 );
858
859 dc.DrawText( _T("BMP handler"), 30, 1055 );
860 if (my_horse_bmp.Ok())
861 dc.DrawBitmap( my_horse_bmp, 30, 1070 );
862
863 dc.DrawText( _T("BMP read from memory"), 280, 1055 );
864 if (my_horse_bmp2.Ok())
865 dc.DrawBitmap( my_horse_bmp2, 280, 1070 );
866
867 dc.DrawText( _T("PNM handler"), 30, 1285 );
868 if (my_horse_pnm.Ok())
869 dc.DrawBitmap( my_horse_pnm, 30, 1300 );
870
871 dc.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 );
872 if (my_horse_asciigrey_pnm.Ok())
873 dc.DrawBitmap( my_horse_asciigrey_pnm, 280, 1300 );
874
875 dc.DrawText( _T("PNM handler (raw grey)"), 530, 1285 );
876 if (my_horse_rawgrey_pnm.Ok())
877 dc.DrawBitmap( my_horse_rawgrey_pnm, 530, 1300 );
878
879 dc.DrawText( _T("TIFF handler"), 30, 1515 );
880 if (my_horse_tiff.Ok())
881 dc.DrawBitmap( my_horse_tiff, 30, 1530 );
882
883 dc.DrawText( _T("TGA handler"), 30, 1745 );
884 if (my_horse_tga.Ok())
885 dc.DrawBitmap( my_horse_tga, 30, 1760 );
886
887 dc.DrawText( _T("XPM handler"), 30, 1975 );
888 if (my_horse_xpm.Ok())
889 dc.DrawBitmap( my_horse_xpm, 30, 2000 );
890
891 // toucans
892 {
893 int x = 750, y = 10, yy = 170;
894
895 dc.DrawText(wxT("Original toucan"), x+50, y);
896 dc.DrawBitmap(my_toucan, x, y+15, true);
897 y += yy;
898 dc.DrawText(wxT("Flipped horizontally"), x+50, y);
899 dc.DrawBitmap(my_toucan_flipped_horiz, x, y+15, true);
900 y += yy;
901 dc.DrawText(wxT("Flipped vertically"), x+50, y);
902 dc.DrawBitmap(my_toucan_flipped_vert, x, y+15, true);
903 y += yy;
904 dc.DrawText(wxT("Flipped both h&v"), x+50, y);
905 dc.DrawBitmap(my_toucan_flipped_both, x, y+15, true);
906
907 y += yy;
908 dc.DrawText(wxT("In greyscale"), x+50, y);
909 dc.DrawBitmap(my_toucan_grey, x, y+15, true);
910
911 y += yy;
912 dc.DrawText(wxT("Toucan's head"), x+50, y);
913 dc.DrawBitmap(my_toucan_head, x, y+15, true);
914
915 y += yy;
916 dc.DrawText(wxT("Scaled with normal quality"), x+50, y);
917 dc.DrawBitmap(my_toucan_scaled_normal, x, y+15, true);
918
919 y += yy;
920 dc.DrawText(wxT("Scaled with high quality"), x+50, y);
921 dc.DrawBitmap(my_toucan_scaled_high, x, y+15, true);
922
923 y += yy;
924 dc.DrawText(wxT("Blured"), x+50, y);
925 dc.DrawBitmap(my_toucan_blur, x, y+15, true);
926 }
927
928 if (my_smile_xbm.Ok())
929 {
930 int x = 300, y = 1800;
931
932 dc.DrawText( _T("XBM bitmap"), x, y );
933 dc.DrawText( _T("(green on red)"), x, y + 15 );
934 dc.SetTextForeground( _T("GREEN") );
935 dc.SetTextBackground( _T("RED") );
936 dc.DrawBitmap( my_smile_xbm, x, y + 30 );
937
938 dc.SetTextForeground( *wxBLACK );
939 dc.DrawText( _T("After wxImage conversion"), x + 120, y );
940 dc.DrawText( _T("(red on white)"), x + 120, y + 15 );
941 dc.SetTextForeground( wxT("RED") );
942 wxImage i = my_smile_xbm.ConvertToImage();
943 i.SetMaskColour( 255, 255, 255 );
944 i.Replace( 0, 0, 0,
945 wxRED_PEN->GetColour().Red(),
946 wxRED_PEN->GetColour().Green(),
947 wxRED_PEN->GetColour().Blue() );
948 dc.DrawBitmap( wxBitmap(i), x + 120, y + 30, true );
949 dc.SetTextForeground( *wxBLACK );
950 }
951
952
953 wxBitmap mono( 60,50,1 );
954 wxMemoryDC memdc;
955 memdc.SelectObject( mono );
956 memdc.SetPen( *wxBLACK_PEN );
957 memdc.SetBrush( *wxWHITE_BRUSH );
958 memdc.DrawRectangle( 0,0,60,50 );
959 memdc.SetTextForeground( *wxBLACK );
960 #ifndef __WXGTK20__
961 // I cannot convince GTK2 to draw into mono bitmaps
962 memdc.DrawText( _T("Hi!"), 5, 5 );
963 #endif
964 memdc.SetBrush( *wxBLACK_BRUSH );
965 memdc.DrawRectangle( 33,5,20,20 );
966 memdc.SetPen( *wxRED_PEN );
967 memdc.DrawLine( 5, 42, 50, 42 );
968 memdc.SelectObject( wxNullBitmap );
969
970 if (mono.Ok())
971 {
972 int x = 300, y = 1900;
973
974 dc.DrawText( _T("Mono bitmap"), x, y );
975 dc.DrawText( _T("(red on green)"), x, y + 15 );
976 dc.SetTextForeground( wxT("RED") );
977 dc.SetTextBackground( wxT("GREEN") );
978 dc.DrawBitmap( mono, x, y + 30 );
979
980 dc.SetTextForeground( *wxBLACK );
981 dc.DrawText( _T("After wxImage conversion"), x + 120, y );
982 dc.DrawText( _T("(red on white)"), x + 120, y + 15 );
983 dc.SetTextForeground( wxT("RED") );
984 wxImage i = mono.ConvertToImage();
985 i.SetMaskColour( 255,255,255 );
986 i.Replace( 0,0,0,
987 wxRED_PEN->GetColour().Red(),
988 wxRED_PEN->GetColour().Green(),
989 wxRED_PEN->GetColour().Blue() );
990 dc.DrawBitmap( wxBitmap(i), x + 120, y + 30, true );
991 dc.SetTextForeground( *wxBLACK );
992 }
993
994 // For testing transparency
995 dc.SetBrush( *wxRED_BRUSH );
996 dc.DrawRectangle( 20, 2220, 560, 68 );
997
998 dc.DrawText(_T("XPM bitmap"), 30, 2230 );
999 if ( m_bmpSmileXpm.Ok() )
1000 dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, true);
1001
1002 dc.DrawText(_T("XPM icon"), 110, 2230 );
1003 if ( m_iconSmileXpm.Ok() )
1004 dc.DrawIcon(m_iconSmileXpm, 110, 2250);
1005
1006 // testing icon -> bitmap conversion
1007 wxBitmap to_blit( m_iconSmileXpm );
1008 if (to_blit.Ok())
1009 {
1010 dc.DrawText( _T("SubBitmap"), 170, 2230 );
1011 wxBitmap sub = to_blit.GetSubBitmap( wxRect(0,0,15,15) );
1012 if (sub.Ok())
1013 dc.DrawBitmap( sub, 170, 2250, true );
1014
1015 dc.DrawText( _T("Enlarged"), 250, 2230 );
1016 dc.SetUserScale( 1.5, 1.5 );
1017 dc.DrawBitmap( to_blit, (int)(250/1.5), (int)(2250/1.5), true );
1018 dc.SetUserScale( 2, 2 );
1019 dc.DrawBitmap( to_blit, (int)(300/2), (int)(2250/2), true );
1020 dc.SetUserScale( 1.0, 1.0 );
1021
1022 dc.DrawText( _T("Blit"), 400, 2230);
1023 wxMemoryDC blit_dc;
1024 blit_dc.SelectObject( to_blit );
1025 dc.Blit( 400, 2250, to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
1026 dc.SetUserScale( 1.5, 1.5 );
1027 dc.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
1028 dc.SetUserScale( 2, 2 );
1029 dc.Blit( (int)(500/2), (int)(2250/2), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
1030 dc.SetUserScale( 1.0, 1.0 );
1031 }
1032
1033 dc.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
1034 if (my_horse_ico32.Ok())
1035 dc.DrawBitmap( my_horse_ico32, 30, 2330, true );
1036
1037 dc.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
1038 if (my_horse_ico16.Ok())
1039 dc.DrawBitmap( my_horse_ico16, 230, 2330, true );
1040
1041 dc.DrawText( _T("ICO handler (best image)"), 430, 2290 );
1042 if (my_horse_ico.Ok())
1043 dc.DrawBitmap( my_horse_ico, 430, 2330, true );
1044
1045 dc.DrawText( _T("CUR handler"), 30, 2390 );
1046 if (my_horse_cur.Ok())
1047 {
1048 dc.DrawBitmap( my_horse_cur, 30, 2420, true );
1049 dc.SetPen (*wxRED_PEN);
1050 dc.DrawLine (xH-10,yH,xH+10,yH);
1051 dc.DrawLine (xH,yH-10,xH,yH+10);
1052 }
1053
1054 dc.DrawText( _T("ANI handler"), 230, 2390 );
1055 for ( int i=0; i < m_ani_images; i++ )
1056 {
1057 if (my_horse_ani[i].Ok())
1058 {
1059 dc.DrawBitmap( my_horse_ani[i], 230 + i * 2 * my_horse_ani[i].GetWidth() , 2420, true );
1060 }
1061 }
1062 }
1063
1064 void MyCanvas::CreateAntiAliasedBitmap()
1065 {
1066 wxBitmap bitmap( 300, 300 );
1067
1068 wxMemoryDC dc;
1069
1070 dc.SelectObject( bitmap );
1071
1072 dc.Clear();
1073
1074 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
1075 dc.SetTextForeground( wxT("RED") );
1076 dc.DrawText( _T("This is anti-aliased Text."), 20, 5 );
1077 dc.DrawText( _T("And a Rectangle."), 20, 45 );
1078
1079 dc.SetBrush( *wxRED_BRUSH );
1080 dc.SetPen( *wxTRANSPARENT_PEN );
1081 dc.DrawRoundedRectangle( 20, 85, 200, 180, 20 );
1082
1083 wxImage original= bitmap.ConvertToImage();
1084 wxImage anti( 150, 150 );
1085
1086 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
1087
1088 for (int y = 1; y < 149; y++)
1089 for (int x = 1; x < 149; x++)
1090 {
1091 int red = original.GetRed( x*2, y*2 ) +
1092 original.GetRed( x*2-1, y*2 ) +
1093 original.GetRed( x*2, y*2+1 ) +
1094 original.GetRed( x*2+1, y*2+1 );
1095 red = red/4;
1096
1097 int green = original.GetGreen( x*2, y*2 ) +
1098 original.GetGreen( x*2-1, y*2 ) +
1099 original.GetGreen( x*2, y*2+1 ) +
1100 original.GetGreen( x*2+1, y*2+1 );
1101 green = green/4;
1102
1103 int blue = original.GetBlue( x*2, y*2 ) +
1104 original.GetBlue( x*2-1, y*2 ) +
1105 original.GetBlue( x*2, y*2+1 ) +
1106 original.GetBlue( x*2+1, y*2+1 );
1107 blue = blue/4;
1108 anti.SetRGB( x, y, (unsigned char)red, (unsigned char)green, (unsigned char)blue );
1109 }
1110 my_anti = wxBitmap(anti);
1111 }
1112
1113 // MyFrame
1114
1115 enum
1116 {
1117 ID_QUIT = wxID_EXIT,
1118 ID_ABOUT = wxID_ABOUT,
1119 ID_NEW = 100,
1120 ID_INFO,
1121 ID_SHOWRAW,
1122 ID_SHOWTHUMBNAIL
1123 };
1124
1125 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
1126
1127 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
1128 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
1129 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
1130 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
1131 EVT_MENU (ID_INFO, MyFrame::OnImageInfo)
1132 EVT_MENU (ID_SHOWTHUMBNAIL, MyFrame::OnThumbnail)
1133 #ifdef wxHAVE_RAW_BITMAP
1134 EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap)
1135 #endif
1136
1137 #if wxUSE_CLIPBOARD
1138 EVT_MENU(wxID_COPY, MyFrame::OnCopy)
1139 EVT_MENU(wxID_PASTE, MyFrame::OnPaste)
1140 #endif // wxUSE_CLIPBOARD
1141 END_EVENT_TABLE()
1142
1143 MyFrame::MyFrame()
1144 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxImage sample"),
1145 wxPoint(20, 20), wxSize(950, 700) )
1146 {
1147 wxMenuBar *menu_bar = new wxMenuBar();
1148
1149 wxMenu *menuImage = new wxMenu;
1150 menuImage->Append( ID_NEW, _T("&Show any image...\tCtrl-O"));
1151 menuImage->Append( ID_INFO, _T("Show image &information...\tCtrl-I"));
1152 #ifdef wxHAVE_RAW_BITMAP
1153 menuImage->AppendSeparator();
1154 menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R"));
1155 #endif
1156 menuImage->AppendSeparator();
1157 menuImage->Append( ID_SHOWTHUMBNAIL, _T("Test &thumbnail...\tCtrl-T"),
1158 "Test scaling the image during load (try with JPEG)");
1159 menuImage->AppendSeparator();
1160 menuImage->Append( ID_ABOUT, _T("&About..."));
1161 menuImage->AppendSeparator();
1162 menuImage->Append( ID_QUIT, _T("E&xit\tCtrl-Q"));
1163 menu_bar->Append(menuImage, _T("&Image"));
1164
1165 #if wxUSE_CLIPBOARD
1166 wxMenu *menuClipboard = new wxMenu;
1167 menuClipboard->Append(wxID_COPY, _T("&Copy test image\tCtrl-C"));
1168 menuClipboard->Append(wxID_PASTE, _T("&Paste image\tCtrl-V"));
1169 menu_bar->Append(menuClipboard, _T("&Clipboard"));
1170 #endif // wxUSE_CLIPBOARD
1171
1172 SetMenuBar( menu_bar );
1173
1174 #if wxUSE_STATUSBAR
1175 CreateStatusBar(2);
1176 int widths[] = { -1, 100 };
1177 SetStatusWidths( 2, widths );
1178 #endif // wxUSE_STATUSBAR
1179
1180 m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) );
1181
1182 // 500 width * 2750 height
1183 m_canvas->SetScrollbars( 10, 10, 50, 275 );
1184 }
1185
1186 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
1187 {
1188 Close( true );
1189 }
1190
1191 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
1192 {
1193 (void)wxMessageBox( _T("wxImage demo\n")
1194 _T("Robert Roebling (c) 1998,2000"),
1195 _T("About wxImage Demo"), wxICON_INFORMATION | wxOK );
1196 }
1197
1198 wxString MyFrame::LoadUserImage(wxImage& image)
1199 {
1200 wxString filename;
1201
1202 #if wxUSE_FILEDLG
1203 filename = wxFileSelector(_T("Select image file"));
1204 if ( !filename.empty() )
1205 {
1206 if ( !image.LoadFile(filename) )
1207 {
1208 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
1209
1210 return wxEmptyString;
1211 }
1212 }
1213 #endif // wxUSE_FILEDLG
1214
1215 return filename;
1216 }
1217
1218 void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
1219 {
1220 wxImage image;
1221 wxString filename = LoadUserImage(image);
1222 if ( !filename.empty() )
1223 (new MyImageFrame(this, filename, wxBitmap(image)))->Show();
1224 }
1225
1226 void MyFrame::OnImageInfo( wxCommandEvent &WXUNUSED(event) )
1227 {
1228 wxImage image;
1229 if ( !LoadUserImage(image).empty() )
1230 {
1231 // TODO: show more information about the file
1232 wxString info = wxString::Format("Image size: %dx%d",
1233 image.GetWidth(),
1234 image.GetHeight());
1235
1236 int xres = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX),
1237 yres = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY);
1238 if ( xres || yres )
1239 {
1240 info += wxString::Format("\nResolution: %dx%d", xres, yres);
1241 switch ( image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT) )
1242 {
1243 default:
1244 wxFAIL_MSG( "unknown image resolution units" );
1245 // fall through
1246
1247 case wxIMAGE_RESOLUTION_NONE:
1248 info += " in default units";
1249 break;
1250
1251 case wxIMAGE_RESOLUTION_INCHES:
1252 info += " in";
1253 break;
1254
1255 case wxIMAGE_RESOLUTION_CM:
1256 info += " cm";
1257 break;
1258 }
1259 }
1260
1261 wxLogMessage("%s", info);
1262 }
1263 }
1264
1265 #ifdef wxHAVE_RAW_BITMAP
1266
1267 void MyFrame::OnTestRawBitmap( wxCommandEvent &WXUNUSED(event) )
1268 {
1269 (new MyRawBitmapFrame(this))->Show();
1270 }
1271
1272 #endif // wxHAVE_RAW_BITMAP
1273
1274 #if wxUSE_CLIPBOARD
1275
1276 void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
1277 {
1278 wxBitmapDataObject *dobjBmp = new wxBitmapDataObject;
1279 dobjBmp->SetBitmap(m_canvas->my_horse_png);
1280
1281 wxTheClipboard->Open();
1282
1283 if ( !wxTheClipboard->SetData(dobjBmp) )
1284 {
1285 wxLogError(_T("Failed to copy bitmap to clipboard"));
1286 }
1287
1288 wxTheClipboard->Close();
1289 }
1290
1291 void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
1292 {
1293 wxBitmapDataObject dobjBmp;
1294
1295 wxTheClipboard->Open();
1296 if ( !wxTheClipboard->GetData(dobjBmp) )
1297 {
1298 wxLogMessage(_T("No bitmap data in the clipboard"));
1299 }
1300 else
1301 {
1302 (new MyImageFrame(this, _T("Clipboard"), dobjBmp.GetBitmap()))->Show();
1303 }
1304 wxTheClipboard->Close();
1305 }
1306
1307 #endif // wxUSE_CLIPBOARD
1308
1309 //-----------------------------------------------------------------------------
1310 // MyApp
1311 //-----------------------------------------------------------------------------
1312
1313 bool MyApp::OnInit()
1314 {
1315 if ( !wxApp::OnInit() )
1316 return false;
1317
1318 wxInitAllImageHandlers();
1319
1320 wxFrame *frame = new MyFrame();
1321 frame->Show( true );
1322
1323 return true;
1324 }
1325
1326 void MyFrame::OnThumbnail( wxCommandEvent &WXUNUSED(event) )
1327 {
1328 #if wxUSE_FILEDLG
1329 wxString filename = wxFileSelector(_T("Select image file"));
1330 if ( filename.empty() )
1331 return;
1332
1333 static const int THUMBNAIL_WIDTH = 320;
1334 static const int THUMBNAIL_HEIGHT = 240;
1335
1336 wxImage image;
1337 image.SetOption(wxIMAGE_OPTION_MAX_WIDTH, THUMBNAIL_WIDTH);
1338 image.SetOption(wxIMAGE_OPTION_MAX_HEIGHT, THUMBNAIL_HEIGHT);
1339
1340 wxStopWatch sw;
1341 if ( !image.LoadFile(filename) )
1342 {
1343 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
1344 return;
1345 }
1346
1347 const long loadTime = sw.Time();
1348
1349 MyImageFrame * const
1350 frame = new MyImageFrame(this, filename, wxBitmap(image));
1351 frame->Show();
1352 wxLogStatus(frame, "Loaded \"%s\" in %ldms", filename, loadTime);
1353 #else
1354 wxLogError( _T("Couldn't create file selector dialog") );
1355 return;
1356 #endif // wxUSE_FILEDLG
1357 }
1358