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