use wxBitmap objects instead of pointers
[wxWidgets.git] / samples / image / image.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: samples/image/image.cpp
3 // Purpose: sample showing operations with wxImage
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 1998
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998-2005 Robert Roebling
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #include "wx/image.h"
24 #include "wx/file.h"
25 #include "wx/filename.h"
26 #include "wx/mstream.h"
27 #include "wx/wfstream.h"
28 #include "wx/quantize.h"
29
30 #if wxUSE_CLIPBOARD
31 #include "wx/dataobj.h"
32 #include "wx/clipbrd.h"
33 #endif // wxUSE_CLIPBOARD
34
35 #include "smile.xbm"
36 #include "smile.xpm"
37
38 #if defined(__WXMSW__)
39 #ifdef wxHAVE_RAW_BITMAP
40 #include "wx/rawbmp.h"
41 #endif
42 #endif
43
44 #if defined(__WXMAC__) || defined(__WXGTK__)
45 #define wxHAVE_RAW_BITMAP
46 #include "wx/rawbmp.h"
47 #endif
48
49 // derived classes
50
51 class MyFrame;
52 class MyApp;
53
54 // MyCanvas
55
56 class MyCanvas: public wxScrolledWindow
57 {
58 public:
59 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
60 ~MyCanvas();
61 void OnPaint( wxPaintEvent &event );
62 void CreateAntiAliasedBitmap();
63
64 wxBitmap my_horse_png;
65 wxBitmap my_horse_jpeg;
66 wxBitmap my_horse_gif;
67 wxBitmap my_horse_bmp;
68 wxBitmap my_horse_bmp2;
69 wxBitmap my_horse_pcx;
70 wxBitmap my_horse_pnm;
71 wxBitmap my_horse_tiff;
72 wxBitmap my_horse_xpm;
73 wxBitmap my_horse_ico32;
74 wxBitmap my_horse_ico16;
75 wxBitmap my_horse_ico;
76 wxBitmap my_horse_cur;
77
78 wxBitmap my_smile_xbm;
79 wxBitmap my_square;
80 wxBitmap my_anti;
81
82 wxBitmap my_horse_asciigrey_pnm;
83 wxBitmap my_horse_rawgrey_pnm;
84
85 wxBitmap colorized_horse_jpeg;
86
87 wxBitmap my_toucan;
88 wxBitmap my_toucan_flipped_horiz;
89 wxBitmap my_toucan_flipped_vert;
90 wxBitmap my_toucan_flipped_both;
91 wxBitmap my_toucan_head;
92
93 int xH, yH ;
94 int m_ani_images;
95 wxBitmap *my_horse_ani;
96
97 private:
98 wxBitmap m_bmpSmileXpm;
99 wxIcon m_iconSmileXpm;
100
101 DECLARE_EVENT_TABLE()
102 };
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 #ifdef wxHAVE_RAW_BITMAP
116 void OnTestRawBitmap( wxCommandEvent &event );
117 #endif // wxHAVE_RAW_BITMAP
118 void OnQuit( wxCommandEvent &event );
119
120 #if wxUSE_CLIPBOARD
121 void OnCopy(wxCommandEvent& event);
122 void OnPaste(wxCommandEvent& event);
123 #endif // wxUSE_CLIPBOARD
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, wxID_ANY, _T("Double click to save"),
137 wxDefaultPosition, wxDefaultSize,
138 wxCAPTION | wxSYSTEM_MENU | wxCLOSE_BOX),
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 #if wxUSE_FILEDLG
158 wxImage image = m_bitmap.ConvertToImage();
159
160 wxString savefilename = wxFileSelector( wxT("Save Image"),
161 wxEmptyString,
162 wxEmptyString,
163 (const wxChar *)NULL,
164 wxT("BMP files (*.bmp)|*.bmp|")
165 wxT("PNG files (*.png)|*.png|")
166 wxT("JPEG files (*.jpg)|*.jpg|")
167 wxT("GIF files (*.gif)|*.gif|")
168 wxT("TIFF files (*.tif)|*.tif|")
169 wxT("PCX files (*.pcx)|*.pcx|")
170 wxT("ICO files (*.ico)|*.ico|")
171 wxT("CUR files (*.cur)|*.cur"),
172 wxSAVE,
173 this);
174
175 if ( savefilename.empty() )
176 return;
177
178 wxString extension;
179 wxFileName::SplitPath(savefilename, NULL, NULL, &extension);
180
181 bool saved = false;
182 if ( extension == _T("bpp") )
183 {
184 static const int bppvalues[] =
185 {
186 wxBMP_1BPP,
187 wxBMP_1BPP_BW,
188 wxBMP_4BPP,
189 wxBMP_8BPP,
190 wxBMP_8BPP_GREY,
191 wxBMP_8BPP_RED,
192 wxBMP_8BPP_PALETTE,
193 wxBMP_24BPP
194 };
195
196 const wxString bppchoices[] =
197 {
198 _T("1 bpp color"),
199 _T("1 bpp B&W"),
200 _T("4 bpp color"),
201 _T("8 bpp color"),
202 _T("8 bpp greyscale"),
203 _T("8 bpp red"),
204 _T("8 bpp own palette"),
205 _T("24 bpp")
206 };
207
208 int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"),
209 _T("Image sample: save file"),
210 WXSIZEOF(bppchoices),
211 bppchoices,
212 this);
213 if ( bppselection != -1 )
214 {
215 int format = bppvalues[bppselection];
216 image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, format);
217
218 if ( format == wxBMP_8BPP_PALETTE )
219 {
220 unsigned char *cmap = new unsigned char [256];
221 for ( int i = 0; i < 256; i++ )
222 cmap[i] = (unsigned char)i;
223 image.SetPalette(wxPalette(256, cmap, cmap, cmap));
224
225 delete[] cmap;
226 }
227 }
228 }
229 else if ( extension == _T("png") )
230 {
231 static const int pngvalues[] =
232 {
233 wxPNG_TYPE_COLOUR,
234 wxPNG_TYPE_COLOUR,
235 wxPNG_TYPE_GREY,
236 wxPNG_TYPE_GREY,
237 wxPNG_TYPE_GREY_RED,
238 wxPNG_TYPE_GREY_RED,
239 };
240
241 const wxString pngchoices[] =
242 {
243 _T("Colour 8bpp"),
244 _T("Colour 16bpp"),
245 _T("Grey 8bpp"),
246 _T("Grey 16bpp"),
247 _T("Grey red 8bpp"),
248 _T("Grey red 16bpp"),
249 };
250
251 int sel = wxGetSingleChoiceIndex(_T("Set PNG format"),
252 _T("Image sample: save file"),
253 WXSIZEOF(pngchoices),
254 pngchoices,
255 this);
256 if ( sel != -1 )
257 {
258 image.SetOption(wxIMAGE_OPTION_PNG_FORMAT, pngvalues[sel]);
259 image.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH, sel % 2 ? 16 : 8);
260 }
261 }
262 else if ( extension == _T("cur") )
263 {
264 image.Rescale(32,32);
265 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0);
266 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0);
267 // This shows how you can save an image with explicitly
268 // specified image format:
269 saved = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR);
270 }
271
272 if ( !saved )
273 {
274 // This one guesses image format from filename extension
275 // (it may fail if the extension is not recognized):
276 image.SaveFile(savefilename);
277 }
278 #endif // wxUSE_FILEDLG
279 }
280
281 private:
282 wxBitmap m_bitmap;
283
284 DECLARE_EVENT_TABLE()
285 };
286
287 #ifdef wxHAVE_RAW_BITMAP
288
289 #include "wx/rawbmp.h"
290
291 class MyRawBitmapFrame : public wxFrame
292 {
293 public:
294 enum
295 {
296 BORDER = 15,
297 SIZE = 150,
298 REAL_SIZE = SIZE - 2*BORDER
299 };
300
301 MyRawBitmapFrame(wxFrame *parent)
302 : wxFrame(parent, wxID_ANY, _T("Raw bitmaps (how exciting)")),
303 m_bitmap(SIZE, SIZE, 32)
304 {
305 SetClientSize(SIZE, SIZE);
306
307 // another possibility: wxNativePixelData (don't forget to remove code
308 // setting alpha in the loop below then)
309 typedef wxAlphaPixelData Data;
310 // typedef wxNativePixelData Data;
311
312 // First, clear the whole bitmap by making it alpha
313 {
314 Data data( m_bitmap, wxPoint(0,0), wxSize(SIZE, SIZE) );
315 if ( !data )
316 {
317 wxLogError(_T("Failed to gain raw access to bitmap data"));
318 return;
319 }
320 data.UseAlpha();
321 Data::Iterator p(data);
322 for ( int y = 0; y < SIZE; ++y )
323 {
324 Data::Iterator rowStart = p;
325 for ( int x = 0; x < SIZE; ++x )
326 {
327 p.Alpha() = 0;
328 ++p; // same as p.OffsetX(1)
329 }
330 p = rowStart;
331 p.OffsetY(data, 1);
332 }
333 }
334
335 // Then, draw colourful alpha-blended stripes
336 Data data(m_bitmap, wxPoint(BORDER, BORDER) , wxSize(REAL_SIZE, REAL_SIZE));
337 if ( !data )
338 {
339 wxLogError(_T("Failed to gain raw access to bitmap data"));
340 return;
341 }
342
343 data.UseAlpha();
344
345 Data::Iterator p(data);
346
347 for ( int y = 0; y < REAL_SIZE; ++y )
348 {
349 Data::Iterator rowStart = p;
350
351 int r = y < REAL_SIZE/3 ? 255 : 0,
352 g = (REAL_SIZE/3 <= y) && (y < 2*(REAL_SIZE/3)) ? 255 : 0,
353 b = 2*(REAL_SIZE/3) <= y ? 255 : 0;
354
355 for ( int x = 0; x < REAL_SIZE; ++x )
356 {
357 // note that RGB must be premultiplied by alpha
358 unsigned a = (Data::Iterator::ChannelType)((x*255.)/REAL_SIZE);
359 p.Red() = r * a / 256;
360 p.Green() = g * a / 256;
361 p.Blue() = b * a / 256;
362 p.Alpha() = a;
363
364 ++p; // same as p.OffsetX(1)
365 }
366
367 p = rowStart;
368 p.OffsetY(data, 1);
369 }
370 }
371
372 void OnPaint(wxPaintEvent& WXUNUSED(event))
373 {
374 wxPaintDC dc( this );
375 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER);
376 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE/2 - BORDER);
377 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE - 2*BORDER);
378 dc.DrawBitmap( m_bitmap, 0, 0, true /* use mask */ );
379 }
380
381 private:
382 wxBitmap m_bitmap;
383
384 DECLARE_EVENT_TABLE()
385 };
386
387 #endif // wxHAVE_RAW_BITMAP
388
389 // MyApp
390
391 class MyApp: public wxApp
392 {
393 public:
394 virtual bool OnInit();
395 };
396
397 // main program
398
399 IMPLEMENT_APP(MyApp)
400
401 // MyCanvas
402
403 BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
404 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground)
405 EVT_PAINT(MyImageFrame::OnPaint)
406 EVT_LEFT_DCLICK(MyImageFrame::OnSave)
407 END_EVENT_TABLE()
408
409 #ifdef wxHAVE_RAW_BITMAP
410
411 BEGIN_EVENT_TABLE(MyRawBitmapFrame, wxFrame)
412 EVT_PAINT(MyRawBitmapFrame::OnPaint)
413 END_EVENT_TABLE()
414
415 #endif // wxHAVE_RAW_BITMAP
416
417 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
418 EVT_PAINT(MyCanvas::OnPaint)
419 END_EVENT_TABLE()
420
421 MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
422 const wxPoint &pos, const wxSize &size )
423 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
424 , m_bmpSmileXpm((const char **) smile_xpm)
425 , m_iconSmileXpm((const char **) smile_xpm)
426 {
427 my_horse_ani = NULL;
428 m_ani_images = 0 ;
429
430 SetBackgroundColour(* wxWHITE);
431
432 wxBitmap bitmap( 100, 100 );
433
434 wxMemoryDC dc;
435 dc.SelectObject( bitmap );
436 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
437 dc.SetPen( *wxBLACK_PEN );
438 dc.DrawRectangle( 0, 0, 100, 100 );
439 dc.SetBrush( *wxWHITE_BRUSH );
440 dc.DrawRectangle( 20, 20, 60, 60 );
441 dc.SelectObject( wxNullBitmap );
442
443 // try to find the directory with our images
444 wxString dir;
445 if ( wxFile::Exists(wxT("./horse.png")) )
446 dir = wxT("./");
447 else if ( wxFile::Exists(wxT("../horse.png")) )
448 dir = wxT("../");
449 else
450 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
451
452 wxImage image = bitmap.ConvertToImage();
453
454 #if wxUSE_LIBPNG
455 if ( !image.SaveFile( dir + _T("test.png"), wxBITMAP_TYPE_PNG ))
456 wxLogError(wxT("Can't save file"));
457
458 image.Destroy();
459
460 if ( image.LoadFile( dir + _T("test.png") ) )
461 my_square = wxBitmap( image );
462
463 image.Destroy();
464
465 if ( !image.LoadFile( dir + _T("horse.png")) )
466 wxLogError(wxT("Can't load PNG image"));
467 else
468 my_horse_png = wxBitmap( image );
469
470 image = wxImage(wxT("toucan.png"));
471 my_toucan = wxBitmap(image);
472 my_toucan_flipped_horiz = wxBitmap(image.Mirror(true));
473 my_toucan_flipped_vert = wxBitmap(image.Mirror(false));
474 my_toucan_flipped_both = wxBitmap(image.Mirror(true).Mirror(false));
475 my_toucan_head = wxBitmap(image.GetSubImage(wxRect(40, 7, 80, 60)));
476
477 #endif // wxUSE_LIBPNG
478
479 #if wxUSE_LIBJPEG
480 image.Destroy();
481
482 if ( !image.LoadFile( dir + _T("horse.jpg")) )
483 wxLogError(wxT("Can't load JPG image"));
484 else
485 {
486 my_horse_jpeg = wxBitmap( image );
487 // Colorize by rotating green hue to red
488 wxImage::HSVValue greenHSV = wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0));
489 wxImage::HSVValue redHSV = wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0));
490 image.RotateHue(redHSV.hue - greenHSV.hue);
491 colorized_horse_jpeg = wxBitmap( image );
492 }
493 #endif // wxUSE_LIBJPEG
494
495 #if wxUSE_GIF
496 image.Destroy();
497
498 if ( !image.LoadFile( dir + _T("horse.gif" )) )
499 wxLogError(wxT("Can't load GIF image"));
500 else
501 my_horse_gif = wxBitmap( image );
502 #endif
503
504 #if wxUSE_PCX
505 image.Destroy();
506
507 if ( !image.LoadFile( dir + _T("horse.pcx"), wxBITMAP_TYPE_PCX ) )
508 wxLogError(wxT("Can't load PCX image"));
509 else
510 my_horse_pcx = wxBitmap( image );
511 #endif
512
513 image.Destroy();
514
515 if ( !image.LoadFile( dir + _T("horse.bmp"), wxBITMAP_TYPE_BMP ) )
516 wxLogError(wxT("Can't load BMP image"));
517 else
518 my_horse_bmp = wxBitmap( image );
519
520 #if wxUSE_XPM
521 image.Destroy();
522
523 if ( !image.LoadFile( dir + _T("horse.xpm"), wxBITMAP_TYPE_XPM ) )
524 wxLogError(wxT("Can't load XPM image"));
525 else
526 my_horse_xpm = wxBitmap( image );
527
528 if ( !image.SaveFile( dir + _T("test.xpm"), wxBITMAP_TYPE_XPM ))
529 wxLogError(wxT("Can't save file"));
530 #endif
531
532 #if wxUSE_PNM
533 image.Destroy();
534
535 if ( !image.LoadFile( dir + _T("horse.pnm"), wxBITMAP_TYPE_PNM ) )
536 wxLogError(wxT("Can't load PNM image"));
537 else
538 my_horse_pnm = wxBitmap( image );
539
540 image.Destroy();
541
542 if ( !image.LoadFile( dir + _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM ) )
543 wxLogError(wxT("Can't load PNM image"));
544 else
545 my_horse_asciigrey_pnm = wxBitmap( image );
546
547 image.Destroy();
548
549 if ( !image.LoadFile( dir + _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM ) )
550 wxLogError(wxT("Can't load PNM image"));
551 else
552 my_horse_rawgrey_pnm = wxBitmap( image );
553 #endif
554
555 #if wxUSE_LIBTIFF
556 image.Destroy();
557
558 if ( !image.LoadFile( dir + _T("horse.tif"), wxBITMAP_TYPE_TIF ) )
559 wxLogError(wxT("Can't load TIFF image"));
560 else
561 my_horse_tiff = wxBitmap( image );
562 #endif
563
564 CreateAntiAliasedBitmap();
565
566 my_smile_xbm = wxBitmap( (const char*)smile_bits, smile_width,
567 smile_height, 1 );
568
569 // demonstrates XPM automatically using the mask when saving
570 if ( m_bmpSmileXpm.Ok() )
571 m_bmpSmileXpm.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM);
572
573 #if wxUSE_ICO_CUR
574 image.Destroy();
575
576 if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 0 ) )
577 wxLogError(wxT("Can't load first ICO image"));
578 else
579 my_horse_ico32 = wxBitmap( image );
580
581 image.Destroy();
582
583 if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 1 ) )
584 wxLogError(wxT("Can't load second ICO image"));
585 else
586 my_horse_ico16 = wxBitmap( image );
587
588 image.Destroy();
589
590 if ( !image.LoadFile( dir + _T("horse.ico") ) )
591 wxLogError(wxT("Can't load best ICO image"));
592 else
593 my_horse_ico = wxBitmap( image );
594
595 image.Destroy();
596
597 if ( !image.LoadFile( dir + _T("horse.cur"), wxBITMAP_TYPE_CUR ) )
598 wxLogError(wxT("Can't load best ICO image"));
599 else
600 {
601 my_horse_cur = wxBitmap( image );
602 xH = 30 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) ;
603 yH = 2420 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ;
604 }
605
606 m_ani_images = wxImage::GetImageCount ( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI );
607 if (m_ani_images==0)
608 wxLogError(wxT("No ANI-format images found"));
609 else
610 my_horse_ani = new wxBitmap [m_ani_images];
611 int i ;
612 for (i=0; i < m_ani_images; i++)
613 {
614 image.Destroy();
615 if (!image.LoadFile( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI, i ))
616 {
617 wxString tmp = wxT("Can't load image number ");
618 tmp << i ;
619 wxLogError(tmp);
620 }
621 else
622 my_horse_ani [i] = wxBitmap( image );
623 }
624 #endif // wxUSE_ICO_CUR
625
626 image.Destroy();
627
628 // test image loading from stream
629 wxFile file(dir + _T("horse.bmp"));
630 if ( file.IsOpened() )
631 {
632 wxFileOffset len = file.Length();
633 size_t dataSize = (size_t)len;
634 void *data = malloc(dataSize);
635 if ( file.Read(data, dataSize) != len )
636 wxLogError(_T("Reading bitmap file failed"));
637 else
638 {
639 wxMemoryInputStream mis(data, dataSize);
640 if ( !image.LoadFile(mis) )
641 wxLogError(wxT("Can't load BMP image from stream"));
642 else
643 my_horse_bmp2 = wxBitmap( image );
644 }
645
646 free(data);
647 }
648 }
649
650 MyCanvas::~MyCanvas()
651 {
652 delete [] my_horse_ani;
653 }
654
655 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
656 {
657 wxPaintDC dc( this );
658 PrepareDC( dc );
659
660 dc.DrawText( _T("Loaded image"), 30, 10 );
661 if (my_square.Ok())
662 dc.DrawBitmap( my_square, 30, 30 );
663
664 dc.DrawText( _T("Drawn directly"), 150, 10 );
665 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
666 dc.SetPen( *wxBLACK_PEN );
667 dc.DrawRectangle( 150, 30, 100, 100 );
668 dc.SetBrush( *wxWHITE_BRUSH );
669 dc.DrawRectangle( 170, 50, 60, 60 );
670
671 if (my_anti.Ok())
672 dc.DrawBitmap( my_anti, 280, 30 );
673
674 dc.DrawText( _T("PNG handler"), 30, 135 );
675 if (my_horse_png.Ok())
676 {
677 dc.DrawBitmap( my_horse_png, 30, 150 );
678 wxRect rect(0,0,100,100);
679 wxBitmap sub( my_horse_png.GetSubBitmap(rect) );
680 dc.DrawText( _T("GetSubBitmap()"), 280, 175 );
681 dc.DrawBitmap( sub, 280, 195 );
682 }
683
684 dc.DrawText( _T("JPEG handler"), 30, 365 );
685 if (my_horse_jpeg.Ok())
686 dc.DrawBitmap( my_horse_jpeg, 30, 380 );
687
688 dc.DrawText( _T("GIF handler"), 30, 595 );
689 if (my_horse_gif.Ok())
690 dc.DrawBitmap( my_horse_gif, 30, 610 );
691
692 dc.DrawText( _T("PCX handler"), 30, 825 );
693 if (my_horse_pcx.Ok())
694 dc.DrawBitmap( my_horse_pcx, 30, 840 );
695
696 dc.DrawText( _T("BMP handler"), 30, 1055 );
697 if (my_horse_bmp.Ok())
698 dc.DrawBitmap( my_horse_bmp, 30, 1070 );
699
700 dc.DrawText( _T("BMP read from memory"), 280, 1055 );
701 if (my_horse_bmp2.Ok())
702 dc.DrawBitmap( my_horse_bmp2, 280, 1070 );
703
704 dc.DrawText( _T("PNM handler"), 30, 1285 );
705 if (my_horse_pnm.Ok())
706 dc.DrawBitmap( my_horse_pnm, 30, 1300 );
707
708 dc.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 );
709 if (my_horse_asciigrey_pnm.Ok())
710 dc.DrawBitmap( my_horse_asciigrey_pnm, 280, 1300 );
711
712 dc.DrawText( _T("PNM handler (raw grey)"), 530, 1285 );
713 if (my_horse_rawgrey_pnm.Ok())
714 dc.DrawBitmap( my_horse_rawgrey_pnm, 530, 1300 );
715
716 dc.DrawText( _T("TIFF handler"), 30, 1515 );
717 if (my_horse_tiff.Ok())
718 dc.DrawBitmap( my_horse_tiff, 30, 1530 );
719
720 dc.DrawText( _T("XPM handler"), 30, 1745 );
721 if (my_horse_xpm.Ok())
722 dc.DrawBitmap( my_horse_xpm, 30, 1760 );
723
724 {
725 int x = 200, y = 300, yy = 170;;
726
727 dc.DrawText(wxT("Original toucan"), x+50, y);
728 dc.DrawBitmap(my_toucan, x, y+15);
729 y += yy;
730 dc.DrawText(wxT("Flipped horizontally"), x+50, y);
731 dc.DrawBitmap(my_toucan_flipped_horiz, x, y+15);
732 y += yy;
733 dc.DrawText(wxT("Flipped vertically"), x+50, y);
734 dc.DrawBitmap(my_toucan_flipped_vert, x, y+15);
735 y += yy;
736 dc.DrawText(wxT("Flipped both h&v"), x+50, y);
737 dc.DrawBitmap(my_toucan_flipped_both, x, y+15);
738
739 y += yy;
740 dc.DrawText(wxT("Toucan's head"), x+50, y);
741 dc.DrawBitmap(my_toucan_head, x, y+15);
742 }
743
744 if (my_smile_xbm.Ok())
745 {
746 dc.DrawText( _T("XBM bitmap"), 30, 1975 );
747 dc.DrawText( _T("(green on red)"), 30, 1990 );
748 dc.SetTextForeground( _T("GREEN") );
749 dc.SetTextBackground( _T("RED") );
750 dc.DrawBitmap( my_smile_xbm, 30, 2010 );
751
752 dc.SetTextForeground( wxT("BLACK") );
753 dc.DrawText( _T("After wxImage conversion"), 150, 1975 );
754 dc.DrawText( _T("(red on white)"), 150, 1990 );
755 dc.SetTextForeground( wxT("RED") );
756 wxImage i = my_smile_xbm.ConvertToImage();
757 i.SetMaskColour( 255, 255, 255 );
758 i.Replace( 0, 0, 0,
759 wxRED_PEN->GetColour().Red(),
760 wxRED_PEN->GetColour().Green(),
761 wxRED_PEN->GetColour().Blue() );
762 dc.DrawBitmap( wxBitmap(i), 150, 2010, true );
763 dc.SetTextForeground( wxT("BLACK") );
764 }
765
766
767 wxBitmap mono( 60,50,1 );
768 wxMemoryDC memdc;
769 memdc.SelectObject( mono );
770 memdc.SetPen( *wxBLACK_PEN );
771 memdc.SetBrush( *wxWHITE_BRUSH );
772 memdc.DrawRectangle( 0,0,60,50 );
773 memdc.SetTextForeground( *wxBLACK );
774 #ifndef __WXGTK20__
775 // I cannot convince GTK2 to draw into mono bitmaps
776 memdc.DrawText( _T("Hi!"), 5, 5 );
777 #endif
778 memdc.SetBrush( *wxBLACK_BRUSH );
779 memdc.DrawRectangle( 33,5,20,20 );
780 memdc.SetPen( *wxRED_PEN );
781 memdc.DrawLine( 5, 42, 50, 42 );
782 memdc.SelectObject( wxNullBitmap );
783
784 if (mono.Ok())
785 {
786 dc.DrawText( _T("Mono bitmap"), 30, 2095 );
787 dc.DrawText( _T("(red on green)"), 30, 2110 );
788 dc.SetTextForeground( wxT("RED") );
789 dc.SetTextBackground( wxT("GREEN") );
790 dc.DrawBitmap( mono, 30, 2130 );
791
792 dc.SetTextForeground( wxT("BLACK") );
793 dc.DrawText( _T("After wxImage conversion"), 150, 2095 );
794 dc.DrawText( _T("(red on white)"), 150, 2110 );
795 dc.SetTextForeground( wxT("RED") );
796 wxImage i = mono.ConvertToImage();
797 i.SetMaskColour( 255,255,255 );
798 i.Replace( 0,0,0,
799 wxRED_PEN->GetColour().Red(),
800 wxRED_PEN->GetColour().Green(),
801 wxRED_PEN->GetColour().Blue() );
802 dc.DrawBitmap( wxBitmap(i), 150, 2130, true );
803 dc.SetTextForeground( wxT("BLACK") );
804 }
805
806 // For testing transparency
807 dc.SetBrush( *wxRED_BRUSH );
808 dc.DrawRectangle( 20, 2220, 560, 68 );
809
810 dc.DrawText(_T("XPM bitmap"), 30, 2230 );
811 if ( m_bmpSmileXpm.Ok() )
812 dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, true);
813
814 dc.DrawText(_T("XPM icon"), 110, 2230 );
815 if ( m_iconSmileXpm.Ok() )
816 dc.DrawIcon(m_iconSmileXpm, 110, 2250);
817
818 // testing icon -> bitmap conversion
819 wxBitmap to_blit( m_iconSmileXpm );
820 if (to_blit.Ok())
821 {
822 dc.DrawText( _T("SubBitmap"), 170, 2230 );
823 wxBitmap sub = to_blit.GetSubBitmap( wxRect(0,0,15,15) );
824 if (sub.Ok())
825 dc.DrawBitmap( sub, 170, 2250, true );
826
827 dc.DrawText( _T("Enlarged"), 250, 2230 );
828 dc.SetUserScale( 1.5, 1.5 );
829 dc.DrawBitmap( to_blit, (int)(250/1.5), (int)(2250/1.5), true );
830 dc.SetUserScale( 2, 2 );
831 dc.DrawBitmap( to_blit, (int)(300/2), (int)(2250/2), true );
832 dc.SetUserScale( 1.0, 1.0 );
833
834 dc.DrawText( _T("Blit"), 400, 2230);
835 wxMemoryDC blit_dc;
836 blit_dc.SelectObject( to_blit );
837 dc.Blit( 400, 2250, to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
838 dc.SetUserScale( 1.5, 1.5 );
839 dc.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
840 dc.SetUserScale( 2, 2 );
841 dc.Blit( (int)(500/2), (int)(2250/2), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
842 dc.SetUserScale( 1.0, 1.0 );
843 }
844
845 dc.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
846 if (my_horse_ico32.Ok())
847 dc.DrawBitmap( my_horse_ico32, 30, 2330, true );
848
849 dc.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
850 if (my_horse_ico16.Ok())
851 dc.DrawBitmap( my_horse_ico16, 230, 2330, true );
852
853 dc.DrawText( _T("ICO handler (best image)"), 430, 2290 );
854 if (my_horse_ico.Ok())
855 dc.DrawBitmap( my_horse_ico, 430, 2330, true );
856
857 dc.DrawText( _T("CUR handler"), 30, 2390 );
858 if (my_horse_cur.Ok())
859 {
860 dc.DrawBitmap( my_horse_cur, 30, 2420, true );
861 dc.SetPen (*wxRED_PEN);
862 dc.DrawLine (xH-10,yH,xH+10,yH);
863 dc.DrawLine (xH,yH-10,xH,yH+10);
864 }
865
866 dc.DrawText( _T("ANI handler"), 230, 2390 );
867 for ( int i=0; i < m_ani_images; i++ )
868 {
869 if (my_horse_ani[i].Ok())
870 {
871 dc.DrawBitmap( my_horse_ani[i], 230 + i * 2 * my_horse_ani[i].GetWidth() , 2420, true );
872 }
873 }
874
875 #if wxUSE_LIBJPEG
876 if (colorized_horse_jpeg.Ok())
877 {
878 dc.DrawText( _T("Colorize image by rotating green hue to red"), 30, 2490 );
879 dc.DrawBitmap( colorized_horse_jpeg, 30, 2520 );
880 }
881 #endif // wxUSE_LIBJPEG
882
883 }
884
885 void MyCanvas::CreateAntiAliasedBitmap()
886 {
887 wxBitmap bitmap( 300, 300 );
888
889 wxMemoryDC dc;
890
891 dc.SelectObject( bitmap );
892
893 dc.Clear();
894
895 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
896 dc.SetTextForeground( wxT("RED") );
897 dc.DrawText( _T("This is anti-aliased Text."), 20, 5 );
898 dc.DrawText( _T("And a Rectangle."), 20, 45 );
899
900 dc.SetBrush( *wxRED_BRUSH );
901 dc.SetPen( *wxTRANSPARENT_PEN );
902 dc.DrawRoundedRectangle( 20, 85, 200, 180, 20 );
903
904 wxImage original= bitmap.ConvertToImage();
905 wxImage anti( 150, 150 );
906
907 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
908
909 for (int y = 1; y < 149; y++)
910 for (int x = 1; x < 149; x++)
911 {
912 int red = original.GetRed( x*2, y*2 ) +
913 original.GetRed( x*2-1, y*2 ) +
914 original.GetRed( x*2, y*2+1 ) +
915 original.GetRed( x*2+1, y*2+1 );
916 red = red/4;
917
918 int green = original.GetGreen( x*2, y*2 ) +
919 original.GetGreen( x*2-1, y*2 ) +
920 original.GetGreen( x*2, y*2+1 ) +
921 original.GetGreen( x*2+1, y*2+1 );
922 green = green/4;
923
924 int blue = original.GetBlue( x*2, y*2 ) +
925 original.GetBlue( x*2-1, y*2 ) +
926 original.GetBlue( x*2, y*2+1 ) +
927 original.GetBlue( x*2+1, y*2+1 );
928 blue = blue/4;
929 anti.SetRGB( x, y, (unsigned char)red, (unsigned char)green, (unsigned char)blue );
930 }
931 my_anti = wxBitmap(anti);
932 }
933
934 // MyFrame
935
936 enum
937 {
938 ID_QUIT = wxID_EXIT,
939 ID_ABOUT = wxID_ABOUT,
940 ID_NEW = 100,
941 ID_SHOWRAW = 101
942 };
943
944 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
945
946 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
947 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
948 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
949 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
950 #ifdef wxHAVE_RAW_BITMAP
951 EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap)
952 #endif
953
954 #if wxUSE_CLIPBOARD
955 EVT_MENU(wxID_COPY, MyFrame::OnCopy)
956 EVT_MENU(wxID_PASTE, MyFrame::OnPaste)
957 #endif // wxUSE_CLIPBOARD
958 END_EVENT_TABLE()
959
960 MyFrame::MyFrame()
961 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxImage sample"),
962 wxPoint(20,20), wxSize(470,360) )
963 {
964 wxMenuBar *menu_bar = new wxMenuBar();
965
966 wxMenu *menuImage = new wxMenu;
967 menuImage->Append( ID_NEW, _T("&Show any image...\tCtrl-O"));
968
969 #ifdef wxHAVE_RAW_BITMAP
970 menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R"));
971 #endif
972 menuImage->AppendSeparator();
973 menuImage->Append( ID_ABOUT, _T("&About..."));
974 menuImage->AppendSeparator();
975 menuImage->Append( ID_QUIT, _T("E&xit\tCtrl-Q"));
976 menu_bar->Append(menuImage, _T("&Image"));
977
978 #if wxUSE_CLIPBOARD
979 wxMenu *menuClipboard = new wxMenu;
980 menuClipboard->Append(wxID_COPY, _T("&Copy test image\tCtrl-C"));
981 menuClipboard->Append(wxID_PASTE, _T("&Paste image\tCtrl-V"));
982 menu_bar->Append(menuClipboard, _T("&Clipboard"));
983 #endif // wxUSE_CLIPBOARD
984
985 SetMenuBar( menu_bar );
986
987 #if wxUSE_STATUSBAR
988 CreateStatusBar(2);
989 int widths[] = { -1, 100 };
990 SetStatusWidths( 2, widths );
991 #endif // wxUSE_STATUSBAR
992
993 m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) );
994
995 // 500 width * 2750 height
996 m_canvas->SetScrollbars( 10, 10, 50, 275 );
997 }
998
999 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
1000 {
1001 Close( true );
1002 }
1003
1004 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
1005 {
1006 (void)wxMessageBox( _T("wxImage demo\n")
1007 _T("Robert Roebling (c) 1998,2000"),
1008 _T("About wxImage Demo"), wxICON_INFORMATION | wxOK );
1009 }
1010
1011 void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
1012 {
1013 #if wxUSE_FILEDLG
1014 wxString filename = wxFileSelector(_T("Select image file"));
1015 if ( !filename )
1016 return;
1017
1018 wxImage image;
1019 if ( !image.LoadFile(filename) )
1020 {
1021 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
1022
1023 return;
1024 }
1025
1026 (new MyImageFrame(this, wxBitmap(image)))->Show();
1027 #endif // wxUSE_FILEDLG
1028 }
1029
1030 #ifdef wxHAVE_RAW_BITMAP
1031
1032 void MyFrame::OnTestRawBitmap( wxCommandEvent &WXUNUSED(event) )
1033 {
1034 (new MyRawBitmapFrame(this))->Show();
1035 }
1036
1037 #endif // wxHAVE_RAW_BITMAP
1038
1039 #if wxUSE_CLIPBOARD
1040
1041 void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
1042 {
1043 wxBitmapDataObject *dobjBmp = new wxBitmapDataObject;
1044 dobjBmp->SetBitmap(m_canvas->my_horse_png);
1045
1046 wxTheClipboard->Open();
1047
1048 if ( !wxTheClipboard->SetData(dobjBmp) )
1049 {
1050 wxLogError(_T("Failed to copy bitmap to clipboard"));
1051 }
1052
1053 wxTheClipboard->Close();
1054 }
1055
1056 void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
1057 {
1058 wxBitmapDataObject dobjBmp;
1059
1060 wxTheClipboard->Open();
1061 if ( !wxTheClipboard->GetData(dobjBmp) )
1062 {
1063 wxLogMessage(_T("No bitmap data in the clipboard"));
1064 }
1065 else
1066 {
1067 (new MyImageFrame(this, dobjBmp.GetBitmap()))->Show();
1068 }
1069 wxTheClipboard->Close();
1070 }
1071
1072 #endif // wxUSE_CLIPBOARD
1073
1074 //-----------------------------------------------------------------------------
1075 // MyApp
1076 //-----------------------------------------------------------------------------
1077
1078 bool MyApp::OnInit()
1079 {
1080 #if wxUSE_LIBPNG
1081 wxImage::AddHandler( new wxPNGHandler );
1082 #endif
1083
1084 #if wxUSE_LIBJPEG
1085 wxImage::AddHandler( new wxJPEGHandler );
1086 #endif
1087
1088 #if wxUSE_LIBTIFF
1089 wxImage::AddHandler( new wxTIFFHandler );
1090 #endif
1091
1092 #if wxUSE_GIF
1093 wxImage::AddHandler( new wxGIFHandler );
1094 #endif
1095
1096 #if wxUSE_PCX
1097 wxImage::AddHandler( new wxPCXHandler );
1098 #endif
1099
1100 #if wxUSE_PNM
1101 wxImage::AddHandler( new wxPNMHandler );
1102 #endif
1103
1104 #if wxUSE_XPM
1105 wxImage::AddHandler( new wxXPMHandler );
1106 #endif
1107
1108 #if wxUSE_ICO_CUR
1109 wxImage::AddHandler( new wxICOHandler );
1110 wxImage::AddHandler( new wxCURHandler );
1111 wxImage::AddHandler( new wxANIHandler );
1112 #endif
1113
1114 wxFrame *frame = new MyFrame();
1115 frame->Show( true );
1116
1117 return true;
1118 }