1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: samples/image/image.cpp
3 // Purpose: sample showing operations with wxImage
4 // Author: Robert Roebling
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
39 #if defined(__WXMSW__)
40 #ifdef wxHAVE_RAW_BITMAP
41 #include "wx/rawbmp.h"
45 #if defined(__WXMAC__) || defined(__WXGTK__)
46 #define wxHAVE_RAW_BITMAP
47 #include "wx/rawbmp.h"
57 class MyCanvas
: public wxScrolledWindow
60 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
62 void OnPaint( wxPaintEvent
&event
);
63 void CreateAntiAliasedBitmap();
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
;
80 wxBitmap my_smile_xbm
;
84 wxBitmap my_horse_asciigrey_pnm
;
85 wxBitmap my_horse_rawgrey_pnm
;
87 wxBitmap colorized_horse_jpeg
;
88 wxBitmap my_cmyk_jpeg
;
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
;
102 wxBitmap
*my_horse_ani
;
105 wxBitmap m_bmpSmileXpm
;
106 wxIcon m_iconSmileXpm
;
108 DECLARE_EVENT_TABLE()
115 class MyFrame
: public wxFrame
120 void OnAbout( wxCommandEvent
&event
);
121 void OnNewFrame( wxCommandEvent
&event
);
122 void OnImageInfo( wxCommandEvent
&event
);
123 void OnThumbnail( wxCommandEvent
&event
);
125 #ifdef wxHAVE_RAW_BITMAP
126 void OnTestRawBitmap( wxCommandEvent
&event
);
127 #endif // wxHAVE_RAW_BITMAP
128 void OnQuit( wxCommandEvent
&event
);
131 void OnCopy(wxCommandEvent
& event
);
132 void OnPaste(wxCommandEvent
& event
);
133 #endif // wxUSE_CLIPBOARD
138 // ask user for the file name and try to load an image from it
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
);
145 DECLARE_DYNAMIC_CLASS(MyFrame
)
146 DECLARE_EVENT_TABLE()
149 // ----------------------------------------------------------------------------
150 // Frame used for showing a standalone image
151 // ----------------------------------------------------------------------------
155 ID_ROTATE_LEFT
= 100,
160 class MyImageFrame
: public wxFrame
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
),
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"));
178 wxMenuBar
*mbar
= new wxMenuBar
;
179 mbar
->Append(menu
, _T("&Image"));
184 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
189 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
191 // do nothing here to be able to see how transparent images are shown
194 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
197 const wxSize size
= GetClientSize();
198 dc
.DrawBitmap(m_bitmap
,
199 (size
.x
- m_bitmap
.GetWidth())/2,
200 (size
.y
- m_bitmap
.GetHeight())/2,
201 true /* use mask */);
204 void OnSave(wxCommandEvent
& WXUNUSED(event
))
207 wxImage image
= m_bitmap
.ConvertToImage();
209 wxString savefilename
= wxFileSelector( wxT("Save Image"),
212 (const wxChar
*)NULL
,
213 wxT("BMP files (*.bmp)|*.bmp|")
214 wxT("PNG files (*.png)|*.png|")
215 wxT("JPEG files (*.jpg)|*.jpg|")
216 wxT("GIF files (*.gif)|*.gif|")
217 wxT("TIFF files (*.tif)|*.tif|")
218 wxT("PCX files (*.pcx)|*.pcx|")
219 wxT("ICO files (*.ico)|*.ico|")
220 wxT("CUR files (*.cur)|*.cur"),
224 if ( savefilename
.empty() )
228 wxFileName::SplitPath(savefilename
, NULL
, NULL
, &extension
);
231 if ( extension
== _T("bmp") )
233 static const int bppvalues
[] =
245 const wxString bppchoices
[] =
251 _T("8 bpp greyscale"),
253 _T("8 bpp own palette"),
257 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
258 _T("Image sample: save file"),
259 WXSIZEOF(bppchoices
),
262 if ( bppselection
!= -1 )
264 int format
= bppvalues
[bppselection
];
265 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, format
);
267 if ( format
== wxBMP_8BPP_PALETTE
)
269 unsigned char *cmap
= new unsigned char [256];
270 for ( int i
= 0; i
< 256; i
++ )
271 cmap
[i
] = (unsigned char)i
;
272 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
278 else if ( extension
== _T("png") )
280 static const int pngvalues
[] =
290 const wxString pngchoices
[] =
297 _T("Grey red 16bpp"),
300 int sel
= wxGetSingleChoiceIndex(_T("Set PNG format"),
301 _T("Image sample: save file"),
302 WXSIZEOF(pngchoices
),
307 image
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, pngvalues
[sel
]);
308 image
.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH
, sel
% 2 ? 16 : 8);
311 else if ( extension
== _T("cur") )
313 image
.Rescale(32,32);
314 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
315 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
316 // This shows how you can save an image with explicitly
317 // specified image format:
318 saved
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
323 // This one guesses image format from filename extension
324 // (it may fail if the extension is not recognized):
325 image
.SaveFile(savefilename
);
327 #endif // wxUSE_FILEDLG
330 void OnResize(wxCommandEvent
& WXUNUSED(event
))
332 wxImage
img(m_bitmap
.ConvertToImage());
334 const wxSize size
= GetClientSize();
335 img
.Rescale(size
.x
, size
.y
, wxIMAGE_QUALITY_HIGH
);
336 m_bitmap
= wxBitmap(img
);
342 void OnRotate(wxCommandEvent
& event
)
345 if ( event
.GetId() == ID_ROTATE_LEFT
)
348 wxImage
img(m_bitmap
.ConvertToImage());
349 img
= img
.Rotate(angle
, wxPoint(img
.GetWidth() / 2, img
.GetHeight() / 2));
352 wxLogWarning(_T("Rotation failed"));
356 m_bitmap
= wxBitmap(img
);
363 void UpdateStatusBar()
365 wxLogStatus(this, _T("Image size: (%d, %d)"),
367 m_bitmap
.GetHeight());
372 DECLARE_EVENT_TABLE()
375 #ifdef wxHAVE_RAW_BITMAP
377 #include "wx/rawbmp.h"
379 class MyRawBitmapFrame
: public wxFrame
386 REAL_SIZE
= SIZE
- 2*BORDER
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)
394 SetClientSize(SIZE
, SIZE
*2+25);
401 void InitAlphaBitmap()
403 // First, clear the whole bitmap by making it alpha
405 wxAlphaPixelData
data( m_alphaBitmap
, wxPoint(0,0), wxSize(SIZE
, SIZE
) );
408 wxLogError(_T("Failed to gain raw access to bitmap data"));
411 wxAlphaPixelData::Iterator
p(data
);
412 for ( int y
= 0; y
< SIZE
; ++y
)
414 wxAlphaPixelData::Iterator rowStart
= p
;
415 for ( int x
= 0; x
< SIZE
; ++x
)
418 ++p
; // same as p.OffsetX(1)
425 // Then, draw colourful alpha-blended stripes
426 wxAlphaPixelData
data(m_alphaBitmap
, wxPoint(BORDER
, BORDER
),
427 wxSize(REAL_SIZE
, REAL_SIZE
));
430 wxLogError(_T("Failed to gain raw access to bitmap data"));
434 wxAlphaPixelData::Iterator
p(data
);
436 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
438 wxAlphaPixelData::Iterator rowStart
= p
;
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;
444 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
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;
453 ++p
; // same as p.OffsetX(1)
463 // draw some colourful stripes without alpha
464 wxNativePixelData
data(m_bitmap
);
467 wxLogError(_T("Failed to gain raw access to bitmap data"));
471 wxNativePixelData::Iterator
p(data
);
472 for ( int y
= 0; y
< SIZE
; ++y
)
474 wxNativePixelData::Iterator rowStart
= p
;
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;
480 for ( int x
= 0; x
< SIZE
; ++x
)
485 ++p
; // same as p.OffsetX(1)
493 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
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 */ );
501 dc
.DrawText(_T("Raw bitmap access without alpha"), 0, SIZE
+5);
502 dc
.DrawBitmap( m_bitmap
, 0, SIZE
+5+dc
.GetCharHeight());
507 wxBitmap m_alphaBitmap
;
509 DECLARE_EVENT_TABLE()
512 #endif // wxHAVE_RAW_BITMAP
516 class MyApp
: public wxApp
519 virtual bool OnInit();
528 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
529 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
530 EVT_PAINT(MyImageFrame::OnPaint
)
532 EVT_MENU(wxID_SAVE
, MyImageFrame::OnSave
)
533 EVT_MENU_RANGE(ID_ROTATE_LEFT
, ID_ROTATE_RIGHT
, MyImageFrame::OnRotate
)
534 EVT_MENU(ID_RESIZE
, MyImageFrame::OnResize
)
537 #ifdef wxHAVE_RAW_BITMAP
539 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
540 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
543 #endif // wxHAVE_RAW_BITMAP
545 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
546 EVT_PAINT(MyCanvas::OnPaint
)
549 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
550 const wxPoint
&pos
, const wxSize
&size
)
551 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
552 , m_bmpSmileXpm(smile_xpm
)
553 , m_iconSmileXpm(smile_xpm
)
558 SetBackgroundColour(* wxWHITE
);
560 wxBitmap
bitmap( 100, 100 );
563 dc
.SelectObject( bitmap
);
564 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
565 dc
.SetPen( *wxBLACK_PEN
);
566 dc
.DrawRectangle( 0, 0, 100, 100 );
567 dc
.SetBrush( *wxWHITE_BRUSH
);
568 dc
.DrawRectangle( 20, 20, 60, 60 );
569 dc
.SelectObject( wxNullBitmap
);
571 // try to find the directory with our images
573 if ( wxFile::Exists(wxT("./horse.png")) )
575 else if ( wxFile::Exists(wxT("../horse.png")) )
578 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
580 wxImage image
= bitmap
.ConvertToImage();
583 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
584 wxLogError(wxT("Can't save file"));
588 if ( image
.LoadFile( dir
+ _T("test.png") ) )
589 my_square
= wxBitmap( image
);
593 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
594 wxLogError(wxT("Can't load PNG image"));
596 my_horse_png
= wxBitmap( image
);
598 if ( !image
.LoadFile( dir
+ _T("toucan.png")) )
599 wxLogError(wxT("Can't load PNG image"));
601 my_toucan
= wxBitmap(image
);
603 my_toucan_flipped_horiz
= wxBitmap(image
.Mirror(true));
604 my_toucan_flipped_vert
= wxBitmap(image
.Mirror(false));
605 my_toucan_flipped_both
= wxBitmap(image
.Mirror(true).Mirror(false));
606 my_toucan_grey
= wxBitmap(image
.ConvertToGreyscale());
607 my_toucan_head
= wxBitmap(image
.GetSubImage(wxRect(40, 7, 80, 60)));
608 my_toucan_scaled_normal
= wxBitmap(image
.Scale(110,90,wxIMAGE_QUALITY_NORMAL
));
609 my_toucan_scaled_high
= wxBitmap(image
.Scale(110,90,wxIMAGE_QUALITY_HIGH
));
610 my_toucan_blur
= wxBitmap(image
.Blur(10));
612 #endif // wxUSE_LIBPNG
617 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
618 wxLogError(wxT("Can't load JPG image"));
621 my_horse_jpeg
= wxBitmap( image
);
623 // Colorize by rotating green hue to red
624 wxImage::HSVValue greenHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0));
625 wxImage::HSVValue redHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0));
626 image
.RotateHue(redHSV
.hue
- greenHSV
.hue
);
627 colorized_horse_jpeg
= wxBitmap( image
);
630 if ( !image
.LoadFile( dir
+ _T("cmyk.jpg")) )
631 wxLogError(_T("Can't load CMYK JPG image"));
633 my_cmyk_jpeg
= wxBitmap(image
);
634 #endif // wxUSE_LIBJPEG
639 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
640 wxLogError(wxT("Can't load GIF image"));
642 my_horse_gif
= wxBitmap( image
);
648 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
649 wxLogError(wxT("Can't load PCX image"));
651 my_horse_pcx
= wxBitmap( image
);
656 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
657 wxLogError(wxT("Can't load BMP image"));
659 my_horse_bmp
= wxBitmap( image
);
664 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
665 wxLogError(wxT("Can't load XPM image"));
667 my_horse_xpm
= wxBitmap( image
);
669 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
670 wxLogError(wxT("Can't save file"));
676 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
677 wxLogError(wxT("Can't load PNM image"));
679 my_horse_pnm
= wxBitmap( image
);
683 if ( !image
.LoadFile( dir
+ _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM
) )
684 wxLogError(wxT("Can't load PNM image"));
686 my_horse_asciigrey_pnm
= wxBitmap( image
);
690 if ( !image
.LoadFile( dir
+ _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM
) )
691 wxLogError(wxT("Can't load PNM image"));
693 my_horse_rawgrey_pnm
= wxBitmap( image
);
699 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
700 wxLogError(wxT("Can't load TIFF image"));
702 my_horse_tiff
= wxBitmap( image
);
708 if ( !image
.LoadFile( dir
+ _T("horse.tga"), wxBITMAP_TYPE_TGA
) )
709 wxLogError(wxT("Can't load TGA image"));
711 my_horse_tga
= wxBitmap( image
);
714 CreateAntiAliasedBitmap();
716 my_smile_xbm
= wxBitmap( (const char*)smile_bits
, smile_width
,
719 // demonstrates XPM automatically using the mask when saving
720 if ( m_bmpSmileXpm
.Ok() )
721 m_bmpSmileXpm
.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM
);
726 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
727 wxLogError(wxT("Can't load first ICO image"));
729 my_horse_ico32
= wxBitmap( image
);
733 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
734 wxLogError(wxT("Can't load second ICO image"));
736 my_horse_ico16
= wxBitmap( image
);
740 if ( !image
.LoadFile( dir
+ _T("horse.ico") ) )
741 wxLogError(wxT("Can't load best ICO image"));
743 my_horse_ico
= wxBitmap( image
);
747 if ( !image
.LoadFile( dir
+ _T("horse.cur"), wxBITMAP_TYPE_CUR
) )
748 wxLogError(wxT("Can't load best ICO image"));
751 my_horse_cur
= wxBitmap( image
);
752 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
753 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
756 m_ani_images
= wxImage::GetImageCount ( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
);
758 wxLogError(wxT("No ANI-format images found"));
760 my_horse_ani
= new wxBitmap
[m_ani_images
];
762 for (i
=0; i
< m_ani_images
; i
++)
765 if (!image
.LoadFile( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
, i
))
767 wxString tmp
= wxT("Can't load image number ");
772 my_horse_ani
[i
] = wxBitmap( image
);
774 #endif // wxUSE_ICO_CUR
778 // test image loading from stream
779 wxFile
file(dir
+ _T("horse.bmp"));
780 if ( file
.IsOpened() )
782 wxFileOffset len
= file
.Length();
783 size_t dataSize
= (size_t)len
;
784 void *data
= malloc(dataSize
);
785 if ( file
.Read(data
, dataSize
) != len
)
786 wxLogError(_T("Reading bitmap file failed"));
789 wxMemoryInputStream
mis(data
, dataSize
);
790 if ( !image
.LoadFile(mis
) )
791 wxLogError(wxT("Can't load BMP image from stream"));
793 my_horse_bmp2
= wxBitmap( image
);
800 MyCanvas::~MyCanvas()
802 delete [] my_horse_ani
;
805 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
807 wxPaintDC
dc( this );
810 dc
.DrawText( _T("Loaded image"), 30, 10 );
812 dc
.DrawBitmap( my_square
, 30, 30 );
814 dc
.DrawText( _T("Drawn directly"), 150, 10 );
815 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
816 dc
.SetPen( *wxBLACK_PEN
);
817 dc
.DrawRectangle( 150, 30, 100, 100 );
818 dc
.SetBrush( *wxWHITE_BRUSH
);
819 dc
.DrawRectangle( 170, 50, 60, 60 );
822 dc
.DrawBitmap( my_anti
, 280, 30 );
824 dc
.DrawText( _T("PNG handler"), 30, 135 );
825 if (my_horse_png
.Ok())
827 dc
.DrawBitmap( my_horse_png
, 30, 150 );
828 wxRect
rect(0,0,100,100);
829 wxBitmap
sub( my_horse_png
.GetSubBitmap(rect
) );
830 dc
.DrawText( _T("GetSubBitmap()"), 280, 175 );
831 dc
.DrawBitmap( sub
, 280, 195 );
834 dc
.DrawText( _T("JPEG handler"), 30, 365 );
835 if (my_horse_jpeg
.Ok())
836 dc
.DrawBitmap( my_horse_jpeg
, 30, 380 );
838 dc
.DrawText( _T("Green rotated to red"), 280, 365 );
839 if (colorized_horse_jpeg
.Ok())
840 dc
.DrawBitmap( colorized_horse_jpeg
, 280, 380 );
842 dc
.DrawText( _T("CMYK JPEG image"), 530, 365 );
843 if (my_cmyk_jpeg
.Ok())
844 dc
.DrawBitmap( my_cmyk_jpeg
, 530, 380 );
846 dc
.DrawText( _T("GIF handler"), 30, 595 );
847 if (my_horse_gif
.Ok())
848 dc
.DrawBitmap( my_horse_gif
, 30, 610 );
850 dc
.DrawText( _T("PCX handler"), 30, 825 );
851 if (my_horse_pcx
.Ok())
852 dc
.DrawBitmap( my_horse_pcx
, 30, 840 );
854 dc
.DrawText( _T("BMP handler"), 30, 1055 );
855 if (my_horse_bmp
.Ok())
856 dc
.DrawBitmap( my_horse_bmp
, 30, 1070 );
858 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
859 if (my_horse_bmp2
.Ok())
860 dc
.DrawBitmap( my_horse_bmp2
, 280, 1070 );
862 dc
.DrawText( _T("PNM handler"), 30, 1285 );
863 if (my_horse_pnm
.Ok())
864 dc
.DrawBitmap( my_horse_pnm
, 30, 1300 );
866 dc
.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 );
867 if (my_horse_asciigrey_pnm
.Ok())
868 dc
.DrawBitmap( my_horse_asciigrey_pnm
, 280, 1300 );
870 dc
.DrawText( _T("PNM handler (raw grey)"), 530, 1285 );
871 if (my_horse_rawgrey_pnm
.Ok())
872 dc
.DrawBitmap( my_horse_rawgrey_pnm
, 530, 1300 );
874 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
875 if (my_horse_tiff
.Ok())
876 dc
.DrawBitmap( my_horse_tiff
, 30, 1530 );
878 dc
.DrawText( _T("TGA handler"), 30, 1745 );
879 if (my_horse_tga
.Ok())
880 dc
.DrawBitmap( my_horse_tga
, 30, 1760 );
882 dc
.DrawText( _T("XPM handler"), 30, 1975 );
883 if (my_horse_xpm
.Ok())
884 dc
.DrawBitmap( my_horse_xpm
, 30, 2000 );
888 int x
= 750, y
= 10, yy
= 170;
890 dc
.DrawText(wxT("Original toucan"), x
+50, y
);
891 dc
.DrawBitmap(my_toucan
, x
, y
+15, true);
893 dc
.DrawText(wxT("Flipped horizontally"), x
+50, y
);
894 dc
.DrawBitmap(my_toucan_flipped_horiz
, x
, y
+15, true);
896 dc
.DrawText(wxT("Flipped vertically"), x
+50, y
);
897 dc
.DrawBitmap(my_toucan_flipped_vert
, x
, y
+15, true);
899 dc
.DrawText(wxT("Flipped both h&v"), x
+50, y
);
900 dc
.DrawBitmap(my_toucan_flipped_both
, x
, y
+15, true);
903 dc
.DrawText(wxT("In greyscale"), x
+50, y
);
904 dc
.DrawBitmap(my_toucan_grey
, x
, y
+15, true);
907 dc
.DrawText(wxT("Toucan's head"), x
+50, y
);
908 dc
.DrawBitmap(my_toucan_head
, x
, y
+15, true);
911 dc
.DrawText(wxT("Scaled with normal quality"), x
+50, y
);
912 dc
.DrawBitmap(my_toucan_scaled_normal
, x
, y
+15, true);
915 dc
.DrawText(wxT("Scaled with high quality"), x
+50, y
);
916 dc
.DrawBitmap(my_toucan_scaled_high
, x
, y
+15, true);
919 dc
.DrawText(wxT("Blured"), x
+50, y
);
920 dc
.DrawBitmap(my_toucan_blur
, x
, y
+15, true);
923 if (my_smile_xbm
.Ok())
925 int x
= 300, y
= 1800;
927 dc
.DrawText( _T("XBM bitmap"), x
, y
);
928 dc
.DrawText( _T("(green on red)"), x
, y
+ 15 );
929 dc
.SetTextForeground( _T("GREEN") );
930 dc
.SetTextBackground( _T("RED") );
931 dc
.DrawBitmap( my_smile_xbm
, x
, y
+ 30 );
933 dc
.SetTextForeground( *wxBLACK
);
934 dc
.DrawText( _T("After wxImage conversion"), x
+ 120, y
);
935 dc
.DrawText( _T("(red on white)"), x
+ 120, y
+ 15 );
936 dc
.SetTextForeground( wxT("RED") );
937 wxImage i
= my_smile_xbm
.ConvertToImage();
938 i
.SetMaskColour( 255, 255, 255 );
940 wxRED_PEN
->GetColour().Red(),
941 wxRED_PEN
->GetColour().Green(),
942 wxRED_PEN
->GetColour().Blue() );
943 dc
.DrawBitmap( wxBitmap(i
), x
+ 120, y
+ 30, true );
944 dc
.SetTextForeground( *wxBLACK
);
948 wxBitmap
mono( 60,50,1 );
950 memdc
.SelectObject( mono
);
951 memdc
.SetPen( *wxBLACK_PEN
);
952 memdc
.SetBrush( *wxWHITE_BRUSH
);
953 memdc
.DrawRectangle( 0,0,60,50 );
954 memdc
.SetTextForeground( *wxBLACK
);
956 // I cannot convince GTK2 to draw into mono bitmaps
957 memdc
.DrawText( _T("Hi!"), 5, 5 );
959 memdc
.SetBrush( *wxBLACK_BRUSH
);
960 memdc
.DrawRectangle( 33,5,20,20 );
961 memdc
.SetPen( *wxRED_PEN
);
962 memdc
.DrawLine( 5, 42, 50, 42 );
963 memdc
.SelectObject( wxNullBitmap
);
967 int x
= 300, y
= 1900;
969 dc
.DrawText( _T("Mono bitmap"), x
, y
);
970 dc
.DrawText( _T("(red on green)"), x
, y
+ 15 );
971 dc
.SetTextForeground( wxT("RED") );
972 dc
.SetTextBackground( wxT("GREEN") );
973 dc
.DrawBitmap( mono
, x
, y
+ 30 );
975 dc
.SetTextForeground( *wxBLACK
);
976 dc
.DrawText( _T("After wxImage conversion"), x
+ 120, y
);
977 dc
.DrawText( _T("(red on white)"), x
+ 120, y
+ 15 );
978 dc
.SetTextForeground( wxT("RED") );
979 wxImage i
= mono
.ConvertToImage();
980 i
.SetMaskColour( 255,255,255 );
982 wxRED_PEN
->GetColour().Red(),
983 wxRED_PEN
->GetColour().Green(),
984 wxRED_PEN
->GetColour().Blue() );
985 dc
.DrawBitmap( wxBitmap(i
), x
+ 120, y
+ 30, true );
986 dc
.SetTextForeground( *wxBLACK
);
989 // For testing transparency
990 dc
.SetBrush( *wxRED_BRUSH
);
991 dc
.DrawRectangle( 20, 2220, 560, 68 );
993 dc
.DrawText(_T("XPM bitmap"), 30, 2230 );
994 if ( m_bmpSmileXpm
.Ok() )
995 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, true);
997 dc
.DrawText(_T("XPM icon"), 110, 2230 );
998 if ( m_iconSmileXpm
.Ok() )
999 dc
.DrawIcon(m_iconSmileXpm
, 110, 2250);
1001 // testing icon -> bitmap conversion
1002 wxBitmap
to_blit( m_iconSmileXpm
);
1005 dc
.DrawText( _T("SubBitmap"), 170, 2230 );
1006 wxBitmap sub
= to_blit
.GetSubBitmap( wxRect(0,0,15,15) );
1008 dc
.DrawBitmap( sub
, 170, 2250, true );
1010 dc
.DrawText( _T("Enlarged"), 250, 2230 );
1011 dc
.SetUserScale( 1.5, 1.5 );
1012 dc
.DrawBitmap( to_blit
, (int)(250/1.5), (int)(2250/1.5), true );
1013 dc
.SetUserScale( 2, 2 );
1014 dc
.DrawBitmap( to_blit
, (int)(300/2), (int)(2250/2), true );
1015 dc
.SetUserScale( 1.0, 1.0 );
1017 dc
.DrawText( _T("Blit"), 400, 2230);
1019 blit_dc
.SelectObject( to_blit
);
1020 dc
.Blit( 400, 2250, to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
1021 dc
.SetUserScale( 1.5, 1.5 );
1022 dc
.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
1023 dc
.SetUserScale( 2, 2 );
1024 dc
.Blit( (int)(500/2), (int)(2250/2), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
1025 dc
.SetUserScale( 1.0, 1.0 );
1028 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
1029 if (my_horse_ico32
.Ok())
1030 dc
.DrawBitmap( my_horse_ico32
, 30, 2330, true );
1032 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
1033 if (my_horse_ico16
.Ok())
1034 dc
.DrawBitmap( my_horse_ico16
, 230, 2330, true );
1036 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
1037 if (my_horse_ico
.Ok())
1038 dc
.DrawBitmap( my_horse_ico
, 430, 2330, true );
1040 dc
.DrawText( _T("CUR handler"), 30, 2390 );
1041 if (my_horse_cur
.Ok())
1043 dc
.DrawBitmap( my_horse_cur
, 30, 2420, true );
1044 dc
.SetPen (*wxRED_PEN
);
1045 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
1046 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
1049 dc
.DrawText( _T("ANI handler"), 230, 2390 );
1050 for ( int i
=0; i
< m_ani_images
; i
++ )
1052 if (my_horse_ani
[i
].Ok())
1054 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, true );
1059 void MyCanvas::CreateAntiAliasedBitmap()
1061 wxBitmap
bitmap( 300, 300 );
1065 dc
.SelectObject( bitmap
);
1069 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
1070 dc
.SetTextForeground( wxT("RED") );
1071 dc
.DrawText( _T("This is anti-aliased Text."), 20, 5 );
1072 dc
.DrawText( _T("And a Rectangle."), 20, 45 );
1074 dc
.SetBrush( *wxRED_BRUSH
);
1075 dc
.SetPen( *wxTRANSPARENT_PEN
);
1076 dc
.DrawRoundedRectangle( 20, 85, 200, 180, 20 );
1078 wxImage original
= bitmap
.ConvertToImage();
1079 wxImage
anti( 150, 150 );
1081 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
1083 for (int y
= 1; y
< 149; y
++)
1084 for (int x
= 1; x
< 149; x
++)
1086 int red
= original
.GetRed( x
*2, y
*2 ) +
1087 original
.GetRed( x
*2-1, y
*2 ) +
1088 original
.GetRed( x
*2, y
*2+1 ) +
1089 original
.GetRed( x
*2+1, y
*2+1 );
1092 int green
= original
.GetGreen( x
*2, y
*2 ) +
1093 original
.GetGreen( x
*2-1, y
*2 ) +
1094 original
.GetGreen( x
*2, y
*2+1 ) +
1095 original
.GetGreen( x
*2+1, y
*2+1 );
1098 int blue
= original
.GetBlue( x
*2, y
*2 ) +
1099 original
.GetBlue( x
*2-1, y
*2 ) +
1100 original
.GetBlue( x
*2, y
*2+1 ) +
1101 original
.GetBlue( x
*2+1, y
*2+1 );
1103 anti
.SetRGB( x
, y
, (unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
1105 my_anti
= wxBitmap(anti
);
1112 ID_QUIT
= wxID_EXIT
,
1113 ID_ABOUT
= wxID_ABOUT
,
1120 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
1122 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
1123 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
1124 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
1125 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
1126 EVT_MENU (ID_INFO
, MyFrame::OnImageInfo
)
1127 EVT_MENU (ID_SHOWTHUMBNAIL
, MyFrame::OnThumbnail
)
1128 #ifdef wxHAVE_RAW_BITMAP
1129 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
1133 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
1134 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
1135 #endif // wxUSE_CLIPBOARD
1139 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxImage sample"),
1140 wxPoint(20, 20), wxSize(950, 700) )
1142 wxMenuBar
*menu_bar
= new wxMenuBar();
1144 wxMenu
*menuImage
= new wxMenu
;
1145 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
1146 menuImage
->Append( ID_INFO
, _T("Show image &information...\tCtrl-I"));
1147 #ifdef wxHAVE_RAW_BITMAP
1148 menuImage
->AppendSeparator();
1149 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
1151 menuImage
->AppendSeparator();
1152 menuImage
->Append( ID_SHOWTHUMBNAIL
, _T("Test &thumbnail...\tCtrl-T"),
1153 "Test scaling the image during load (try with JPEG)");
1154 menuImage
->AppendSeparator();
1155 menuImage
->Append( ID_ABOUT
, _T("&About..."));
1156 menuImage
->AppendSeparator();
1157 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
1158 menu_bar
->Append(menuImage
, _T("&Image"));
1161 wxMenu
*menuClipboard
= new wxMenu
;
1162 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
1163 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
1164 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
1165 #endif // wxUSE_CLIPBOARD
1167 SetMenuBar( menu_bar
);
1171 int widths
[] = { -1, 100 };
1172 SetStatusWidths( 2, widths
);
1173 #endif // wxUSE_STATUSBAR
1175 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
1177 // 500 width * 2750 height
1178 m_canvas
->SetScrollbars( 10, 10, 50, 275 );
1181 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
1186 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
1188 (void)wxMessageBox( _T("wxImage demo\n")
1189 _T("Robert Roebling (c) 1998,2000"),
1190 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
1193 wxString
MyFrame::LoadUserImage(wxImage
& image
)
1198 filename
= wxFileSelector(_T("Select image file"));
1199 if ( !filename
.empty() )
1201 if ( !image
.LoadFile(filename
) )
1203 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
1205 return wxEmptyString
;
1208 #endif // wxUSE_FILEDLG
1213 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
1216 wxString filename
= LoadUserImage(image
);
1217 if ( !filename
.empty() )
1218 (new MyImageFrame(this, filename
, wxBitmap(image
)))->Show();
1221 void MyFrame::OnImageInfo( wxCommandEvent
&WXUNUSED(event
) )
1224 if ( !LoadUserImage(image
).empty() )
1226 // TODO: show more information about the file
1227 wxString info
= wxString::Format("Image size: %dx%d",
1231 int xres
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
),
1232 yres
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
1235 info
+= wxString::Format("\nResolution: %dx%d", xres
, yres
);
1236 switch ( image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
) )
1239 wxFAIL_MSG( "unknown image resolution units" );
1242 case wxIMAGE_RESOLUTION_NONE
:
1243 info
+= " in default units";
1246 case wxIMAGE_RESOLUTION_INCHES
:
1250 case wxIMAGE_RESOLUTION_CM
:
1256 wxLogMessage("%s", info
);
1260 #ifdef wxHAVE_RAW_BITMAP
1262 void MyFrame::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
1264 (new MyRawBitmapFrame(this))->Show();
1267 #endif // wxHAVE_RAW_BITMAP
1271 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1273 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
1274 dobjBmp
->SetBitmap(m_canvas
->my_horse_png
);
1276 wxTheClipboard
->Open();
1278 if ( !wxTheClipboard
->SetData(dobjBmp
) )
1280 wxLogError(_T("Failed to copy bitmap to clipboard"));
1283 wxTheClipboard
->Close();
1286 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1288 wxBitmapDataObject dobjBmp
;
1290 wxTheClipboard
->Open();
1291 if ( !wxTheClipboard
->GetData(dobjBmp
) )
1293 wxLogMessage(_T("No bitmap data in the clipboard"));
1297 (new MyImageFrame(this, _T("Clipboard"), dobjBmp
.GetBitmap()))->Show();
1299 wxTheClipboard
->Close();
1302 #endif // wxUSE_CLIPBOARD
1304 //-----------------------------------------------------------------------------
1306 //-----------------------------------------------------------------------------
1308 bool MyApp::OnInit()
1310 if ( !wxApp::OnInit() )
1313 wxInitAllImageHandlers();
1315 wxFrame
*frame
= new MyFrame();
1316 frame
->Show( true );
1321 void MyFrame::OnThumbnail( wxCommandEvent
&WXUNUSED(event
) )
1324 wxString filename
= wxFileSelector(_T("Select image file"));
1325 if ( filename
.empty() )
1328 static const int THUMBNAIL_WIDTH
= 320;
1329 static const int THUMBNAIL_HEIGHT
= 240;
1332 image
.SetOption(wxIMAGE_OPTION_MAX_WIDTH
, THUMBNAIL_WIDTH
);
1333 image
.SetOption(wxIMAGE_OPTION_MAX_HEIGHT
, THUMBNAIL_HEIGHT
);
1336 if ( !image
.LoadFile(filename
) )
1338 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
1342 const long loadTime
= sw
.Time();
1344 MyImageFrame
* const
1345 frame
= new MyImageFrame(this, filename
, wxBitmap(image
));
1347 wxLogStatus(frame
, "Loaded \"%s\" in %ldms", filename
, loadTime
);
1349 wxLogError( _T("Couldn't create file selector dialog") );
1351 #endif // wxUSE_FILEDLG