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
34 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
38 #if defined(__WXMSW__) || defined(__WXMAC__)
39 #include "wx/rawbmp.h"
40 #define wxHAVE_RAW_BITMAP
50 class MyCanvas
: public wxScrolledWindow
54 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
56 void OnPaint( wxPaintEvent
&event
);
57 void CreateAntiAliasedBitmap();
59 wxBitmap
*my_horse_png
;
60 wxBitmap
*my_horse_jpeg
;
61 wxBitmap
*my_horse_gif
;
62 wxBitmap
*my_horse_bmp
;
63 wxBitmap
*my_horse_bmp2
;
64 wxBitmap
*my_horse_pcx
;
65 wxBitmap
*my_horse_pnm
;
66 wxBitmap
*my_horse_tiff
;
67 wxBitmap
*my_horse_xpm
;
68 wxBitmap
*my_horse_ico32
;
69 wxBitmap
*my_horse_ico16
;
70 wxBitmap
*my_horse_ico
;
71 wxBitmap
*my_horse_cur
;
72 wxBitmap
*my_horse_ani
;
74 wxBitmap
*my_smile_xbm
;
82 wxBitmap m_bmpSmileXpm
;
83 wxIcon m_iconSmileXpm
;
86 DECLARE_DYNAMIC_CLASS(MyCanvas
)
91 const int nChoices
= 8 ;
92 static const wxString bppchoices
[nChoices
] =
98 _T("8 bpp greyscale"),
100 _T("8 bpp own palette"),
104 static const int bppvalues
[nChoices
] =
119 class MyFrame
: public wxFrame
124 void OnAbout( wxCommandEvent
&event
);
125 void OnNewFrame( wxCommandEvent
&event
);
126 #ifdef wxHAVE_RAW_BITMAP
127 void OnTestRawBitmap( wxCommandEvent
&event
);
128 #endif // wxHAVE_RAW_BITMAP
129 void OnQuit( wxCommandEvent
&event
);
132 void OnCopy(wxCommandEvent
& event
);
133 void OnPaste(wxCommandEvent
& event
);
134 #endif // wxUSE_CLIPBOARD
139 DECLARE_DYNAMIC_CLASS(MyFrame
)
140 DECLARE_EVENT_TABLE()
143 class MyImageFrame
: public wxFrame
146 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
147 : wxFrame(parent
, -1, _T("Double click to save"),
148 wxDefaultPosition
, wxDefaultSize
,
149 wxCAPTION
| wxSYSTEM_MENU
| wxCLOSE_BOX
),
152 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
155 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
157 // do nothing here to be able to see how transparent images are shown
160 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
162 wxPaintDC
dc( this );
163 dc
.DrawBitmap( m_bitmap
, 0, 0, TRUE
/* use mask */ );
166 void OnSave(wxMouseEvent
& WXUNUSED(event
))
168 wxImage image
= m_bitmap
.ConvertToImage();
170 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
175 if ( bppselection
== -1 )
181 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, bppvalues
[bppselection
]);
183 wxString deffilename
= bppchoices
[bppselection
];
184 deffilename
.Replace(wxT(" "), wxT("_"));
185 deffilename
+= wxT(".bmp");
186 wxString savefilename
= wxFileSelector( wxT("Save Image"),
189 (const wxChar
*)NULL
,
190 wxT("BMP files (*.bmp)|*.bmp|")
191 wxT("PNG files (*.png)|*.png|")
192 wxT("JPEG files (*.jpg)|*.jpg|")
193 wxT("GIF files (*.gif)|*.gif|")
194 wxT("TIFF files (*.tif)|*.tif|")
195 wxT("PCX files (*.pcx)|*.pcx|")
196 wxT("ICO files (*.ico)|*.ico|")
197 wxT("CUR files (*.cur)|*.cur"),
200 if ( savefilename
.empty() )
203 if ( image
.GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT
) == wxBMP_8BPP_PALETTE
)
205 unsigned char *cmap
= new unsigned char [256];
206 for ( int i
= 0; i
< 256; i
++ )
208 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
214 wxString extension
= savefilename
.AfterLast('.').Lower();
216 if (extension
== _T("cur"))
218 image
.Rescale(32,32);
219 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
220 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
221 // This shows how you can save an image with explicitly
222 // specified image format:
223 loaded
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
227 // This one guesses image format from filename extension
228 // (it may fail if the extension is not recognized):
229 loaded
= image
.SaveFile(savefilename
);
233 wxMessageBox(_T("No handler for this file type."),
234 _T("File was not saved"),
235 wxOK
|wxCENTRE
, this);
241 DECLARE_EVENT_TABLE()
244 #ifdef wxHAVE_RAW_BITMAP
246 #include "wx/rawbmp.h"
248 class MyRawBitmapFrame
: public wxFrame
255 REAL_SIZE
= SIZE
- 2*BORDER
258 MyRawBitmapFrame(wxFrame
*parent
)
259 : wxFrame(parent
, -1, _T("Raw bitmaps (how exciting)")),
260 m_bitmap(SIZE
, SIZE
, 32)
262 SetClientSize(SIZE
, SIZE
);
264 // another possibility: wxNativePixelData (don't forget to remove code
265 // setting alpha in the loop below then)
266 typedef wxAlphaPixelData Data
;
268 Data
data(m_bitmap
, wxPoint(BORDER
, BORDER
), wxSize(REAL_SIZE
, REAL_SIZE
));
271 wxLogError(_T("Failed to gain raw access to bitmap data"));
277 Data::Iterator
p(data
);
279 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
281 Data::Iterator rowStart
= p
;
283 int r
= y
< REAL_SIZE
/3 ? 255 : 0,
284 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ? 255 : 0,
285 b
= 2*(REAL_SIZE
/3) <= y
? 255 : 0;
287 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
292 p
.Alpha() = (Data::Iterator::ChannelType
)((x
*255.)/REAL_SIZE
);
294 ++p
; // same as p.OffsetX(1)
302 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
304 wxPaintDC
dc( this );
305 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER
);
306 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
307 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
308 dc
.DrawBitmap( m_bitmap
, 0, 0, TRUE
/* use mask */ );
314 DECLARE_EVENT_TABLE()
317 #endif // wxHAVE_RAW_BITMAP
321 class MyApp
: public wxApp
324 virtual bool OnInit();
333 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
335 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
336 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
337 EVT_PAINT(MyImageFrame::OnPaint
)
338 EVT_LEFT_DCLICK(MyImageFrame::OnSave
)
341 #ifdef wxHAVE_RAW_BITMAP
343 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
344 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
347 #endif // wxHAVE_RAW_BITMAP
349 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
350 EVT_PAINT(MyCanvas::OnPaint
)
353 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
354 const wxPoint
&pos
, const wxSize
&size
)
355 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
356 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
357 , m_bmpSmileXpm((const char **) smile_xpm
)
358 , m_iconSmileXpm((const char **) smile_xpm
)
361 my_horse_png
= (wxBitmap
*) NULL
;
362 my_horse_jpeg
= (wxBitmap
*) NULL
;
363 my_horse_gif
= (wxBitmap
*) NULL
;
364 my_horse_bmp
= (wxBitmap
*) NULL
;
365 my_horse_bmp2
= (wxBitmap
*) NULL
;
366 my_horse_pcx
= (wxBitmap
*) NULL
;
367 my_horse_pnm
= (wxBitmap
*) NULL
;
368 my_horse_tiff
= (wxBitmap
*) NULL
;
369 my_horse_xpm
= (wxBitmap
*) NULL
;
370 my_horse_ico32
= (wxBitmap
*) NULL
;
371 my_horse_ico16
= (wxBitmap
*) NULL
;
372 my_horse_ico
= (wxBitmap
*) NULL
;
373 my_horse_cur
= (wxBitmap
*) NULL
;
374 my_horse_ani
= (wxBitmap
*) NULL
;
376 my_smile_xbm
= (wxBitmap
*) NULL
;
377 my_square
= (wxBitmap
*) NULL
;
378 my_anti
= (wxBitmap
*) NULL
;
382 SetBackgroundColour(* wxWHITE
);
384 wxBitmap
bitmap( 100, 100 );
387 dc
.SelectObject( bitmap
);
388 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
389 dc
.SetPen( *wxBLACK_PEN
);
390 dc
.DrawRectangle( 0, 0, 100, 100 );
391 dc
.SetBrush( *wxWHITE_BRUSH
);
392 dc
.DrawRectangle( 20, 20, 60, 60 );
393 dc
.SelectObject( wxNullBitmap
);
395 // try to find the directory with our images
397 if ( wxFile::Exists(wxT("./horse.png")) )
399 else if ( wxFile::Exists(wxT("../horse.png")) )
402 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
404 wxImage image
= bitmap
.ConvertToImage();
407 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
408 wxLogError(wxT("Can't save file"));
412 if ( image
.LoadFile( dir
+ _T("test.png") ) )
413 my_square
= new wxBitmap( image
);
417 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
418 wxLogError(wxT("Can't load PNG image"));
420 my_horse_png
= new wxBitmap( image
);
421 #endif // wxUSE_LIBPNG
426 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
427 wxLogError(wxT("Can't load JPG image"));
429 my_horse_jpeg
= new wxBitmap( image
);
430 #endif // wxUSE_LIBJPEG
435 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
436 wxLogError(wxT("Can't load GIF image"));
438 my_horse_gif
= new wxBitmap( image
);
444 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
445 wxLogError(wxT("Can't load PCX image"));
447 my_horse_pcx
= new wxBitmap( image
);
452 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
453 wxLogError(wxT("Can't load BMP image"));
455 my_horse_bmp
= new wxBitmap( image
);
460 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
461 wxLogError(wxT("Can't load XPM image"));
463 my_horse_xpm
= new wxBitmap( image
);
465 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
466 wxLogError(wxT("Can't save file"));
472 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
473 wxLogError(wxT("Can't load PNM image"));
475 my_horse_pnm
= new wxBitmap( image
);
481 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
482 wxLogError(wxT("Can't load TIFF image"));
484 my_horse_tiff
= new wxBitmap( image
);
487 CreateAntiAliasedBitmap();
489 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
492 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
493 // demonstrates XPM automatically using the mask when saving
494 if ( m_bmpSmileXpm
.Ok() )
495 m_bmpSmileXpm
.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM
);
501 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
502 wxLogError(wxT("Can't load first ICO image"));
504 my_horse_ico32
= new wxBitmap( image
);
508 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
509 wxLogError(wxT("Can't load second ICO image"));
511 my_horse_ico16
= new wxBitmap( image
);
515 if ( !image
.LoadFile( dir
+ _T("horse.ico") ) )
516 wxLogError(wxT("Can't load best ICO image"));
518 my_horse_ico
= new wxBitmap( image
);
522 if ( !image
.LoadFile( dir
+ _T("horse.cur"), wxBITMAP_TYPE_CUR
) )
523 wxLogError(wxT("Can't load best ICO image"));
526 my_horse_cur
= new wxBitmap( image
);
527 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
528 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
531 m_ani_images
= wxImage::GetImageCount ( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
);
533 wxLogError(wxT("No ANI-format images found"));
535 my_horse_ani
= new wxBitmap
[m_ani_images
];
537 for (i
=0; i
< m_ani_images
; i
++)
540 if (!image
.LoadFile( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
, i
))
542 wxString tmp
= wxT("Can't load image number ");
547 my_horse_ani
[i
] = wxBitmap( image
);
549 #endif // wxUSE_ICO_CUR
553 // test image loading from stream
554 wxFile
file(dir
+ _T("horse.bmp"));
555 if ( file
.IsOpened() )
557 off_t len
= file
.Length();
558 void *data
= malloc(len
);
559 if ( file
.Read(data
, len
) != len
)
560 wxLogError(_T("Reading bitmap file failed"));
563 wxMemoryInputStream
mis(data
, len
);
564 if ( !image
.LoadFile(mis
) )
565 wxLogError(wxT("Can't load BMP image from stream"));
567 my_horse_bmp2
= new wxBitmap( image
);
574 MyCanvas::~MyCanvas()
578 delete my_horse_jpeg
;
581 delete my_horse_bmp2
;
583 delete my_horse_tiff
;
585 delete my_horse_ico32
;
586 delete my_horse_ico16
;
589 delete [] my_horse_ani
;
595 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
597 wxPaintDC
dc( this );
600 dc
.DrawText( _T("Loaded image"), 30, 10 );
601 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
603 dc
.DrawText( _T("Drawn directly"), 150, 10 );
604 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
605 dc
.SetPen( *wxBLACK_PEN
);
606 dc
.DrawRectangle( 150, 30, 100, 100 );
607 dc
.SetBrush( *wxWHITE_BRUSH
);
608 dc
.DrawRectangle( 170, 50, 60, 60 );
610 if (my_anti
&& my_anti
->Ok())
611 dc
.DrawBitmap( *my_anti
, 280, 30 );
613 dc
.DrawText( _T("PNG handler"), 30, 135 );
614 if (my_horse_png
&& my_horse_png
->Ok())
616 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
617 wxRect
rect(0,0,100,100);
618 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
619 dc
.DrawText( _T("GetSubBitmap()"), 280, 190 );
620 dc
.DrawBitmap( sub
, 280, 210 );
623 dc
.DrawText( _T("JPEG handler"), 30, 365 );
624 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
625 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
627 dc
.DrawText( _T("GIF handler"), 30, 595 );
628 if (my_horse_gif
&& my_horse_gif
->Ok())
629 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
631 dc
.DrawText( _T("PCX handler"), 30, 825 );
632 if (my_horse_pcx
&& my_horse_pcx
->Ok())
633 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
635 dc
.DrawText( _T("BMP handler"), 30, 1055 );
636 if (my_horse_bmp
&& my_horse_bmp
->Ok())
637 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
639 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
640 if (my_horse_bmp2
&& my_horse_bmp2
->Ok())
641 dc
.DrawBitmap( *my_horse_bmp2
, 280, 1070 );
643 dc
.DrawText( _T("PNM handler"), 30, 1285 );
644 if (my_horse_pnm
&& my_horse_pnm
->Ok())
645 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
647 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
648 if (my_horse_tiff
&& my_horse_tiff
->Ok())
649 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
651 dc
.DrawText( _T("XPM handler"), 30, 1745 );
652 if (my_horse_xpm
&& my_horse_xpm
->Ok())
653 dc
.DrawBitmap( *my_horse_xpm
, 30, 1760 );
656 if (my_smile_xbm
&& my_smile_xbm
->Ok())
658 dc
.DrawText( _T("XBM bitmap"), 30, 1975 );
659 dc
.DrawText( _T("(green on red)"), 30, 1990 );
660 dc
.SetTextForeground( _T("GREEN") );
661 dc
.SetTextBackground( _T("RED") );
662 dc
.DrawBitmap( *my_smile_xbm
, 30, 2010 );
664 dc
.SetTextForeground( wxT("BLACK") );
665 dc
.DrawText( _T("After wxImage conversion"), 150, 1975 );
666 dc
.DrawText( _T("(red on white)"), 150, 1990 );
667 dc
.SetTextForeground( wxT("RED") );
668 wxImage i
= my_smile_xbm
->ConvertToImage();
669 i
.SetMaskColour( 255, 255, 255 );
671 wxRED_PEN
->GetColour().Red(),
672 wxRED_PEN
->GetColour().Green(),
673 wxRED_PEN
->GetColour().Blue() );
674 dc
.DrawBitmap( wxBitmap(i
), 150, 2010, TRUE
);
675 dc
.SetTextForeground( wxT("BLACK") );
679 wxBitmap
mono( 60,50,1 );
681 memdc
.SelectObject( mono
);
682 memdc
.SetPen( *wxBLACK_PEN
);
683 memdc
.SetBrush( *wxWHITE_BRUSH
);
684 memdc
.DrawRectangle( 0,0,60,50 );
685 memdc
.SetTextForeground( *wxBLACK
);
686 memdc
.DrawText( _T("Hi!"), 5, 5 );
687 memdc
.SetBrush( *wxBLACK_BRUSH
);
688 memdc
.DrawRectangle( 33,5,20,20 );
689 memdc
.SetPen( *wxRED_PEN
);
690 memdc
.DrawLine( 5, 42, 50, 42 );
691 memdc
.SelectObject( wxNullBitmap
);
695 dc
.DrawText( _T("Mono bitmap"), 30, 2095 );
696 dc
.DrawText( _T("(red on green)"), 30, 2110 );
697 dc
.SetTextForeground( wxT("RED") );
698 dc
.SetTextBackground( wxT("GREEN") );
699 dc
.DrawBitmap( mono
, 30, 2130 );
701 dc
.SetTextForeground( wxT("BLACK") );
702 dc
.DrawText( _T("After wxImage conversion"), 150, 2095 );
703 dc
.DrawText( _T("(red on white)"), 150, 2110 );
704 dc
.SetTextForeground( wxT("RED") );
705 wxImage i
= mono
.ConvertToImage();
706 i
.SetMaskColour( 255,255,255 );
708 wxRED_PEN
->GetColour().Red(),
709 wxRED_PEN
->GetColour().Green(),
710 wxRED_PEN
->GetColour().Blue() );
711 dc
.DrawBitmap( wxBitmap(i
), 150, 2130, TRUE
);
712 dc
.SetTextForeground( wxT("BLACK") );
715 dc
.DrawText(_T("XPM bitmap"), 30, 2230);
716 if ( m_bmpSmileXpm
.Ok() )
718 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, TRUE
);
721 dc
.DrawText(_T("XPM icon"), 150, 2230);
722 if ( m_iconSmileXpm
.Ok() )
724 dc
.DrawIcon(m_iconSmileXpm
, 150, 2250);
727 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
728 if (my_horse_ico32
&& my_horse_ico32
->Ok())
729 dc
.DrawBitmap( *my_horse_ico32
, 30, 2330, TRUE
);
731 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
732 if (my_horse_ico16
&& my_horse_ico16
->Ok())
733 dc
.DrawBitmap( *my_horse_ico16
, 230, 2330, TRUE
);
735 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
736 if (my_horse_ico
&& my_horse_ico
->Ok())
737 dc
.DrawBitmap( *my_horse_ico
, 430, 2330, TRUE
);
739 dc
.DrawText( _T("CUR handler"), 30, 2390 );
740 if (my_horse_cur
&& my_horse_cur
->Ok())
742 dc
.DrawBitmap( *my_horse_cur
, 30, 2420, TRUE
);
743 dc
.SetPen (*wxRED_PEN
);
744 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
745 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
747 dc
.DrawText( _T("ANI handler"), 230, 2390 );
749 for (i
=0; i
< m_ani_images
; i
++)
750 if (my_horse_ani
[i
].Ok())
752 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, TRUE
);
756 void MyCanvas::CreateAntiAliasedBitmap()
758 wxBitmap
bitmap( 300, 300 );
762 dc
.SelectObject( bitmap
);
766 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
767 dc
.SetTextForeground( wxT("RED") );
768 dc
.DrawText( _T("This is anti-aliased Text."), 20, 20 );
769 dc
.DrawText( _T("And a Rectangle."), 20, 60 );
771 dc
.SetBrush( *wxRED_BRUSH
);
772 dc
.SetPen( *wxTRANSPARENT_PEN
);
773 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
775 wxImage original
= bitmap
.ConvertToImage();
776 wxImage
anti( 150, 150 );
778 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
780 for (int y
= 1; y
< 149; y
++)
781 for (int x
= 1; x
< 149; x
++)
783 int red
= original
.GetRed( x
*2, y
*2 ) +
784 original
.GetRed( x
*2-1, y
*2 ) +
785 original
.GetRed( x
*2, y
*2+1 ) +
786 original
.GetRed( x
*2+1, y
*2+1 );
789 int green
= original
.GetGreen( x
*2, y
*2 ) +
790 original
.GetGreen( x
*2-1, y
*2 ) +
791 original
.GetGreen( x
*2, y
*2+1 ) +
792 original
.GetGreen( x
*2+1, y
*2+1 );
795 int blue
= original
.GetBlue( x
*2, y
*2 ) +
796 original
.GetBlue( x
*2-1, y
*2 ) +
797 original
.GetBlue( x
*2, y
*2+1 ) +
798 original
.GetBlue( x
*2+1, y
*2+1 );
800 anti
.SetRGB( x
, y
, red
, green
, blue
);
802 my_anti
= new wxBitmap(anti
);
815 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
817 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
818 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
819 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
820 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
821 #ifdef wxHAVE_RAW_BITMAP
822 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
826 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
827 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
828 #endif // wxUSE_CLIPBOARD
832 : wxFrame( (wxFrame
*)NULL
, -1, _T("wxImage sample"),
833 wxPoint(20,20), wxSize(470,360) )
835 wxMenuBar
*menu_bar
= new wxMenuBar();
837 wxMenu
*menuImage
= new wxMenu
;
838 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
840 #ifdef wxHAVE_RAW_BITMAP
841 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
843 menuImage
->AppendSeparator();
844 menuImage
->Append( ID_ABOUT
, _T("&About..."));
845 menuImage
->AppendSeparator();
846 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
847 menu_bar
->Append(menuImage
, _T("&Image"));
850 wxMenu
*menuClipboard
= new wxMenu
;
851 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
852 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
853 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
854 #endif // wxUSE_CLIPBOARD
856 SetMenuBar( menu_bar
);
859 int widths
[] = { -1, 100 };
860 SetStatusWidths( 2, widths
);
862 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
864 // 500 width * 2500 height
865 m_canvas
->SetScrollbars( 10, 10, 50, 250 );
868 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
873 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
875 (void)wxMessageBox( _T("wxImage demo\n")
876 _T("Robert Roebling (c) 1998,2000"),
877 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
880 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
882 wxString filename
= wxFileSelector(_T("Select image file"));
887 if ( !image
.LoadFile(filename
) )
889 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
894 (new MyImageFrame(this, wxBitmap(image
)))->Show();
897 #ifdef wxHAVE_RAW_BITMAP
899 void MyFrame::OnTestRawBitmap( wxCommandEvent
&event
)
901 (new MyRawBitmapFrame(this))->Show();
904 #endif // wxHAVE_RAW_BITMAP
908 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
910 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
911 dobjBmp
->SetBitmap(*m_canvas
->my_horse_png
);
913 wxTheClipboard
->Open();
915 if ( !wxTheClipboard
->SetData(dobjBmp
) )
917 wxLogError(_T("Failed to copy bitmap to clipboard"));
920 wxTheClipboard
->Close();
923 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
925 wxBitmapDataObject dobjBmp
;
927 wxTheClipboard
->Open();
928 if ( !wxTheClipboard
->GetData(dobjBmp
) )
930 wxLogMessage(_T("No bitmap data in the clipboard"));
934 (new MyImageFrame(this, dobjBmp
.GetBitmap()))->Show();
936 wxTheClipboard
->Close();
939 #endif // wxUSE_CLIPBOARD
941 //-----------------------------------------------------------------------------
943 //-----------------------------------------------------------------------------
948 wxImage::AddHandler( new wxPNGHandler
);
952 wxImage::AddHandler( new wxJPEGHandler
);
956 wxImage::AddHandler( new wxTIFFHandler
);
960 wxImage::AddHandler( new wxGIFHandler
);
964 wxImage::AddHandler( new wxPCXHandler
);
968 wxImage::AddHandler( new wxPNMHandler
);
972 wxImage::AddHandler( new wxXPMHandler
);
976 wxImage::AddHandler( new wxICOHandler
);
977 wxImage::AddHandler( new wxCURHandler
);
978 wxImage::AddHandler( new wxANIHandler
);
981 wxFrame
*frame
= new MyFrame();