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