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