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