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
;
87 wxBitmap
*colorized_horse_jpeg
;
90 wxBitmap my_toucan_flipped_horiz
;
91 wxBitmap my_toucan_flipped_vert
;
92 wxBitmap my_toucan_flipped_both
;
93 wxBitmap my_toucan_head
;
99 wxBitmap m_bmpSmileXpm
;
100 wxIcon m_iconSmileXpm
;
103 DECLARE_DYNAMIC_CLASS(MyCanvas
)
104 DECLARE_EVENT_TABLE()
111 class MyFrame
: public wxFrame
116 void OnAbout( wxCommandEvent
&event
);
117 void OnNewFrame( wxCommandEvent
&event
);
118 #ifdef wxHAVE_RAW_BITMAP
119 void OnTestRawBitmap( wxCommandEvent
&event
);
120 #endif // wxHAVE_RAW_BITMAP
121 void OnQuit( wxCommandEvent
&event
);
124 void OnCopy(wxCommandEvent
& event
);
125 void OnPaste(wxCommandEvent
& event
);
126 #endif // wxUSE_CLIPBOARD
131 DECLARE_DYNAMIC_CLASS(MyFrame
)
132 DECLARE_EVENT_TABLE()
135 class MyImageFrame
: public wxFrame
138 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
139 : wxFrame(parent
, wxID_ANY
, _T("Double click to save"),
140 wxDefaultPosition
, wxDefaultSize
,
141 wxCAPTION
| wxSYSTEM_MENU
| wxCLOSE_BOX
),
144 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
147 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
149 // do nothing here to be able to see how transparent images are shown
152 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
154 wxPaintDC
dc( this );
155 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
158 void OnSave(wxMouseEvent
& WXUNUSED(event
))
161 wxImage image
= m_bitmap
.ConvertToImage();
163 wxString savefilename
= wxFileSelector( wxT("Save Image"),
166 (const wxChar
*)NULL
,
167 wxT("BMP files (*.bmp)|*.bmp|")
168 wxT("PNG files (*.png)|*.png|")
169 wxT("JPEG files (*.jpg)|*.jpg|")
170 wxT("GIF files (*.gif)|*.gif|")
171 wxT("TIFF files (*.tif)|*.tif|")
172 wxT("PCX files (*.pcx)|*.pcx|")
173 wxT("ICO files (*.ico)|*.ico|")
174 wxT("CUR files (*.cur)|*.cur"),
178 if ( savefilename
.empty() )
182 wxFileName::SplitPath(savefilename
, NULL
, NULL
, &extension
);
185 if ( extension
== _T("bpp") )
187 static const int bppvalues
[] =
199 const wxString bppchoices
[] =
205 _T("8 bpp greyscale"),
207 _T("8 bpp own palette"),
211 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
212 _T("Image sample: save file"),
213 WXSIZEOF(bppchoices
),
216 if ( bppselection
!= -1 )
218 int format
= bppvalues
[bppselection
];
219 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, format
);
221 if ( format
== wxBMP_8BPP_PALETTE
)
223 unsigned char *cmap
= new unsigned char [256];
224 for ( int i
= 0; i
< 256; i
++ )
225 cmap
[i
] = (unsigned char)i
;
226 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
232 else if ( extension
== _T("png") )
234 static const int pngvalues
[] =
244 const wxString pngchoices
[] =
251 _T("Grey red 16bpp"),
254 int sel
= wxGetSingleChoiceIndex(_T("Set PNG format"),
255 _T("Image sample: save file"),
256 WXSIZEOF(pngchoices
),
261 image
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, pngvalues
[sel
]);
262 image
.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH
, sel
% 2 ? 16 : 8);
265 else if ( extension
== _T("cur") )
267 image
.Rescale(32,32);
268 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
269 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
270 // This shows how you can save an image with explicitly
271 // specified image format:
272 saved
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
277 // This one guesses image format from filename extension
278 // (it may fail if the extension is not recognized):
279 image
.SaveFile(savefilename
);
281 #endif // wxUSE_FILEDLG
287 DECLARE_EVENT_TABLE()
290 #ifdef wxHAVE_RAW_BITMAP
292 #include "wx/rawbmp.h"
294 class MyRawBitmapFrame
: public wxFrame
301 REAL_SIZE
= SIZE
- 2*BORDER
304 MyRawBitmapFrame(wxFrame
*parent
)
305 : wxFrame(parent
, wxID_ANY
, _T("Raw bitmaps (how exciting)")),
306 m_bitmap(SIZE
, SIZE
, 32)
308 SetClientSize(SIZE
, SIZE
);
310 // another possibility: wxNativePixelData (don't forget to remove code
311 // setting alpha in the loop below then)
312 typedef wxAlphaPixelData Data
;
313 // typedef wxNativePixelData Data;
315 // First, clear the whole bitmap by making it alpha
317 Data
data( m_bitmap
, wxPoint(0,0), wxSize(SIZE
, SIZE
) );
320 wxLogError(_T("Failed to gain raw access to bitmap data"));
324 Data::Iterator
p(data
);
325 for ( int y
= 0; y
< SIZE
; ++y
)
327 Data::Iterator rowStart
= p
;
328 for ( int x
= 0; x
< SIZE
; ++x
)
331 ++p
; // same as p.OffsetX(1)
338 // Then, draw colourful alpha-blended stripes
339 Data
data(m_bitmap
, wxPoint(BORDER
, BORDER
) , wxSize(REAL_SIZE
, REAL_SIZE
));
342 wxLogError(_T("Failed to gain raw access to bitmap data"));
348 Data::Iterator
p(data
);
350 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
352 Data::Iterator rowStart
= p
;
354 int r
= y
< REAL_SIZE
/3 ? 255 : 0,
355 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ? 255 : 0,
356 b
= 2*(REAL_SIZE
/3) <= y
? 255 : 0;
358 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
360 // note that RGB must be premultiplied by alpha
361 unsigned a
= (Data::Iterator::ChannelType
)((x
*255.)/REAL_SIZE
);
362 p
.Red() = r
* a
/ 256;
363 p
.Green() = g
* a
/ 256;
364 p
.Blue() = b
* a
/ 256;
367 ++p
; // same as p.OffsetX(1)
375 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
377 wxPaintDC
dc( this );
378 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER
);
379 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
380 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
381 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
387 DECLARE_EVENT_TABLE()
390 #endif // wxHAVE_RAW_BITMAP
394 class MyApp
: public wxApp
397 virtual bool OnInit();
406 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
408 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
409 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
410 EVT_PAINT(MyImageFrame::OnPaint
)
411 EVT_LEFT_DCLICK(MyImageFrame::OnSave
)
414 #ifdef wxHAVE_RAW_BITMAP
416 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
417 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
420 #endif // wxHAVE_RAW_BITMAP
422 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
423 EVT_PAINT(MyCanvas::OnPaint
)
426 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
427 const wxPoint
&pos
, const wxSize
&size
)
428 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
429 , m_bmpSmileXpm((const char **) smile_xpm
)
430 , m_iconSmileXpm((const char **) smile_xpm
)
432 my_horse_png
= (wxBitmap
*) NULL
;
433 my_horse_jpeg
= (wxBitmap
*) NULL
;
434 my_horse_gif
= (wxBitmap
*) NULL
;
435 my_horse_bmp
= (wxBitmap
*) NULL
;
436 my_horse_bmp2
= (wxBitmap
*) NULL
;
437 my_horse_pcx
= (wxBitmap
*) NULL
;
438 my_horse_pnm
= (wxBitmap
*) NULL
;
439 my_horse_tiff
= (wxBitmap
*) NULL
;
440 my_horse_xpm
= (wxBitmap
*) NULL
;
441 my_horse_ico32
= (wxBitmap
*) NULL
;
442 my_horse_ico16
= (wxBitmap
*) NULL
;
443 my_horse_ico
= (wxBitmap
*) NULL
;
444 my_horse_cur
= (wxBitmap
*) NULL
;
445 my_horse_ani
= (wxBitmap
*) NULL
;
446 colorized_horse_jpeg
= (wxBitmap
*) NULL
;
448 my_smile_xbm
= (wxBitmap
*) NULL
;
449 my_square
= (wxBitmap
*) NULL
;
450 my_anti
= (wxBitmap
*) NULL
;
452 my_horse_asciigrey_pnm
= (wxBitmap
*) NULL
;
453 my_horse_rawgrey_pnm
= (wxBitmap
*) NULL
;
457 SetBackgroundColour(* wxWHITE
);
459 wxBitmap
bitmap( 100, 100 );
462 dc
.SelectObject( bitmap
);
463 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
464 dc
.SetPen( *wxBLACK_PEN
);
465 dc
.DrawRectangle( 0, 0, 100, 100 );
466 dc
.SetBrush( *wxWHITE_BRUSH
);
467 dc
.DrawRectangle( 20, 20, 60, 60 );
468 dc
.SelectObject( wxNullBitmap
);
470 // try to find the directory with our images
472 if ( wxFile::Exists(wxT("./horse.png")) )
474 else if ( wxFile::Exists(wxT("../horse.png")) )
477 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
479 wxImage image
= bitmap
.ConvertToImage();
482 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
483 wxLogError(wxT("Can't save file"));
487 if ( image
.LoadFile( dir
+ _T("test.png") ) )
488 my_square
= new wxBitmap( image
);
492 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
493 wxLogError(wxT("Can't load PNG image"));
495 my_horse_png
= new wxBitmap( image
);
497 image
= wxImage(wxT("toucan.png"));
498 my_toucan
= wxBitmap(image
);
499 my_toucan_flipped_horiz
= wxBitmap(image
.Mirror(true));
500 my_toucan_flipped_vert
= wxBitmap(image
.Mirror(false));
501 my_toucan_flipped_both
= wxBitmap(image
.Mirror(true).Mirror(false));
502 my_toucan_head
= wxBitmap(image
.GetSubImage(wxRect(40, 7, 80, 60)));
504 #endif // wxUSE_LIBPNG
509 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
510 wxLogError(wxT("Can't load JPG image"));
513 my_horse_jpeg
= new wxBitmap( image
);
514 // Colorize by rotating green hue to red
515 wxImage::HSVValue greenHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0));
516 wxImage::HSVValue redHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0));
517 image
.RotateHue(redHSV
.hue
- greenHSV
.hue
);
518 colorized_horse_jpeg
= new wxBitmap( image
);
520 #endif // wxUSE_LIBJPEG
525 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
526 wxLogError(wxT("Can't load GIF image"));
528 my_horse_gif
= new wxBitmap( image
);
534 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
535 wxLogError(wxT("Can't load PCX image"));
537 my_horse_pcx
= new wxBitmap( image
);
542 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
543 wxLogError(wxT("Can't load BMP image"));
545 my_horse_bmp
= new wxBitmap( image
);
550 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
551 wxLogError(wxT("Can't load XPM image"));
553 my_horse_xpm
= new wxBitmap( image
);
555 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
556 wxLogError(wxT("Can't save file"));
562 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
563 wxLogError(wxT("Can't load PNM image"));
565 my_horse_pnm
= new wxBitmap( image
);
569 if ( !image
.LoadFile( dir
+ _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM
) )
570 wxLogError(wxT("Can't load PNM image"));
572 my_horse_asciigrey_pnm
= new wxBitmap( image
);
576 if ( !image
.LoadFile( dir
+ _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM
) )
577 wxLogError(wxT("Can't load PNM image"));
579 my_horse_rawgrey_pnm
= new wxBitmap( image
);
585 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
586 wxLogError(wxT("Can't load TIFF image"));
588 my_horse_tiff
= new wxBitmap( image
);
591 CreateAntiAliasedBitmap();
593 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
596 // demonstrates XPM automatically using the mask when saving
597 if ( m_bmpSmileXpm
.Ok() )
598 m_bmpSmileXpm
.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM
);
603 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
604 wxLogError(wxT("Can't load first ICO image"));
606 my_horse_ico32
= new wxBitmap( image
);
610 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
611 wxLogError(wxT("Can't load second ICO image"));
613 my_horse_ico16
= new wxBitmap( image
);
617 if ( !image
.LoadFile( dir
+ _T("horse.ico") ) )
618 wxLogError(wxT("Can't load best ICO image"));
620 my_horse_ico
= new wxBitmap( image
);
624 if ( !image
.LoadFile( dir
+ _T("horse.cur"), wxBITMAP_TYPE_CUR
) )
625 wxLogError(wxT("Can't load best ICO image"));
628 my_horse_cur
= new wxBitmap( image
);
629 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
630 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
633 m_ani_images
= wxImage::GetImageCount ( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
);
635 wxLogError(wxT("No ANI-format images found"));
637 my_horse_ani
= new wxBitmap
[m_ani_images
];
639 for (i
=0; i
< m_ani_images
; i
++)
642 if (!image
.LoadFile( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
, i
))
644 wxString tmp
= wxT("Can't load image number ");
649 my_horse_ani
[i
] = wxBitmap( image
);
651 #endif // wxUSE_ICO_CUR
655 // test image loading from stream
656 wxFile
file(dir
+ _T("horse.bmp"));
657 if ( file
.IsOpened() )
659 wxFileOffset len
= file
.Length();
660 size_t dataSize
= (size_t)len
;
661 void *data
= malloc(dataSize
);
662 if ( file
.Read(data
, dataSize
) != len
)
663 wxLogError(_T("Reading bitmap file failed"));
666 wxMemoryInputStream
mis(data
, dataSize
);
667 if ( !image
.LoadFile(mis
) )
668 wxLogError(wxT("Can't load BMP image from stream"));
670 my_horse_bmp2
= new wxBitmap( image
);
677 MyCanvas::~MyCanvas()
681 delete my_horse_jpeg
;
684 delete my_horse_bmp2
;
686 delete my_horse_tiff
;
688 delete my_horse_ico32
;
689 delete my_horse_ico16
;
692 delete [] my_horse_ani
;
696 delete my_horse_asciigrey_pnm
;
697 delete my_horse_rawgrey_pnm
;
698 delete colorized_horse_jpeg
;
701 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
703 wxPaintDC
dc( this );
706 dc
.DrawText( _T("Loaded image"), 30, 10 );
707 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
709 dc
.DrawText( _T("Drawn directly"), 150, 10 );
710 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
711 dc
.SetPen( *wxBLACK_PEN
);
712 dc
.DrawRectangle( 150, 30, 100, 100 );
713 dc
.SetBrush( *wxWHITE_BRUSH
);
714 dc
.DrawRectangle( 170, 50, 60, 60 );
716 if (my_anti
&& my_anti
->Ok())
717 dc
.DrawBitmap( *my_anti
, 280, 30 );
719 dc
.DrawText( _T("PNG handler"), 30, 135 );
720 if (my_horse_png
&& my_horse_png
->Ok())
722 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
723 wxRect
rect(0,0,100,100);
724 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
725 dc
.DrawText( _T("GetSubBitmap()"), 280, 175 );
726 dc
.DrawBitmap( sub
, 280, 195 );
729 dc
.DrawText( _T("JPEG handler"), 30, 365 );
730 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
731 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
733 dc
.DrawText( _T("GIF handler"), 30, 595 );
734 if (my_horse_gif
&& my_horse_gif
->Ok())
735 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
737 dc
.DrawText( _T("PCX handler"), 30, 825 );
738 if (my_horse_pcx
&& my_horse_pcx
->Ok())
739 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
741 dc
.DrawText( _T("BMP handler"), 30, 1055 );
742 if (my_horse_bmp
&& my_horse_bmp
->Ok())
743 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
745 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
746 if (my_horse_bmp2
&& my_horse_bmp2
->Ok())
747 dc
.DrawBitmap( *my_horse_bmp2
, 280, 1070 );
749 dc
.DrawText( _T("PNM handler"), 30, 1285 );
750 if (my_horse_pnm
&& my_horse_pnm
->Ok())
751 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
753 dc
.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 );
754 if (my_horse_asciigrey_pnm
&& my_horse_asciigrey_pnm
->Ok())
755 dc
.DrawBitmap( *my_horse_asciigrey_pnm
, 280, 1300 );
757 dc
.DrawText( _T("PNM handler (raw grey)"), 530, 1285 );
758 if (my_horse_rawgrey_pnm
&& my_horse_rawgrey_pnm
->Ok())
759 dc
.DrawBitmap( *my_horse_rawgrey_pnm
, 530, 1300 );
761 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
762 if (my_horse_tiff
&& my_horse_tiff
->Ok())
763 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
765 dc
.DrawText( _T("XPM handler"), 30, 1745 );
766 if (my_horse_xpm
&& my_horse_xpm
->Ok())
767 dc
.DrawBitmap( *my_horse_xpm
, 30, 1760 );
770 int x
= 200, y
= 300, yy
= 170;;
772 dc
.DrawText(wxT("Original toucan"), x
+50, y
);
773 dc
.DrawBitmap(my_toucan
, x
, y
+15);
775 dc
.DrawText(wxT("Flipped horizontally"), x
+50, y
);
776 dc
.DrawBitmap(my_toucan_flipped_horiz
, x
, y
+15);
778 dc
.DrawText(wxT("Flipped vertically"), x
+50, y
);
779 dc
.DrawBitmap(my_toucan_flipped_vert
, x
, y
+15);
781 dc
.DrawText(wxT("Flipped both h&v"), x
+50, y
);
782 dc
.DrawBitmap(my_toucan_flipped_both
, x
, y
+15);
785 dc
.DrawText(wxT("Toucan's head"), x
+50, y
);
786 dc
.DrawBitmap(my_toucan_head
, x
, y
+15);
789 if (my_smile_xbm
&& my_smile_xbm
->Ok())
791 dc
.DrawText( _T("XBM bitmap"), 30, 1975 );
792 dc
.DrawText( _T("(green on red)"), 30, 1990 );
793 dc
.SetTextForeground( _T("GREEN") );
794 dc
.SetTextBackground( _T("RED") );
795 dc
.DrawBitmap( *my_smile_xbm
, 30, 2010 );
797 dc
.SetTextForeground( wxT("BLACK") );
798 dc
.DrawText( _T("After wxImage conversion"), 150, 1975 );
799 dc
.DrawText( _T("(red on white)"), 150, 1990 );
800 dc
.SetTextForeground( wxT("RED") );
801 wxImage i
= my_smile_xbm
->ConvertToImage();
802 i
.SetMaskColour( 255, 255, 255 );
804 wxRED_PEN
->GetColour().Red(),
805 wxRED_PEN
->GetColour().Green(),
806 wxRED_PEN
->GetColour().Blue() );
807 dc
.DrawBitmap( wxBitmap(i
), 150, 2010, true );
808 dc
.SetTextForeground( wxT("BLACK") );
812 wxBitmap
mono( 60,50,1 );
814 memdc
.SelectObject( mono
);
815 memdc
.SetPen( *wxBLACK_PEN
);
816 memdc
.SetBrush( *wxWHITE_BRUSH
);
817 memdc
.DrawRectangle( 0,0,60,50 );
818 memdc
.SetTextForeground( *wxBLACK
);
820 // I cannot convince GTK2 to draw into mono bitmaps
821 memdc
.DrawText( _T("Hi!"), 5, 5 );
823 memdc
.SetBrush( *wxBLACK_BRUSH
);
824 memdc
.DrawRectangle( 33,5,20,20 );
825 memdc
.SetPen( *wxRED_PEN
);
826 memdc
.DrawLine( 5, 42, 50, 42 );
827 memdc
.SelectObject( wxNullBitmap
);
831 dc
.DrawText( _T("Mono bitmap"), 30, 2095 );
832 dc
.DrawText( _T("(red on green)"), 30, 2110 );
833 dc
.SetTextForeground( wxT("RED") );
834 dc
.SetTextBackground( wxT("GREEN") );
835 dc
.DrawBitmap( mono
, 30, 2130 );
837 dc
.SetTextForeground( wxT("BLACK") );
838 dc
.DrawText( _T("After wxImage conversion"), 150, 2095 );
839 dc
.DrawText( _T("(red on white)"), 150, 2110 );
840 dc
.SetTextForeground( wxT("RED") );
841 wxImage i
= mono
.ConvertToImage();
842 i
.SetMaskColour( 255,255,255 );
844 wxRED_PEN
->GetColour().Red(),
845 wxRED_PEN
->GetColour().Green(),
846 wxRED_PEN
->GetColour().Blue() );
847 dc
.DrawBitmap( wxBitmap(i
), 150, 2130, true );
848 dc
.SetTextForeground( wxT("BLACK") );
851 // For testing transparency
852 dc
.SetBrush( *wxRED_BRUSH
);
853 dc
.DrawRectangle( 20, 2220, 560, 68 );
855 dc
.DrawText(_T("XPM bitmap"), 30, 2230 );
856 if ( m_bmpSmileXpm
.Ok() )
857 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, true);
859 dc
.DrawText(_T("XPM icon"), 110, 2230 );
860 if ( m_iconSmileXpm
.Ok() )
861 dc
.DrawIcon(m_iconSmileXpm
, 110, 2250);
863 // testing icon -> bitmap conversion
864 wxBitmap
to_blit( m_iconSmileXpm
);
867 dc
.DrawText( _T("SubBitmap"), 170, 2230 );
868 wxBitmap sub
= to_blit
.GetSubBitmap( wxRect(0,0,15,15) );
870 dc
.DrawBitmap( sub
, 170, 2250, true );
872 dc
.DrawText( _T("Enlarged"), 250, 2230 );
873 dc
.SetUserScale( 1.5, 1.5 );
874 dc
.DrawBitmap( to_blit
, (int)(250/1.5), (int)(2250/1.5), true );
875 dc
.SetUserScale( 2, 2 );
876 dc
.DrawBitmap( to_blit
, (int)(300/2), (int)(2250/2), true );
877 dc
.SetUserScale( 1.0, 1.0 );
879 dc
.DrawText( _T("Blit"), 400, 2230);
881 blit_dc
.SelectObject( to_blit
);
882 dc
.Blit( 400, 2250, to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
883 dc
.SetUserScale( 1.5, 1.5 );
884 dc
.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
885 dc
.SetUserScale( 2, 2 );
886 dc
.Blit( (int)(500/2), (int)(2250/2), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
887 dc
.SetUserScale( 1.0, 1.0 );
890 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
891 if (my_horse_ico32
&& my_horse_ico32
->Ok())
892 dc
.DrawBitmap( *my_horse_ico32
, 30, 2330, true );
894 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
895 if (my_horse_ico16
&& my_horse_ico16
->Ok())
896 dc
.DrawBitmap( *my_horse_ico16
, 230, 2330, true );
898 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
899 if (my_horse_ico
&& my_horse_ico
->Ok())
900 dc
.DrawBitmap( *my_horse_ico
, 430, 2330, true );
902 dc
.DrawText( _T("CUR handler"), 30, 2390 );
903 if (my_horse_cur
&& my_horse_cur
->Ok())
905 dc
.DrawBitmap( *my_horse_cur
, 30, 2420, true );
906 dc
.SetPen (*wxRED_PEN
);
907 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
908 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
910 dc
.DrawText( _T("ANI handler"), 230, 2390 );
912 for (i
=0; i
< m_ani_images
; i
++)
913 if (my_horse_ani
[i
].Ok())
915 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, true );
918 if (colorized_horse_jpeg
)
920 dc
.DrawText( _T("Colorize image by rotating green hue to red"), 30, 2490 );
921 dc
.DrawBitmap( *colorized_horse_jpeg
, 30, 2520 );
923 #endif // wxUSE_LIBJPEG
927 void MyCanvas::CreateAntiAliasedBitmap()
929 wxBitmap
bitmap( 300, 300 );
933 dc
.SelectObject( bitmap
);
937 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
938 dc
.SetTextForeground( wxT("RED") );
939 dc
.DrawText( _T("This is anti-aliased Text."), 20, 5 );
940 dc
.DrawText( _T("And a Rectangle."), 20, 45 );
942 dc
.SetBrush( *wxRED_BRUSH
);
943 dc
.SetPen( *wxTRANSPARENT_PEN
);
944 dc
.DrawRoundedRectangle( 20, 85, 200, 180, 20 );
946 wxImage original
= bitmap
.ConvertToImage();
947 wxImage
anti( 150, 150 );
949 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
951 for (int y
= 1; y
< 149; y
++)
952 for (int x
= 1; x
< 149; x
++)
954 int red
= original
.GetRed( x
*2, y
*2 ) +
955 original
.GetRed( x
*2-1, y
*2 ) +
956 original
.GetRed( x
*2, y
*2+1 ) +
957 original
.GetRed( x
*2+1, y
*2+1 );
960 int green
= original
.GetGreen( x
*2, y
*2 ) +
961 original
.GetGreen( x
*2-1, y
*2 ) +
962 original
.GetGreen( x
*2, y
*2+1 ) +
963 original
.GetGreen( x
*2+1, y
*2+1 );
966 int blue
= original
.GetBlue( x
*2, y
*2 ) +
967 original
.GetBlue( x
*2-1, y
*2 ) +
968 original
.GetBlue( x
*2, y
*2+1 ) +
969 original
.GetBlue( x
*2+1, y
*2+1 );
971 anti
.SetRGB( x
, y
, (unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
973 my_anti
= new wxBitmap(anti
);
981 ID_ABOUT
= wxID_ABOUT
,
986 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
988 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
989 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
990 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
991 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
992 #ifdef wxHAVE_RAW_BITMAP
993 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
997 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
998 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
999 #endif // wxUSE_CLIPBOARD
1003 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxImage sample"),
1004 wxPoint(20,20), wxSize(470,360) )
1006 wxMenuBar
*menu_bar
= new wxMenuBar();
1008 wxMenu
*menuImage
= new wxMenu
;
1009 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
1011 #ifdef wxHAVE_RAW_BITMAP
1012 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
1014 menuImage
->AppendSeparator();
1015 menuImage
->Append( ID_ABOUT
, _T("&About..."));
1016 menuImage
->AppendSeparator();
1017 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
1018 menu_bar
->Append(menuImage
, _T("&Image"));
1021 wxMenu
*menuClipboard
= new wxMenu
;
1022 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
1023 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
1024 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
1025 #endif // wxUSE_CLIPBOARD
1027 SetMenuBar( menu_bar
);
1031 int widths
[] = { -1, 100 };
1032 SetStatusWidths( 2, widths
);
1033 #endif // wxUSE_STATUSBAR
1035 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
1037 // 500 width * 2750 height
1038 m_canvas
->SetScrollbars( 10, 10, 50, 275 );
1041 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
1046 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
1048 (void)wxMessageBox( _T("wxImage demo\n")
1049 _T("Robert Roebling (c) 1998,2000"),
1050 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
1053 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
1056 wxString filename
= wxFileSelector(_T("Select image file"));
1061 if ( !image
.LoadFile(filename
) )
1063 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
1068 (new MyImageFrame(this, wxBitmap(image
)))->Show();
1069 #endif // wxUSE_FILEDLG
1072 #ifdef wxHAVE_RAW_BITMAP
1074 void MyFrame::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
1076 (new MyRawBitmapFrame(this))->Show();
1079 #endif // wxHAVE_RAW_BITMAP
1083 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1085 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
1086 dobjBmp
->SetBitmap(*m_canvas
->my_horse_png
);
1088 wxTheClipboard
->Open();
1090 if ( !wxTheClipboard
->SetData(dobjBmp
) )
1092 wxLogError(_T("Failed to copy bitmap to clipboard"));
1095 wxTheClipboard
->Close();
1098 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1100 wxBitmapDataObject dobjBmp
;
1102 wxTheClipboard
->Open();
1103 if ( !wxTheClipboard
->GetData(dobjBmp
) )
1105 wxLogMessage(_T("No bitmap data in the clipboard"));
1109 (new MyImageFrame(this, dobjBmp
.GetBitmap()))->Show();
1111 wxTheClipboard
->Close();
1114 #endif // wxUSE_CLIPBOARD
1116 //-----------------------------------------------------------------------------
1118 //-----------------------------------------------------------------------------
1120 bool MyApp::OnInit()
1123 wxImage::AddHandler( new wxPNGHandler
);
1127 wxImage::AddHandler( new wxJPEGHandler
);
1131 wxImage::AddHandler( new wxTIFFHandler
);
1135 wxImage::AddHandler( new wxGIFHandler
);
1139 wxImage::AddHandler( new wxPCXHandler
);
1143 wxImage::AddHandler( new wxPNMHandler
);
1147 wxImage::AddHandler( new wxXPMHandler
);
1151 wxImage::AddHandler( new wxICOHandler
);
1152 wxImage::AddHandler( new wxCURHandler
);
1153 wxImage::AddHandler( new wxANIHandler
);
1156 wxFrame
*frame
= new MyFrame();
1157 frame
->Show( true );