]> git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
3dfe14841fc63eaabe314f07cf488da05b5ec8d5
[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 wxRawBitmapIterator p(data);
271
272 p.Offset(BORDER, BORDER);
273
274 for ( int y = 0; y < REAL_SIZE; ++y )
275 {
276 wxRawBitmapIterator rowStart = p;
277
278 int r = y < REAL_SIZE/3 ? 255 : 0,
279 g = (REAL_SIZE/3 <= y) && (y < 2*(REAL_SIZE/3)) ? 255 : 0,
280 b = 2*(REAL_SIZE/3) <= y ? 255 : 0;
281
282 for ( int x = 0; x < REAL_SIZE; ++x )
283 {
284 p.Red() = r;
285 p.Green() = g;
286 p.Blue() = b;
287 p.Alpha() = x;
288
289 ++p; // same as p.OffsetX(1)
290 }
291
292 p = rowStart;
293 p.OffsetY(1);
294 }
295 }
296
297 void OnPaint(wxPaintEvent& WXUNUSED(event))
298 {
299 wxPaintDC dc( this );
300 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER);
301 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE/2 - BORDER);
302 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE - 2*BORDER);
303 dc.DrawBitmap( m_bitmap, 0, 0, TRUE /* use mask */ );
304 }
305
306 private:
307 wxBitmap m_bitmap;
308
309 DECLARE_EVENT_TABLE()
310 };
311
312 #endif // wxHAVE_RAW_BITMAP
313
314 // MyApp
315
316 class MyApp: public wxApp
317 {
318 public:
319 virtual bool OnInit();
320 };
321
322 // main program
323
324 IMPLEMENT_APP(MyApp)
325
326 // MyCanvas
327
328 IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
329
330 BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
331 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground)
332 EVT_PAINT(MyImageFrame::OnPaint)
333 EVT_LEFT_DCLICK(MyImageFrame::OnSave)
334 END_EVENT_TABLE()
335
336 #ifdef wxHAVE_RAW_BITMAP
337
338 BEGIN_EVENT_TABLE(MyRawBitmapFrame, wxFrame)
339 EVT_PAINT(MyRawBitmapFrame::OnPaint)
340 END_EVENT_TABLE()
341
342 #endif // wxHAVE_RAW_BITMAP
343
344 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
345 EVT_PAINT(MyCanvas::OnPaint)
346 END_EVENT_TABLE()
347
348 MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
349 const wxPoint &pos, const wxSize &size )
350 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
351 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
352 , m_bmpSmileXpm((const char **) smile_xpm)
353 , m_iconSmileXpm((const char **) smile_xpm)
354 #endif
355 {
356 my_horse_png = (wxBitmap*) NULL;
357 my_horse_jpeg = (wxBitmap*) NULL;
358 my_horse_gif = (wxBitmap*) NULL;
359 my_horse_bmp = (wxBitmap*) NULL;
360 my_horse_bmp2 = (wxBitmap*) NULL;
361 my_horse_pcx = (wxBitmap*) NULL;
362 my_horse_pnm = (wxBitmap*) NULL;
363 my_horse_tiff = (wxBitmap*) NULL;
364 my_horse_xpm = (wxBitmap*) NULL;
365 my_horse_ico32 = (wxBitmap*) NULL;
366 my_horse_ico16 = (wxBitmap*) NULL;
367 my_horse_ico = (wxBitmap*) NULL;
368 my_horse_cur = (wxBitmap*) NULL;
369 my_horse_ani = (wxBitmap*) NULL;
370
371 my_smile_xbm = (wxBitmap*) NULL;
372 my_square = (wxBitmap*) NULL;
373 my_anti = (wxBitmap*) NULL;
374
375 m_ani_images = 0 ;
376
377 SetBackgroundColour(* wxWHITE);
378
379 wxBitmap bitmap( 100, 100 );
380
381 wxMemoryDC dc;
382 dc.SelectObject( bitmap );
383 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
384 dc.SetPen( *wxBLACK_PEN );
385 dc.DrawRectangle( 0, 0, 100, 100 );
386 dc.SetBrush( *wxWHITE_BRUSH );
387 dc.DrawRectangle( 20, 20, 60, 60 );
388 dc.SelectObject( wxNullBitmap );
389
390 // try to find the directory with our images
391 wxString dir;
392 if ( wxFile::Exists(wxT("./horse.png")) )
393 dir = wxT("./");
394 else if ( wxFile::Exists(wxT("../horse.png")) )
395 dir = wxT("../");
396 else
397 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
398
399 wxImage image = bitmap.ConvertToImage();
400
401 #if wxUSE_LIBPNG
402 if ( !image.SaveFile( dir + _T("test.png"), wxBITMAP_TYPE_PNG ))
403 wxLogError(wxT("Can't save file"));
404
405 image.Destroy();
406
407 image.LoadFile( dir + _T("test.png") );
408 my_square = new wxBitmap( image );
409
410 image.Destroy();
411
412 if ( !image.LoadFile( dir + _T("horse.png")) )
413 wxLogError(wxT("Can't load PNG image"));
414 else
415 my_horse_png = new wxBitmap( image );
416 #endif // wxUSE_LIBPNG
417
418 #if wxUSE_LIBJPEG
419 image.Destroy();
420
421 if ( !image.LoadFile( dir + _T("horse.jpg")) )
422 wxLogError(wxT("Can't load JPG image"));
423 else
424 my_horse_jpeg = new wxBitmap( image );
425 #endif // wxUSE_LIBJPEG
426
427 #if wxUSE_GIF
428 image.Destroy();
429
430 if ( !image.LoadFile( dir + _T("horse.gif" )) )
431 wxLogError(wxT("Can't load GIF image"));
432 else
433 my_horse_gif = new wxBitmap( image );
434 #endif
435
436 #if wxUSE_PCX
437 image.Destroy();
438
439 if ( !image.LoadFile( dir + _T("horse.pcx"), wxBITMAP_TYPE_PCX ) )
440 wxLogError(wxT("Can't load PCX image"));
441 else
442 my_horse_pcx = new wxBitmap( image );
443 #endif
444
445 image.Destroy();
446
447 if ( !image.LoadFile( dir + _T("horse.bmp"), wxBITMAP_TYPE_BMP ) )
448 wxLogError(wxT("Can't load BMP image"));
449 else
450 my_horse_bmp = new wxBitmap( image );
451
452 #if wxUSE_XPM
453 image.Destroy();
454
455 if ( !image.LoadFile( dir + _T("horse.xpm"), wxBITMAP_TYPE_XPM ) )
456 wxLogError(wxT("Can't load XPM image"));
457 else
458 my_horse_xpm = new wxBitmap( image );
459
460 if ( !image.SaveFile( dir + _T("test.xpm"), wxBITMAP_TYPE_XPM ))
461 wxLogError(wxT("Can't save file"));
462 #endif
463
464 #if wxUSE_PNM
465 image.Destroy();
466
467 if ( !image.LoadFile( dir + _T("horse.pnm"), wxBITMAP_TYPE_PNM ) )
468 wxLogError(wxT("Can't load PNM image"));
469 else
470 my_horse_pnm = new wxBitmap( image );
471 #endif
472
473 #if wxUSE_LIBTIFF
474 image.Destroy();
475
476 if ( !image.LoadFile( dir + _T("horse.tif"), wxBITMAP_TYPE_TIF ) )
477 wxLogError(wxT("Can't load TIFF image"));
478 else
479 my_horse_tiff = new wxBitmap( image );
480 #endif
481
482 CreateAntiAliasedBitmap();
483
484 my_smile_xbm = new wxBitmap( (const char*)smile_bits, smile_width,
485 smile_height, 1 );
486
487 #if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
488 // demonstrates XPM automatically using the mask when saving
489 if ( m_bmpSmileXpm.Ok() )
490 m_bmpSmileXpm.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM);
491 #endif
492
493 #if wxUSE_ICO_CUR
494 image.Destroy();
495
496 if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 0 ) )
497 wxLogError(wxT("Can't load first ICO image"));
498 else
499 my_horse_ico32 = new wxBitmap( image );
500
501 image.Destroy();
502
503 if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 1 ) )
504 wxLogError(wxT("Can't load second ICO image"));
505 else
506 my_horse_ico16 = new wxBitmap( image );
507
508 image.Destroy();
509
510 if ( !image.LoadFile( dir + _T("horse.ico") ) )
511 wxLogError(wxT("Can't load best ICO image"));
512 else
513 my_horse_ico = new wxBitmap( image );
514
515 image.Destroy();
516
517 if ( !image.LoadFile( dir + _T("horse.cur"), wxBITMAP_TYPE_CUR ) )
518 wxLogError(wxT("Can't load best ICO image"));
519 else
520 {
521 my_horse_cur = new wxBitmap( image );
522 xH = 30 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) ;
523 yH = 2420 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ;
524 }
525
526 m_ani_images = wxImage::GetImageCount ( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI );
527 if (m_ani_images==0)
528 wxLogError(wxT("No ANI-format images found"));
529 else
530 my_horse_ani = new wxBitmap [m_ani_images];
531 int i ;
532 for (i=0; i < m_ani_images; i++)
533 {
534 image.Destroy();
535 if (!image.LoadFile( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI, i ))
536 {
537 wxString tmp = wxT("Can't load image number ");
538 tmp << i ;
539 wxLogError(tmp);
540 }
541 else
542 my_horse_ani [i] = wxBitmap( image );
543 }
544 #endif // wxUSE_ICO_CUR
545
546 image.Destroy();
547
548 // test image loading from stream
549 wxFile file(dir + _T("horse.bmp"));
550 off_t len = file.Length();
551 void *data = malloc(len);
552 if ( file.Read(data, len) != len )
553 wxLogError(_T("Reading bitmap file failed"));
554 else
555 {
556 wxMemoryInputStream mis(data, len);
557 if ( !image.LoadFile(mis) )
558 wxLogError(wxT("Can't load BMP image from stream"));
559 else
560 my_horse_bmp2 = new wxBitmap( image );
561 }
562
563 free(data);
564 }
565
566 MyCanvas::~MyCanvas()
567 {
568 delete my_horse_pnm;
569 delete my_horse_png;
570 delete my_horse_jpeg;
571 delete my_horse_gif;
572 delete my_horse_bmp;
573 delete my_horse_bmp2;
574 delete my_horse_pcx;
575 delete my_horse_tiff;
576 delete my_horse_xpm;
577 delete my_horse_ico32;
578 delete my_horse_ico16;
579 delete my_horse_ico;
580 delete my_horse_cur;
581 delete [] my_horse_ani;
582 delete my_smile_xbm;
583 delete my_square;
584 delete my_anti;
585 }
586
587 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
588 {
589 wxPaintDC dc( this );
590 PrepareDC( dc );
591
592 dc.DrawText( _T("Loaded image"), 30, 10 );
593 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
594
595 dc.DrawText( _T("Drawn directly"), 150, 10 );
596 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
597 dc.SetPen( *wxBLACK_PEN );
598 dc.DrawRectangle( 150, 30, 100, 100 );
599 dc.SetBrush( *wxWHITE_BRUSH );
600 dc.DrawRectangle( 170, 50, 60, 60 );
601
602 if (my_anti && my_anti->Ok())
603 dc.DrawBitmap( *my_anti, 280, 30 );
604
605 dc.DrawText( _T("PNG handler"), 30, 135 );
606 if (my_horse_png && my_horse_png->Ok())
607 {
608 dc.DrawBitmap( *my_horse_png, 30, 150 );
609 wxRect rect(0,0,100,100);
610 wxBitmap sub( my_horse_png->GetSubBitmap(rect) );
611 dc.DrawText( _T("GetSubBitmap()"), 280, 190 );
612 dc.DrawBitmap( sub, 280, 210 );
613 }
614
615 dc.DrawText( _T("JPEG handler"), 30, 365 );
616 if (my_horse_jpeg && my_horse_jpeg->Ok())
617 dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
618
619 dc.DrawText( _T("GIF handler"), 30, 595 );
620 if (my_horse_gif && my_horse_gif->Ok())
621 dc.DrawBitmap( *my_horse_gif, 30, 610 );
622
623 dc.DrawText( _T("PCX handler"), 30, 825 );
624 if (my_horse_pcx && my_horse_pcx->Ok())
625 dc.DrawBitmap( *my_horse_pcx, 30, 840 );
626
627 dc.DrawText( _T("BMP handler"), 30, 1055 );
628 if (my_horse_bmp && my_horse_bmp->Ok())
629 dc.DrawBitmap( *my_horse_bmp, 30, 1070 );
630
631 dc.DrawText( _T("BMP read from memory"), 280, 1055 );
632 if (my_horse_bmp2 && my_horse_bmp2->Ok())
633 dc.DrawBitmap( *my_horse_bmp2, 280, 1070 );
634
635 dc.DrawText( _T("PNM handler"), 30, 1285 );
636 if (my_horse_pnm && my_horse_pnm->Ok())
637 dc.DrawBitmap( *my_horse_pnm, 30, 1300 );
638
639 dc.DrawText( _T("TIFF handler"), 30, 1515 );
640 if (my_horse_tiff && my_horse_tiff->Ok())
641 dc.DrawBitmap( *my_horse_tiff, 30, 1530 );
642
643 dc.DrawText( _T("XPM handler"), 30, 1745 );
644 if (my_horse_xpm && my_horse_xpm->Ok())
645 dc.DrawBitmap( *my_horse_xpm, 30, 1760 );
646
647
648 if (my_smile_xbm && my_smile_xbm->Ok())
649 {
650 dc.DrawText( _T("XBM bitmap"), 30, 1975 );
651 dc.DrawText( _T("(green on red)"), 30, 1990 );
652 dc.SetTextForeground( _T("GREEN") );
653 dc.SetTextBackground( _T("RED") );
654 dc.DrawBitmap( *my_smile_xbm, 30, 2010 );
655
656 dc.SetTextForeground( wxT("BLACK") );
657 dc.DrawText( _T("After wxImage conversion"), 150, 1975 );
658 dc.DrawText( _T("(red on white)"), 150, 1990 );
659 dc.SetTextForeground( wxT("RED") );
660 wxImage i = my_smile_xbm->ConvertToImage();
661 i.SetMaskColour( 255, 255, 255 );
662 i.Replace( 0, 0, 0,
663 wxRED_PEN->GetColour().Red(),
664 wxRED_PEN->GetColour().Green(),
665 wxRED_PEN->GetColour().Blue() );
666 dc.DrawBitmap( wxBitmap(i), 150, 2010, TRUE );
667 dc.SetTextForeground( wxT("BLACK") );
668 }
669
670
671 wxBitmap mono( 60,50,1 );
672 wxMemoryDC memdc;
673 memdc.SelectObject( mono );
674 memdc.SetPen( *wxBLACK_PEN );
675 memdc.SetBrush( *wxWHITE_BRUSH );
676 memdc.DrawRectangle( 0,0,60,50 );
677 memdc.SetTextForeground( *wxBLACK );
678 memdc.DrawText( _T("Hi!"), 5, 5 );
679 memdc.SetBrush( *wxBLACK_BRUSH );
680 memdc.DrawRectangle( 33,5,20,20 );
681 memdc.SetPen( *wxRED_PEN );
682 memdc.DrawLine( 5, 42, 50, 42 );
683 memdc.SelectObject( wxNullBitmap );
684
685 if (mono.Ok())
686 {
687 dc.DrawText( _T("Mono bitmap"), 30, 2095 );
688 dc.DrawText( _T("(red on green)"), 30, 2110 );
689 dc.SetTextForeground( wxT("RED") );
690 dc.SetTextBackground( wxT("GREEN") );
691 dc.DrawBitmap( mono, 30, 2130 );
692
693 dc.SetTextForeground( wxT("BLACK") );
694 dc.DrawText( _T("After wxImage conversion"), 150, 2095 );
695 dc.DrawText( _T("(red on white)"), 150, 2110 );
696 dc.SetTextForeground( wxT("RED") );
697 wxImage i = mono.ConvertToImage();
698 i.SetMaskColour( 255,255,255 );
699 i.Replace( 0,0,0,
700 wxRED_PEN->GetColour().Red(),
701 wxRED_PEN->GetColour().Green(),
702 wxRED_PEN->GetColour().Blue() );
703 dc.DrawBitmap( wxBitmap(i), 150, 2130, TRUE );
704 dc.SetTextForeground( wxT("BLACK") );
705 }
706
707 dc.DrawText(_T("XPM bitmap"), 30, 2230);
708 if ( m_bmpSmileXpm.Ok() )
709 {
710 dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, TRUE);
711 }
712
713 dc.DrawText(_T("XPM icon"), 150, 2230);
714 if ( m_iconSmileXpm.Ok() )
715 {
716 dc.DrawIcon(m_iconSmileXpm, 150, 2250);
717 }
718
719 dc.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
720 if (my_horse_ico32 && my_horse_ico32->Ok())
721 dc.DrawBitmap( *my_horse_ico32, 30, 2330, TRUE );
722
723 dc.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
724 if (my_horse_ico16 && my_horse_ico16->Ok())
725 dc.DrawBitmap( *my_horse_ico16, 230, 2330, TRUE );
726
727 dc.DrawText( _T("ICO handler (best image)"), 430, 2290 );
728 if (my_horse_ico && my_horse_ico->Ok())
729 dc.DrawBitmap( *my_horse_ico, 430, 2330, TRUE );
730
731 dc.DrawText( _T("CUR handler"), 30, 2390 );
732 if (my_horse_cur && my_horse_cur->Ok())
733 {
734 dc.DrawBitmap( *my_horse_cur, 30, 2420, TRUE );
735 dc.SetPen (*wxRED_PEN);
736 dc.DrawLine (xH-10,yH,xH+10,yH);
737 dc.DrawLine (xH,yH-10,xH,yH+10);
738 }
739 dc.DrawText( _T("ANI handler"), 230, 2390 );
740 int i ;
741 for (i=0; i < m_ani_images; i ++)
742 if (my_horse_ani[i].Ok())
743 {
744 dc.DrawBitmap( my_horse_ani[i], 230 + i * 2 * my_horse_ani[i].GetWidth() , 2420, TRUE );
745 }
746 }
747
748 void MyCanvas::CreateAntiAliasedBitmap()
749 {
750 wxBitmap bitmap( 300, 300 );
751
752 wxMemoryDC dc;
753
754 dc.SelectObject( bitmap );
755
756 dc.Clear();
757
758 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
759 dc.SetTextForeground( wxT("RED") );
760 dc.DrawText( _T("This is anti-aliased Text."), 20, 20 );
761 dc.DrawText( _T("And a Rectangle."), 20, 60 );
762
763 dc.SetBrush( *wxRED_BRUSH );
764 dc.SetPen( *wxTRANSPARENT_PEN );
765 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
766
767 wxImage original= bitmap.ConvertToImage();
768 wxImage anti( 150, 150 );
769
770 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
771
772 for (int y = 1; y < 149; y++)
773 for (int x = 1; x < 149; x++)
774 {
775 int red = original.GetRed( x*2, y*2 ) +
776 original.GetRed( x*2-1, y*2 ) +
777 original.GetRed( x*2, y*2+1 ) +
778 original.GetRed( x*2+1, y*2+1 );
779 red = red/4;
780
781 int green = original.GetGreen( x*2, y*2 ) +
782 original.GetGreen( x*2-1, y*2 ) +
783 original.GetGreen( x*2, y*2+1 ) +
784 original.GetGreen( x*2+1, y*2+1 );
785 green = green/4;
786
787 int blue = original.GetBlue( x*2, y*2 ) +
788 original.GetBlue( x*2-1, y*2 ) +
789 original.GetBlue( x*2, y*2+1 ) +
790 original.GetBlue( x*2+1, y*2+1 );
791 blue = blue/4;
792 anti.SetRGB( x, y, red, green, blue );
793 }
794 my_anti = new wxBitmap(anti);
795 }
796
797 // MyFrame
798
799 enum
800 {
801 ID_QUIT = 108,
802 ID_ABOUT,
803 ID_NEW,
804 ID_SHOWRAW
805 };
806
807 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
808
809 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
810 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
811 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
812 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
813 #ifdef wxHAVE_RAW_BITMAP
814 EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap)
815 #endif
816
817 #if wxUSE_CLIPBOARD && !defined(__WXMOTIF__)
818 EVT_MENU(wxID_COPY, MyFrame::OnCopy)
819 EVT_MENU(wxID_PASTE, MyFrame::OnPaste)
820 #endif // wxUSE_CLIPBOARD
821 END_EVENT_TABLE()
822
823 MyFrame::MyFrame()
824 : wxFrame( (wxFrame *)NULL, -1, _T("wxImage sample"),
825 wxPoint(20,20), wxSize(470,360) )
826 {
827 wxMenuBar *menu_bar = new wxMenuBar();
828
829 wxMenu *menuImage = new wxMenu;
830 menuImage->Append( ID_NEW, _T("&Show any image...\tCtrl-O"));
831 #ifdef wxHAVE_RAW_BITMAP
832 menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R"));
833 #endif
834 menuImage->AppendSeparator();
835 menuImage->Append( ID_ABOUT, _T("&About..."));
836 menuImage->AppendSeparator();
837 menuImage->Append( ID_QUIT, _T("E&xit\tCtrl-Q"));
838 menu_bar->Append(menuImage, _T("&Image"));
839
840 #if wxUSE_CLIPBOARD && !defined(__WXMOTIF__)
841 wxMenu *menuClipboard = new wxMenu;
842 menuClipboard->Append(wxID_COPY, _T("&Copy test image\tCtrl-C"));
843 menuClipboard->Append(wxID_PASTE, _T("&Paste image\tCtrl-V"));
844 menu_bar->Append(menuClipboard, _T("&Clipboard"));
845 #endif // wxUSE_CLIPBOARD
846
847 SetMenuBar( menu_bar );
848
849 CreateStatusBar(2);
850 int widths[] = { -1, 100 };
851 SetStatusWidths( 2, widths );
852
853 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
854
855 // 500 width * 2500 height
856 m_canvas->SetScrollbars( 10, 10, 50, 250 );
857 }
858
859 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
860 {
861 Close( TRUE );
862 }
863
864 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
865 {
866 (void)wxMessageBox( _T("wxImage demo\n")
867 _T("Robert Roebling (c) 1998,2000"),
868 _T("About wxImage Demo"), wxICON_INFORMATION | wxOK );
869 }
870
871 void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
872 {
873 wxString filename = wxFileSelector(_T("Select image file"));
874 if ( !filename )
875 return;
876
877 wxImage image;
878 if ( !image.LoadFile(filename) )
879 {
880 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
881
882 return;
883 }
884
885 (new MyImageFrame(this, wxBitmap(image)))->Show();
886 }
887
888 #ifdef wxHAVE_RAW_BITMAP
889
890 void MyFrame::OnTestRawBitmap( wxCommandEvent &event )
891 {
892 (new MyRawBitmapFrame(this))->Show();
893 }
894
895 #endif // wxHAVE_RAW_BITMAP
896
897 #if wxUSE_CLIPBOARD && !defined(__WXMOTIF__)
898
899 void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
900 {
901 wxBitmapDataObject *dobjBmp = new wxBitmapDataObject;
902 dobjBmp->SetBitmap(*m_canvas->my_horse_png);
903
904 if ( !wxTheClipboard->SetData(dobjBmp) )
905 {
906 wxLogError(_T("Failed to copy bitmap to clipboard"));
907 }
908 }
909
910 void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
911 {
912 wxBitmapDataObject dobjBmp;
913 if ( !wxTheClipboard->GetData(dobjBmp) )
914 {
915 wxLogMessage(_T("No bitmap data in the clipboard"));
916 }
917 else
918 {
919 (new MyImageFrame(this, dobjBmp.GetBitmap()))->Show();
920 }
921 }
922
923 #endif // wxUSE_CLIPBOARD
924
925 //-----------------------------------------------------------------------------
926 // MyApp
927 //-----------------------------------------------------------------------------
928
929 bool MyApp::OnInit()
930 {
931 #if wxUSE_LIBPNG
932 wxImage::AddHandler( new wxPNGHandler );
933 #endif
934
935 #if wxUSE_LIBJPEG
936 wxImage::AddHandler( new wxJPEGHandler );
937 #endif
938
939 #if wxUSE_LIBTIFF
940 wxImage::AddHandler( new wxTIFFHandler );
941 #endif
942
943 #if wxUSE_GIF
944 wxImage::AddHandler( new wxGIFHandler );
945 #endif
946
947 #if wxUSE_PCX
948 wxImage::AddHandler( new wxPCXHandler );
949 #endif
950
951 #if wxUSE_PNM
952 wxImage::AddHandler( new wxPNMHandler );
953 #endif
954
955 #if wxUSE_XPM
956 wxImage::AddHandler( new wxXPMHandler );
957 #endif
958
959 #if wxUSE_ICO_CUR
960 wxImage::AddHandler( new wxICOHandler );
961 wxImage::AddHandler( new wxCURHandler );
962 wxImage::AddHandler( new wxANIHandler );
963 #endif
964
965 wxFrame *frame = new MyFrame();
966 frame->Show( TRUE );
967
968 return TRUE;
969 }
970