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