]> git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
documented need to install ZIP handler
[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 #include "wx/mstream.h"
24 #include "wx/wfstream.h"
25 #include "wx/quantize.h"
26
27 #include "smile.xbm"
28
29 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
30 #include "smile.xpm"
31 #endif
32
33
34 // derived classes
35
36 class MyFrame;
37 class MyApp;
38
39 // MyCanvas
40
41 class MyCanvas: public wxScrolledWindow
42 {
43 public:
44 MyCanvas() {}
45 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
46 ~MyCanvas();
47 void OnPaint( wxPaintEvent &event );
48 void CreateAntiAliasedBitmap();
49
50 wxBitmap *my_horse_png;
51 wxBitmap *my_horse_jpeg;
52 wxBitmap *my_horse_gif;
53 wxBitmap *my_horse_bmp;
54 wxBitmap *my_horse_bmp2;
55 wxBitmap *my_horse_pcx;
56 wxBitmap *my_horse_pnm;
57 wxBitmap *my_horse_tiff;
58 wxBitmap *my_horse_xpm;
59 wxBitmap *my_horse_ico32;
60 wxBitmap *my_horse_ico16;
61 wxBitmap *my_horse_ico;
62 wxBitmap *my_horse_cur;
63
64 wxBitmap *my_smile_xbm;
65 wxBitmap *my_square;
66 wxBitmap *my_anti;
67
68 int xH, yH ;
69
70 protected:
71 wxBitmap m_bmpSmileXpm;
72 wxIcon m_iconSmileXpm;
73
74 private:
75 DECLARE_DYNAMIC_CLASS(MyCanvas)
76 DECLARE_EVENT_TABLE()
77 };
78
79
80 const int nChoices = 8 ;
81 static const wxString bppchoices[nChoices] =
82 {
83 _T("1 bpp color"),
84 _T("1 bpp B&W"),
85 _T("4 bpp color"),
86 _T("8 bpp color"),
87 _T("8 bpp greyscale"),
88 _T("8 bpp red"),
89 _T("8 bpp own palette"),
90 _T("24 bpp")
91 };
92
93 static const int bppvalues[nChoices] =
94 {
95 wxBMP_1BPP,
96 wxBMP_1BPP_BW,
97 wxBMP_4BPP,
98 wxBMP_8BPP,
99 wxBMP_8BPP_GREY,
100 wxBMP_8BPP_RED,
101 wxBMP_8BPP_PALETTE,
102 wxBMP_24BPP
103 };
104
105 // MyFrame
106
107
108 class MyFrame: public wxFrame
109 {
110 public:
111 MyFrame();
112
113 void OnAbout( wxCommandEvent &event );
114 void OnNewFrame( wxCommandEvent &event );
115 void OnQuit( wxCommandEvent &event );
116
117 MyCanvas *m_canvas;
118
119 private:
120 DECLARE_DYNAMIC_CLASS(MyFrame)
121 DECLARE_EVENT_TABLE()
122 };
123
124 class MyImageFrame : public wxFrame
125 {
126 public:
127 MyImageFrame(wxFrame *parent, const wxBitmap& bitmap)
128 : wxFrame(parent, -1, _T("Double click to save"),
129 wxDefaultPosition, wxDefaultSize,
130 wxCAPTION | wxSYSTEM_MENU),
131 m_bitmap(bitmap)
132 {
133 SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
134 }
135
136 void OnPaint(wxPaintEvent& WXUNUSED(event))
137 {
138 wxPaintDC dc( this );
139 //TRUE for masked images
140 dc.DrawBitmap( m_bitmap, 0, 0, TRUE );
141 }
142
143 void OnSave(wxCommandEvent& WXUNUSED(event))
144 {
145 wxImage image = m_bitmap.ConvertToImage();
146
147 int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"),
148 _T("Set BMP BPP"),
149 nChoices,
150 bppchoices,
151 this);
152 if ( bppselection == -1 )
153 {
154 // cancelled
155 return;
156 }
157
158 image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, bppvalues[bppselection]);
159
160 wxString deffilename = bppchoices[bppselection];
161 deffilename.Replace(wxT(" "), wxT("_"));
162 deffilename += wxT(".bmp");
163 wxString savefilename = wxFileSelector( wxT("Save Image"),
164 wxT(""),
165 deffilename,
166 (const wxChar *)NULL,
167 wxT("BMP files (*.bmp)|*.bmp|")
168 wxT("PNG files (*.png)|*.png|")
169 wxT("JPEG files (*.jpg)|*.jpg|")
170 wxT("GIF files (*.gif)|*.gif|")
171 wxT("TIFF files (*.tif)|*.tif|")
172 wxT("PCX files (*.pcx)|*.pcx|")
173 wxT("ICO files (*.ico)|*.ico|")
174 wxT("CUR files (*.cur)|*.cur"),
175 wxSAVE);
176
177 if ( savefilename.empty() )
178 return;
179
180 if ( image.GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT) == wxBMP_8BPP_PALETTE )
181 {
182 unsigned char *cmap = new unsigned char [256];
183 for ( int i = 0; i < 256; i++ )
184 cmap[i] = i;
185 image.SetPalette(wxPalette(256, cmap, cmap, cmap));
186
187 delete cmap;
188 }
189
190 bool loaded;
191 wxString extension = savefilename.AfterLast('.').Lower();
192
193 if (extension == _T("cur"))
194 {
195 image.Rescale(32,32);
196 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0);
197 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0);
198 // This shows how you can save an image with explicitly
199 // specified image format:
200 loaded = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR);
201 }
202 else
203 {
204 // This one guesses image format from filename extension
205 // (it may fail if the extension is not recognized):
206 loaded = image.SaveFile(savefilename);
207 }
208
209 if ( !loaded )
210 wxMessageBox(_T("No handler for this file type."),
211 _T("File was not saved"),
212 wxOK|wxCENTRE, this);
213 }
214
215 private:
216 wxBitmap m_bitmap;
217
218 DECLARE_EVENT_TABLE()
219 };
220
221 // MyApp
222
223 class MyApp: public wxApp
224 {
225 public:
226 virtual bool OnInit();
227 };
228
229 // main program
230
231 IMPLEMENT_APP(MyApp)
232
233 // MyCanvas
234
235 IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
236
237 BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
238 EVT_PAINT(MyImageFrame::OnPaint)
239 EVT_LEFT_DCLICK(MyImageFrame::OnSave)
240 END_EVENT_TABLE()
241
242 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
243 EVT_PAINT(MyCanvas::OnPaint)
244 END_EVENT_TABLE()
245
246 MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
247 const wxPoint &pos, const wxSize &size )
248 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
249 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
250 , m_bmpSmileXpm((const char **) smile_xpm)
251 , m_iconSmileXpm((const char **) smile_xpm)
252 #endif
253 {
254 my_horse_png = (wxBitmap*) NULL;
255 my_horse_jpeg = (wxBitmap*) NULL;
256 my_horse_gif = (wxBitmap*) NULL;
257 my_horse_bmp = (wxBitmap*) NULL;
258 my_horse_bmp2 = (wxBitmap*) NULL;
259 my_horse_pcx = (wxBitmap*) NULL;
260 my_horse_pnm = (wxBitmap*) NULL;
261 my_horse_tiff = (wxBitmap*) NULL;
262 my_horse_xpm = (wxBitmap*) NULL;
263 my_horse_ico32 = (wxBitmap*) NULL;
264 my_horse_ico16 = (wxBitmap*) NULL;
265 my_horse_ico = (wxBitmap*) NULL;
266 my_horse_cur = (wxBitmap*) NULL;
267
268 my_smile_xbm = (wxBitmap*) NULL;
269 my_square = (wxBitmap*) NULL;
270 my_anti = (wxBitmap*) NULL;
271
272 SetBackgroundColour(* wxWHITE);
273
274 wxBitmap bitmap( 100, 100 );
275
276 wxMemoryDC dc;
277 dc.SelectObject( bitmap );
278 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
279 dc.SetPen( *wxBLACK_PEN );
280 dc.DrawRectangle( 0, 0, 100, 100 );
281 dc.SetBrush( *wxWHITE_BRUSH );
282 dc.DrawRectangle( 20, 20, 60, 60 );
283 dc.SelectObject( wxNullBitmap );
284
285 // try to find the directory with our images
286 wxString dir;
287 if ( wxFile::Exists(wxT("./horse.png")) )
288 dir = "./";
289 else if ( wxFile::Exists(wxT("../horse.png")) )
290 dir = "../";
291 else
292 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
293
294 wxImage image = bitmap.ConvertToImage();
295
296 #if wxUSE_LIBPNG
297 if ( !image.SaveFile( dir + _T("test.png"), wxBITMAP_TYPE_PNG ))
298 wxLogError(wxT("Can't save file"));
299
300 image.Destroy();
301
302 image.LoadFile( dir + _T("test.png") );
303 my_square = new wxBitmap( image );
304
305 image.Destroy();
306
307 if ( !image.LoadFile( dir + _T("horse.png")) )
308 wxLogError(wxT("Can't load PNG image"));
309 else
310 my_horse_png = new wxBitmap( image );
311 #endif // wxUSE_LIBPNG
312
313 #if wxUSE_LIBJPEG
314 image.Destroy();
315
316 if ( !image.LoadFile( dir + _T("horse.jpg")) )
317 wxLogError(wxT("Can't load JPG image"));
318 else
319 my_horse_jpeg = new wxBitmap( image );
320 #endif // wxUSE_LIBJPEG
321
322 #if wxUSE_GIF
323 image.Destroy();
324
325 if ( !image.LoadFile( dir + wxString("horse.gif")))
326 wxLogError(wxT("Can't load GIF image"));
327 else
328 my_horse_gif = new wxBitmap( image );
329 #endif
330
331 #if wxUSE_PCX
332 image.Destroy();
333
334 if ( !image.LoadFile( dir + _T("horse.pcx"), wxBITMAP_TYPE_PCX ) )
335 wxLogError(wxT("Can't load PCX image"));
336 else
337 my_horse_pcx = new wxBitmap( image );
338 #endif
339
340 image.Destroy();
341
342 if ( !image.LoadFile( dir + _T("horse.bmp"), wxBITMAP_TYPE_BMP ) )
343 wxLogError(wxT("Can't load BMP image"));
344 else
345 my_horse_bmp = new wxBitmap( image );
346
347 #if wxUSE_XPM
348 image.Destroy();
349
350 if ( !image.LoadFile( dir + _T("horse.xpm"), wxBITMAP_TYPE_XPM ) )
351 wxLogError(wxT("Can't load XPM image"));
352 else
353 my_horse_xpm = new wxBitmap( image );
354
355 if ( !image.SaveFile( dir + _T("test.xpm"), wxBITMAP_TYPE_XPM ))
356 wxLogError(wxT("Can't save file"));
357 #endif
358
359 #if wxUSE_PNM
360 image.Destroy();
361
362 if ( !image.LoadFile( dir + _T("horse.pnm"), wxBITMAP_TYPE_PNM ) )
363 wxLogError(wxT("Can't load PNM image"));
364 else
365 my_horse_pnm = new wxBitmap( image );
366 #endif
367
368 #if wxUSE_LIBTIFF
369 image.Destroy();
370
371 if ( !image.LoadFile( dir + _T("horse.tif"), wxBITMAP_TYPE_TIF ) )
372 wxLogError(wxT("Can't load TIFF image"));
373 else
374 my_horse_tiff = new wxBitmap( image );
375 #endif
376
377 CreateAntiAliasedBitmap();
378
379 my_smile_xbm = new wxBitmap( (const char*)smile_bits, smile_width,
380 smile_height, 1 );
381
382 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
383 // demonstrates XPM automatically using the mask when saving
384 if ( m_bmpSmileXpm.Ok() )
385 m_bmpSmileXpm.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM);
386 #endif
387
388 #if wxUSE_ICO_CUR
389 image.Destroy();
390
391 if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 0 ) )
392 wxLogError(wxT("Can't load first ICO image"));
393 else
394 my_horse_ico32 = new wxBitmap( image );
395
396 image.Destroy();
397
398 if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 1 ) )
399 wxLogError(wxT("Can't load second ICO image"));
400 else
401 my_horse_ico16 = new wxBitmap( image );
402
403 image.Destroy();
404
405 if ( !image.LoadFile( dir + _T("horse.ico") ) )
406 wxLogError(wxT("Can't load best ICO image"));
407 else
408 my_horse_ico = new wxBitmap( image );
409
410 image.Destroy();
411
412 if ( !image.LoadFile( dir + _T("horse.cur"), wxBITMAP_TYPE_CUR ) )
413 wxLogError(wxT("Can't load best ICO image"));
414 else
415 {
416 my_horse_cur = new wxBitmap( image );
417 xH = 30 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) ;
418 yH = 2420 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ;
419 }
420 #endif
421
422 image.Destroy();
423
424 // test image loading from stream
425 wxFile file(dir + _T("horse.bmp"));
426 off_t len = file.Length();
427 void *data = malloc(len);
428 if ( file.Read(data, len) != len )
429 wxLogError(_T("Reading bitmap file failed"));
430 else
431 {
432 wxMemoryInputStream mis(data, len);
433 if ( !image.LoadFile(mis) )
434 wxLogError(wxT("Can't load BMP image from stream"));
435 else
436 my_horse_bmp2 = new wxBitmap( image );
437 }
438
439 free(data);
440 }
441
442 MyCanvas::~MyCanvas()
443 {
444 delete my_horse_pnm;
445 delete my_horse_png;
446 delete my_horse_jpeg;
447 delete my_horse_gif;
448 delete my_horse_bmp;
449 delete my_horse_bmp2;
450 delete my_horse_pcx;
451 delete my_horse_tiff;
452 delete my_horse_xpm;
453 delete my_horse_ico32;
454 delete my_horse_ico16;
455 delete my_horse_ico;
456 delete my_horse_cur;
457 delete my_smile_xbm;
458 delete my_square;
459 delete my_anti;
460 }
461
462 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
463 {
464 wxPaintDC dc( this );
465 PrepareDC( dc );
466
467 dc.DrawText( _T("Loaded image"), 30, 10 );
468 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
469
470 dc.DrawText( _T("Drawn directly"), 150, 10 );
471 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
472 dc.SetPen( *wxBLACK_PEN );
473 dc.DrawRectangle( 150, 30, 100, 100 );
474 dc.SetBrush( *wxWHITE_BRUSH );
475 dc.DrawRectangle( 170, 50, 60, 60 );
476
477 if (my_anti && my_anti->Ok())
478 dc.DrawBitmap( *my_anti, 280, 30 );
479
480 dc.DrawText( _T("PNG handler"), 30, 135 );
481 if (my_horse_png && my_horse_png->Ok())
482 {
483 dc.DrawBitmap( *my_horse_png, 30, 150 );
484 wxRect rect(0,0,100,100);
485 wxBitmap sub( my_horse_png->GetSubBitmap(rect) );
486 dc.DrawText( _T("GetSubBitmap()"), 280, 190 );
487 dc.DrawBitmap( sub, 280, 210 );
488 }
489
490 dc.DrawText( _T("JPEG handler"), 30, 365 );
491 if (my_horse_jpeg && my_horse_jpeg->Ok())
492 dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
493
494 dc.DrawText( _T("GIF handler"), 30, 595 );
495 if (my_horse_gif && my_horse_gif->Ok())
496 dc.DrawBitmap( *my_horse_gif, 30, 610 );
497
498 dc.DrawText( _T("PCX handler"), 30, 825 );
499 if (my_horse_pcx && my_horse_pcx->Ok())
500 dc.DrawBitmap( *my_horse_pcx, 30, 840 );
501
502 dc.DrawText( _T("BMP handler"), 30, 1055 );
503 if (my_horse_bmp && my_horse_bmp->Ok())
504 dc.DrawBitmap( *my_horse_bmp, 30, 1070 );
505
506 dc.DrawText( _T("BMP read from memory"), 280, 1055 );
507 if (my_horse_bmp2 && my_horse_bmp2->Ok())
508 dc.DrawBitmap( *my_horse_bmp2, 280, 1070 );
509
510 dc.DrawText( _T("PNM handler"), 30, 1285 );
511 if (my_horse_pnm && my_horse_pnm->Ok())
512 dc.DrawBitmap( *my_horse_pnm, 30, 1300 );
513
514 dc.DrawText( _T("TIFF handler"), 30, 1515 );
515 if (my_horse_tiff && my_horse_tiff->Ok())
516 dc.DrawBitmap( *my_horse_tiff, 30, 1530 );
517
518 dc.DrawText( _T("XPM handler"), 30, 1745 );
519 if (my_horse_xpm && my_horse_xpm->Ok())
520 dc.DrawBitmap( *my_horse_xpm, 30, 1760 );
521
522
523 if (my_smile_xbm && my_smile_xbm->Ok())
524 {
525 dc.DrawText( _T("XBM bitmap"), 30, 1975 );
526 dc.DrawText( _T("(green on red)"), 30, 1990 );
527 dc.SetTextForeground( _T("GREEN") );
528 dc.SetTextBackground( _T("RED") );
529 dc.DrawBitmap( *my_smile_xbm, 30, 2010 );
530
531 dc.SetTextForeground( wxT("BLACK") );
532 dc.DrawText( _T("After wxImage conversion"), 150, 1975 );
533 dc.DrawText( _T("(red on white)"), 150, 1990 );
534 dc.SetTextForeground( wxT("RED") );
535 wxImage i = my_smile_xbm->ConvertToImage();
536 i.SetMaskColour( 255, 255, 255 );
537 i.Replace( 0, 0, 0,
538 wxRED_PEN->GetColour().Red(),
539 wxRED_PEN->GetColour().Green(),
540 wxRED_PEN->GetColour().Blue() );
541 dc.DrawBitmap( wxBitmap(i), 150, 2010, TRUE );
542 dc.SetTextForeground( wxT("BLACK") );
543 }
544
545
546 wxBitmap mono( 60,50,1 );
547 wxMemoryDC memdc;
548 memdc.SelectObject( mono );
549 memdc.SetPen( *wxBLACK_PEN );
550 memdc.SetBrush( *wxWHITE_BRUSH );
551 memdc.DrawRectangle( 0,0,60,50 );
552 memdc.SetTextForeground( *wxBLACK );
553 memdc.DrawText( _T("Hi!"), 5, 5 );
554 memdc.SetBrush( *wxBLACK_BRUSH );
555 memdc.DrawRectangle( 33,5,20,20 );
556 memdc.SetPen( *wxRED_PEN );
557 memdc.DrawLine( 5, 42, 50, 42 );
558 memdc.SelectObject( wxNullBitmap );
559
560 if (mono.Ok())
561 {
562 dc.DrawText( _T("Mono bitmap"), 30, 2095 );
563 dc.DrawText( _T("(red on green)"), 30, 2110 );
564 dc.SetTextForeground( wxT("RED") );
565 dc.SetTextBackground( wxT("GREEN") );
566 dc.DrawBitmap( mono, 30, 2130 );
567
568 dc.SetTextForeground( wxT("BLACK") );
569 dc.DrawText( _T("After wxImage conversion"), 150, 2095 );
570 dc.DrawText( _T("(red on white)"), 150, 2110 );
571 dc.SetTextForeground( wxT("RED") );
572 wxImage i = mono.ConvertToImage();
573 i.SetMaskColour( 255,255,255 );
574 i.Replace( 0,0,0,
575 wxRED_PEN->GetColour().Red(),
576 wxRED_PEN->GetColour().Green(),
577 wxRED_PEN->GetColour().Blue() );
578 dc.DrawBitmap( wxBitmap(i), 150, 2130, TRUE );
579 dc.SetTextForeground( wxT("BLACK") );
580 }
581
582 dc.DrawText(_T("XPM bitmap"), 30, 2230);
583 if ( m_bmpSmileXpm.Ok() )
584 {
585 dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, TRUE);
586 }
587
588 dc.DrawText(_T("XPM icon"), 150, 2230);
589 if ( m_iconSmileXpm.Ok() )
590 {
591 dc.DrawIcon(m_iconSmileXpm, 150, 2250);
592 }
593
594 dc.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
595 if (my_horse_ico32 && my_horse_ico32->Ok())
596 dc.DrawBitmap( *my_horse_ico32, 30, 2330, TRUE );
597
598 dc.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
599 if (my_horse_ico16 && my_horse_ico16->Ok())
600 dc.DrawBitmap( *my_horse_ico16, 230, 2330, TRUE );
601
602 dc.DrawText( _T("ICO handler (best image)"), 430, 2290 );
603 if (my_horse_ico && my_horse_ico->Ok())
604 dc.DrawBitmap( *my_horse_ico, 430, 2330, TRUE );
605
606 dc.DrawText( _T("CUR handler"), 30, 2390 );
607 if (my_horse_cur && my_horse_cur->Ok())
608 {
609 dc.DrawBitmap( *my_horse_cur, 30, 2420, TRUE );
610 dc.SetPen (*wxRED_PEN);
611 dc.DrawLine (xH-10,yH,xH+10,yH);
612 dc.DrawLine (xH,yH-10,xH,yH+10);
613 }
614 }
615
616 void MyCanvas::CreateAntiAliasedBitmap()
617 {
618 wxBitmap bitmap( 300, 300 );
619
620 wxMemoryDC dc;
621
622 dc.SelectObject( bitmap );
623
624 dc.Clear();
625
626 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
627 dc.SetTextForeground( wxT("RED") );
628 dc.DrawText( _T("This is anti-aliased Text."), 20, 20 );
629 dc.DrawText( _T("And a Rectangle."), 20, 60 );
630
631 dc.SetBrush( *wxRED_BRUSH );
632 dc.SetPen( *wxTRANSPARENT_PEN );
633 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
634
635 wxImage original= bitmap.ConvertToImage();
636 wxImage anti( 150, 150 );
637
638 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
639
640 for (int y = 1; y < 149; y++)
641 for (int x = 1; x < 149; x++)
642 {
643 int red = original.GetRed( x*2, y*2 ) +
644 original.GetRed( x*2-1, y*2 ) +
645 original.GetRed( x*2, y*2+1 ) +
646 original.GetRed( x*2+1, y*2+1 );
647 red = red/4;
648
649 int green = original.GetGreen( x*2, y*2 ) +
650 original.GetGreen( x*2-1, y*2 ) +
651 original.GetGreen( x*2, y*2+1 ) +
652 original.GetGreen( x*2+1, y*2+1 );
653 green = green/4;
654
655 int blue = original.GetBlue( x*2, y*2 ) +
656 original.GetBlue( x*2-1, y*2 ) +
657 original.GetBlue( x*2, y*2+1 ) +
658 original.GetBlue( x*2+1, y*2+1 );
659 blue = blue/4;
660 anti.SetRGB( x, y, red, green, blue );
661 }
662 my_anti = new wxBitmap(anti);
663 }
664
665 // MyFrame
666
667 const int ID_QUIT = 108;
668 const int ID_ABOUT = 109;
669 const int ID_NEW = 110;
670
671 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
672
673 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
674 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
675 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
676 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
677 END_EVENT_TABLE()
678
679 MyFrame::MyFrame()
680 : wxFrame( (wxFrame *)NULL, -1, _T("wxImage sample"),
681 wxPoint(20,20), wxSize(470,360) )
682 {
683 wxMenu *file_menu = new wxMenu();
684 file_menu->Append( ID_NEW, _T("&Show image..."));
685 file_menu->AppendSeparator();
686 file_menu->Append( ID_ABOUT, _T("&About..."));
687 file_menu->AppendSeparator();
688 file_menu->Append( ID_QUIT, _T("E&xit"));
689
690 wxMenuBar *menu_bar = new wxMenuBar();
691 menu_bar->Append(file_menu, _T("&File"));
692
693 SetMenuBar( menu_bar );
694
695 CreateStatusBar(2);
696 int widths[] = { -1, 100 };
697 SetStatusWidths( 2, widths );
698
699 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
700
701 // 500 width * 2500 height
702 m_canvas->SetScrollbars( 10, 10, 50, 250 );
703 }
704
705 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
706 {
707 Close( TRUE );
708 }
709
710 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
711 {
712 (void)wxMessageBox( _T("wxImage demo\n")
713 _T("Robert Roebling (c) 1998,2000"),
714 _T("About wxImage Demo"), wxICON_INFORMATION | wxOK );
715 }
716
717 void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
718 {
719 wxString filename = wxFileSelector(_T("Select image file"));
720 if ( !filename )
721 return;
722
723 wxImage image;
724 if ( !image.LoadFile(filename) )
725 {
726 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
727
728 return;
729 }
730
731 (new MyImageFrame(this, wxBitmap(image)))->Show();
732 }
733
734 //-----------------------------------------------------------------------------
735 // MyApp
736 //-----------------------------------------------------------------------------
737
738 bool MyApp::OnInit()
739 {
740 #if wxUSE_LIBPNG
741 wxImage::AddHandler( new wxPNGHandler );
742 #endif
743
744 #if wxUSE_LIBJPEG
745 wxImage::AddHandler( new wxJPEGHandler );
746 #endif
747
748 #if wxUSE_LIBTIFF
749 wxImage::AddHandler( new wxTIFFHandler );
750 #endif
751
752 #if wxUSE_GIF
753 wxImage::AddHandler( new wxGIFHandler );
754 #endif
755
756 #if wxUSE_PCX
757 wxImage::AddHandler( new wxPCXHandler );
758 #endif
759
760 #if wxUSE_PNM
761 wxImage::AddHandler( new wxPNMHandler );
762 #endif
763
764 #if wxUSE_XPM
765 wxImage::AddHandler( new wxXPMHandler );
766 #endif
767
768 #if wxUSE_ICO_CUR
769 wxImage::AddHandler( new wxICOHandler );
770 wxImage::AddHandler( new wxCURHandler );
771 #endif
772
773 wxFrame *frame = new MyFrame();
774 frame->Show( TRUE );
775
776 return TRUE;
777 }
778