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__)
39 #define wxHAVE_RAW_BITMAP
49 class MyCanvas
: public wxScrolledWindow
53 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
55 void OnPaint( wxPaintEvent
&event
);
56 void CreateAntiAliasedBitmap();
58 wxBitmap
*my_horse_png
;
59 wxBitmap
*my_horse_jpeg
;
60 wxBitmap
*my_horse_gif
;
61 wxBitmap
*my_horse_bmp
;
62 wxBitmap
*my_horse_bmp2
;
63 wxBitmap
*my_horse_pcx
;
64 wxBitmap
*my_horse_pnm
;
65 wxBitmap
*my_horse_tiff
;
66 wxBitmap
*my_horse_xpm
;
67 wxBitmap
*my_horse_ico32
;
68 wxBitmap
*my_horse_ico16
;
69 wxBitmap
*my_horse_ico
;
70 wxBitmap
*my_horse_cur
;
71 wxBitmap
*my_horse_ani
;
73 wxBitmap
*my_smile_xbm
;
81 wxBitmap m_bmpSmileXpm
;
82 wxIcon m_iconSmileXpm
;
85 DECLARE_DYNAMIC_CLASS(MyCanvas
)
90 const int nChoices
= 8 ;
91 static const wxString bppchoices
[nChoices
] =
97 _T("8 bpp greyscale"),
99 _T("8 bpp own palette"),
103 static const int bppvalues
[nChoices
] =
118 class MyFrame
: public wxFrame
123 void OnAbout( wxCommandEvent
&event
);
124 void OnNewFrame( wxCommandEvent
&event
);
125 #ifdef wxHAVE_RAW_BITMAP
126 void OnTestRawBitmap( wxCommandEvent
&event
);
127 #endif // wxHAVE_RAW_BITMAP
128 void OnQuit( wxCommandEvent
&event
);
131 void OnCopy(wxCommandEvent
& event
);
132 void OnPaste(wxCommandEvent
& event
);
133 #endif // wxUSE_CLIPBOARD
138 DECLARE_DYNAMIC_CLASS(MyFrame
)
139 DECLARE_EVENT_TABLE()
142 class MyImageFrame
: public wxFrame
145 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
146 : wxFrame(parent
, -1, _T("Double click to save"),
147 wxDefaultPosition
, wxDefaultSize
,
148 wxCAPTION
| wxSYSTEM_MENU
| wxCLOSE_BOX
),
151 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
154 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
156 // do nothing here to be able to see how transparent images are shown
159 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
161 wxPaintDC
dc( this );
162 dc
.DrawBitmap( m_bitmap
, 0, 0, TRUE
/* use mask */ );
165 void OnSave(wxMouseEvent
& WXUNUSED(event
))
167 wxImage image
= m_bitmap
.ConvertToImage();
169 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
174 if ( bppselection
== -1 )
180 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, bppvalues
[bppselection
]);
182 wxString deffilename
= bppchoices
[bppselection
];
183 deffilename
.Replace(wxT(" "), wxT("_"));
184 deffilename
+= wxT(".bmp");
185 wxString savefilename
= wxFileSelector( wxT("Save Image"),
188 (const wxChar
*)NULL
,
189 wxT("BMP files (*.bmp)|*.bmp|")
190 wxT("PNG files (*.png)|*.png|")
191 wxT("JPEG files (*.jpg)|*.jpg|")
192 wxT("GIF files (*.gif)|*.gif|")
193 wxT("TIFF files (*.tif)|*.tif|")
194 wxT("PCX files (*.pcx)|*.pcx|")
195 wxT("ICO files (*.ico)|*.ico|")
196 wxT("CUR files (*.cur)|*.cur"),
199 if ( savefilename
.empty() )
202 if ( image
.GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT
) == wxBMP_8BPP_PALETTE
)
204 unsigned char *cmap
= new unsigned char [256];
205 for ( int i
= 0; i
< 256; i
++ )
207 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
213 wxString extension
= savefilename
.AfterLast('.').Lower();
215 if (extension
== _T("cur"))
217 image
.Rescale(32,32);
218 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
219 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
220 // This shows how you can save an image with explicitly
221 // specified image format:
222 loaded
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
226 // This one guesses image format from filename extension
227 // (it may fail if the extension is not recognized):
228 loaded
= image
.SaveFile(savefilename
);
232 wxMessageBox(_T("No handler for this file type."),
233 _T("File was not saved"),
234 wxOK
|wxCENTRE
, this);
240 DECLARE_EVENT_TABLE()
243 #ifdef wxHAVE_RAW_BITMAP
245 #include "wx/rawbmp.h"
247 class MyRawBitmapFrame
: public wxFrame
254 REAL_SIZE
= SIZE
- 2*BORDER
257 MyRawBitmapFrame(wxFrame
*parent
)
258 : wxFrame(parent
, -1, _T("Raw bitmaps (how exciting)")),
259 m_bitmap(SIZE
, SIZE
, 32)
261 SetClientSize(SIZE
, SIZE
);
263 wxRawBitmapData
data(m_bitmap
);
266 wxLogError(_T("Failed to gain raw access to bitmap data"));
270 wxRawBitmapIterator
p(data
);
272 p
.Offset(BORDER
, BORDER
);
274 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
276 wxRawBitmapIterator rowStart
= p
;
278 int r
= y
< REAL_SIZE
/3 ? 255 : 0,
279 g
= (REAL_SIZE
/3 <= y
) && (y
< 2*(REAL_SIZE
/3)) ? 255 : 0,
280 b
= 2*(REAL_SIZE
/3) <= y
? 255 : 0;
282 for ( int x
= 0; x
< REAL_SIZE
; ++x
)
289 ++p
; // same as p.OffsetX(1)
297 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
299 wxPaintDC
dc( this );
300 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER
);
301 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
/2 - BORDER
);
302 dc
.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE
- 2*BORDER
);
303 dc
.DrawBitmap( m_bitmap
, 0, 0, TRUE
/* use mask */ );
309 DECLARE_EVENT_TABLE()
312 #endif // wxHAVE_RAW_BITMAP
316 class MyApp
: public wxApp
319 virtual bool OnInit();
328 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
330 BEGIN_EVENT_TABLE(MyImageFrame
, wxFrame
)
331 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground
)
332 EVT_PAINT(MyImageFrame::OnPaint
)
333 EVT_LEFT_DCLICK(MyImageFrame::OnSave
)
336 #ifdef wxHAVE_RAW_BITMAP
338 BEGIN_EVENT_TABLE(MyRawBitmapFrame
, wxFrame
)
339 EVT_PAINT(MyRawBitmapFrame::OnPaint
)
342 #endif // wxHAVE_RAW_BITMAP
344 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
345 EVT_PAINT(MyCanvas::OnPaint
)
348 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
349 const wxPoint
&pos
, const wxSize
&size
)
350 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
351 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
352 , m_bmpSmileXpm((const char **) smile_xpm
)
353 , m_iconSmileXpm((const char **) smile_xpm
)
356 my_horse_png
= (wxBitmap
*) NULL
;
357 my_horse_jpeg
= (wxBitmap
*) NULL
;
358 my_horse_gif
= (wxBitmap
*) NULL
;
359 my_horse_bmp
= (wxBitmap
*) NULL
;
360 my_horse_bmp2
= (wxBitmap
*) NULL
;
361 my_horse_pcx
= (wxBitmap
*) NULL
;
362 my_horse_pnm
= (wxBitmap
*) NULL
;
363 my_horse_tiff
= (wxBitmap
*) NULL
;
364 my_horse_xpm
= (wxBitmap
*) NULL
;
365 my_horse_ico32
= (wxBitmap
*) NULL
;
366 my_horse_ico16
= (wxBitmap
*) NULL
;
367 my_horse_ico
= (wxBitmap
*) NULL
;
368 my_horse_cur
= (wxBitmap
*) NULL
;
369 my_horse_ani
= (wxBitmap
*) NULL
;
371 my_smile_xbm
= (wxBitmap
*) NULL
;
372 my_square
= (wxBitmap
*) NULL
;
373 my_anti
= (wxBitmap
*) NULL
;
377 SetBackgroundColour(* wxWHITE
);
379 wxBitmap
bitmap( 100, 100 );
382 dc
.SelectObject( bitmap
);
383 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
384 dc
.SetPen( *wxBLACK_PEN
);
385 dc
.DrawRectangle( 0, 0, 100, 100 );
386 dc
.SetBrush( *wxWHITE_BRUSH
);
387 dc
.DrawRectangle( 20, 20, 60, 60 );
388 dc
.SelectObject( wxNullBitmap
);
390 // try to find the directory with our images
392 if ( wxFile::Exists(wxT("./horse.png")) )
394 else if ( wxFile::Exists(wxT("../horse.png")) )
397 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
399 wxImage image
= bitmap
.ConvertToImage();
402 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
403 wxLogError(wxT("Can't save file"));
407 image
.LoadFile( dir
+ _T("test.png") );
408 my_square
= new wxBitmap( image
);
412 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
413 wxLogError(wxT("Can't load PNG image"));
415 my_horse_png
= new wxBitmap( image
);
416 #endif // wxUSE_LIBPNG
421 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
422 wxLogError(wxT("Can't load JPG image"));
424 my_horse_jpeg
= new wxBitmap( image
);
425 #endif // wxUSE_LIBJPEG
430 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
431 wxLogError(wxT("Can't load GIF image"));
433 my_horse_gif
= new wxBitmap( image
);
439 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
440 wxLogError(wxT("Can't load PCX image"));
442 my_horse_pcx
= new wxBitmap( image
);
447 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
448 wxLogError(wxT("Can't load BMP image"));
450 my_horse_bmp
= new wxBitmap( image
);
455 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
456 wxLogError(wxT("Can't load XPM image"));
458 my_horse_xpm
= new wxBitmap( image
);
460 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
461 wxLogError(wxT("Can't save file"));
467 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
468 wxLogError(wxT("Can't load PNM image"));
470 my_horse_pnm
= new wxBitmap( image
);
476 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
477 wxLogError(wxT("Can't load TIFF image"));
479 my_horse_tiff
= new wxBitmap( image
);
482 CreateAntiAliasedBitmap();
484 my_smile_xbm
= new wxBitmap( (const char*)smile_bits
, smile_width
,
487 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
488 // demonstrates XPM automatically using the mask when saving
489 if ( m_bmpSmileXpm
.Ok() )
490 m_bmpSmileXpm
.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM
);
496 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
497 wxLogError(wxT("Can't load first ICO image"));
499 my_horse_ico32
= new wxBitmap( image
);
503 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
504 wxLogError(wxT("Can't load second ICO image"));
506 my_horse_ico16
= new wxBitmap( image
);
510 if ( !image
.LoadFile( dir
+ _T("horse.ico") ) )
511 wxLogError(wxT("Can't load best ICO image"));
513 my_horse_ico
= new wxBitmap( image
);
517 if ( !image
.LoadFile( dir
+ _T("horse.cur"), wxBITMAP_TYPE_CUR
) )
518 wxLogError(wxT("Can't load best ICO image"));
521 my_horse_cur
= new wxBitmap( image
);
522 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
523 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
526 m_ani_images
= wxImage::GetImageCount ( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
);
528 wxLogError(wxT("No ANI-format images found"));
530 my_horse_ani
= new wxBitmap
[m_ani_images
];
532 for (i
=0; i
< m_ani_images
; i
++)
535 if (!image
.LoadFile( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
, i
))
537 wxString tmp
= wxT("Can't load image number ");
542 my_horse_ani
[i
] = wxBitmap( image
);
544 #endif // wxUSE_ICO_CUR
548 // test image loading from stream
549 wxFile
file(dir
+ _T("horse.bmp"));
550 off_t len
= file
.Length();
551 void *data
= malloc(len
);
552 if ( file
.Read(data
, len
) != len
)
553 wxLogError(_T("Reading bitmap file failed"));
556 wxMemoryInputStream
mis(data
, len
);
557 if ( !image
.LoadFile(mis
) )
558 wxLogError(wxT("Can't load BMP image from stream"));
560 my_horse_bmp2
= new wxBitmap( image
);
566 MyCanvas::~MyCanvas()
570 delete my_horse_jpeg
;
573 delete my_horse_bmp2
;
575 delete my_horse_tiff
;
577 delete my_horse_ico32
;
578 delete my_horse_ico16
;
581 delete [] my_horse_ani
;
587 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
589 wxPaintDC
dc( this );
592 dc
.DrawText( _T("Loaded image"), 30, 10 );
593 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
595 dc
.DrawText( _T("Drawn directly"), 150, 10 );
596 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
597 dc
.SetPen( *wxBLACK_PEN
);
598 dc
.DrawRectangle( 150, 30, 100, 100 );
599 dc
.SetBrush( *wxWHITE_BRUSH
);
600 dc
.DrawRectangle( 170, 50, 60, 60 );
602 if (my_anti
&& my_anti
->Ok())
603 dc
.DrawBitmap( *my_anti
, 280, 30 );
605 dc
.DrawText( _T("PNG handler"), 30, 135 );
606 if (my_horse_png
&& my_horse_png
->Ok())
608 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
609 wxRect
rect(0,0,100,100);
610 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
611 dc
.DrawText( _T("GetSubBitmap()"), 280, 190 );
612 dc
.DrawBitmap( sub
, 280, 210 );
615 dc
.DrawText( _T("JPEG handler"), 30, 365 );
616 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
617 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
619 dc
.DrawText( _T("GIF handler"), 30, 595 );
620 if (my_horse_gif
&& my_horse_gif
->Ok())
621 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
623 dc
.DrawText( _T("PCX handler"), 30, 825 );
624 if (my_horse_pcx
&& my_horse_pcx
->Ok())
625 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
627 dc
.DrawText( _T("BMP handler"), 30, 1055 );
628 if (my_horse_bmp
&& my_horse_bmp
->Ok())
629 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
631 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
632 if (my_horse_bmp2
&& my_horse_bmp2
->Ok())
633 dc
.DrawBitmap( *my_horse_bmp2
, 280, 1070 );
635 dc
.DrawText( _T("PNM handler"), 30, 1285 );
636 if (my_horse_pnm
&& my_horse_pnm
->Ok())
637 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
639 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
640 if (my_horse_tiff
&& my_horse_tiff
->Ok())
641 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
643 dc
.DrawText( _T("XPM handler"), 30, 1745 );
644 if (my_horse_xpm
&& my_horse_xpm
->Ok())
645 dc
.DrawBitmap( *my_horse_xpm
, 30, 1760 );
648 if (my_smile_xbm
&& my_smile_xbm
->Ok())
650 dc
.DrawText( _T("XBM bitmap"), 30, 1975 );
651 dc
.DrawText( _T("(green on red)"), 30, 1990 );
652 dc
.SetTextForeground( _T("GREEN") );
653 dc
.SetTextBackground( _T("RED") );
654 dc
.DrawBitmap( *my_smile_xbm
, 30, 2010 );
656 dc
.SetTextForeground( wxT("BLACK") );
657 dc
.DrawText( _T("After wxImage conversion"), 150, 1975 );
658 dc
.DrawText( _T("(red on white)"), 150, 1990 );
659 dc
.SetTextForeground( wxT("RED") );
660 wxImage i
= my_smile_xbm
->ConvertToImage();
661 i
.SetMaskColour( 255, 255, 255 );
663 wxRED_PEN
->GetColour().Red(),
664 wxRED_PEN
->GetColour().Green(),
665 wxRED_PEN
->GetColour().Blue() );
666 dc
.DrawBitmap( wxBitmap(i
), 150, 2010, TRUE
);
667 dc
.SetTextForeground( wxT("BLACK") );
671 wxBitmap
mono( 60,50,1 );
673 memdc
.SelectObject( mono
);
674 memdc
.SetPen( *wxBLACK_PEN
);
675 memdc
.SetBrush( *wxWHITE_BRUSH
);
676 memdc
.DrawRectangle( 0,0,60,50 );
677 memdc
.SetTextForeground( *wxBLACK
);
678 memdc
.DrawText( _T("Hi!"), 5, 5 );
679 memdc
.SetBrush( *wxBLACK_BRUSH
);
680 memdc
.DrawRectangle( 33,5,20,20 );
681 memdc
.SetPen( *wxRED_PEN
);
682 memdc
.DrawLine( 5, 42, 50, 42 );
683 memdc
.SelectObject( wxNullBitmap
);
687 dc
.DrawText( _T("Mono bitmap"), 30, 2095 );
688 dc
.DrawText( _T("(red on green)"), 30, 2110 );
689 dc
.SetTextForeground( wxT("RED") );
690 dc
.SetTextBackground( wxT("GREEN") );
691 dc
.DrawBitmap( mono
, 30, 2130 );
693 dc
.SetTextForeground( wxT("BLACK") );
694 dc
.DrawText( _T("After wxImage conversion"), 150, 2095 );
695 dc
.DrawText( _T("(red on white)"), 150, 2110 );
696 dc
.SetTextForeground( wxT("RED") );
697 wxImage i
= mono
.ConvertToImage();
698 i
.SetMaskColour( 255,255,255 );
700 wxRED_PEN
->GetColour().Red(),
701 wxRED_PEN
->GetColour().Green(),
702 wxRED_PEN
->GetColour().Blue() );
703 dc
.DrawBitmap( wxBitmap(i
), 150, 2130, TRUE
);
704 dc
.SetTextForeground( wxT("BLACK") );
707 dc
.DrawText(_T("XPM bitmap"), 30, 2230);
708 if ( m_bmpSmileXpm
.Ok() )
710 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, TRUE
);
713 dc
.DrawText(_T("XPM icon"), 150, 2230);
714 if ( m_iconSmileXpm
.Ok() )
716 dc
.DrawIcon(m_iconSmileXpm
, 150, 2250);
719 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
720 if (my_horse_ico32
&& my_horse_ico32
->Ok())
721 dc
.DrawBitmap( *my_horse_ico32
, 30, 2330, TRUE
);
723 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
724 if (my_horse_ico16
&& my_horse_ico16
->Ok())
725 dc
.DrawBitmap( *my_horse_ico16
, 230, 2330, TRUE
);
727 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
728 if (my_horse_ico
&& my_horse_ico
->Ok())
729 dc
.DrawBitmap( *my_horse_ico
, 430, 2330, TRUE
);
731 dc
.DrawText( _T("CUR handler"), 30, 2390 );
732 if (my_horse_cur
&& my_horse_cur
->Ok())
734 dc
.DrawBitmap( *my_horse_cur
, 30, 2420, TRUE
);
735 dc
.SetPen (*wxRED_PEN
);
736 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
737 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
739 dc
.DrawText( _T("ANI handler"), 230, 2390 );
741 for (i
=0; i
< m_ani_images
; i
++)
742 if (my_horse_ani
[i
].Ok())
744 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, TRUE
);
748 void MyCanvas::CreateAntiAliasedBitmap()
750 wxBitmap
bitmap( 300, 300 );
754 dc
.SelectObject( bitmap
);
758 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
759 dc
.SetTextForeground( wxT("RED") );
760 dc
.DrawText( _T("This is anti-aliased Text."), 20, 20 );
761 dc
.DrawText( _T("And a Rectangle."), 20, 60 );
763 dc
.SetBrush( *wxRED_BRUSH
);
764 dc
.SetPen( *wxTRANSPARENT_PEN
);
765 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
767 wxImage original
= bitmap
.ConvertToImage();
768 wxImage
anti( 150, 150 );
770 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
772 for (int y
= 1; y
< 149; y
++)
773 for (int x
= 1; x
< 149; x
++)
775 int red
= original
.GetRed( x
*2, y
*2 ) +
776 original
.GetRed( x
*2-1, y
*2 ) +
777 original
.GetRed( x
*2, y
*2+1 ) +
778 original
.GetRed( x
*2+1, y
*2+1 );
781 int green
= original
.GetGreen( x
*2, y
*2 ) +
782 original
.GetGreen( x
*2-1, y
*2 ) +
783 original
.GetGreen( x
*2, y
*2+1 ) +
784 original
.GetGreen( x
*2+1, y
*2+1 );
787 int blue
= original
.GetBlue( x
*2, y
*2 ) +
788 original
.GetBlue( x
*2-1, y
*2 ) +
789 original
.GetBlue( x
*2, y
*2+1 ) +
790 original
.GetBlue( x
*2+1, y
*2+1 );
792 anti
.SetRGB( x
, y
, red
, green
, blue
);
794 my_anti
= new wxBitmap(anti
);
807 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
809 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
810 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
811 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
812 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
813 #ifdef wxHAVE_RAW_BITMAP
814 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
818 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
819 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
820 #endif // wxUSE_CLIPBOARD
824 : wxFrame( (wxFrame
*)NULL
, -1, _T("wxImage sample"),
825 wxPoint(20,20), wxSize(470,360) )
827 wxMenuBar
*menu_bar
= new wxMenuBar();
829 wxMenu
*menuImage
= new wxMenu
;
830 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
831 #ifdef wxHAVE_RAW_BITMAP
832 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
834 menuImage
->AppendSeparator();
835 menuImage
->Append( ID_ABOUT
, _T("&About..."));
836 menuImage
->AppendSeparator();
837 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
838 menu_bar
->Append(menuImage
, _T("&Image"));
841 wxMenu
*menuClipboard
= new wxMenu
;
842 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
843 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
844 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
845 #endif // wxUSE_CLIPBOARD
847 SetMenuBar( menu_bar
);
850 int widths
[] = { -1, 100 };
851 SetStatusWidths( 2, widths
);
853 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
855 // 500 width * 2500 height
856 m_canvas
->SetScrollbars( 10, 10, 50, 250 );
859 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
864 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
866 (void)wxMessageBox( _T("wxImage demo\n")
867 _T("Robert Roebling (c) 1998,2000"),
868 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
871 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
873 wxString filename
= wxFileSelector(_T("Select image file"));
878 if ( !image
.LoadFile(filename
) )
880 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
885 (new MyImageFrame(this, wxBitmap(image
)))->Show();
888 #ifdef wxHAVE_RAW_BITMAP
890 void MyFrame::OnTestRawBitmap( wxCommandEvent
&event
)
892 (new MyRawBitmapFrame(this))->Show();
895 #endif // wxHAVE_RAW_BITMAP
899 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
901 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
902 dobjBmp
->SetBitmap(*m_canvas
->my_horse_png
);
904 if ( !wxTheClipboard
->SetData(dobjBmp
) )
906 wxLogError(_T("Failed to copy bitmap to clipboard"));
910 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
912 wxBitmapDataObject dobjBmp
;
913 if ( !wxTheClipboard
->GetData(dobjBmp
) )
915 wxLogMessage(_T("No bitmap data in the clipboard"));
919 (new MyImageFrame(this, dobjBmp
.GetBitmap()))->Show();
923 #endif // wxUSE_CLIPBOARD
925 //-----------------------------------------------------------------------------
927 //-----------------------------------------------------------------------------
932 wxImage::AddHandler( new wxPNGHandler
);
936 wxImage::AddHandler( new wxJPEGHandler
);
940 wxImage::AddHandler( new wxTIFFHandler
);
944 wxImage::AddHandler( new wxGIFHandler
);
948 wxImage::AddHandler( new wxPCXHandler
);
952 wxImage::AddHandler( new wxPNMHandler
);
956 wxImage::AddHandler( new wxXPMHandler
);
960 wxImage::AddHandler( new wxICOHandler
);
961 wxImage::AddHandler( new wxCURHandler
);
962 wxImage::AddHandler( new wxANIHandler
);
965 wxFrame
*frame
= new MyFrame();