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