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