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