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