]> git.saurik.com Git - wxWidgets.git/blame - samples/image/image.cpp
final (touch wood) tidy after things went pearshaped on line 666
[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
VZ
22#include "wx/file.h"
23
dc1dbfc6 24#include "smile.xbm"
bea56879 25#include "smile.xpm"
dc1dbfc6 26
01111366
RR
27// derived classes
28
29class MyFrame;
30class MyApp;
31
32// MyCanvas
33
34class MyCanvas: public wxScrolledWindow
35{
1d5b7a0b
VZ
36public:
37 MyCanvas() {};
01111366 38 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
1d5b7a0b 39 ~MyCanvas();
01111366 40 void OnPaint( wxPaintEvent &event );
9390a202 41 void CreateAntiAliasedBitmap();
1d5b7a0b 42
e1929140
RR
43 wxBitmap *my_horse_png;
44 wxBitmap *my_horse_jpeg;
45 wxBitmap *my_horse_gif;
ca26177c 46 wxBitmap *my_horse_bmp;
cbd4be25 47 wxBitmap *my_horse_pcx;
6319afe3 48 wxBitmap *my_horse_pnm;
60a41aee 49 wxBitmap *my_horse_tiff;
dc1dbfc6 50 wxBitmap *my_smile_xbm;
e8fdc264 51 wxBitmap *my_square;
9390a202 52 wxBitmap *my_anti;
1d5b7a0b 53
bea56879
VZ
54protected:
55 wxBitmap m_bmpSmileXpm;
56 wxIcon m_iconSmileXpm;
57
60a41aee 58private:
1d5b7a0b
VZ
59 DECLARE_DYNAMIC_CLASS(MyCanvas)
60 DECLARE_EVENT_TABLE()
01111366
RR
61};
62
63// MyFrame
64
65class MyFrame: public wxFrame
66{
1d5b7a0b
VZ
67public:
68 MyFrame();
01111366 69
01111366
RR
70 void OnAbout( wxCommandEvent &event );
71 void OnQuit( wxCommandEvent &event );
1d5b7a0b 72
01111366 73 MyCanvas *m_canvas;
1d5b7a0b 74
60a41aee 75private:
1d5b7a0b
VZ
76 DECLARE_DYNAMIC_CLASS(MyFrame)
77 DECLARE_EVENT_TABLE()
01111366
RR
78};
79
80// MyApp
81
82class MyApp: public wxApp
83{
1d5b7a0b
VZ
84public:
85 virtual bool OnInit();
01111366
RR
86};
87
88// main program
89
90IMPLEMENT_APP(MyApp)
91
92// MyCanvas
93
94IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
95
1d5b7a0b
VZ
96BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
97 EVT_PAINT(MyCanvas::OnPaint)
01111366
RR
98END_EVENT_TABLE()
99
acbd13a3 100MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
1d5b7a0b 101 const wxPoint &pos, const wxSize &size )
81278df2
VZ
102 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
103#if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
104 , m_bmpSmileXpm((const char **) smile_xpm)
105 , m_iconSmileXpm((const char **) smile_xpm)
106#endif
01111366 107{
60a41aee
RR
108 my_horse_png = (wxBitmap*) NULL;
109 my_horse_jpeg = (wxBitmap*) NULL;
110 my_horse_gif = (wxBitmap*) NULL;
111 my_horse_bmp = (wxBitmap*) NULL;
112 my_horse_pcx = (wxBitmap*) NULL;
113 my_horse_pnm = (wxBitmap*) NULL;
114 my_horse_tiff = (wxBitmap*) NULL;
dc1dbfc6 115 my_smile_xbm = (wxBitmap*) NULL;
60a41aee
RR
116 my_square = (wxBitmap*) NULL;
117 my_anti = (wxBitmap*) NULL;
118
119 SetBackgroundColour(* wxWHITE);
120
121 wxBitmap bitmap( 100, 100 );
122
123 wxMemoryDC dc;
124 dc.SelectObject( bitmap );
125 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
cf3da716 126 dc.SetPen( *wxBLACK_PEN );
60a41aee 127 dc.DrawRectangle( 0, 0, 100, 100 );
cf3da716
RR
128 dc.SetBrush( *wxWHITE_BRUSH );
129 dc.DrawRectangle( 20, 20, 60, 60 );
60a41aee
RR
130 dc.SelectObject( wxNullBitmap );
131
132 // try to find the directory with our images
133 wxString dir;
134 if ( wxFile::Exists("./horse.png") )
81278df2 135 dir = "./";
60a41aee
RR
136 else if ( wxFile::Exists("../horse.png") )
137 dir = "../";
138 else
139 wxLogWarning("Can't find image files in either '.' or '..'!");
140
141 wxImage image( bitmap );
142
bea56879 143#if wxUSE_LIBPNG
176fe5af 144 if ( !image.SaveFile( dir + wxString("test.png"), wxBITMAP_TYPE_PNG ))
60a41aee 145 wxLogError("Can't save file");
279ababf 146
176fe5af 147 image.Destroy();
60a41aee 148
cc977e5f
RR
149 image.LoadFile( dir + wxString("test.png") );
150 my_square = new wxBitmap( image.ConvertToBitmap() );
151
176fe5af 152 image.Destroy();
cc977e5f 153
176fe5af 154 if ( !image.LoadFile( dir + wxString("horse.png")) )
60a41aee
RR
155 wxLogError("Can't load PNG image");
156 else
157 my_horse_png = new wxBitmap( image.ConvertToBitmap() );
bea56879 158#endif // wxUSE_LIBPNG
60a41aee 159
bea56879 160#if wxUSE_LIBJPEG
176fe5af 161 image.Destroy();
279ababf 162
176fe5af 163 if ( !image.LoadFile( dir + wxString("horse.jpg")) )
60a41aee
RR
164 wxLogError("Can't load JPG image");
165 else
166 my_horse_jpeg = new wxBitmap( image.ConvertToBitmap() );
bea56879 167#endif // wxUSE_LIBJPEG
6e47faf1
JS
168
169#if wxUSE_GIF
176fe5af 170 image.Destroy();
279ababf 171
176fe5af 172 if ( !image.LoadFile( dir + wxString("horse.gif")) )
60a41aee
RR
173 wxLogError("Can't load GIF image");
174 else
175 my_horse_gif = new wxBitmap( image.ConvertToBitmap() );
6e47faf1 176#endif
cbd4be25 177
6e47faf1 178#if wxUSE_PCX
176fe5af 179 image.Destroy();
279ababf 180
60a41aee
RR
181 if ( !image.LoadFile( dir + wxString("horse.pcx"), wxBITMAP_TYPE_PCX ) )
182 wxLogError("Can't load PCX image");
183 else
184 my_horse_pcx = new wxBitmap( image.ConvertToBitmap() );
6e47faf1 185#endif
cbd4be25 186
176fe5af 187 image.Destroy();
279ababf 188
60a41aee
RR
189 if ( !image.LoadFile( dir + wxString("horse.bmp"), wxBITMAP_TYPE_BMP ) )
190 wxLogError("Can't load BMP image");
191 else
192 my_horse_bmp = new wxBitmap( image.ConvertToBitmap() );
6319afe3 193
6e47faf1 194#if wxUSE_PNM
176fe5af 195 image.Destroy();
279ababf 196
60a41aee
RR
197 if ( !image.LoadFile( dir + wxString("horse.pnm"), wxBITMAP_TYPE_PNM ) )
198 wxLogError("Can't load PNM image");
199 else
200 my_horse_pnm = new wxBitmap( image.ConvertToBitmap() );
6e47faf1 201#endif
f048e32f 202
60a41aee 203#if wxUSE_LIBTIFF
176fe5af 204 image.Destroy();
279ababf 205
60a41aee
RR
206 if ( !image.LoadFile( dir + wxString("horse.tif"), wxBITMAP_TYPE_TIF ) )
207 wxLogError("Can't load TIFF image");
208 else
209 my_horse_tiff = new wxBitmap( image.ConvertToBitmap() );
210#endif
211
60a41aee 212 CreateAntiAliasedBitmap();
dc1dbfc6
RL
213
214 my_smile_xbm = new wxBitmap( (const char*)smile_bits, smile_width,
215 smile_height, 1 );
bfef20bf 216
279ababf 217#ifndef __WXGTK__
81278df2 218#if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
bfef20bf
GRG
219 // demonstrates XPM automatically using the mask when saving
220 if ( m_bmpSmileXpm.Ok() )
221 m_bmpSmileXpm.SaveFile("saved.xpm", wxBITMAP_TYPE_XPM);
81278df2 222#endif
279ababf 223#endif
01111366
RR
224}
225
1d5b7a0b 226MyCanvas::~MyCanvas()
01111366 227{
60a41aee
RR
228 delete my_horse_pnm;
229 delete my_horse_png;
230 delete my_horse_jpeg;
231 delete my_horse_gif;
232 delete my_horse_bmp;
233 delete my_horse_pcx;
234 delete my_horse_tiff;
dc1dbfc6 235 delete my_smile_xbm;
60a41aee
RR
236 delete my_square;
237 delete my_anti;
01111366
RR
238}
239
240void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
241{
be25e480
RR
242 wxPaintDC dc( this );
243 PrepareDC( dc );
a91b47e8 244
be25e480
RR
245 dc.DrawText( "Loaded image", 30, 10 );
246 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
f048e32f 247
be25e480
RR
248 dc.DrawText( "Drawn directly", 150, 10 );
249 dc.SetBrush( wxBrush( "orange", wxSOLID ) );
cf3da716 250 dc.SetPen( *wxBLACK_PEN );
be25e480 251 dc.DrawRectangle( 150, 30, 100, 100 );
cf3da716
RR
252 dc.SetBrush( *wxWHITE_BRUSH );
253 dc.DrawRectangle( 170, 50, 60, 60 );
be25e480 254
408b4168 255 if (my_anti && my_anti->Ok())
be25e480
RR
256 dc.DrawBitmap( *my_anti, 280, 30 );
257
258 dc.DrawText( "PNG handler", 30, 135 );
259 if (my_horse_png && my_horse_png->Ok())
408b4168 260 {
be25e480
RR
261 dc.DrawBitmap( *my_horse_png, 30, 150 );
262 wxRect rect(0,0,100,100);
263 wxBitmap sub( my_horse_png->GetSubBitmap(rect) );
264 dc.DrawText( "GetSubBitmap()", 280, 190 );
265 dc.DrawBitmap( sub, 280, 210 );
266 }
267
268 dc.DrawText( "JPEG handler", 30, 365 );
408b4168 269 if (my_horse_jpeg && my_horse_jpeg->Ok())
be25e480
RR
270 dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
271
272 dc.DrawText( "GIF handler", 30, 595 );
408b4168 273 if (my_horse_gif && my_horse_gif->Ok())
be25e480
RR
274 dc.DrawBitmap( *my_horse_gif, 30, 610 );
275
276 dc.DrawText( "PCX handler", 30, 825 );
408b4168 277 if (my_horse_pcx && my_horse_pcx->Ok())
be25e480
RR
278 dc.DrawBitmap( *my_horse_pcx, 30, 840 );
279
280 dc.DrawText( "BMP handler", 30, 1055 );
408b4168 281 if (my_horse_bmp && my_horse_bmp->Ok())
be25e480
RR
282 dc.DrawBitmap( *my_horse_bmp, 30, 1070 );
283
284 dc.DrawText( "PNM handler", 30, 1285 );
408b4168 285 if (my_horse_pnm && my_horse_pnm->Ok())
be25e480 286 dc.DrawBitmap( *my_horse_pnm, 30, 1300 );
408b4168 287
be25e480 288 dc.DrawText( "TIFF handler", 30, 1515 );
408b4168 289 if (my_horse_tiff && my_horse_tiff->Ok())
4b05d973 290 dc.DrawBitmap( *my_horse_tiff, 30, 1530 );
be25e480 291
408b4168 292 if (my_smile_xbm && my_smile_xbm->Ok())
be25e480
RR
293 {
294 dc.DrawText( "XBM bitmap", 30, 1745 );
176fe5af
GRG
295 dc.DrawText( "(green on red)", 30, 1760 );
296 dc.SetTextForeground( "GREEN" );
297 dc.SetTextBackground( "RED" );
298 dc.DrawBitmap( *my_smile_xbm, 30, 1780 );
408b4168 299
176fe5af 300 dc.SetTextForeground( "BLACK" );
be25e480 301 dc.DrawText( "After wxImage conversion", 150, 1745 );
176fe5af
GRG
302 dc.DrawText( "(red on white)", 150, 1760 );
303 dc.SetTextForeground( "RED" );
be25e480 304 wxImage i( *my_smile_xbm );
176fe5af
GRG
305 i.SetMaskColour( 255, 255, 255 );
306 i.Replace( 0, 0, 0,
be25e480
RR
307 wxRED_PEN->GetColour().Red(),
308 wxRED_PEN->GetColour().Green(),
309 wxRED_PEN->GetColour().Blue() );
176fe5af
GRG
310 dc.DrawBitmap( i.ConvertToBitmap(), 150, 1780, TRUE );
311 dc.SetTextForeground( "BLACK" );
be25e480 312 }
176fe5af 313
408b4168 314
41fbc841 315 wxBitmap mono( 60,50,1 );
82ea63e6
RR
316 wxMemoryDC memdc;
317 memdc.SelectObject( mono );
bfef20bf 318 memdc.SetPen( *wxBLACK_PEN );
82ea63e6 319 memdc.SetBrush( *wxWHITE_BRUSH );
41fbc841
RR
320 memdc.DrawRectangle( 0,0,60,50 );
321 memdc.SetTextForeground( *wxBLACK );
322 memdc.DrawText( "Hi!", 5, 5 );
323 memdc.SetBrush( *wxBLACK_BRUSH );
324 memdc.DrawRectangle( 33,5,20,20 );
325 memdc.SetPen( *wxRED_PEN );
326 memdc.DrawLine( 5, 42, 50, 42 );
82ea63e6 327 memdc.SelectObject( wxNullBitmap );
176fe5af 328
408b4168 329 if (mono.Ok())
82ea63e6 330 {
176fe5af
GRG
331 dc.DrawText( "Mono bitmap", 30, 1865 );
332 dc.DrawText( "(red on green)", 30, 1880 );
82ea63e6
RR
333 dc.SetTextForeground( "RED" );
334 dc.SetTextBackground( "GREEN" );
176fe5af 335 dc.DrawBitmap( mono, 30, 1900 );
408b4168 336
bea56879 337 dc.SetTextForeground( "BLACK" );
176fe5af
GRG
338 dc.DrawText( "After wxImage conversion", 150, 1865 );
339 dc.DrawText( "(red on white)", 150, 1880 );
bea56879 340 dc.SetTextForeground( "RED" );
82ea63e6 341 wxImage i( mono );
408b4168
VZ
342 i.SetMaskColour( 255,255,255 );
343 i.Replace( 0,0,0,
82ea63e6
RR
344 wxRED_PEN->GetColour().Red(),
345 wxRED_PEN->GetColour().Green(),
346 wxRED_PEN->GetColour().Blue() );
176fe5af 347 dc.DrawBitmap( i.ConvertToBitmap(), 150, 1900, TRUE );
bea56879
VZ
348 dc.SetTextForeground( "BLACK" );
349 }
350
176fe5af 351 dc.DrawText("XPM bitmap", 30, 2000);
bea56879
VZ
352 if ( m_bmpSmileXpm.Ok() )
353 {
176fe5af 354 dc.DrawBitmap(m_bmpSmileXpm, 30, 2020, TRUE);
bea56879
VZ
355 }
356
176fe5af 357 dc.DrawText("XPM icon", 150, 2000);
bea56879
VZ
358 if ( m_iconSmileXpm.Ok() )
359 {
176fe5af 360 dc.DrawIcon(m_iconSmileXpm, 150, 2020);
82ea63e6 361 }
9390a202 362}
1d5b7a0b 363
9390a202
RR
364void MyCanvas::CreateAntiAliasedBitmap()
365{
366 wxBitmap bitmap( 300, 300 );
a91b47e8 367
9390a202 368 wxMemoryDC dc;
a91b47e8 369
9390a202 370 dc.SelectObject( bitmap );
a91b47e8 371
9390a202 372 dc.Clear();
f048e32f 373
6e47faf1 374 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
9390a202
RR
375 dc.SetTextForeground( "RED" );
376 dc.DrawText( "This is anti-aliased Text.", 20, 20 );
377 dc.DrawText( "And a Rectangle.", 20, 60 );
f048e32f 378
9390a202 379 dc.SetBrush( *wxRED_BRUSH );
91b8de8d 380 dc.SetPen( *wxTRANSPARENT_PEN );
9390a202 381 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
f048e32f 382
9390a202
RR
383 wxImage original( bitmap );
384 wxImage anti( 150, 150 );
a91b47e8 385
9390a202 386 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
f048e32f 387
9390a202
RR
388 for (int y = 1; y < 149; y++)
389 for (int x = 1; x < 149; x++)
390 {
391 int red = original.GetRed( x*2, y*2 ) +
f048e32f
VZ
392 original.GetRed( x*2-1, y*2 ) +
393 original.GetRed( x*2, y*2+1 ) +
9390a202 394 original.GetRed( x*2+1, y*2+1 );
f048e32f
VZ
395 red = red/4;
396
9390a202 397 int green = original.GetGreen( x*2, y*2 ) +
f048e32f
VZ
398 original.GetGreen( x*2-1, y*2 ) +
399 original.GetGreen( x*2, y*2+1 ) +
9390a202 400 original.GetGreen( x*2+1, y*2+1 );
f048e32f
VZ
401 green = green/4;
402
9390a202 403 int blue = original.GetBlue( x*2, y*2 ) +
f048e32f
VZ
404 original.GetBlue( x*2-1, y*2 ) +
405 original.GetBlue( x*2, y*2+1 ) +
9390a202 406 original.GetBlue( x*2+1, y*2+1 );
f048e32f 407 blue = blue/4;
9390a202
RR
408 anti.SetRGB( x, y, red, green, blue );
409 }
410 my_anti = new wxBitmap( anti.ConvertToBitmap() );
01111366
RR
411}
412
413// MyFrame
414
79490c3d
VZ
415const int ID_QUIT = 108;
416const int ID_ABOUT = 109;
01111366
RR
417
418IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
419
420BEGIN_EVENT_TABLE(MyFrame,wxFrame)
421 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
422 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
423END_EVENT_TABLE()
424
1d5b7a0b
VZ
425MyFrame::MyFrame()
426 : wxFrame( (wxFrame *)NULL, -1, "wxImage sample",
427 wxPoint(20,20), wxSize(470,360) )
01111366
RR
428{
429 wxMenu *file_menu = new wxMenu();
81278df2 430 file_menu->Append( ID_ABOUT, "&About...");
90e58684 431 file_menu->Append( ID_QUIT, "E&xit");
1d5b7a0b 432
01111366 433 wxMenuBar *menu_bar = new wxMenuBar();
90e58684 434 menu_bar->Append(file_menu, "&File");
3d05544e 435
01111366 436 SetMenuBar( menu_bar );
1d5b7a0b 437
917e533b
RR
438 CreateStatusBar(2);
439 int widths[] = { -1, 100 };
440 SetStatusWidths( 2, widths );
1d5b7a0b 441
fb1585ae 442 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
cbd4be25 443
bea56879 444 // 500 width * 2100 height
176fe5af 445 m_canvas->SetScrollbars( 10, 10, 50, 220 );
01111366
RR
446}
447
448void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
449{
450 Close( TRUE );
451}
452
453void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
454{
1d5b7a0b 455 (void)wxMessageBox( "wxImage demo\n"
41fbc841 456 "Robert Roebling (c) 1998,2000",
1d5b7a0b 457 "About wxImage Demo", wxICON_INFORMATION | wxOK );
fb1585ae
RR
458}
459
01111366
RR
460//-----------------------------------------------------------------------------
461// MyApp
462//-----------------------------------------------------------------------------
463
1d5b7a0b 464bool MyApp::OnInit()
01111366 465{
e9c4b1a2
JS
466#if wxUSE_LIBPNG
467 wxImage::AddHandler( new wxPNGHandler );
468#endif
469
470#if wxUSE_LIBJPEG
471 wxImage::AddHandler( new wxJPEGHandler );
472#endif
473
60a41aee
RR
474#if wxUSE_LIBTIFF
475 wxImage::AddHandler( new wxTIFFHandler );
476#endif
477
6e47faf1 478#if wxUSE_GIF
e1929140 479 wxImage::AddHandler( new wxGIFHandler );
6e47faf1
JS
480#endif
481
482#if wxUSE_PCX
cbd4be25 483 wxImage::AddHandler( new wxPCXHandler );
6e47faf1
JS
484#endif
485
486#if wxUSE_PNM
6319afe3 487 wxImage::AddHandler( new wxPNMHandler );
6e47faf1 488#endif
e1929140 489
01111366
RR
490 wxFrame *frame = new MyFrame();
491 frame->Show( TRUE );
1d5b7a0b 492
01111366
RR
493 return TRUE;
494}
495