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