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"
31 #include "wx/dataobj.h"
32 #include "wx/clipbrd.h"
33 #endif // wxUSE_CLIPBOARD
38 #if defined(__WXMSW__)
39 #ifdef wxHAVE_RAW_BITMAP
40 #include "wx/rawbmp.h"
44 #if defined(__WXMAC__) || defined(__WXGTK__)
45 #define wxHAVE_RAW_BITMAP
46 #include "wx/rawbmp.h"
56 class MyCanvas
: public wxScrolledWindow
59 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
61 void OnPaint( wxPaintEvent
&event
);
62 void CreateAntiAliasedBitmap();
64 wxBitmap my_horse_png
;
65 wxBitmap my_horse_jpeg
;
66 wxBitmap my_horse_gif
;
67 wxBitmap my_horse_bmp
;
68 wxBitmap my_horse_bmp2
;
69 wxBitmap my_horse_pcx
;
70 wxBitmap my_horse_pnm
;
71 wxBitmap my_horse_tiff
;
72 wxBitmap my_horse_xpm
;
73 wxBitmap my_horse_ico32
;
74 wxBitmap my_horse_ico16
;
75 wxBitmap my_horse_ico
;
76 wxBitmap my_horse_cur
;
78 wxBitmap my_smile_xbm
;
82 wxBitmap my_horse_asciigrey_pnm
;
83 wxBitmap my_horse_rawgrey_pnm
;
85 wxBitmap colorized_horse_jpeg
;
86 wxBitmap my_cmyk_jpeg
;
89 wxBitmap my_toucan_flipped_horiz
;
90 wxBitmap my_toucan_flipped_vert
;
91 wxBitmap my_toucan_flipped_both
;
92 wxBitmap my_toucan_grey
;
93 wxBitmap my_toucan_head
;
94 wxBitmap my_toucan_scaled_normal
;
95 wxBitmap my_toucan_scaled_high
;
96 wxBitmap my_toucan_blur
;
100 wxBitmap
*my_horse_ani
;
103 wxBitmap m_bmpSmileXpm
;
104 wxIcon m_iconSmileXpm
;
106 DECLARE_EVENT_TABLE()
113 class MyFrame
: public wxFrame
118 void OnAbout( wxCommandEvent
&event
);
119 void OnNewFrame( wxCommandEvent
&event
);
120 #ifdef wxHAVE_RAW_BITMAP
121 void OnTestRawBitmap( wxCommandEvent
&event
);
122 #endif // wxHAVE_RAW_BITMAP
123 void OnQuit( wxCommandEvent
&event
);
126 void OnCopy(wxCommandEvent
& event
);
127 void OnPaste(wxCommandEvent
& event
);
128 #endif // wxUSE_CLIPBOARD
133 DECLARE_DYNAMIC_CLASS(MyFrame
)
134 DECLARE_EVENT_TABLE()
137 class MyImageFrame
: public wxFrame
140 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
141 : wxFrame(parent
, wxID_ANY
, _T("Double click to save"),
142 wxDefaultPosition
, wxDefaultSize
,
143 wxCAPTION
| wxSYSTEM_MENU
| wxCLOSE_BOX
),
146 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
149 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
151 // do nothing here to be able to see how transparent images are shown
154 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
156 wxPaintDC
dc( this );
157 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
160 void OnSave(wxMouseEvent
& WXUNUSED(event
))
163 wxImage image
= m_bitmap
.ConvertToImage();
165 wxString savefilename
= wxFileSelector( wxT("Save Image"),
168 (const wxChar
*)NULL
,
169 wxT("BMP files (*.bmp)|*.bmp|")
170 wxT("PNG files (*.png)|*.png|")
171 wxT("JPEG files (*.jpg)|*.jpg|")
172 wxT("GIF files (*.gif)|*.gif|")
173 wxT("TIFF files (*.tif)|*.tif|")
174 wxT("PCX files (*.pcx)|*.pcx|")
175 wxT("ICO files (*.ico)|*.ico|")
176 wxT("CUR files (*.cur)|*.cur"),
180 if ( savefilename
.empty() )
184 wxFileName::SplitPath(savefilename
, NULL
, NULL
, &extension
);
187 if ( extension
== _T("bpp") )
189 static const int bppvalues
[] =
201 const wxString bppchoices
[] =
207 _T("8 bpp greyscale"),
209 _T("8 bpp own palette"),
213 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
214 _T("Image sample: save file"),
215 WXSIZEOF(bppchoices
),
218 if ( bppselection
!= -1 )
220 int format
= bppvalues
[bppselection
];
221 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, format
);
223 if ( format
== wxBMP_8BPP_PALETTE
)
225 unsigned char *cmap
= new unsigned char [256];
226 for ( int i
= 0; i
< 256; i
++ )
227 cmap
[i
] = (unsigned char)i
;
228 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
234 else if ( extension
== _T("png") )
236 static const int pngvalues
[] =
246 const wxString pngchoices
[] =
253 _T("Grey red 16bpp"),
256 int sel
= wxGetSingleChoiceIndex(_T("Set PNG format"),
257 _T("Image sample: save file"),
258 WXSIZEOF(pngchoices
),
263 image
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, pngvalues
[sel
]);
264 image
.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH
, sel
% 2 ? 16 : 8);
267 else if ( extension
== _T("cur") )
269 image
.Rescale(32,32);
270 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
271 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
272 // This shows how you can save an image with explicitly
273 // specified image format:
274 saved
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
279 // This one guesses image format from filename extension
280 // (it may fail if the extension is not recognized):
281 image
.SaveFile(savefilename
);
283 #endif // wxUSE_FILEDLG
289 DECLARE_EVENT_TABLE()
292 #ifdef wxHAVE_RAW_BITMAP
294 #include "wx/rawbmp.h"
296 class MyRawBitmapFrame
: public wxFrame
303 REAL_SIZE
= SIZE
- 2*BORDER
306 MyRawBitmapFrame(wxFrame
*parent
)
307 : wxFrame(parent
, wxID_ANY
, _T("Raw bitmaps (how exciting)")),
308 m_bitmap(SIZE
, SIZE
, 24),
309 m_alphaBitmap(SIZE
, SIZE
, 32)
311 SetClientSize(SIZE
, SIZE
*2+25);
318 void InitAlphaBitmap()
320 // First, clear the whole bitmap by making it alpha
322 wxAlphaPixelData
data( m_alphaBitmap
, wxPoint(0,0), wxSize(SIZE
, SIZE
) );
325 wxLogError(_T("Failed to gain raw access to bitmap data"));
329 wxAlphaPixelData::Iterator
p(data
);
330 for ( int y
= 0; y
< SIZE
; ++y
)
332 wxAlphaPixelData::Iterator rowStart
= p
;
333 for ( int x
= 0; x
< SIZE
; ++x
)
336 ++p
; // same as p.OffsetX(1)
343 // Then, draw colourful alpha-blended stripes
344 wxAlphaPixelData
data(m_alphaBitmap
, wxPoint(BORDER
, BORDER
),
345 wxSize(REAL_SIZE
, REAL_SIZE
));
348 wxLogError(_T("Failed to gain raw access to bitmap data"));
353 wxAlphaPixelData::Iterator
p(data
);
355 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
357 wxAlphaPixelData::Iterator rowStart
= p
;
359 int r
= y
< REAL_SIZE
/3 ? 255 : 0,
360 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ? 255 : 0,
361 b
= 2*(REAL_SIZE
/3) <= y
? 255 : 0;
363 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
365 // note that RGB must be premultiplied by alpha
366 unsigned a
= (wxAlphaPixelData::Iterator::ChannelType
)((x
*255.)/REAL_SIZE
);
367 p
.Red() = r
* a
/ 256;
368 p
.Green() = g
* a
/ 256;
369 p
.Blue() = b
* a
/ 256;
372 ++p
; // same as p.OffsetX(1)
382 // draw some colourful stripes without alpha
383 wxNativePixelData
data(m_bitmap
);
386 wxLogError(_T("Failed to gain raw access to bitmap data"));
390 wxNativePixelData::Iterator
p(data
);
391 for ( int y
= 0; y
< SIZE
; ++y
)
393 wxNativePixelData::Iterator rowStart
= p
;
395 int r
= y
< SIZE
/3 ? 255 : 0,
396 g
= (SIZE
/3 <= y
) && (y
< 2*(SIZE
/3)) ? 255 : 0,
397 b
= 2*(SIZE
/3) <= y
? 255 : 0;
399 for ( int x
= 0; x
< SIZE
; ++x
)
404 ++p
; // same as p.OffsetX(1)
412 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
414 wxPaintDC
dc( this );
415 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER
);
416 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
417 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
418 dc
.DrawBitmap( m_alphaBitmap
, 0, 0, true /* use mask */ );
420 dc
.DrawText(_T("Raw bitmap access without alpha"), 0, SIZE
+5);
421 dc
.DrawBitmap( m_bitmap
, 0, SIZE
+5+dc
.GetCharHeight());
426 wxBitmap m_alphaBitmap
;
428 DECLARE_EVENT_TABLE()
431 #endif // wxHAVE_RAW_BITMAP
435 class MyApp
: public wxApp
438 virtual bool OnInit();
447 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
448 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
449 EVT_PAINT(MyImageFrame::OnPaint
)
450 EVT_LEFT_DCLICK(MyImageFrame::OnSave
)
453 #ifdef wxHAVE_RAW_BITMAP
455 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
456 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
459 #endif // wxHAVE_RAW_BITMAP
461 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
462 EVT_PAINT(MyCanvas::OnPaint
)
465 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
466 const wxPoint
&pos
, const wxSize
&size
)
467 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
468 , m_bmpSmileXpm((const char **) smile_xpm
)
469 , m_iconSmileXpm((const char **) smile_xpm
)
474 SetBackgroundColour(* wxWHITE
);
476 wxBitmap
bitmap( 100, 100 );
479 dc
.SelectObject( bitmap
);
480 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
481 dc
.SetPen( *wxBLACK_PEN
);
482 dc
.DrawRectangle( 0, 0, 100, 100 );
483 dc
.SetBrush( *wxWHITE_BRUSH
);
484 dc
.DrawRectangle( 20, 20, 60, 60 );
485 dc
.SelectObject( wxNullBitmap
);
487 // try to find the directory with our images
489 if ( wxFile::Exists(wxT("./horse.png")) )
491 else if ( wxFile::Exists(wxT("../horse.png")) )
494 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
496 wxImage image
= bitmap
.ConvertToImage();
499 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
500 wxLogError(wxT("Can't save file"));
504 if ( image
.LoadFile( dir
+ _T("test.png") ) )
505 my_square
= wxBitmap( image
);
509 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
510 wxLogError(wxT("Can't load PNG image"));
512 my_horse_png
= wxBitmap( image
);
514 if ( !image
.LoadFile( dir
+ _T("toucan.png")) )
515 wxLogError(wxT("Can't load PNG image"));
517 my_toucan
= wxBitmap(image
);
519 my_toucan_flipped_horiz
= wxBitmap(image
.Mirror(true));
520 my_toucan_flipped_vert
= wxBitmap(image
.Mirror(false));
521 my_toucan_flipped_both
= wxBitmap(image
.Mirror(true).Mirror(false));
522 my_toucan_grey
= wxBitmap(image
.ConvertToGreyscale());
523 my_toucan_head
= wxBitmap(image
.GetSubImage(wxRect(40, 7, 80, 60)));
524 my_toucan_scaled_normal
= wxBitmap(image
.Scale(110,90,wxIMAGE_QUALITY_NORMAL
));
525 my_toucan_scaled_high
= wxBitmap(image
.Scale(110,90,wxIMAGE_QUALITY_HIGH
));
526 my_toucan_blur
= wxBitmap(image
.Blur(10));
528 #endif // wxUSE_LIBPNG
533 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
534 wxLogError(wxT("Can't load JPG image"));
537 my_horse_jpeg
= wxBitmap( image
);
539 // Colorize by rotating green hue to red
540 wxImage::HSVValue greenHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0));
541 wxImage::HSVValue redHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0));
542 image
.RotateHue(redHSV
.hue
- greenHSV
.hue
);
543 colorized_horse_jpeg
= wxBitmap( image
);
546 if ( !image
.LoadFile( dir
+ _T("cmyk.jpg")) )
547 wxLogError(_T("Can't load CMYK JPG image"));
549 my_cmyk_jpeg
= wxBitmap(image
);
550 #endif // wxUSE_LIBJPEG
555 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
556 wxLogError(wxT("Can't load GIF image"));
558 my_horse_gif
= wxBitmap( image
);
564 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
565 wxLogError(wxT("Can't load PCX image"));
567 my_horse_pcx
= wxBitmap( image
);
572 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
573 wxLogError(wxT("Can't load BMP image"));
575 my_horse_bmp
= wxBitmap( image
);
580 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
581 wxLogError(wxT("Can't load XPM image"));
583 my_horse_xpm
= wxBitmap( image
);
585 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
586 wxLogError(wxT("Can't save file"));
592 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
593 wxLogError(wxT("Can't load PNM image"));
595 my_horse_pnm
= wxBitmap( image
);
599 if ( !image
.LoadFile( dir
+ _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM
) )
600 wxLogError(wxT("Can't load PNM image"));
602 my_horse_asciigrey_pnm
= wxBitmap( image
);
606 if ( !image
.LoadFile( dir
+ _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM
) )
607 wxLogError(wxT("Can't load PNM image"));
609 my_horse_rawgrey_pnm
= wxBitmap( image
);
615 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
616 wxLogError(wxT("Can't load TIFF image"));
618 my_horse_tiff
= wxBitmap( image
);
621 CreateAntiAliasedBitmap();
623 my_smile_xbm
= wxBitmap( (const char*)smile_bits
, smile_width
,
626 // demonstrates XPM automatically using the mask when saving
627 if ( m_bmpSmileXpm
.Ok() )
628 m_bmpSmileXpm
.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM
);
633 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
634 wxLogError(wxT("Can't load first ICO image"));
636 my_horse_ico32
= wxBitmap( image
);
640 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
641 wxLogError(wxT("Can't load second ICO image"));
643 my_horse_ico16
= wxBitmap( image
);
647 if ( !image
.LoadFile( dir
+ _T("horse.ico") ) )
648 wxLogError(wxT("Can't load best ICO image"));
650 my_horse_ico
= wxBitmap( image
);
654 if ( !image
.LoadFile( dir
+ _T("horse.cur"), wxBITMAP_TYPE_CUR
) )
655 wxLogError(wxT("Can't load best ICO image"));
658 my_horse_cur
= wxBitmap( image
);
659 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
660 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
663 m_ani_images
= wxImage::GetImageCount ( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
);
665 wxLogError(wxT("No ANI-format images found"));
667 my_horse_ani
= new wxBitmap
[m_ani_images
];
669 for (i
=0; i
< m_ani_images
; i
++)
672 if (!image
.LoadFile( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
, i
))
674 wxString tmp
= wxT("Can't load image number ");
679 my_horse_ani
[i
] = wxBitmap( image
);
681 #endif // wxUSE_ICO_CUR
685 // test image loading from stream
686 wxFile
file(dir
+ _T("horse.bmp"));
687 if ( file
.IsOpened() )
689 wxFileOffset len
= file
.Length();
690 size_t dataSize
= (size_t)len
;
691 void *data
= malloc(dataSize
);
692 if ( file
.Read(data
, dataSize
) != len
)
693 wxLogError(_T("Reading bitmap file failed"));
696 wxMemoryInputStream
mis(data
, dataSize
);
697 if ( !image
.LoadFile(mis
) )
698 wxLogError(wxT("Can't load BMP image from stream"));
700 my_horse_bmp2
= wxBitmap( image
);
707 MyCanvas::~MyCanvas()
709 delete [] my_horse_ani
;
712 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
714 wxPaintDC
dc( this );
717 dc
.DrawText( _T("Loaded image"), 30, 10 );
719 dc
.DrawBitmap( my_square
, 30, 30 );
721 dc
.DrawText( _T("Drawn directly"), 150, 10 );
722 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
723 dc
.SetPen( *wxBLACK_PEN
);
724 dc
.DrawRectangle( 150, 30, 100, 100 );
725 dc
.SetBrush( *wxWHITE_BRUSH
);
726 dc
.DrawRectangle( 170, 50, 60, 60 );
729 dc
.DrawBitmap( my_anti
, 280, 30 );
731 dc
.DrawText( _T("PNG handler"), 30, 135 );
732 if (my_horse_png
.Ok())
734 dc
.DrawBitmap( my_horse_png
, 30, 150 );
735 wxRect
rect(0,0,100,100);
736 wxBitmap
sub( my_horse_png
.GetSubBitmap(rect
) );
737 dc
.DrawText( _T("GetSubBitmap()"), 280, 175 );
738 dc
.DrawBitmap( sub
, 280, 195 );
741 dc
.DrawText( _T("JPEG handler"), 30, 365 );
742 if (my_horse_jpeg
.Ok())
743 dc
.DrawBitmap( my_horse_jpeg
, 30, 380 );
745 dc
.DrawText( _T("Green rotated to red"), 280, 365 );
746 if (colorized_horse_jpeg
.Ok())
747 dc
.DrawBitmap( colorized_horse_jpeg
, 280, 380 );
749 dc
.DrawText( _T("CMYK JPEG image"), 530, 365 );
750 if (my_cmyk_jpeg
.Ok())
751 dc
.DrawBitmap( my_cmyk_jpeg
, 530, 380 );
753 dc
.DrawText( _T("GIF handler"), 30, 595 );
754 if (my_horse_gif
.Ok())
755 dc
.DrawBitmap( my_horse_gif
, 30, 610 );
757 dc
.DrawText( _T("PCX handler"), 30, 825 );
758 if (my_horse_pcx
.Ok())
759 dc
.DrawBitmap( my_horse_pcx
, 30, 840 );
761 dc
.DrawText( _T("BMP handler"), 30, 1055 );
762 if (my_horse_bmp
.Ok())
763 dc
.DrawBitmap( my_horse_bmp
, 30, 1070 );
765 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
766 if (my_horse_bmp2
.Ok())
767 dc
.DrawBitmap( my_horse_bmp2
, 280, 1070 );
769 dc
.DrawText( _T("PNM handler"), 30, 1285 );
770 if (my_horse_pnm
.Ok())
771 dc
.DrawBitmap( my_horse_pnm
, 30, 1300 );
773 dc
.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 );
774 if (my_horse_asciigrey_pnm
.Ok())
775 dc
.DrawBitmap( my_horse_asciigrey_pnm
, 280, 1300 );
777 dc
.DrawText( _T("PNM handler (raw grey)"), 530, 1285 );
778 if (my_horse_rawgrey_pnm
.Ok())
779 dc
.DrawBitmap( my_horse_rawgrey_pnm
, 530, 1300 );
781 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
782 if (my_horse_tiff
.Ok())
783 dc
.DrawBitmap( my_horse_tiff
, 30, 1530 );
785 dc
.DrawText( _T("XPM handler"), 30, 1745 );
786 if (my_horse_xpm
.Ok())
787 dc
.DrawBitmap( my_horse_xpm
, 30, 1760 );
791 int x
= 750, y
= 10, yy
= 170;
793 dc
.DrawText(wxT("Original toucan"), x
+50, y
);
794 dc
.DrawBitmap(my_toucan
, x
, y
+15, true);
796 dc
.DrawText(wxT("Flipped horizontally"), x
+50, y
);
797 dc
.DrawBitmap(my_toucan_flipped_horiz
, x
, y
+15, true);
799 dc
.DrawText(wxT("Flipped vertically"), x
+50, y
);
800 dc
.DrawBitmap(my_toucan_flipped_vert
, x
, y
+15, true);
802 dc
.DrawText(wxT("Flipped both h&v"), x
+50, y
);
803 dc
.DrawBitmap(my_toucan_flipped_both
, x
, y
+15, true);
806 dc
.DrawText(wxT("In greyscale"), x
+50, y
);
807 dc
.DrawBitmap(my_toucan_grey
, x
, y
+15, true);
810 dc
.DrawText(wxT("Toucan's head"), x
+50, y
);
811 dc
.DrawBitmap(my_toucan_head
, x
, y
+15, true);
814 dc
.DrawText(wxT("Scaled with normal quality"), x
+50, y
);
815 dc
.DrawBitmap(my_toucan_scaled_normal
, x
, y
+15, true);
818 dc
.DrawText(wxT("Scaled with high quality"), x
+50, y
);
819 dc
.DrawBitmap(my_toucan_scaled_high
, x
, y
+15, true);
822 dc
.DrawText(wxT("Blured"), x
+50, y
);
823 dc
.DrawBitmap(my_toucan_blur
, x
, y
+15, true);
826 if (my_smile_xbm
.Ok())
828 dc
.DrawText( _T("XBM bitmap"), 30, 1975 );
829 dc
.DrawText( _T("(green on red)"), 30, 1990 );
830 dc
.SetTextForeground( _T("GREEN") );
831 dc
.SetTextBackground( _T("RED") );
832 dc
.DrawBitmap( my_smile_xbm
, 30, 2010 );
834 dc
.SetTextForeground( *wxBLACK
);
835 dc
.DrawText( _T("After wxImage conversion"), 150, 1975 );
836 dc
.DrawText( _T("(red on white)"), 150, 1990 );
837 dc
.SetTextForeground( wxT("RED") );
838 wxImage i
= my_smile_xbm
.ConvertToImage();
839 i
.SetMaskColour( 255, 255, 255 );
841 wxRED_PEN
->GetColour().Red(),
842 wxRED_PEN
->GetColour().Green(),
843 wxRED_PEN
->GetColour().Blue() );
844 dc
.DrawBitmap( wxBitmap(i
), 150, 2010, true );
845 dc
.SetTextForeground( *wxBLACK
);
849 wxBitmap
mono( 60,50,1 );
851 memdc
.SelectObject( mono
);
852 memdc
.SetPen( *wxBLACK_PEN
);
853 memdc
.SetBrush( *wxWHITE_BRUSH
);
854 memdc
.DrawRectangle( 0,0,60,50 );
855 memdc
.SetTextForeground( *wxBLACK
);
857 // I cannot convince GTK2 to draw into mono bitmaps
858 memdc
.DrawText( _T("Hi!"), 5, 5 );
860 memdc
.SetBrush( *wxBLACK_BRUSH
);
861 memdc
.DrawRectangle( 33,5,20,20 );
862 memdc
.SetPen( *wxRED_PEN
);
863 memdc
.DrawLine( 5, 42, 50, 42 );
864 memdc
.SelectObject( wxNullBitmap
);
868 dc
.DrawText( _T("Mono bitmap"), 30, 2095 );
869 dc
.DrawText( _T("(red on green)"), 30, 2110 );
870 dc
.SetTextForeground( wxT("RED") );
871 dc
.SetTextBackground( wxT("GREEN") );
872 dc
.DrawBitmap( mono
, 30, 2130 );
874 dc
.SetTextForeground( *wxBLACK
);
875 dc
.DrawText( _T("After wxImage conversion"), 150, 2095 );
876 dc
.DrawText( _T("(red on white)"), 150, 2110 );
877 dc
.SetTextForeground( wxT("RED") );
878 wxImage i
= mono
.ConvertToImage();
879 i
.SetMaskColour( 255,255,255 );
881 wxRED_PEN
->GetColour().Red(),
882 wxRED_PEN
->GetColour().Green(),
883 wxRED_PEN
->GetColour().Blue() );
884 dc
.DrawBitmap( wxBitmap(i
), 150, 2130, true );
885 dc
.SetTextForeground( *wxBLACK
);
888 // For testing transparency
889 dc
.SetBrush( *wxRED_BRUSH
);
890 dc
.DrawRectangle( 20, 2220, 560, 68 );
892 dc
.DrawText(_T("XPM bitmap"), 30, 2230 );
893 if ( m_bmpSmileXpm
.Ok() )
894 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, true);
896 dc
.DrawText(_T("XPM icon"), 110, 2230 );
897 if ( m_iconSmileXpm
.Ok() )
898 dc
.DrawIcon(m_iconSmileXpm
, 110, 2250);
900 // testing icon -> bitmap conversion
901 wxBitmap
to_blit( m_iconSmileXpm
);
904 dc
.DrawText( _T("SubBitmap"), 170, 2230 );
905 wxBitmap sub
= to_blit
.GetSubBitmap( wxRect(0,0,15,15) );
907 dc
.DrawBitmap( sub
, 170, 2250, true );
909 dc
.DrawText( _T("Enlarged"), 250, 2230 );
910 dc
.SetUserScale( 1.5, 1.5 );
911 dc
.DrawBitmap( to_blit
, (int)(250/1.5), (int)(2250/1.5), true );
912 dc
.SetUserScale( 2, 2 );
913 dc
.DrawBitmap( to_blit
, (int)(300/2), (int)(2250/2), true );
914 dc
.SetUserScale( 1.0, 1.0 );
916 dc
.DrawText( _T("Blit"), 400, 2230);
918 blit_dc
.SelectObject( to_blit
);
919 dc
.Blit( 400, 2250, to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
920 dc
.SetUserScale( 1.5, 1.5 );
921 dc
.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
922 dc
.SetUserScale( 2, 2 );
923 dc
.Blit( (int)(500/2), (int)(2250/2), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
924 dc
.SetUserScale( 1.0, 1.0 );
927 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
928 if (my_horse_ico32
.Ok())
929 dc
.DrawBitmap( my_horse_ico32
, 30, 2330, true );
931 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
932 if (my_horse_ico16
.Ok())
933 dc
.DrawBitmap( my_horse_ico16
, 230, 2330, true );
935 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
936 if (my_horse_ico
.Ok())
937 dc
.DrawBitmap( my_horse_ico
, 430, 2330, true );
939 dc
.DrawText( _T("CUR handler"), 30, 2390 );
940 if (my_horse_cur
.Ok())
942 dc
.DrawBitmap( my_horse_cur
, 30, 2420, true );
943 dc
.SetPen (*wxRED_PEN
);
944 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
945 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
948 dc
.DrawText( _T("ANI handler"), 230, 2390 );
949 for ( int i
=0; i
< m_ani_images
; i
++ )
951 if (my_horse_ani
[i
].Ok())
953 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, true );
958 void MyCanvas::CreateAntiAliasedBitmap()
960 wxBitmap
bitmap( 300, 300 );
964 dc
.SelectObject( bitmap
);
968 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
969 dc
.SetTextForeground( wxT("RED") );
970 dc
.DrawText( _T("This is anti-aliased Text."), 20, 5 );
971 dc
.DrawText( _T("And a Rectangle."), 20, 45 );
973 dc
.SetBrush( *wxRED_BRUSH
);
974 dc
.SetPen( *wxTRANSPARENT_PEN
);
975 dc
.DrawRoundedRectangle( 20, 85, 200, 180, 20 );
977 wxImage original
= bitmap
.ConvertToImage();
978 wxImage
anti( 150, 150 );
980 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
982 for (int y
= 1; y
< 149; y
++)
983 for (int x
= 1; x
< 149; x
++)
985 int red
= original
.GetRed( x
*2, y
*2 ) +
986 original
.GetRed( x
*2-1, y
*2 ) +
987 original
.GetRed( x
*2, y
*2+1 ) +
988 original
.GetRed( x
*2+1, y
*2+1 );
991 int green
= original
.GetGreen( x
*2, y
*2 ) +
992 original
.GetGreen( x
*2-1, y
*2 ) +
993 original
.GetGreen( x
*2, y
*2+1 ) +
994 original
.GetGreen( x
*2+1, y
*2+1 );
997 int blue
= original
.GetBlue( x
*2, y
*2 ) +
998 original
.GetBlue( x
*2-1, y
*2 ) +
999 original
.GetBlue( x
*2, y
*2+1 ) +
1000 original
.GetBlue( x
*2+1, y
*2+1 );
1002 anti
.SetRGB( x
, y
, (unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
1004 my_anti
= wxBitmap(anti
);
1011 ID_QUIT
= wxID_EXIT
,
1012 ID_ABOUT
= wxID_ABOUT
,
1017 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
1019 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
1020 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
1021 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
1022 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
1023 #ifdef wxHAVE_RAW_BITMAP
1024 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
1028 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
1029 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
1030 #endif // wxUSE_CLIPBOARD
1034 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxImage sample"),
1035 wxPoint(20, 20), wxSize(950, 700) )
1037 wxMenuBar
*menu_bar
= new wxMenuBar();
1039 wxMenu
*menuImage
= new wxMenu
;
1040 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
1042 #ifdef wxHAVE_RAW_BITMAP
1043 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
1045 menuImage
->AppendSeparator();
1046 menuImage
->Append( ID_ABOUT
, _T("&About..."));
1047 menuImage
->AppendSeparator();
1048 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
1049 menu_bar
->Append(menuImage
, _T("&Image"));
1052 wxMenu
*menuClipboard
= new wxMenu
;
1053 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
1054 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
1055 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
1056 #endif // wxUSE_CLIPBOARD
1058 SetMenuBar( menu_bar
);
1062 int widths
[] = { -1, 100 };
1063 SetStatusWidths( 2, widths
);
1064 #endif // wxUSE_STATUSBAR
1066 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
1068 // 500 width * 2750 height
1069 m_canvas
->SetScrollbars( 10, 10, 50, 275 );
1072 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
1077 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
1079 (void)wxMessageBox( _T("wxImage demo\n")
1080 _T("Robert Roebling (c) 1998,2000"),
1081 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
1084 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
1087 wxString filename
= wxFileSelector(_T("Select image file"));
1092 if ( !image
.LoadFile(filename
) )
1094 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
1099 (new MyImageFrame(this, wxBitmap(image
)))->Show();
1100 #endif // wxUSE_FILEDLG
1103 #ifdef wxHAVE_RAW_BITMAP
1105 void MyFrame::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
1107 (new MyRawBitmapFrame(this))->Show();
1110 #endif // wxHAVE_RAW_BITMAP
1114 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1116 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
1117 dobjBmp
->SetBitmap(m_canvas
->my_horse_png
);
1119 wxTheClipboard
->Open();
1121 if ( !wxTheClipboard
->SetData(dobjBmp
) )
1123 wxLogError(_T("Failed to copy bitmap to clipboard"));
1126 wxTheClipboard
->Close();
1129 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1131 wxBitmapDataObject dobjBmp
;
1133 wxTheClipboard
->Open();
1134 if ( !wxTheClipboard
->GetData(dobjBmp
) )
1136 wxLogMessage(_T("No bitmap data in the clipboard"));
1140 (new MyImageFrame(this, dobjBmp
.GetBitmap()))->Show();
1142 wxTheClipboard
->Close();
1145 #endif // wxUSE_CLIPBOARD
1147 //-----------------------------------------------------------------------------
1149 //-----------------------------------------------------------------------------
1151 bool MyApp::OnInit()
1154 wxImage::AddHandler( new wxPNGHandler
);
1158 wxImage::AddHandler( new wxJPEGHandler
);
1162 wxImage::AddHandler( new wxTIFFHandler
);
1166 wxImage::AddHandler( new wxGIFHandler
);
1170 wxImage::AddHandler( new wxPCXHandler
);
1174 wxImage::AddHandler( new wxPNMHandler
);
1178 wxImage::AddHandler( new wxXPMHandler
);
1182 wxImage::AddHandler( new wxICOHandler
);
1183 wxImage::AddHandler( new wxCURHandler
);
1184 wxImage::AddHandler( new wxANIHandler
);
1187 wxFrame
*frame
= new MyFrame();
1188 frame
->Show( true );