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