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
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_xpm
;
74 wxBitmap
*my_horse_ico32
;
75 wxBitmap
*my_horse_ico16
;
76 wxBitmap
*my_horse_ico
;
77 wxBitmap
*my_horse_cur
;
78 wxBitmap
*my_horse_ani
;
80 wxBitmap
*my_smile_xbm
;
84 wxBitmap
*my_horse_asciigrey_pnm
;
85 wxBitmap
*my_horse_rawgrey_pnm
;
87 wxBitmap
*colorized_horse_jpeg
;
93 wxBitmap m_bmpSmileXpm
;
94 wxIcon m_iconSmileXpm
;
97 DECLARE_DYNAMIC_CLASS(MyCanvas
)
105 class MyFrame
: public wxFrame
110 void OnAbout( wxCommandEvent
&event
);
111 void OnNewFrame( wxCommandEvent
&event
);
112 #ifdef wxHAVE_RAW_BITMAP
113 void OnTestRawBitmap( wxCommandEvent
&event
);
114 #endif // wxHAVE_RAW_BITMAP
115 void OnQuit( wxCommandEvent
&event
);
118 void OnCopy(wxCommandEvent
& event
);
119 void OnPaste(wxCommandEvent
& event
);
120 #endif // wxUSE_CLIPBOARD
125 DECLARE_DYNAMIC_CLASS(MyFrame
)
126 DECLARE_EVENT_TABLE()
129 class MyImageFrame
: public wxFrame
132 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
133 : wxFrame(parent
, wxID_ANY
, _T("Double click to save"),
134 wxDefaultPosition
, wxDefaultSize
,
135 wxCAPTION
| wxSYSTEM_MENU
| wxCLOSE_BOX
),
138 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
141 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
143 // do nothing here to be able to see how transparent images are shown
146 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
148 wxPaintDC
dc( this );
149 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
152 void OnSave(wxMouseEvent
& WXUNUSED(event
))
154 wxImage image
= m_bitmap
.ConvertToImage();
156 wxString savefilename
= wxFileSelector( wxT("Save Image"),
159 (const wxChar
*)NULL
,
160 wxT("BMP files (*.bmp)|*.bmp|")
161 wxT("PNG files (*.png)|*.png|")
162 wxT("JPEG files (*.jpg)|*.jpg|")
163 wxT("GIF files (*.gif)|*.gif|")
164 wxT("TIFF files (*.tif)|*.tif|")
165 wxT("PCX files (*.pcx)|*.pcx|")
166 wxT("ICO files (*.ico)|*.ico|")
167 wxT("CUR files (*.cur)|*.cur"),
171 if ( savefilename
.empty() )
175 wxFileName::SplitPath(savefilename
, NULL
, NULL
, &extension
);
178 if ( extension
== _T("bpp") )
180 static const int bppvalues
[] =
192 const wxString bppchoices
[] =
198 _T("8 bpp greyscale"),
200 _T("8 bpp own palette"),
204 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
205 _T("Image sample: save file"),
206 WXSIZEOF(bppchoices
),
209 if ( bppselection
!= -1 )
211 int format
= bppvalues
[bppselection
];
212 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, format
);
214 if ( format
== wxBMP_8BPP_PALETTE
)
216 unsigned char *cmap
= new unsigned char [256];
217 for ( int i
= 0; i
< 256; i
++ )
218 cmap
[i
] = (unsigned char)i
;
219 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
225 else if ( extension
== _T("png") )
227 static const int pngvalues
[] =
237 const wxString pngchoices
[] =
244 _T("Grey red 16bpp"),
247 int sel
= wxGetSingleChoiceIndex(_T("Set PNG format"),
248 _T("Image sample: save file"),
249 WXSIZEOF(pngchoices
),
254 image
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, pngvalues
[sel
]);
255 image
.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH
, sel
% 2 ? 16 : 8);
258 else if ( extension
== _T("cur") )
260 image
.Rescale(32,32);
261 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
262 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
263 // This shows how you can save an image with explicitly
264 // specified image format:
265 saved
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
270 // This one guesses image format from filename extension
271 // (it may fail if the extension is not recognized):
272 image
.SaveFile(savefilename
);
279 DECLARE_EVENT_TABLE()
282 #ifdef wxHAVE_RAW_BITMAP
284 #include "wx/rawbmp.h"
286 class MyRawBitmapFrame
: public wxFrame
293 REAL_SIZE
= SIZE
- 2*BORDER
296 MyRawBitmapFrame(wxFrame
*parent
)
297 : wxFrame(parent
, wxID_ANY
, _T("Raw bitmaps (how exciting)")),
298 m_bitmap(SIZE
, SIZE
, 32)
300 SetClientSize(SIZE
, SIZE
);
302 // another possibility: wxNativePixelData (don't forget to remove code
303 // setting alpha in the loop below then)
304 typedef wxAlphaPixelData Data
;
306 Data
data(m_bitmap
, wxPoint(BORDER
, BORDER
), wxSize(REAL_SIZE
, REAL_SIZE
));
309 wxLogError(_T("Failed to gain raw access to bitmap data"));
315 Data::Iterator
p(data
);
317 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
319 Data::Iterator rowStart
= p
;
321 int r
= y
< REAL_SIZE
/3 ? 255 : 0,
322 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ? 255 : 0,
323 b
= 2*(REAL_SIZE
/3) <= y
? 255 : 0;
325 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
327 // note that RGB must be premultiplied by alpha
328 unsigned a
= (Data::Iterator::ChannelType
)((x
*255.)/REAL_SIZE
);
329 p
.Red() = r
* a
/ 256;
330 p
.Green() = g
* a
/ 256;
331 p
.Blue() = b
* a
/ 256;
334 ++p
; // same as p.OffsetX(1)
342 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
344 wxPaintDC
dc( this );
345 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER
);
346 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
347 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
348 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
354 DECLARE_EVENT_TABLE()
357 #endif // wxHAVE_RAW_BITMAP
361 class MyApp
: public wxApp
364 virtual bool OnInit();
373 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
375 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
376 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
377 EVT_PAINT(MyImageFrame::OnPaint
)
378 EVT_LEFT_DCLICK(MyImageFrame::OnSave
)
381 #ifdef wxHAVE_RAW_BITMAP
383 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
384 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
387 #endif // wxHAVE_RAW_BITMAP
389 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
390 EVT_PAINT(MyCanvas::OnPaint
)
393 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
394 const wxPoint
&pos
, const wxSize
&size
)
395 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
396 , m_bmpSmileXpm((const char **) smile_xpm
)
397 , m_iconSmileXpm((const char **) smile_xpm
)
399 my_horse_png
= (wxBitmap
*) NULL
;
400 my_horse_jpeg
= (wxBitmap
*) NULL
;
401 my_horse_gif
= (wxBitmap
*) NULL
;
402 my_horse_bmp
= (wxBitmap
*) NULL
;
403 my_horse_bmp2
= (wxBitmap
*) NULL
;
404 my_horse_pcx
= (wxBitmap
*) NULL
;
405 my_horse_pnm
= (wxBitmap
*) NULL
;
406 my_horse_tiff
= (wxBitmap
*) NULL
;
407 my_horse_xpm
= (wxBitmap
*) NULL
;
408 my_horse_ico32
= (wxBitmap
*) NULL
;
409 my_horse_ico16
= (wxBitmap
*) NULL
;
410 my_horse_ico
= (wxBitmap
*) NULL
;
411 my_horse_cur
= (wxBitmap
*) NULL
;
412 my_horse_ani
= (wxBitmap
*) NULL
;
413 colorized_horse_jpeg
= (wxBitmap
*) NULL
;
415 my_smile_xbm
= (wxBitmap
*) NULL
;
416 my_square
= (wxBitmap
*) NULL
;
417 my_anti
= (wxBitmap
*) NULL
;
419 my_horse_asciigrey_pnm
= (wxBitmap
*) NULL
;
420 my_horse_rawgrey_pnm
= (wxBitmap
*) NULL
;
424 SetBackgroundColour(* wxWHITE
);
426 wxBitmap
bitmap( 100, 100 );
429 dc
.SelectObject( bitmap
);
430 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
431 dc
.SetPen( *wxBLACK_PEN
);
432 dc
.DrawRectangle( 0, 0, 100, 100 );
433 dc
.SetBrush( *wxWHITE_BRUSH
);
434 dc
.DrawRectangle( 20, 20, 60, 60 );
435 dc
.SelectObject( wxNullBitmap
);
437 // try to find the directory with our images
439 if ( wxFile::Exists(wxT("./horse.png")) )
441 else if ( wxFile::Exists(wxT("../horse.png")) )
444 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
446 wxImage image
= bitmap
.ConvertToImage();
449 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
450 wxLogError(wxT("Can't save file"));
454 if ( image
.LoadFile( dir
+ _T("test.png") ) )
455 my_square
= new wxBitmap( image
);
459 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
460 wxLogError(wxT("Can't load PNG image"));
462 my_horse_png
= new wxBitmap( image
);
463 #endif // wxUSE_LIBPNG
468 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
469 wxLogError(wxT("Can't load JPG image"));
472 my_horse_jpeg
= new wxBitmap( image
);
473 // Colorize by rotating green hue to red
474 wxImage::HSVValue greenHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0));
475 wxImage::HSVValue redHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0));
476 image
.RotateHue(redHSV
.hue
- greenHSV
.hue
);
477 colorized_horse_jpeg
= new wxBitmap( image
);
479 #endif // wxUSE_LIBJPEG
484 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
485 wxLogError(wxT("Can't load GIF image"));
487 my_horse_gif
= new wxBitmap( image
);
493 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
494 wxLogError(wxT("Can't load PCX image"));
496 my_horse_pcx
= new wxBitmap( image
);
501 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
502 wxLogError(wxT("Can't load BMP image"));
504 my_horse_bmp
= new wxBitmap( image
);
509 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
510 wxLogError(wxT("Can't load XPM image"));
512 my_horse_xpm
= new wxBitmap( image
);
514 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
515 wxLogError(wxT("Can't save file"));
521 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
522 wxLogError(wxT("Can't load PNM image"));
524 my_horse_pnm
= new wxBitmap( image
);
528 if ( !image
.LoadFile( dir
+ _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM
) )
529 wxLogError(wxT("Can't load PNM image"));
531 my_horse_asciigrey_pnm
= new wxBitmap( image
);
535 if ( !image
.LoadFile( dir
+ _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM
) )
536 wxLogError(wxT("Can't load PNM image"));
538 my_horse_rawgrey_pnm
= new wxBitmap( image
);
544 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
545 wxLogError(wxT("Can't load TIFF image"));
547 my_horse_tiff
= new wxBitmap( image
);
550 CreateAntiAliasedBitmap();
552 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
555 // demonstrates XPM automatically using the mask when saving
556 if ( m_bmpSmileXpm
.Ok() )
557 m_bmpSmileXpm
.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM
);
562 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
563 wxLogError(wxT("Can't load first ICO image"));
565 my_horse_ico32
= new wxBitmap( image
);
569 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
570 wxLogError(wxT("Can't load second ICO image"));
572 my_horse_ico16
= new wxBitmap( image
);
576 if ( !image
.LoadFile( dir
+ _T("horse.ico") ) )
577 wxLogError(wxT("Can't load best ICO image"));
579 my_horse_ico
= new wxBitmap( image
);
583 if ( !image
.LoadFile( dir
+ _T("horse.cur"), wxBITMAP_TYPE_CUR
) )
584 wxLogError(wxT("Can't load best ICO image"));
587 my_horse_cur
= new wxBitmap( image
);
588 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
589 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
592 m_ani_images
= wxImage::GetImageCount ( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
);
594 wxLogError(wxT("No ANI-format images found"));
596 my_horse_ani
= new wxBitmap
[m_ani_images
];
598 for (i
=0; i
< m_ani_images
; i
++)
601 if (!image
.LoadFile( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
, i
))
603 wxString tmp
= wxT("Can't load image number ");
608 my_horse_ani
[i
] = wxBitmap( image
);
610 #endif // wxUSE_ICO_CUR
614 // test image loading from stream
615 wxFile
file(dir
+ _T("horse.bmp"));
616 if ( file
.IsOpened() )
618 wxFileOffset len
= file
.Length();
619 size_t dataSize
= (size_t)len
;
620 void *data
= malloc(dataSize
);
621 if ( file
.Read(data
, dataSize
) != len
)
622 wxLogError(_T("Reading bitmap file failed"));
625 wxMemoryInputStream
mis(data
, dataSize
);
626 if ( !image
.LoadFile(mis
) )
627 wxLogError(wxT("Can't load BMP image from stream"));
629 my_horse_bmp2
= new wxBitmap( image
);
636 MyCanvas::~MyCanvas()
640 delete my_horse_jpeg
;
643 delete my_horse_bmp2
;
645 delete my_horse_tiff
;
647 delete my_horse_ico32
;
648 delete my_horse_ico16
;
651 delete [] my_horse_ani
;
655 delete my_horse_asciigrey_pnm
;
656 delete my_horse_rawgrey_pnm
;
657 delete colorized_horse_jpeg
;
660 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
662 wxPaintDC
dc( this );
665 dc
.DrawText( _T("Loaded image"), 30, 10 );
666 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
668 dc
.DrawText( _T("Drawn directly"), 150, 10 );
669 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
670 dc
.SetPen( *wxBLACK_PEN
);
671 dc
.DrawRectangle( 150, 30, 100, 100 );
672 dc
.SetBrush( *wxWHITE_BRUSH
);
673 dc
.DrawRectangle( 170, 50, 60, 60 );
675 if (my_anti
&& my_anti
->Ok())
676 dc
.DrawBitmap( *my_anti
, 280, 30 );
678 dc
.DrawText( _T("PNG handler"), 30, 135 );
679 if (my_horse_png
&& my_horse_png
->Ok())
681 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
682 wxRect
rect(0,0,100,100);
683 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
684 dc
.DrawText( _T("GetSubBitmap()"), 280, 190 );
685 dc
.DrawBitmap( sub
, 280, 210 );
688 dc
.DrawText( _T("JPEG handler"), 30, 365 );
689 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
690 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
692 dc
.DrawText( _T("GIF handler"), 30, 595 );
693 if (my_horse_gif
&& my_horse_gif
->Ok())
694 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
696 dc
.DrawText( _T("PCX handler"), 30, 825 );
697 if (my_horse_pcx
&& my_horse_pcx
->Ok())
698 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
700 dc
.DrawText( _T("BMP handler"), 30, 1055 );
701 if (my_horse_bmp
&& my_horse_bmp
->Ok())
702 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
704 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
705 if (my_horse_bmp2
&& my_horse_bmp2
->Ok())
706 dc
.DrawBitmap( *my_horse_bmp2
, 280, 1070 );
708 dc
.DrawText( _T("PNM handler"), 30, 1285 );
709 if (my_horse_pnm
&& my_horse_pnm
->Ok())
710 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
712 dc
.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 );
713 if (my_horse_asciigrey_pnm
&& my_horse_asciigrey_pnm
->Ok())
714 dc
.DrawBitmap( *my_horse_asciigrey_pnm
, 280, 1300 );
716 dc
.DrawText( _T("PNM handler (raw grey)"), 530, 1285 );
717 if (my_horse_rawgrey_pnm
&& my_horse_rawgrey_pnm
->Ok())
718 dc
.DrawBitmap( *my_horse_rawgrey_pnm
, 530, 1300 );
720 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
721 if (my_horse_tiff
&& my_horse_tiff
->Ok())
722 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
724 dc
.DrawText( _T("XPM handler"), 30, 1745 );
725 if (my_horse_xpm
&& my_horse_xpm
->Ok())
726 dc
.DrawBitmap( *my_horse_xpm
, 30, 1760 );
729 if (my_smile_xbm
&& my_smile_xbm
->Ok())
731 dc
.DrawText( _T("XBM bitmap"), 30, 1975 );
732 dc
.DrawText( _T("(green on red)"), 30, 1990 );
733 dc
.SetTextForeground( _T("GREEN") );
734 dc
.SetTextBackground( _T("RED") );
735 dc
.DrawBitmap( *my_smile_xbm
, 30, 2010 );
737 dc
.SetTextForeground( wxT("BLACK") );
738 dc
.DrawText( _T("After wxImage conversion"), 150, 1975 );
739 dc
.DrawText( _T("(red on white)"), 150, 1990 );
740 dc
.SetTextForeground( wxT("RED") );
741 wxImage i
= my_smile_xbm
->ConvertToImage();
742 i
.SetMaskColour( 255, 255, 255 );
744 wxRED_PEN
->GetColour().Red(),
745 wxRED_PEN
->GetColour().Green(),
746 wxRED_PEN
->GetColour().Blue() );
747 dc
.DrawBitmap( wxBitmap(i
), 150, 2010, true );
748 dc
.SetTextForeground( wxT("BLACK") );
752 wxBitmap
mono( 60,50,1 );
754 memdc
.SelectObject( mono
);
755 memdc
.SetPen( *wxBLACK_PEN
);
756 memdc
.SetBrush( *wxWHITE_BRUSH
);
757 memdc
.DrawRectangle( 0,0,60,50 );
758 memdc
.SetTextForeground( *wxBLACK
);
760 // I cannot convince GTK2 to draw into mono bitmaps
761 memdc
.DrawText( _T("Hi!"), 5, 5 );
763 memdc
.SetBrush( *wxBLACK_BRUSH
);
764 memdc
.DrawRectangle( 33,5,20,20 );
765 memdc
.SetPen( *wxRED_PEN
);
766 memdc
.DrawLine( 5, 42, 50, 42 );
767 memdc
.SelectObject( wxNullBitmap
);
771 dc
.DrawText( _T("Mono bitmap"), 30, 2095 );
772 dc
.DrawText( _T("(red on green)"), 30, 2110 );
773 dc
.SetTextForeground( wxT("RED") );
774 dc
.SetTextBackground( wxT("GREEN") );
775 dc
.DrawBitmap( mono
, 30, 2130 );
777 dc
.SetTextForeground( wxT("BLACK") );
778 dc
.DrawText( _T("After wxImage conversion"), 150, 2095 );
779 dc
.DrawText( _T("(red on white)"), 150, 2110 );
780 dc
.SetTextForeground( wxT("RED") );
781 wxImage i
= mono
.ConvertToImage();
782 i
.SetMaskColour( 255,255,255 );
784 wxRED_PEN
->GetColour().Red(),
785 wxRED_PEN
->GetColour().Green(),
786 wxRED_PEN
->GetColour().Blue() );
787 dc
.DrawBitmap( wxBitmap(i
), 150, 2130, true );
788 dc
.SetTextForeground( wxT("BLACK") );
791 // For testing transparency
792 dc
.SetBrush( *wxRED_BRUSH
);
793 dc
.DrawRectangle( 20, 2220, 560, 68 );
795 dc
.DrawText(_T("XPM bitmap"), 30, 2230 );
796 if ( m_bmpSmileXpm
.Ok() )
797 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, true);
799 dc
.DrawText(_T("XPM icon"), 110, 2230 );
800 if ( m_iconSmileXpm
.Ok() )
801 dc
.DrawIcon(m_iconSmileXpm
, 110, 2250);
803 // testing icon -> bitmap conversion
804 wxBitmap
to_blit( m_iconSmileXpm
);
807 dc
.DrawText( _T("SubBitmap"), 170, 2230 );
808 wxBitmap sub
= to_blit
.GetSubBitmap( wxRect(0,0,15,15) );
810 dc
.DrawBitmap( sub
, 170, 2250, true );
812 dc
.DrawText( _T("Enlarged"), 250, 2230 );
813 dc
.SetUserScale( 1.5, 1.5 );
814 dc
.DrawBitmap( to_blit
, (int)(250/1.5), (int)(2250/1.5), true );
815 dc
.SetUserScale( 2, 2 );
816 dc
.DrawBitmap( to_blit
, (int)(300/2), (int)(2250/2), true );
817 dc
.SetUserScale( 1.0, 1.0 );
819 dc
.DrawText( _T("Blit"), 400, 2230);
821 blit_dc
.SelectObject( to_blit
);
822 dc
.Blit( 400, 2250, to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
823 dc
.SetUserScale( 1.5, 1.5 );
824 dc
.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
825 dc
.SetUserScale( 2, 2 );
826 dc
.Blit( (int)(500/2), (int)(2250/2), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
827 dc
.SetUserScale( 1.0, 1.0 );
830 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
831 if (my_horse_ico32
&& my_horse_ico32
->Ok())
832 dc
.DrawBitmap( *my_horse_ico32
, 30, 2330, true );
834 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
835 if (my_horse_ico16
&& my_horse_ico16
->Ok())
836 dc
.DrawBitmap( *my_horse_ico16
, 230, 2330, true );
838 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
839 if (my_horse_ico
&& my_horse_ico
->Ok())
840 dc
.DrawBitmap( *my_horse_ico
, 430, 2330, true );
842 dc
.DrawText( _T("CUR handler"), 30, 2390 );
843 if (my_horse_cur
&& my_horse_cur
->Ok())
845 dc
.DrawBitmap( *my_horse_cur
, 30, 2420, true );
846 dc
.SetPen (*wxRED_PEN
);
847 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
848 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
850 dc
.DrawText( _T("ANI handler"), 230, 2390 );
852 for (i
=0; i
< m_ani_images
; i
++)
853 if (my_horse_ani
[i
].Ok())
855 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, true );
858 if (colorized_horse_jpeg
)
860 dc
.DrawText( _T("Colorize image by rotating green hue to red"), 30, 2490 );
861 dc
.DrawBitmap( *colorized_horse_jpeg
, 30, 2520 );
863 #endif // wxUSE_LIBJPEG
867 void MyCanvas::CreateAntiAliasedBitmap()
869 wxBitmap
bitmap( 300, 300 );
873 dc
.SelectObject( bitmap
);
877 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
878 dc
.SetTextForeground( wxT("RED") );
879 dc
.DrawText( _T("This is anti-aliased Text."), 20, 20 );
880 dc
.DrawText( _T("And a Rectangle."), 20, 60 );
882 dc
.SetBrush( *wxRED_BRUSH
);
883 dc
.SetPen( *wxTRANSPARENT_PEN
);
884 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
886 wxImage original
= bitmap
.ConvertToImage();
887 wxImage
anti( 150, 150 );
889 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
891 for (int y
= 1; y
< 149; y
++)
892 for (int x
= 1; x
< 149; x
++)
894 int red
= original
.GetRed( x
*2, y
*2 ) +
895 original
.GetRed( x
*2-1, y
*2 ) +
896 original
.GetRed( x
*2, y
*2+1 ) +
897 original
.GetRed( x
*2+1, y
*2+1 );
900 int green
= original
.GetGreen( x
*2, y
*2 ) +
901 original
.GetGreen( x
*2-1, y
*2 ) +
902 original
.GetGreen( x
*2, y
*2+1 ) +
903 original
.GetGreen( x
*2+1, y
*2+1 );
906 int blue
= original
.GetBlue( x
*2, y
*2 ) +
907 original
.GetBlue( x
*2-1, y
*2 ) +
908 original
.GetBlue( x
*2, y
*2+1 ) +
909 original
.GetBlue( x
*2+1, y
*2+1 );
911 anti
.SetRGB( x
, y
, (unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
913 my_anti
= new wxBitmap(anti
);
921 ID_ABOUT
= wxID_ABOUT
,
926 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
928 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
929 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
930 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
931 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
932 #ifdef wxHAVE_RAW_BITMAP
933 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
937 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
938 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
939 #endif // wxUSE_CLIPBOARD
943 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxImage sample"),
944 wxPoint(20,20), wxSize(470,360) )
946 wxMenuBar
*menu_bar
= new wxMenuBar();
948 wxMenu
*menuImage
= new wxMenu
;
949 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
951 #ifdef wxHAVE_RAW_BITMAP
952 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
954 menuImage
->AppendSeparator();
955 menuImage
->Append( ID_ABOUT
, _T("&About..."));
956 menuImage
->AppendSeparator();
957 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
958 menu_bar
->Append(menuImage
, _T("&Image"));
961 wxMenu
*menuClipboard
= new wxMenu
;
962 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
963 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
964 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
965 #endif // wxUSE_CLIPBOARD
967 SetMenuBar( menu_bar
);
971 int widths
[] = { -1, 100 };
972 SetStatusWidths( 2, widths
);
973 #endif // wxUSE_STATUSBAR
975 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
977 // 500 width * 2750 height
978 m_canvas
->SetScrollbars( 10, 10, 50, 275 );
981 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
986 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
988 (void)wxMessageBox( _T("wxImage demo\n")
989 _T("Robert Roebling (c) 1998,2000"),
990 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
993 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
995 wxString filename
= wxFileSelector(_T("Select image file"));
1000 if ( !image
.LoadFile(filename
) )
1002 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
1007 (new MyImageFrame(this, wxBitmap(image
)))->Show();
1010 #ifdef wxHAVE_RAW_BITMAP
1012 void MyFrame::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
1014 (new MyRawBitmapFrame(this))->Show();
1017 #endif // wxHAVE_RAW_BITMAP
1021 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1023 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
1024 dobjBmp
->SetBitmap(*m_canvas
->my_horse_png
);
1026 wxTheClipboard
->Open();
1028 if ( !wxTheClipboard
->SetData(dobjBmp
) )
1030 wxLogError(_T("Failed to copy bitmap to clipboard"));
1033 wxTheClipboard
->Close();
1036 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1038 wxBitmapDataObject dobjBmp
;
1040 wxTheClipboard
->Open();
1041 if ( !wxTheClipboard
->GetData(dobjBmp
) )
1043 wxLogMessage(_T("No bitmap data in the clipboard"));
1047 (new MyImageFrame(this, dobjBmp
.GetBitmap()))->Show();
1049 wxTheClipboard
->Close();
1052 #endif // wxUSE_CLIPBOARD
1054 //-----------------------------------------------------------------------------
1056 //-----------------------------------------------------------------------------
1058 bool MyApp::OnInit()
1061 wxImage::AddHandler( new wxPNGHandler
);
1065 wxImage::AddHandler( new wxJPEGHandler
);
1069 wxImage::AddHandler( new wxTIFFHandler
);
1073 wxImage::AddHandler( new wxGIFHandler
);
1077 wxImage::AddHandler( new wxPCXHandler
);
1081 wxImage::AddHandler( new wxPNMHandler
);
1085 wxImage::AddHandler( new wxXPMHandler
);
1089 wxImage::AddHandler( new wxICOHandler
);
1090 wxImage::AddHandler( new wxCURHandler
);
1091 wxImage::AddHandler( new wxANIHandler
);
1094 wxFrame
*frame
= new MyFrame();
1095 frame
->Show( true );