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