4 * Author: Robert Roebling
6 * Copyright: (C) 1998, Robert Roebling
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
23 #include "wx/mstream.h"
24 #include "wx/wfstream.h"
25 #include "wx/quantize.h"
28 #include "wx/dataobj.h"
29 #include "wx/clipbrd.h"
30 #endif // wxUSE_CLIPBOARD
35 #if defined(__WXMSW__) || defined(__WXMAC__)
36 #ifdef wxHAVE_RAW_BITMAP
37 #include "wx/rawbmp.h"
48 class MyCanvas
: public wxScrolledWindow
52 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
54 void OnPaint( wxPaintEvent
&event
);
55 void CreateAntiAliasedBitmap();
57 wxBitmap
*my_horse_png
;
58 wxBitmap
*my_horse_jpeg
;
59 wxBitmap
*my_horse_gif
;
60 wxBitmap
*my_horse_bmp
;
61 wxBitmap
*my_horse_bmp2
;
62 wxBitmap
*my_horse_pcx
;
63 wxBitmap
*my_horse_pnm
;
64 wxBitmap
*my_horse_tiff
;
65 wxBitmap
*my_horse_xpm
;
66 wxBitmap
*my_horse_ico32
;
67 wxBitmap
*my_horse_ico16
;
68 wxBitmap
*my_horse_ico
;
69 wxBitmap
*my_horse_cur
;
70 wxBitmap
*my_horse_ani
;
72 wxBitmap
*my_smile_xbm
;
80 wxBitmap m_bmpSmileXpm
;
81 wxIcon m_iconSmileXpm
;
84 DECLARE_DYNAMIC_CLASS(MyCanvas
)
89 const int nChoices
= 8 ;
90 static const wxString bppchoices
[nChoices
] =
96 _T("8 bpp greyscale"),
98 _T("8 bpp own palette"),
102 static const int bppvalues
[nChoices
] =
117 class MyFrame
: public wxFrame
122 void OnAbout( wxCommandEvent
&event
);
123 void OnNewFrame( wxCommandEvent
&event
);
124 #ifdef wxHAVE_RAW_BITMAP
125 void OnTestRawBitmap( wxCommandEvent
&event
);
126 #endif // wxHAVE_RAW_BITMAP
127 void OnQuit( wxCommandEvent
&event
);
130 void OnCopy(wxCommandEvent
& event
);
131 void OnPaste(wxCommandEvent
& event
);
132 #endif // wxUSE_CLIPBOARD
137 DECLARE_DYNAMIC_CLASS(MyFrame
)
138 DECLARE_EVENT_TABLE()
141 class MyImageFrame
: public wxFrame
144 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
145 : wxFrame(parent
, -1, _T("Double click to save"),
146 wxDefaultPosition
, wxDefaultSize
,
147 wxCAPTION
| wxSYSTEM_MENU
| wxCLOSE_BOX
),
150 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
153 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
155 // do nothing here to be able to see how transparent images are shown
158 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
160 wxPaintDC
dc( this );
161 dc
.DrawBitmap( m_bitmap
, 0, 0, TRUE
/* use mask */ );
164 void OnSave(wxMouseEvent
& WXUNUSED(event
))
166 wxImage image
= m_bitmap
.ConvertToImage();
168 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
173 if ( bppselection
== -1 )
179 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, bppvalues
[bppselection
]);
181 wxString deffilename
= bppchoices
[bppselection
];
182 deffilename
.Replace(wxT(" "), wxT("_"));
183 deffilename
+= wxT(".bmp");
184 wxString savefilename
= wxFileSelector( wxT("Save Image"),
187 (const wxChar
*)NULL
,
188 wxT("BMP files (*.bmp)|*.bmp|")
189 wxT("PNG files (*.png)|*.png|")
190 wxT("JPEG files (*.jpg)|*.jpg|")
191 wxT("GIF files (*.gif)|*.gif|")
192 wxT("TIFF files (*.tif)|*.tif|")
193 wxT("PCX files (*.pcx)|*.pcx|")
194 wxT("ICO files (*.ico)|*.ico|")
195 wxT("CUR files (*.cur)|*.cur"),
198 if ( savefilename
.empty() )
201 if ( image
.GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT
) == wxBMP_8BPP_PALETTE
)
203 unsigned char *cmap
= new unsigned char [256];
204 for ( int i
= 0; i
< 256; i
++ )
206 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
212 wxString extension
= savefilename
.AfterLast('.').Lower();
214 if (extension
== _T("cur"))
216 image
.Rescale(32,32);
217 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
218 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
219 // This shows how you can save an image with explicitly
220 // specified image format:
221 loaded
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
225 // This one guesses image format from filename extension
226 // (it may fail if the extension is not recognized):
227 loaded
= image
.SaveFile(savefilename
);
231 wxMessageBox(_T("No handler for this file type."),
232 _T("File was not saved"),
233 wxOK
|wxCENTRE
, this);
239 DECLARE_EVENT_TABLE()
242 #ifdef wxHAVE_RAW_BITMAP
244 #include "wx/rawbmp.h"
246 class MyRawBitmapFrame
: public wxFrame
253 REAL_SIZE
= SIZE
- 2*BORDER
256 MyRawBitmapFrame(wxFrame
*parent
)
257 : wxFrame(parent
, -1, _T("Raw bitmaps (how exciting)")),
258 m_bitmap(SIZE
, SIZE
, 32)
260 SetClientSize(SIZE
, SIZE
);
262 // another possibility: wxNativePixelData (don't forget to remove code
263 // setting alpha in the loop below then)
264 typedef wxAlphaPixelData Data
;
266 Data
data(m_bitmap
, wxPoint(BORDER
, BORDER
), wxSize(REAL_SIZE
, REAL_SIZE
));
269 wxLogError(_T("Failed to gain raw access to bitmap data"));
275 Data::Iterator
p(data
);
277 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
279 Data::Iterator rowStart
= p
;
281 int r
= y
< REAL_SIZE
/3 ? 255 : 0,
282 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ? 255 : 0,
283 b
= 2*(REAL_SIZE
/3) <= y
? 255 : 0;
285 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
290 p
.Alpha() = (Data::Iterator::ChannelType
)((x
*255.)/REAL_SIZE
);
292 ++p
; // same as p.OffsetX(1)
300 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
302 wxPaintDC
dc( this );
303 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER
);
304 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
305 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
306 dc
.DrawBitmap( m_bitmap
, 0, 0, TRUE
/* use mask */ );
312 DECLARE_EVENT_TABLE()
315 #endif // wxHAVE_RAW_BITMAP
319 class MyApp
: public wxApp
322 virtual bool OnInit();
331 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
333 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
334 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
335 EVT_PAINT(MyImageFrame::OnPaint
)
336 EVT_LEFT_DCLICK(MyImageFrame::OnSave
)
339 #ifdef wxHAVE_RAW_BITMAP
341 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
342 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
345 #endif // wxHAVE_RAW_BITMAP
347 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
348 EVT_PAINT(MyCanvas::OnPaint
)
351 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
352 const wxPoint
&pos
, const wxSize
&size
)
353 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
354 , m_bmpSmileXpm((const char **) smile_xpm
)
355 , m_iconSmileXpm((const char **) smile_xpm
)
357 my_horse_png
= (wxBitmap
*) NULL
;
358 my_horse_jpeg
= (wxBitmap
*) NULL
;
359 my_horse_gif
= (wxBitmap
*) NULL
;
360 my_horse_bmp
= (wxBitmap
*) NULL
;
361 my_horse_bmp2
= (wxBitmap
*) NULL
;
362 my_horse_pcx
= (wxBitmap
*) NULL
;
363 my_horse_pnm
= (wxBitmap
*) NULL
;
364 my_horse_tiff
= (wxBitmap
*) NULL
;
365 my_horse_xpm
= (wxBitmap
*) NULL
;
366 my_horse_ico32
= (wxBitmap
*) NULL
;
367 my_horse_ico16
= (wxBitmap
*) NULL
;
368 my_horse_ico
= (wxBitmap
*) NULL
;
369 my_horse_cur
= (wxBitmap
*) NULL
;
370 my_horse_ani
= (wxBitmap
*) NULL
;
372 my_smile_xbm
= (wxBitmap
*) NULL
;
373 my_square
= (wxBitmap
*) NULL
;
374 my_anti
= (wxBitmap
*) NULL
;
378 SetBackgroundColour(* wxWHITE
);
380 wxBitmap
bitmap( 100, 100 );
383 dc
.SelectObject( bitmap
);
384 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
385 dc
.SetPen( *wxBLACK_PEN
);
386 dc
.DrawRectangle( 0, 0, 100, 100 );
387 dc
.SetBrush( *wxWHITE_BRUSH
);
388 dc
.DrawRectangle( 20, 20, 60, 60 );
389 dc
.SelectObject( wxNullBitmap
);
391 // try to find the directory with our images
393 if ( wxFile::Exists(wxT("./horse.png")) )
395 else if ( wxFile::Exists(wxT("../horse.png")) )
398 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
400 wxImage image
= bitmap
.ConvertToImage();
403 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
404 wxLogError(wxT("Can't save file"));
408 if ( image
.LoadFile( dir
+ _T("test.png") ) )
409 my_square
= new wxBitmap( image
);
413 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
414 wxLogError(wxT("Can't load PNG image"));
416 my_horse_png
= new wxBitmap( image
);
417 #endif // wxUSE_LIBPNG
422 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
423 wxLogError(wxT("Can't load JPG image"));
425 my_horse_jpeg
= new wxBitmap( image
);
426 #endif // wxUSE_LIBJPEG
431 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
432 wxLogError(wxT("Can't load GIF image"));
434 my_horse_gif
= new wxBitmap( image
);
440 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
441 wxLogError(wxT("Can't load PCX image"));
443 my_horse_pcx
= new wxBitmap( image
);
448 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
449 wxLogError(wxT("Can't load BMP image"));
451 my_horse_bmp
= new wxBitmap( image
);
456 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
457 wxLogError(wxT("Can't load XPM image"));
459 my_horse_xpm
= new wxBitmap( image
);
461 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
462 wxLogError(wxT("Can't save file"));
468 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
469 wxLogError(wxT("Can't load PNM image"));
471 my_horse_pnm
= new wxBitmap( image
);
477 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
478 wxLogError(wxT("Can't load TIFF image"));
480 my_horse_tiff
= new wxBitmap( image
);
483 CreateAntiAliasedBitmap();
485 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
488 // demonstrates XPM automatically using the mask when saving
489 if ( m_bmpSmileXpm
.Ok() )
490 m_bmpSmileXpm
.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM
);
495 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
496 wxLogError(wxT("Can't load first ICO image"));
498 my_horse_ico32
= new wxBitmap( image
);
502 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
503 wxLogError(wxT("Can't load second ICO image"));
505 my_horse_ico16
= new wxBitmap( image
);
509 if ( !image
.LoadFile( dir
+ _T("horse.ico") ) )
510 wxLogError(wxT("Can't load best ICO image"));
512 my_horse_ico
= new wxBitmap( image
);
516 if ( !image
.LoadFile( dir
+ _T("horse.cur"), wxBITMAP_TYPE_CUR
) )
517 wxLogError(wxT("Can't load best ICO image"));
520 my_horse_cur
= new wxBitmap( image
);
521 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
522 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
525 m_ani_images
= wxImage::GetImageCount ( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
);
527 wxLogError(wxT("No ANI-format images found"));
529 my_horse_ani
= new wxBitmap
[m_ani_images
];
531 for (i
=0; i
< m_ani_images
; i
++)
534 if (!image
.LoadFile( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
, i
))
536 wxString tmp
= wxT("Can't load image number ");
541 my_horse_ani
[i
] = wxBitmap( image
);
543 #endif // wxUSE_ICO_CUR
547 // test image loading from stream
548 wxFile
file(dir
+ _T("horse.bmp"));
549 if ( file
.IsOpened() )
551 off_t len
= file
.Length();
552 void *data
= malloc(len
);
553 if ( file
.Read(data
, len
) != len
)
554 wxLogError(_T("Reading bitmap file failed"));
557 wxMemoryInputStream
mis(data
, len
);
558 if ( !image
.LoadFile(mis
) )
559 wxLogError(wxT("Can't load BMP image from stream"));
561 my_horse_bmp2
= new wxBitmap( image
);
568 MyCanvas::~MyCanvas()
572 delete my_horse_jpeg
;
575 delete my_horse_bmp2
;
577 delete my_horse_tiff
;
579 delete my_horse_ico32
;
580 delete my_horse_ico16
;
583 delete [] my_horse_ani
;
589 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
591 wxPaintDC
dc( this );
594 dc
.DrawText( _T("Loaded image"), 30, 10 );
595 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
597 dc
.DrawText( _T("Drawn directly"), 150, 10 );
598 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
599 dc
.SetPen( *wxBLACK_PEN
);
600 dc
.DrawRectangle( 150, 30, 100, 100 );
601 dc
.SetBrush( *wxWHITE_BRUSH
);
602 dc
.DrawRectangle( 170, 50, 60, 60 );
604 if (my_anti
&& my_anti
->Ok())
605 dc
.DrawBitmap( *my_anti
, 280, 30 );
607 dc
.DrawText( _T("PNG handler"), 30, 135 );
608 if (my_horse_png
&& my_horse_png
->Ok())
610 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
611 wxRect
rect(0,0,100,100);
612 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
613 dc
.DrawText( _T("GetSubBitmap()"), 280, 190 );
614 dc
.DrawBitmap( sub
, 280, 210 );
617 dc
.DrawText( _T("JPEG handler"), 30, 365 );
618 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
619 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
621 dc
.DrawText( _T("GIF handler"), 30, 595 );
622 if (my_horse_gif
&& my_horse_gif
->Ok())
623 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
625 dc
.DrawText( _T("PCX handler"), 30, 825 );
626 if (my_horse_pcx
&& my_horse_pcx
->Ok())
627 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
629 dc
.DrawText( _T("BMP handler"), 30, 1055 );
630 if (my_horse_bmp
&& my_horse_bmp
->Ok())
631 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
633 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
634 if (my_horse_bmp2
&& my_horse_bmp2
->Ok())
635 dc
.DrawBitmap( *my_horse_bmp2
, 280, 1070 );
637 dc
.DrawText( _T("PNM handler"), 30, 1285 );
638 if (my_horse_pnm
&& my_horse_pnm
->Ok())
639 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
641 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
642 if (my_horse_tiff
&& my_horse_tiff
->Ok())
643 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
645 dc
.DrawText( _T("XPM handler"), 30, 1745 );
646 if (my_horse_xpm
&& my_horse_xpm
->Ok())
647 dc
.DrawBitmap( *my_horse_xpm
, 30, 1760 );
650 if (my_smile_xbm
&& my_smile_xbm
->Ok())
652 dc
.DrawText( _T("XBM bitmap"), 30, 1975 );
653 dc
.DrawText( _T("(green on red)"), 30, 1990 );
654 dc
.SetTextForeground( _T("GREEN") );
655 dc
.SetTextBackground( _T("RED") );
656 dc
.DrawBitmap( *my_smile_xbm
, 30, 2010 );
658 dc
.SetTextForeground( wxT("BLACK") );
659 dc
.DrawText( _T("After wxImage conversion"), 150, 1975 );
660 dc
.DrawText( _T("(red on white)"), 150, 1990 );
661 dc
.SetTextForeground( wxT("RED") );
662 wxImage i
= my_smile_xbm
->ConvertToImage();
663 i
.SetMaskColour( 255, 255, 255 );
665 wxRED_PEN
->GetColour().Red(),
666 wxRED_PEN
->GetColour().Green(),
667 wxRED_PEN
->GetColour().Blue() );
668 dc
.DrawBitmap( wxBitmap(i
), 150, 2010, TRUE
);
669 dc
.SetTextForeground( wxT("BLACK") );
673 wxBitmap
mono( 60,50,1 );
675 memdc
.SelectObject( mono
);
676 memdc
.SetPen( *wxBLACK_PEN
);
677 memdc
.SetBrush( *wxWHITE_BRUSH
);
678 memdc
.DrawRectangle( 0,0,60,50 );
679 memdc
.SetTextForeground( *wxBLACK
);
681 // I cannot convince GTK2 to draw into mono bitmaps
682 memdc
.DrawText( _T("Hi!"), 5, 5 );
684 memdc
.SetBrush( *wxBLACK_BRUSH
);
685 memdc
.DrawRectangle( 33,5,20,20 );
686 memdc
.SetPen( *wxRED_PEN
);
687 memdc
.DrawLine( 5, 42, 50, 42 );
688 memdc
.SelectObject( wxNullBitmap
);
692 dc
.DrawText( _T("Mono bitmap"), 30, 2095 );
693 dc
.DrawText( _T("(red on green)"), 30, 2110 );
694 dc
.SetTextForeground( wxT("RED") );
695 dc
.SetTextBackground( wxT("GREEN") );
696 dc
.DrawBitmap( mono
, 30, 2130 );
698 dc
.SetTextForeground( wxT("BLACK") );
699 dc
.DrawText( _T("After wxImage conversion"), 150, 2095 );
700 dc
.DrawText( _T("(red on white)"), 150, 2110 );
701 dc
.SetTextForeground( wxT("RED") );
702 wxImage i
= mono
.ConvertToImage();
703 i
.SetMaskColour( 255,255,255 );
705 wxRED_PEN
->GetColour().Red(),
706 wxRED_PEN
->GetColour().Green(),
707 wxRED_PEN
->GetColour().Blue() );
708 dc
.DrawBitmap( wxBitmap(i
), 150, 2130, TRUE
);
709 dc
.SetTextForeground( wxT("BLACK") );
712 // For testing transparency
713 dc
.SetBrush( *wxRED_BRUSH
);
714 dc
.DrawRectangle( 20, 2220, 560, 68 );
716 dc
.DrawText(_T("XPM bitmap"), 30, 2230 );
717 if ( m_bmpSmileXpm
.Ok() )
718 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, TRUE
);
720 dc
.DrawText(_T("XPM icon"), 110, 2230 );
721 if ( m_iconSmileXpm
.Ok() )
722 dc
.DrawIcon(m_iconSmileXpm
, 110, 2250);
724 // testing icon -> bitmap conversion
725 wxBitmap
to_blit( m_iconSmileXpm
);
728 dc
.DrawText( _T("SubBitmap"), 170, 2230 );
729 wxBitmap sub
= to_blit
.GetSubBitmap( wxRect(0,0,15,15) );
731 dc
.DrawBitmap( sub
, 170, 2250, TRUE
);
733 dc
.DrawText( _T("Enlarged"), 250, 2230 );
734 dc
.SetUserScale( 1.5, 1.5 );
735 dc
.DrawBitmap( to_blit
, (int)(250/1.5), (int)(2250/1.5), TRUE
);
736 dc
.SetUserScale( 2, 2 );
737 dc
.DrawBitmap( to_blit
, (int)(300/2), (int)(2250/2), TRUE
);
738 dc
.SetUserScale( 1.0, 1.0 );
740 dc
.DrawText( _T("Blit"), 400, 2230);
742 blit_dc
.SelectObject( to_blit
);
743 dc
.Blit( 400, 2250, to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, TRUE
);
744 dc
.SetUserScale( 1.5, 1.5 );
745 dc
.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, TRUE
);
746 dc
.SetUserScale( 2, 2 );
747 dc
.Blit( (int)(500/2), (int)(2250/2), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, TRUE
);
748 dc
.SetUserScale( 1.0, 1.0 );
751 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
752 if (my_horse_ico32
&& my_horse_ico32
->Ok())
753 dc
.DrawBitmap( *my_horse_ico32
, 30, 2330, TRUE
);
755 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
756 if (my_horse_ico16
&& my_horse_ico16
->Ok())
757 dc
.DrawBitmap( *my_horse_ico16
, 230, 2330, TRUE
);
759 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
760 if (my_horse_ico
&& my_horse_ico
->Ok())
761 dc
.DrawBitmap( *my_horse_ico
, 430, 2330, TRUE
);
763 dc
.DrawText( _T("CUR handler"), 30, 2390 );
764 if (my_horse_cur
&& my_horse_cur
->Ok())
766 dc
.DrawBitmap( *my_horse_cur
, 30, 2420, TRUE
);
767 dc
.SetPen (*wxRED_PEN
);
768 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
769 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
771 dc
.DrawText( _T("ANI handler"), 230, 2390 );
773 for (i
=0; i
< m_ani_images
; i
++)
774 if (my_horse_ani
[i
].Ok())
776 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, TRUE
);
780 void MyCanvas::CreateAntiAliasedBitmap()
782 wxBitmap
bitmap( 300, 300 );
786 dc
.SelectObject( bitmap
);
790 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
791 dc
.SetTextForeground( wxT("RED") );
792 dc
.DrawText( _T("This is anti-aliased Text."), 20, 20 );
793 dc
.DrawText( _T("And a Rectangle."), 20, 60 );
795 dc
.SetBrush( *wxRED_BRUSH
);
796 dc
.SetPen( *wxTRANSPARENT_PEN
);
797 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
799 wxImage original
= bitmap
.ConvertToImage();
800 wxImage
anti( 150, 150 );
802 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
804 for (int y
= 1; y
< 149; y
++)
805 for (int x
= 1; x
< 149; x
++)
807 int red
= original
.GetRed( x
*2, y
*2 ) +
808 original
.GetRed( x
*2-1, y
*2 ) +
809 original
.GetRed( x
*2, y
*2+1 ) +
810 original
.GetRed( x
*2+1, y
*2+1 );
813 int green
= original
.GetGreen( x
*2, y
*2 ) +
814 original
.GetGreen( x
*2-1, y
*2 ) +
815 original
.GetGreen( x
*2, y
*2+1 ) +
816 original
.GetGreen( x
*2+1, y
*2+1 );
819 int blue
= original
.GetBlue( x
*2, y
*2 ) +
820 original
.GetBlue( x
*2-1, y
*2 ) +
821 original
.GetBlue( x
*2, y
*2+1 ) +
822 original
.GetBlue( x
*2+1, y
*2+1 );
824 anti
.SetRGB( x
, y
, red
, green
, blue
);
826 my_anti
= new wxBitmap(anti
);
839 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
841 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
842 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
843 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
844 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
845 #ifdef wxHAVE_RAW_BITMAP
846 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
850 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
851 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
852 #endif // wxUSE_CLIPBOARD
856 : wxFrame( (wxFrame
*)NULL
, -1, _T("wxImage sample"),
857 wxPoint(20,20), wxSize(470,360) )
859 wxMenuBar
*menu_bar
= new wxMenuBar();
861 wxMenu
*menuImage
= new wxMenu
;
862 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
864 #ifdef wxHAVE_RAW_BITMAP
865 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
867 menuImage
->AppendSeparator();
868 menuImage
->Append( ID_ABOUT
, _T("&About..."));
869 menuImage
->AppendSeparator();
870 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
871 menu_bar
->Append(menuImage
, _T("&Image"));
874 wxMenu
*menuClipboard
= new wxMenu
;
875 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
876 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
877 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
878 #endif // wxUSE_CLIPBOARD
880 SetMenuBar( menu_bar
);
883 int widths
[] = { -1, 100 };
884 SetStatusWidths( 2, widths
);
886 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
888 // 500 width * 2500 height
889 m_canvas
->SetScrollbars( 10, 10, 50, 250 );
892 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
897 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
899 (void)wxMessageBox( _T("wxImage demo\n")
900 _T("Robert Roebling (c) 1998,2000"),
901 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
904 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
906 wxString filename
= wxFileSelector(_T("Select image file"));
911 if ( !image
.LoadFile(filename
) )
913 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
918 (new MyImageFrame(this, wxBitmap(image
)))->Show();
921 #ifdef wxHAVE_RAW_BITMAP
923 void MyFrame::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
925 (new MyRawBitmapFrame(this))->Show();
928 #endif // wxHAVE_RAW_BITMAP
932 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
934 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
935 dobjBmp
->SetBitmap(*m_canvas
->my_horse_png
);
937 wxTheClipboard
->Open();
939 if ( !wxTheClipboard
->SetData(dobjBmp
) )
941 wxLogError(_T("Failed to copy bitmap to clipboard"));
944 wxTheClipboard
->Close();
947 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
949 wxBitmapDataObject dobjBmp
;
951 wxTheClipboard
->Open();
952 if ( !wxTheClipboard
->GetData(dobjBmp
) )
954 wxLogMessage(_T("No bitmap data in the clipboard"));
958 (new MyImageFrame(this, dobjBmp
.GetBitmap()))->Show();
960 wxTheClipboard
->Close();
963 #endif // wxUSE_CLIPBOARD
965 //-----------------------------------------------------------------------------
967 //-----------------------------------------------------------------------------
972 wxImage::AddHandler( new wxPNGHandler
);
976 wxImage::AddHandler( new wxJPEGHandler
);
980 wxImage::AddHandler( new wxTIFFHandler
);
984 wxImage::AddHandler( new wxGIFHandler
);
988 wxImage::AddHandler( new wxPCXHandler
);
992 wxImage::AddHandler( new wxPNMHandler
);
996 wxImage::AddHandler( new wxXPMHandler
);
1000 wxImage::AddHandler( new wxICOHandler
);
1001 wxImage::AddHandler( new wxCURHandler
);
1002 wxImage::AddHandler( new wxANIHandler
);
1005 wxFrame
*frame
= new MyFrame();
1006 frame
->Show( TRUE
);