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