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