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
;
88 wxBitmap m_bmpSmileXpm
;
89 wxIcon m_iconSmileXpm
;
92 DECLARE_DYNAMIC_CLASS(MyCanvas
)
100 class MyFrame
: public wxFrame
105 void OnAbout( wxCommandEvent
&event
);
106 void OnNewFrame( wxCommandEvent
&event
);
107 #ifdef wxHAVE_RAW_BITMAP
108 void OnTestRawBitmap( wxCommandEvent
&event
);
109 #endif // wxHAVE_RAW_BITMAP
110 void OnQuit( wxCommandEvent
&event
);
113 void OnCopy(wxCommandEvent
& event
);
114 void OnPaste(wxCommandEvent
& event
);
115 #endif // wxUSE_CLIPBOARD
120 DECLARE_DYNAMIC_CLASS(MyFrame
)
121 DECLARE_EVENT_TABLE()
124 class MyImageFrame
: public wxFrame
127 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
128 : wxFrame(parent
, wxID_ANY
, _T("Double click to save"),
129 wxDefaultPosition
, wxDefaultSize
,
130 wxCAPTION
| wxSYSTEM_MENU
| wxCLOSE_BOX
),
133 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
136 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
138 // do nothing here to be able to see how transparent images are shown
141 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
143 wxPaintDC
dc( this );
144 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
147 void OnSave(wxMouseEvent
& WXUNUSED(event
))
149 wxImage image
= m_bitmap
.ConvertToImage();
151 wxString savefilename
= wxFileSelector( wxT("Save Image"),
154 (const wxChar
*)NULL
,
155 wxT("BMP files (*.bmp)|*.bmp|")
156 wxT("PNG files (*.png)|*.png|")
157 wxT("JPEG files (*.jpg)|*.jpg|")
158 wxT("GIF files (*.gif)|*.gif|")
159 wxT("TIFF files (*.tif)|*.tif|")
160 wxT("PCX files (*.pcx)|*.pcx|")
161 wxT("ICO files (*.ico)|*.ico|")
162 wxT("CUR files (*.cur)|*.cur"),
166 if ( savefilename
.empty() )
170 wxFileName::SplitPath(savefilename
, NULL
, NULL
, &extension
);
173 if ( extension
== _T("bpp") )
175 static const int bppvalues
[] =
187 const wxString bppchoices
[] =
193 _T("8 bpp greyscale"),
195 _T("8 bpp own palette"),
199 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
200 _T("Image sample: save file"),
201 WXSIZEOF(bppchoices
),
204 if ( bppselection
!= -1 )
206 int format
= bppvalues
[bppselection
];
207 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, format
);
209 if ( format
== wxBMP_8BPP_PALETTE
)
211 unsigned char *cmap
= new unsigned char [256];
212 for ( int i
= 0; i
< 256; i
++ )
213 cmap
[i
] = (unsigned char)i
;
214 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
220 else if ( extension
== _T("png") )
222 static const int pngvalues
[] =
232 const wxString pngchoices
[] =
239 _T("Grey red 16bpp"),
242 int sel
= wxGetSingleChoiceIndex(_T("Set PNG format"),
243 _T("Image sample: save file"),
244 WXSIZEOF(pngchoices
),
249 image
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, pngvalues
[sel
]);
250 image
.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH
, sel
% 2 ? 16 : 8);
253 else if ( extension
== _T("cur") )
255 image
.Rescale(32,32);
256 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
257 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
258 // This shows how you can save an image with explicitly
259 // specified image format:
260 saved
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
265 // This one guesses image format from filename extension
266 // (it may fail if the extension is not recognized):
267 image
.SaveFile(savefilename
);
274 DECLARE_EVENT_TABLE()
277 #ifdef wxHAVE_RAW_BITMAP
279 #include "wx/rawbmp.h"
281 class MyRawBitmapFrame
: public wxFrame
288 REAL_SIZE
= SIZE
- 2*BORDER
291 MyRawBitmapFrame(wxFrame
*parent
)
292 : wxFrame(parent
, wxID_ANY
, _T("Raw bitmaps (how exciting)")),
293 m_bitmap(SIZE
, SIZE
, 32)
295 SetClientSize(SIZE
, SIZE
);
297 // another possibility: wxNativePixelData (don't forget to remove code
298 // setting alpha in the loop below then)
299 typedef wxAlphaPixelData Data
;
301 Data
data(m_bitmap
, wxPoint(BORDER
, BORDER
), wxSize(REAL_SIZE
, REAL_SIZE
));
304 wxLogError(_T("Failed to gain raw access to bitmap data"));
310 Data::Iterator
p(data
);
312 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
314 Data::Iterator rowStart
= p
;
316 int r
= y
< REAL_SIZE
/3 ? 255 : 0,
317 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ? 255 : 0,
318 b
= 2*(REAL_SIZE
/3) <= y
? 255 : 0;
320 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
325 p
.Alpha() = (Data::Iterator::ChannelType
)((x
*255.)/REAL_SIZE
);
327 ++p
; // same as p.OffsetX(1)
335 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
337 wxPaintDC
dc( this );
338 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER
);
339 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
340 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
341 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
347 DECLARE_EVENT_TABLE()
350 #endif // wxHAVE_RAW_BITMAP
354 class MyApp
: public wxApp
357 virtual bool OnInit();
366 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
368 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
369 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
370 EVT_PAINT(MyImageFrame::OnPaint
)
371 EVT_LEFT_DCLICK(MyImageFrame::OnSave
)
374 #ifdef wxHAVE_RAW_BITMAP
376 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
377 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
380 #endif // wxHAVE_RAW_BITMAP
382 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
383 EVT_PAINT(MyCanvas::OnPaint
)
386 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
387 const wxPoint
&pos
, const wxSize
&size
)
388 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
389 , m_bmpSmileXpm((const char **) smile_xpm
)
390 , m_iconSmileXpm((const char **) smile_xpm
)
392 my_horse_png
= (wxBitmap
*) NULL
;
393 my_horse_jpeg
= (wxBitmap
*) NULL
;
394 my_horse_gif
= (wxBitmap
*) NULL
;
395 my_horse_bmp
= (wxBitmap
*) NULL
;
396 my_horse_bmp2
= (wxBitmap
*) NULL
;
397 my_horse_pcx
= (wxBitmap
*) NULL
;
398 my_horse_pnm
= (wxBitmap
*) NULL
;
399 my_horse_tiff
= (wxBitmap
*) NULL
;
400 my_horse_xpm
= (wxBitmap
*) NULL
;
401 my_horse_ico32
= (wxBitmap
*) NULL
;
402 my_horse_ico16
= (wxBitmap
*) NULL
;
403 my_horse_ico
= (wxBitmap
*) NULL
;
404 my_horse_cur
= (wxBitmap
*) NULL
;
405 my_horse_ani
= (wxBitmap
*) NULL
;
407 my_smile_xbm
= (wxBitmap
*) NULL
;
408 my_square
= (wxBitmap
*) NULL
;
409 my_anti
= (wxBitmap
*) NULL
;
413 SetBackgroundColour(* wxWHITE
);
415 wxBitmap
bitmap( 100, 100 );
418 dc
.SelectObject( bitmap
);
419 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
420 dc
.SetPen( *wxBLACK_PEN
);
421 dc
.DrawRectangle( 0, 0, 100, 100 );
422 dc
.SetBrush( *wxWHITE_BRUSH
);
423 dc
.DrawRectangle( 20, 20, 60, 60 );
424 dc
.SelectObject( wxNullBitmap
);
426 // try to find the directory with our images
428 if ( wxFile::Exists(wxT("./horse.png")) )
430 else if ( wxFile::Exists(wxT("../horse.png")) )
433 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
435 wxImage image
= bitmap
.ConvertToImage();
438 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
439 wxLogError(wxT("Can't save file"));
443 if ( image
.LoadFile( dir
+ _T("test.png") ) )
444 my_square
= new wxBitmap( image
);
448 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
449 wxLogError(wxT("Can't load PNG image"));
451 my_horse_png
= new wxBitmap( image
);
452 #endif // wxUSE_LIBPNG
457 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
458 wxLogError(wxT("Can't load JPG image"));
460 my_horse_jpeg
= new wxBitmap( image
);
461 #endif // wxUSE_LIBJPEG
466 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
467 wxLogError(wxT("Can't load GIF image"));
469 my_horse_gif
= new wxBitmap( image
);
475 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
476 wxLogError(wxT("Can't load PCX image"));
478 my_horse_pcx
= new wxBitmap( image
);
483 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
484 wxLogError(wxT("Can't load BMP image"));
486 my_horse_bmp
= new wxBitmap( image
);
491 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
492 wxLogError(wxT("Can't load XPM image"));
494 my_horse_xpm
= new wxBitmap( image
);
496 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
497 wxLogError(wxT("Can't save file"));
503 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
504 wxLogError(wxT("Can't load PNM image"));
506 my_horse_pnm
= new wxBitmap( image
);
512 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
513 wxLogError(wxT("Can't load TIFF image"));
515 my_horse_tiff
= new wxBitmap( image
);
518 CreateAntiAliasedBitmap();
520 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
523 // demonstrates XPM automatically using the mask when saving
524 if ( m_bmpSmileXpm
.Ok() )
525 m_bmpSmileXpm
.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM
);
530 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
531 wxLogError(wxT("Can't load first ICO image"));
533 my_horse_ico32
= new wxBitmap( image
);
537 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
538 wxLogError(wxT("Can't load second ICO image"));
540 my_horse_ico16
= new wxBitmap( image
);
544 if ( !image
.LoadFile( dir
+ _T("horse.ico") ) )
545 wxLogError(wxT("Can't load best ICO image"));
547 my_horse_ico
= new wxBitmap( image
);
551 if ( !image
.LoadFile( dir
+ _T("horse.cur"), wxBITMAP_TYPE_CUR
) )
552 wxLogError(wxT("Can't load best ICO image"));
555 my_horse_cur
= new wxBitmap( image
);
556 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
557 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
560 m_ani_images
= wxImage::GetImageCount ( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
);
562 wxLogError(wxT("No ANI-format images found"));
564 my_horse_ani
= new wxBitmap
[m_ani_images
];
566 for (i
=0; i
< m_ani_images
; i
++)
569 if (!image
.LoadFile( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
, i
))
571 wxString tmp
= wxT("Can't load image number ");
576 my_horse_ani
[i
] = wxBitmap( image
);
578 #endif // wxUSE_ICO_CUR
582 // test image loading from stream
583 wxFile
file(dir
+ _T("horse.bmp"));
584 if ( file
.IsOpened() )
586 wxFileOffset len
= file
.Length();
587 size_t dataSize
= (size_t)len
;
588 void *data
= malloc(dataSize
);
589 if ( file
.Read(data
, dataSize
) != len
)
590 wxLogError(_T("Reading bitmap file failed"));
593 wxMemoryInputStream
mis(data
, dataSize
);
594 if ( !image
.LoadFile(mis
) )
595 wxLogError(wxT("Can't load BMP image from stream"));
597 my_horse_bmp2
= new wxBitmap( image
);
604 MyCanvas::~MyCanvas()
608 delete my_horse_jpeg
;
611 delete my_horse_bmp2
;
613 delete my_horse_tiff
;
615 delete my_horse_ico32
;
616 delete my_horse_ico16
;
619 delete [] my_horse_ani
;
625 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
627 wxPaintDC
dc( this );
630 dc
.DrawText( _T("Loaded image"), 30, 10 );
631 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
633 dc
.DrawText( _T("Drawn directly"), 150, 10 );
634 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
635 dc
.SetPen( *wxBLACK_PEN
);
636 dc
.DrawRectangle( 150, 30, 100, 100 );
637 dc
.SetBrush( *wxWHITE_BRUSH
);
638 dc
.DrawRectangle( 170, 50, 60, 60 );
640 if (my_anti
&& my_anti
->Ok())
641 dc
.DrawBitmap( *my_anti
, 280, 30 );
643 dc
.DrawText( _T("PNG handler"), 30, 135 );
644 if (my_horse_png
&& my_horse_png
->Ok())
646 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
647 wxRect
rect(0,0,100,100);
648 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
649 dc
.DrawText( _T("GetSubBitmap()"), 280, 190 );
650 dc
.DrawBitmap( sub
, 280, 210 );
653 dc
.DrawText( _T("JPEG handler"), 30, 365 );
654 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
655 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
657 dc
.DrawText( _T("GIF handler"), 30, 595 );
658 if (my_horse_gif
&& my_horse_gif
->Ok())
659 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
661 dc
.DrawText( _T("PCX handler"), 30, 825 );
662 if (my_horse_pcx
&& my_horse_pcx
->Ok())
663 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
665 dc
.DrawText( _T("BMP handler"), 30, 1055 );
666 if (my_horse_bmp
&& my_horse_bmp
->Ok())
667 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
669 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
670 if (my_horse_bmp2
&& my_horse_bmp2
->Ok())
671 dc
.DrawBitmap( *my_horse_bmp2
, 280, 1070 );
673 dc
.DrawText( _T("PNM handler"), 30, 1285 );
674 if (my_horse_pnm
&& my_horse_pnm
->Ok())
675 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
677 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
678 if (my_horse_tiff
&& my_horse_tiff
->Ok())
679 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
681 dc
.DrawText( _T("XPM handler"), 30, 1745 );
682 if (my_horse_xpm
&& my_horse_xpm
->Ok())
683 dc
.DrawBitmap( *my_horse_xpm
, 30, 1760 );
686 if (my_smile_xbm
&& my_smile_xbm
->Ok())
688 dc
.DrawText( _T("XBM bitmap"), 30, 1975 );
689 dc
.DrawText( _T("(green on red)"), 30, 1990 );
690 dc
.SetTextForeground( _T("GREEN") );
691 dc
.SetTextBackground( _T("RED") );
692 dc
.DrawBitmap( *my_smile_xbm
, 30, 2010 );
694 dc
.SetTextForeground( wxT("BLACK") );
695 dc
.DrawText( _T("After wxImage conversion"), 150, 1975 );
696 dc
.DrawText( _T("(red on white)"), 150, 1990 );
697 dc
.SetTextForeground( wxT("RED") );
698 wxImage i
= my_smile_xbm
->ConvertToImage();
699 i
.SetMaskColour( 255, 255, 255 );
701 wxRED_PEN
->GetColour().Red(),
702 wxRED_PEN
->GetColour().Green(),
703 wxRED_PEN
->GetColour().Blue() );
704 dc
.DrawBitmap( wxBitmap(i
), 150, 2010, true );
705 dc
.SetTextForeground( wxT("BLACK") );
709 wxBitmap
mono( 60,50,1 );
711 memdc
.SelectObject( mono
);
712 memdc
.SetPen( *wxBLACK_PEN
);
713 memdc
.SetBrush( *wxWHITE_BRUSH
);
714 memdc
.DrawRectangle( 0,0,60,50 );
715 memdc
.SetTextForeground( *wxBLACK
);
717 // I cannot convince GTK2 to draw into mono bitmaps
718 memdc
.DrawText( _T("Hi!"), 5, 5 );
720 memdc
.SetBrush( *wxBLACK_BRUSH
);
721 memdc
.DrawRectangle( 33,5,20,20 );
722 memdc
.SetPen( *wxRED_PEN
);
723 memdc
.DrawLine( 5, 42, 50, 42 );
724 memdc
.SelectObject( wxNullBitmap
);
728 dc
.DrawText( _T("Mono bitmap"), 30, 2095 );
729 dc
.DrawText( _T("(red on green)"), 30, 2110 );
730 dc
.SetTextForeground( wxT("RED") );
731 dc
.SetTextBackground( wxT("GREEN") );
732 dc
.DrawBitmap( mono
, 30, 2130 );
734 dc
.SetTextForeground( wxT("BLACK") );
735 dc
.DrawText( _T("After wxImage conversion"), 150, 2095 );
736 dc
.DrawText( _T("(red on white)"), 150, 2110 );
737 dc
.SetTextForeground( wxT("RED") );
738 wxImage i
= mono
.ConvertToImage();
739 i
.SetMaskColour( 255,255,255 );
741 wxRED_PEN
->GetColour().Red(),
742 wxRED_PEN
->GetColour().Green(),
743 wxRED_PEN
->GetColour().Blue() );
744 dc
.DrawBitmap( wxBitmap(i
), 150, 2130, true );
745 dc
.SetTextForeground( wxT("BLACK") );
748 // For testing transparency
749 dc
.SetBrush( *wxRED_BRUSH
);
750 dc
.DrawRectangle( 20, 2220, 560, 68 );
752 dc
.DrawText(_T("XPM bitmap"), 30, 2230 );
753 if ( m_bmpSmileXpm
.Ok() )
754 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, true);
756 dc
.DrawText(_T("XPM icon"), 110, 2230 );
757 if ( m_iconSmileXpm
.Ok() )
758 dc
.DrawIcon(m_iconSmileXpm
, 110, 2250);
760 // testing icon -> bitmap conversion
761 wxBitmap
to_blit( m_iconSmileXpm
);
764 dc
.DrawText( _T("SubBitmap"), 170, 2230 );
765 wxBitmap sub
= to_blit
.GetSubBitmap( wxRect(0,0,15,15) );
767 dc
.DrawBitmap( sub
, 170, 2250, true );
769 dc
.DrawText( _T("Enlarged"), 250, 2230 );
770 dc
.SetUserScale( 1.5, 1.5 );
771 dc
.DrawBitmap( to_blit
, (int)(250/1.5), (int)(2250/1.5), true );
772 dc
.SetUserScale( 2, 2 );
773 dc
.DrawBitmap( to_blit
, (int)(300/2), (int)(2250/2), true );
774 dc
.SetUserScale( 1.0, 1.0 );
776 dc
.DrawText( _T("Blit"), 400, 2230);
778 blit_dc
.SelectObject( to_blit
);
779 dc
.Blit( 400, 2250, to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
780 dc
.SetUserScale( 1.5, 1.5 );
781 dc
.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
782 dc
.SetUserScale( 2, 2 );
783 dc
.Blit( (int)(500/2), (int)(2250/2), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
784 dc
.SetUserScale( 1.0, 1.0 );
787 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
788 if (my_horse_ico32
&& my_horse_ico32
->Ok())
789 dc
.DrawBitmap( *my_horse_ico32
, 30, 2330, true );
791 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
792 if (my_horse_ico16
&& my_horse_ico16
->Ok())
793 dc
.DrawBitmap( *my_horse_ico16
, 230, 2330, true );
795 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
796 if (my_horse_ico
&& my_horse_ico
->Ok())
797 dc
.DrawBitmap( *my_horse_ico
, 430, 2330, true );
799 dc
.DrawText( _T("CUR handler"), 30, 2390 );
800 if (my_horse_cur
&& my_horse_cur
->Ok())
802 dc
.DrawBitmap( *my_horse_cur
, 30, 2420, true );
803 dc
.SetPen (*wxRED_PEN
);
804 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
805 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
807 dc
.DrawText( _T("ANI handler"), 230, 2390 );
809 for (i
=0; i
< m_ani_images
; i
++)
810 if (my_horse_ani
[i
].Ok())
812 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, true );
816 void MyCanvas::CreateAntiAliasedBitmap()
818 wxBitmap
bitmap( 300, 300 );
822 dc
.SelectObject( bitmap
);
826 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
827 dc
.SetTextForeground( wxT("RED") );
828 dc
.DrawText( _T("This is anti-aliased Text."), 20, 20 );
829 dc
.DrawText( _T("And a Rectangle."), 20, 60 );
831 dc
.SetBrush( *wxRED_BRUSH
);
832 dc
.SetPen( *wxTRANSPARENT_PEN
);
833 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
835 wxImage original
= bitmap
.ConvertToImage();
836 wxImage
anti( 150, 150 );
838 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
840 for (int y
= 1; y
< 149; y
++)
841 for (int x
= 1; x
< 149; x
++)
843 int red
= original
.GetRed( x
*2, y
*2 ) +
844 original
.GetRed( x
*2-1, y
*2 ) +
845 original
.GetRed( x
*2, y
*2+1 ) +
846 original
.GetRed( x
*2+1, y
*2+1 );
849 int green
= original
.GetGreen( x
*2, y
*2 ) +
850 original
.GetGreen( x
*2-1, y
*2 ) +
851 original
.GetGreen( x
*2, y
*2+1 ) +
852 original
.GetGreen( x
*2+1, y
*2+1 );
855 int blue
= original
.GetBlue( x
*2, y
*2 ) +
856 original
.GetBlue( x
*2-1, y
*2 ) +
857 original
.GetBlue( x
*2, y
*2+1 ) +
858 original
.GetBlue( x
*2+1, y
*2+1 );
860 anti
.SetRGB( x
, y
, (unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
862 my_anti
= new wxBitmap(anti
);
875 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
877 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
878 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
879 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
880 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
881 #ifdef wxHAVE_RAW_BITMAP
882 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
886 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
887 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
888 #endif // wxUSE_CLIPBOARD
892 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxImage sample"),
893 wxPoint(20,20), wxSize(470,360) )
895 wxMenuBar
*menu_bar
= new wxMenuBar();
897 wxMenu
*menuImage
= new wxMenu
;
898 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
900 #ifdef wxHAVE_RAW_BITMAP
901 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
903 menuImage
->AppendSeparator();
904 menuImage
->Append( ID_ABOUT
, _T("&About..."));
905 menuImage
->AppendSeparator();
906 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
907 menu_bar
->Append(menuImage
, _T("&Image"));
910 wxMenu
*menuClipboard
= new wxMenu
;
911 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
912 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
913 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
914 #endif // wxUSE_CLIPBOARD
916 SetMenuBar( menu_bar
);
920 int widths
[] = { -1, 100 };
921 SetStatusWidths( 2, widths
);
922 #endif // wxUSE_STATUSBAR
924 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
926 // 500 width * 2500 height
927 m_canvas
->SetScrollbars( 10, 10, 50, 250 );
930 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
935 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
937 (void)wxMessageBox( _T("wxImage demo\n")
938 _T("Robert Roebling (c) 1998,2000"),
939 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
942 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
944 wxString filename
= wxFileSelector(_T("Select image file"));
949 if ( !image
.LoadFile(filename
) )
951 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
956 (new MyImageFrame(this, wxBitmap(image
)))->Show();
959 #ifdef wxHAVE_RAW_BITMAP
961 void MyFrame::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
963 (new MyRawBitmapFrame(this))->Show();
966 #endif // wxHAVE_RAW_BITMAP
970 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
972 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
973 dobjBmp
->SetBitmap(*m_canvas
->my_horse_png
);
975 wxTheClipboard
->Open();
977 if ( !wxTheClipboard
->SetData(dobjBmp
) )
979 wxLogError(_T("Failed to copy bitmap to clipboard"));
982 wxTheClipboard
->Close();
985 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
987 wxBitmapDataObject dobjBmp
;
989 wxTheClipboard
->Open();
990 if ( !wxTheClipboard
->GetData(dobjBmp
) )
992 wxLogMessage(_T("No bitmap data in the clipboard"));
996 (new MyImageFrame(this, dobjBmp
.GetBitmap()))->Show();
998 wxTheClipboard
->Close();
1001 #endif // wxUSE_CLIPBOARD
1003 //-----------------------------------------------------------------------------
1005 //-----------------------------------------------------------------------------
1007 bool MyApp::OnInit()
1010 wxImage::AddHandler( new wxPNGHandler
);
1014 wxImage::AddHandler( new wxJPEGHandler
);
1018 wxImage::AddHandler( new wxTIFFHandler
);
1022 wxImage::AddHandler( new wxGIFHandler
);
1026 wxImage::AddHandler( new wxPCXHandler
);
1030 wxImage::AddHandler( new wxPNMHandler
);
1034 wxImage::AddHandler( new wxXPMHandler
);
1038 wxImage::AddHandler( new wxICOHandler
);
1039 wxImage::AddHandler( new wxCURHandler
);
1040 wxImage::AddHandler( new wxANIHandler
);
1043 wxFrame
*frame
= new MyFrame();
1044 frame
->Show( true );