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
37 #include "wx/rawbmp.h"
47 class MyCanvas
: public wxScrolledWindow
51 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
53 void OnPaint( wxPaintEvent
&event
);
54 void CreateAntiAliasedBitmap();
56 wxBitmap
*my_horse_png
;
57 wxBitmap
*my_horse_jpeg
;
58 wxBitmap
*my_horse_gif
;
59 wxBitmap
*my_horse_bmp
;
60 wxBitmap
*my_horse_bmp2
;
61 wxBitmap
*my_horse_pcx
;
62 wxBitmap
*my_horse_pnm
;
63 wxBitmap
*my_horse_tiff
;
64 wxBitmap
*my_horse_xpm
;
65 wxBitmap
*my_horse_ico32
;
66 wxBitmap
*my_horse_ico16
;
67 wxBitmap
*my_horse_ico
;
68 wxBitmap
*my_horse_cur
;
69 wxBitmap
*my_horse_ani
;
71 wxBitmap
*my_smile_xbm
;
79 wxBitmap m_bmpSmileXpm
;
80 wxIcon m_iconSmileXpm
;
83 DECLARE_DYNAMIC_CLASS(MyCanvas
)
88 const int nChoices
= 8 ;
89 static const wxString bppchoices
[nChoices
] =
95 _T("8 bpp greyscale"),
97 _T("8 bpp own palette"),
101 static const int bppvalues
[nChoices
] =
116 class MyFrame
: public wxFrame
121 void OnAbout( wxCommandEvent
&event
);
122 void OnNewFrame( wxCommandEvent
&event
);
123 #ifdef wxHAVE_RAW_BITMAP
124 void OnTestRawBitmap( wxCommandEvent
&event
);
125 #endif // wxHAVE_RAW_BITMAP
126 void OnQuit( wxCommandEvent
&event
);
129 void OnCopy(wxCommandEvent
& event
);
130 void OnPaste(wxCommandEvent
& event
);
131 #endif // wxUSE_CLIPBOARD
136 DECLARE_DYNAMIC_CLASS(MyFrame
)
137 DECLARE_EVENT_TABLE()
140 class MyImageFrame
: public wxFrame
143 MyImageFrame(wxFrame
*parent
, const wxBitmap
& bitmap
)
144 : wxFrame(parent
, -1, _T("Double click to save"),
145 wxDefaultPosition
, wxDefaultSize
,
146 wxCAPTION
| wxSYSTEM_MENU
| wxCLOSE_BOX
),
149 SetClientSize(bitmap
.GetWidth(), bitmap
.GetHeight());
152 void OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
154 // do nothing here to be able to see how transparent images are shown
157 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
159 wxPaintDC
dc( this );
160 dc
.DrawBitmap( m_bitmap
, 0, 0, TRUE
/* use mask */ );
163 void OnSave(wxMouseEvent
& WXUNUSED(event
))
165 wxImage image
= m_bitmap
.ConvertToImage();
167 int bppselection
= wxGetSingleChoiceIndex(_T("Set BMP BPP"),
172 if ( bppselection
== -1 )
178 image
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, bppvalues
[bppselection
]);
180 wxString deffilename
= bppchoices
[bppselection
];
181 deffilename
.Replace(wxT(" "), wxT("_"));
182 deffilename
+= wxT(".bmp");
183 wxString savefilename
= wxFileSelector( wxT("Save Image"),
186 (const wxChar
*)NULL
,
187 wxT("BMP files (*.bmp)|*.bmp|")
188 wxT("PNG files (*.png)|*.png|")
189 wxT("JPEG files (*.jpg)|*.jpg|")
190 wxT("GIF files (*.gif)|*.gif|")
191 wxT("TIFF files (*.tif)|*.tif|")
192 wxT("PCX files (*.pcx)|*.pcx|")
193 wxT("ICO files (*.ico)|*.ico|")
194 wxT("CUR files (*.cur)|*.cur"),
197 if ( savefilename
.empty() )
200 if ( image
.GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT
) == wxBMP_8BPP_PALETTE
)
202 unsigned char *cmap
= new unsigned char [256];
203 for ( int i
= 0; i
< 256; i
++ )
205 image
.SetPalette(wxPalette(256, cmap
, cmap
, cmap
));
211 wxString extension
= savefilename
.AfterLast('.').Lower();
213 if (extension
== _T("cur"))
215 image
.Rescale(32,32);
216 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, 0);
217 image
.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, 0);
218 // This shows how you can save an image with explicitly
219 // specified image format:
220 loaded
= image
.SaveFile(savefilename
, wxBITMAP_TYPE_CUR
);
224 // This one guesses image format from filename extension
225 // (it may fail if the extension is not recognized):
226 loaded
= image
.SaveFile(savefilename
);
230 wxMessageBox(_T("No handler for this file type."),
231 _T("File was not saved"),
232 wxOK
|wxCENTRE
, this);
238 DECLARE_EVENT_TABLE()
241 #ifdef wxHAVE_RAW_BITMAP
243 #include "wx/rawbmp.h"
245 class MyRawBitmapFrame
: public wxFrame
252 REAL_SIZE
= SIZE
- 2*BORDER
255 MyRawBitmapFrame(wxFrame
*parent
)
256 : wxFrame(parent
, -1, _T("Raw bitmaps (how exciting)")),
257 m_bitmap(SIZE
, SIZE
, 32)
259 SetClientSize(SIZE
, SIZE
);
261 wxAlphaPixelData
data(m_bitmap
,
262 wxPoint(BORDER
, BORDER
),
263 wxSize(REAL_SIZE
, REAL_SIZE
));
266 wxLogError(_T("Failed to gain raw access to bitmap data"));
272 wxAlphaPixelData::Iterator
p(data
);
274 for ( int y
= 0; y
< REAL_SIZE
; ++y
)
276 wxAlphaPixelData::Iterator 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
)
287 p
.Alpha() = (x
*255.)/REAL_SIZE
;
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 if ( 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 if ( file
.IsOpened() )
552 off_t len
= file
.Length();
553 void *data
= malloc(len
);
554 if ( file
.Read(data
, len
) != len
)
555 wxLogError(_T("Reading bitmap file failed"));
558 wxMemoryInputStream
mis(data
, len
);
559 if ( !image
.LoadFile(mis
) )
560 wxLogError(wxT("Can't load BMP image from stream"));
562 my_horse_bmp2
= new wxBitmap( image
);
569 MyCanvas::~MyCanvas()
573 delete my_horse_jpeg
;
576 delete my_horse_bmp2
;
578 delete my_horse_tiff
;
580 delete my_horse_ico32
;
581 delete my_horse_ico16
;
584 delete [] my_horse_ani
;
590 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
592 wxPaintDC
dc( this );
595 dc
.DrawText( _T("Loaded image"), 30, 10 );
596 if (my_square
&& my_square
->Ok()) dc
.DrawBitmap( *my_square
, 30, 30 );
598 dc
.DrawText( _T("Drawn directly"), 150, 10 );
599 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
600 dc
.SetPen( *wxBLACK_PEN
);
601 dc
.DrawRectangle( 150, 30, 100, 100 );
602 dc
.SetBrush( *wxWHITE_BRUSH
);
603 dc
.DrawRectangle( 170, 50, 60, 60 );
605 if (my_anti
&& my_anti
->Ok())
606 dc
.DrawBitmap( *my_anti
, 280, 30 );
608 dc
.DrawText( _T("PNG handler"), 30, 135 );
609 if (my_horse_png
&& my_horse_png
->Ok())
611 dc
.DrawBitmap( *my_horse_png
, 30, 150 );
612 wxRect
rect(0,0,100,100);
613 wxBitmap
sub( my_horse_png
->GetSubBitmap(rect
) );
614 dc
.DrawText( _T("GetSubBitmap()"), 280, 190 );
615 dc
.DrawBitmap( sub
, 280, 210 );
618 dc
.DrawText( _T("JPEG handler"), 30, 365 );
619 if (my_horse_jpeg
&& my_horse_jpeg
->Ok())
620 dc
.DrawBitmap( *my_horse_jpeg
, 30, 380 );
622 dc
.DrawText( _T("GIF handler"), 30, 595 );
623 if (my_horse_gif
&& my_horse_gif
->Ok())
624 dc
.DrawBitmap( *my_horse_gif
, 30, 610 );
626 dc
.DrawText( _T("PCX handler"), 30, 825 );
627 if (my_horse_pcx
&& my_horse_pcx
->Ok())
628 dc
.DrawBitmap( *my_horse_pcx
, 30, 840 );
630 dc
.DrawText( _T("BMP handler"), 30, 1055 );
631 if (my_horse_bmp
&& my_horse_bmp
->Ok())
632 dc
.DrawBitmap( *my_horse_bmp
, 30, 1070 );
634 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
635 if (my_horse_bmp2
&& my_horse_bmp2
->Ok())
636 dc
.DrawBitmap( *my_horse_bmp2
, 280, 1070 );
638 dc
.DrawText( _T("PNM handler"), 30, 1285 );
639 if (my_horse_pnm
&& my_horse_pnm
->Ok())
640 dc
.DrawBitmap( *my_horse_pnm
, 30, 1300 );
642 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
643 if (my_horse_tiff
&& my_horse_tiff
->Ok())
644 dc
.DrawBitmap( *my_horse_tiff
, 30, 1530 );
646 dc
.DrawText( _T("XPM handler"), 30, 1745 );
647 if (my_horse_xpm
&& my_horse_xpm
->Ok())
648 dc
.DrawBitmap( *my_horse_xpm
, 30, 1760 );
651 if (my_smile_xbm
&& my_smile_xbm
->Ok())
653 dc
.DrawText( _T("XBM bitmap"), 30, 1975 );
654 dc
.DrawText( _T("(green on red)"), 30, 1990 );
655 dc
.SetTextForeground( _T("GREEN") );
656 dc
.SetTextBackground( _T("RED") );
657 dc
.DrawBitmap( *my_smile_xbm
, 30, 2010 );
659 dc
.SetTextForeground( wxT("BLACK") );
660 dc
.DrawText( _T("After wxImage conversion"), 150, 1975 );
661 dc
.DrawText( _T("(red on white)"), 150, 1990 );
662 dc
.SetTextForeground( wxT("RED") );
663 wxImage i
= my_smile_xbm
->ConvertToImage();
664 i
.SetMaskColour( 255, 255, 255 );
666 wxRED_PEN
->GetColour().Red(),
667 wxRED_PEN
->GetColour().Green(),
668 wxRED_PEN
->GetColour().Blue() );
669 dc
.DrawBitmap( wxBitmap(i
), 150, 2010, TRUE
);
670 dc
.SetTextForeground( wxT("BLACK") );
674 wxBitmap
mono( 60,50,1 );
676 memdc
.SelectObject( mono
);
677 memdc
.SetPen( *wxBLACK_PEN
);
678 memdc
.SetBrush( *wxWHITE_BRUSH
);
679 memdc
.DrawRectangle( 0,0,60,50 );
680 memdc
.SetTextForeground( *wxBLACK
);
681 memdc
.DrawText( _T("Hi!"), 5, 5 );
682 memdc
.SetBrush( *wxBLACK_BRUSH
);
683 memdc
.DrawRectangle( 33,5,20,20 );
684 memdc
.SetPen( *wxRED_PEN
);
685 memdc
.DrawLine( 5, 42, 50, 42 );
686 memdc
.SelectObject( wxNullBitmap
);
690 dc
.DrawText( _T("Mono bitmap"), 30, 2095 );
691 dc
.DrawText( _T("(red on green)"), 30, 2110 );
692 dc
.SetTextForeground( wxT("RED") );
693 dc
.SetTextBackground( wxT("GREEN") );
694 dc
.DrawBitmap( mono
, 30, 2130 );
696 dc
.SetTextForeground( wxT("BLACK") );
697 dc
.DrawText( _T("After wxImage conversion"), 150, 2095 );
698 dc
.DrawText( _T("(red on white)"), 150, 2110 );
699 dc
.SetTextForeground( wxT("RED") );
700 wxImage i
= mono
.ConvertToImage();
701 i
.SetMaskColour( 255,255,255 );
703 wxRED_PEN
->GetColour().Red(),
704 wxRED_PEN
->GetColour().Green(),
705 wxRED_PEN
->GetColour().Blue() );
706 dc
.DrawBitmap( wxBitmap(i
), 150, 2130, TRUE
);
707 dc
.SetTextForeground( wxT("BLACK") );
710 dc
.DrawText(_T("XPM bitmap"), 30, 2230);
711 if ( m_bmpSmileXpm
.Ok() )
713 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, TRUE
);
716 dc
.DrawText(_T("XPM icon"), 150, 2230);
717 if ( m_iconSmileXpm
.Ok() )
719 dc
.DrawIcon(m_iconSmileXpm
, 150, 2250);
722 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
723 if (my_horse_ico32
&& my_horse_ico32
->Ok())
724 dc
.DrawBitmap( *my_horse_ico32
, 30, 2330, TRUE
);
726 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
727 if (my_horse_ico16
&& my_horse_ico16
->Ok())
728 dc
.DrawBitmap( *my_horse_ico16
, 230, 2330, TRUE
);
730 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
731 if (my_horse_ico
&& my_horse_ico
->Ok())
732 dc
.DrawBitmap( *my_horse_ico
, 430, 2330, TRUE
);
734 dc
.DrawText( _T("CUR handler"), 30, 2390 );
735 if (my_horse_cur
&& my_horse_cur
->Ok())
737 dc
.DrawBitmap( *my_horse_cur
, 30, 2420, TRUE
);
738 dc
.SetPen (*wxRED_PEN
);
739 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
740 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
742 dc
.DrawText( _T("ANI handler"), 230, 2390 );
744 for (i
=0; i
< m_ani_images
; i
++)
745 if (my_horse_ani
[i
].Ok())
747 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, TRUE
);
751 void MyCanvas::CreateAntiAliasedBitmap()
753 wxBitmap
bitmap( 300, 300 );
757 dc
.SelectObject( bitmap
);
761 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
762 dc
.SetTextForeground( wxT("RED") );
763 dc
.DrawText( _T("This is anti-aliased Text."), 20, 20 );
764 dc
.DrawText( _T("And a Rectangle."), 20, 60 );
766 dc
.SetBrush( *wxRED_BRUSH
);
767 dc
.SetPen( *wxTRANSPARENT_PEN
);
768 dc
.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
770 wxImage original
= bitmap
.ConvertToImage();
771 wxImage
anti( 150, 150 );
773 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
775 for (int y
= 1; y
< 149; y
++)
776 for (int x
= 1; x
< 149; x
++)
778 int red
= original
.GetRed( x
*2, y
*2 ) +
779 original
.GetRed( x
*2-1, y
*2 ) +
780 original
.GetRed( x
*2, y
*2+1 ) +
781 original
.GetRed( x
*2+1, y
*2+1 );
784 int green
= original
.GetGreen( x
*2, y
*2 ) +
785 original
.GetGreen( x
*2-1, y
*2 ) +
786 original
.GetGreen( x
*2, y
*2+1 ) +
787 original
.GetGreen( x
*2+1, y
*2+1 );
790 int blue
= original
.GetBlue( x
*2, y
*2 ) +
791 original
.GetBlue( x
*2-1, y
*2 ) +
792 original
.GetBlue( x
*2, y
*2+1 ) +
793 original
.GetBlue( x
*2+1, y
*2+1 );
795 anti
.SetRGB( x
, y
, red
, green
, blue
);
797 my_anti
= new wxBitmap(anti
);
810 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
812 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
813 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
814 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
815 EVT_MENU (ID_NEW
, MyFrame::OnNewFrame
)
816 #ifdef wxHAVE_RAW_BITMAP
817 EVT_MENU (ID_SHOWRAW
, MyFrame::OnTestRawBitmap
)
821 EVT_MENU(wxID_COPY
, MyFrame::OnCopy
)
822 EVT_MENU(wxID_PASTE
, MyFrame::OnPaste
)
823 #endif // wxUSE_CLIPBOARD
827 : wxFrame( (wxFrame
*)NULL
, -1, _T("wxImage sample"),
828 wxPoint(20,20), wxSize(470,360) )
830 wxMenuBar
*menu_bar
= new wxMenuBar();
832 wxMenu
*menuImage
= new wxMenu
;
833 menuImage
->Append( ID_NEW
, _T("&Show any image...\tCtrl-O"));
835 #ifdef wxHAVE_RAW_BITMAP
836 menuImage
->Append( ID_SHOWRAW
, _T("Test &raw bitmap...\tCtrl-R"));
838 menuImage
->AppendSeparator();
839 menuImage
->Append( ID_ABOUT
, _T("&About..."));
840 menuImage
->AppendSeparator();
841 menuImage
->Append( ID_QUIT
, _T("E&xit\tCtrl-Q"));
842 menu_bar
->Append(menuImage
, _T("&Image"));
845 wxMenu
*menuClipboard
= new wxMenu
;
846 menuClipboard
->Append(wxID_COPY
, _T("&Copy test image\tCtrl-C"));
847 menuClipboard
->Append(wxID_PASTE
, _T("&Paste image\tCtrl-V"));
848 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
849 #endif // wxUSE_CLIPBOARD
851 SetMenuBar( menu_bar
);
854 int widths
[] = { -1, 100 };
855 SetStatusWidths( 2, widths
);
857 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
859 // 500 width * 2500 height
860 m_canvas
->SetScrollbars( 10, 10, 50, 250 );
863 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
868 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
870 (void)wxMessageBox( _T("wxImage demo\n")
871 _T("Robert Roebling (c) 1998,2000"),
872 _T("About wxImage Demo"), wxICON_INFORMATION
| wxOK
);
875 void MyFrame::OnNewFrame( wxCommandEvent
&WXUNUSED(event
) )
877 wxString filename
= wxFileSelector(_T("Select image file"));
882 if ( !image
.LoadFile(filename
) )
884 wxLogError(_T("Couldn't load image from '%s'."), filename
.c_str());
889 (new MyImageFrame(this, wxBitmap(image
)))->Show();
892 #ifdef wxHAVE_RAW_BITMAP
894 void MyFrame::OnTestRawBitmap( wxCommandEvent
&event
)
896 (new MyRawBitmapFrame(this))->Show();
899 #endif // wxHAVE_RAW_BITMAP
903 void MyFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
905 wxBitmapDataObject
*dobjBmp
= new wxBitmapDataObject
;
906 dobjBmp
->SetBitmap(*m_canvas
->my_horse_png
);
908 wxTheClipboard
->Open();
910 if ( !wxTheClipboard
->SetData(dobjBmp
) )
912 wxLogError(_T("Failed to copy bitmap to clipboard"));
915 wxTheClipboard
->Close();
918 void MyFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
920 wxBitmapDataObject dobjBmp
;
922 wxTheClipboard
->Open();
923 if ( !wxTheClipboard
->GetData(dobjBmp
) )
925 wxLogMessage(_T("No bitmap data in the clipboard"));
929 (new MyImageFrame(this, dobjBmp
.GetBitmap()))->Show();
931 wxTheClipboard
->Close();
934 #endif // wxUSE_CLIPBOARD
936 //-----------------------------------------------------------------------------
938 //-----------------------------------------------------------------------------
943 wxImage::AddHandler( new wxPNGHandler
);
947 wxImage::AddHandler( new wxJPEGHandler
);
951 wxImage::AddHandler( new wxTIFFHandler
);
955 wxImage::AddHandler( new wxGIFHandler
);
959 wxImage::AddHandler( new wxPCXHandler
);
963 wxImage::AddHandler( new wxPNMHandler
);
967 wxImage::AddHandler( new wxXPMHandler
);
971 wxImage::AddHandler( new wxICOHandler
);
972 wxImage::AddHandler( new wxCURHandler
);
973 wxImage::AddHandler( new wxANIHandler
);
976 wxFrame
*frame
= new MyFrame();