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__)
36 #ifdef wxHAVE_RAW_BITMAP
37 #include "wx/rawbmp.h"
41 #if defined(__WXMAC__) || defined(__WXGTK__)
42 #define wxHAVE_RAW_BITMAP
43 #include "wx/rawbmp.h"
53 class MyCanvas
: public wxScrolledWindow
57 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
59 void OnPaint( wxPaintEvent
&event
);
60 void CreateAntiAliasedBitmap();
62 wxBitmap
*my_horse_png
;
63 wxBitmap
*my_horse_jpeg
;
64 wxBitmap
*my_horse_gif
;
65 wxBitmap
*my_horse_bmp
;
66 wxBitmap
*my_horse_bmp2
;
67 wxBitmap
*my_horse_pcx
;
68 wxBitmap
*my_horse_pnm
;
69 wxBitmap
*my_horse_tiff
;
70 wxBitmap
*my_horse_xpm
;
71 wxBitmap
*my_horse_ico32
;
72 wxBitmap
*my_horse_ico16
;
73 wxBitmap
*my_horse_ico
;
74 wxBitmap
*my_horse_cur
;
75 wxBitmap
*my_horse_ani
;
77 wxBitmap
*my_smile_xbm
;
85 wxBitmap m_bmpSmileXpm
;
86 wxIcon m_iconSmileXpm
;
89 DECLARE_DYNAMIC_CLASS(MyCanvas
)
94 const int nChoices
= 8 ;
95 static const wxString bppchoices
[nChoices
] =
101 _T("8 bpp greyscale"),
103 _T("8 bpp own palette"),
107 static const int bppvalues
[nChoices
] =
122 class MyFrame
: public wxFrame
127 void OnAbout( wxCommandEvent
&event
);
128 void OnNewFrame( wxCommandEvent
&event
);
129 #ifdef wxHAVE_RAW_BITMAP
130 void OnTestRawBitmap( wxCommandEvent
&event
);
131 #endif // wxHAVE_RAW_BITMAP
132 void OnQuit( wxCommandEvent
&event
);
135 void OnCopy(wxCommandEvent
& event
);
136 void OnPaste(wxCommandEvent
& event
);
137 #endif // wxUSE_CLIPBOARD
142 DECLARE_DYNAMIC_CLASS(MyFrame
)
143 DECLARE_EVENT_TABLE()
146 class MyImageFrame
: public wxFrame
149 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
150 : wxFrame(parent
, wxID_ANY
, _T("Double click to save"),
151 wxDefaultPosition
, wxDefaultSize
,
152 wxCAPTION
| wxSYSTEM_MENU
| wxCLOSE_BOX
),
155 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
158 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
160 // do nothing here to be able to see how transparent images are shown
163 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
165 wxPaintDC
dc( this );
166 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
169 void OnSave(wxMouseEvent
& WXUNUSED(event
))
171 wxImage image
= m_bitmap
.ConvertToImage();
173 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
178 if ( bppselection
== -1 )
184 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, bppvalues
[bppselection
]);
186 wxString deffilename
= bppchoices
[bppselection
];
187 deffilename
.Replace(wxT(" "), wxT("_"));
188 deffilename
+= wxT(".bmp");
189 wxString savefilename
= wxFileSelector( wxT("Save Image"),
192 (const wxChar
*)NULL
,
193 wxT("BMP files (*.bmp)|*.bmp|")
194 wxT("PNG files (*.png)|*.png|")
195 wxT("JPEG files (*.jpg)|*.jpg|")
196 wxT("GIF files (*.gif)|*.gif|")
197 wxT("TIFF files (*.tif)|*.tif|")
198 wxT("PCX files (*.pcx)|*.pcx|")
199 wxT("ICO files (*.ico)|*.ico|")
200 wxT("CUR files (*.cur)|*.cur"),
204 if ( savefilename
.empty() )
207 if ( image
.GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT
) == wxBMP_8BPP_PALETTE
)
209 unsigned char *cmap
= new unsigned char [256];
210 for ( int i
= 0; i
< 256; i
++ )
211 cmap
[i
] = (unsigned char)i
;
212 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
218 wxString extension
= savefilename
.AfterLast('.').Lower();
220 if (extension
== _T("cur"))
222 image
.Rescale(32,32);
223 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
224 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
225 // This shows how you can save an image with explicitly
226 // specified image format:
227 loaded
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
231 // This one guesses image format from filename extension
232 // (it may fail if the extension is not recognized):
233 loaded
= image
.SaveFile(savefilename
);
237 wxMessageBox(_T("No handler for this file type."),
238 _T("File was not saved"),
239 wxOK
|wxCENTRE
, this);
245 DECLARE_EVENT_TABLE()
248 #ifdef wxHAVE_RAW_BITMAP
250 #include "wx/rawbmp.h"
252 class MyRawBitmapFrame
: public wxFrame
259 REAL_SIZE
= SIZE
- 2*BORDER
262 MyRawBitmapFrame(wxFrame
*parent
)
263 : wxFrame(parent
, wxID_ANY
, _T("Raw bitmaps (how exciting)")),
264 m_bitmap(SIZE
, SIZE
, 32)
266 SetClientSize(SIZE
, SIZE
);
268 // another possibility: wxNativePixelData (don't forget to remove code
269 // setting alpha in the loop below then)
270 typedef wxAlphaPixelData Data
;
272 Data
data(m_bitmap
, wxPoint(BORDER
, BORDER
), wxSize(REAL_SIZE
, REAL_SIZE
));
275 wxLogError(_T("Failed to gain raw access to bitmap data"));
281 Data::Iterator
p(data
);
283 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
285 Data::Iterator rowStart
= p
;
287 int r
= y
< REAL_SIZE
/3 ? 255 : 0,
288 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ? 255 : 0,
289 b
= 2*(REAL_SIZE
/3) <= y
? 255 : 0;
291 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
296 p
.Alpha() = (Data::Iterator::ChannelType
)((x
*255.)/REAL_SIZE
);
298 ++p
; // same as p.OffsetX(1)
306 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
308 wxPaintDC
dc( this );
309 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER
);
310 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
311 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
312 dc
.DrawBitmap( m_bitmap
, 0, 0, true /* use mask */ );
318 DECLARE_EVENT_TABLE()
321 #endif // wxHAVE_RAW_BITMAP
325 class MyApp
: public wxApp
328 virtual bool OnInit();
337 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
339 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
340 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
341 EVT_PAINT(MyImageFrame::OnPaint
)
342 EVT_LEFT_DCLICK(MyImageFrame::OnSave
)
345 #ifdef wxHAVE_RAW_BITMAP
347 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
348 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
351 #endif // wxHAVE_RAW_BITMAP
353 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
354 EVT_PAINT(MyCanvas::OnPaint
)
357 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
358 const wxPoint
&pos
, const wxSize
&size
)
359 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
360 , m_bmpSmileXpm((const char **) smile_xpm
)
361 , m_iconSmileXpm((const char **) smile_xpm
)
363 my_horse_png
= (wxBitmap
*) NULL
;
364 my_horse_jpeg
= (wxBitmap
*) NULL
;
365 my_horse_gif
= (wxBitmap
*) NULL
;
366 my_horse_bmp
= (wxBitmap
*) NULL
;
367 my_horse_bmp2
= (wxBitmap
*) NULL
;
368 my_horse_pcx
= (wxBitmap
*) NULL
;
369 my_horse_pnm
= (wxBitmap
*) NULL
;
370 my_horse_tiff
= (wxBitmap
*) NULL
;
371 my_horse_xpm
= (wxBitmap
*) NULL
;
372 my_horse_ico32
= (wxBitmap
*) NULL
;
373 my_horse_ico16
= (wxBitmap
*) NULL
;
374 my_horse_ico
= (wxBitmap
*) NULL
;
375 my_horse_cur
= (wxBitmap
*) NULL
;
376 my_horse_ani
= (wxBitmap
*) NULL
;
378 my_smile_xbm
= (wxBitmap
*) NULL
;
379 my_square
= (wxBitmap
*) NULL
;
380 my_anti
= (wxBitmap
*) NULL
;
384 SetBackgroundColour(* wxWHITE
);
386 wxBitmap
bitmap( 100, 100 );
389 dc
.SelectObject( bitmap
);
390 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
391 dc
.SetPen( *wxBLACK_PEN
);
392 dc
.DrawRectangle( 0, 0, 100, 100 );
393 dc
.SetBrush( *wxWHITE_BRUSH
);
394 dc
.DrawRectangle( 20, 20, 60, 60 );
395 dc
.SelectObject( wxNullBitmap
);
397 // try to find the directory with our images
399 if ( wxFile::Exists(wxT("./horse.png")) )
401 else if ( wxFile::Exists(wxT("../horse.png")) )
404 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
406 wxImage image
= bitmap
.ConvertToImage();
409 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
410 wxLogError(wxT("Can't save file"));
414 if ( image
.LoadFile( dir
+ _T("test.png") ) )
415 my_square
= new wxBitmap( image
);
419 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
420 wxLogError(wxT("Can't load PNG image"));
422 my_horse_png
= new wxBitmap( image
);
423 #endif // wxUSE_LIBPNG
428 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
429 wxLogError(wxT("Can't load JPG image"));
431 my_horse_jpeg
= new wxBitmap( image
);
432 #endif // wxUSE_LIBJPEG
437 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
438 wxLogError(wxT("Can't load GIF image"));
440 my_horse_gif
= new wxBitmap( image
);
446 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
447 wxLogError(wxT("Can't load PCX image"));
449 my_horse_pcx
= new wxBitmap( image
);
454 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
455 wxLogError(wxT("Can't load BMP image"));
457 my_horse_bmp
= new wxBitmap( image
);
462 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
463 wxLogError(wxT("Can't load XPM image"));
465 my_horse_xpm
= new wxBitmap( image
);
467 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
468 wxLogError(wxT("Can't save file"));
474 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
475 wxLogError(wxT("Can't load PNM image"));
477 my_horse_pnm
= new wxBitmap( image
);
483 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
484 wxLogError(wxT("Can't load TIFF image"));
486 my_horse_tiff
= new wxBitmap( image
);
489 CreateAntiAliasedBitmap();
491 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
494 // demonstrates XPM automatically using the mask when saving
495 if ( m_bmpSmileXpm
.Ok() )
496 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 wxFileOffset len
= file
.Length();
558 size_t dataSize
= (size_t)len
;
559 void *data
= malloc(dataSize
);
560 if ( file
.Read(data
, dataSize
) != len
)
561 wxLogError(_T("Reading bitmap file failed"));
564 wxMemoryInputStream
mis(data
, dataSize
);
565 if ( !image
.LoadFile(mis
) )
566 wxLogError(wxT("Can't load BMP image from stream"));
568 my_horse_bmp2
= new wxBitmap( image
);
575 MyCanvas::~MyCanvas()
579 delete my_horse_jpeg
;
582 delete my_horse_bmp2
;
584 delete my_horse_tiff
;
586 delete my_horse_ico32
;
587 delete my_horse_ico16
;
590 delete [] my_horse_ani
;
596 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
598 wxPaintDC
dc( this );
601 dc
.DrawText( _T("Loaded image"), 30, 10 );
602 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
604 dc
.DrawText( _T("Drawn directly"), 150, 10 );
605 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
606 dc
.SetPen( *wxBLACK_PEN
);
607 dc
.DrawRectangle( 150, 30, 100, 100 );
608 dc
.SetBrush( *wxWHITE_BRUSH
);
609 dc
.DrawRectangle( 170, 50, 60, 60 );
611 if (my_anti
&& my_anti
->Ok())
612 dc
.DrawBitmap( *my_anti
, 280, 30 );
614 dc
.DrawText( _T("PNG handler"), 30, 135 );
615 if (my_horse_png
&& my_horse_png
->Ok())
617 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
618 wxRect
rect(0,0,100,100);
619 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
620 dc
.DrawText( _T("GetSubBitmap()"), 280, 190 );
621 dc
.DrawBitmap( sub
, 280, 210 );
624 dc
.DrawText( _T("JPEG handler"), 30, 365 );
625 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
626 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
628 dc
.DrawText( _T("GIF handler"), 30, 595 );
629 if (my_horse_gif
&& my_horse_gif
->Ok())
630 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
632 dc
.DrawText( _T("PCX handler"), 30, 825 );
633 if (my_horse_pcx
&& my_horse_pcx
->Ok())
634 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
636 dc
.DrawText( _T("BMP handler"), 30, 1055 );
637 if (my_horse_bmp
&& my_horse_bmp
->Ok())
638 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
640 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
641 if (my_horse_bmp2
&& my_horse_bmp2
->Ok())
642 dc
.DrawBitmap( *my_horse_bmp2
, 280, 1070 );
644 dc
.DrawText( _T("PNM handler"), 30, 1285 );
645 if (my_horse_pnm
&& my_horse_pnm
->Ok())
646 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
648 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
649 if (my_horse_tiff
&& my_horse_tiff
->Ok())
650 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
652 dc
.DrawText( _T("XPM handler"), 30, 1745 );
653 if (my_horse_xpm
&& my_horse_xpm
->Ok())
654 dc
.DrawBitmap( *my_horse_xpm
, 30, 1760 );
657 if (my_smile_xbm
&& my_smile_xbm
->Ok())
659 dc
.DrawText( _T("XBM bitmap"), 30, 1975 );
660 dc
.DrawText( _T("(green on red)"), 30, 1990 );
661 dc
.SetTextForeground( _T("GREEN") );
662 dc
.SetTextBackground( _T("RED") );
663 dc
.DrawBitmap( *my_smile_xbm
, 30, 2010 );
665 dc
.SetTextForeground( wxT("BLACK") );
666 dc
.DrawText( _T("After wxImage conversion"), 150, 1975 );
667 dc
.DrawText( _T("(red on white)"), 150, 1990 );
668 dc
.SetTextForeground( wxT("RED") );
669 wxImage i
= my_smile_xbm
->ConvertToImage();
670 i
.SetMaskColour( 255, 255, 255 );
672 wxRED_PEN
->GetColour().Red(),
673 wxRED_PEN
->GetColour().Green(),
674 wxRED_PEN
->GetColour().Blue() );
675 dc
.DrawBitmap( wxBitmap(i
), 150, 2010, true );
676 dc
.SetTextForeground( wxT("BLACK") );
680 wxBitmap
mono( 60,50,1 );
682 memdc
.SelectObject( mono
);
683 memdc
.SetPen( *wxBLACK_PEN
);
684 memdc
.SetBrush( *wxWHITE_BRUSH
);
685 memdc
.DrawRectangle( 0,0,60,50 );
686 memdc
.SetTextForeground( *wxBLACK
);
688 // I cannot convince GTK2 to draw into mono bitmaps
689 memdc
.DrawText( _T("Hi!"), 5, 5 );
691 memdc
.SetBrush( *wxBLACK_BRUSH
);
692 memdc
.DrawRectangle( 33,5,20,20 );
693 memdc
.SetPen( *wxRED_PEN
);
694 memdc
.DrawLine( 5, 42, 50, 42 );
695 memdc
.SelectObject( wxNullBitmap
);
699 dc
.DrawText( _T("Mono bitmap"), 30, 2095 );
700 dc
.DrawText( _T("(red on green)"), 30, 2110 );
701 dc
.SetTextForeground( wxT("RED") );
702 dc
.SetTextBackground( wxT("GREEN") );
703 dc
.DrawBitmap( mono
, 30, 2130 );
705 dc
.SetTextForeground( wxT("BLACK") );
706 dc
.DrawText( _T("After wxImage conversion"), 150, 2095 );
707 dc
.DrawText( _T("(red on white)"), 150, 2110 );
708 dc
.SetTextForeground( wxT("RED") );
709 wxImage i
= mono
.ConvertToImage();
710 i
.SetMaskColour( 255,255,255 );
712 wxRED_PEN
->GetColour().Red(),
713 wxRED_PEN
->GetColour().Green(),
714 wxRED_PEN
->GetColour().Blue() );
715 dc
.DrawBitmap( wxBitmap(i
), 150, 2130, true );
716 dc
.SetTextForeground( wxT("BLACK") );
719 // For testing transparency
720 dc
.SetBrush( *wxRED_BRUSH
);
721 dc
.DrawRectangle( 20, 2220, 560, 68 );
723 dc
.DrawText(_T("XPM bitmap"), 30, 2230 );
724 if ( m_bmpSmileXpm
.Ok() )
725 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, true);
727 dc
.DrawText(_T("XPM icon"), 110, 2230 );
728 if ( m_iconSmileXpm
.Ok() )
729 dc
.DrawIcon(m_iconSmileXpm
, 110, 2250);
731 // testing icon -> bitmap conversion
732 wxBitmap
to_blit( m_iconSmileXpm
);
735 dc
.DrawText( _T("SubBitmap"), 170, 2230 );
736 wxBitmap sub
= to_blit
.GetSubBitmap( wxRect(0,0,15,15) );
738 dc
.DrawBitmap( sub
, 170, 2250, true );
740 dc
.DrawText( _T("Enlarged"), 250, 2230 );
741 dc
.SetUserScale( 1.5, 1.5 );
742 dc
.DrawBitmap( to_blit
, (int)(250/1.5), (int)(2250/1.5), true );
743 dc
.SetUserScale( 2, 2 );
744 dc
.DrawBitmap( to_blit
, (int)(300/2), (int)(2250/2), true );
745 dc
.SetUserScale( 1.0, 1.0 );
747 dc
.DrawText( _T("Blit"), 400, 2230);
749 blit_dc
.SelectObject( to_blit
);
750 dc
.Blit( 400, 2250, to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
751 dc
.SetUserScale( 1.5, 1.5 );
752 dc
.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
753 dc
.SetUserScale( 2, 2 );
754 dc
.Blit( (int)(500/2), (int)(2250/2), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
755 dc
.SetUserScale( 1.0, 1.0 );
758 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
759 if (my_horse_ico32
&& my_horse_ico32
->Ok())
760 dc
.DrawBitmap( *my_horse_ico32
, 30, 2330, true );
762 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
763 if (my_horse_ico16
&& my_horse_ico16
->Ok())
764 dc
.DrawBitmap( *my_horse_ico16
, 230, 2330, true );
766 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
767 if (my_horse_ico
&& my_horse_ico
->Ok())
768 dc
.DrawBitmap( *my_horse_ico
, 430, 2330, true );
770 dc
.DrawText( _T("CUR handler"), 30, 2390 );
771 if (my_horse_cur
&& my_horse_cur
->Ok())
773 dc
.DrawBitmap( *my_horse_cur
, 30, 2420, true );
774 dc
.SetPen (*wxRED_PEN
);
775 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
776 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
778 dc
.DrawText( _T("ANI handler"), 230, 2390 );
780 for (i
=0; i
< m_ani_images
; i
++)
781 if (my_horse_ani
[i
].Ok())
783 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, true );
787 void MyCanvas::CreateAntiAliasedBitmap()
789 wxBitmap
bitmap( 300, 300 );
793 dc
.SelectObject( bitmap
);
797 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
798 dc
.SetTextForeground( wxT("RED") );
799 dc
.DrawText( _T("This is anti-aliased Text."), 20, 20 );
800 dc
.DrawText( _T("And a Rectangle."), 20, 60 );
802 dc
.SetBrush( *wxRED_BRUSH
);
803 dc
.SetPen( *wxTRANSPARENT_PEN
);
804 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
806 wxImage original
= bitmap
.ConvertToImage();
807 wxImage
anti( 150, 150 );
809 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
811 for (int y
= 1; y
< 149; y
++)
812 for (int x
= 1; x
< 149; x
++)
814 int red
= original
.GetRed( x
*2, y
*2 ) +
815 original
.GetRed( x
*2-1, y
*2 ) +
816 original
.GetRed( x
*2, y
*2+1 ) +
817 original
.GetRed( x
*2+1, y
*2+1 );
820 int green
= original
.GetGreen( x
*2, y
*2 ) +
821 original
.GetGreen( x
*2-1, y
*2 ) +
822 original
.GetGreen( x
*2, y
*2+1 ) +
823 original
.GetGreen( x
*2+1, y
*2+1 );
826 int blue
= original
.GetBlue( x
*2, y
*2 ) +
827 original
.GetBlue( x
*2-1, y
*2 ) +
828 original
.GetBlue( x
*2, y
*2+1 ) +
829 original
.GetBlue( x
*2+1, y
*2+1 );
831 anti
.SetRGB( x
, y
, (unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
833 my_anti
= new wxBitmap(anti
);
846 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
848 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
849 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
850 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
851 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
852 #ifdef wxHAVE_RAW_BITMAP
853 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
857 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
858 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
859 #endif // wxUSE_CLIPBOARD
863 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxImage sample"),
864 wxPoint(20,20), wxSize(470,360) )
866 wxMenuBar
*menu_bar
= new wxMenuBar();
868 wxMenu
*menuImage
= new wxMenu
;
869 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
871 #ifdef wxHAVE_RAW_BITMAP
872 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
874 menuImage
->AppendSeparator();
875 menuImage
->Append( ID_ABOUT
, _T("&About..."));
876 menuImage
->AppendSeparator();
877 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
878 menu_bar
->Append(menuImage
, _T("&Image"));
881 wxMenu
*menuClipboard
= new wxMenu
;
882 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
883 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
884 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
885 #endif // wxUSE_CLIPBOARD
887 SetMenuBar( menu_bar
);
891 int widths
[] = { -1, 100 };
892 SetStatusWidths( 2, widths
);
893 #endif // wxUSE_STATUSBAR
895 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
897 // 500 width * 2500 height
898 m_canvas
->SetScrollbars( 10, 10, 50, 250 );
901 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
906 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
908 (void)wxMessageBox( _T("wxImage demo\n")
909 _T("Robert Roebling (c) 1998,2000"),
910 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
913 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
915 wxString filename
= wxFileSelector(_T("Select image file"));
920 if ( !image
.LoadFile(filename
) )
922 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
927 (new MyImageFrame(this, wxBitmap(image
)))->Show();
930 #ifdef wxHAVE_RAW_BITMAP
932 void MyFrame::OnTestRawBitmap( wxCommandEvent
&WXUNUSED(event
) )
934 (new MyRawBitmapFrame(this))->Show();
937 #endif // wxHAVE_RAW_BITMAP
941 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
943 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
944 dobjBmp
->SetBitmap(*m_canvas
->my_horse_png
);
946 wxTheClipboard
->Open();
948 if ( !wxTheClipboard
->SetData(dobjBmp
) )
950 wxLogError(_T("Failed to copy bitmap to clipboard"));
953 wxTheClipboard
->Close();
956 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
958 wxBitmapDataObject dobjBmp
;
960 wxTheClipboard
->Open();
961 if ( !wxTheClipboard
->GetData(dobjBmp
) )
963 wxLogMessage(_T("No bitmap data in the clipboard"));
967 (new MyImageFrame(this, dobjBmp
.GetBitmap()))->Show();
969 wxTheClipboard
->Close();
972 #endif // wxUSE_CLIPBOARD
974 //-----------------------------------------------------------------------------
976 //-----------------------------------------------------------------------------
981 wxImage::AddHandler( new wxPNGHandler
);
985 wxImage::AddHandler( new wxJPEGHandler
);
989 wxImage::AddHandler( new wxTIFFHandler
);
993 wxImage::AddHandler( new wxGIFHandler
);
997 wxImage::AddHandler( new wxPCXHandler
);
1001 wxImage::AddHandler( new wxPNMHandler
);
1005 wxImage::AddHandler( new wxXPMHandler
);
1009 wxImage::AddHandler( new wxICOHandler
);
1010 wxImage::AddHandler( new wxCURHandler
);
1011 wxImage::AddHandler( new wxANIHandler
);
1014 wxFrame
*frame
= new MyFrame();
1015 frame
->Show( true );