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_head
;
96 wxBitmap
*my_horse_ani
;
99 wxBitmap m_bmpSmileXpm
;
100 wxIcon m_iconSmileXpm
;
102 DECLARE_EVENT_TABLE()
109 class MyFrame
: public wxFrame
114 void OnAbout( wxCommandEvent
&event
);
115 void OnNewFrame( wxCommandEvent
&event
);
116 #ifdef wxHAVE_RAW_BITMAP
117 void OnTestRawBitmap( wxCommandEvent
&event
);
118 #endif // wxHAVE_RAW_BITMAP
119 void OnQuit( wxCommandEvent
&event
);
122 void OnCopy(wxCommandEvent
& event
);
123 void OnPaste(wxCommandEvent
& event
);
124 #endif // wxUSE_CLIPBOARD
129 DECLARE_DYNAMIC_CLASS(MyFrame
)
130 DECLARE_EVENT_TABLE()
133 class MyImageFrame
: public wxFrame
136 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
137 : wxFrame(parent
, wxID_ANY
, _T("Double click to save"),
138 wxDefaultPosition
, wxDefaultSize
,
139 wxCAPTION
| wxSYSTEM_MENU
| wxCLOSE_BOX
),
142 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
145 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
147 // do nothing here to be able to see how transparent images are shown
150 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
152 wxPaintDC
dc( this );
153 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
156 void OnSave(wxMouseEvent
& WXUNUSED(event
))
159 wxImage image
= m_bitmap
.ConvertToImage();
161 wxString savefilename
= wxFileSelector( wxT("Save Image"),
164 (const wxChar
*)NULL
,
165 wxT("BMP files (*.bmp)|*.bmp|")
166 wxT("PNG files (*.png)|*.png|")
167 wxT("JPEG files (*.jpg)|*.jpg|")
168 wxT("GIF files (*.gif)|*.gif|")
169 wxT("TIFF files (*.tif)|*.tif|")
170 wxT("PCX files (*.pcx)|*.pcx|")
171 wxT("ICO files (*.ico)|*.ico|")
172 wxT("CUR files (*.cur)|*.cur"),
176 if ( savefilename
.empty() )
180 wxFileName::SplitPath(savefilename
, NULL
, NULL
, &extension
);
183 if ( extension
== _T("bpp") )
185 static const int bppvalues
[] =
197 const wxString bppchoices
[] =
203 _T("8 bpp greyscale"),
205 _T("8 bpp own palette"),
209 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
210 _T("Image sample: save file"),
211 WXSIZEOF(bppchoices
),
214 if ( bppselection
!= -1 )
216 int format
= bppvalues
[bppselection
];
217 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, format
);
219 if ( format
== wxBMP_8BPP_PALETTE
)
221 unsigned char *cmap
= new unsigned char [256];
222 for ( int i
= 0; i
< 256; i
++ )
223 cmap
[i
] = (unsigned char)i
;
224 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
230 else if ( extension
== _T("png") )
232 static const int pngvalues
[] =
242 const wxString pngchoices
[] =
249 _T("Grey red 16bpp"),
252 int sel
= wxGetSingleChoiceIndex(_T("Set PNG format"),
253 _T("Image sample: save file"),
254 WXSIZEOF(pngchoices
),
259 image
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, pngvalues
[sel
]);
260 image
.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH
, sel
% 2 ? 16 : 8);
263 else if ( extension
== _T("cur") )
265 image
.Rescale(32,32);
266 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
267 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
268 // This shows how you can save an image with explicitly
269 // specified image format:
270 saved
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
275 // This one guesses image format from filename extension
276 // (it may fail if the extension is not recognized):
277 image
.SaveFile(savefilename
);
279 #endif // wxUSE_FILEDLG
285 DECLARE_EVENT_TABLE()
288 #ifdef wxHAVE_RAW_BITMAP
290 #include "wx/rawbmp.h"
292 class MyRawBitmapFrame
: public wxFrame
299 REAL_SIZE
= SIZE
- 2*BORDER
302 MyRawBitmapFrame(wxFrame
*parent
)
303 : wxFrame(parent
, wxID_ANY
, _T("Raw bitmaps (how exciting)")),
304 m_bitmap(SIZE
, SIZE
, 32)
306 SetClientSize(SIZE
, SIZE
);
308 // another possibility: wxNativePixelData (don't forget to remove code
309 // setting alpha in the loop below then)
310 typedef wxAlphaPixelData Data
;
311 // typedef wxNativePixelData Data;
313 // First, clear the whole bitmap by making it alpha
315 Data
data( m_bitmap
, wxPoint(0,0), wxSize(SIZE
, SIZE
) );
318 wxLogError(_T("Failed to gain raw access to bitmap data"));
322 Data::Iterator
p(data
);
323 for ( int y
= 0; y
< SIZE
; ++y
)
325 Data::Iterator rowStart
= p
;
326 for ( int x
= 0; x
< SIZE
; ++x
)
329 ++p
; // same as p.OffsetX(1)
336 // Then, draw colourful alpha-blended stripes
337 Data
data(m_bitmap
, wxPoint(BORDER
, BORDER
) , wxSize(REAL_SIZE
, REAL_SIZE
));
340 wxLogError(_T("Failed to gain raw access to bitmap data"));
346 Data::Iterator
p(data
);
348 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
350 Data::Iterator rowStart
= p
;
352 int r
= y
< REAL_SIZE
/3 ? 255 : 0,
353 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ? 255 : 0,
354 b
= 2*(REAL_SIZE
/3) <= y
? 255 : 0;
356 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
358 // note that RGB must be premultiplied by alpha
359 unsigned a
= (Data::Iterator::ChannelType
)((x
*255.)/REAL_SIZE
);
360 p
.Red() = r
* a
/ 256;
361 p
.Green() = g
* a
/ 256;
362 p
.Blue() = b
* a
/ 256;
365 ++p
; // same as p.OffsetX(1)
373 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
375 wxPaintDC
dc( this );
376 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER
);
377 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
378 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
379 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
385 DECLARE_EVENT_TABLE()
388 #endif // wxHAVE_RAW_BITMAP
392 class MyApp
: public wxApp
395 virtual bool OnInit();
404 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
405 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
406 EVT_PAINT(MyImageFrame::OnPaint
)
407 EVT_LEFT_DCLICK(MyImageFrame::OnSave
)
410 #ifdef wxHAVE_RAW_BITMAP
412 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
413 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
416 #endif // wxHAVE_RAW_BITMAP
418 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
419 EVT_PAINT(MyCanvas::OnPaint
)
422 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
423 const wxPoint
&pos
, const wxSize
&size
)
424 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
425 , m_bmpSmileXpm((const char **) smile_xpm
)
426 , m_iconSmileXpm((const char **) smile_xpm
)
431 SetBackgroundColour(* wxWHITE
);
433 wxBitmap
bitmap( 100, 100 );
436 dc
.SelectObject( bitmap
);
437 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
438 dc
.SetPen( *wxBLACK_PEN
);
439 dc
.DrawRectangle( 0, 0, 100, 100 );
440 dc
.SetBrush( *wxWHITE_BRUSH
);
441 dc
.DrawRectangle( 20, 20, 60, 60 );
442 dc
.SelectObject( wxNullBitmap
);
444 // try to find the directory with our images
446 if ( wxFile::Exists(wxT("./horse.png")) )
448 else if ( wxFile::Exists(wxT("../horse.png")) )
451 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
453 wxImage image
= bitmap
.ConvertToImage();
456 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
457 wxLogError(wxT("Can't save file"));
461 if ( image
.LoadFile( dir
+ _T("test.png") ) )
462 my_square
= wxBitmap( image
);
466 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
467 wxLogError(wxT("Can't load PNG image"));
469 my_horse_png
= wxBitmap( image
);
471 image
= wxImage(wxT("toucan.png"));
472 my_toucan
= wxBitmap(image
);
473 my_toucan_flipped_horiz
= wxBitmap(image
.Mirror(true));
474 my_toucan_flipped_vert
= wxBitmap(image
.Mirror(false));
475 my_toucan_flipped_both
= wxBitmap(image
.Mirror(true).Mirror(false));
476 my_toucan_head
= wxBitmap(image
.GetSubImage(wxRect(40, 7, 80, 60)));
478 #endif // wxUSE_LIBPNG
483 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
484 wxLogError(wxT("Can't load JPG image"));
487 my_horse_jpeg
= wxBitmap( image
);
489 // Colorize by rotating green hue to red
490 wxImage::HSVValue greenHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0));
491 wxImage::HSVValue redHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0));
492 image
.RotateHue(redHSV
.hue
- greenHSV
.hue
);
493 colorized_horse_jpeg
= wxBitmap( image
);
496 if ( !image
.LoadFile( dir
+ _T("cmyk.jpg")) )
497 wxLogError(_T("Can't load CMYK JPG image"));
499 my_cmyk_jpeg
= wxBitmap(image
);
500 #endif // wxUSE_LIBJPEG
505 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
506 wxLogError(wxT("Can't load GIF image"));
508 my_horse_gif
= wxBitmap( image
);
514 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
515 wxLogError(wxT("Can't load PCX image"));
517 my_horse_pcx
= wxBitmap( image
);
522 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
523 wxLogError(wxT("Can't load BMP image"));
525 my_horse_bmp
= wxBitmap( image
);
530 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
531 wxLogError(wxT("Can't load XPM image"));
533 my_horse_xpm
= wxBitmap( image
);
535 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
536 wxLogError(wxT("Can't save file"));
542 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
543 wxLogError(wxT("Can't load PNM image"));
545 my_horse_pnm
= wxBitmap( image
);
549 if ( !image
.LoadFile( dir
+ _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM
) )
550 wxLogError(wxT("Can't load PNM image"));
552 my_horse_asciigrey_pnm
= wxBitmap( image
);
556 if ( !image
.LoadFile( dir
+ _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM
) )
557 wxLogError(wxT("Can't load PNM image"));
559 my_horse_rawgrey_pnm
= wxBitmap( image
);
565 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
566 wxLogError(wxT("Can't load TIFF image"));
568 my_horse_tiff
= wxBitmap( image
);
571 CreateAntiAliasedBitmap();
573 my_smile_xbm
= wxBitmap( (const char*)smile_bits
, smile_width
,
576 // demonstrates XPM automatically using the mask when saving
577 if ( m_bmpSmileXpm
.Ok() )
578 m_bmpSmileXpm
.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM
);
583 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
584 wxLogError(wxT("Can't load first ICO image"));
586 my_horse_ico32
= wxBitmap( image
);
590 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
591 wxLogError(wxT("Can't load second ICO image"));
593 my_horse_ico16
= wxBitmap( image
);
597 if ( !image
.LoadFile( dir
+ _T("horse.ico") ) )
598 wxLogError(wxT("Can't load best ICO image"));
600 my_horse_ico
= wxBitmap( image
);
604 if ( !image
.LoadFile( dir
+ _T("horse.cur"), wxBITMAP_TYPE_CUR
) )
605 wxLogError(wxT("Can't load best ICO image"));
608 my_horse_cur
= wxBitmap( image
);
609 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
610 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
613 m_ani_images
= wxImage::GetImageCount ( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
);
615 wxLogError(wxT("No ANI-format images found"));
617 my_horse_ani
= new wxBitmap
[m_ani_images
];
619 for (i
=0; i
< m_ani_images
; i
++)
622 if (!image
.LoadFile( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
, i
))
624 wxString tmp
= wxT("Can't load image number ");
629 my_horse_ani
[i
] = wxBitmap( image
);
631 #endif // wxUSE_ICO_CUR
635 // test image loading from stream
636 wxFile
file(dir
+ _T("horse.bmp"));
637 if ( file
.IsOpened() )
639 wxFileOffset len
= file
.Length();
640 size_t dataSize
= (size_t)len
;
641 void *data
= malloc(dataSize
);
642 if ( file
.Read(data
, dataSize
) != len
)
643 wxLogError(_T("Reading bitmap file failed"));
646 wxMemoryInputStream
mis(data
, dataSize
);
647 if ( !image
.LoadFile(mis
) )
648 wxLogError(wxT("Can't load BMP image from stream"));
650 my_horse_bmp2
= wxBitmap( image
);
657 MyCanvas::~MyCanvas()
659 delete [] my_horse_ani
;
662 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
664 wxPaintDC
dc( this );
667 dc
.DrawText( _T("Loaded image"), 30, 10 );
669 dc
.DrawBitmap( my_square
, 30, 30 );
671 dc
.DrawText( _T("Drawn directly"), 150, 10 );
672 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
673 dc
.SetPen( *wxBLACK_PEN
);
674 dc
.DrawRectangle( 150, 30, 100, 100 );
675 dc
.SetBrush( *wxWHITE_BRUSH
);
676 dc
.DrawRectangle( 170, 50, 60, 60 );
679 dc
.DrawBitmap( my_anti
, 280, 30 );
681 dc
.DrawText( _T("PNG handler"), 30, 135 );
682 if (my_horse_png
.Ok())
684 dc
.DrawBitmap( my_horse_png
, 30, 150 );
685 wxRect
rect(0,0,100,100);
686 wxBitmap
sub( my_horse_png
.GetSubBitmap(rect
) );
687 dc
.DrawText( _T("GetSubBitmap()"), 280, 175 );
688 dc
.DrawBitmap( sub
, 280, 195 );
691 dc
.DrawText( _T("JPEG handler"), 30, 365 );
692 if (my_horse_jpeg
.Ok())
693 dc
.DrawBitmap( my_horse_jpeg
, 30, 380 );
695 dc
.DrawText( _T("Green rotated to red"), 280, 365 );
696 if (colorized_horse_jpeg
.Ok())
697 dc
.DrawBitmap( colorized_horse_jpeg
, 280, 380 );
699 dc
.DrawText( _T("CMYK JPEG image"), 530, 365 );
700 if (my_cmyk_jpeg
.Ok())
701 dc
.DrawBitmap( my_cmyk_jpeg
, 530, 380 );
703 dc
.DrawText( _T("GIF handler"), 30, 595 );
704 if (my_horse_gif
.Ok())
705 dc
.DrawBitmap( my_horse_gif
, 30, 610 );
707 dc
.DrawText( _T("PCX handler"), 30, 825 );
708 if (my_horse_pcx
.Ok())
709 dc
.DrawBitmap( my_horse_pcx
, 30, 840 );
711 dc
.DrawText( _T("BMP handler"), 30, 1055 );
712 if (my_horse_bmp
.Ok())
713 dc
.DrawBitmap( my_horse_bmp
, 30, 1070 );
715 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
716 if (my_horse_bmp2
.Ok())
717 dc
.DrawBitmap( my_horse_bmp2
, 280, 1070 );
719 dc
.DrawText( _T("PNM handler"), 30, 1285 );
720 if (my_horse_pnm
.Ok())
721 dc
.DrawBitmap( my_horse_pnm
, 30, 1300 );
723 dc
.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 );
724 if (my_horse_asciigrey_pnm
.Ok())
725 dc
.DrawBitmap( my_horse_asciigrey_pnm
, 280, 1300 );
727 dc
.DrawText( _T("PNM handler (raw grey)"), 530, 1285 );
728 if (my_horse_rawgrey_pnm
.Ok())
729 dc
.DrawBitmap( my_horse_rawgrey_pnm
, 530, 1300 );
731 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
732 if (my_horse_tiff
.Ok())
733 dc
.DrawBitmap( my_horse_tiff
, 30, 1530 );
735 dc
.DrawText( _T("XPM handler"), 30, 1745 );
736 if (my_horse_xpm
.Ok())
737 dc
.DrawBitmap( my_horse_xpm
, 30, 1760 );
741 int x
= 750, y
= 10, yy
= 170;
743 dc
.DrawText(wxT("Original toucan"), x
+50, y
);
744 dc
.DrawBitmap(my_toucan
, x
, y
+15);
746 dc
.DrawText(wxT("Flipped horizontally"), x
+50, y
);
747 dc
.DrawBitmap(my_toucan_flipped_horiz
, x
, y
+15);
749 dc
.DrawText(wxT("Flipped vertically"), x
+50, y
);
750 dc
.DrawBitmap(my_toucan_flipped_vert
, x
, y
+15);
752 dc
.DrawText(wxT("Flipped both h&v"), x
+50, y
);
753 dc
.DrawBitmap(my_toucan_flipped_both
, x
, y
+15);
756 dc
.DrawText(wxT("Toucan's head"), x
+50, y
);
757 dc
.DrawBitmap(my_toucan_head
, x
, y
+15);
760 if (my_smile_xbm
.Ok())
762 dc
.DrawText( _T("XBM bitmap"), 30, 1975 );
763 dc
.DrawText( _T("(green on red)"), 30, 1990 );
764 dc
.SetTextForeground( _T("GREEN") );
765 dc
.SetTextBackground( _T("RED") );
766 dc
.DrawBitmap( my_smile_xbm
, 30, 2010 );
768 dc
.SetTextForeground( wxT("BLACK") );
769 dc
.DrawText( _T("After wxImage conversion"), 150, 1975 );
770 dc
.DrawText( _T("(red on white)"), 150, 1990 );
771 dc
.SetTextForeground( wxT("RED") );
772 wxImage i
= my_smile_xbm
.ConvertToImage();
773 i
.SetMaskColour( 255, 255, 255 );
775 wxRED_PEN
->GetColour().Red(),
776 wxRED_PEN
->GetColour().Green(),
777 wxRED_PEN
->GetColour().Blue() );
778 dc
.DrawBitmap( wxBitmap(i
), 150, 2010, true );
779 dc
.SetTextForeground( wxT("BLACK") );
783 wxBitmap
mono( 60,50,1 );
785 memdc
.SelectObject( mono
);
786 memdc
.SetPen( *wxBLACK_PEN
);
787 memdc
.SetBrush( *wxWHITE_BRUSH
);
788 memdc
.DrawRectangle( 0,0,60,50 );
789 memdc
.SetTextForeground( *wxBLACK
);
791 // I cannot convince GTK2 to draw into mono bitmaps
792 memdc
.DrawText( _T("Hi!"), 5, 5 );
794 memdc
.SetBrush( *wxBLACK_BRUSH
);
795 memdc
.DrawRectangle( 33,5,20,20 );
796 memdc
.SetPen( *wxRED_PEN
);
797 memdc
.DrawLine( 5, 42, 50, 42 );
798 memdc
.SelectObject( wxNullBitmap
);
802 dc
.DrawText( _T("Mono bitmap"), 30, 2095 );
803 dc
.DrawText( _T("(red on green)"), 30, 2110 );
804 dc
.SetTextForeground( wxT("RED") );
805 dc
.SetTextBackground( wxT("GREEN") );
806 dc
.DrawBitmap( mono
, 30, 2130 );
808 dc
.SetTextForeground( wxT("BLACK") );
809 dc
.DrawText( _T("After wxImage conversion"), 150, 2095 );
810 dc
.DrawText( _T("(red on white)"), 150, 2110 );
811 dc
.SetTextForeground( wxT("RED") );
812 wxImage i
= mono
.ConvertToImage();
813 i
.SetMaskColour( 255,255,255 );
815 wxRED_PEN
->GetColour().Red(),
816 wxRED_PEN
->GetColour().Green(),
817 wxRED_PEN
->GetColour().Blue() );
818 dc
.DrawBitmap( wxBitmap(i
), 150, 2130, true );
819 dc
.SetTextForeground( wxT("BLACK") );
822 // For testing transparency
823 dc
.SetBrush( *wxRED_BRUSH
);
824 dc
.DrawRectangle( 20, 2220, 560, 68 );
826 dc
.DrawText(_T("XPM bitmap"), 30, 2230 );
827 if ( m_bmpSmileXpm
.Ok() )
828 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, true);
830 dc
.DrawText(_T("XPM icon"), 110, 2230 );
831 if ( m_iconSmileXpm
.Ok() )
832 dc
.DrawIcon(m_iconSmileXpm
, 110, 2250);
834 // testing icon -> bitmap conversion
835 wxBitmap
to_blit( m_iconSmileXpm
);
838 dc
.DrawText( _T("SubBitmap"), 170, 2230 );
839 wxBitmap sub
= to_blit
.GetSubBitmap( wxRect(0,0,15,15) );
841 dc
.DrawBitmap( sub
, 170, 2250, true );
843 dc
.DrawText( _T("Enlarged"), 250, 2230 );
844 dc
.SetUserScale( 1.5, 1.5 );
845 dc
.DrawBitmap( to_blit
, (int)(250/1.5), (int)(2250/1.5), true );
846 dc
.SetUserScale( 2, 2 );
847 dc
.DrawBitmap( to_blit
, (int)(300/2), (int)(2250/2), true );
848 dc
.SetUserScale( 1.0, 1.0 );
850 dc
.DrawText( _T("Blit"), 400, 2230);
852 blit_dc
.SelectObject( to_blit
);
853 dc
.Blit( 400, 2250, to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
854 dc
.SetUserScale( 1.5, 1.5 );
855 dc
.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
856 dc
.SetUserScale( 2, 2 );
857 dc
.Blit( (int)(500/2), (int)(2250/2), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
858 dc
.SetUserScale( 1.0, 1.0 );
861 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
862 if (my_horse_ico32
.Ok())
863 dc
.DrawBitmap( my_horse_ico32
, 30, 2330, true );
865 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
866 if (my_horse_ico16
.Ok())
867 dc
.DrawBitmap( my_horse_ico16
, 230, 2330, true );
869 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
870 if (my_horse_ico
.Ok())
871 dc
.DrawBitmap( my_horse_ico
, 430, 2330, true );
873 dc
.DrawText( _T("CUR handler"), 30, 2390 );
874 if (my_horse_cur
.Ok())
876 dc
.DrawBitmap( my_horse_cur
, 30, 2420, true );
877 dc
.SetPen (*wxRED_PEN
);
878 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
879 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
882 dc
.DrawText( _T("ANI handler"), 230, 2390 );
883 for ( int i
=0; i
< m_ani_images
; i
++ )
885 if (my_horse_ani
[i
].Ok())
887 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, true );
892 void MyCanvas::CreateAntiAliasedBitmap()
894 wxBitmap
bitmap( 300, 300 );
898 dc
.SelectObject( bitmap
);
902 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
903 dc
.SetTextForeground( wxT("RED") );
904 dc
.DrawText( _T("This is anti-aliased Text."), 20, 5 );
905 dc
.DrawText( _T("And a Rectangle."), 20, 45 );
907 dc
.SetBrush( *wxRED_BRUSH
);
908 dc
.SetPen( *wxTRANSPARENT_PEN
);
909 dc
.DrawRoundedRectangle( 20, 85, 200, 180, 20 );
911 wxImage original
= bitmap
.ConvertToImage();
912 wxImage
anti( 150, 150 );
914 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
916 for (int y
= 1; y
< 149; y
++)
917 for (int x
= 1; x
< 149; x
++)
919 int red
= original
.GetRed( x
*2, y
*2 ) +
920 original
.GetRed( x
*2-1, y
*2 ) +
921 original
.GetRed( x
*2, y
*2+1 ) +
922 original
.GetRed( x
*2+1, y
*2+1 );
925 int green
= original
.GetGreen( x
*2, y
*2 ) +
926 original
.GetGreen( x
*2-1, y
*2 ) +
927 original
.GetGreen( x
*2, y
*2+1 ) +
928 original
.GetGreen( x
*2+1, y
*2+1 );
931 int blue
= original
.GetBlue( x
*2, y
*2 ) +
932 original
.GetBlue( x
*2-1, y
*2 ) +
933 original
.GetBlue( x
*2, y
*2+1 ) +
934 original
.GetBlue( x
*2+1, y
*2+1 );
936 anti
.SetRGB( x
, y
, (unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
938 my_anti
= wxBitmap(anti
);
946 ID_ABOUT
= wxID_ABOUT
,
951 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
953 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
954 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
955 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
956 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
957 #ifdef wxHAVE_RAW_BITMAP
958 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
962 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
963 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
964 #endif // wxUSE_CLIPBOARD
968 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxImage sample"),
969 wxPoint(20, 20), wxSize(950, 700) )
971 wxMenuBar
*menu_bar
= new wxMenuBar();
973 wxMenu
*menuImage
= new wxMenu
;
974 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
976 #ifdef wxHAVE_RAW_BITMAP
977 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
979 menuImage
->AppendSeparator();
980 menuImage
->Append( ID_ABOUT
, _T("&About..."));
981 menuImage
->AppendSeparator();
982 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
983 menu_bar
->Append(menuImage
, _T("&Image"));
986 wxMenu
*menuClipboard
= new wxMenu
;
987 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
988 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
989 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
990 #endif // wxUSE_CLIPBOARD
992 SetMenuBar( menu_bar
);
996 int widths
[] = { -1, 100 };
997 SetStatusWidths( 2, widths
);
998 #endif // wxUSE_STATUSBAR
1000 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
1002 // 500 width * 2750 height
1003 m_canvas
->SetScrollbars( 10, 10, 50, 275 );
1006 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
1011 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
1013 (void)wxMessageBox( _T("wxImage demo\n")
1014 _T("Robert Roebling (c) 1998,2000"),
1015 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
1018 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
1021 wxString filename
= wxFileSelector(_T("Select image file"));
1026 if ( !image
.LoadFile(filename
) )
1028 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
1033 (new MyImageFrame(this, wxBitmap(image
)))->Show();
1034 #endif // wxUSE_FILEDLG
1037 #ifdef wxHAVE_RAW_BITMAP
1039 void MyFrame::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
1041 (new MyRawBitmapFrame(this))->Show();
1044 #endif // wxHAVE_RAW_BITMAP
1048 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1050 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
1051 dobjBmp
->SetBitmap(m_canvas
->my_horse_png
);
1053 wxTheClipboard
->Open();
1055 if ( !wxTheClipboard
->SetData(dobjBmp
) )
1057 wxLogError(_T("Failed to copy bitmap to clipboard"));
1060 wxTheClipboard
->Close();
1063 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1065 wxBitmapDataObject dobjBmp
;
1067 wxTheClipboard
->Open();
1068 if ( !wxTheClipboard
->GetData(dobjBmp
) )
1070 wxLogMessage(_T("No bitmap data in the clipboard"));
1074 (new MyImageFrame(this, dobjBmp
.GetBitmap()))->Show();
1076 wxTheClipboard
->Close();
1079 #endif // wxUSE_CLIPBOARD
1081 //-----------------------------------------------------------------------------
1083 //-----------------------------------------------------------------------------
1085 bool MyApp::OnInit()
1088 wxImage::AddHandler( new wxPNGHandler
);
1092 wxImage::AddHandler( new wxJPEGHandler
);
1096 wxImage::AddHandler( new wxTIFFHandler
);
1100 wxImage::AddHandler( new wxGIFHandler
);
1104 wxImage::AddHandler( new wxPCXHandler
);
1108 wxImage::AddHandler( new wxPNMHandler
);
1112 wxImage::AddHandler( new wxXPMHandler
);
1116 wxImage::AddHandler( new wxICOHandler
);
1117 wxImage::AddHandler( new wxCURHandler
);
1118 wxImage::AddHandler( new wxANIHandler
);
1121 wxFrame
*frame
= new MyFrame();
1122 frame
->Show( true );