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_xpm
;
73 wxBitmap my_horse_ico32
;
74 wxBitmap my_horse_ico16
;
75 wxBitmap my_horse_ico
;
76 wxBitmap my_horse_cur
;
78 wxBitmap my_smile_xbm
;
82 wxBitmap my_horse_asciigrey_pnm
;
83 wxBitmap my_horse_rawgrey_pnm
;
85 wxBitmap colorized_horse_jpeg
;
88 wxBitmap my_toucan_flipped_horiz
;
89 wxBitmap my_toucan_flipped_vert
;
90 wxBitmap my_toucan_flipped_both
;
91 wxBitmap my_toucan_head
;
95 wxBitmap
*my_horse_ani
;
98 wxBitmap m_bmpSmileXpm
;
99 wxIcon m_iconSmileXpm
;
101 DECLARE_EVENT_TABLE()
108 class MyFrame
: public wxFrame
113 void OnAbout( wxCommandEvent
&event
);
114 void OnNewFrame( wxCommandEvent
&event
);
115 #ifdef wxHAVE_RAW_BITMAP
116 void OnTestRawBitmap( wxCommandEvent
&event
);
117 #endif // wxHAVE_RAW_BITMAP
118 void OnQuit( wxCommandEvent
&event
);
121 void OnCopy(wxCommandEvent
& event
);
122 void OnPaste(wxCommandEvent
& event
);
123 #endif // wxUSE_CLIPBOARD
128 DECLARE_DYNAMIC_CLASS(MyFrame
)
129 DECLARE_EVENT_TABLE()
132 class MyImageFrame
: public wxFrame
135 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
136 : wxFrame(parent
, wxID_ANY
, _T("Double click to save"),
137 wxDefaultPosition
, wxDefaultSize
,
138 wxCAPTION
| wxSYSTEM_MENU
| wxCLOSE_BOX
),
141 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
144 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
146 // do nothing here to be able to see how transparent images are shown
149 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
151 wxPaintDC
dc( this );
152 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
155 void OnSave(wxMouseEvent
& WXUNUSED(event
))
158 wxImage image
= m_bitmap
.ConvertToImage();
160 wxString savefilename
= wxFileSelector( wxT("Save Image"),
163 (const wxChar
*)NULL
,
164 wxT("BMP files (*.bmp)|*.bmp|")
165 wxT("PNG files (*.png)|*.png|")
166 wxT("JPEG files (*.jpg)|*.jpg|")
167 wxT("GIF files (*.gif)|*.gif|")
168 wxT("TIFF files (*.tif)|*.tif|")
169 wxT("PCX files (*.pcx)|*.pcx|")
170 wxT("ICO files (*.ico)|*.ico|")
171 wxT("CUR files (*.cur)|*.cur"),
175 if ( savefilename
.empty() )
179 wxFileName
::SplitPath(savefilename
, NULL
, NULL
, &extension
);
182 if ( extension
== _T("bpp") )
184 static const int bppvalues
[] =
196 const wxString bppchoices
[] =
202 _T("8 bpp greyscale"),
204 _T("8 bpp own palette"),
208 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
209 _T("Image sample: save file"),
210 WXSIZEOF(bppchoices
),
213 if ( bppselection
!= -1 )
215 int format
= bppvalues
[bppselection
];
216 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, format
);
218 if ( format
== wxBMP_8BPP_PALETTE
)
220 unsigned char *cmap
= new unsigned char [256];
221 for ( int i
= 0; i
< 256; i
++ )
222 cmap
[i
] = (unsigned char)i
;
223 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
229 else if ( extension
== _T("png") )
231 static const int pngvalues
[] =
241 const wxString pngchoices
[] =
248 _T("Grey red 16bpp"),
251 int sel
= wxGetSingleChoiceIndex(_T("Set PNG format"),
252 _T("Image sample: save file"),
253 WXSIZEOF(pngchoices
),
258 image
.SetOption(wxIMAGE_OPTION_PNG_FORMAT
, pngvalues
[sel
]);
259 image
.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH
, sel
% 2 ?
16 : 8);
262 else if ( extension
== _T("cur") )
264 image
.Rescale(32,32);
265 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
266 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
267 // This shows how you can save an image with explicitly
268 // specified image format:
269 saved
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
274 // This one guesses image format from filename extension
275 // (it may fail if the extension is not recognized):
276 image
.SaveFile(savefilename
);
278 #endif // wxUSE_FILEDLG
284 DECLARE_EVENT_TABLE()
287 #ifdef wxHAVE_RAW_BITMAP
289 #include "wx/rawbmp.h"
291 class MyRawBitmapFrame
: public wxFrame
298 REAL_SIZE
= SIZE
- 2*BORDER
301 MyRawBitmapFrame(wxFrame
*parent
)
302 : wxFrame(parent
, wxID_ANY
, _T("Raw bitmaps (how exciting)")),
303 m_bitmap(SIZE
, SIZE
, 32)
305 SetClientSize(SIZE
, SIZE
);
307 // another possibility: wxNativePixelData (don't forget to remove code
308 // setting alpha in the loop below then)
309 typedef wxAlphaPixelData Data
;
310 // typedef wxNativePixelData Data;
312 // First, clear the whole bitmap by making it alpha
314 Data
data( m_bitmap
, wxPoint(0,0), wxSize(SIZE
, SIZE
) );
317 wxLogError(_T("Failed to gain raw access to bitmap data"));
321 Data
::Iterator
p(data
);
322 for ( int y
= 0; y
< SIZE
; ++y
)
324 Data
::Iterator rowStart
= p
;
325 for ( int x
= 0; x
< SIZE
; ++x
)
328 ++p
; // same as p.OffsetX(1)
335 // Then, draw colourful alpha-blended stripes
336 Data
data(m_bitmap
, wxPoint(BORDER
, BORDER
) , wxSize(REAL_SIZE
, REAL_SIZE
));
339 wxLogError(_T("Failed to gain raw access to bitmap data"));
345 Data
::Iterator
p(data
);
347 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
349 Data
::Iterator rowStart
= p
;
351 int r
= y
< REAL_SIZE
/3 ?
255 : 0,
352 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ?
255 : 0,
353 b
= 2*(REAL_SIZE
/3) <= y ?
255 : 0;
355 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
357 // note that RGB must be premultiplied by alpha
358 unsigned a
= (Data
::Iterator
::ChannelType
)((x
*255.)/REAL_SIZE
);
359 p
.Red() = r
* a
/ 256;
360 p
.Green() = g
* a
/ 256;
361 p
.Blue() = b
* a
/ 256;
364 ++p
; // same as p.OffsetX(1)
372 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
374 wxPaintDC
dc( this );
375 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER
);
376 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
377 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
378 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
384 DECLARE_EVENT_TABLE()
387 #endif // wxHAVE_RAW_BITMAP
391 class MyApp
: public wxApp
394 virtual bool OnInit();
403 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
404 EVT_ERASE_BACKGROUND(MyImageFrame
::OnEraseBackground
)
405 EVT_PAINT(MyImageFrame
::OnPaint
)
406 EVT_LEFT_DCLICK(MyImageFrame
::OnSave
)
409 #ifdef wxHAVE_RAW_BITMAP
411 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
412 EVT_PAINT(MyRawBitmapFrame
::OnPaint
)
415 #endif // wxHAVE_RAW_BITMAP
417 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
418 EVT_PAINT(MyCanvas
::OnPaint
)
421 MyCanvas
::MyCanvas( wxWindow
*parent
, wxWindowID id
,
422 const wxPoint
&pos
, const wxSize
&size
)
423 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
424 , m_bmpSmileXpm((const char **) smile_xpm
)
425 , m_iconSmileXpm((const char **) smile_xpm
)
430 SetBackgroundColour(* wxWHITE
);
432 wxBitmap
bitmap( 100, 100 );
435 dc
.SelectObject( bitmap
);
436 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
437 dc
.SetPen( *wxBLACK_PEN
);
438 dc
.DrawRectangle( 0, 0, 100, 100 );
439 dc
.SetBrush( *wxWHITE_BRUSH
);
440 dc
.DrawRectangle( 20, 20, 60, 60 );
441 dc
.SelectObject( wxNullBitmap
);
443 // try to find the directory with our images
445 if ( wxFile
::Exists(wxT("./horse.png")) )
447 else if ( wxFile
::Exists(wxT("../horse.png")) )
450 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
452 wxImage image
= bitmap
.ConvertToImage();
455 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
456 wxLogError(wxT("Can't save file"));
460 if ( image
.LoadFile( dir
+ _T("test.png") ) )
461 my_square
= wxBitmap( image
);
465 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
466 wxLogError(wxT("Can't load PNG image"));
468 my_horse_png
= wxBitmap( image
);
470 image
= wxImage(wxT("toucan.png"));
471 my_toucan
= wxBitmap(image
);
472 my_toucan_flipped_horiz
= wxBitmap(image
.Mirror(true));
473 my_toucan_flipped_vert
= wxBitmap(image
.Mirror(false));
474 my_toucan_flipped_both
= wxBitmap(image
.Mirror(true).Mirror(false));
475 my_toucan_head
= wxBitmap(image
.GetSubImage(wxRect(40, 7, 80, 60)));
477 #endif // wxUSE_LIBPNG
482 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
483 wxLogError(wxT("Can't load JPG image"));
486 my_horse_jpeg
= wxBitmap( image
);
487 // Colorize by rotating green hue to red
488 wxImage
::HSVValue greenHSV
= wxImage
::RGBtoHSV(wxImage
::RGBValue(0, 255, 0));
489 wxImage
::HSVValue redHSV
= wxImage
::RGBtoHSV(wxImage
::RGBValue(255, 0, 0));
490 image
.RotateHue(redHSV
.hue
- greenHSV
.hue
);
491 colorized_horse_jpeg
= wxBitmap( image
);
493 #endif // wxUSE_LIBJPEG
498 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
499 wxLogError(wxT("Can't load GIF image"));
501 my_horse_gif
= wxBitmap( image
);
507 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
508 wxLogError(wxT("Can't load PCX image"));
510 my_horse_pcx
= wxBitmap( image
);
515 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
516 wxLogError(wxT("Can't load BMP image"));
518 my_horse_bmp
= wxBitmap( image
);
523 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
524 wxLogError(wxT("Can't load XPM image"));
526 my_horse_xpm
= wxBitmap( image
);
528 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
529 wxLogError(wxT("Can't save file"));
535 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
536 wxLogError(wxT("Can't load PNM image"));
538 my_horse_pnm
= wxBitmap( image
);
542 if ( !image
.LoadFile( dir
+ _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM
) )
543 wxLogError(wxT("Can't load PNM image"));
545 my_horse_asciigrey_pnm
= wxBitmap( image
);
549 if ( !image
.LoadFile( dir
+ _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM
) )
550 wxLogError(wxT("Can't load PNM image"));
552 my_horse_rawgrey_pnm
= wxBitmap( image
);
558 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
559 wxLogError(wxT("Can't load TIFF image"));
561 my_horse_tiff
= wxBitmap( image
);
564 CreateAntiAliasedBitmap();
566 my_smile_xbm
= wxBitmap( (const char*)smile_bits
, smile_width
,
569 // demonstrates XPM automatically using the mask when saving
570 if ( m_bmpSmileXpm
.Ok() )
571 m_bmpSmileXpm
.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM
);
576 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
577 wxLogError(wxT("Can't load first ICO image"));
579 my_horse_ico32
= wxBitmap( image
);
583 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
584 wxLogError(wxT("Can't load second ICO image"));
586 my_horse_ico16
= wxBitmap( image
);
590 if ( !image
.LoadFile( dir
+ _T("horse.ico") ) )
591 wxLogError(wxT("Can't load best ICO image"));
593 my_horse_ico
= wxBitmap( image
);
597 if ( !image
.LoadFile( dir
+ _T("horse.cur"), wxBITMAP_TYPE_CUR
) )
598 wxLogError(wxT("Can't load best ICO image"));
601 my_horse_cur
= wxBitmap( image
);
602 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
603 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
606 m_ani_images
= wxImage
::GetImageCount ( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
);
608 wxLogError(wxT("No ANI-format images found"));
610 my_horse_ani
= new wxBitmap
[m_ani_images
];
612 for (i
=0; i
< m_ani_images
; i
++)
615 if (!image
.LoadFile( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
, i
))
617 wxString tmp
= wxT("Can't load image number ");
622 my_horse_ani
[i
] = wxBitmap( image
);
624 #endif // wxUSE_ICO_CUR
628 // test image loading from stream
629 wxFile
file(dir
+ _T("horse.bmp"));
630 if ( file
.IsOpened() )
632 wxFileOffset len
= file
.Length();
633 size_t dataSize
= (size_t)len
;
634 void *data
= malloc(dataSize
);
635 if ( file
.Read(data
, dataSize
) != len
)
636 wxLogError(_T("Reading bitmap file failed"));
639 wxMemoryInputStream
mis(data
, dataSize
);
640 if ( !image
.LoadFile(mis
) )
641 wxLogError(wxT("Can't load BMP image from stream"));
643 my_horse_bmp2
= wxBitmap( image
);
650 MyCanvas
::~MyCanvas()
652 delete [] my_horse_ani
;
655 void MyCanvas
::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
657 wxPaintDC
dc( this );
660 dc
.DrawText( _T("Loaded image"), 30, 10 );
662 dc
.DrawBitmap( my_square
, 30, 30 );
664 dc
.DrawText( _T("Drawn directly"), 150, 10 );
665 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
666 dc
.SetPen( *wxBLACK_PEN
);
667 dc
.DrawRectangle( 150, 30, 100, 100 );
668 dc
.SetBrush( *wxWHITE_BRUSH
);
669 dc
.DrawRectangle( 170, 50, 60, 60 );
672 dc
.DrawBitmap( my_anti
, 280, 30 );
674 dc
.DrawText( _T("PNG handler"), 30, 135 );
675 if (my_horse_png
.Ok())
677 dc
.DrawBitmap( my_horse_png
, 30, 150 );
678 wxRect
rect(0,0,100,100);
679 wxBitmap
sub( my_horse_png
.GetSubBitmap(rect
) );
680 dc
.DrawText( _T("GetSubBitmap()"), 280, 175 );
681 dc
.DrawBitmap( sub
, 280, 195 );
684 dc
.DrawText( _T("JPEG handler"), 30, 365 );
685 if (my_horse_jpeg
.Ok())
686 dc
.DrawBitmap( my_horse_jpeg
, 30, 380 );
688 dc
.DrawText( _T("GIF handler"), 30, 595 );
689 if (my_horse_gif
.Ok())
690 dc
.DrawBitmap( my_horse_gif
, 30, 610 );
692 dc
.DrawText( _T("PCX handler"), 30, 825 );
693 if (my_horse_pcx
.Ok())
694 dc
.DrawBitmap( my_horse_pcx
, 30, 840 );
696 dc
.DrawText( _T("BMP handler"), 30, 1055 );
697 if (my_horse_bmp
.Ok())
698 dc
.DrawBitmap( my_horse_bmp
, 30, 1070 );
700 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
701 if (my_horse_bmp2
.Ok())
702 dc
.DrawBitmap( my_horse_bmp2
, 280, 1070 );
704 dc
.DrawText( _T("PNM handler"), 30, 1285 );
705 if (my_horse_pnm
.Ok())
706 dc
.DrawBitmap( my_horse_pnm
, 30, 1300 );
708 dc
.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 );
709 if (my_horse_asciigrey_pnm
.Ok())
710 dc
.DrawBitmap( my_horse_asciigrey_pnm
, 280, 1300 );
712 dc
.DrawText( _T("PNM handler (raw grey)"), 530, 1285 );
713 if (my_horse_rawgrey_pnm
.Ok())
714 dc
.DrawBitmap( my_horse_rawgrey_pnm
, 530, 1300 );
716 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
717 if (my_horse_tiff
.Ok())
718 dc
.DrawBitmap( my_horse_tiff
, 30, 1530 );
720 dc
.DrawText( _T("XPM handler"), 30, 1745 );
721 if (my_horse_xpm
.Ok())
722 dc
.DrawBitmap( my_horse_xpm
, 30, 1760 );
725 int x
= 200, y
= 300, yy
= 170;;
727 dc
.DrawText(wxT("Original toucan"), x
+50, y
);
728 dc
.DrawBitmap(my_toucan
, x
, y
+15);
730 dc
.DrawText(wxT("Flipped horizontally"), x
+50, y
);
731 dc
.DrawBitmap(my_toucan_flipped_horiz
, x
, y
+15);
733 dc
.DrawText(wxT("Flipped vertically"), x
+50, y
);
734 dc
.DrawBitmap(my_toucan_flipped_vert
, x
, y
+15);
736 dc
.DrawText(wxT("Flipped both h&v"), x
+50, y
);
737 dc
.DrawBitmap(my_toucan_flipped_both
, x
, y
+15);
740 dc
.DrawText(wxT("Toucan's head"), x
+50, y
);
741 dc
.DrawBitmap(my_toucan_head
, x
, y
+15);
744 if (my_smile_xbm
.Ok())
746 dc
.DrawText( _T("XBM bitmap"), 30, 1975 );
747 dc
.DrawText( _T("(green on red)"), 30, 1990 );
748 dc
.SetTextForeground( _T("GREEN") );
749 dc
.SetTextBackground( _T("RED") );
750 dc
.DrawBitmap( my_smile_xbm
, 30, 2010 );
752 dc
.SetTextForeground( wxT("BLACK") );
753 dc
.DrawText( _T("After wxImage conversion"), 150, 1975 );
754 dc
.DrawText( _T("(red on white)"), 150, 1990 );
755 dc
.SetTextForeground( wxT("RED") );
756 wxImage i
= my_smile_xbm
.ConvertToImage();
757 i
.SetMaskColour( 255, 255, 255 );
759 wxRED_PEN
->GetColour().Red(),
760 wxRED_PEN
->GetColour().Green(),
761 wxRED_PEN
->GetColour().Blue() );
762 dc
.DrawBitmap( wxBitmap(i
), 150, 2010, true );
763 dc
.SetTextForeground( wxT("BLACK") );
767 wxBitmap
mono( 60,50,1 );
769 memdc
.SelectObject( mono
);
770 memdc
.SetPen( *wxBLACK_PEN
);
771 memdc
.SetBrush( *wxWHITE_BRUSH
);
772 memdc
.DrawRectangle( 0,0,60,50 );
773 memdc
.SetTextForeground( *wxBLACK
);
775 // I cannot convince GTK2 to draw into mono bitmaps
776 memdc
.DrawText( _T("Hi!"), 5, 5 );
778 memdc
.SetBrush( *wxBLACK_BRUSH
);
779 memdc
.DrawRectangle( 33,5,20,20 );
780 memdc
.SetPen( *wxRED_PEN
);
781 memdc
.DrawLine( 5, 42, 50, 42 );
782 memdc
.SelectObject( wxNullBitmap
);
786 dc
.DrawText( _T("Mono bitmap"), 30, 2095 );
787 dc
.DrawText( _T("(red on green)"), 30, 2110 );
788 dc
.SetTextForeground( wxT("RED") );
789 dc
.SetTextBackground( wxT("GREEN") );
790 dc
.DrawBitmap( mono
, 30, 2130 );
792 dc
.SetTextForeground( wxT("BLACK") );
793 dc
.DrawText( _T("After wxImage conversion"), 150, 2095 );
794 dc
.DrawText( _T("(red on white)"), 150, 2110 );
795 dc
.SetTextForeground( wxT("RED") );
796 wxImage i
= mono
.ConvertToImage();
797 i
.SetMaskColour( 255,255,255 );
799 wxRED_PEN
->GetColour().Red(),
800 wxRED_PEN
->GetColour().Green(),
801 wxRED_PEN
->GetColour().Blue() );
802 dc
.DrawBitmap( wxBitmap(i
), 150, 2130, true );
803 dc
.SetTextForeground( wxT("BLACK") );
806 // For testing transparency
807 dc
.SetBrush( *wxRED_BRUSH
);
808 dc
.DrawRectangle( 20, 2220, 560, 68 );
810 dc
.DrawText(_T("XPM bitmap"), 30, 2230 );
811 if ( m_bmpSmileXpm
.Ok() )
812 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, true);
814 dc
.DrawText(_T("XPM icon"), 110, 2230 );
815 if ( m_iconSmileXpm
.Ok() )
816 dc
.DrawIcon(m_iconSmileXpm
, 110, 2250);
818 // testing icon -> bitmap conversion
819 wxBitmap
to_blit( m_iconSmileXpm
);
822 dc
.DrawText( _T("SubBitmap"), 170, 2230 );
823 wxBitmap sub
= to_blit
.GetSubBitmap( wxRect(0,0,15,15) );
825 dc
.DrawBitmap( sub
, 170, 2250, true );
827 dc
.DrawText( _T("Enlarged"), 250, 2230 );
828 dc
.SetUserScale( 1.5, 1.5 );
829 dc
.DrawBitmap( to_blit
, (int)(250/1.5), (int)(2250/1.5), true );
830 dc
.SetUserScale( 2, 2 );
831 dc
.DrawBitmap( to_blit
, (int)(300/2), (int)(2250/2), true );
832 dc
.SetUserScale( 1.0, 1.0 );
834 dc
.DrawText( _T("Blit"), 400, 2230);
836 blit_dc
.SelectObject( to_blit
);
837 dc
.Blit( 400, 2250, to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
838 dc
.SetUserScale( 1.5, 1.5 );
839 dc
.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
840 dc
.SetUserScale( 2, 2 );
841 dc
.Blit( (int)(500/2), (int)(2250/2), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
842 dc
.SetUserScale( 1.0, 1.0 );
845 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
846 if (my_horse_ico32
.Ok())
847 dc
.DrawBitmap( my_horse_ico32
, 30, 2330, true );
849 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
850 if (my_horse_ico16
.Ok())
851 dc
.DrawBitmap( my_horse_ico16
, 230, 2330, true );
853 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
854 if (my_horse_ico
.Ok())
855 dc
.DrawBitmap( my_horse_ico
, 430, 2330, true );
857 dc
.DrawText( _T("CUR handler"), 30, 2390 );
858 if (my_horse_cur
.Ok())
860 dc
.DrawBitmap( my_horse_cur
, 30, 2420, true );
861 dc
.SetPen (*wxRED_PEN
);
862 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
863 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
866 dc
.DrawText( _T("ANI handler"), 230, 2390 );
867 for ( int i
=0; i
< m_ani_images
; i
++ )
869 if (my_horse_ani
[i
].Ok())
871 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, true );
876 if (colorized_horse_jpeg
.Ok())
878 dc
.DrawText( _T("Colorize image by rotating green hue to red"), 30, 2490 );
879 dc
.DrawBitmap( colorized_horse_jpeg
, 30, 2520 );
881 #endif // wxUSE_LIBJPEG
885 void MyCanvas
::CreateAntiAliasedBitmap()
887 wxBitmap
bitmap( 300, 300 );
891 dc
.SelectObject( bitmap
);
895 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
896 dc
.SetTextForeground( wxT("RED") );
897 dc
.DrawText( _T("This is anti-aliased Text."), 20, 5 );
898 dc
.DrawText( _T("And a Rectangle."), 20, 45 );
900 dc
.SetBrush( *wxRED_BRUSH
);
901 dc
.SetPen( *wxTRANSPARENT_PEN
);
902 dc
.DrawRoundedRectangle( 20, 85, 200, 180, 20 );
904 wxImage original
= bitmap
.ConvertToImage();
905 wxImage
anti( 150, 150 );
907 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
909 for (int y
= 1; y
< 149; y
++)
910 for (int x
= 1; x
< 149; x
++)
912 int red
= original
.GetRed( x
*2, y
*2 ) +
913 original
.GetRed( x
*2-1, y
*2 ) +
914 original
.GetRed( x
*2, y
*2+1 ) +
915 original
.GetRed( x
*2+1, y
*2+1 );
918 int green
= original
.GetGreen( x
*2, y
*2 ) +
919 original
.GetGreen( x
*2-1, y
*2 ) +
920 original
.GetGreen( x
*2, y
*2+1 ) +
921 original
.GetGreen( x
*2+1, y
*2+1 );
924 int blue
= original
.GetBlue( x
*2, y
*2 ) +
925 original
.GetBlue( x
*2-1, y
*2 ) +
926 original
.GetBlue( x
*2, y
*2+1 ) +
927 original
.GetBlue( x
*2+1, y
*2+1 );
929 anti
.SetRGB( x
, y
, (unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
931 my_anti
= wxBitmap(anti
);
939 ID_ABOUT
= wxID_ABOUT
,
944 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
946 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
947 EVT_MENU (ID_ABOUT
, MyFrame
::OnAbout
)
948 EVT_MENU (ID_QUIT
, MyFrame
::OnQuit
)
949 EVT_MENU (ID_NEW
, MyFrame
::OnNewFrame
)
950 #ifdef wxHAVE_RAW_BITMAP
951 EVT_MENU (ID_SHOWRAW
, MyFrame
::OnTestRawBitmap
)
955 EVT_MENU(wxID_COPY
, MyFrame
::OnCopy
)
956 EVT_MENU(wxID_PASTE
, MyFrame
::OnPaste
)
957 #endif // wxUSE_CLIPBOARD
961 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxImage sample"),
962 wxPoint(20,20), wxSize(470,360) )
964 wxMenuBar
*menu_bar
= new wxMenuBar();
966 wxMenu
*menuImage
= new wxMenu
;
967 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
969 #ifdef wxHAVE_RAW_BITMAP
970 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
972 menuImage
->AppendSeparator();
973 menuImage
->Append( ID_ABOUT
, _T("&About..."));
974 menuImage
->AppendSeparator();
975 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
976 menu_bar
->Append(menuImage
, _T("&Image"));
979 wxMenu
*menuClipboard
= new wxMenu
;
980 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
981 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
982 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
983 #endif // wxUSE_CLIPBOARD
985 SetMenuBar( menu_bar
);
989 int widths
[] = { -1, 100 };
990 SetStatusWidths( 2, widths
);
991 #endif // wxUSE_STATUSBAR
993 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
995 // 500 width * 2750 height
996 m_canvas
->SetScrollbars( 10, 10, 50, 275 );
999 void MyFrame
::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
1004 void MyFrame
::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
1006 (void)wxMessageBox( _T("wxImage demo\n")
1007 _T("Robert Roebling (c) 1998,2000"),
1008 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
1011 void MyFrame
::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
1014 wxString filename
= wxFileSelector(_T("Select image file"));
1019 if ( !image
.LoadFile(filename
) )
1021 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
1026 (new MyImageFrame(this, wxBitmap(image
)))->Show();
1027 #endif // wxUSE_FILEDLG
1030 #ifdef wxHAVE_RAW_BITMAP
1032 void MyFrame
::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
1034 (new MyRawBitmapFrame(this))->Show();
1037 #endif // wxHAVE_RAW_BITMAP
1041 void MyFrame
::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1043 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
1044 dobjBmp
->SetBitmap(m_canvas
->my_horse_png
);
1046 wxTheClipboard
->Open();
1048 if ( !wxTheClipboard
->SetData(dobjBmp
) )
1050 wxLogError(_T("Failed to copy bitmap to clipboard"));
1053 wxTheClipboard
->Close();
1056 void MyFrame
::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1058 wxBitmapDataObject dobjBmp
;
1060 wxTheClipboard
->Open();
1061 if ( !wxTheClipboard
->GetData(dobjBmp
) )
1063 wxLogMessage(_T("No bitmap data in the clipboard"));
1067 (new MyImageFrame(this, dobjBmp
.GetBitmap()))->Show();
1069 wxTheClipboard
->Close();
1072 #endif // wxUSE_CLIPBOARD
1074 //-----------------------------------------------------------------------------
1076 //-----------------------------------------------------------------------------
1078 bool MyApp
::OnInit()
1081 wxImage
::AddHandler( new wxPNGHandler
);
1085 wxImage
::AddHandler( new wxJPEGHandler
);
1089 wxImage
::AddHandler( new wxTIFFHandler
);
1093 wxImage
::AddHandler( new wxGIFHandler
);
1097 wxImage
::AddHandler( new wxPCXHandler
);
1101 wxImage
::AddHandler( new wxPNMHandler
);
1105 wxImage
::AddHandler( new wxXPMHandler
);
1109 wxImage
::AddHandler( new wxICOHandler
);
1110 wxImage
::AddHandler( new wxCURHandler
);
1111 wxImage
::AddHandler( new wxANIHandler
);
1114 wxFrame
*frame
= new MyFrame();
1115 frame
->Show( true );