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
;
91 wxBitmap m_bmpSmileXpm
;
92 wxIcon m_iconSmileXpm
;
95 DECLARE_DYNAMIC_CLASS(MyCanvas
)
103 class MyFrame
: public wxFrame
108 void OnAbout( wxCommandEvent
&event
);
109 void OnNewFrame( wxCommandEvent
&event
);
110 #ifdef wxHAVE_RAW_BITMAP
111 void OnTestRawBitmap( wxCommandEvent
&event
);
112 #endif // wxHAVE_RAW_BITMAP
113 void OnQuit( wxCommandEvent
&event
);
116 void OnCopy(wxCommandEvent
& event
);
117 void OnPaste(wxCommandEvent
& event
);
118 #endif // wxUSE_CLIPBOARD
123 DECLARE_DYNAMIC_CLASS(MyFrame
)
124 DECLARE_EVENT_TABLE()
127 class MyImageFrame
: public wxFrame
130 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
131 : wxFrame(parent
, wxID_ANY
, _T("Double click to save"),
132 wxDefaultPosition
, wxDefaultSize
,
133 wxCAPTION
| wxSYSTEM_MENU
| wxCLOSE_BOX
),
136 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
139 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
141 // do nothing here to be able to see how transparent images are shown
144 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
146 wxPaintDC
dc( this );
147 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
150 void OnSave(wxMouseEvent
& WXUNUSED(event
))
152 wxImage image
= m_bitmap
.ConvertToImage();
154 wxString savefilename
= wxFileSelector( wxT("Save Image"),
157 (const wxChar
*)NULL
,
158 wxT("BMP files (*.bmp)|*.bmp|")
159 wxT("PNG files (*.png)|*.png|")
160 wxT("JPEG files (*.jpg)|*.jpg|")
161 wxT("GIF files (*.gif)|*.gif|")
162 wxT("TIFF files (*.tif)|*.tif|")
163 wxT("PCX files (*.pcx)|*.pcx|")
164 wxT("ICO files (*.ico)|*.ico|")
165 wxT("CUR files (*.cur)|*.cur"),
169 if ( savefilename
.empty() )
173 wxFileName::SplitPath(savefilename
, NULL
, NULL
, &extension
);
176 if ( extension
== _T("bpp") )
178 static const int bppvalues
[] =
190 const wxString bppchoices
[] =
196 _T("8 bpp greyscale"),
198 _T("8 bpp own palette"),
202 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
203 _T("Image sample: save file"),
204 WXSIZEOF(bppchoices
),
207 if ( bppselection
!= -1 )
209 int format
= bppvalues
[bppselection
];
210 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, format
);
212 if ( format
== wxBMP_8BPP_PALETTE
)
214 unsigned char *cmap
= new unsigned char [256];
215 for ( int i
= 0; i
< 256; i
++ )
216 cmap
[i
] = (unsigned char)i
;
217 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
223 else if ( extension
== _T("png") )
225 static const int pngvalues
[] =
235 const wxString pngchoices
[] =
242 _T("Grey red 16bpp"),
245 int sel
= wxGetSingleChoiceIndex(_T("Set PNG format"),
246 _T("Image sample: save file"),
247 WXSIZEOF(pngchoices
),
252 image
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, pngvalues
[sel
]);
253 image
.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH
, sel
% 2 ? 16 : 8);
256 else if ( extension
== _T("cur") )
258 image
.Rescale(32,32);
259 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
260 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
261 // This shows how you can save an image with explicitly
262 // specified image format:
263 saved
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
268 // This one guesses image format from filename extension
269 // (it may fail if the extension is not recognized):
270 image
.SaveFile(savefilename
);
277 DECLARE_EVENT_TABLE()
280 #ifdef wxHAVE_RAW_BITMAP
282 #include "wx/rawbmp.h"
284 class MyRawBitmapFrame
: public wxFrame
291 REAL_SIZE
= SIZE
- 2*BORDER
294 MyRawBitmapFrame(wxFrame
*parent
)
295 : wxFrame(parent
, wxID_ANY
, _T("Raw bitmaps (how exciting)")),
296 m_bitmap(SIZE
, SIZE
, 32)
298 SetClientSize(SIZE
, SIZE
);
300 // another possibility: wxNativePixelData (don't forget to remove code
301 // setting alpha in the loop below then)
302 typedef wxAlphaPixelData Data
;
304 Data
data(m_bitmap
, wxPoint(BORDER
, BORDER
), wxSize(REAL_SIZE
, REAL_SIZE
));
307 wxLogError(_T("Failed to gain raw access to bitmap data"));
313 Data::Iterator
p(data
);
315 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
317 Data::Iterator rowStart
= p
;
319 int r
= y
< REAL_SIZE
/3 ? 255 : 0,
320 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ? 255 : 0,
321 b
= 2*(REAL_SIZE
/3) <= y
? 255 : 0;
323 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
325 // note that RGB must be premultiplied by alpha
326 unsigned a
= (Data::Iterator::ChannelType
)((x
*255.)/REAL_SIZE
);
327 p
.Red() = r
* a
/ 256;
328 p
.Green() = g
* a
/ 256;
329 p
.Blue() = b
* a
/ 256;
332 ++p
; // same as p.OffsetX(1)
340 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
342 wxPaintDC
dc( this );
343 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER
);
344 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
345 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
346 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
352 DECLARE_EVENT_TABLE()
355 #endif // wxHAVE_RAW_BITMAP
359 class MyApp
: public wxApp
362 virtual bool OnInit();
371 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
373 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
374 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
375 EVT_PAINT(MyImageFrame::OnPaint
)
376 EVT_LEFT_DCLICK(MyImageFrame::OnSave
)
379 #ifdef wxHAVE_RAW_BITMAP
381 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
382 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
385 #endif // wxHAVE_RAW_BITMAP
387 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
388 EVT_PAINT(MyCanvas::OnPaint
)
391 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
392 const wxPoint
&pos
, const wxSize
&size
)
393 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
394 , m_bmpSmileXpm((const char **) smile_xpm
)
395 , m_iconSmileXpm((const char **) smile_xpm
)
397 my_horse_png
= (wxBitmap
*) NULL
;
398 my_horse_jpeg
= (wxBitmap
*) NULL
;
399 my_horse_gif
= (wxBitmap
*) NULL
;
400 my_horse_bmp
= (wxBitmap
*) NULL
;
401 my_horse_bmp2
= (wxBitmap
*) NULL
;
402 my_horse_pcx
= (wxBitmap
*) NULL
;
403 my_horse_pnm
= (wxBitmap
*) NULL
;
404 my_horse_tiff
= (wxBitmap
*) NULL
;
405 my_horse_xpm
= (wxBitmap
*) NULL
;
406 my_horse_ico32
= (wxBitmap
*) NULL
;
407 my_horse_ico16
= (wxBitmap
*) NULL
;
408 my_horse_ico
= (wxBitmap
*) NULL
;
409 my_horse_cur
= (wxBitmap
*) NULL
;
410 my_horse_ani
= (wxBitmap
*) NULL
;
412 my_smile_xbm
= (wxBitmap
*) NULL
;
413 my_square
= (wxBitmap
*) NULL
;
414 my_anti
= (wxBitmap
*) NULL
;
416 my_horse_asciigrey_pnm
= (wxBitmap
*) NULL
;
417 my_horse_rawgrey_pnm
= (wxBitmap
*) NULL
;
421 SetBackgroundColour(* wxWHITE
);
423 wxBitmap
bitmap( 100, 100 );
426 dc
.SelectObject( bitmap
);
427 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
428 dc
.SetPen( *wxBLACK_PEN
);
429 dc
.DrawRectangle( 0, 0, 100, 100 );
430 dc
.SetBrush( *wxWHITE_BRUSH
);
431 dc
.DrawRectangle( 20, 20, 60, 60 );
432 dc
.SelectObject( wxNullBitmap
);
434 // try to find the directory with our images
436 if ( wxFile::Exists(wxT("./horse.png")) )
438 else if ( wxFile::Exists(wxT("../horse.png")) )
441 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
443 wxImage image
= bitmap
.ConvertToImage();
446 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
447 wxLogError(wxT("Can't save file"));
451 if ( image
.LoadFile( dir
+ _T("test.png") ) )
452 my_square
= new wxBitmap( image
);
456 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
457 wxLogError(wxT("Can't load PNG image"));
459 my_horse_png
= new wxBitmap( image
);
460 #endif // wxUSE_LIBPNG
465 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
466 wxLogError(wxT("Can't load JPG image"));
468 my_horse_jpeg
= new wxBitmap( image
);
469 #endif // wxUSE_LIBJPEG
474 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
475 wxLogError(wxT("Can't load GIF image"));
477 my_horse_gif
= new wxBitmap( image
);
483 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
484 wxLogError(wxT("Can't load PCX image"));
486 my_horse_pcx
= new wxBitmap( image
);
491 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
492 wxLogError(wxT("Can't load BMP image"));
494 my_horse_bmp
= new wxBitmap( image
);
499 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
500 wxLogError(wxT("Can't load XPM image"));
502 my_horse_xpm
= new wxBitmap( image
);
504 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
505 wxLogError(wxT("Can't save file"));
511 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
512 wxLogError(wxT("Can't load PNM image"));
514 my_horse_pnm
= new wxBitmap( image
);
518 if ( !image
.LoadFile( dir
+ _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM
) )
519 wxLogError(wxT("Can't load PNM image"));
521 my_horse_asciigrey_pnm
= new wxBitmap( image
);
525 if ( !image
.LoadFile( dir
+ _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM
) )
526 wxLogError(wxT("Can't load PNM image"));
528 my_horse_rawgrey_pnm
= new wxBitmap( image
);
534 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
535 wxLogError(wxT("Can't load TIFF image"));
537 my_horse_tiff
= new wxBitmap( image
);
540 CreateAntiAliasedBitmap();
542 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
545 // demonstrates XPM automatically using the mask when saving
546 if ( m_bmpSmileXpm
.Ok() )
547 m_bmpSmileXpm
.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM
);
552 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
553 wxLogError(wxT("Can't load first ICO image"));
555 my_horse_ico32
= new wxBitmap( image
);
559 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
560 wxLogError(wxT("Can't load second ICO image"));
562 my_horse_ico16
= new wxBitmap( image
);
566 if ( !image
.LoadFile( dir
+ _T("horse.ico") ) )
567 wxLogError(wxT("Can't load best ICO image"));
569 my_horse_ico
= new wxBitmap( image
);
573 if ( !image
.LoadFile( dir
+ _T("horse.cur"), wxBITMAP_TYPE_CUR
) )
574 wxLogError(wxT("Can't load best ICO image"));
577 my_horse_cur
= new wxBitmap( image
);
578 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
579 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
582 m_ani_images
= wxImage::GetImageCount ( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
);
584 wxLogError(wxT("No ANI-format images found"));
586 my_horse_ani
= new wxBitmap
[m_ani_images
];
588 for (i
=0; i
< m_ani_images
; i
++)
591 if (!image
.LoadFile( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
, i
))
593 wxString tmp
= wxT("Can't load image number ");
598 my_horse_ani
[i
] = wxBitmap( image
);
600 #endif // wxUSE_ICO_CUR
604 // test image loading from stream
605 wxFile
file(dir
+ _T("horse.bmp"));
606 if ( file
.IsOpened() )
608 wxFileOffset len
= file
.Length();
609 size_t dataSize
= (size_t)len
;
610 void *data
= malloc(dataSize
);
611 if ( file
.Read(data
, dataSize
) != len
)
612 wxLogError(_T("Reading bitmap file failed"));
615 wxMemoryInputStream
mis(data
, dataSize
);
616 if ( !image
.LoadFile(mis
) )
617 wxLogError(wxT("Can't load BMP image from stream"));
619 my_horse_bmp2
= new wxBitmap( image
);
626 MyCanvas::~MyCanvas()
630 delete my_horse_jpeg
;
633 delete my_horse_bmp2
;
635 delete my_horse_tiff
;
637 delete my_horse_ico32
;
638 delete my_horse_ico16
;
641 delete [] my_horse_ani
;
645 delete my_horse_asciigrey_pnm
;
646 delete my_horse_rawgrey_pnm
;
649 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
651 wxPaintDC
dc( this );
654 dc
.DrawText( _T("Loaded image"), 30, 10 );
655 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
657 dc
.DrawText( _T("Drawn directly"), 150, 10 );
658 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
659 dc
.SetPen( *wxBLACK_PEN
);
660 dc
.DrawRectangle( 150, 30, 100, 100 );
661 dc
.SetBrush( *wxWHITE_BRUSH
);
662 dc
.DrawRectangle( 170, 50, 60, 60 );
664 if (my_anti
&& my_anti
->Ok())
665 dc
.DrawBitmap( *my_anti
, 280, 30 );
667 dc
.DrawText( _T("PNG handler"), 30, 135 );
668 if (my_horse_png
&& my_horse_png
->Ok())
670 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
671 wxRect
rect(0,0,100,100);
672 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
673 dc
.DrawText( _T("GetSubBitmap()"), 280, 190 );
674 dc
.DrawBitmap( sub
, 280, 210 );
677 dc
.DrawText( _T("JPEG handler"), 30, 365 );
678 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
679 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
681 dc
.DrawText( _T("GIF handler"), 30, 595 );
682 if (my_horse_gif
&& my_horse_gif
->Ok())
683 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
685 dc
.DrawText( _T("PCX handler"), 30, 825 );
686 if (my_horse_pcx
&& my_horse_pcx
->Ok())
687 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
689 dc
.DrawText( _T("BMP handler"), 30, 1055 );
690 if (my_horse_bmp
&& my_horse_bmp
->Ok())
691 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
693 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
694 if (my_horse_bmp2
&& my_horse_bmp2
->Ok())
695 dc
.DrawBitmap( *my_horse_bmp2
, 280, 1070 );
697 dc
.DrawText( _T("PNM handler"), 30, 1285 );
698 if (my_horse_pnm
&& my_horse_pnm
->Ok())
699 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
701 dc
.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 );
702 if (my_horse_asciigrey_pnm
&& my_horse_asciigrey_pnm
->Ok())
703 dc
.DrawBitmap( *my_horse_asciigrey_pnm
, 280, 1300 );
705 dc
.DrawText( _T("PNM handler (raw grey)"), 530, 1285 );
706 if (my_horse_rawgrey_pnm
&& my_horse_rawgrey_pnm
->Ok())
707 dc
.DrawBitmap( *my_horse_rawgrey_pnm
, 530, 1300 );
709 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
710 if (my_horse_tiff
&& my_horse_tiff
->Ok())
711 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
713 dc
.DrawText( _T("XPM handler"), 30, 1745 );
714 if (my_horse_xpm
&& my_horse_xpm
->Ok())
715 dc
.DrawBitmap( *my_horse_xpm
, 30, 1760 );
718 if (my_smile_xbm
&& my_smile_xbm
->Ok())
720 dc
.DrawText( _T("XBM bitmap"), 30, 1975 );
721 dc
.DrawText( _T("(green on red)"), 30, 1990 );
722 dc
.SetTextForeground( _T("GREEN") );
723 dc
.SetTextBackground( _T("RED") );
724 dc
.DrawBitmap( *my_smile_xbm
, 30, 2010 );
726 dc
.SetTextForeground( wxT("BLACK") );
727 dc
.DrawText( _T("After wxImage conversion"), 150, 1975 );
728 dc
.DrawText( _T("(red on white)"), 150, 1990 );
729 dc
.SetTextForeground( wxT("RED") );
730 wxImage i
= my_smile_xbm
->ConvertToImage();
731 i
.SetMaskColour( 255, 255, 255 );
733 wxRED_PEN
->GetColour().Red(),
734 wxRED_PEN
->GetColour().Green(),
735 wxRED_PEN
->GetColour().Blue() );
736 dc
.DrawBitmap( wxBitmap(i
), 150, 2010, true );
737 dc
.SetTextForeground( wxT("BLACK") );
741 wxBitmap
mono( 60,50,1 );
743 memdc
.SelectObject( mono
);
744 memdc
.SetPen( *wxBLACK_PEN
);
745 memdc
.SetBrush( *wxWHITE_BRUSH
);
746 memdc
.DrawRectangle( 0,0,60,50 );
747 memdc
.SetTextForeground( *wxBLACK
);
749 // I cannot convince GTK2 to draw into mono bitmaps
750 memdc
.DrawText( _T("Hi!"), 5, 5 );
752 memdc
.SetBrush( *wxBLACK_BRUSH
);
753 memdc
.DrawRectangle( 33,5,20,20 );
754 memdc
.SetPen( *wxRED_PEN
);
755 memdc
.DrawLine( 5, 42, 50, 42 );
756 memdc
.SelectObject( wxNullBitmap
);
760 dc
.DrawText( _T("Mono bitmap"), 30, 2095 );
761 dc
.DrawText( _T("(red on green)"), 30, 2110 );
762 dc
.SetTextForeground( wxT("RED") );
763 dc
.SetTextBackground( wxT("GREEN") );
764 dc
.DrawBitmap( mono
, 30, 2130 );
766 dc
.SetTextForeground( wxT("BLACK") );
767 dc
.DrawText( _T("After wxImage conversion"), 150, 2095 );
768 dc
.DrawText( _T("(red on white)"), 150, 2110 );
769 dc
.SetTextForeground( wxT("RED") );
770 wxImage i
= mono
.ConvertToImage();
771 i
.SetMaskColour( 255,255,255 );
773 wxRED_PEN
->GetColour().Red(),
774 wxRED_PEN
->GetColour().Green(),
775 wxRED_PEN
->GetColour().Blue() );
776 dc
.DrawBitmap( wxBitmap(i
), 150, 2130, true );
777 dc
.SetTextForeground( wxT("BLACK") );
780 // For testing transparency
781 dc
.SetBrush( *wxRED_BRUSH
);
782 dc
.DrawRectangle( 20, 2220, 560, 68 );
784 dc
.DrawText(_T("XPM bitmap"), 30, 2230 );
785 if ( m_bmpSmileXpm
.Ok() )
786 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, true);
788 dc
.DrawText(_T("XPM icon"), 110, 2230 );
789 if ( m_iconSmileXpm
.Ok() )
790 dc
.DrawIcon(m_iconSmileXpm
, 110, 2250);
792 // testing icon -> bitmap conversion
793 wxBitmap
to_blit( m_iconSmileXpm
);
796 dc
.DrawText( _T("SubBitmap"), 170, 2230 );
797 wxBitmap sub
= to_blit
.GetSubBitmap( wxRect(0,0,15,15) );
799 dc
.DrawBitmap( sub
, 170, 2250, true );
801 dc
.DrawText( _T("Enlarged"), 250, 2230 );
802 dc
.SetUserScale( 1.5, 1.5 );
803 dc
.DrawBitmap( to_blit
, (int)(250/1.5), (int)(2250/1.5), true );
804 dc
.SetUserScale( 2, 2 );
805 dc
.DrawBitmap( to_blit
, (int)(300/2), (int)(2250/2), true );
806 dc
.SetUserScale( 1.0, 1.0 );
808 dc
.DrawText( _T("Blit"), 400, 2230);
810 blit_dc
.SelectObject( to_blit
);
811 dc
.Blit( 400, 2250, to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
812 dc
.SetUserScale( 1.5, 1.5 );
813 dc
.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
814 dc
.SetUserScale( 2, 2 );
815 dc
.Blit( (int)(500/2), (int)(2250/2), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
816 dc
.SetUserScale( 1.0, 1.0 );
819 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
820 if (my_horse_ico32
&& my_horse_ico32
->Ok())
821 dc
.DrawBitmap( *my_horse_ico32
, 30, 2330, true );
823 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
824 if (my_horse_ico16
&& my_horse_ico16
->Ok())
825 dc
.DrawBitmap( *my_horse_ico16
, 230, 2330, true );
827 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
828 if (my_horse_ico
&& my_horse_ico
->Ok())
829 dc
.DrawBitmap( *my_horse_ico
, 430, 2330, true );
831 dc
.DrawText( _T("CUR handler"), 30, 2390 );
832 if (my_horse_cur
&& my_horse_cur
->Ok())
834 dc
.DrawBitmap( *my_horse_cur
, 30, 2420, true );
835 dc
.SetPen (*wxRED_PEN
);
836 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
837 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
839 dc
.DrawText( _T("ANI handler"), 230, 2390 );
841 for (i
=0; i
< m_ani_images
; i
++)
842 if (my_horse_ani
[i
].Ok())
844 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, true );
848 void MyCanvas::CreateAntiAliasedBitmap()
850 wxBitmap
bitmap( 300, 300 );
854 dc
.SelectObject( bitmap
);
858 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
859 dc
.SetTextForeground( wxT("RED") );
860 dc
.DrawText( _T("This is anti-aliased Text."), 20, 20 );
861 dc
.DrawText( _T("And a Rectangle."), 20, 60 );
863 dc
.SetBrush( *wxRED_BRUSH
);
864 dc
.SetPen( *wxTRANSPARENT_PEN
);
865 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
867 wxImage original
= bitmap
.ConvertToImage();
868 wxImage
anti( 150, 150 );
870 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
872 for (int y
= 1; y
< 149; y
++)
873 for (int x
= 1; x
< 149; x
++)
875 int red
= original
.GetRed( x
*2, y
*2 ) +
876 original
.GetRed( x
*2-1, y
*2 ) +
877 original
.GetRed( x
*2, y
*2+1 ) +
878 original
.GetRed( x
*2+1, y
*2+1 );
881 int green
= original
.GetGreen( x
*2, y
*2 ) +
882 original
.GetGreen( x
*2-1, y
*2 ) +
883 original
.GetGreen( x
*2, y
*2+1 ) +
884 original
.GetGreen( x
*2+1, y
*2+1 );
887 int blue
= original
.GetBlue( x
*2, y
*2 ) +
888 original
.GetBlue( x
*2-1, y
*2 ) +
889 original
.GetBlue( x
*2, y
*2+1 ) +
890 original
.GetBlue( x
*2+1, y
*2+1 );
892 anti
.SetRGB( x
, y
, (unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
894 my_anti
= new wxBitmap(anti
);
907 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
909 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
910 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
911 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
912 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
913 #ifdef wxHAVE_RAW_BITMAP
914 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
918 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
919 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
920 #endif // wxUSE_CLIPBOARD
924 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxImage sample"),
925 wxPoint(20,20), wxSize(470,360) )
927 wxMenuBar
*menu_bar
= new wxMenuBar();
929 wxMenu
*menuImage
= new wxMenu
;
930 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
932 #ifdef wxHAVE_RAW_BITMAP
933 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
935 menuImage
->AppendSeparator();
936 menuImage
->Append( ID_ABOUT
, _T("&About..."));
937 menuImage
->AppendSeparator();
938 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
939 menu_bar
->Append(menuImage
, _T("&Image"));
942 wxMenu
*menuClipboard
= new wxMenu
;
943 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
944 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
945 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
946 #endif // wxUSE_CLIPBOARD
948 SetMenuBar( menu_bar
);
952 int widths
[] = { -1, 100 };
953 SetStatusWidths( 2, widths
);
954 #endif // wxUSE_STATUSBAR
956 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
958 // 500 width * 2500 height
959 m_canvas
->SetScrollbars( 10, 10, 50, 250 );
962 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
967 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
969 (void)wxMessageBox( _T("wxImage demo\n")
970 _T("Robert Roebling (c) 1998,2000"),
971 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
974 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
976 wxString filename
= wxFileSelector(_T("Select image file"));
981 if ( !image
.LoadFile(filename
) )
983 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
988 (new MyImageFrame(this, wxBitmap(image
)))->Show();
991 #ifdef wxHAVE_RAW_BITMAP
993 void MyFrame::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
995 (new MyRawBitmapFrame(this))->Show();
998 #endif // wxHAVE_RAW_BITMAP
1002 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1004 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
1005 dobjBmp
->SetBitmap(*m_canvas
->my_horse_png
);
1007 wxTheClipboard
->Open();
1009 if ( !wxTheClipboard
->SetData(dobjBmp
) )
1011 wxLogError(_T("Failed to copy bitmap to clipboard"));
1014 wxTheClipboard
->Close();
1017 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1019 wxBitmapDataObject dobjBmp
;
1021 wxTheClipboard
->Open();
1022 if ( !wxTheClipboard
->GetData(dobjBmp
) )
1024 wxLogMessage(_T("No bitmap data in the clipboard"));
1028 (new MyImageFrame(this, dobjBmp
.GetBitmap()))->Show();
1030 wxTheClipboard
->Close();
1033 #endif // wxUSE_CLIPBOARD
1035 //-----------------------------------------------------------------------------
1037 //-----------------------------------------------------------------------------
1039 bool MyApp::OnInit()
1042 wxImage::AddHandler( new wxPNGHandler
);
1046 wxImage::AddHandler( new wxJPEGHandler
);
1050 wxImage::AddHandler( new wxTIFFHandler
);
1054 wxImage::AddHandler( new wxGIFHandler
);
1058 wxImage::AddHandler( new wxPCXHandler
);
1062 wxImage::AddHandler( new wxPNMHandler
);
1066 wxImage::AddHandler( new wxXPMHandler
);
1070 wxImage::AddHandler( new wxICOHandler
);
1071 wxImage::AddHandler( new wxCURHandler
);
1072 wxImage::AddHandler( new wxANIHandler
);
1075 wxFrame
*frame
= new MyFrame();
1076 frame
->Show( true );