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