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