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 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.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"
32 #include "wx/dataobj.h"
33 #include "wx/clipbrd.h"
34 #endif // wxUSE_CLIPBOARD
36 #if defined(__WXMSW__)
37 #ifdef wxHAVE_RAW_BITMAP
38 #include "wx/rawbmp.h"
42 #if defined(__WXMAC__) || defined(__WXGTK__)
43 #define wxHAVE_RAW_BITMAP
44 #include "wx/rawbmp.h"
50 #include "../sample.xpm"
53 // ============================================================================
55 // ============================================================================
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 class MyApp
: public wxApp
64 virtual bool OnInit();
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 class MyFrame
: public wxFrame
76 void OnAbout( wxCommandEvent
&event
);
77 void OnNewFrame( wxCommandEvent
&event
);
78 void OnImageInfo( wxCommandEvent
&event
);
79 void OnThumbnail( wxCommandEvent
&event
);
81 #ifdef wxHAVE_RAW_BITMAP
82 void OnTestRawBitmap( wxCommandEvent
&event
);
83 #endif // wxHAVE_RAW_BITMAP
84 void OnQuit( wxCommandEvent
&event
);
87 void OnCopy(wxCommandEvent
& event
);
88 void OnPaste(wxCommandEvent
& event
);
89 #endif // wxUSE_CLIPBOARD
94 // ask user for the file name and try to load an image from it
96 // return the file path on success, empty string if we failed to load the
97 // image or were cancelled by user
98 static wxString
LoadUserImage(wxImage
& image
);
101 DECLARE_DYNAMIC_CLASS(MyFrame
)
102 DECLARE_EVENT_TABLE()
105 // ----------------------------------------------------------------------------
106 // Frame used for showing a standalone image
107 // ----------------------------------------------------------------------------
111 ID_ROTATE_LEFT
= wxID_HIGHEST
+1,
117 class MyImageFrame
: public wxFrame
120 MyImageFrame(wxFrame
*parent
, const wxString
& desc
, const wxImage
& image
)
122 Create(parent
, desc
, wxBitmap(image
), image
.GetImageCount(desc
));
125 MyImageFrame(wxFrame
*parent
, const wxString
& desc
, const wxBitmap
& bitmap
)
127 Create(parent
, desc
, bitmap
);
130 bool Create(wxFrame
*parent
,
131 const wxString
& desc
,
132 const wxBitmap
& bitmap
,
135 if ( !wxFrame
::Create(parent
, wxID_ANY
,
136 wxString
::Format(_T("Image from %s"), desc
),
137 wxDefaultPosition
, wxDefaultSize
,
138 wxDEFAULT_FRAME_STYLE
| wxFULL_REPAINT_ON_RESIZE
) )
143 wxMenu
*menu
= new wxMenu
;
144 menu
->Append(wxID_SAVE
);
145 menu
->AppendSeparator();
146 m_pClearBgMenu
= menu
->AppendCheckItem(ID_PAINT_BG
, _T("&Paint background"));
147 menu
->AppendSeparator();
148 menu
->Append(ID_RESIZE
, _T("&Fit to window\tCtrl-F"));
149 menu
->AppendSeparator();
150 menu
->Append(ID_ROTATE_LEFT
, _T("Rotate &left\tCtrl-L"));
151 menu
->Append(ID_ROTATE_RIGHT
, _T("Rotate &right\tCtrl-R"));
153 wxMenuBar
*mbar
= new wxMenuBar
;
154 mbar
->Append(menu
, _T("&Image"));
158 if ( numImages
!= 1 )
159 SetStatusText(wxString
::Format("%d images", numImages
), 1);
161 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
170 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
172 // do nothing here to be able to see how transparent images are shown
175 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
179 if (m_pClearBgMenu
->IsChecked())
182 const wxSize size
= GetClientSize();
183 dc
.DrawBitmap(m_bitmap
,
184 (size
.x
- m_bitmap
.GetWidth())/2,
185 (size
.y
- m_bitmap
.GetHeight())/2,
186 true /* use mask */);
189 void OnSave(wxCommandEvent
& WXUNUSED(event
))
192 wxImage image
= m_bitmap
.ConvertToImage();
194 wxString savefilename
= wxFileSelector( wxT("Save Image"),
197 (const wxChar
*)NULL
,
198 wxT("BMP files (*.bmp)|*.bmp|")
199 wxT("PNG files (*.png)|*.png|")
200 wxT("JPEG files (*.jpg)|*.jpg|")
201 wxT("GIF files (*.gif)|*.gif|")
202 wxT("TIFF files (*.tif)|*.tif|")
203 wxT("PCX files (*.pcx)|*.pcx|")
204 wxT("ICO files (*.ico)|*.ico|")
205 wxT("CUR files (*.cur)|*.cur"),
209 if ( savefilename
.empty() )
213 wxFileName
::SplitPath(savefilename
, NULL
, NULL
, &extension
);
216 if ( extension
== _T("bmp") )
218 static const int bppvalues
[] =
230 const wxString bppchoices
[] =
236 _T("8 bpp greyscale"),
238 _T("8 bpp own palette"),
242 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
243 _T("Image sample: save file"),
244 WXSIZEOF(bppchoices
),
247 if ( bppselection
!= -1 )
249 int format
= bppvalues
[bppselection
];
250 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, format
);
252 if ( format
== wxBMP_8BPP_PALETTE
)
254 unsigned char *cmap
= new unsigned char [256];
255 for ( int i
= 0; i
< 256; i
++ )
256 cmap
[i
] = (unsigned char)i
;
257 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
263 else if ( extension
== _T("png") )
265 static const int pngvalues
[] =
275 const wxString pngchoices
[] =
282 _T("Grey red 16bpp"),
285 int sel
= wxGetSingleChoiceIndex(_T("Set PNG format"),
286 _T("Image sample: save file"),
287 WXSIZEOF(pngchoices
),
292 image
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, pngvalues
[sel
]);
293 image
.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH
, sel
% 2 ?
16 : 8);
295 // these values are taken from OptiPNG with -o3 switch
296 const wxString compressionChoices
[] =
298 _T("compression = 9, memory = 8, strategy = 0, filter = 0"),
299 _T("compression = 9, memory = 9, strategy = 0, filter = 0"),
300 _T("compression = 9, memory = 8, strategy = 1, filter = 0"),
301 _T("compression = 9, memory = 9, strategy = 1, filter = 0"),
302 _T("compression = 1, memory = 8, strategy = 2, filter = 0"),
303 _T("compression = 1, memory = 9, strategy = 2, filter = 0"),
304 _T("compression = 9, memory = 8, strategy = 0, filter = 5"),
305 _T("compression = 9, memory = 9, strategy = 0, filter = 5"),
306 _T("compression = 9, memory = 8, strategy = 1, filter = 5"),
307 _T("compression = 9, memory = 9, strategy = 1, filter = 5"),
308 _T("compression = 1, memory = 8, strategy = 2, filter = 5"),
309 _T("compression = 1, memory = 9, strategy = 2, filter = 5"),
312 int sel
= wxGetSingleChoiceIndex(_T("Select compression option (Cancel to use default)\n"),
313 _T("PNG Compression Options"),
314 WXSIZEOF(compressionChoices
),
319 const int zc
[] = {9, 9, 9, 9, 1, 1, 9, 9, 9, 9, 1, 1};
320 const int zm
[] = {8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9};
321 const int zs
[] = {0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2};
322 const int f
[] = {0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
323 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8};
325 image
.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL
, zc
[sel
]);
326 image
.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL
, zm
[sel
]);
327 image
.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY
, zs
[sel
]);
328 image
.SetOption(wxIMAGE_OPTION_PNG_FILTER
, f
[sel
]);
329 image
.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE
, 1048576); // 1 MB
333 else if ( extension
== _T("cur") )
335 image
.Rescale(32,32);
336 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
337 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
338 // This shows how you can save an image with explicitly
339 // specified image format:
340 saved
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
345 // This one guesses image format from filename extension
346 // (it may fail if the extension is not recognized):
347 image
.SaveFile(savefilename
);
349 #endif // wxUSE_FILEDLG
352 void OnResize(wxCommandEvent
& WXUNUSED(event
))
354 wxImage
img(m_bitmap
.ConvertToImage());
356 const wxSize size
= GetClientSize();
357 img
.Rescale(size
.x
, size
.y
, wxIMAGE_QUALITY_HIGH
);
358 m_bitmap
= wxBitmap(img
);
364 void OnRotate(wxCommandEvent
& event
)
367 if ( event
.GetId() == ID_ROTATE_LEFT
)
370 wxImage
img(m_bitmap
.ConvertToImage());
371 img
= img
.Rotate(angle
, wxPoint(img
.GetWidth() / 2, img
.GetHeight() / 2));
374 wxLogWarning(_T("Rotation failed"));
378 m_bitmap
= wxBitmap(img
);
385 void UpdateStatusBar()
387 wxLogStatus(this, _T("Image size: (%d, %d)"),
389 m_bitmap
.GetHeight());
393 wxMenuItem
* m_pClearBgMenu
;
395 DECLARE_EVENT_TABLE()
398 #ifdef wxHAVE_RAW_BITMAP
400 #include "wx/rawbmp.h"
402 class MyRawBitmapFrame
: public wxFrame
409 REAL_SIZE
= SIZE
- 2*BORDER
412 MyRawBitmapFrame(wxFrame
*parent
)
413 : wxFrame(parent
, wxID_ANY
, _T("Raw bitmaps (how exciting)")),
414 m_bitmap(SIZE
, SIZE
, 24),
415 m_alphaBitmap(SIZE
, SIZE
, 32)
417 SetClientSize(SIZE
, SIZE
*2+25);
424 void InitAlphaBitmap()
426 // First, clear the whole bitmap by making it alpha
428 wxAlphaPixelData
data( m_alphaBitmap
, wxPoint(0,0), wxSize(SIZE
, SIZE
) );
431 wxLogError(_T("Failed to gain raw access to bitmap data"));
434 wxAlphaPixelData
::Iterator
p(data
);
435 for ( int y
= 0; y
< SIZE
; ++y
)
437 wxAlphaPixelData
::Iterator rowStart
= p
;
438 for ( int x
= 0; x
< SIZE
; ++x
)
441 ++p
; // same as p.OffsetX(1)
448 // Then, draw colourful alpha-blended stripes
449 wxAlphaPixelData
data(m_alphaBitmap
, wxPoint(BORDER
, BORDER
),
450 wxSize(REAL_SIZE
, REAL_SIZE
));
453 wxLogError(_T("Failed to gain raw access to bitmap data"));
457 wxAlphaPixelData
::Iterator
p(data
);
459 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
461 wxAlphaPixelData
::Iterator rowStart
= p
;
463 int r
= y
< REAL_SIZE
/3 ?
255 : 0,
464 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ?
255 : 0,
465 b
= 2*(REAL_SIZE
/3) <= y ?
255 : 0;
467 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
469 // note that RGB must be premultiplied by alpha
470 unsigned a
= (wxAlphaPixelData
::Iterator
::ChannelType
)((x
*255.)/REAL_SIZE
);
471 p
.Red() = r
* a
/ 256;
472 p
.Green() = g
* a
/ 256;
473 p
.Blue() = b
* a
/ 256;
476 ++p
; // same as p.OffsetX(1)
486 // draw some colourful stripes without alpha
487 wxNativePixelData
data(m_bitmap
);
490 wxLogError(_T("Failed to gain raw access to bitmap data"));
494 wxNativePixelData
::Iterator
p(data
);
495 for ( int y
= 0; y
< SIZE
; ++y
)
497 wxNativePixelData
::Iterator rowStart
= p
;
499 int r
= y
< SIZE
/3 ?
255 : 0,
500 g
= (SIZE
/3 <= y
) && (y
< 2*(SIZE
/3)) ?
255 : 0,
501 b
= 2*(SIZE
/3) <= y ?
255 : 0;
503 for ( int x
= 0; x
< SIZE
; ++x
)
508 ++p
; // same as p.OffsetX(1)
516 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
518 wxPaintDC
dc( this );
519 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER
);
520 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
521 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
522 dc
.DrawBitmap( m_alphaBitmap
, 0, 0, true /* use mask */ );
524 dc
.DrawText(_T("Raw bitmap access without alpha"), 0, SIZE
+5);
525 dc
.DrawBitmap( m_bitmap
, 0, SIZE
+5+dc
.GetCharHeight());
530 wxBitmap m_alphaBitmap
;
532 DECLARE_EVENT_TABLE()
535 #endif // wxHAVE_RAW_BITMAP
538 // ============================================================================
540 // ============================================================================
542 //-----------------------------------------------------------------------------
544 //-----------------------------------------------------------------------------
546 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
547 EVT_ERASE_BACKGROUND(MyImageFrame
::OnEraseBackground
)
548 EVT_PAINT(MyImageFrame
::OnPaint
)
550 EVT_MENU(wxID_SAVE
, MyImageFrame
::OnSave
)
551 EVT_MENU_RANGE(ID_ROTATE_LEFT
, ID_ROTATE_RIGHT
, MyImageFrame
::OnRotate
)
552 EVT_MENU(ID_RESIZE
, MyImageFrame
::OnResize
)
555 //-----------------------------------------------------------------------------
557 //-----------------------------------------------------------------------------
559 #ifdef wxHAVE_RAW_BITMAP
561 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
562 EVT_PAINT(MyRawBitmapFrame
::OnPaint
)
565 #endif // wxHAVE_RAW_BITMAP
567 //-----------------------------------------------------------------------------
569 //-----------------------------------------------------------------------------
574 ID_ABOUT
= wxID_ABOUT
,
581 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
582 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
583 EVT_MENU (ID_ABOUT
, MyFrame
::OnAbout
)
584 EVT_MENU (ID_QUIT
, MyFrame
::OnQuit
)
585 EVT_MENU (ID_NEW
, MyFrame
::OnNewFrame
)
586 EVT_MENU (ID_INFO
, MyFrame
::OnImageInfo
)
587 EVT_MENU (ID_SHOWTHUMBNAIL
, MyFrame
::OnThumbnail
)
588 #ifdef wxHAVE_RAW_BITMAP
589 EVT_MENU (ID_SHOWRAW
, MyFrame
::OnTestRawBitmap
)
592 EVT_MENU(wxID_COPY
, MyFrame
::OnCopy
)
593 EVT_MENU(wxID_PASTE
, MyFrame
::OnPaste
)
594 #endif // wxUSE_CLIPBOARD
598 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxImage sample"),
599 wxPoint(20, 20), wxSize(950, 700) )
601 SetIcon(wxICON(sample
));
603 wxMenuBar
*menu_bar
= new wxMenuBar();
605 wxMenu
*menuImage
= new wxMenu
;
606 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
607 menuImage
->Append( ID_INFO
, _T("Show image &information...\tCtrl-I"));
608 #ifdef wxHAVE_RAW_BITMAP
609 menuImage
->AppendSeparator();
610 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
612 menuImage
->AppendSeparator();
613 menuImage
->Append( ID_SHOWTHUMBNAIL
, _T("Test &thumbnail...\tCtrl-T"),
614 "Test scaling the image during load (try with JPEG)");
615 menuImage
->AppendSeparator();
616 menuImage
->Append( ID_ABOUT
, _T("&About..."));
617 menuImage
->AppendSeparator();
618 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
619 menu_bar
->Append(menuImage
, _T("&Image"));
622 wxMenu
*menuClipboard
= new wxMenu
;
623 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
624 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
625 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
626 #endif // wxUSE_CLIPBOARD
628 SetMenuBar( menu_bar
);
632 int widths
[] = { -1, 100 };
633 SetStatusWidths( 2, widths
);
634 #endif // wxUSE_STATUSBAR
636 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
638 // 500 width * 2750 height
639 m_canvas
->SetScrollbars( 10, 10, 50, 275 );
642 void MyFrame
::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
647 void MyFrame
::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
649 (void)wxMessageBox( _T("wxImage demo\n")
650 _T("Robert Roebling (c) 1998,2000"),
651 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
654 wxString MyFrame
::LoadUserImage(wxImage
& image
)
659 filename
= wxFileSelector(_T("Select image file"));
660 if ( !filename
.empty() )
662 if ( !image
.LoadFile(filename
) )
664 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
666 return wxEmptyString
;
669 #endif // wxUSE_FILEDLG
674 void MyFrame
::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
677 wxString filename
= LoadUserImage(image
);
678 if ( !filename
.empty() )
679 new MyImageFrame(this, filename
, image
);
682 void MyFrame
::OnImageInfo( wxCommandEvent
&WXUNUSED(event
) )
685 if ( !LoadUserImage(image
).empty() )
687 // TODO: show more information about the file
688 wxString info
= wxString
::Format("Image size: %dx%d",
692 int xres
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
),
693 yres
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
696 info
+= wxString
::Format("\nResolution: %dx%d", xres
, yres
);
697 switch ( image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
) )
700 wxFAIL_MSG( "unknown image resolution units" );
703 case wxIMAGE_RESOLUTION_NONE
:
704 info
+= " in default units";
707 case wxIMAGE_RESOLUTION_INCHES
:
711 case wxIMAGE_RESOLUTION_CM
:
717 wxLogMessage("%s", info
);
721 #ifdef wxHAVE_RAW_BITMAP
723 void MyFrame
::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
725 (new MyRawBitmapFrame(this))->Show();
728 #endif // wxHAVE_RAW_BITMAP
732 void MyFrame
::OnCopy(wxCommandEvent
& WXUNUSED(event
))
734 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
735 dobjBmp
->SetBitmap(m_canvas
->my_horse_png
);
737 wxTheClipboard
->Open();
739 if ( !wxTheClipboard
->SetData(dobjBmp
) )
741 wxLogError(_T("Failed to copy bitmap to clipboard"));
744 wxTheClipboard
->Close();
747 void MyFrame
::OnPaste(wxCommandEvent
& WXUNUSED(event
))
749 wxBitmapDataObject dobjBmp
;
751 wxTheClipboard
->Open();
752 if ( !wxTheClipboard
->GetData(dobjBmp
) )
754 wxLogMessage(_T("No bitmap data in the clipboard"));
758 new MyImageFrame(this, _T("Clipboard"), dobjBmp
.GetBitmap());
760 wxTheClipboard
->Close();
763 #endif // wxUSE_CLIPBOARD
765 void MyFrame
::OnThumbnail( wxCommandEvent
&WXUNUSED(event
) )
768 wxString filename
= wxFileSelector(_T("Select image file"));
769 if ( filename
.empty() )
772 static const int THUMBNAIL_WIDTH
= 320;
773 static const int THUMBNAIL_HEIGHT
= 240;
776 image
.SetOption(wxIMAGE_OPTION_MAX_WIDTH
, THUMBNAIL_WIDTH
);
777 image
.SetOption(wxIMAGE_OPTION_MAX_HEIGHT
, THUMBNAIL_HEIGHT
);
780 if ( !image
.LoadFile(filename
) )
782 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
786 const long loadTime
= sw
.Time();
788 MyImageFrame
* const frame
= new MyImageFrame(this, filename
, image
);
789 wxLogStatus(frame
, "Loaded \"%s\" in %ldms", filename
, loadTime
);
791 wxLogError( _T("Couldn't create file selector dialog") );
793 #endif // wxUSE_FILEDLG
796 //-----------------------------------------------------------------------------
798 //-----------------------------------------------------------------------------
804 if ( !wxApp
::OnInit() )
807 wxInitAllImageHandlers();
809 wxFrame
*frame
= new MyFrame();