added wxBitmap::UseAlpha()
[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
38 #if defined(__WXMSW__)
39 #define wxHAVE_RAW_BITMAP
40 #endif
41
42 // derived classes
43
44 class MyFrame;
45 class MyApp;
46
47 // MyCanvas
48
49 class MyCanvas: public wxScrolledWindow
50 {
51 public:
52 MyCanvas() {}
53 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
54 ~MyCanvas();
55 void OnPaint( wxPaintEvent &event );
56 void CreateAntiAliasedBitmap();
57
58 wxBitmap *my_horse_png;
59 wxBitmap *my_horse_jpeg;
60 wxBitmap *my_horse_gif;
61 wxBitmap *my_horse_bmp;
62 wxBitmap *my_horse_bmp2;
63 wxBitmap *my_horse_pcx;
64 wxBitmap *my_horse_pnm;
65 wxBitmap *my_horse_tiff;
66 wxBitmap *my_horse_xpm;
67 wxBitmap *my_horse_ico32;
68 wxBitmap *my_horse_ico16;
69 wxBitmap *my_horse_ico;
70 wxBitmap *my_horse_cur;
71 wxBitmap *my_horse_ani;
72
73 wxBitmap *my_smile_xbm;
74 wxBitmap *my_square;
75 wxBitmap *my_anti;
76
77 int xH, yH ;
78 int m_ani_images ;
79
80 protected:
81 wxBitmap m_bmpSmileXpm;
82 wxIcon m_iconSmileXpm;
83
84 private:
85 DECLARE_DYNAMIC_CLASS(MyCanvas)
86 DECLARE_EVENT_TABLE()
87 };
88
89
90 const int nChoices = 8 ;
91 static const wxString bppchoices[nChoices] =
92 {
93 _T("1 bpp color"),
94 _T("1 bpp B&W"),
95 _T("4 bpp color"),
96 _T("8 bpp color"),
97 _T("8 bpp greyscale"),
98 _T("8 bpp red"),
99 _T("8 bpp own palette"),
100 _T("24 bpp")
101 };
102
103 static const int bppvalues[nChoices] =
104 {
105 wxBMP_1BPP,
106 wxBMP_1BPP_BW,
107 wxBMP_4BPP,
108 wxBMP_8BPP,
109 wxBMP_8BPP_GREY,
110 wxBMP_8BPP_RED,
111 wxBMP_8BPP_PALETTE,
112 wxBMP_24BPP
113 };
114
115 // MyFrame
116
117
118 class MyFrame: public wxFrame
119 {
120 public:
121 MyFrame();
122
123 void OnAbout( wxCommandEvent &event );
124 void OnNewFrame( wxCommandEvent &event );
125 #ifdef wxHAVE_RAW_BITMAP
126 void OnTestRawBitmap( wxCommandEvent &event );
127 #endif // wxHAVE_RAW_BITMAP
128 void OnQuit( wxCommandEvent &event );
129
130 #if wxUSE_CLIPBOARD && !defined(__WXMOTIF_)
131 void OnCopy(wxCommandEvent& event);
132 void OnPaste(wxCommandEvent& event);
133 #endif // wxUSE_CLIPBOARD
134
135 MyCanvas *m_canvas;
136
137 private:
138 DECLARE_DYNAMIC_CLASS(MyFrame)
139 DECLARE_EVENT_TABLE()
140 };
141
142 class MyImageFrame : public wxFrame
143 {
144 public:
145 MyImageFrame(wxFrame *parent, const wxBitmap& bitmap)
146 : wxFrame(parent, -1, _T("Double click to save"),
147 wxDefaultPosition, wxDefaultSize,
148 wxCAPTION | wxSYSTEM_MENU | wxCLOSE_BOX),
149 m_bitmap(bitmap)
150 {
151 SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
152 }
153
154 void OnEraseBackground(wxEraseEvent& WXUNUSED(event))
155 {
156 // do nothing here to be able to see how transparent images are shown
157 }
158
159 void OnPaint(wxPaintEvent& WXUNUSED(event))
160 {
161 wxPaintDC dc( this );
162 dc.DrawBitmap( m_bitmap, 0, 0, TRUE /* use mask */ );
163 }
164
165 void OnSave(wxMouseEvent& WXUNUSED(event))
166 {
167 wxImage image = m_bitmap.ConvertToImage();
168
169 int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"),
170 _T("Set BMP BPP"),
171 nChoices,
172 bppchoices,
173 this);
174 if ( bppselection == -1 )
175 {
176 // cancelled
177 return;
178 }
179
180 image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, bppvalues[bppselection]);
181
182 wxString deffilename = bppchoices[bppselection];
183 deffilename.Replace(wxT(" "), wxT("_"));
184 deffilename += wxT(".bmp");
185 wxString savefilename = wxFileSelector( wxT("Save Image"),
186 wxT(""),
187 deffilename,
188 (const wxChar *)NULL,
189 wxT("BMP files (*.bmp)|*.bmp|")
190 wxT("PNG files (*.png)|*.png|")
191 wxT("JPEG files (*.jpg)|*.jpg|")
192 wxT("GIF files (*.gif)|*.gif|")
193 wxT("TIFF files (*.tif)|*.tif|")
194 wxT("PCX files (*.pcx)|*.pcx|")
195 wxT("ICO files (*.ico)|*.ico|")
196 wxT("CUR files (*.cur)|*.cur"),
197 wxSAVE);
198
199 if ( savefilename.empty() )
200 return;
201
202 if ( image.GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT) == wxBMP_8BPP_PALETTE )
203 {
204 unsigned char *cmap = new unsigned char [256];
205 for ( int i = 0; i < 256; i++ )
206 cmap[i] = i;
207 image.SetPalette(wxPalette(256, cmap, cmap, cmap));
208
209 delete cmap;
210 }
211
212 bool loaded;
213 wxString extension = savefilename.AfterLast('.').Lower();
214
215 if (extension == _T("cur"))
216 {
217 image.Rescale(32,32);
218 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0);
219 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0);
220 // This shows how you can save an image with explicitly
221 // specified image format:
222 loaded = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR);
223 }
224 else
225 {
226 // This one guesses image format from filename extension
227 // (it may fail if the extension is not recognized):
228 loaded = image.SaveFile(savefilename);
229 }
230
231 if ( !loaded )
232 wxMessageBox(_T("No handler for this file type."),
233 _T("File was not saved"),
234 wxOK|wxCENTRE, this);
235 }
236
237 private:
238 wxBitmap m_bitmap;
239
240 DECLARE_EVENT_TABLE()
241 };
242
243 #ifdef wxHAVE_RAW_BITMAP
244
245 #include "wx/rawbmp.h"
246
247 class MyRawBitmapFrame : public wxFrame
248 {
249 public:
250 enum
251 {
252 BORDER = 15,
253 SIZE = 150,
254 REAL_SIZE = SIZE - 2*BORDER
255 };
256
257 MyRawBitmapFrame(wxFrame *parent)
258 : wxFrame(parent, -1, _T("Raw bitmaps (how exciting)")),
259 m_bitmap(SIZE, SIZE, 32)
260 {
261 SetClientSize(SIZE, SIZE);
262
263 wxRawBitmapData data(m_bitmap);
264 if ( !data )
265 {
266 wxLogError(_T("Failed to gain raw access to bitmap data"));
267 return;
268 }
269
270 data.UseAlpha();
271
272 wxRawBitmapIterator p(data);
273
274 p.Offset(BORDER, BORDER);
275
276 for ( int y = 0; y < REAL_SIZE; ++y )
277 {
278 wxRawBitmapIterator rowStart = p;
279
280 int r = y < REAL_SIZE/3 ? 255 : 0,
281 g = (REAL_SIZE/3 <= y) && (y < 2*(REAL_SIZE/3)) ? 255 : 0,
282 b = 2*(REAL_SIZE/3) <= y ? 255 : 0;
283
284 for ( int x = 0; x < REAL_SIZE; ++x )
285 {
286 p.Red() = r;
287 p.Green() = g;
288 p.Blue() = b;
289 p.Alpha() = x;
290
291 ++p; // same as p.OffsetX(1)
292 }
293
294 p = rowStart;
295 p.OffsetY(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 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 off_t len = file.Length();
553 void *data = malloc(len);
554 if ( file.Read(data, len) != len )
555 wxLogError(_T("Reading bitmap file failed"));
556 else
557 {
558 wxMemoryInputStream mis(data, len);
559 if ( !image.LoadFile(mis) )
560 wxLogError(wxT("Can't load BMP image from stream"));
561 else
562 my_horse_bmp2 = new wxBitmap( image );
563 }
564
565 free(data);
566 }
567
568 MyCanvas::~MyCanvas()
569 {
570 delete my_horse_pnm;
571 delete my_horse_png;
572 delete my_horse_jpeg;
573 delete my_horse_gif;
574 delete my_horse_bmp;
575 delete my_horse_bmp2;
576 delete my_horse_pcx;
577 delete my_horse_tiff;
578 delete my_horse_xpm;
579 delete my_horse_ico32;
580 delete my_horse_ico16;
581 delete my_horse_ico;
582 delete my_horse_cur;
583 delete [] my_horse_ani;
584 delete my_smile_xbm;
585 delete my_square;
586 delete my_anti;
587 }
588
589 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
590 {
591 wxPaintDC dc( this );
592 PrepareDC( dc );
593
594 dc.DrawText( _T("Loaded image"), 30, 10 );
595 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
596
597 dc.DrawText( _T("Drawn directly"), 150, 10 );
598 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
599 dc.SetPen( *wxBLACK_PEN );
600 dc.DrawRectangle( 150, 30, 100, 100 );
601 dc.SetBrush( *wxWHITE_BRUSH );
602 dc.DrawRectangle( 170, 50, 60, 60 );
603
604 if (my_anti && my_anti->Ok())
605 dc.DrawBitmap( *my_anti, 280, 30 );
606
607 dc.DrawText( _T("PNG handler"), 30, 135 );
608 if (my_horse_png && my_horse_png->Ok())
609 {
610 dc.DrawBitmap( *my_horse_png, 30, 150 );
611 wxRect rect(0,0,100,100);
612 wxBitmap sub( my_horse_png->GetSubBitmap(rect) );
613 dc.DrawText( _T("GetSubBitmap()"), 280, 190 );
614 dc.DrawBitmap( sub, 280, 210 );
615 }
616
617 dc.DrawText( _T("JPEG handler"), 30, 365 );
618 if (my_horse_jpeg && my_horse_jpeg->Ok())
619 dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
620
621 dc.DrawText( _T("GIF handler"), 30, 595 );
622 if (my_horse_gif && my_horse_gif->Ok())
623 dc.DrawBitmap( *my_horse_gif, 30, 610 );
624
625 dc.DrawText( _T("PCX handler"), 30, 825 );
626 if (my_horse_pcx && my_horse_pcx->Ok())
627 dc.DrawBitmap( *my_horse_pcx, 30, 840 );
628
629 dc.DrawText( _T("BMP handler"), 30, 1055 );
630 if (my_horse_bmp && my_horse_bmp->Ok())
631 dc.DrawBitmap( *my_horse_bmp, 30, 1070 );
632
633 dc.DrawText( _T("BMP read from memory"), 280, 1055 );
634 if (my_horse_bmp2 && my_horse_bmp2->Ok())
635 dc.DrawBitmap( *my_horse_bmp2, 280, 1070 );
636
637 dc.DrawText( _T("PNM handler"), 30, 1285 );
638 if (my_horse_pnm && my_horse_pnm->Ok())
639 dc.DrawBitmap( *my_horse_pnm, 30, 1300 );
640
641 dc.DrawText( _T("TIFF handler"), 30, 1515 );
642 if (my_horse_tiff && my_horse_tiff->Ok())
643 dc.DrawBitmap( *my_horse_tiff, 30, 1530 );
644
645 dc.DrawText( _T("XPM handler"), 30, 1745 );
646 if (my_horse_xpm && my_horse_xpm->Ok())
647 dc.DrawBitmap( *my_horse_xpm, 30, 1760 );
648
649
650 if (my_smile_xbm && my_smile_xbm->Ok())
651 {
652 dc.DrawText( _T("XBM bitmap"), 30, 1975 );
653 dc.DrawText( _T("(green on red)"), 30, 1990 );
654 dc.SetTextForeground( _T("GREEN") );
655 dc.SetTextBackground( _T("RED") );
656 dc.DrawBitmap( *my_smile_xbm, 30, 2010 );
657
658 dc.SetTextForeground( wxT("BLACK") );
659 dc.DrawText( _T("After wxImage conversion"), 150, 1975 );
660 dc.DrawText( _T("(red on white)"), 150, 1990 );
661 dc.SetTextForeground( wxT("RED") );
662 wxImage i = my_smile_xbm->ConvertToImage();
663 i.SetMaskColour( 255, 255, 255 );
664 i.Replace( 0, 0, 0,
665 wxRED_PEN->GetColour().Red(),
666 wxRED_PEN->GetColour().Green(),
667 wxRED_PEN->GetColour().Blue() );
668 dc.DrawBitmap( wxBitmap(i), 150, 2010, TRUE );
669 dc.SetTextForeground( wxT("BLACK") );
670 }
671
672
673 wxBitmap mono( 60,50,1 );
674 wxMemoryDC memdc;
675 memdc.SelectObject( mono );
676 memdc.SetPen( *wxBLACK_PEN );
677 memdc.SetBrush( *wxWHITE_BRUSH );
678 memdc.DrawRectangle( 0,0,60,50 );
679 memdc.SetTextForeground( *wxBLACK );
680 memdc.DrawText( _T("Hi!"), 5, 5 );
681 memdc.SetBrush( *wxBLACK_BRUSH );
682 memdc.DrawRectangle( 33,5,20,20 );
683 memdc.SetPen( *wxRED_PEN );
684 memdc.DrawLine( 5, 42, 50, 42 );
685 memdc.SelectObject( wxNullBitmap );
686
687 if (mono.Ok())
688 {
689 dc.DrawText( _T("Mono bitmap"), 30, 2095 );
690 dc.DrawText( _T("(red on green)"), 30, 2110 );
691 dc.SetTextForeground( wxT("RED") );
692 dc.SetTextBackground( wxT("GREEN") );
693 dc.DrawBitmap( mono, 30, 2130 );
694
695 dc.SetTextForeground( wxT("BLACK") );
696 dc.DrawText( _T("After wxImage conversion"), 150, 2095 );
697 dc.DrawText( _T("(red on white)"), 150, 2110 );
698 dc.SetTextForeground( wxT("RED") );
699 wxImage i = mono.ConvertToImage();
700 i.SetMaskColour( 255,255,255 );
701 i.Replace( 0,0,0,
702 wxRED_PEN->GetColour().Red(),
703 wxRED_PEN->GetColour().Green(),
704 wxRED_PEN->GetColour().Blue() );
705 dc.DrawBitmap( wxBitmap(i), 150, 2130, TRUE );
706 dc.SetTextForeground( wxT("BLACK") );
707 }
708
709 dc.DrawText(_T("XPM bitmap"), 30, 2230);
710 if ( m_bmpSmileXpm.Ok() )
711 {
712 dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, TRUE);
713 }
714
715 dc.DrawText(_T("XPM icon"), 150, 2230);
716 if ( m_iconSmileXpm.Ok() )
717 {
718 dc.DrawIcon(m_iconSmileXpm, 150, 2250);
719 }
720
721 dc.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
722 if (my_horse_ico32 && my_horse_ico32->Ok())
723 dc.DrawBitmap( *my_horse_ico32, 30, 2330, TRUE );
724
725 dc.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
726 if (my_horse_ico16 && my_horse_ico16->Ok())
727 dc.DrawBitmap( *my_horse_ico16, 230, 2330, TRUE );
728
729 dc.DrawText( _T("ICO handler (best image)"), 430, 2290 );
730 if (my_horse_ico && my_horse_ico->Ok())
731 dc.DrawBitmap( *my_horse_ico, 430, 2330, TRUE );
732
733 dc.DrawText( _T("CUR handler"), 30, 2390 );
734 if (my_horse_cur && my_horse_cur->Ok())
735 {
736 dc.DrawBitmap( *my_horse_cur, 30, 2420, TRUE );
737 dc.SetPen (*wxRED_PEN);
738 dc.DrawLine (xH-10,yH,xH+10,yH);
739 dc.DrawLine (xH,yH-10,xH,yH+10);
740 }
741 dc.DrawText( _T("ANI handler"), 230, 2390 );
742 int i ;
743 for (i=0; i < m_ani_images; i ++)
744 if (my_horse_ani[i].Ok())
745 {
746 dc.DrawBitmap( my_horse_ani[i], 230 + i * 2 * my_horse_ani[i].GetWidth() , 2420, TRUE );
747 }
748 }
749
750 void MyCanvas::CreateAntiAliasedBitmap()
751 {
752 wxBitmap bitmap( 300, 300 );
753
754 wxMemoryDC dc;
755
756 dc.SelectObject( bitmap );
757
758 dc.Clear();
759
760 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
761 dc.SetTextForeground( wxT("RED") );
762 dc.DrawText( _T("This is anti-aliased Text."), 20, 20 );
763 dc.DrawText( _T("And a Rectangle."), 20, 60 );
764
765 dc.SetBrush( *wxRED_BRUSH );
766 dc.SetPen( *wxTRANSPARENT_PEN );
767 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
768
769 wxImage original= bitmap.ConvertToImage();
770 wxImage anti( 150, 150 );
771
772 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
773
774 for (int y = 1; y < 149; y++)
775 for (int x = 1; x < 149; x++)
776 {
777 int red = original.GetRed( x*2, y*2 ) +
778 original.GetRed( x*2-1, y*2 ) +
779 original.GetRed( x*2, y*2+1 ) +
780 original.GetRed( x*2+1, y*2+1 );
781 red = red/4;
782
783 int green = original.GetGreen( x*2, y*2 ) +
784 original.GetGreen( x*2-1, y*2 ) +
785 original.GetGreen( x*2, y*2+1 ) +
786 original.GetGreen( x*2+1, y*2+1 );
787 green = green/4;
788
789 int blue = original.GetBlue( x*2, y*2 ) +
790 original.GetBlue( x*2-1, y*2 ) +
791 original.GetBlue( x*2, y*2+1 ) +
792 original.GetBlue( x*2+1, y*2+1 );
793 blue = blue/4;
794 anti.SetRGB( x, y, red, green, blue );
795 }
796 my_anti = new wxBitmap(anti);
797 }
798
799 // MyFrame
800
801 enum
802 {
803 ID_QUIT = 108,
804 ID_ABOUT,
805 ID_NEW,
806 ID_SHOWRAW
807 };
808
809 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
810
811 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
812 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
813 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
814 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
815 #ifdef wxHAVE_RAW_BITMAP
816 EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap)
817 #endif
818
819 #if wxUSE_CLIPBOARD && !defined(__WXMOTIF__)
820 EVT_MENU(wxID_COPY, MyFrame::OnCopy)
821 EVT_MENU(wxID_PASTE, MyFrame::OnPaste)
822 #endif // wxUSE_CLIPBOARD
823 END_EVENT_TABLE()
824
825 MyFrame::MyFrame()
826 : wxFrame( (wxFrame *)NULL, -1, _T("wxImage sample"),
827 wxPoint(20,20), wxSize(470,360) )
828 {
829 wxMenuBar *menu_bar = new wxMenuBar();
830
831 wxMenu *menuImage = new wxMenu;
832 menuImage->Append( ID_NEW, _T("&Show any image...\tCtrl-O"));
833 #ifdef wxHAVE_RAW_BITMAP
834 menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R"));
835 #endif
836 menuImage->AppendSeparator();
837 menuImage->Append( ID_ABOUT, _T("&About..."));
838 menuImage->AppendSeparator();
839 menuImage->Append( ID_QUIT, _T("E&xit\tCtrl-Q"));
840 menu_bar->Append(menuImage, _T("&Image"));
841
842 #if wxUSE_CLIPBOARD && !defined(__WXMOTIF__)
843 wxMenu *menuClipboard = new wxMenu;
844 menuClipboard->Append(wxID_COPY, _T("&Copy test image\tCtrl-C"));
845 menuClipboard->Append(wxID_PASTE, _T("&Paste image\tCtrl-V"));
846 menu_bar->Append(menuClipboard, _T("&Clipboard"));
847 #endif // wxUSE_CLIPBOARD
848
849 SetMenuBar( menu_bar );
850
851 CreateStatusBar(2);
852 int widths[] = { -1, 100 };
853 SetStatusWidths( 2, widths );
854
855 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
856
857 // 500 width * 2500 height
858 m_canvas->SetScrollbars( 10, 10, 50, 250 );
859 }
860
861 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
862 {
863 Close( TRUE );
864 }
865
866 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
867 {
868 (void)wxMessageBox( _T("wxImage demo\n")
869 _T("Robert Roebling (c) 1998,2000"),
870 _T("About wxImage Demo"), wxICON_INFORMATION | wxOK );
871 }
872
873 void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
874 {
875 wxString filename = wxFileSelector(_T("Select image file"));
876 if ( !filename )
877 return;
878
879 wxImage image;
880 if ( !image.LoadFile(filename) )
881 {
882 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
883
884 return;
885 }
886
887 (new MyImageFrame(this, wxBitmap(image)))->Show();
888 }
889
890 #ifdef wxHAVE_RAW_BITMAP
891
892 void MyFrame::OnTestRawBitmap( wxCommandEvent &event )
893 {
894 (new MyRawBitmapFrame(this))->Show();
895 }
896
897 #endif // wxHAVE_RAW_BITMAP
898
899 #if wxUSE_CLIPBOARD && !defined(__WXMOTIF__)
900
901 void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
902 {
903 wxBitmapDataObject *dobjBmp = new wxBitmapDataObject;
904 dobjBmp->SetBitmap(*m_canvas->my_horse_png);
905
906 if ( !wxTheClipboard->SetData(dobjBmp) )
907 {
908 wxLogError(_T("Failed to copy bitmap to clipboard"));
909 }
910 }
911
912 void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
913 {
914 wxBitmapDataObject dobjBmp;
915 if ( !wxTheClipboard->GetData(dobjBmp) )
916 {
917 wxLogMessage(_T("No bitmap data in the clipboard"));
918 }
919 else
920 {
921 (new MyImageFrame(this, dobjBmp.GetBitmap()))->Show();
922 }
923 }
924
925 #endif // wxUSE_CLIPBOARD
926
927 //-----------------------------------------------------------------------------
928 // MyApp
929 //-----------------------------------------------------------------------------
930
931 bool MyApp::OnInit()
932 {
933 #if wxUSE_LIBPNG
934 wxImage::AddHandler( new wxPNGHandler );
935 #endif
936
937 #if wxUSE_LIBJPEG
938 wxImage::AddHandler( new wxJPEGHandler );
939 #endif
940
941 #if wxUSE_LIBTIFF
942 wxImage::AddHandler( new wxTIFFHandler );
943 #endif
944
945 #if wxUSE_GIF
946 wxImage::AddHandler( new wxGIFHandler );
947 #endif
948
949 #if wxUSE_PCX
950 wxImage::AddHandler( new wxPCXHandler );
951 #endif
952
953 #if wxUSE_PNM
954 wxImage::AddHandler( new wxPNMHandler );
955 #endif
956
957 #if wxUSE_XPM
958 wxImage::AddHandler( new wxXPMHandler );
959 #endif
960
961 #if wxUSE_ICO_CUR
962 wxImage::AddHandler( new wxICOHandler );
963 wxImage::AddHandler( new wxCURHandler );
964 wxImage::AddHandler( new wxANIHandler );
965 #endif
966
967 wxFrame *frame = new MyFrame();
968 frame->Show( TRUE );
969
970 return TRUE;
971 }
972