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