1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: samples/image/image.cpp
3 // Purpose: sample showing operations with wxImage
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi
8 // Copyright: (c) 1998-2005 Robert Roebling
9 // (c) 2005-2009 Vadim Zeitlin
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx/wx.h".
14 #include "wx/wxprec.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"
36 #include "wx/dataobj.h"
37 #include "wx/clipbrd.h"
38 #endif // wxUSE_CLIPBOARD
40 #if defined(__WXMSW__)
41 #ifdef wxHAVE_RAW_BITMAP
42 #include "wx/rawbmp.h"
46 #if defined(__WXMAC__) || defined(__WXGTK__)
47 #define wxHAVE_RAW_BITMAP
48 #include "wx/rawbmp.h"
54 #include "../sample.xpm"
57 // ============================================================================
59 // ============================================================================
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 class MyApp
: public wxApp
68 virtual bool OnInit();
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 class MyFrame
: public wxFrame
80 void OnAbout( wxCommandEvent
&event
);
81 void OnNewFrame( wxCommandEvent
&event
);
82 void OnImageInfo( wxCommandEvent
&event
);
83 void OnThumbnail( wxCommandEvent
&event
);
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
);
94 void OnCopy(wxCommandEvent
& event
);
95 void OnPaste(wxCommandEvent
& event
);
96 #endif // wxUSE_CLIPBOARD
101 // ask user for the file name and try to load an image from it
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
);
108 DECLARE_DYNAMIC_CLASS(MyFrame
)
109 DECLARE_EVENT_TABLE()
112 // ----------------------------------------------------------------------------
113 // Frame used for showing a standalone image
114 // ----------------------------------------------------------------------------
118 ID_ROTATE_LEFT
= wxID_HIGHEST
+1,
124 class MyImageFrame
: public wxFrame
127 MyImageFrame(wxFrame
*parent
, const wxString
& desc
, const wxImage
& image
)
129 Create(parent
, desc
, wxBitmap(image
), image
.GetImageCount(desc
));
132 MyImageFrame(wxFrame
*parent
, const wxString
& desc
, const wxBitmap
& bitmap
)
134 Create(parent
, desc
, bitmap
);
138 bool Create(wxFrame
*parent
,
139 const wxString
& desc
,
140 const wxBitmap
& bitmap
,
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
) )
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"));
166 wxMenuBar
*mbar
= new wxMenuBar
;
167 mbar
->Append(menu
, wxT("&Image"));
170 mbar
->Check(ID_PAINT_BG
, true);
173 if ( numImages
!= 1 )
174 SetStatusText(wxString::Format("%d images", numImages
), 1);
176 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
185 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
187 // do nothing here to be able to see how transparent images are shown
190 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
194 if ( GetMenuBar()->IsChecked(ID_PAINT_BG
) )
197 dc
.SetUserScale(m_zoom
, m_zoom
);
199 const wxSize size
= GetClientSize();
203 dc
.DeviceToLogicalX((size
.x
- m_zoom
*m_bitmap
.GetWidth())/2),
204 dc
.DeviceToLogicalY((size
.y
- m_zoom
*m_bitmap
.GetHeight())/2),
209 void OnSave(wxCommandEvent
& WXUNUSED(event
))
212 wxImage image
= m_bitmap
.ConvertToImage();
214 wxString savefilename
= wxFileSelector( wxT("Save Image"),
217 (const wxChar
*)NULL
,
218 wxT("BMP files (*.bmp)|*.bmp|")
220 wxT("PNG files (*.png)|*.png|")
223 wxT("JPEG files (*.jpg)|*.jpg|")
226 wxT("GIF files (*.gif)|*.gif|")
229 wxT("TIFF files (*.tif)|*.tif|")
232 wxT("PCX files (*.pcx)|*.pcx|")
234 wxT("ICO files (*.ico)|*.ico|")
235 wxT("CUR files (*.cur)|*.cur"),
239 if ( savefilename
.empty() )
243 wxFileName::SplitPath(savefilename
, NULL
, NULL
, &extension
);
246 if ( extension
== wxT("bmp") )
248 static const int bppvalues
[] =
260 const wxString bppchoices
[] =
266 wxT("8 bpp greyscale"),
268 wxT("8 bpp own palette"),
272 int bppselection
= wxGetSingleChoiceIndex(wxT("Set BMP BPP"),
273 wxT("Image sample: save file"),
274 WXSIZEOF(bppchoices
),
277 if ( bppselection
!= -1 )
279 int format
= bppvalues
[bppselection
];
280 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, format
);
282 if ( format
== wxBMP_8BPP_PALETTE
)
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
));
294 else if ( extension
== wxT("png") )
296 static const int pngvalues
[] =
306 const wxString pngchoices
[] =
312 wxT("Grey red 8bpp"),
313 wxT("Grey red 16bpp"),
316 int sel
= wxGetSingleChoiceIndex(wxT("Set PNG format"),
317 wxT("Image sample: save file"),
318 WXSIZEOF(pngchoices
),
323 image
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, pngvalues
[sel
]);
324 image
.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH
, sel
% 2 ? 16 : 8);
326 // these values are taken from OptiPNG with -o3 switch
327 const wxString compressionChoices
[] =
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"),
343 int sel
= wxGetSingleChoiceIndex(wxT("Select compression option (Cancel to use default)\n"),
344 wxT("PNG Compression Options"),
345 WXSIZEOF(compressionChoices
),
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};
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
364 #endif // wxUSE_LIBPNG
365 else if ( extension
== wxT("cur") )
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
);
377 // This one guesses image format from filename extension
378 // (it may fail if the extension is not recognized):
379 image
.SaveFile(savefilename
);
381 #endif // wxUSE_FILEDLG
384 void OnResize(wxCommandEvent
& WXUNUSED(event
))
386 wxImage
img(m_bitmap
.ConvertToImage());
388 const wxSize size
= GetClientSize();
389 img
.Rescale(size
.x
, size
.y
, wxIMAGE_QUALITY_HIGH
);
390 m_bitmap
= wxBitmap(img
);
395 void OnZoom(wxCommandEvent
& event
)
397 if ( event
.GetId() == wxID_ZOOM_IN
)
399 else if ( event
.GetId() == wxID_ZOOM_OUT
)
401 else // wxID_ZOOM_100
407 void OnRotate(wxCommandEvent
& event
)
410 if ( event
.GetId() == ID_ROTATE_LEFT
)
413 wxImage
img(m_bitmap
.ConvertToImage());
414 img
= img
.Rotate(angle
, wxPoint(img
.GetWidth() / 2, img
.GetHeight() / 2));
417 wxLogWarning(wxT("Rotation failed"));
421 m_bitmap
= wxBitmap(img
);
426 void UpdateStatusBar()
428 wxLogStatus(this, wxT("Image size: (%d, %d), zoom %.2f"),
430 m_bitmap
.GetHeight(),
438 DECLARE_EVENT_TABLE()
441 #ifdef wxHAVE_RAW_BITMAP
443 #include "wx/rawbmp.h"
445 class MyRawBitmapFrame
: public wxFrame
452 REAL_SIZE
= SIZE
- 2*BORDER
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)
460 SetClientSize(SIZE
, SIZE
*2+25);
467 void InitAlphaBitmap()
469 // First, clear the whole bitmap by making it alpha
471 wxAlphaPixelData
data( m_alphaBitmap
, wxPoint(0,0), wxSize(SIZE
, SIZE
) );
474 wxLogError(wxT("Failed to gain raw access to bitmap data"));
477 wxAlphaPixelData::Iterator
p(data
);
478 for ( int y
= 0; y
< SIZE
; ++y
)
480 wxAlphaPixelData::Iterator rowStart
= p
;
481 for ( int x
= 0; x
< SIZE
; ++x
)
484 ++p
; // same as p.OffsetX(1)
491 // Then, draw colourful alpha-blended stripes
492 wxAlphaPixelData
data(m_alphaBitmap
, wxPoint(BORDER
, BORDER
),
493 wxSize(REAL_SIZE
, REAL_SIZE
));
496 wxLogError(wxT("Failed to gain raw access to bitmap data"));
500 wxAlphaPixelData::Iterator
p(data
);
502 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
504 wxAlphaPixelData::Iterator rowStart
= p
;
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;
510 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
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;
519 ++p
; // same as p.OffsetX(1)
529 // draw some colourful stripes without alpha
530 wxNativePixelData
data(m_bitmap
);
533 wxLogError(wxT("Failed to gain raw access to bitmap data"));
537 wxNativePixelData::Iterator
p(data
);
538 for ( int y
= 0; y
< SIZE
; ++y
)
540 wxNativePixelData::Iterator rowStart
= p
;
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;
546 for ( int x
= 0; x
< SIZE
; ++x
)
551 ++p
; // same as p.OffsetX(1)
559 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
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 */ );
567 dc
.DrawText(wxT("Raw bitmap access without alpha"), 0, SIZE
+5);
568 dc
.DrawBitmap( m_bitmap
, 0, SIZE
+5+dc
.GetCharHeight());
573 wxBitmap m_alphaBitmap
;
575 DECLARE_EVENT_TABLE()
578 #endif // wxHAVE_RAW_BITMAP
581 // ============================================================================
583 // ============================================================================
585 //-----------------------------------------------------------------------------
587 //-----------------------------------------------------------------------------
589 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
590 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
591 EVT_PAINT(MyImageFrame::OnPaint
)
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
)
597 EVT_MENU(wxID_ZOOM_IN
, MyImageFrame::OnZoom
)
598 EVT_MENU(wxID_ZOOM_OUT
, MyImageFrame::OnZoom
)
599 EVT_MENU(wxID_ZOOM_100
, MyImageFrame::OnZoom
)
602 //-----------------------------------------------------------------------------
604 //-----------------------------------------------------------------------------
606 #ifdef wxHAVE_RAW_BITMAP
608 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
609 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
612 #endif // wxHAVE_RAW_BITMAP
614 //-----------------------------------------------------------------------------
616 //-----------------------------------------------------------------------------
621 ID_ABOUT
= wxID_ABOUT
,
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
)
639 #if wxUSE_GRAPHICS_CONTEXT
640 EVT_MENU (ID_GRAPHICS
, MyFrame::OnTestGraphics
)
641 #endif // wxUSE_GRAPHICS_CONTEXT
643 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
644 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
645 #endif // wxUSE_CLIPBOARD
649 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, wxT("wxImage sample"),
650 wxPoint(20, 20), wxSize(950, 700) )
652 SetIcon(wxICON(sample
));
654 wxMenuBar
*menu_bar
= new wxMenuBar();
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"));
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"));
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
683 SetMenuBar( menu_bar
);
687 int widths
[] = { -1, 100 };
688 SetStatusWidths( 2, widths
);
689 #endif // wxUSE_STATUSBAR
691 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
693 // 500 width * 2750 height
694 m_canvas
->SetScrollbars( 10, 10, 50, 275 );
695 m_canvas
->SetCursor(wxImage("cursor.png"));
698 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
703 #if wxUSE_ZLIB && wxUSE_STREAMS
704 #include "wx/zstream.h"
707 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
711 array
.Add("wxImage demo");
712 array
.Add("(c) Robert Roebling 1998-2005");
713 array
.Add("(c) Vadim Zeitlin 2005-2009");
715 array
.Add(wxEmptyString
);
716 array
.Add("Version of the libraries used:");
719 array
.Add(wxPNGHandler::GetLibraryVersionInfo().ToString());
722 array
.Add(wxJPEGHandler::GetLibraryVersionInfo().ToString());
725 array
.Add(wxTIFFHandler::GetLibraryVersionInfo().ToString());
727 #if wxUSE_ZLIB && wxUSE_STREAMS
728 // zlib is used by libpng
729 array
.Add(wxGetZlibVersionInfo().ToString());
731 (void)wxMessageBox( wxJoin(array
, '\n'),
732 "About wxImage Demo",
733 wxICON_INFORMATION
| wxOK
);
736 wxString
MyFrame::LoadUserImage(wxImage
& image
)
741 filename
= wxLoadFileSelector(wxT("image"), wxEmptyString
);
742 if ( !filename
.empty() )
744 if ( !image
.LoadFile(filename
) )
746 wxLogError(wxT("Couldn't load image from '%s'."), filename
.c_str());
748 return wxEmptyString
;
751 #endif // wxUSE_FILEDLG
756 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
759 wxString filename
= LoadUserImage(image
);
760 if ( !filename
.empty() )
761 new MyImageFrame(this, filename
, image
);
764 void MyFrame::OnImageInfo( wxCommandEvent
&WXUNUSED(event
) )
767 if ( !LoadUserImage(image
).empty() )
769 // TODO: show more information about the file
770 wxString info
= wxString::Format("Image size: %dx%d",
774 int xres
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
),
775 yres
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
778 info
+= wxString::Format("\nResolution: %dx%d", xres
, yres
);
779 switch ( image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
) )
782 wxFAIL_MSG( "unknown image resolution units" );
785 case wxIMAGE_RESOLUTION_NONE
:
786 info
+= " in default units";
789 case wxIMAGE_RESOLUTION_INCHES
:
793 case wxIMAGE_RESOLUTION_CM
:
799 wxLogMessage("%s", info
);
803 #ifdef wxHAVE_RAW_BITMAP
805 void MyFrame::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
807 (new MyRawBitmapFrame(this))->Show();
810 #endif // wxHAVE_RAW_BITMAP
812 #if wxUSE_GRAPHICS_CONTEXT
814 class MyGraphicsFrame
: public wxFrame
823 MyGraphicsFrame(wxWindow
* parent
) :
824 wxFrame(parent
, wxID_ANY
, "Graphics context test"),
825 m_image(WIDTH
, HEIGHT
, false)
827 // Create a test image: it has 3 horizontal primary colour bands with
828 // alpha increasing from left to right.
830 unsigned char* alpha
= m_image
.GetAlpha();
831 unsigned char* data
= m_image
.GetData();
833 for ( int y
= 0; y
< HEIGHT
; y
++ )
840 else if ( y
< (2*HEIGHT
)/3 )
845 for ( int x
= 0; x
< WIDTH
; x
++ )
854 m_bitmap
= wxBitmap(m_image
);
856 Connect(wxEVT_PAINT
, wxPaintEventHandler(MyGraphicsFrame::OnPaint
));
862 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
865 wxScopedPtr
<wxGraphicsContext
> gc(wxGraphicsContext::Create(dc
));
866 wxGraphicsBitmap
gb(gc
->CreateBitmapFromImage(m_image
));
868 gc
->SetFont(*wxNORMAL_FONT
, *wxBLACK
);
869 gc
->DrawText("Bitmap", 0, HEIGHT
/2);
870 gc
->DrawBitmap(m_bitmap
, 0, 0, WIDTH
, HEIGHT
);
872 wxGraphicsFont gf
= gc
->CreateFont(wxNORMAL_FONT
->GetPixelSize().y
, "");
874 gc
->DrawText("Graphics bitmap", 0, (3*HEIGHT
)/2);
875 gc
->DrawBitmap(gb
, 0, HEIGHT
, WIDTH
, HEIGHT
);
881 wxDECLARE_NO_COPY_CLASS(MyGraphicsFrame
);
884 void MyFrame::OnTestGraphics(wxCommandEvent
& WXUNUSED(event
))
886 new MyGraphicsFrame(this);
889 #endif // wxUSE_GRAPHICS_CONTEXT
893 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
895 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
896 dobjBmp
->SetBitmap(m_canvas
->my_horse_png
);
898 wxTheClipboard
->Open();
900 if ( !wxTheClipboard
->SetData(dobjBmp
) )
902 wxLogError(wxT("Failed to copy bitmap to clipboard"));
905 wxTheClipboard
->Close();
908 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
910 wxBitmapDataObject dobjBmp
;
912 wxTheClipboard
->Open();
913 if ( !wxTheClipboard
->GetData(dobjBmp
) )
915 wxLogMessage(wxT("No bitmap data in the clipboard"));
919 new MyImageFrame(this, wxT("Clipboard"), dobjBmp
.GetBitmap());
921 wxTheClipboard
->Close();
924 #endif // wxUSE_CLIPBOARD
926 void MyFrame::OnThumbnail( wxCommandEvent
&WXUNUSED(event
) )
929 wxString filename
= wxLoadFileSelector(wxT("image"), wxEmptyString
, wxEmptyString
, this);
930 if ( filename
.empty() )
933 static const int THUMBNAIL_WIDTH
= 320;
934 static const int THUMBNAIL_HEIGHT
= 240;
937 image
.SetOption(wxIMAGE_OPTION_MAX_WIDTH
, THUMBNAIL_WIDTH
);
938 image
.SetOption(wxIMAGE_OPTION_MAX_HEIGHT
, THUMBNAIL_HEIGHT
);
941 if ( !image
.LoadFile(filename
) )
943 wxLogError(wxT("Couldn't load image from '%s'."), filename
.c_str());
947 int origWidth
= image
.GetOptionInt( wxIMAGE_OPTION_ORIGINAL_WIDTH
);
948 int origHeight
= image
.GetOptionInt( wxIMAGE_OPTION_ORIGINAL_HEIGHT
);
950 const long loadTime
= sw
.Time();
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
);
956 wxLogError( wxT("Couldn't create file selector dialog") );
958 #endif // wxUSE_FILEDLG
961 //-----------------------------------------------------------------------------
963 //-----------------------------------------------------------------------------
969 if ( !wxApp::OnInit() )
972 wxInitAllImageHandlers();
974 wxFrame
*frame
= new MyFrame();