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