]> git.saurik.com Git - wxWidgets.git/blame - samples/image/image.cpp
Tcl regex lib
[wxWidgets.git] / samples / image / image.cpp
CommitLineData
01111366
RR
1/*
2 * Program: image
1d5b7a0b 3 *
01111366
RR
4 * Author: Robert Roebling
5 *
6 * Copyright: (C) 1998, Robert Roebling
7 *
8 */
9
3d05544e
JS
10// For compilers that support precompilation, includes "wx/wx.h".
11#include "wx/wxprec.h"
12
13#ifdef __BORLANDC__
14#pragma hdrstop
15#endif
16
17#ifndef WX_PRECOMP
01111366 18#include "wx/wx.h"
3d05544e
JS
19#endif
20
01111366 21#include "wx/image.h"
a23fd0e1 22#include "wx/file.h"
a8d4e3ac
VS
23#include "wx/mstream.h"
24#include "wx/wfstream.h"
1971d23c 25#include "wx/quantize.h"
a23fd0e1 26
dc1dbfc6 27#include "smile.xbm"
bea56879 28#include "smile.xpm"
dc1dbfc6 29
a8d4e3ac 30
01111366
RR
31// derived classes
32
33class MyFrame;
34class MyApp;
35
36// MyCanvas
37
38class MyCanvas: public wxScrolledWindow
39{
1d5b7a0b 40public:
51b07644 41 MyCanvas() {}
01111366 42 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
1d5b7a0b 43 ~MyCanvas();
01111366 44 void OnPaint( wxPaintEvent &event );
9390a202 45 void CreateAntiAliasedBitmap();
1d5b7a0b 46
e1929140
RR
47 wxBitmap *my_horse_png;
48 wxBitmap *my_horse_jpeg;
49 wxBitmap *my_horse_gif;
ca26177c 50 wxBitmap *my_horse_bmp;
51b07644 51 wxBitmap *my_horse_bmp2;
cbd4be25 52 wxBitmap *my_horse_pcx;
6319afe3 53 wxBitmap *my_horse_pnm;
60a41aee 54 wxBitmap *my_horse_tiff;
9d8bdf2d 55 wxBitmap *my_horse_xpm;
bf504f98
VS
56 wxBitmap *my_horse_ico32;
57 wxBitmap *my_horse_ico16;
58 wxBitmap *my_horse_ico;
59 wxBitmap *my_horse_cur;
51b07644 60
dc1dbfc6 61 wxBitmap *my_smile_xbm;
e8fdc264 62 wxBitmap *my_square;
9390a202 63 wxBitmap *my_anti;
1d5b7a0b 64
bf504f98
VS
65 int xH, yH ;
66
bea56879
VZ
67protected:
68 wxBitmap m_bmpSmileXpm;
69 wxIcon m_iconSmileXpm;
70
60a41aee 71private:
1d5b7a0b
VZ
72 DECLARE_DYNAMIC_CLASS(MyCanvas)
73 DECLARE_EVENT_TABLE()
01111366
RR
74};
75
18134a1c
VZ
76
77const int nChoices = 8 ;
78static const wxString bppchoices[nChoices] =
79{
51b07644
VZ
80 _T("1 bpp color"),
81 _T("1 bpp B&W"),
82 _T("4 bpp color"),
83 _T("8 bpp color"),
84 _T("8 bpp greyscale"),
85 _T("8 bpp red"),
86 _T("8 bpp own palette"),
87 _T("24 bpp")
18134a1c
VZ
88};
89
90static const int bppvalues[nChoices] =
91{
92 wxBMP_1BPP,
93 wxBMP_1BPP_BW,
94 wxBMP_4BPP,
95 wxBMP_8BPP,
96 wxBMP_8BPP_GREY,
97 wxBMP_8BPP_RED,
98 wxBMP_8BPP_PALETTE,
99 wxBMP_24BPP
100};
101
01111366
RR
102// MyFrame
103
18134a1c 104
01111366
RR
105class MyFrame: public wxFrame
106{
1d5b7a0b
VZ
107public:
108 MyFrame();
01111366 109
01111366 110 void OnAbout( wxCommandEvent &event );
ab0f0386 111 void OnNewFrame( wxCommandEvent &event );
01111366 112 void OnQuit( wxCommandEvent &event );
1d5b7a0b 113
01111366 114 MyCanvas *m_canvas;
1d5b7a0b 115
60a41aee 116private:
1d5b7a0b
VZ
117 DECLARE_DYNAMIC_CLASS(MyFrame)
118 DECLARE_EVENT_TABLE()
01111366
RR
119};
120
ab0f0386
VZ
121class MyImageFrame : public wxFrame
122{
123public:
124 MyImageFrame(wxFrame *parent, const wxBitmap& bitmap)
1971d23c 125 : wxFrame(parent, -1, _T("Double click to save"),
ab0f0386 126 wxDefaultPosition, wxDefaultSize,
f6bcfd97 127 wxCAPTION | wxSYSTEM_MENU),
1971d23c 128 m_bitmap(bitmap)
ab0f0386
VZ
129 {
130 SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
131 }
132
133 void OnPaint(wxPaintEvent& WXUNUSED(event))
134 {
135 wxPaintDC dc( this );
bf504f98
VS
136 //TRUE for masked images
137 dc.DrawBitmap( m_bitmap, 0, 0, TRUE );
ab0f0386
VZ
138 }
139
1971d23c
VZ
140 void OnSave(wxCommandEvent& WXUNUSED(event))
141 {
368d59f0 142 wxImage image = m_bitmap.ConvertToImage();
1971d23c 143
51b07644
VZ
144 int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"),
145 _T("Set BMP BPP"),
18134a1c 146 nChoices,
1971d23c
VZ
147 bppchoices,
148 this);
149 if ( bppselection == -1 )
150 {
151 // cancelled
152 return;
153 }
154
fd94e8aa 155 image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, bppvalues[bppselection]);
1971d23c
VZ
156
157 wxString deffilename = bppchoices[bppselection];
4693b20c
MB
158 deffilename.Replace(wxT(" "), wxT("_"));
159 deffilename += wxT(".bmp");
160 wxString savefilename = wxFileSelector( wxT("Save Image"),
161 wxT(""),
1971d23c 162 deffilename,
4693b20c
MB
163 (const wxChar *)NULL,
164 wxT("BMP files (*.bmp)|*.bmp|")
165 wxT("PNG files (*.png)|*.png|")
166 wxT("JPEG files (*.jpg)|*.jpg|")
167 wxT("GIF files (*.gif)|*.gif|")
168 wxT("TIFF files (*.tif)|*.tif|")
bf504f98
VS
169 wxT("PCX files (*.pcx)|*.pcx|")
170 wxT("ICO files (*.ico)|*.ico|")
171 wxT("CUR files (*.cur)|*.cur"),
1971d23c
VZ
172 wxSAVE);
173
174 if ( savefilename.empty() )
175 return;
176
fd94e8aa 177 if ( image.GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT) == wxBMP_8BPP_PALETTE )
1971d23c
VZ
178 {
179 unsigned char *cmap = new unsigned char [256];
180 for ( int i = 0; i < 256; i++ )
181 cmap[i] = i;
182 image.SetPalette(wxPalette(256, cmap, cmap, cmap));
183
184 delete cmap;
185 }
186
45647dcf 187 bool loaded;
1971d23c
VZ
188 wxString extension = savefilename.AfterLast('.').Lower();
189
51b07644 190 if (extension == _T("cur"))
45647dcf 191 {
51b07644
VZ
192 image.Rescale(32,32);
193 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0);
45647dcf
VS
194 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0);
195 // This shows how you can save an image with explicitly
196 // specified image format:
197 loaded = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR);
198 }
51b07644 199 else
45647dcf
VS
200 {
201 // This one guesses image format from filename extension
202 // (it may fail if the extension is not recognized):
203 loaded = image.SaveFile(savefilename);
204 }
51b07644 205
45647dcf 206 if ( !loaded )
51b07644
VZ
207 wxMessageBox(_T("No handler for this file type."),
208 _T("File was not saved"),
1971d23c
VZ
209 wxOK|wxCENTRE, this);
210 }
211
ab0f0386
VZ
212private:
213 wxBitmap m_bitmap;
214
215 DECLARE_EVENT_TABLE()
216};
217
01111366
RR
218// MyApp
219
220class MyApp: public wxApp
221{
1d5b7a0b
VZ
222public:
223 virtual bool OnInit();
01111366
RR
224};
225
226// main program
227
228IMPLEMENT_APP(MyApp)
229
230// MyCanvas
231
232IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
233
ab0f0386 234BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
1971d23c
VZ
235 EVT_PAINT(MyImageFrame::OnPaint)
236 EVT_LEFT_DCLICK(MyImageFrame::OnSave)
ab0f0386
VZ
237END_EVENT_TABLE()
238
1d5b7a0b
VZ
239BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
240 EVT_PAINT(MyCanvas::OnPaint)
01111366
RR
241END_EVENT_TABLE()
242
acbd13a3 243MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
1d5b7a0b 244 const wxPoint &pos, const wxSize &size )
81278df2
VZ
245 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
246#if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
247 , m_bmpSmileXpm((const char **) smile_xpm)
248 , m_iconSmileXpm((const char **) smile_xpm)
249#endif
01111366 250{
60a41aee
RR
251 my_horse_png = (wxBitmap*) NULL;
252 my_horse_jpeg = (wxBitmap*) NULL;
253 my_horse_gif = (wxBitmap*) NULL;
254 my_horse_bmp = (wxBitmap*) NULL;
51b07644 255 my_horse_bmp2 = (wxBitmap*) NULL;
60a41aee
RR
256 my_horse_pcx = (wxBitmap*) NULL;
257 my_horse_pnm = (wxBitmap*) NULL;
258 my_horse_tiff = (wxBitmap*) NULL;
9d8bdf2d 259 my_horse_xpm = (wxBitmap*) NULL;
bf504f98
VS
260 my_horse_ico32 = (wxBitmap*) NULL;
261 my_horse_ico16 = (wxBitmap*) NULL;
262 my_horse_ico = (wxBitmap*) NULL;
263 my_horse_cur = (wxBitmap*) NULL;
264
dc1dbfc6 265 my_smile_xbm = (wxBitmap*) NULL;
60a41aee
RR
266 my_square = (wxBitmap*) NULL;
267 my_anti = (wxBitmap*) NULL;
268
269 SetBackgroundColour(* wxWHITE);
270
271 wxBitmap bitmap( 100, 100 );
272
273 wxMemoryDC dc;
274 dc.SelectObject( bitmap );
a60b1f5d 275 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
cf3da716 276 dc.SetPen( *wxBLACK_PEN );
60a41aee 277 dc.DrawRectangle( 0, 0, 100, 100 );
cf3da716
RR
278 dc.SetBrush( *wxWHITE_BRUSH );
279 dc.DrawRectangle( 20, 20, 60, 60 );
60a41aee
RR
280 dc.SelectObject( wxNullBitmap );
281
282 // try to find the directory with our images
283 wxString dir;
4693b20c 284 if ( wxFile::Exists(wxT("./horse.png")) )
81278df2 285 dir = "./";
4693b20c 286 else if ( wxFile::Exists(wxT("../horse.png")) )
60a41aee
RR
287 dir = "../";
288 else
4693b20c 289 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
60a41aee 290
a8d4e3ac 291 wxImage image = bitmap.ConvertToImage();
60a41aee 292
bea56879 293#if wxUSE_LIBPNG
51b07644 294 if ( !image.SaveFile( dir + _T("test.png"), wxBITMAP_TYPE_PNG ))
4693b20c 295 wxLogError(wxT("Can't save file"));
f6bcfd97 296
176fe5af 297 image.Destroy();
60a41aee 298
51b07644 299 image.LoadFile( dir + _T("test.png") );
a8d4e3ac 300 my_square = new wxBitmap( image );
f6bcfd97 301
176fe5af 302 image.Destroy();
cc977e5f 303
51b07644 304 if ( !image.LoadFile( dir + _T("horse.png")) )
4693b20c 305 wxLogError(wxT("Can't load PNG image"));
60a41aee 306 else
a8d4e3ac 307 my_horse_png = new wxBitmap( image );
bea56879 308#endif // wxUSE_LIBPNG
60a41aee 309
bea56879 310#if wxUSE_LIBJPEG
176fe5af 311 image.Destroy();
279ababf 312
51b07644 313 if ( !image.LoadFile( dir + _T("horse.jpg")) )
4693b20c 314 wxLogError(wxT("Can't load JPG image"));
60a41aee 315 else
9d8bdf2d 316 my_horse_jpeg = new wxBitmap( image );
bea56879 317#endif // wxUSE_LIBJPEG
6e47faf1
JS
318
319#if wxUSE_GIF
176fe5af 320 image.Destroy();
279ababf 321
2f6c54eb 322 if ( !image.LoadFile( dir + wxString("horse.gif")))
4693b20c 323 wxLogError(wxT("Can't load GIF image"));
60a41aee 324 else
9d8bdf2d 325 my_horse_gif = new wxBitmap( image );
6e47faf1 326#endif
cbd4be25 327
6e47faf1 328#if wxUSE_PCX
176fe5af 329 image.Destroy();
279ababf 330
51b07644 331 if ( !image.LoadFile( dir + _T("horse.pcx"), wxBITMAP_TYPE_PCX ) )
4693b20c 332 wxLogError(wxT("Can't load PCX image"));
60a41aee 333 else
9d8bdf2d 334 my_horse_pcx = new wxBitmap( image );
6e47faf1 335#endif
cbd4be25 336
176fe5af 337 image.Destroy();
279ababf 338
51b07644 339 if ( !image.LoadFile( dir + _T("horse.bmp"), wxBITMAP_TYPE_BMP ) )
4693b20c 340 wxLogError(wxT("Can't load BMP image"));
60a41aee 341 else
9d8bdf2d 342 my_horse_bmp = new wxBitmap( image );
6319afe3 343
a8d4e3ac 344#if wxUSE_XPM
9d8bdf2d
VS
345 image.Destroy();
346
51b07644 347 if ( !image.LoadFile( dir + _T("horse.xpm"), wxBITMAP_TYPE_XPM ) )
4693b20c 348 wxLogError(wxT("Can't load XPM image"));
9d8bdf2d
VS
349 else
350 my_horse_xpm = new wxBitmap( image );
351
51b07644 352 if ( !image.SaveFile( dir + _T("test.xpm"), wxBITMAP_TYPE_XPM ))
4693b20c 353 wxLogError(wxT("Can't save file"));
a8d4e3ac
VS
354#endif
355
6e47faf1 356#if wxUSE_PNM
176fe5af 357 image.Destroy();
279ababf 358
51b07644 359 if ( !image.LoadFile( dir + _T("horse.pnm"), wxBITMAP_TYPE_PNM ) )
4693b20c 360 wxLogError(wxT("Can't load PNM image"));
60a41aee 361 else
9d8bdf2d 362 my_horse_pnm = new wxBitmap( image );
6e47faf1 363#endif
f048e32f 364
60a41aee 365#if wxUSE_LIBTIFF
176fe5af 366 image.Destroy();
279ababf 367
51b07644 368 if ( !image.LoadFile( dir + _T("horse.tif"), wxBITMAP_TYPE_TIF ) )
4693b20c 369 wxLogError(wxT("Can't load TIFF image"));
60a41aee 370 else
9d8bdf2d 371 my_horse_tiff = new wxBitmap( image );
60a41aee
RR
372#endif
373
60a41aee 374 CreateAntiAliasedBitmap();
dc1dbfc6
RL
375
376 my_smile_xbm = new wxBitmap( (const char*)smile_bits, smile_width,
377 smile_height, 1 );
bfef20bf 378
81278df2 379#if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
bfef20bf
GRG
380 // demonstrates XPM automatically using the mask when saving
381 if ( m_bmpSmileXpm.Ok() )
51b07644 382 m_bmpSmileXpm.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM);
81278df2 383#endif
bf504f98
VS
384
385#if wxUSE_ICO_CUR
386 image.Destroy();
387
51b07644 388 if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 0 ) )
bf504f98
VS
389 wxLogError(wxT("Can't load first ICO image"));
390 else
391 my_horse_ico32 = new wxBitmap( image );
51b07644 392
bf504f98
VS
393 image.Destroy();
394
51b07644 395 if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 1 ) )
bf504f98
VS
396 wxLogError(wxT("Can't load second ICO image"));
397 else
398 my_horse_ico16 = new wxBitmap( image );
399
400 image.Destroy();
401
51b07644 402 if ( !image.LoadFile( dir + _T("horse.ico") ) )
bf504f98
VS
403 wxLogError(wxT("Can't load best ICO image"));
404 else
405 my_horse_ico = new wxBitmap( image );
406
407 image.Destroy();
408
51b07644 409 if ( !image.LoadFile( dir + _T("horse.cur"), wxBITMAP_TYPE_CUR ) )
bf504f98
VS
410 wxLogError(wxT("Can't load best ICO image"));
411 else
51b07644 412 {
bf504f98 413 my_horse_cur = new wxBitmap( image );
fd94e8aa
VS
414 xH = 30 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) ;
415 yH = 2420 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ;
51b07644 416 }
bf504f98
VS
417#endif
418
51b07644
VZ
419 image.Destroy();
420
421 // test image loading from stream
422 wxFile file(dir + _T("horse.bmp"));
423 off_t len = file.Length();
424 void *data = malloc(len);
425 if ( file.Read(data, len) != len )
426 wxLogError(_T("Reading bitmap file failed"));
427 else
428 {
429 wxMemoryInputStream mis(data, len);
430 if ( !image.LoadFile(mis) )
431 wxLogError(wxT("Can't load BMP image from stream"));
432 else
433 my_horse_bmp2 = new wxBitmap( image );
434 }
435
436 free(data);
01111366
RR
437}
438
1d5b7a0b 439MyCanvas::~MyCanvas()
01111366 440{
60a41aee
RR
441 delete my_horse_pnm;
442 delete my_horse_png;
443 delete my_horse_jpeg;
444 delete my_horse_gif;
445 delete my_horse_bmp;
51b07644 446 delete my_horse_bmp2;
60a41aee
RR
447 delete my_horse_pcx;
448 delete my_horse_tiff;
9d8bdf2d 449 delete my_horse_xpm;
bf504f98
VS
450 delete my_horse_ico32;
451 delete my_horse_ico16;
452 delete my_horse_ico;
2f6c54eb 453 delete my_horse_cur;
dc1dbfc6 454 delete my_smile_xbm;
60a41aee
RR
455 delete my_square;
456 delete my_anti;
01111366
RR
457}
458
459void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
460{
be25e480
RR
461 wxPaintDC dc( this );
462 PrepareDC( dc );
a91b47e8 463
51b07644 464 dc.DrawText( _T("Loaded image"), 30, 10 );
be25e480 465 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
f048e32f 466
51b07644 467 dc.DrawText( _T("Drawn directly"), 150, 10 );
a60b1f5d 468 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
cf3da716 469 dc.SetPen( *wxBLACK_PEN );
be25e480 470 dc.DrawRectangle( 150, 30, 100, 100 );
cf3da716
RR
471 dc.SetBrush( *wxWHITE_BRUSH );
472 dc.DrawRectangle( 170, 50, 60, 60 );
be25e480 473
408b4168 474 if (my_anti && my_anti->Ok())
be25e480
RR
475 dc.DrawBitmap( *my_anti, 280, 30 );
476
51b07644 477 dc.DrawText( _T("PNG handler"), 30, 135 );
be25e480 478 if (my_horse_png && my_horse_png->Ok())
408b4168 479 {
be25e480
RR
480 dc.DrawBitmap( *my_horse_png, 30, 150 );
481 wxRect rect(0,0,100,100);
482 wxBitmap sub( my_horse_png->GetSubBitmap(rect) );
51b07644 483 dc.DrawText( _T("GetSubBitmap()"), 280, 190 );
be25e480
RR
484 dc.DrawBitmap( sub, 280, 210 );
485 }
486
51b07644 487 dc.DrawText( _T("JPEG handler"), 30, 365 );
408b4168 488 if (my_horse_jpeg && my_horse_jpeg->Ok())
be25e480
RR
489 dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
490
51b07644 491 dc.DrawText( _T("GIF handler"), 30, 595 );
408b4168 492 if (my_horse_gif && my_horse_gif->Ok())
be25e480
RR
493 dc.DrawBitmap( *my_horse_gif, 30, 610 );
494
51b07644 495 dc.DrawText( _T("PCX handler"), 30, 825 );
408b4168 496 if (my_horse_pcx && my_horse_pcx->Ok())
be25e480
RR
497 dc.DrawBitmap( *my_horse_pcx, 30, 840 );
498
51b07644 499 dc.DrawText( _T("BMP handler"), 30, 1055 );
408b4168 500 if (my_horse_bmp && my_horse_bmp->Ok())
be25e480
RR
501 dc.DrawBitmap( *my_horse_bmp, 30, 1070 );
502
51b07644
VZ
503 dc.DrawText( _T("BMP read from memory"), 280, 1055 );
504 if (my_horse_bmp2 && my_horse_bmp2->Ok())
505 dc.DrawBitmap( *my_horse_bmp2, 280, 1070 );
506
507 dc.DrawText( _T("PNM handler"), 30, 1285 );
408b4168 508 if (my_horse_pnm && my_horse_pnm->Ok())
be25e480 509 dc.DrawBitmap( *my_horse_pnm, 30, 1300 );
408b4168 510
51b07644 511 dc.DrawText( _T("TIFF handler"), 30, 1515 );
408b4168 512 if (my_horse_tiff && my_horse_tiff->Ok())
4b05d973 513 dc.DrawBitmap( *my_horse_tiff, 30, 1530 );
be25e480 514
51b07644 515 dc.DrawText( _T("XPM handler"), 30, 1745 );
9d8bdf2d
VS
516 if (my_horse_xpm && my_horse_xpm->Ok())
517 dc.DrawBitmap( *my_horse_xpm, 30, 1760 );
518
bf504f98 519
408b4168 520 if (my_smile_xbm && my_smile_xbm->Ok())
be25e480 521 {
51b07644
VZ
522 dc.DrawText( _T("XBM bitmap"), 30, 1975 );
523 dc.DrawText( _T("(green on red)"), 30, 1990 );
524 dc.SetTextForeground( _T("GREEN") );
525 dc.SetTextBackground( _T("RED") );
9d8bdf2d 526 dc.DrawBitmap( *my_smile_xbm, 30, 2010 );
408b4168 527
a60b1f5d 528 dc.SetTextForeground( wxT("BLACK") );
51b07644
VZ
529 dc.DrawText( _T("After wxImage conversion"), 150, 1975 );
530 dc.DrawText( _T("(red on white)"), 150, 1990 );
a60b1f5d 531 dc.SetTextForeground( wxT("RED") );
a8d4e3ac 532 wxImage i = my_smile_xbm->ConvertToImage();
176fe5af
GRG
533 i.SetMaskColour( 255, 255, 255 );
534 i.Replace( 0, 0, 0,
be25e480
RR
535 wxRED_PEN->GetColour().Red(),
536 wxRED_PEN->GetColour().Green(),
537 wxRED_PEN->GetColour().Blue() );
368d59f0 538 dc.DrawBitmap( wxBitmap(i), 150, 2010, TRUE );
a60b1f5d 539 dc.SetTextForeground( wxT("BLACK") );
be25e480 540 }
176fe5af 541
408b4168 542
41fbc841 543 wxBitmap mono( 60,50,1 );
82ea63e6
RR
544 wxMemoryDC memdc;
545 memdc.SelectObject( mono );
bfef20bf 546 memdc.SetPen( *wxBLACK_PEN );
82ea63e6 547 memdc.SetBrush( *wxWHITE_BRUSH );
41fbc841
RR
548 memdc.DrawRectangle( 0,0,60,50 );
549 memdc.SetTextForeground( *wxBLACK );
51b07644 550 memdc.DrawText( _T("Hi!"), 5, 5 );
41fbc841
RR
551 memdc.SetBrush( *wxBLACK_BRUSH );
552 memdc.DrawRectangle( 33,5,20,20 );
553 memdc.SetPen( *wxRED_PEN );
554 memdc.DrawLine( 5, 42, 50, 42 );
82ea63e6 555 memdc.SelectObject( wxNullBitmap );
176fe5af 556
408b4168 557 if (mono.Ok())
82ea63e6 558 {
51b07644
VZ
559 dc.DrawText( _T("Mono bitmap"), 30, 2095 );
560 dc.DrawText( _T("(red on green)"), 30, 2110 );
a60b1f5d
MB
561 dc.SetTextForeground( wxT("RED") );
562 dc.SetTextBackground( wxT("GREEN") );
9d8bdf2d 563 dc.DrawBitmap( mono, 30, 2130 );
408b4168 564
a60b1f5d 565 dc.SetTextForeground( wxT("BLACK") );
51b07644
VZ
566 dc.DrawText( _T("After wxImage conversion"), 150, 2095 );
567 dc.DrawText( _T("(red on white)"), 150, 2110 );
a60b1f5d 568 dc.SetTextForeground( wxT("RED") );
a8d4e3ac 569 wxImage i = mono.ConvertToImage();
408b4168
VZ
570 i.SetMaskColour( 255,255,255 );
571 i.Replace( 0,0,0,
82ea63e6
RR
572 wxRED_PEN->GetColour().Red(),
573 wxRED_PEN->GetColour().Green(),
574 wxRED_PEN->GetColour().Blue() );
368d59f0 575 dc.DrawBitmap( wxBitmap(i), 150, 2130, TRUE );
a60b1f5d 576 dc.SetTextForeground( wxT("BLACK") );
bea56879
VZ
577 }
578
51b07644 579 dc.DrawText(_T("XPM bitmap"), 30, 2230);
bea56879
VZ
580 if ( m_bmpSmileXpm.Ok() )
581 {
9d8bdf2d 582 dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, TRUE);
bea56879
VZ
583 }
584
51b07644 585 dc.DrawText(_T("XPM icon"), 150, 2230);
bea56879
VZ
586 if ( m_iconSmileXpm.Ok() )
587 {
9d8bdf2d 588 dc.DrawIcon(m_iconSmileXpm, 150, 2250);
82ea63e6 589 }
bf504f98 590
51b07644 591 dc.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
bf504f98
VS
592 if (my_horse_ico32 && my_horse_ico32->Ok())
593 dc.DrawBitmap( *my_horse_ico32, 30, 2330, TRUE );
594
51b07644 595 dc.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
bf504f98
VS
596 if (my_horse_ico16 && my_horse_ico16->Ok())
597 dc.DrawBitmap( *my_horse_ico16, 230, 2330, TRUE );
51b07644
VZ
598
599 dc.DrawText( _T("ICO handler (best image)"), 430, 2290 );
bf504f98
VS
600 if (my_horse_ico && my_horse_ico->Ok())
601 dc.DrawBitmap( *my_horse_ico, 430, 2330, TRUE );
602
51b07644 603 dc.DrawText( _T("CUR handler"), 30, 2390 );
bf504f98 604 if (my_horse_cur && my_horse_cur->Ok())
51b07644 605 {
bf504f98
VS
606 dc.DrawBitmap( *my_horse_cur, 30, 2420, TRUE );
607 dc.SetPen (*wxRED_PEN);
608 dc.DrawLine (xH-10,yH,xH+10,yH);
609 dc.DrawLine (xH,yH-10,xH,yH+10);
51b07644 610 }
9390a202 611}
1d5b7a0b 612
9390a202
RR
613void MyCanvas::CreateAntiAliasedBitmap()
614{
615 wxBitmap bitmap( 300, 300 );
a91b47e8 616
9390a202 617 wxMemoryDC dc;
a91b47e8 618
9390a202 619 dc.SelectObject( bitmap );
a91b47e8 620
9390a202 621 dc.Clear();
f048e32f 622
6e47faf1 623 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
a60b1f5d 624 dc.SetTextForeground( wxT("RED") );
51b07644
VZ
625 dc.DrawText( _T("This is anti-aliased Text."), 20, 20 );
626 dc.DrawText( _T("And a Rectangle."), 20, 60 );
f048e32f 627
9390a202 628 dc.SetBrush( *wxRED_BRUSH );
91b8de8d 629 dc.SetPen( *wxTRANSPARENT_PEN );
9390a202 630 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
f048e32f 631
a8d4e3ac 632 wxImage original= bitmap.ConvertToImage();
9390a202 633 wxImage anti( 150, 150 );
a91b47e8 634
9390a202 635 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
f048e32f 636
9390a202
RR
637 for (int y = 1; y < 149; y++)
638 for (int x = 1; x < 149; x++)
639 {
640 int red = original.GetRed( x*2, y*2 ) +
f048e32f
VZ
641 original.GetRed( x*2-1, y*2 ) +
642 original.GetRed( x*2, y*2+1 ) +
9390a202 643 original.GetRed( x*2+1, y*2+1 );
f048e32f
VZ
644 red = red/4;
645
9390a202 646 int green = original.GetGreen( x*2, y*2 ) +
f048e32f
VZ
647 original.GetGreen( x*2-1, y*2 ) +
648 original.GetGreen( x*2, y*2+1 ) +
9390a202 649 original.GetGreen( x*2+1, y*2+1 );
f048e32f
VZ
650 green = green/4;
651
9390a202 652 int blue = original.GetBlue( x*2, y*2 ) +
f048e32f
VZ
653 original.GetBlue( x*2-1, y*2 ) +
654 original.GetBlue( x*2, y*2+1 ) +
9390a202 655 original.GetBlue( x*2+1, y*2+1 );
f048e32f 656 blue = blue/4;
9390a202
RR
657 anti.SetRGB( x, y, red, green, blue );
658 }
368d59f0 659 my_anti = new wxBitmap(anti);
01111366
RR
660}
661
662// MyFrame
663
79490c3d
VZ
664const int ID_QUIT = 108;
665const int ID_ABOUT = 109;
ab0f0386 666const int ID_NEW = 110;
01111366
RR
667
668IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
669
670BEGIN_EVENT_TABLE(MyFrame,wxFrame)
671 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
672 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
ab0f0386 673 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
01111366
RR
674END_EVENT_TABLE()
675
1d5b7a0b 676MyFrame::MyFrame()
51b07644 677 : wxFrame( (wxFrame *)NULL, -1, _T("wxImage sample"),
1d5b7a0b 678 wxPoint(20,20), wxSize(470,360) )
01111366
RR
679{
680 wxMenu *file_menu = new wxMenu();
51b07644 681 file_menu->Append( ID_NEW, _T("&Show image..."));
ab0f0386 682 file_menu->AppendSeparator();
51b07644 683 file_menu->Append( ID_ABOUT, _T("&About..."));
ab0f0386 684 file_menu->AppendSeparator();
51b07644 685 file_menu->Append( ID_QUIT, _T("E&xit"));
1d5b7a0b 686
01111366 687 wxMenuBar *menu_bar = new wxMenuBar();
51b07644 688 menu_bar->Append(file_menu, _T("&File"));
3d05544e 689
01111366 690 SetMenuBar( menu_bar );
1d5b7a0b 691
917e533b
RR
692 CreateStatusBar(2);
693 int widths[] = { -1, 100 };
694 SetStatusWidths( 2, widths );
1d5b7a0b 695
fb1585ae 696 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
cbd4be25 697
bf504f98
VS
698 // 500 width * 2500 height
699 m_canvas->SetScrollbars( 10, 10, 50, 250 );
01111366
RR
700}
701
702void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
703{
704 Close( TRUE );
705}
706
707void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
708{
51b07644
VZ
709 (void)wxMessageBox( _T("wxImage demo\n")
710 _T("Robert Roebling (c) 1998,2000"),
711 _T("About wxImage Demo"), wxICON_INFORMATION | wxOK );
fb1585ae
RR
712}
713
ab0f0386
VZ
714void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
715{
f6bcfd97
BP
716 wxString filename = wxFileSelector(_T("Select image file"));
717 if ( !filename )
718 return;
719
720 wxImage image;
721 if ( !image.LoadFile(filename) )
722 {
723 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
724
725 return;
726 }
727
368d59f0 728 (new MyImageFrame(this, wxBitmap(image)))->Show();
ab0f0386
VZ
729}
730
01111366
RR
731//-----------------------------------------------------------------------------
732// MyApp
733//-----------------------------------------------------------------------------
734
1d5b7a0b 735bool MyApp::OnInit()
01111366 736{
e9c4b1a2
JS
737#if wxUSE_LIBPNG
738 wxImage::AddHandler( new wxPNGHandler );
739#endif
740
741#if wxUSE_LIBJPEG
742 wxImage::AddHandler( new wxJPEGHandler );
743#endif
744
60a41aee
RR
745#if wxUSE_LIBTIFF
746 wxImage::AddHandler( new wxTIFFHandler );
747#endif
748
6e47faf1 749#if wxUSE_GIF
e1929140 750 wxImage::AddHandler( new wxGIFHandler );
6e47faf1
JS
751#endif
752
753#if wxUSE_PCX
cbd4be25 754 wxImage::AddHandler( new wxPCXHandler );
6e47faf1
JS
755#endif
756
757#if wxUSE_PNM
6319afe3 758 wxImage::AddHandler( new wxPNMHandler );
6e47faf1 759#endif
e1929140 760
a8d4e3ac
VS
761#if wxUSE_XPM
762 wxImage::AddHandler( new wxXPMHandler );
763#endif
764
bf504f98
VS
765#if wxUSE_ICO_CUR
766 wxImage::AddHandler( new wxICOHandler );
767 wxImage::AddHandler( new wxCURHandler );
768#endif
769
01111366
RR
770 wxFrame *frame = new MyFrame();
771 frame->Show( TRUE );
1d5b7a0b 772
01111366
RR
773 return TRUE;
774}
775