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_tga
;
73 wxBitmap my_horse_xpm
;
74 wxBitmap my_horse_ico32
;
75 wxBitmap my_horse_ico16
;
76 wxBitmap my_horse_ico
;
77 wxBitmap my_horse_cur
;
79 wxBitmap my_smile_xbm
;
83 wxBitmap my_horse_asciigrey_pnm
;
84 wxBitmap my_horse_rawgrey_pnm
;
86 wxBitmap colorized_horse_jpeg
;
87 wxBitmap my_cmyk_jpeg
;
90 wxBitmap my_toucan_flipped_horiz
;
91 wxBitmap my_toucan_flipped_vert
;
92 wxBitmap my_toucan_flipped_both
;
93 wxBitmap my_toucan_grey
;
94 wxBitmap my_toucan_head
;
95 wxBitmap my_toucan_scaled_normal
;
96 wxBitmap my_toucan_scaled_high
;
97 wxBitmap my_toucan_blur
;
101 wxBitmap
*my_horse_ani
;
104 wxBitmap m_bmpSmileXpm
;
105 wxIcon m_iconSmileXpm
;
107 DECLARE_EVENT_TABLE()
114 class MyFrame
: public wxFrame
119 void OnAbout( wxCommandEvent
&event
);
120 void OnNewFrame( wxCommandEvent
&event
);
121 #ifdef wxHAVE_RAW_BITMAP
122 void OnTestRawBitmap( wxCommandEvent
&event
);
123 #endif // wxHAVE_RAW_BITMAP
124 void OnQuit( wxCommandEvent
&event
);
127 void OnCopy(wxCommandEvent
& event
);
128 void OnPaste(wxCommandEvent
& event
);
129 #endif // wxUSE_CLIPBOARD
134 DECLARE_DYNAMIC_CLASS(MyFrame
)
135 DECLARE_EVENT_TABLE()
138 // ----------------------------------------------------------------------------
139 // Frame used for showing a standalone image
140 // ----------------------------------------------------------------------------
144 ID_ROTATE_LEFT
= 100,
149 class MyImageFrame
: public wxFrame
152 MyImageFrame(wxFrame
*parent
, const wxString
& desc
, const wxBitmap
& bitmap
)
153 : wxFrame(parent
, wxID_ANY
,
154 wxString::Format(_T("Image from %s"), desc
.c_str()),
155 wxDefaultPosition
, wxDefaultSize
,
156 wxDEFAULT_FRAME_STYLE
| wxFULL_REPAINT_ON_RESIZE
),
159 wxMenu
*menu
= new wxMenu
;
160 menu
->Append(wxID_SAVE
);
161 menu
->AppendSeparator();
162 menu
->Append(ID_RESIZE
, _T("&Fit to window\tCtrl-F"));
163 menu
->AppendSeparator();
164 menu
->Append(ID_ROTATE_LEFT
, _T("Rotate &left\tCtrl-L"));
165 menu
->Append(ID_ROTATE_RIGHT
, _T("Rotate &right\tCtrl-R"));
167 wxMenuBar
*mbar
= new wxMenuBar
;
168 mbar
->Append(menu
, _T("&Image"));
173 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
178 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
180 // do nothing here to be able to see how transparent images are shown
183 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
186 const wxSize size
= GetClientSize();
187 dc
.DrawBitmap(m_bitmap
,
188 (size
.x
- m_bitmap
.GetWidth())/2,
189 (size
.y
- m_bitmap
.GetHeight())/2,
190 true /* use mask */);
193 void OnSave(wxCommandEvent
& WXUNUSED(event
))
196 wxImage image
= m_bitmap
.ConvertToImage();
198 wxString savefilename
= wxFileSelector( wxT("Save Image"),
201 (const wxChar
*)NULL
,
202 wxT("BMP files (*.bmp)|*.bmp|")
203 wxT("PNG files (*.png)|*.png|")
204 wxT("JPEG files (*.jpg)|*.jpg|")
205 wxT("GIF files (*.gif)|*.gif|")
206 wxT("TIFF files (*.tif)|*.tif|")
207 wxT("PCX files (*.pcx)|*.pcx|")
208 wxT("ICO files (*.ico)|*.ico|")
209 wxT("CUR files (*.cur)|*.cur"),
213 if ( savefilename
.empty() )
217 wxFileName::SplitPath(savefilename
, NULL
, NULL
, &extension
);
220 if ( extension
== _T("bmp") )
222 static const int bppvalues
[] =
234 const wxString bppchoices
[] =
240 _T("8 bpp greyscale"),
242 _T("8 bpp own palette"),
246 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
247 _T("Image sample: save file"),
248 WXSIZEOF(bppchoices
),
251 if ( bppselection
!= -1 )
253 int format
= bppvalues
[bppselection
];
254 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, format
);
256 if ( format
== wxBMP_8BPP_PALETTE
)
258 unsigned char *cmap
= new unsigned char [256];
259 for ( int i
= 0; i
< 256; i
++ )
260 cmap
[i
] = (unsigned char)i
;
261 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
267 else if ( extension
== _T("png") )
269 static const int pngvalues
[] =
279 const wxString pngchoices
[] =
286 _T("Grey red 16bpp"),
289 int sel
= wxGetSingleChoiceIndex(_T("Set PNG format"),
290 _T("Image sample: save file"),
291 WXSIZEOF(pngchoices
),
296 image
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, pngvalues
[sel
]);
297 image
.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH
, sel
% 2 ? 16 : 8);
300 else if ( extension
== _T("cur") )
302 image
.Rescale(32,32);
303 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
304 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
305 // This shows how you can save an image with explicitly
306 // specified image format:
307 saved
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
312 // This one guesses image format from filename extension
313 // (it may fail if the extension is not recognized):
314 image
.SaveFile(savefilename
);
316 #endif // wxUSE_FILEDLG
319 void OnResize(wxCommandEvent
& WXUNUSED(event
))
321 wxImage
img(m_bitmap
.ConvertToImage());
323 const wxSize size
= GetClientSize();
324 img
.Rescale(size
.x
, size
.y
, wxIMAGE_QUALITY_HIGH
);
325 m_bitmap
= wxBitmap(img
);
331 void OnRotate(wxCommandEvent
& event
)
334 if ( event
.GetId() == ID_ROTATE_LEFT
)
337 wxImage
img(m_bitmap
.ConvertToImage());
338 img
= img
.Rotate(angle
, wxPoint(img
.GetWidth() / 2, img
.GetHeight() / 2));
341 wxLogWarning(_T("Rotation failed"));
345 m_bitmap
= wxBitmap(img
);
352 void UpdateStatusBar()
354 wxLogStatus(this, _T("Image size: (%d, %d)"),
356 m_bitmap
.GetHeight());
361 DECLARE_EVENT_TABLE()
364 #ifdef wxHAVE_RAW_BITMAP
366 #include "wx/rawbmp.h"
368 class MyRawBitmapFrame
: public wxFrame
375 REAL_SIZE
= SIZE
- 2*BORDER
378 MyRawBitmapFrame(wxFrame
*parent
)
379 : wxFrame(parent
, wxID_ANY
, _T("Raw bitmaps (how exciting)")),
380 m_bitmap(SIZE
, SIZE
, 24),
381 m_alphaBitmap(SIZE
, SIZE
, 32)
383 SetClientSize(SIZE
, SIZE
*2+25);
390 void InitAlphaBitmap()
392 // First, clear the whole bitmap by making it alpha
394 wxAlphaPixelData
data( m_alphaBitmap
, wxPoint(0,0), wxSize(SIZE
, SIZE
) );
397 wxLogError(_T("Failed to gain raw access to bitmap data"));
401 wxAlphaPixelData::Iterator
p(data
);
402 for ( int y
= 0; y
< SIZE
; ++y
)
404 wxAlphaPixelData::Iterator rowStart
= p
;
405 for ( int x
= 0; x
< SIZE
; ++x
)
408 ++p
; // same as p.OffsetX(1)
415 // Then, draw colourful alpha-blended stripes
416 wxAlphaPixelData
data(m_alphaBitmap
, wxPoint(BORDER
, BORDER
),
417 wxSize(REAL_SIZE
, REAL_SIZE
));
420 wxLogError(_T("Failed to gain raw access to bitmap data"));
425 wxAlphaPixelData::Iterator
p(data
);
427 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
429 wxAlphaPixelData::Iterator rowStart
= p
;
431 int r
= y
< REAL_SIZE
/3 ? 255 : 0,
432 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ? 255 : 0,
433 b
= 2*(REAL_SIZE
/3) <= y
? 255 : 0;
435 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
437 // note that RGB must be premultiplied by alpha
438 unsigned a
= (wxAlphaPixelData::Iterator::ChannelType
)((x
*255.)/REAL_SIZE
);
439 p
.Red() = r
* a
/ 256;
440 p
.Green() = g
* a
/ 256;
441 p
.Blue() = b
* a
/ 256;
444 ++p
; // same as p.OffsetX(1)
454 // draw some colourful stripes without alpha
455 wxNativePixelData
data(m_bitmap
);
458 wxLogError(_T("Failed to gain raw access to bitmap data"));
462 wxNativePixelData::Iterator
p(data
);
463 for ( int y
= 0; y
< SIZE
; ++y
)
465 wxNativePixelData::Iterator rowStart
= p
;
467 int r
= y
< SIZE
/3 ? 255 : 0,
468 g
= (SIZE
/3 <= y
) && (y
< 2*(SIZE
/3)) ? 255 : 0,
469 b
= 2*(SIZE
/3) <= y
? 255 : 0;
471 for ( int x
= 0; x
< SIZE
; ++x
)
476 ++p
; // same as p.OffsetX(1)
484 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
486 wxPaintDC
dc( this );
487 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER
);
488 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
489 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
490 dc
.DrawBitmap( m_alphaBitmap
, 0, 0, true /* use mask */ );
492 dc
.DrawText(_T("Raw bitmap access without alpha"), 0, SIZE
+5);
493 dc
.DrawBitmap( m_bitmap
, 0, SIZE
+5+dc
.GetCharHeight());
498 wxBitmap m_alphaBitmap
;
500 DECLARE_EVENT_TABLE()
503 #endif // wxHAVE_RAW_BITMAP
507 class MyApp
: public wxApp
510 virtual bool OnInit();
519 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
520 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
521 EVT_PAINT(MyImageFrame::OnPaint
)
523 EVT_MENU(wxID_SAVE
, MyImageFrame::OnSave
)
524 EVT_MENU_RANGE(ID_ROTATE_LEFT
, ID_ROTATE_RIGHT
, MyImageFrame::OnRotate
)
525 EVT_MENU(ID_RESIZE
, MyImageFrame::OnResize
)
528 #ifdef wxHAVE_RAW_BITMAP
530 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
531 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
534 #endif // wxHAVE_RAW_BITMAP
536 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
537 EVT_PAINT(MyCanvas::OnPaint
)
540 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
541 const wxPoint
&pos
, const wxSize
&size
)
542 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
543 , m_bmpSmileXpm(smile_xpm
)
544 , m_iconSmileXpm(smile_xpm
)
549 SetBackgroundColour(* wxWHITE
);
551 wxBitmap
bitmap( 100, 100 );
554 dc
.SelectObject( bitmap
);
555 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
556 dc
.SetPen( *wxBLACK_PEN
);
557 dc
.DrawRectangle( 0, 0, 100, 100 );
558 dc
.SetBrush( *wxWHITE_BRUSH
);
559 dc
.DrawRectangle( 20, 20, 60, 60 );
560 dc
.SelectObject( wxNullBitmap
);
562 // try to find the directory with our images
564 if ( wxFile::Exists(wxT("./horse.png")) )
566 else if ( wxFile::Exists(wxT("../horse.png")) )
569 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
571 wxImage image
= bitmap
.ConvertToImage();
574 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
575 wxLogError(wxT("Can't save file"));
579 if ( image
.LoadFile( dir
+ _T("test.png") ) )
580 my_square
= wxBitmap( image
);
584 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
585 wxLogError(wxT("Can't load PNG image"));
587 my_horse_png
= wxBitmap( image
);
589 if ( !image
.LoadFile( dir
+ _T("toucan.png")) )
590 wxLogError(wxT("Can't load PNG image"));
592 my_toucan
= wxBitmap(image
);
594 my_toucan_flipped_horiz
= wxBitmap(image
.Mirror(true));
595 my_toucan_flipped_vert
= wxBitmap(image
.Mirror(false));
596 my_toucan_flipped_both
= wxBitmap(image
.Mirror(true).Mirror(false));
597 my_toucan_grey
= wxBitmap(image
.ConvertToGreyscale());
598 my_toucan_head
= wxBitmap(image
.GetSubImage(wxRect(40, 7, 80, 60)));
599 my_toucan_scaled_normal
= wxBitmap(image
.Scale(110,90,wxIMAGE_QUALITY_NORMAL
));
600 my_toucan_scaled_high
= wxBitmap(image
.Scale(110,90,wxIMAGE_QUALITY_HIGH
));
601 my_toucan_blur
= wxBitmap(image
.Blur(10));
603 #endif // wxUSE_LIBPNG
608 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
609 wxLogError(wxT("Can't load JPG image"));
612 my_horse_jpeg
= wxBitmap( image
);
614 // Colorize by rotating green hue to red
615 wxImage::HSVValue greenHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0));
616 wxImage::HSVValue redHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0));
617 image
.RotateHue(redHSV
.hue
- greenHSV
.hue
);
618 colorized_horse_jpeg
= wxBitmap( image
);
621 if ( !image
.LoadFile( dir
+ _T("cmyk.jpg")) )
622 wxLogError(_T("Can't load CMYK JPG image"));
624 my_cmyk_jpeg
= wxBitmap(image
);
625 #endif // wxUSE_LIBJPEG
630 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
631 wxLogError(wxT("Can't load GIF image"));
633 my_horse_gif
= wxBitmap( image
);
639 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
640 wxLogError(wxT("Can't load PCX image"));
642 my_horse_pcx
= wxBitmap( image
);
647 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
648 wxLogError(wxT("Can't load BMP image"));
650 my_horse_bmp
= wxBitmap( image
);
655 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
656 wxLogError(wxT("Can't load XPM image"));
658 my_horse_xpm
= wxBitmap( image
);
660 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
661 wxLogError(wxT("Can't save file"));
667 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
668 wxLogError(wxT("Can't load PNM image"));
670 my_horse_pnm
= wxBitmap( image
);
674 if ( !image
.LoadFile( dir
+ _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM
) )
675 wxLogError(wxT("Can't load PNM image"));
677 my_horse_asciigrey_pnm
= wxBitmap( image
);
681 if ( !image
.LoadFile( dir
+ _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM
) )
682 wxLogError(wxT("Can't load PNM image"));
684 my_horse_rawgrey_pnm
= wxBitmap( image
);
690 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
691 wxLogError(wxT("Can't load TIFF image"));
693 my_horse_tiff
= wxBitmap( image
);
699 if ( !image
.LoadFile( dir
+ _T("horse.tga"), wxBITMAP_TYPE_TGA
) )
700 wxLogError(wxT("Can't load TGA image"));
702 my_horse_tga
= wxBitmap( image
);
705 CreateAntiAliasedBitmap();
707 my_smile_xbm
= wxBitmap( (const char*)smile_bits
, smile_width
,
710 // demonstrates XPM automatically using the mask when saving
711 if ( m_bmpSmileXpm
.Ok() )
712 m_bmpSmileXpm
.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM
);
717 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
718 wxLogError(wxT("Can't load first ICO image"));
720 my_horse_ico32
= wxBitmap( image
);
724 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
725 wxLogError(wxT("Can't load second ICO image"));
727 my_horse_ico16
= wxBitmap( image
);
731 if ( !image
.LoadFile( dir
+ _T("horse.ico") ) )
732 wxLogError(wxT("Can't load best ICO image"));
734 my_horse_ico
= wxBitmap( image
);
738 if ( !image
.LoadFile( dir
+ _T("horse.cur"), wxBITMAP_TYPE_CUR
) )
739 wxLogError(wxT("Can't load best ICO image"));
742 my_horse_cur
= wxBitmap( image
);
743 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
744 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
747 m_ani_images
= wxImage::GetImageCount ( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
);
749 wxLogError(wxT("No ANI-format images found"));
751 my_horse_ani
= new wxBitmap
[m_ani_images
];
753 for (i
=0; i
< m_ani_images
; i
++)
756 if (!image
.LoadFile( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
, i
))
758 wxString tmp
= wxT("Can't load image number ");
763 my_horse_ani
[i
] = wxBitmap( image
);
765 #endif // wxUSE_ICO_CUR
769 // test image loading from stream
770 wxFile
file(dir
+ _T("horse.bmp"));
771 if ( file
.IsOpened() )
773 wxFileOffset len
= file
.Length();
774 size_t dataSize
= (size_t)len
;
775 void *data
= malloc(dataSize
);
776 if ( file
.Read(data
, dataSize
) != len
)
777 wxLogError(_T("Reading bitmap file failed"));
780 wxMemoryInputStream
mis(data
, dataSize
);
781 if ( !image
.LoadFile(mis
) )
782 wxLogError(wxT("Can't load BMP image from stream"));
784 my_horse_bmp2
= wxBitmap( image
);
791 MyCanvas::~MyCanvas()
793 delete [] my_horse_ani
;
796 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
798 wxPaintDC
dc( this );
801 dc
.DrawText( _T("Loaded image"), 30, 10 );
803 dc
.DrawBitmap( my_square
, 30, 30 );
805 dc
.DrawText( _T("Drawn directly"), 150, 10 );
806 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
807 dc
.SetPen( *wxBLACK_PEN
);
808 dc
.DrawRectangle( 150, 30, 100, 100 );
809 dc
.SetBrush( *wxWHITE_BRUSH
);
810 dc
.DrawRectangle( 170, 50, 60, 60 );
813 dc
.DrawBitmap( my_anti
, 280, 30 );
815 dc
.DrawText( _T("PNG handler"), 30, 135 );
816 if (my_horse_png
.Ok())
818 dc
.DrawBitmap( my_horse_png
, 30, 150 );
819 wxRect
rect(0,0,100,100);
820 wxBitmap
sub( my_horse_png
.GetSubBitmap(rect
) );
821 dc
.DrawText( _T("GetSubBitmap()"), 280, 175 );
822 dc
.DrawBitmap( sub
, 280, 195 );
825 dc
.DrawText( _T("JPEG handler"), 30, 365 );
826 if (my_horse_jpeg
.Ok())
827 dc
.DrawBitmap( my_horse_jpeg
, 30, 380 );
829 dc
.DrawText( _T("Green rotated to red"), 280, 365 );
830 if (colorized_horse_jpeg
.Ok())
831 dc
.DrawBitmap( colorized_horse_jpeg
, 280, 380 );
833 dc
.DrawText( _T("CMYK JPEG image"), 530, 365 );
834 if (my_cmyk_jpeg
.Ok())
835 dc
.DrawBitmap( my_cmyk_jpeg
, 530, 380 );
837 dc
.DrawText( _T("GIF handler"), 30, 595 );
838 if (my_horse_gif
.Ok())
839 dc
.DrawBitmap( my_horse_gif
, 30, 610 );
841 dc
.DrawText( _T("PCX handler"), 30, 825 );
842 if (my_horse_pcx
.Ok())
843 dc
.DrawBitmap( my_horse_pcx
, 30, 840 );
845 dc
.DrawText( _T("BMP handler"), 30, 1055 );
846 if (my_horse_bmp
.Ok())
847 dc
.DrawBitmap( my_horse_bmp
, 30, 1070 );
849 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
850 if (my_horse_bmp2
.Ok())
851 dc
.DrawBitmap( my_horse_bmp2
, 280, 1070 );
853 dc
.DrawText( _T("PNM handler"), 30, 1285 );
854 if (my_horse_pnm
.Ok())
855 dc
.DrawBitmap( my_horse_pnm
, 30, 1300 );
857 dc
.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 );
858 if (my_horse_asciigrey_pnm
.Ok())
859 dc
.DrawBitmap( my_horse_asciigrey_pnm
, 280, 1300 );
861 dc
.DrawText( _T("PNM handler (raw grey)"), 530, 1285 );
862 if (my_horse_rawgrey_pnm
.Ok())
863 dc
.DrawBitmap( my_horse_rawgrey_pnm
, 530, 1300 );
865 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
866 if (my_horse_tiff
.Ok())
867 dc
.DrawBitmap( my_horse_tiff
, 30, 1530 );
869 dc
.DrawText( _T("TGA handler"), 30, 1745 );
870 if (my_horse_tga
.Ok())
871 dc
.DrawBitmap( my_horse_tga
, 30, 1760 );
873 dc
.DrawText( _T("XPM handler"), 30, 1975 );
874 if (my_horse_xpm
.Ok())
875 dc
.DrawBitmap( my_horse_xpm
, 30, 2000 );
879 int x
= 750, y
= 10, yy
= 170;
881 dc
.DrawText(wxT("Original toucan"), x
+50, y
);
882 dc
.DrawBitmap(my_toucan
, x
, y
+15, true);
884 dc
.DrawText(wxT("Flipped horizontally"), x
+50, y
);
885 dc
.DrawBitmap(my_toucan_flipped_horiz
, x
, y
+15, true);
887 dc
.DrawText(wxT("Flipped vertically"), x
+50, y
);
888 dc
.DrawBitmap(my_toucan_flipped_vert
, x
, y
+15, true);
890 dc
.DrawText(wxT("Flipped both h&v"), x
+50, y
);
891 dc
.DrawBitmap(my_toucan_flipped_both
, x
, y
+15, true);
894 dc
.DrawText(wxT("In greyscale"), x
+50, y
);
895 dc
.DrawBitmap(my_toucan_grey
, x
, y
+15, true);
898 dc
.DrawText(wxT("Toucan's head"), x
+50, y
);
899 dc
.DrawBitmap(my_toucan_head
, x
, y
+15, true);
902 dc
.DrawText(wxT("Scaled with normal quality"), x
+50, y
);
903 dc
.DrawBitmap(my_toucan_scaled_normal
, x
, y
+15, true);
906 dc
.DrawText(wxT("Scaled with high quality"), x
+50, y
);
907 dc
.DrawBitmap(my_toucan_scaled_high
, x
, y
+15, true);
910 dc
.DrawText(wxT("Blured"), x
+50, y
);
911 dc
.DrawBitmap(my_toucan_blur
, x
, y
+15, true);
914 if (my_smile_xbm
.Ok())
916 int x
= 300, y
= 1800;
918 dc
.DrawText( _T("XBM bitmap"), x
, y
);
919 dc
.DrawText( _T("(green on red)"), x
, y
+ 15 );
920 dc
.SetTextForeground( _T("GREEN") );
921 dc
.SetTextBackground( _T("RED") );
922 dc
.DrawBitmap( my_smile_xbm
, x
, y
+ 30 );
924 dc
.SetTextForeground( *wxBLACK
);
925 dc
.DrawText( _T("After wxImage conversion"), x
+ 120, y
);
926 dc
.DrawText( _T("(red on white)"), x
+ 120, y
+ 15 );
927 dc
.SetTextForeground( wxT("RED") );
928 wxImage i
= my_smile_xbm
.ConvertToImage();
929 i
.SetMaskColour( 255, 255, 255 );
931 wxRED_PEN
->GetColour().Red(),
932 wxRED_PEN
->GetColour().Green(),
933 wxRED_PEN
->GetColour().Blue() );
934 dc
.DrawBitmap( wxBitmap(i
), x
+ 120, y
+ 30, true );
935 dc
.SetTextForeground( *wxBLACK
);
939 wxBitmap
mono( 60,50,1 );
941 memdc
.SelectObject( mono
);
942 memdc
.SetPen( *wxBLACK_PEN
);
943 memdc
.SetBrush( *wxWHITE_BRUSH
);
944 memdc
.DrawRectangle( 0,0,60,50 );
945 memdc
.SetTextForeground( *wxBLACK
);
947 // I cannot convince GTK2 to draw into mono bitmaps
948 memdc
.DrawText( _T("Hi!"), 5, 5 );
950 memdc
.SetBrush( *wxBLACK_BRUSH
);
951 memdc
.DrawRectangle( 33,5,20,20 );
952 memdc
.SetPen( *wxRED_PEN
);
953 memdc
.DrawLine( 5, 42, 50, 42 );
954 memdc
.SelectObject( wxNullBitmap
);
958 int x
= 300, y
= 1900;
960 dc
.DrawText( _T("Mono bitmap"), x
, y
);
961 dc
.DrawText( _T("(red on green)"), x
, y
+ 15 );
962 dc
.SetTextForeground( wxT("RED") );
963 dc
.SetTextBackground( wxT("GREEN") );
964 dc
.DrawBitmap( mono
, x
, y
+ 30 );
966 dc
.SetTextForeground( *wxBLACK
);
967 dc
.DrawText( _T("After wxImage conversion"), x
+ 120, y
);
968 dc
.DrawText( _T("(red on white)"), x
+ 120, y
+ 15 );
969 dc
.SetTextForeground( wxT("RED") );
970 wxImage i
= mono
.ConvertToImage();
971 i
.SetMaskColour( 255,255,255 );
973 wxRED_PEN
->GetColour().Red(),
974 wxRED_PEN
->GetColour().Green(),
975 wxRED_PEN
->GetColour().Blue() );
976 dc
.DrawBitmap( wxBitmap(i
), x
+ 120, y
+ 30, true );
977 dc
.SetTextForeground( *wxBLACK
);
980 // For testing transparency
981 dc
.SetBrush( *wxRED_BRUSH
);
982 dc
.DrawRectangle( 20, 2220, 560, 68 );
984 dc
.DrawText(_T("XPM bitmap"), 30, 2230 );
985 if ( m_bmpSmileXpm
.Ok() )
986 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, true);
988 dc
.DrawText(_T("XPM icon"), 110, 2230 );
989 if ( m_iconSmileXpm
.Ok() )
990 dc
.DrawIcon(m_iconSmileXpm
, 110, 2250);
992 // testing icon -> bitmap conversion
993 wxBitmap
to_blit( m_iconSmileXpm
);
996 dc
.DrawText( _T("SubBitmap"), 170, 2230 );
997 wxBitmap sub
= to_blit
.GetSubBitmap( wxRect(0,0,15,15) );
999 dc
.DrawBitmap( sub
, 170, 2250, true );
1001 dc
.DrawText( _T("Enlarged"), 250, 2230 );
1002 dc
.SetUserScale( 1.5, 1.5 );
1003 dc
.DrawBitmap( to_blit
, (int)(250/1.5), (int)(2250/1.5), true );
1004 dc
.SetUserScale( 2, 2 );
1005 dc
.DrawBitmap( to_blit
, (int)(300/2), (int)(2250/2), true );
1006 dc
.SetUserScale( 1.0, 1.0 );
1008 dc
.DrawText( _T("Blit"), 400, 2230);
1010 blit_dc
.SelectObject( to_blit
);
1011 dc
.Blit( 400, 2250, to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
1012 dc
.SetUserScale( 1.5, 1.5 );
1013 dc
.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
1014 dc
.SetUserScale( 2, 2 );
1015 dc
.Blit( (int)(500/2), (int)(2250/2), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
1016 dc
.SetUserScale( 1.0, 1.0 );
1019 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
1020 if (my_horse_ico32
.Ok())
1021 dc
.DrawBitmap( my_horse_ico32
, 30, 2330, true );
1023 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
1024 if (my_horse_ico16
.Ok())
1025 dc
.DrawBitmap( my_horse_ico16
, 230, 2330, true );
1027 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
1028 if (my_horse_ico
.Ok())
1029 dc
.DrawBitmap( my_horse_ico
, 430, 2330, true );
1031 dc
.DrawText( _T("CUR handler"), 30, 2390 );
1032 if (my_horse_cur
.Ok())
1034 dc
.DrawBitmap( my_horse_cur
, 30, 2420, true );
1035 dc
.SetPen (*wxRED_PEN
);
1036 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
1037 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
1040 dc
.DrawText( _T("ANI handler"), 230, 2390 );
1041 for ( int i
=0; i
< m_ani_images
; i
++ )
1043 if (my_horse_ani
[i
].Ok())
1045 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, true );
1050 void MyCanvas::CreateAntiAliasedBitmap()
1052 wxBitmap
bitmap( 300, 300 );
1056 dc
.SelectObject( bitmap
);
1060 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
1061 dc
.SetTextForeground( wxT("RED") );
1062 dc
.DrawText( _T("This is anti-aliased Text."), 20, 5 );
1063 dc
.DrawText( _T("And a Rectangle."), 20, 45 );
1065 dc
.SetBrush( *wxRED_BRUSH
);
1066 dc
.SetPen( *wxTRANSPARENT_PEN
);
1067 dc
.DrawRoundedRectangle( 20, 85, 200, 180, 20 );
1069 wxImage original
= bitmap
.ConvertToImage();
1070 wxImage
anti( 150, 150 );
1072 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
1074 for (int y
= 1; y
< 149; y
++)
1075 for (int x
= 1; x
< 149; x
++)
1077 int red
= original
.GetRed( x
*2, y
*2 ) +
1078 original
.GetRed( x
*2-1, y
*2 ) +
1079 original
.GetRed( x
*2, y
*2+1 ) +
1080 original
.GetRed( x
*2+1, y
*2+1 );
1083 int green
= original
.GetGreen( x
*2, y
*2 ) +
1084 original
.GetGreen( x
*2-1, y
*2 ) +
1085 original
.GetGreen( x
*2, y
*2+1 ) +
1086 original
.GetGreen( x
*2+1, y
*2+1 );
1089 int blue
= original
.GetBlue( x
*2, y
*2 ) +
1090 original
.GetBlue( x
*2-1, y
*2 ) +
1091 original
.GetBlue( x
*2, y
*2+1 ) +
1092 original
.GetBlue( x
*2+1, y
*2+1 );
1094 anti
.SetRGB( x
, y
, (unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
1096 my_anti
= wxBitmap(anti
);
1103 ID_QUIT
= wxID_EXIT
,
1104 ID_ABOUT
= wxID_ABOUT
,
1109 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
1111 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
1112 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
1113 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
1114 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
1115 #ifdef wxHAVE_RAW_BITMAP
1116 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
1120 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
1121 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
1122 #endif // wxUSE_CLIPBOARD
1126 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxImage sample"),
1127 wxPoint(20, 20), wxSize(950, 700) )
1129 wxMenuBar
*menu_bar
= new wxMenuBar();
1131 wxMenu
*menuImage
= new wxMenu
;
1132 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
1134 #ifdef wxHAVE_RAW_BITMAP
1135 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
1137 menuImage
->AppendSeparator();
1138 menuImage
->Append( ID_ABOUT
, _T("&About..."));
1139 menuImage
->AppendSeparator();
1140 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
1141 menu_bar
->Append(menuImage
, _T("&Image"));
1144 wxMenu
*menuClipboard
= new wxMenu
;
1145 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
1146 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
1147 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
1148 #endif // wxUSE_CLIPBOARD
1150 SetMenuBar( menu_bar
);
1154 int widths
[] = { -1, 100 };
1155 SetStatusWidths( 2, widths
);
1156 #endif // wxUSE_STATUSBAR
1158 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
1160 // 500 width * 2750 height
1161 m_canvas
->SetScrollbars( 10, 10, 50, 275 );
1164 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
1169 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
1171 (void)wxMessageBox( _T("wxImage demo\n")
1172 _T("Robert Roebling (c) 1998,2000"),
1173 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
1176 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
1179 wxString filename
= wxFileSelector(_T("Select image file"));
1184 if ( !image
.LoadFile(filename
) )
1186 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
1191 (new MyImageFrame(this, filename
, wxBitmap(image
)))->Show();
1192 #endif // wxUSE_FILEDLG
1195 #ifdef wxHAVE_RAW_BITMAP
1197 void MyFrame::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
1199 (new MyRawBitmapFrame(this))->Show();
1202 #endif // wxHAVE_RAW_BITMAP
1206 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1208 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
1209 dobjBmp
->SetBitmap(m_canvas
->my_horse_png
);
1211 wxTheClipboard
->Open();
1213 if ( !wxTheClipboard
->SetData(dobjBmp
) )
1215 wxLogError(_T("Failed to copy bitmap to clipboard"));
1218 wxTheClipboard
->Close();
1221 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1223 wxBitmapDataObject dobjBmp
;
1225 wxTheClipboard
->Open();
1226 if ( !wxTheClipboard
->GetData(dobjBmp
) )
1228 wxLogMessage(_T("No bitmap data in the clipboard"));
1232 (new MyImageFrame(this, _T("Clipboard"), dobjBmp
.GetBitmap()))->Show();
1234 wxTheClipboard
->Close();
1237 #endif // wxUSE_CLIPBOARD
1239 //-----------------------------------------------------------------------------
1241 //-----------------------------------------------------------------------------
1243 bool MyApp::OnInit()
1245 if ( !wxApp::OnInit() )
1248 wxInitAllImageHandlers();
1250 wxFrame
*frame
= new MyFrame();
1251 frame
->Show( true );