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