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
))
155 wxImage image
= m_bitmap
.ConvertToImage();
157 wxString savefilename
= wxFileSelector( wxT("Save Image"),
160 (const wxChar
*)NULL
,
161 wxT("BMP files (*.bmp)|*.bmp|")
162 wxT("PNG files (*.png)|*.png|")
163 wxT("JPEG files (*.jpg)|*.jpg|")
164 wxT("GIF files (*.gif)|*.gif|")
165 wxT("TIFF files (*.tif)|*.tif|")
166 wxT("PCX files (*.pcx)|*.pcx|")
167 wxT("ICO files (*.ico)|*.ico|")
168 wxT("CUR files (*.cur)|*.cur"),
172 if ( savefilename
.empty() )
176 wxFileName::SplitPath(savefilename
, NULL
, NULL
, &extension
);
179 if ( extension
== _T("bpp") )
181 static const int bppvalues
[] =
193 const wxString bppchoices
[] =
199 _T("8 bpp greyscale"),
201 _T("8 bpp own palette"),
205 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
206 _T("Image sample: save file"),
207 WXSIZEOF(bppchoices
),
210 if ( bppselection
!= -1 )
212 int format
= bppvalues
[bppselection
];
213 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, format
);
215 if ( format
== wxBMP_8BPP_PALETTE
)
217 unsigned char *cmap
= new unsigned char [256];
218 for ( int i
= 0; i
< 256; i
++ )
219 cmap
[i
] = (unsigned char)i
;
220 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
226 else if ( extension
== _T("png") )
228 static const int pngvalues
[] =
238 const wxString pngchoices
[] =
245 _T("Grey red 16bpp"),
248 int sel
= wxGetSingleChoiceIndex(_T("Set PNG format"),
249 _T("Image sample: save file"),
250 WXSIZEOF(pngchoices
),
255 image
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, pngvalues
[sel
]);
256 image
.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH
, sel
% 2 ? 16 : 8);
259 else if ( extension
== _T("cur") )
261 image
.Rescale(32,32);
262 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
263 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
264 // This shows how you can save an image with explicitly
265 // specified image format:
266 saved
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
271 // This one guesses image format from filename extension
272 // (it may fail if the extension is not recognized):
273 image
.SaveFile(savefilename
);
275 #endif // wxUSE_FILEDLG
281 DECLARE_EVENT_TABLE()
284 #ifdef wxHAVE_RAW_BITMAP
286 #include "wx/rawbmp.h"
288 class MyRawBitmapFrame
: public wxFrame
295 REAL_SIZE
= SIZE
- 2*BORDER
298 MyRawBitmapFrame(wxFrame
*parent
)
299 : wxFrame(parent
, wxID_ANY
, _T("Raw bitmaps (how exciting)")),
300 m_bitmap(SIZE
, SIZE
, 32)
302 SetClientSize(SIZE
, SIZE
);
304 // another possibility: wxNativePixelData (don't forget to remove code
305 // setting alpha in the loop below then)
306 typedef wxAlphaPixelData Data
;
307 // typedef wxNativePixelData Data;
309 // First, clear the whole bitmap by making it alpha
311 Data
data( m_bitmap
, wxPoint(0,0), wxSize(SIZE
, SIZE
) );
314 wxLogError(_T("Failed to gain raw access to bitmap data"));
318 Data::Iterator
p(data
);
319 for ( int y
= 0; y
< SIZE
; ++y
)
321 Data::Iterator rowStart
= p
;
322 for ( int x
= 0; x
< SIZE
; ++x
)
325 ++p
; // same as p.OffsetX(1)
332 // Then, draw colourful alpha-blended stripes
333 Data
data(m_bitmap
, wxPoint(BORDER
, BORDER
) , wxSize(REAL_SIZE
, REAL_SIZE
));
336 wxLogError(_T("Failed to gain raw access to bitmap data"));
342 Data::Iterator
p(data
);
344 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
346 Data::Iterator rowStart
= p
;
348 int r
= y
< REAL_SIZE
/3 ? 255 : 0,
349 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ? 255 : 0,
350 b
= 2*(REAL_SIZE
/3) <= y
? 255 : 0;
352 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
354 // note that RGB must be premultiplied by alpha
355 unsigned a
= (Data::Iterator::ChannelType
)((x
*255.)/REAL_SIZE
);
356 p
.Red() = r
* a
/ 256;
357 p
.Green() = g
* a
/ 256;
358 p
.Blue() = b
* a
/ 256;
361 ++p
; // same as p.OffsetX(1)
369 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
371 wxPaintDC
dc( this );
372 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER
);
373 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
374 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
375 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
381 DECLARE_EVENT_TABLE()
384 #endif // wxHAVE_RAW_BITMAP
388 class MyApp
: public wxApp
391 virtual bool OnInit();
400 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
402 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
403 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
404 EVT_PAINT(MyImageFrame::OnPaint
)
405 EVT_LEFT_DCLICK(MyImageFrame::OnSave
)
408 #ifdef wxHAVE_RAW_BITMAP
410 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
411 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
414 #endif // wxHAVE_RAW_BITMAP
416 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
417 EVT_PAINT(MyCanvas::OnPaint
)
420 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
421 const wxPoint
&pos
, const wxSize
&size
)
422 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
423 , m_bmpSmileXpm((const char **) smile_xpm
)
424 , m_iconSmileXpm((const char **) smile_xpm
)
426 my_horse_png
= (wxBitmap
*) NULL
;
427 my_horse_jpeg
= (wxBitmap
*) NULL
;
428 my_horse_gif
= (wxBitmap
*) NULL
;
429 my_horse_bmp
= (wxBitmap
*) NULL
;
430 my_horse_bmp2
= (wxBitmap
*) NULL
;
431 my_horse_pcx
= (wxBitmap
*) NULL
;
432 my_horse_pnm
= (wxBitmap
*) NULL
;
433 my_horse_tiff
= (wxBitmap
*) NULL
;
434 my_horse_xpm
= (wxBitmap
*) NULL
;
435 my_horse_ico32
= (wxBitmap
*) NULL
;
436 my_horse_ico16
= (wxBitmap
*) NULL
;
437 my_horse_ico
= (wxBitmap
*) NULL
;
438 my_horse_cur
= (wxBitmap
*) NULL
;
439 my_horse_ani
= (wxBitmap
*) NULL
;
440 colorized_horse_jpeg
= (wxBitmap
*) NULL
;
442 my_smile_xbm
= (wxBitmap
*) NULL
;
443 my_square
= (wxBitmap
*) NULL
;
444 my_anti
= (wxBitmap
*) NULL
;
446 my_horse_asciigrey_pnm
= (wxBitmap
*) NULL
;
447 my_horse_rawgrey_pnm
= (wxBitmap
*) NULL
;
451 SetBackgroundColour(* wxWHITE
);
453 wxBitmap
bitmap( 100, 100 );
456 dc
.SelectObject( bitmap
);
457 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
458 dc
.SetPen( *wxBLACK_PEN
);
459 dc
.DrawRectangle( 0, 0, 100, 100 );
460 dc
.SetBrush( *wxWHITE_BRUSH
);
461 dc
.DrawRectangle( 20, 20, 60, 60 );
462 dc
.SelectObject( wxNullBitmap
);
464 // try to find the directory with our images
466 if ( wxFile::Exists(wxT("./horse.png")) )
468 else if ( wxFile::Exists(wxT("../horse.png")) )
471 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
473 wxImage image
= bitmap
.ConvertToImage();
476 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
477 wxLogError(wxT("Can't save file"));
481 if ( image
.LoadFile( dir
+ _T("test.png") ) )
482 my_square
= new wxBitmap( image
);
486 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
487 wxLogError(wxT("Can't load PNG image"));
489 my_horse_png
= new wxBitmap( image
);
490 #endif // wxUSE_LIBPNG
495 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
496 wxLogError(wxT("Can't load JPG image"));
499 my_horse_jpeg
= new wxBitmap( image
);
500 // Colorize by rotating green hue to red
501 wxImage::HSVValue greenHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0));
502 wxImage::HSVValue redHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0));
503 image
.RotateHue(redHSV
.hue
- greenHSV
.hue
);
504 colorized_horse_jpeg
= new wxBitmap( image
);
506 #endif // wxUSE_LIBJPEG
511 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
512 wxLogError(wxT("Can't load GIF image"));
514 my_horse_gif
= new wxBitmap( image
);
520 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
521 wxLogError(wxT("Can't load PCX image"));
523 my_horse_pcx
= new wxBitmap( image
);
528 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
529 wxLogError(wxT("Can't load BMP image"));
531 my_horse_bmp
= new wxBitmap( image
);
536 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
537 wxLogError(wxT("Can't load XPM image"));
539 my_horse_xpm
= new wxBitmap( image
);
541 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
542 wxLogError(wxT("Can't save file"));
548 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
549 wxLogError(wxT("Can't load PNM image"));
551 my_horse_pnm
= new wxBitmap( image
);
555 if ( !image
.LoadFile( dir
+ _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM
) )
556 wxLogError(wxT("Can't load PNM image"));
558 my_horse_asciigrey_pnm
= new wxBitmap( image
);
562 if ( !image
.LoadFile( dir
+ _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM
) )
563 wxLogError(wxT("Can't load PNM image"));
565 my_horse_rawgrey_pnm
= new wxBitmap( image
);
571 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
572 wxLogError(wxT("Can't load TIFF image"));
574 my_horse_tiff
= new wxBitmap( image
);
577 CreateAntiAliasedBitmap();
579 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
582 // demonstrates XPM automatically using the mask when saving
583 if ( m_bmpSmileXpm
.Ok() )
584 m_bmpSmileXpm
.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM
);
589 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
590 wxLogError(wxT("Can't load first ICO image"));
592 my_horse_ico32
= new wxBitmap( image
);
596 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
597 wxLogError(wxT("Can't load second ICO image"));
599 my_horse_ico16
= new wxBitmap( image
);
603 if ( !image
.LoadFile( dir
+ _T("horse.ico") ) )
604 wxLogError(wxT("Can't load best ICO image"));
606 my_horse_ico
= new wxBitmap( image
);
610 if ( !image
.LoadFile( dir
+ _T("horse.cur"), wxBITMAP_TYPE_CUR
) )
611 wxLogError(wxT("Can't load best ICO image"));
614 my_horse_cur
= new wxBitmap( image
);
615 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
616 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
619 m_ani_images
= wxImage::GetImageCount ( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
);
621 wxLogError(wxT("No ANI-format images found"));
623 my_horse_ani
= new wxBitmap
[m_ani_images
];
625 for (i
=0; i
< m_ani_images
; i
++)
628 if (!image
.LoadFile( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
, i
))
630 wxString tmp
= wxT("Can't load image number ");
635 my_horse_ani
[i
] = wxBitmap( image
);
637 #endif // wxUSE_ICO_CUR
641 // test image loading from stream
642 wxFile
file(dir
+ _T("horse.bmp"));
643 if ( file
.IsOpened() )
645 wxFileOffset len
= file
.Length();
646 size_t dataSize
= (size_t)len
;
647 void *data
= malloc(dataSize
);
648 if ( file
.Read(data
, dataSize
) != len
)
649 wxLogError(_T("Reading bitmap file failed"));
652 wxMemoryInputStream
mis(data
, dataSize
);
653 if ( !image
.LoadFile(mis
) )
654 wxLogError(wxT("Can't load BMP image from stream"));
656 my_horse_bmp2
= new wxBitmap( image
);
663 MyCanvas::~MyCanvas()
667 delete my_horse_jpeg
;
670 delete my_horse_bmp2
;
672 delete my_horse_tiff
;
674 delete my_horse_ico32
;
675 delete my_horse_ico16
;
678 delete [] my_horse_ani
;
682 delete my_horse_asciigrey_pnm
;
683 delete my_horse_rawgrey_pnm
;
684 delete colorized_horse_jpeg
;
687 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
689 wxPaintDC
dc( this );
692 dc
.DrawText( _T("Loaded image"), 30, 10 );
693 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
695 dc
.DrawText( _T("Drawn directly"), 150, 10 );
696 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
697 dc
.SetPen( *wxBLACK_PEN
);
698 dc
.DrawRectangle( 150, 30, 100, 100 );
699 dc
.SetBrush( *wxWHITE_BRUSH
);
700 dc
.DrawRectangle( 170, 50, 60, 60 );
702 if (my_anti
&& my_anti
->Ok())
703 dc
.DrawBitmap( *my_anti
, 280, 30 );
705 dc
.DrawText( _T("PNG handler"), 30, 135 );
706 if (my_horse_png
&& my_horse_png
->Ok())
708 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
709 wxRect
rect(0,0,100,100);
710 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
711 dc
.DrawText( _T("GetSubBitmap()"), 280, 190 );
712 dc
.DrawBitmap( sub
, 280, 210 );
715 dc
.DrawText( _T("JPEG handler"), 30, 365 );
716 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
717 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
719 dc
.DrawText( _T("GIF handler"), 30, 595 );
720 if (my_horse_gif
&& my_horse_gif
->Ok())
721 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
723 dc
.DrawText( _T("PCX handler"), 30, 825 );
724 if (my_horse_pcx
&& my_horse_pcx
->Ok())
725 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
727 dc
.DrawText( _T("BMP handler"), 30, 1055 );
728 if (my_horse_bmp
&& my_horse_bmp
->Ok())
729 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
731 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
732 if (my_horse_bmp2
&& my_horse_bmp2
->Ok())
733 dc
.DrawBitmap( *my_horse_bmp2
, 280, 1070 );
735 dc
.DrawText( _T("PNM handler"), 30, 1285 );
736 if (my_horse_pnm
&& my_horse_pnm
->Ok())
737 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
739 dc
.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 );
740 if (my_horse_asciigrey_pnm
&& my_horse_asciigrey_pnm
->Ok())
741 dc
.DrawBitmap( *my_horse_asciigrey_pnm
, 280, 1300 );
743 dc
.DrawText( _T("PNM handler (raw grey)"), 530, 1285 );
744 if (my_horse_rawgrey_pnm
&& my_horse_rawgrey_pnm
->Ok())
745 dc
.DrawBitmap( *my_horse_rawgrey_pnm
, 530, 1300 );
747 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
748 if (my_horse_tiff
&& my_horse_tiff
->Ok())
749 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
751 dc
.DrawText( _T("XPM handler"), 30, 1745 );
752 if (my_horse_xpm
&& my_horse_xpm
->Ok())
753 dc
.DrawBitmap( *my_horse_xpm
, 30, 1760 );
756 if (my_smile_xbm
&& my_smile_xbm
->Ok())
758 dc
.DrawText( _T("XBM bitmap"), 30, 1975 );
759 dc
.DrawText( _T("(green on red)"), 30, 1990 );
760 dc
.SetTextForeground( _T("GREEN") );
761 dc
.SetTextBackground( _T("RED") );
762 dc
.DrawBitmap( *my_smile_xbm
, 30, 2010 );
764 dc
.SetTextForeground( wxT("BLACK") );
765 dc
.DrawText( _T("After wxImage conversion"), 150, 1975 );
766 dc
.DrawText( _T("(red on white)"), 150, 1990 );
767 dc
.SetTextForeground( wxT("RED") );
768 wxImage i
= my_smile_xbm
->ConvertToImage();
769 i
.SetMaskColour( 255, 255, 255 );
771 wxRED_PEN
->GetColour().Red(),
772 wxRED_PEN
->GetColour().Green(),
773 wxRED_PEN
->GetColour().Blue() );
774 dc
.DrawBitmap( wxBitmap(i
), 150, 2010, true );
775 dc
.SetTextForeground( wxT("BLACK") );
779 wxBitmap
mono( 60,50,1 );
781 memdc
.SelectObject( mono
);
782 memdc
.SetPen( *wxBLACK_PEN
);
783 memdc
.SetBrush( *wxWHITE_BRUSH
);
784 memdc
.DrawRectangle( 0,0,60,50 );
785 memdc
.SetTextForeground( *wxBLACK
);
787 // I cannot convince GTK2 to draw into mono bitmaps
788 memdc
.DrawText( _T("Hi!"), 5, 5 );
790 memdc
.SetBrush( *wxBLACK_BRUSH
);
791 memdc
.DrawRectangle( 33,5,20,20 );
792 memdc
.SetPen( *wxRED_PEN
);
793 memdc
.DrawLine( 5, 42, 50, 42 );
794 memdc
.SelectObject( wxNullBitmap
);
798 dc
.DrawText( _T("Mono bitmap"), 30, 2095 );
799 dc
.DrawText( _T("(red on green)"), 30, 2110 );
800 dc
.SetTextForeground( wxT("RED") );
801 dc
.SetTextBackground( wxT("GREEN") );
802 dc
.DrawBitmap( mono
, 30, 2130 );
804 dc
.SetTextForeground( wxT("BLACK") );
805 dc
.DrawText( _T("After wxImage conversion"), 150, 2095 );
806 dc
.DrawText( _T("(red on white)"), 150, 2110 );
807 dc
.SetTextForeground( wxT("RED") );
808 wxImage i
= mono
.ConvertToImage();
809 i
.SetMaskColour( 255,255,255 );
811 wxRED_PEN
->GetColour().Red(),
812 wxRED_PEN
->GetColour().Green(),
813 wxRED_PEN
->GetColour().Blue() );
814 dc
.DrawBitmap( wxBitmap(i
), 150, 2130, true );
815 dc
.SetTextForeground( wxT("BLACK") );
818 // For testing transparency
819 dc
.SetBrush( *wxRED_BRUSH
);
820 dc
.DrawRectangle( 20, 2220, 560, 68 );
822 dc
.DrawText(_T("XPM bitmap"), 30, 2230 );
823 if ( m_bmpSmileXpm
.Ok() )
824 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, true);
826 dc
.DrawText(_T("XPM icon"), 110, 2230 );
827 if ( m_iconSmileXpm
.Ok() )
828 dc
.DrawIcon(m_iconSmileXpm
, 110, 2250);
830 // testing icon -> bitmap conversion
831 wxBitmap
to_blit( m_iconSmileXpm
);
834 dc
.DrawText( _T("SubBitmap"), 170, 2230 );
835 wxBitmap sub
= to_blit
.GetSubBitmap( wxRect(0,0,15,15) );
837 dc
.DrawBitmap( sub
, 170, 2250, true );
839 dc
.DrawText( _T("Enlarged"), 250, 2230 );
840 dc
.SetUserScale( 1.5, 1.5 );
841 dc
.DrawBitmap( to_blit
, (int)(250/1.5), (int)(2250/1.5), true );
842 dc
.SetUserScale( 2, 2 );
843 dc
.DrawBitmap( to_blit
, (int)(300/2), (int)(2250/2), true );
844 dc
.SetUserScale( 1.0, 1.0 );
846 dc
.DrawText( _T("Blit"), 400, 2230);
848 blit_dc
.SelectObject( to_blit
);
849 dc
.Blit( 400, 2250, to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
850 dc
.SetUserScale( 1.5, 1.5 );
851 dc
.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
852 dc
.SetUserScale( 2, 2 );
853 dc
.Blit( (int)(500/2), (int)(2250/2), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
854 dc
.SetUserScale( 1.0, 1.0 );
857 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
858 if (my_horse_ico32
&& my_horse_ico32
->Ok())
859 dc
.DrawBitmap( *my_horse_ico32
, 30, 2330, true );
861 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
862 if (my_horse_ico16
&& my_horse_ico16
->Ok())
863 dc
.DrawBitmap( *my_horse_ico16
, 230, 2330, true );
865 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
866 if (my_horse_ico
&& my_horse_ico
->Ok())
867 dc
.DrawBitmap( *my_horse_ico
, 430, 2330, true );
869 dc
.DrawText( _T("CUR handler"), 30, 2390 );
870 if (my_horse_cur
&& my_horse_cur
->Ok())
872 dc
.DrawBitmap( *my_horse_cur
, 30, 2420, true );
873 dc
.SetPen (*wxRED_PEN
);
874 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
875 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
877 dc
.DrawText( _T("ANI handler"), 230, 2390 );
879 for (i
=0; i
< m_ani_images
; i
++)
880 if (my_horse_ani
[i
].Ok())
882 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, true );
885 if (colorized_horse_jpeg
)
887 dc
.DrawText( _T("Colorize image by rotating green hue to red"), 30, 2490 );
888 dc
.DrawBitmap( *colorized_horse_jpeg
, 30, 2520 );
890 #endif // wxUSE_LIBJPEG
894 void MyCanvas::CreateAntiAliasedBitmap()
896 wxBitmap
bitmap( 300, 300 );
900 dc
.SelectObject( bitmap
);
904 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
905 dc
.SetTextForeground( wxT("RED") );
906 dc
.DrawText( _T("This is anti-aliased Text."), 20, 20 );
907 dc
.DrawText( _T("And a Rectangle."), 20, 60 );
909 dc
.SetBrush( *wxRED_BRUSH
);
910 dc
.SetPen( *wxTRANSPARENT_PEN
);
911 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
913 wxImage original
= bitmap
.ConvertToImage();
914 wxImage
anti( 150, 150 );
916 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
918 for (int y
= 1; y
< 149; y
++)
919 for (int x
= 1; x
< 149; x
++)
921 int red
= original
.GetRed( x
*2, y
*2 ) +
922 original
.GetRed( x
*2-1, y
*2 ) +
923 original
.GetRed( x
*2, y
*2+1 ) +
924 original
.GetRed( x
*2+1, y
*2+1 );
927 int green
= original
.GetGreen( x
*2, y
*2 ) +
928 original
.GetGreen( x
*2-1, y
*2 ) +
929 original
.GetGreen( x
*2, y
*2+1 ) +
930 original
.GetGreen( x
*2+1, y
*2+1 );
933 int blue
= original
.GetBlue( x
*2, y
*2 ) +
934 original
.GetBlue( x
*2-1, y
*2 ) +
935 original
.GetBlue( x
*2, y
*2+1 ) +
936 original
.GetBlue( x
*2+1, y
*2+1 );
938 anti
.SetRGB( x
, y
, (unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
940 my_anti
= new wxBitmap(anti
);
948 ID_ABOUT
= wxID_ABOUT
,
953 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
955 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
956 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
957 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
958 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
959 #ifdef wxHAVE_RAW_BITMAP
960 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
964 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
965 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
966 #endif // wxUSE_CLIPBOARD
970 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxImage sample"),
971 wxPoint(20,20), wxSize(470,360) )
973 wxMenuBar
*menu_bar
= new wxMenuBar();
975 wxMenu
*menuImage
= new wxMenu
;
976 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
978 #ifdef wxHAVE_RAW_BITMAP
979 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
981 menuImage
->AppendSeparator();
982 menuImage
->Append( ID_ABOUT
, _T("&About..."));
983 menuImage
->AppendSeparator();
984 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
985 menu_bar
->Append(menuImage
, _T("&Image"));
988 wxMenu
*menuClipboard
= new wxMenu
;
989 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
990 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
991 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
992 #endif // wxUSE_CLIPBOARD
994 SetMenuBar( menu_bar
);
998 int widths
[] = { -1, 100 };
999 SetStatusWidths( 2, widths
);
1000 #endif // wxUSE_STATUSBAR
1002 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
1004 // 500 width * 2750 height
1005 m_canvas
->SetScrollbars( 10, 10, 50, 275 );
1008 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
1013 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
1015 (void)wxMessageBox( _T("wxImage demo\n")
1016 _T("Robert Roebling (c) 1998,2000"),
1017 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
1020 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
1023 wxString filename
= wxFileSelector(_T("Select image file"));
1028 if ( !image
.LoadFile(filename
) )
1030 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
1035 (new MyImageFrame(this, wxBitmap(image
)))->Show();
1036 #endif // wxUSE_FILEDLG
1039 #ifdef wxHAVE_RAW_BITMAP
1041 void MyFrame::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
1043 (new MyRawBitmapFrame(this))->Show();
1046 #endif // wxHAVE_RAW_BITMAP
1050 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1052 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
1053 dobjBmp
->SetBitmap(*m_canvas
->my_horse_png
);
1055 wxTheClipboard
->Open();
1057 if ( !wxTheClipboard
->SetData(dobjBmp
) )
1059 wxLogError(_T("Failed to copy bitmap to clipboard"));
1062 wxTheClipboard
->Close();
1065 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1067 wxBitmapDataObject dobjBmp
;
1069 wxTheClipboard
->Open();
1070 if ( !wxTheClipboard
->GetData(dobjBmp
) )
1072 wxLogMessage(_T("No bitmap data in the clipboard"));
1076 (new MyImageFrame(this, dobjBmp
.GetBitmap()))->Show();
1078 wxTheClipboard
->Close();
1081 #endif // wxUSE_CLIPBOARD
1083 //-----------------------------------------------------------------------------
1085 //-----------------------------------------------------------------------------
1087 bool MyApp::OnInit()
1090 wxImage::AddHandler( new wxPNGHandler
);
1094 wxImage::AddHandler( new wxJPEGHandler
);
1098 wxImage::AddHandler( new wxTIFFHandler
);
1102 wxImage::AddHandler( new wxGIFHandler
);
1106 wxImage::AddHandler( new wxPCXHandler
);
1110 wxImage::AddHandler( new wxPNMHandler
);
1114 wxImage::AddHandler( new wxXPMHandler
);
1118 wxImage::AddHandler( new wxICOHandler
);
1119 wxImage::AddHandler( new wxCURHandler
);
1120 wxImage::AddHandler( new wxANIHandler
);
1123 wxFrame
*frame
= new MyFrame();
1124 frame
->Show( true );