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