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 #ifdef wxHAVE_RAW_BITMAP
122 void OnTestRawBitmap( wxCommandEvent
&event
);
123 #endif // wxHAVE_RAW_BITMAP
124 void OnQuit( wxCommandEvent
&event
);
127 void OnCopy(wxCommandEvent
& event
);
128 void OnPaste(wxCommandEvent
& event
);
129 #endif // wxUSE_CLIPBOARD
134 DECLARE_DYNAMIC_CLASS(MyFrame
)
135 DECLARE_EVENT_TABLE()
138 // ----------------------------------------------------------------------------
139 // Frame used for showing a standalone image
140 // ----------------------------------------------------------------------------
144 ID_ROTATE_LEFT
= 100,
148 class MyImageFrame
: public wxFrame
151 MyImageFrame(wxFrame
*parent
, const wxString
& desc
, const wxBitmap
& bitmap
)
152 : wxFrame(parent
, wxID_ANY
,
153 wxString
::Format(_T("Image from %s"), desc
.c_str()),
154 wxDefaultPosition
, wxDefaultSize
,
155 wxCAPTION
| wxSYSTEM_MENU
| wxCLOSE_BOX
),
158 wxMenu
*menu
= new wxMenu
;
159 menu
->Append(wxID_SAVE
);
160 menu
->AppendSeparator();
161 menu
->Append(ID_ROTATE_LEFT
, _T("Rotate &left\tCtrl-L"));
162 menu
->Append(ID_ROTATE_RIGHT
, _T("Rotate &right\tCtrl-R"));
164 wxMenuBar
*mbar
= new wxMenuBar
;
165 mbar
->Append(menu
, _T("&Image"));
168 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
171 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
173 // do nothing here to be able to see how transparent images are shown
176 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
178 wxPaintDC
dc( this );
179 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
182 void OnSave(wxCommandEvent
& WXUNUSED(event
))
185 wxImage image
= m_bitmap
.ConvertToImage();
187 wxString savefilename
= wxFileSelector( wxT("Save Image"),
190 (const wxChar
*)NULL
,
191 wxT("BMP files (*.bmp)|*.bmp|")
192 wxT("PNG files (*.png)|*.png|")
193 wxT("JPEG files (*.jpg)|*.jpg|")
194 wxT("GIF files (*.gif)|*.gif|")
195 wxT("TIFF files (*.tif)|*.tif|")
196 wxT("PCX files (*.pcx)|*.pcx|")
197 wxT("ICO files (*.ico)|*.ico|")
198 wxT("CUR files (*.cur)|*.cur"),
202 if ( savefilename
.empty() )
206 wxFileName
::SplitPath(savefilename
, NULL
, NULL
, &extension
);
209 if ( extension
== _T("bmp") )
211 static const int bppvalues
[] =
223 const wxString bppchoices
[] =
229 _T("8 bpp greyscale"),
231 _T("8 bpp own palette"),
235 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
236 _T("Image sample: save file"),
237 WXSIZEOF(bppchoices
),
240 if ( bppselection
!= -1 )
242 int format
= bppvalues
[bppselection
];
243 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, format
);
245 if ( format
== wxBMP_8BPP_PALETTE
)
247 unsigned char *cmap
= new unsigned char [256];
248 for ( int i
= 0; i
< 256; i
++ )
249 cmap
[i
] = (unsigned char)i
;
250 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
256 else if ( extension
== _T("png") )
258 static const int pngvalues
[] =
268 const wxString pngchoices
[] =
275 _T("Grey red 16bpp"),
278 int sel
= wxGetSingleChoiceIndex(_T("Set PNG format"),
279 _T("Image sample: save file"),
280 WXSIZEOF(pngchoices
),
285 image
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, pngvalues
[sel
]);
286 image
.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH
, sel
% 2 ?
16 : 8);
289 else if ( extension
== _T("cur") )
291 image
.Rescale(32,32);
292 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
293 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
294 // This shows how you can save an image with explicitly
295 // specified image format:
296 saved
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
301 // This one guesses image format from filename extension
302 // (it may fail if the extension is not recognized):
303 image
.SaveFile(savefilename
);
305 #endif // wxUSE_FILEDLG
308 void OnRotate(wxCommandEvent
& event
)
311 if ( event
.GetId() == ID_ROTATE_LEFT
)
314 wxImage
img(m_bitmap
.ConvertToImage());
315 img
= img
.Rotate(angle
, wxPoint(0, 0));//wxPoint(img.GetWidth() / 2, img.GetHeight() / 2));
318 wxLogWarning(_T("Rotation failed"));
322 m_bitmap
= wxBitmap(img
);
329 DECLARE_EVENT_TABLE()
332 #ifdef wxHAVE_RAW_BITMAP
334 #include "wx/rawbmp.h"
336 class MyRawBitmapFrame
: public wxFrame
343 REAL_SIZE
= SIZE
- 2*BORDER
346 MyRawBitmapFrame(wxFrame
*parent
)
347 : wxFrame(parent
, wxID_ANY
, _T("Raw bitmaps (how exciting)")),
348 m_bitmap(SIZE
, SIZE
, 24),
349 m_alphaBitmap(SIZE
, SIZE
, 32)
351 SetClientSize(SIZE
, SIZE
*2+25);
358 void InitAlphaBitmap()
360 // First, clear the whole bitmap by making it alpha
362 wxAlphaPixelData
data( m_alphaBitmap
, wxPoint(0,0), wxSize(SIZE
, SIZE
) );
365 wxLogError(_T("Failed to gain raw access to bitmap data"));
369 wxAlphaPixelData
::Iterator
p(data
);
370 for ( int y
= 0; y
< SIZE
; ++y
)
372 wxAlphaPixelData
::Iterator rowStart
= p
;
373 for ( int x
= 0; x
< SIZE
; ++x
)
376 ++p
; // same as p.OffsetX(1)
383 // Then, draw colourful alpha-blended stripes
384 wxAlphaPixelData
data(m_alphaBitmap
, wxPoint(BORDER
, BORDER
),
385 wxSize(REAL_SIZE
, REAL_SIZE
));
388 wxLogError(_T("Failed to gain raw access to bitmap data"));
393 wxAlphaPixelData
::Iterator
p(data
);
395 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
397 wxAlphaPixelData
::Iterator rowStart
= p
;
399 int r
= y
< REAL_SIZE
/3 ?
255 : 0,
400 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ?
255 : 0,
401 b
= 2*(REAL_SIZE
/3) <= y ?
255 : 0;
403 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
405 // note that RGB must be premultiplied by alpha
406 unsigned a
= (wxAlphaPixelData
::Iterator
::ChannelType
)((x
*255.)/REAL_SIZE
);
407 p
.Red() = r
* a
/ 256;
408 p
.Green() = g
* a
/ 256;
409 p
.Blue() = b
* a
/ 256;
412 ++p
; // same as p.OffsetX(1)
422 // draw some colourful stripes without alpha
423 wxNativePixelData
data(m_bitmap
);
426 wxLogError(_T("Failed to gain raw access to bitmap data"));
430 wxNativePixelData
::Iterator
p(data
);
431 for ( int y
= 0; y
< SIZE
; ++y
)
433 wxNativePixelData
::Iterator rowStart
= p
;
435 int r
= y
< SIZE
/3 ?
255 : 0,
436 g
= (SIZE
/3 <= y
) && (y
< 2*(SIZE
/3)) ?
255 : 0,
437 b
= 2*(SIZE
/3) <= y ?
255 : 0;
439 for ( int x
= 0; x
< SIZE
; ++x
)
444 ++p
; // same as p.OffsetX(1)
452 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
454 wxPaintDC
dc( this );
455 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER
);
456 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
457 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
458 dc
.DrawBitmap( m_alphaBitmap
, 0, 0, true /* use mask */ );
460 dc
.DrawText(_T("Raw bitmap access without alpha"), 0, SIZE
+5);
461 dc
.DrawBitmap( m_bitmap
, 0, SIZE
+5+dc
.GetCharHeight());
466 wxBitmap m_alphaBitmap
;
468 DECLARE_EVENT_TABLE()
471 #endif // wxHAVE_RAW_BITMAP
475 class MyApp
: public wxApp
478 virtual bool OnInit();
487 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
488 EVT_ERASE_BACKGROUND(MyImageFrame
::OnEraseBackground
)
489 EVT_PAINT(MyImageFrame
::OnPaint
)
491 EVT_MENU(wxID_SAVE
, MyImageFrame
::OnSave
)
492 EVT_MENU_RANGE(ID_ROTATE_LEFT
, ID_ROTATE_RIGHT
, MyImageFrame
::OnRotate
)
495 #ifdef wxHAVE_RAW_BITMAP
497 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
498 EVT_PAINT(MyRawBitmapFrame
::OnPaint
)
501 #endif // wxHAVE_RAW_BITMAP
503 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
504 EVT_PAINT(MyCanvas
::OnPaint
)
507 MyCanvas
::MyCanvas( wxWindow
*parent
, wxWindowID id
,
508 const wxPoint
&pos
, const wxSize
&size
)
509 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
510 , m_bmpSmileXpm(smile_xpm
)
511 , m_iconSmileXpm(smile_xpm
)
516 SetBackgroundColour(* wxWHITE
);
518 wxBitmap
bitmap( 100, 100 );
521 dc
.SelectObject( bitmap
);
522 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
523 dc
.SetPen( *wxBLACK_PEN
);
524 dc
.DrawRectangle( 0, 0, 100, 100 );
525 dc
.SetBrush( *wxWHITE_BRUSH
);
526 dc
.DrawRectangle( 20, 20, 60, 60 );
527 dc
.SelectObject( wxNullBitmap
);
529 // try to find the directory with our images
531 if ( wxFile
::Exists(wxT("./horse.png")) )
533 else if ( wxFile
::Exists(wxT("../horse.png")) )
536 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
538 wxImage image
= bitmap
.ConvertToImage();
541 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
542 wxLogError(wxT("Can't save file"));
546 if ( image
.LoadFile( dir
+ _T("test.png") ) )
547 my_square
= wxBitmap( image
);
551 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
552 wxLogError(wxT("Can't load PNG image"));
554 my_horse_png
= wxBitmap( image
);
556 if ( !image
.LoadFile( dir
+ _T("toucan.png")) )
557 wxLogError(wxT("Can't load PNG image"));
559 my_toucan
= wxBitmap(image
);
561 my_toucan_flipped_horiz
= wxBitmap(image
.Mirror(true));
562 my_toucan_flipped_vert
= wxBitmap(image
.Mirror(false));
563 my_toucan_flipped_both
= wxBitmap(image
.Mirror(true).Mirror(false));
564 my_toucan_grey
= wxBitmap(image
.ConvertToGreyscale());
565 my_toucan_head
= wxBitmap(image
.GetSubImage(wxRect(40, 7, 80, 60)));
566 my_toucan_scaled_normal
= wxBitmap(image
.Scale(110,90,wxIMAGE_QUALITY_NORMAL
));
567 my_toucan_scaled_high
= wxBitmap(image
.Scale(110,90,wxIMAGE_QUALITY_HIGH
));
568 my_toucan_blur
= wxBitmap(image
.Blur(10));
570 #endif // wxUSE_LIBPNG
575 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
576 wxLogError(wxT("Can't load JPG image"));
579 my_horse_jpeg
= wxBitmap( image
);
581 // Colorize by rotating green hue to red
582 wxImage
::HSVValue greenHSV
= wxImage
::RGBtoHSV(wxImage
::RGBValue(0, 255, 0));
583 wxImage
::HSVValue redHSV
= wxImage
::RGBtoHSV(wxImage
::RGBValue(255, 0, 0));
584 image
.RotateHue(redHSV
.hue
- greenHSV
.hue
);
585 colorized_horse_jpeg
= wxBitmap( image
);
588 if ( !image
.LoadFile( dir
+ _T("cmyk.jpg")) )
589 wxLogError(_T("Can't load CMYK JPG image"));
591 my_cmyk_jpeg
= wxBitmap(image
);
592 #endif // wxUSE_LIBJPEG
597 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
598 wxLogError(wxT("Can't load GIF image"));
600 my_horse_gif
= wxBitmap( image
);
606 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
607 wxLogError(wxT("Can't load PCX image"));
609 my_horse_pcx
= wxBitmap( image
);
614 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
615 wxLogError(wxT("Can't load BMP image"));
617 my_horse_bmp
= wxBitmap( image
);
622 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
623 wxLogError(wxT("Can't load XPM image"));
625 my_horse_xpm
= wxBitmap( image
);
627 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
628 wxLogError(wxT("Can't save file"));
634 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
635 wxLogError(wxT("Can't load PNM image"));
637 my_horse_pnm
= wxBitmap( image
);
641 if ( !image
.LoadFile( dir
+ _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM
) )
642 wxLogError(wxT("Can't load PNM image"));
644 my_horse_asciigrey_pnm
= wxBitmap( image
);
648 if ( !image
.LoadFile( dir
+ _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM
) )
649 wxLogError(wxT("Can't load PNM image"));
651 my_horse_rawgrey_pnm
= wxBitmap( image
);
657 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
658 wxLogError(wxT("Can't load TIFF image"));
660 my_horse_tiff
= wxBitmap( image
);
666 if ( !image
.LoadFile( dir
+ _T("horse.tga"), wxBITMAP_TYPE_TGA
) )
667 wxLogError(wxT("Can't load TGA image"));
669 my_horse_tga
= wxBitmap( image
);
672 CreateAntiAliasedBitmap();
674 my_smile_xbm
= wxBitmap( (const char*)smile_bits
, smile_width
,
677 // demonstrates XPM automatically using the mask when saving
678 if ( m_bmpSmileXpm
.Ok() )
679 m_bmpSmileXpm
.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM
);
684 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
685 wxLogError(wxT("Can't load first ICO image"));
687 my_horse_ico32
= wxBitmap( image
);
691 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
692 wxLogError(wxT("Can't load second ICO image"));
694 my_horse_ico16
= wxBitmap( image
);
698 if ( !image
.LoadFile( dir
+ _T("horse.ico") ) )
699 wxLogError(wxT("Can't load best ICO image"));
701 my_horse_ico
= wxBitmap( image
);
705 if ( !image
.LoadFile( dir
+ _T("horse.cur"), wxBITMAP_TYPE_CUR
) )
706 wxLogError(wxT("Can't load best ICO image"));
709 my_horse_cur
= wxBitmap( image
);
710 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
711 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
714 m_ani_images
= wxImage
::GetImageCount ( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
);
716 wxLogError(wxT("No ANI-format images found"));
718 my_horse_ani
= new wxBitmap
[m_ani_images
];
720 for (i
=0; i
< m_ani_images
; i
++)
723 if (!image
.LoadFile( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
, i
))
725 wxString tmp
= wxT("Can't load image number ");
730 my_horse_ani
[i
] = wxBitmap( image
);
732 #endif // wxUSE_ICO_CUR
736 // test image loading from stream
737 wxFile
file(dir
+ _T("horse.bmp"));
738 if ( file
.IsOpened() )
740 wxFileOffset len
= file
.Length();
741 size_t dataSize
= (size_t)len
;
742 void *data
= malloc(dataSize
);
743 if ( file
.Read(data
, dataSize
) != len
)
744 wxLogError(_T("Reading bitmap file failed"));
747 wxMemoryInputStream
mis(data
, dataSize
);
748 if ( !image
.LoadFile(mis
) )
749 wxLogError(wxT("Can't load BMP image from stream"));
751 my_horse_bmp2
= wxBitmap( image
);
758 MyCanvas
::~MyCanvas()
760 delete [] my_horse_ani
;
763 void MyCanvas
::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
765 wxPaintDC
dc( this );
768 dc
.DrawText( _T("Loaded image"), 30, 10 );
770 dc
.DrawBitmap( my_square
, 30, 30 );
772 dc
.DrawText( _T("Drawn directly"), 150, 10 );
773 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
774 dc
.SetPen( *wxBLACK_PEN
);
775 dc
.DrawRectangle( 150, 30, 100, 100 );
776 dc
.SetBrush( *wxWHITE_BRUSH
);
777 dc
.DrawRectangle( 170, 50, 60, 60 );
780 dc
.DrawBitmap( my_anti
, 280, 30 );
782 dc
.DrawText( _T("PNG handler"), 30, 135 );
783 if (my_horse_png
.Ok())
785 dc
.DrawBitmap( my_horse_png
, 30, 150 );
786 wxRect
rect(0,0,100,100);
787 wxBitmap
sub( my_horse_png
.GetSubBitmap(rect
) );
788 dc
.DrawText( _T("GetSubBitmap()"), 280, 175 );
789 dc
.DrawBitmap( sub
, 280, 195 );
792 dc
.DrawText( _T("JPEG handler"), 30, 365 );
793 if (my_horse_jpeg
.Ok())
794 dc
.DrawBitmap( my_horse_jpeg
, 30, 380 );
796 dc
.DrawText( _T("Green rotated to red"), 280, 365 );
797 if (colorized_horse_jpeg
.Ok())
798 dc
.DrawBitmap( colorized_horse_jpeg
, 280, 380 );
800 dc
.DrawText( _T("CMYK JPEG image"), 530, 365 );
801 if (my_cmyk_jpeg
.Ok())
802 dc
.DrawBitmap( my_cmyk_jpeg
, 530, 380 );
804 dc
.DrawText( _T("GIF handler"), 30, 595 );
805 if (my_horse_gif
.Ok())
806 dc
.DrawBitmap( my_horse_gif
, 30, 610 );
808 dc
.DrawText( _T("PCX handler"), 30, 825 );
809 if (my_horse_pcx
.Ok())
810 dc
.DrawBitmap( my_horse_pcx
, 30, 840 );
812 dc
.DrawText( _T("BMP handler"), 30, 1055 );
813 if (my_horse_bmp
.Ok())
814 dc
.DrawBitmap( my_horse_bmp
, 30, 1070 );
816 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
817 if (my_horse_bmp2
.Ok())
818 dc
.DrawBitmap( my_horse_bmp2
, 280, 1070 );
820 dc
.DrawText( _T("PNM handler"), 30, 1285 );
821 if (my_horse_pnm
.Ok())
822 dc
.DrawBitmap( my_horse_pnm
, 30, 1300 );
824 dc
.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 );
825 if (my_horse_asciigrey_pnm
.Ok())
826 dc
.DrawBitmap( my_horse_asciigrey_pnm
, 280, 1300 );
828 dc
.DrawText( _T("PNM handler (raw grey)"), 530, 1285 );
829 if (my_horse_rawgrey_pnm
.Ok())
830 dc
.DrawBitmap( my_horse_rawgrey_pnm
, 530, 1300 );
832 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
833 if (my_horse_tiff
.Ok())
834 dc
.DrawBitmap( my_horse_tiff
, 30, 1530 );
836 dc
.DrawText( _T("TGA handler"), 30, 1745 );
837 if (my_horse_tga
.Ok())
838 dc
.DrawBitmap( my_horse_tga
, 30, 1760 );
840 dc
.DrawText( _T("XPM handler"), 30, 1975 );
841 if (my_horse_xpm
.Ok())
842 dc
.DrawBitmap( my_horse_xpm
, 30, 2000 );
846 int x
= 750, y
= 10, yy
= 170;
848 dc
.DrawText(wxT("Original toucan"), x
+50, y
);
849 dc
.DrawBitmap(my_toucan
, x
, y
+15, true);
851 dc
.DrawText(wxT("Flipped horizontally"), x
+50, y
);
852 dc
.DrawBitmap(my_toucan_flipped_horiz
, x
, y
+15, true);
854 dc
.DrawText(wxT("Flipped vertically"), x
+50, y
);
855 dc
.DrawBitmap(my_toucan_flipped_vert
, x
, y
+15, true);
857 dc
.DrawText(wxT("Flipped both h&v"), x
+50, y
);
858 dc
.DrawBitmap(my_toucan_flipped_both
, x
, y
+15, true);
861 dc
.DrawText(wxT("In greyscale"), x
+50, y
);
862 dc
.DrawBitmap(my_toucan_grey
, x
, y
+15, true);
865 dc
.DrawText(wxT("Toucan's head"), x
+50, y
);
866 dc
.DrawBitmap(my_toucan_head
, x
, y
+15, true);
869 dc
.DrawText(wxT("Scaled with normal quality"), x
+50, y
);
870 dc
.DrawBitmap(my_toucan_scaled_normal
, x
, y
+15, true);
873 dc
.DrawText(wxT("Scaled with high quality"), x
+50, y
);
874 dc
.DrawBitmap(my_toucan_scaled_high
, x
, y
+15, true);
877 dc
.DrawText(wxT("Blured"), x
+50, y
);
878 dc
.DrawBitmap(my_toucan_blur
, x
, y
+15, true);
881 if (my_smile_xbm
.Ok())
883 int x
= 300, y
= 1800;
885 dc
.DrawText( _T("XBM bitmap"), x
, y
);
886 dc
.DrawText( _T("(green on red)"), x
, y
+ 15 );
887 dc
.SetTextForeground( _T("GREEN") );
888 dc
.SetTextBackground( _T("RED") );
889 dc
.DrawBitmap( my_smile_xbm
, x
, y
+ 30 );
891 dc
.SetTextForeground( *wxBLACK
);
892 dc
.DrawText( _T("After wxImage conversion"), x
+ 120, y
);
893 dc
.DrawText( _T("(red on white)"), x
+ 120, y
+ 15 );
894 dc
.SetTextForeground( wxT("RED") );
895 wxImage i
= my_smile_xbm
.ConvertToImage();
896 i
.SetMaskColour( 255, 255, 255 );
898 wxRED_PEN
->GetColour().Red(),
899 wxRED_PEN
->GetColour().Green(),
900 wxRED_PEN
->GetColour().Blue() );
901 dc
.DrawBitmap( wxBitmap(i
), x
+ 120, y
+ 30, true );
902 dc
.SetTextForeground( *wxBLACK
);
906 wxBitmap
mono( 60,50,1 );
908 memdc
.SelectObject( mono
);
909 memdc
.SetPen( *wxBLACK_PEN
);
910 memdc
.SetBrush( *wxWHITE_BRUSH
);
911 memdc
.DrawRectangle( 0,0,60,50 );
912 memdc
.SetTextForeground( *wxBLACK
);
914 // I cannot convince GTK2 to draw into mono bitmaps
915 memdc
.DrawText( _T("Hi!"), 5, 5 );
917 memdc
.SetBrush( *wxBLACK_BRUSH
);
918 memdc
.DrawRectangle( 33,5,20,20 );
919 memdc
.SetPen( *wxRED_PEN
);
920 memdc
.DrawLine( 5, 42, 50, 42 );
921 memdc
.SelectObject( wxNullBitmap
);
925 int x
= 300, y
= 1900;
927 dc
.DrawText( _T("Mono bitmap"), x
, y
);
928 dc
.DrawText( _T("(red on green)"), x
, y
+ 15 );
929 dc
.SetTextForeground( wxT("RED") );
930 dc
.SetTextBackground( wxT("GREEN") );
931 dc
.DrawBitmap( mono
, x
, y
+ 30 );
933 dc
.SetTextForeground( *wxBLACK
);
934 dc
.DrawText( _T("After wxImage conversion"), x
+ 120, y
);
935 dc
.DrawText( _T("(red on white)"), x
+ 120, y
+ 15 );
936 dc
.SetTextForeground( wxT("RED") );
937 wxImage i
= mono
.ConvertToImage();
938 i
.SetMaskColour( 255,255,255 );
940 wxRED_PEN
->GetColour().Red(),
941 wxRED_PEN
->GetColour().Green(),
942 wxRED_PEN
->GetColour().Blue() );
943 dc
.DrawBitmap( wxBitmap(i
), x
+ 120, y
+ 30, true );
944 dc
.SetTextForeground( *wxBLACK
);
947 // For testing transparency
948 dc
.SetBrush( *wxRED_BRUSH
);
949 dc
.DrawRectangle( 20, 2220, 560, 68 );
951 dc
.DrawText(_T("XPM bitmap"), 30, 2230 );
952 if ( m_bmpSmileXpm
.Ok() )
953 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, true);
955 dc
.DrawText(_T("XPM icon"), 110, 2230 );
956 if ( m_iconSmileXpm
.Ok() )
957 dc
.DrawIcon(m_iconSmileXpm
, 110, 2250);
959 // testing icon -> bitmap conversion
960 wxBitmap
to_blit( m_iconSmileXpm
);
963 dc
.DrawText( _T("SubBitmap"), 170, 2230 );
964 wxBitmap sub
= to_blit
.GetSubBitmap( wxRect(0,0,15,15) );
966 dc
.DrawBitmap( sub
, 170, 2250, true );
968 dc
.DrawText( _T("Enlarged"), 250, 2230 );
969 dc
.SetUserScale( 1.5, 1.5 );
970 dc
.DrawBitmap( to_blit
, (int)(250/1.5), (int)(2250/1.5), true );
971 dc
.SetUserScale( 2, 2 );
972 dc
.DrawBitmap( to_blit
, (int)(300/2), (int)(2250/2), true );
973 dc
.SetUserScale( 1.0, 1.0 );
975 dc
.DrawText( _T("Blit"), 400, 2230);
977 blit_dc
.SelectObject( to_blit
);
978 dc
.Blit( 400, 2250, to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
979 dc
.SetUserScale( 1.5, 1.5 );
980 dc
.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
981 dc
.SetUserScale( 2, 2 );
982 dc
.Blit( (int)(500/2), (int)(2250/2), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
983 dc
.SetUserScale( 1.0, 1.0 );
986 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
987 if (my_horse_ico32
.Ok())
988 dc
.DrawBitmap( my_horse_ico32
, 30, 2330, true );
990 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
991 if (my_horse_ico16
.Ok())
992 dc
.DrawBitmap( my_horse_ico16
, 230, 2330, true );
994 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
995 if (my_horse_ico
.Ok())
996 dc
.DrawBitmap( my_horse_ico
, 430, 2330, true );
998 dc
.DrawText( _T("CUR handler"), 30, 2390 );
999 if (my_horse_cur
.Ok())
1001 dc
.DrawBitmap( my_horse_cur
, 30, 2420, true );
1002 dc
.SetPen (*wxRED_PEN
);
1003 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
1004 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
1007 dc
.DrawText( _T("ANI handler"), 230, 2390 );
1008 for ( int i
=0; i
< m_ani_images
; i
++ )
1010 if (my_horse_ani
[i
].Ok())
1012 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, true );
1017 void MyCanvas
::CreateAntiAliasedBitmap()
1019 wxBitmap
bitmap( 300, 300 );
1023 dc
.SelectObject( bitmap
);
1027 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
1028 dc
.SetTextForeground( wxT("RED") );
1029 dc
.DrawText( _T("This is anti-aliased Text."), 20, 5 );
1030 dc
.DrawText( _T("And a Rectangle."), 20, 45 );
1032 dc
.SetBrush( *wxRED_BRUSH
);
1033 dc
.SetPen( *wxTRANSPARENT_PEN
);
1034 dc
.DrawRoundedRectangle( 20, 85, 200, 180, 20 );
1036 wxImage original
= bitmap
.ConvertToImage();
1037 wxImage
anti( 150, 150 );
1039 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
1041 for (int y
= 1; y
< 149; y
++)
1042 for (int x
= 1; x
< 149; x
++)
1044 int red
= original
.GetRed( x
*2, y
*2 ) +
1045 original
.GetRed( x
*2-1, y
*2 ) +
1046 original
.GetRed( x
*2, y
*2+1 ) +
1047 original
.GetRed( x
*2+1, y
*2+1 );
1050 int green
= original
.GetGreen( x
*2, y
*2 ) +
1051 original
.GetGreen( x
*2-1, y
*2 ) +
1052 original
.GetGreen( x
*2, y
*2+1 ) +
1053 original
.GetGreen( x
*2+1, y
*2+1 );
1056 int blue
= original
.GetBlue( x
*2, y
*2 ) +
1057 original
.GetBlue( x
*2-1, y
*2 ) +
1058 original
.GetBlue( x
*2, y
*2+1 ) +
1059 original
.GetBlue( x
*2+1, y
*2+1 );
1061 anti
.SetRGB( x
, y
, (unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
1063 my_anti
= wxBitmap(anti
);
1070 ID_QUIT
= wxID_EXIT
,
1071 ID_ABOUT
= wxID_ABOUT
,
1076 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
1078 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
1079 EVT_MENU (ID_ABOUT
, MyFrame
::OnAbout
)
1080 EVT_MENU (ID_QUIT
, MyFrame
::OnQuit
)
1081 EVT_MENU (ID_NEW
, MyFrame
::OnNewFrame
)
1082 #ifdef wxHAVE_RAW_BITMAP
1083 EVT_MENU (ID_SHOWRAW
, MyFrame
::OnTestRawBitmap
)
1087 EVT_MENU(wxID_COPY
, MyFrame
::OnCopy
)
1088 EVT_MENU(wxID_PASTE
, MyFrame
::OnPaste
)
1089 #endif // wxUSE_CLIPBOARD
1093 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxImage sample"),
1094 wxPoint(20, 20), wxSize(950, 700) )
1096 wxMenuBar
*menu_bar
= new wxMenuBar();
1098 wxMenu
*menuImage
= new wxMenu
;
1099 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
1101 #ifdef wxHAVE_RAW_BITMAP
1102 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
1104 menuImage
->AppendSeparator();
1105 menuImage
->Append( ID_ABOUT
, _T("&About..."));
1106 menuImage
->AppendSeparator();
1107 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
1108 menu_bar
->Append(menuImage
, _T("&Image"));
1111 wxMenu
*menuClipboard
= new wxMenu
;
1112 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
1113 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
1114 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
1115 #endif // wxUSE_CLIPBOARD
1117 SetMenuBar( menu_bar
);
1121 int widths
[] = { -1, 100 };
1122 SetStatusWidths( 2, widths
);
1123 #endif // wxUSE_STATUSBAR
1125 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
1127 // 500 width * 2750 height
1128 m_canvas
->SetScrollbars( 10, 10, 50, 275 );
1131 void MyFrame
::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
1136 void MyFrame
::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
1138 (void)wxMessageBox( _T("wxImage demo\n")
1139 _T("Robert Roebling (c) 1998,2000"),
1140 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
1143 void MyFrame
::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
1146 wxString filename
= wxFileSelector(_T("Select image file"));
1151 if ( !image
.LoadFile(filename
) )
1153 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
1158 (new MyImageFrame(this, filename
, wxBitmap(image
)))->Show();
1159 #endif // wxUSE_FILEDLG
1162 #ifdef wxHAVE_RAW_BITMAP
1164 void MyFrame
::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
1166 (new MyRawBitmapFrame(this))->Show();
1169 #endif // wxHAVE_RAW_BITMAP
1173 void MyFrame
::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1175 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
1176 dobjBmp
->SetBitmap(m_canvas
->my_horse_png
);
1178 wxTheClipboard
->Open();
1180 if ( !wxTheClipboard
->SetData(dobjBmp
) )
1182 wxLogError(_T("Failed to copy bitmap to clipboard"));
1185 wxTheClipboard
->Close();
1188 void MyFrame
::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1190 wxBitmapDataObject dobjBmp
;
1192 wxTheClipboard
->Open();
1193 if ( !wxTheClipboard
->GetData(dobjBmp
) )
1195 wxLogMessage(_T("No bitmap data in the clipboard"));
1199 (new MyImageFrame(this, _T("Clipboard"), dobjBmp
.GetBitmap()))->Show();
1201 wxTheClipboard
->Close();
1204 #endif // wxUSE_CLIPBOARD
1206 //-----------------------------------------------------------------------------
1208 //-----------------------------------------------------------------------------
1210 bool MyApp
::OnInit()
1212 if ( !wxApp
::OnInit() )
1215 wxInitAllImageHandlers();
1217 wxFrame
*frame
= new MyFrame();
1218 frame
->Show( true );