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_tga
;
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
;
79 wxBitmap my_smile_xbm
;
83 wxBitmap my_horse_asciigrey_pnm
;
84 wxBitmap my_horse_rawgrey_pnm
;
86 wxBitmap colorized_horse_jpeg
;
87 wxBitmap my_cmyk_jpeg
;
90 wxBitmap my_toucan_flipped_horiz
;
91 wxBitmap my_toucan_flipped_vert
;
92 wxBitmap my_toucan_flipped_both
;
93 wxBitmap my_toucan_grey
;
94 wxBitmap my_toucan_head
;
95 wxBitmap my_toucan_scaled_normal
;
96 wxBitmap my_toucan_scaled_high
;
97 wxBitmap my_toucan_blur
;
101 wxBitmap
*my_horse_ani
;
104 wxBitmap m_bmpSmileXpm
;
105 wxIcon m_iconSmileXpm
;
107 DECLARE_EVENT_TABLE()
114 class MyFrame
: public wxFrame
119 void OnAbout( wxCommandEvent
&event
);
120 void OnNewFrame( wxCommandEvent
&event
);
121 void OnImageInfo( wxCommandEvent
&event
);
122 #ifdef wxHAVE_RAW_BITMAP
123 void OnTestRawBitmap( wxCommandEvent
&event
);
124 #endif // wxHAVE_RAW_BITMAP
125 void OnQuit( wxCommandEvent
&event
);
128 void OnCopy(wxCommandEvent
& event
);
129 void OnPaste(wxCommandEvent
& event
);
130 #endif // wxUSE_CLIPBOARD
135 // ask user for the file name and try to load an image from it
137 // return the file path on success, empty string if we failed to load the
138 // image or were cancelled by user
139 static wxString
LoadUserImage(wxImage
& image
);
142 DECLARE_DYNAMIC_CLASS(MyFrame
)
143 DECLARE_EVENT_TABLE()
146 // ----------------------------------------------------------------------------
147 // Frame used for showing a standalone image
148 // ----------------------------------------------------------------------------
152 ID_ROTATE_LEFT
= 100,
157 class MyImageFrame
: public wxFrame
160 MyImageFrame(wxFrame
*parent
, const wxString
& desc
, const wxBitmap
& bitmap
)
161 : wxFrame(parent
, wxID_ANY
,
162 wxString::Format(_T("Image from %s"), desc
.c_str()),
163 wxDefaultPosition
, wxDefaultSize
,
164 wxDEFAULT_FRAME_STYLE
| wxFULL_REPAINT_ON_RESIZE
),
167 wxMenu
*menu
= new wxMenu
;
168 menu
->Append(wxID_SAVE
);
169 menu
->AppendSeparator();
170 menu
->Append(ID_RESIZE
, _T("&Fit to window\tCtrl-F"));
171 menu
->AppendSeparator();
172 menu
->Append(ID_ROTATE_LEFT
, _T("Rotate &left\tCtrl-L"));
173 menu
->Append(ID_ROTATE_RIGHT
, _T("Rotate &right\tCtrl-R"));
175 wxMenuBar
*mbar
= new wxMenuBar
;
176 mbar
->Append(menu
, _T("&Image"));
181 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
186 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
188 // do nothing here to be able to see how transparent images are shown
191 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
194 const wxSize size
= GetClientSize();
195 dc
.DrawBitmap(m_bitmap
,
196 (size
.x
- m_bitmap
.GetWidth())/2,
197 (size
.y
- m_bitmap
.GetHeight())/2,
198 true /* use mask */);
201 void OnSave(wxCommandEvent
& WXUNUSED(event
))
204 wxImage image
= m_bitmap
.ConvertToImage();
206 wxString savefilename
= wxFileSelector( wxT("Save Image"),
209 (const wxChar
*)NULL
,
210 wxT("BMP files (*.bmp)|*.bmp|")
211 wxT("PNG files (*.png)|*.png|")
212 wxT("JPEG files (*.jpg)|*.jpg|")
213 wxT("GIF files (*.gif)|*.gif|")
214 wxT("TIFF files (*.tif)|*.tif|")
215 wxT("PCX files (*.pcx)|*.pcx|")
216 wxT("ICO files (*.ico)|*.ico|")
217 wxT("CUR files (*.cur)|*.cur"),
221 if ( savefilename
.empty() )
225 wxFileName::SplitPath(savefilename
, NULL
, NULL
, &extension
);
228 if ( extension
== _T("bmp") )
230 static const int bppvalues
[] =
242 const wxString bppchoices
[] =
248 _T("8 bpp greyscale"),
250 _T("8 bpp own palette"),
254 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
255 _T("Image sample: save file"),
256 WXSIZEOF(bppchoices
),
259 if ( bppselection
!= -1 )
261 int format
= bppvalues
[bppselection
];
262 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, format
);
264 if ( format
== wxBMP_8BPP_PALETTE
)
266 unsigned char *cmap
= new unsigned char [256];
267 for ( int i
= 0; i
< 256; i
++ )
268 cmap
[i
] = (unsigned char)i
;
269 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
275 else if ( extension
== _T("png") )
277 static const int pngvalues
[] =
287 const wxString pngchoices
[] =
294 _T("Grey red 16bpp"),
297 int sel
= wxGetSingleChoiceIndex(_T("Set PNG format"),
298 _T("Image sample: save file"),
299 WXSIZEOF(pngchoices
),
304 image
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, pngvalues
[sel
]);
305 image
.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH
, sel
% 2 ? 16 : 8);
308 else if ( extension
== _T("cur") )
310 image
.Rescale(32,32);
311 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
312 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
313 // This shows how you can save an image with explicitly
314 // specified image format:
315 saved
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
320 // This one guesses image format from filename extension
321 // (it may fail if the extension is not recognized):
322 image
.SaveFile(savefilename
);
324 #endif // wxUSE_FILEDLG
327 void OnResize(wxCommandEvent
& WXUNUSED(event
))
329 wxImage
img(m_bitmap
.ConvertToImage());
331 const wxSize size
= GetClientSize();
332 img
.Rescale(size
.x
, size
.y
, wxIMAGE_QUALITY_HIGH
);
333 m_bitmap
= wxBitmap(img
);
339 void OnRotate(wxCommandEvent
& event
)
342 if ( event
.GetId() == ID_ROTATE_LEFT
)
345 wxImage
img(m_bitmap
.ConvertToImage());
346 img
= img
.Rotate(angle
, wxPoint(img
.GetWidth() / 2, img
.GetHeight() / 2));
349 wxLogWarning(_T("Rotation failed"));
353 m_bitmap
= wxBitmap(img
);
360 void UpdateStatusBar()
362 wxLogStatus(this, _T("Image size: (%d, %d)"),
364 m_bitmap
.GetHeight());
369 DECLARE_EVENT_TABLE()
372 #ifdef wxHAVE_RAW_BITMAP
374 #include "wx/rawbmp.h"
376 class MyRawBitmapFrame
: public wxFrame
383 REAL_SIZE
= SIZE
- 2*BORDER
386 MyRawBitmapFrame(wxFrame
*parent
)
387 : wxFrame(parent
, wxID_ANY
, _T("Raw bitmaps (how exciting)")),
388 m_bitmap(SIZE
, SIZE
, 24),
389 m_alphaBitmap(SIZE
, SIZE
, 32)
391 SetClientSize(SIZE
, SIZE
*2+25);
398 void InitAlphaBitmap()
400 // First, clear the whole bitmap by making it alpha
402 wxAlphaPixelData
data( m_alphaBitmap
, wxPoint(0,0), wxSize(SIZE
, SIZE
) );
405 wxLogError(_T("Failed to gain raw access to bitmap data"));
408 wxAlphaPixelData::Iterator
p(data
);
409 for ( int y
= 0; y
< SIZE
; ++y
)
411 wxAlphaPixelData::Iterator rowStart
= p
;
412 for ( int x
= 0; x
< SIZE
; ++x
)
415 ++p
; // same as p.OffsetX(1)
422 // Then, draw colourful alpha-blended stripes
423 wxAlphaPixelData
data(m_alphaBitmap
, wxPoint(BORDER
, BORDER
),
424 wxSize(REAL_SIZE
, REAL_SIZE
));
427 wxLogError(_T("Failed to gain raw access to bitmap data"));
431 wxAlphaPixelData::Iterator
p(data
);
433 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
435 wxAlphaPixelData::Iterator rowStart
= p
;
437 int r
= y
< REAL_SIZE
/3 ? 255 : 0,
438 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ? 255 : 0,
439 b
= 2*(REAL_SIZE
/3) <= y
? 255 : 0;
441 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
443 // note that RGB must be premultiplied by alpha
444 unsigned a
= (wxAlphaPixelData::Iterator::ChannelType
)((x
*255.)/REAL_SIZE
);
445 p
.Red() = r
* a
/ 256;
446 p
.Green() = g
* a
/ 256;
447 p
.Blue() = b
* a
/ 256;
450 ++p
; // same as p.OffsetX(1)
460 // draw some colourful stripes without alpha
461 wxNativePixelData
data(m_bitmap
);
464 wxLogError(_T("Failed to gain raw access to bitmap data"));
468 wxNativePixelData::Iterator
p(data
);
469 for ( int y
= 0; y
< SIZE
; ++y
)
471 wxNativePixelData::Iterator rowStart
= p
;
473 int r
= y
< SIZE
/3 ? 255 : 0,
474 g
= (SIZE
/3 <= y
) && (y
< 2*(SIZE
/3)) ? 255 : 0,
475 b
= 2*(SIZE
/3) <= y
? 255 : 0;
477 for ( int x
= 0; x
< SIZE
; ++x
)
482 ++p
; // same as p.OffsetX(1)
490 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
492 wxPaintDC
dc( this );
493 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER
);
494 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
495 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
496 dc
.DrawBitmap( m_alphaBitmap
, 0, 0, true /* use mask */ );
498 dc
.DrawText(_T("Raw bitmap access without alpha"), 0, SIZE
+5);
499 dc
.DrawBitmap( m_bitmap
, 0, SIZE
+5+dc
.GetCharHeight());
504 wxBitmap m_alphaBitmap
;
506 DECLARE_EVENT_TABLE()
509 #endif // wxHAVE_RAW_BITMAP
513 class MyApp
: public wxApp
516 virtual bool OnInit();
525 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
526 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
527 EVT_PAINT(MyImageFrame::OnPaint
)
529 EVT_MENU(wxID_SAVE
, MyImageFrame::OnSave
)
530 EVT_MENU_RANGE(ID_ROTATE_LEFT
, ID_ROTATE_RIGHT
, MyImageFrame::OnRotate
)
531 EVT_MENU(ID_RESIZE
, MyImageFrame::OnResize
)
534 #ifdef wxHAVE_RAW_BITMAP
536 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
537 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
540 #endif // wxHAVE_RAW_BITMAP
542 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
543 EVT_PAINT(MyCanvas::OnPaint
)
546 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
547 const wxPoint
&pos
, const wxSize
&size
)
548 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
549 , m_bmpSmileXpm(smile_xpm
)
550 , m_iconSmileXpm(smile_xpm
)
555 SetBackgroundColour(* wxWHITE
);
557 wxBitmap
bitmap( 100, 100 );
560 dc
.SelectObject( bitmap
);
561 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
562 dc
.SetPen( *wxBLACK_PEN
);
563 dc
.DrawRectangle( 0, 0, 100, 100 );
564 dc
.SetBrush( *wxWHITE_BRUSH
);
565 dc
.DrawRectangle( 20, 20, 60, 60 );
566 dc
.SelectObject( wxNullBitmap
);
568 // try to find the directory with our images
570 if ( wxFile::Exists(wxT("./horse.png")) )
572 else if ( wxFile::Exists(wxT("../horse.png")) )
575 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
577 wxImage image
= bitmap
.ConvertToImage();
580 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
581 wxLogError(wxT("Can't save file"));
585 if ( image
.LoadFile( dir
+ _T("test.png") ) )
586 my_square
= wxBitmap( image
);
590 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
591 wxLogError(wxT("Can't load PNG image"));
593 my_horse_png
= wxBitmap( image
);
595 if ( !image
.LoadFile( dir
+ _T("toucan.png")) )
596 wxLogError(wxT("Can't load PNG image"));
598 my_toucan
= wxBitmap(image
);
600 my_toucan_flipped_horiz
= wxBitmap(image
.Mirror(true));
601 my_toucan_flipped_vert
= wxBitmap(image
.Mirror(false));
602 my_toucan_flipped_both
= wxBitmap(image
.Mirror(true).Mirror(false));
603 my_toucan_grey
= wxBitmap(image
.ConvertToGreyscale());
604 my_toucan_head
= wxBitmap(image
.GetSubImage(wxRect(40, 7, 80, 60)));
605 my_toucan_scaled_normal
= wxBitmap(image
.Scale(110,90,wxIMAGE_QUALITY_NORMAL
));
606 my_toucan_scaled_high
= wxBitmap(image
.Scale(110,90,wxIMAGE_QUALITY_HIGH
));
607 my_toucan_blur
= wxBitmap(image
.Blur(10));
609 #endif // wxUSE_LIBPNG
614 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
615 wxLogError(wxT("Can't load JPG image"));
618 my_horse_jpeg
= wxBitmap( image
);
620 // Colorize by rotating green hue to red
621 wxImage::HSVValue greenHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0));
622 wxImage::HSVValue redHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0));
623 image
.RotateHue(redHSV
.hue
- greenHSV
.hue
);
624 colorized_horse_jpeg
= wxBitmap( image
);
627 if ( !image
.LoadFile( dir
+ _T("cmyk.jpg")) )
628 wxLogError(_T("Can't load CMYK JPG image"));
630 my_cmyk_jpeg
= wxBitmap(image
);
631 #endif // wxUSE_LIBJPEG
636 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
637 wxLogError(wxT("Can't load GIF image"));
639 my_horse_gif
= wxBitmap( image
);
645 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
646 wxLogError(wxT("Can't load PCX image"));
648 my_horse_pcx
= wxBitmap( image
);
653 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
654 wxLogError(wxT("Can't load BMP image"));
656 my_horse_bmp
= wxBitmap( image
);
661 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
662 wxLogError(wxT("Can't load XPM image"));
664 my_horse_xpm
= wxBitmap( image
);
666 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
667 wxLogError(wxT("Can't save file"));
673 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
674 wxLogError(wxT("Can't load PNM image"));
676 my_horse_pnm
= wxBitmap( image
);
680 if ( !image
.LoadFile( dir
+ _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM
) )
681 wxLogError(wxT("Can't load PNM image"));
683 my_horse_asciigrey_pnm
= wxBitmap( image
);
687 if ( !image
.LoadFile( dir
+ _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM
) )
688 wxLogError(wxT("Can't load PNM image"));
690 my_horse_rawgrey_pnm
= wxBitmap( image
);
696 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
697 wxLogError(wxT("Can't load TIFF image"));
699 my_horse_tiff
= wxBitmap( image
);
705 if ( !image
.LoadFile( dir
+ _T("horse.tga"), wxBITMAP_TYPE_TGA
) )
706 wxLogError(wxT("Can't load TGA image"));
708 my_horse_tga
= wxBitmap( image
);
711 CreateAntiAliasedBitmap();
713 my_smile_xbm
= wxBitmap( (const char*)smile_bits
, smile_width
,
716 // demonstrates XPM automatically using the mask when saving
717 if ( m_bmpSmileXpm
.Ok() )
718 m_bmpSmileXpm
.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM
);
723 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
724 wxLogError(wxT("Can't load first ICO image"));
726 my_horse_ico32
= wxBitmap( image
);
730 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
731 wxLogError(wxT("Can't load second ICO image"));
733 my_horse_ico16
= wxBitmap( image
);
737 if ( !image
.LoadFile( dir
+ _T("horse.ico") ) )
738 wxLogError(wxT("Can't load best ICO image"));
740 my_horse_ico
= wxBitmap( image
);
744 if ( !image
.LoadFile( dir
+ _T("horse.cur"), wxBITMAP_TYPE_CUR
) )
745 wxLogError(wxT("Can't load best ICO image"));
748 my_horse_cur
= wxBitmap( image
);
749 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
750 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
753 m_ani_images
= wxImage::GetImageCount ( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
);
755 wxLogError(wxT("No ANI-format images found"));
757 my_horse_ani
= new wxBitmap
[m_ani_images
];
759 for (i
=0; i
< m_ani_images
; i
++)
762 if (!image
.LoadFile( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
, i
))
764 wxString tmp
= wxT("Can't load image number ");
769 my_horse_ani
[i
] = wxBitmap( image
);
771 #endif // wxUSE_ICO_CUR
775 // test image loading from stream
776 wxFile
file(dir
+ _T("horse.bmp"));
777 if ( file
.IsOpened() )
779 wxFileOffset len
= file
.Length();
780 size_t dataSize
= (size_t)len
;
781 void *data
= malloc(dataSize
);
782 if ( file
.Read(data
, dataSize
) != len
)
783 wxLogError(_T("Reading bitmap file failed"));
786 wxMemoryInputStream
mis(data
, dataSize
);
787 if ( !image
.LoadFile(mis
) )
788 wxLogError(wxT("Can't load BMP image from stream"));
790 my_horse_bmp2
= wxBitmap( image
);
797 MyCanvas::~MyCanvas()
799 delete [] my_horse_ani
;
802 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
804 wxPaintDC
dc( this );
807 dc
.DrawText( _T("Loaded image"), 30, 10 );
809 dc
.DrawBitmap( my_square
, 30, 30 );
811 dc
.DrawText( _T("Drawn directly"), 150, 10 );
812 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
813 dc
.SetPen( *wxBLACK_PEN
);
814 dc
.DrawRectangle( 150, 30, 100, 100 );
815 dc
.SetBrush( *wxWHITE_BRUSH
);
816 dc
.DrawRectangle( 170, 50, 60, 60 );
819 dc
.DrawBitmap( my_anti
, 280, 30 );
821 dc
.DrawText( _T("PNG handler"), 30, 135 );
822 if (my_horse_png
.Ok())
824 dc
.DrawBitmap( my_horse_png
, 30, 150 );
825 wxRect
rect(0,0,100,100);
826 wxBitmap
sub( my_horse_png
.GetSubBitmap(rect
) );
827 dc
.DrawText( _T("GetSubBitmap()"), 280, 175 );
828 dc
.DrawBitmap( sub
, 280, 195 );
831 dc
.DrawText( _T("JPEG handler"), 30, 365 );
832 if (my_horse_jpeg
.Ok())
833 dc
.DrawBitmap( my_horse_jpeg
, 30, 380 );
835 dc
.DrawText( _T("Green rotated to red"), 280, 365 );
836 if (colorized_horse_jpeg
.Ok())
837 dc
.DrawBitmap( colorized_horse_jpeg
, 280, 380 );
839 dc
.DrawText( _T("CMYK JPEG image"), 530, 365 );
840 if (my_cmyk_jpeg
.Ok())
841 dc
.DrawBitmap( my_cmyk_jpeg
, 530, 380 );
843 dc
.DrawText( _T("GIF handler"), 30, 595 );
844 if (my_horse_gif
.Ok())
845 dc
.DrawBitmap( my_horse_gif
, 30, 610 );
847 dc
.DrawText( _T("PCX handler"), 30, 825 );
848 if (my_horse_pcx
.Ok())
849 dc
.DrawBitmap( my_horse_pcx
, 30, 840 );
851 dc
.DrawText( _T("BMP handler"), 30, 1055 );
852 if (my_horse_bmp
.Ok())
853 dc
.DrawBitmap( my_horse_bmp
, 30, 1070 );
855 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
856 if (my_horse_bmp2
.Ok())
857 dc
.DrawBitmap( my_horse_bmp2
, 280, 1070 );
859 dc
.DrawText( _T("PNM handler"), 30, 1285 );
860 if (my_horse_pnm
.Ok())
861 dc
.DrawBitmap( my_horse_pnm
, 30, 1300 );
863 dc
.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 );
864 if (my_horse_asciigrey_pnm
.Ok())
865 dc
.DrawBitmap( my_horse_asciigrey_pnm
, 280, 1300 );
867 dc
.DrawText( _T("PNM handler (raw grey)"), 530, 1285 );
868 if (my_horse_rawgrey_pnm
.Ok())
869 dc
.DrawBitmap( my_horse_rawgrey_pnm
, 530, 1300 );
871 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
872 if (my_horse_tiff
.Ok())
873 dc
.DrawBitmap( my_horse_tiff
, 30, 1530 );
875 dc
.DrawText( _T("TGA handler"), 30, 1745 );
876 if (my_horse_tga
.Ok())
877 dc
.DrawBitmap( my_horse_tga
, 30, 1760 );
879 dc
.DrawText( _T("XPM handler"), 30, 1975 );
880 if (my_horse_xpm
.Ok())
881 dc
.DrawBitmap( my_horse_xpm
, 30, 2000 );
885 int x
= 750, y
= 10, yy
= 170;
887 dc
.DrawText(wxT("Original toucan"), x
+50, y
);
888 dc
.DrawBitmap(my_toucan
, x
, y
+15, true);
890 dc
.DrawText(wxT("Flipped horizontally"), x
+50, y
);
891 dc
.DrawBitmap(my_toucan_flipped_horiz
, x
, y
+15, true);
893 dc
.DrawText(wxT("Flipped vertically"), x
+50, y
);
894 dc
.DrawBitmap(my_toucan_flipped_vert
, x
, y
+15, true);
896 dc
.DrawText(wxT("Flipped both h&v"), x
+50, y
);
897 dc
.DrawBitmap(my_toucan_flipped_both
, x
, y
+15, true);
900 dc
.DrawText(wxT("In greyscale"), x
+50, y
);
901 dc
.DrawBitmap(my_toucan_grey
, x
, y
+15, true);
904 dc
.DrawText(wxT("Toucan's head"), x
+50, y
);
905 dc
.DrawBitmap(my_toucan_head
, x
, y
+15, true);
908 dc
.DrawText(wxT("Scaled with normal quality"), x
+50, y
);
909 dc
.DrawBitmap(my_toucan_scaled_normal
, x
, y
+15, true);
912 dc
.DrawText(wxT("Scaled with high quality"), x
+50, y
);
913 dc
.DrawBitmap(my_toucan_scaled_high
, x
, y
+15, true);
916 dc
.DrawText(wxT("Blured"), x
+50, y
);
917 dc
.DrawBitmap(my_toucan_blur
, x
, y
+15, true);
920 if (my_smile_xbm
.Ok())
922 int x
= 300, y
= 1800;
924 dc
.DrawText( _T("XBM bitmap"), x
, y
);
925 dc
.DrawText( _T("(green on red)"), x
, y
+ 15 );
926 dc
.SetTextForeground( _T("GREEN") );
927 dc
.SetTextBackground( _T("RED") );
928 dc
.DrawBitmap( my_smile_xbm
, x
, y
+ 30 );
930 dc
.SetTextForeground( *wxBLACK
);
931 dc
.DrawText( _T("After wxImage conversion"), x
+ 120, y
);
932 dc
.DrawText( _T("(red on white)"), x
+ 120, y
+ 15 );
933 dc
.SetTextForeground( wxT("RED") );
934 wxImage i
= my_smile_xbm
.ConvertToImage();
935 i
.SetMaskColour( 255, 255, 255 );
937 wxRED_PEN
->GetColour().Red(),
938 wxRED_PEN
->GetColour().Green(),
939 wxRED_PEN
->GetColour().Blue() );
940 dc
.DrawBitmap( wxBitmap(i
), x
+ 120, y
+ 30, true );
941 dc
.SetTextForeground( *wxBLACK
);
945 wxBitmap
mono( 60,50,1 );
947 memdc
.SelectObject( mono
);
948 memdc
.SetPen( *wxBLACK_PEN
);
949 memdc
.SetBrush( *wxWHITE_BRUSH
);
950 memdc
.DrawRectangle( 0,0,60,50 );
951 memdc
.SetTextForeground( *wxBLACK
);
953 // I cannot convince GTK2 to draw into mono bitmaps
954 memdc
.DrawText( _T("Hi!"), 5, 5 );
956 memdc
.SetBrush( *wxBLACK_BRUSH
);
957 memdc
.DrawRectangle( 33,5,20,20 );
958 memdc
.SetPen( *wxRED_PEN
);
959 memdc
.DrawLine( 5, 42, 50, 42 );
960 memdc
.SelectObject( wxNullBitmap
);
964 int x
= 300, y
= 1900;
966 dc
.DrawText( _T("Mono bitmap"), x
, y
);
967 dc
.DrawText( _T("(red on green)"), x
, y
+ 15 );
968 dc
.SetTextForeground( wxT("RED") );
969 dc
.SetTextBackground( wxT("GREEN") );
970 dc
.DrawBitmap( mono
, x
, y
+ 30 );
972 dc
.SetTextForeground( *wxBLACK
);
973 dc
.DrawText( _T("After wxImage conversion"), x
+ 120, y
);
974 dc
.DrawText( _T("(red on white)"), x
+ 120, y
+ 15 );
975 dc
.SetTextForeground( wxT("RED") );
976 wxImage i
= mono
.ConvertToImage();
977 i
.SetMaskColour( 255,255,255 );
979 wxRED_PEN
->GetColour().Red(),
980 wxRED_PEN
->GetColour().Green(),
981 wxRED_PEN
->GetColour().Blue() );
982 dc
.DrawBitmap( wxBitmap(i
), x
+ 120, y
+ 30, true );
983 dc
.SetTextForeground( *wxBLACK
);
986 // For testing transparency
987 dc
.SetBrush( *wxRED_BRUSH
);
988 dc
.DrawRectangle( 20, 2220, 560, 68 );
990 dc
.DrawText(_T("XPM bitmap"), 30, 2230 );
991 if ( m_bmpSmileXpm
.Ok() )
992 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, true);
994 dc
.DrawText(_T("XPM icon"), 110, 2230 );
995 if ( m_iconSmileXpm
.Ok() )
996 dc
.DrawIcon(m_iconSmileXpm
, 110, 2250);
998 // testing icon -> bitmap conversion
999 wxBitmap
to_blit( m_iconSmileXpm
);
1002 dc
.DrawText( _T("SubBitmap"), 170, 2230 );
1003 wxBitmap sub
= to_blit
.GetSubBitmap( wxRect(0,0,15,15) );
1005 dc
.DrawBitmap( sub
, 170, 2250, true );
1007 dc
.DrawText( _T("Enlarged"), 250, 2230 );
1008 dc
.SetUserScale( 1.5, 1.5 );
1009 dc
.DrawBitmap( to_blit
, (int)(250/1.5), (int)(2250/1.5), true );
1010 dc
.SetUserScale( 2, 2 );
1011 dc
.DrawBitmap( to_blit
, (int)(300/2), (int)(2250/2), true );
1012 dc
.SetUserScale( 1.0, 1.0 );
1014 dc
.DrawText( _T("Blit"), 400, 2230);
1016 blit_dc
.SelectObject( to_blit
);
1017 dc
.Blit( 400, 2250, to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
1018 dc
.SetUserScale( 1.5, 1.5 );
1019 dc
.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
1020 dc
.SetUserScale( 2, 2 );
1021 dc
.Blit( (int)(500/2), (int)(2250/2), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
1022 dc
.SetUserScale( 1.0, 1.0 );
1025 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
1026 if (my_horse_ico32
.Ok())
1027 dc
.DrawBitmap( my_horse_ico32
, 30, 2330, true );
1029 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
1030 if (my_horse_ico16
.Ok())
1031 dc
.DrawBitmap( my_horse_ico16
, 230, 2330, true );
1033 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
1034 if (my_horse_ico
.Ok())
1035 dc
.DrawBitmap( my_horse_ico
, 430, 2330, true );
1037 dc
.DrawText( _T("CUR handler"), 30, 2390 );
1038 if (my_horse_cur
.Ok())
1040 dc
.DrawBitmap( my_horse_cur
, 30, 2420, true );
1041 dc
.SetPen (*wxRED_PEN
);
1042 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
1043 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
1046 dc
.DrawText( _T("ANI handler"), 230, 2390 );
1047 for ( int i
=0; i
< m_ani_images
; i
++ )
1049 if (my_horse_ani
[i
].Ok())
1051 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, true );
1056 void MyCanvas::CreateAntiAliasedBitmap()
1058 wxBitmap
bitmap( 300, 300 );
1062 dc
.SelectObject( bitmap
);
1066 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
1067 dc
.SetTextForeground( wxT("RED") );
1068 dc
.DrawText( _T("This is anti-aliased Text."), 20, 5 );
1069 dc
.DrawText( _T("And a Rectangle."), 20, 45 );
1071 dc
.SetBrush( *wxRED_BRUSH
);
1072 dc
.SetPen( *wxTRANSPARENT_PEN
);
1073 dc
.DrawRoundedRectangle( 20, 85, 200, 180, 20 );
1075 wxImage original
= bitmap
.ConvertToImage();
1076 wxImage
anti( 150, 150 );
1078 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
1080 for (int y
= 1; y
< 149; y
++)
1081 for (int x
= 1; x
< 149; x
++)
1083 int red
= original
.GetRed( x
*2, y
*2 ) +
1084 original
.GetRed( x
*2-1, y
*2 ) +
1085 original
.GetRed( x
*2, y
*2+1 ) +
1086 original
.GetRed( x
*2+1, y
*2+1 );
1089 int green
= original
.GetGreen( x
*2, y
*2 ) +
1090 original
.GetGreen( x
*2-1, y
*2 ) +
1091 original
.GetGreen( x
*2, y
*2+1 ) +
1092 original
.GetGreen( x
*2+1, y
*2+1 );
1095 int blue
= original
.GetBlue( x
*2, y
*2 ) +
1096 original
.GetBlue( x
*2-1, y
*2 ) +
1097 original
.GetBlue( x
*2, y
*2+1 ) +
1098 original
.GetBlue( x
*2+1, y
*2+1 );
1100 anti
.SetRGB( x
, y
, (unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
1102 my_anti
= wxBitmap(anti
);
1109 ID_QUIT
= wxID_EXIT
,
1110 ID_ABOUT
= wxID_ABOUT
,
1116 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
1118 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
1119 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
1120 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
1121 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
1122 EVT_MENU (ID_INFO
, MyFrame::OnImageInfo
)
1123 #ifdef wxHAVE_RAW_BITMAP
1124 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
1128 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
1129 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
1130 #endif // wxUSE_CLIPBOARD
1134 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxImage sample"),
1135 wxPoint(20, 20), wxSize(950, 700) )
1137 wxMenuBar
*menu_bar
= new wxMenuBar();
1139 wxMenu
*menuImage
= new wxMenu
;
1140 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
1141 menuImage
->Append( ID_INFO
, _T("Show image &information...\tCtrl-I"));
1142 #ifdef wxHAVE_RAW_BITMAP
1143 menuImage
->AppendSeparator();
1144 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
1146 menuImage
->AppendSeparator();
1147 menuImage
->Append( ID_ABOUT
, _T("&About..."));
1148 menuImage
->AppendSeparator();
1149 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
1150 menu_bar
->Append(menuImage
, _T("&Image"));
1153 wxMenu
*menuClipboard
= new wxMenu
;
1154 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
1155 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
1156 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
1157 #endif // wxUSE_CLIPBOARD
1159 SetMenuBar( menu_bar
);
1163 int widths
[] = { -1, 100 };
1164 SetStatusWidths( 2, widths
);
1165 #endif // wxUSE_STATUSBAR
1167 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
1169 // 500 width * 2750 height
1170 m_canvas
->SetScrollbars( 10, 10, 50, 275 );
1173 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
1178 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
1180 (void)wxMessageBox( _T("wxImage demo\n")
1181 _T("Robert Roebling (c) 1998,2000"),
1182 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
1185 wxString
MyFrame::LoadUserImage(wxImage
& image
)
1190 filename
= wxFileSelector(_T("Select image file"));
1191 if ( !filename
.empty() )
1193 if ( !image
.LoadFile(filename
) )
1195 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
1197 return wxEmptyString
;
1200 #endif // wxUSE_FILEDLG
1205 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
1208 wxString filename
= LoadUserImage(image
);
1209 if ( !filename
.empty() )
1210 (new MyImageFrame(this, filename
, wxBitmap(image
)))->Show();
1213 void MyFrame::OnImageInfo( wxCommandEvent
&WXUNUSED(event
) )
1216 if ( !LoadUserImage(image
).empty() )
1218 // TODO: show more information about the file
1219 wxString info
= wxString::Format("Image size: %dx%d",
1223 int xres
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
),
1224 yres
= image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
1227 info
+= wxString::Format("\nResolution: %dx%d", xres
, yres
);
1228 switch ( image
.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT
) )
1231 wxFAIL_MSG( "unknown image resolution units" );
1234 case wxIMAGE_RESOLUTION_NONE
:
1235 info
+= " in default units";
1238 case wxIMAGE_RESOLUTION_INCHES
:
1242 case wxIMAGE_RESOLUTION_CM
:
1248 wxLogMessage("%s", info
);
1252 #ifdef wxHAVE_RAW_BITMAP
1254 void MyFrame::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
1256 (new MyRawBitmapFrame(this))->Show();
1259 #endif // wxHAVE_RAW_BITMAP
1263 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1265 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
1266 dobjBmp
->SetBitmap(m_canvas
->my_horse_png
);
1268 wxTheClipboard
->Open();
1270 if ( !wxTheClipboard
->SetData(dobjBmp
) )
1272 wxLogError(_T("Failed to copy bitmap to clipboard"));
1275 wxTheClipboard
->Close();
1278 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1280 wxBitmapDataObject dobjBmp
;
1282 wxTheClipboard
->Open();
1283 if ( !wxTheClipboard
->GetData(dobjBmp
) )
1285 wxLogMessage(_T("No bitmap data in the clipboard"));
1289 (new MyImageFrame(this, _T("Clipboard"), dobjBmp
.GetBitmap()))->Show();
1291 wxTheClipboard
->Close();
1294 #endif // wxUSE_CLIPBOARD
1296 //-----------------------------------------------------------------------------
1298 //-----------------------------------------------------------------------------
1300 bool MyApp::OnInit()
1302 if ( !wxApp::OnInit() )
1305 wxInitAllImageHandlers();
1307 wxFrame
*frame
= new MyFrame();
1308 frame
->Show( true );