]> git.saurik.com Git - wxWidgets.git/blob - samples/image/image.cpp
final sweep over docs - replace & with \&
[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 #include "smile.xpm"
34
35 #if defined(__WXMSW__) || defined(__WXMAC__)
36 #ifdef wxHAVE_RAW_BITMAP
37 #include "wx/rawbmp.h"
38 #endif
39 #endif
40
41 // derived classes
42
43 class MyFrame;
44 class MyApp;
45
46 // MyCanvas
47
48 class MyCanvas: public wxScrolledWindow
49 {
50 public:
51 MyCanvas() {}
52 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
53 ~MyCanvas();
54 void OnPaint( wxPaintEvent &event );
55 void CreateAntiAliasedBitmap();
56
57 wxBitmap *my_horse_png;
58 wxBitmap *my_horse_jpeg;
59 wxBitmap *my_horse_gif;
60 wxBitmap *my_horse_bmp;
61 wxBitmap *my_horse_bmp2;
62 wxBitmap *my_horse_pcx;
63 wxBitmap *my_horse_pnm;
64 wxBitmap *my_horse_tiff;
65 wxBitmap *my_horse_xpm;
66 wxBitmap *my_horse_ico32;
67 wxBitmap *my_horse_ico16;
68 wxBitmap *my_horse_ico;
69 wxBitmap *my_horse_cur;
70 wxBitmap *my_horse_ani;
71
72 wxBitmap *my_smile_xbm;
73 wxBitmap *my_square;
74 wxBitmap *my_anti;
75
76 int xH, yH ;
77 int m_ani_images ;
78
79 protected:
80 wxBitmap m_bmpSmileXpm;
81 wxIcon m_iconSmileXpm;
82
83 private:
84 DECLARE_DYNAMIC_CLASS(MyCanvas)
85 DECLARE_EVENT_TABLE()
86 };
87
88
89 const int nChoices = 8 ;
90 static const wxString bppchoices[nChoices] =
91 {
92 _T("1 bpp color"),
93 _T("1 bpp B&W"),
94 _T("4 bpp color"),
95 _T("8 bpp color"),
96 _T("8 bpp greyscale"),
97 _T("8 bpp red"),
98 _T("8 bpp own palette"),
99 _T("24 bpp")
100 };
101
102 static const int bppvalues[nChoices] =
103 {
104 wxBMP_1BPP,
105 wxBMP_1BPP_BW,
106 wxBMP_4BPP,
107 wxBMP_8BPP,
108 wxBMP_8BPP_GREY,
109 wxBMP_8BPP_RED,
110 wxBMP_8BPP_PALETTE,
111 wxBMP_24BPP
112 };
113
114 // MyFrame
115
116
117 class MyFrame: public wxFrame
118 {
119 public:
120 MyFrame();
121
122 void OnAbout( wxCommandEvent &event );
123 void OnNewFrame( wxCommandEvent &event );
124 #ifdef wxHAVE_RAW_BITMAP
125 void OnTestRawBitmap( wxCommandEvent &event );
126 #endif // wxHAVE_RAW_BITMAP
127 void OnQuit( wxCommandEvent &event );
128
129 #if wxUSE_CLIPBOARD
130 void OnCopy(wxCommandEvent& event);
131 void OnPaste(wxCommandEvent& event);
132 #endif // wxUSE_CLIPBOARD
133
134 MyCanvas *m_canvas;
135
136 private:
137 DECLARE_DYNAMIC_CLASS(MyFrame)
138 DECLARE_EVENT_TABLE()
139 };
140
141 class MyImageFrame : public wxFrame
142 {
143 public:
144 MyImageFrame(wxFrame *parent, const wxBitmap& bitmap)
145 : wxFrame(parent, wxID_ANY, _T("Double click to save"),
146 wxDefaultPosition, wxDefaultSize,
147 wxCAPTION | wxSYSTEM_MENU | wxCLOSE_BOX),
148 m_bitmap(bitmap)
149 {
150 SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
151 }
152
153 void OnEraseBackground(wxEraseEvent& WXUNUSED(event))
154 {
155 // do nothing here to be able to see how transparent images are shown
156 }
157
158 void OnPaint(wxPaintEvent& WXUNUSED(event))
159 {
160 wxPaintDC dc( this );
161 dc.DrawBitmap( m_bitmap, 0, 0, true /* use mask */ );
162 }
163
164 void OnSave(wxMouseEvent& WXUNUSED(event))
165 {
166 wxImage image = m_bitmap.ConvertToImage();
167
168 int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"),
169 _T("Set BMP BPP"),
170 nChoices,
171 bppchoices,
172 this);
173 if ( bppselection == -1 )
174 {
175 // cancelled
176 return;
177 }
178
179 image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, bppvalues[bppselection]);
180
181 wxString deffilename = bppchoices[bppselection];
182 deffilename.Replace(wxT(" "), wxT("_"));
183 deffilename += wxT(".bmp");
184 wxString savefilename = wxFileSelector( wxT("Save Image"),
185 wxT(""),
186 deffilename,
187 (const wxChar *)NULL,
188 wxT("BMP files (*.bmp)|*.bmp|")
189 wxT("PNG files (*.png)|*.png|")
190 wxT("JPEG files (*.jpg)|*.jpg|")
191 wxT("GIF files (*.gif)|*.gif|")
192 wxT("TIFF files (*.tif)|*.tif|")
193 wxT("PCX files (*.pcx)|*.pcx|")
194 wxT("ICO files (*.ico)|*.ico|")
195 wxT("CUR files (*.cur)|*.cur"),
196 wxSAVE,
197 this);
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] = (unsigned char)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, wxID_ANY, _T("Raw bitmaps (how exciting)")),
259 m_bitmap(SIZE, SIZE, 32)
260 {
261 SetClientSize(SIZE, SIZE);
262
263 // another possibility: wxNativePixelData (don't forget to remove code
264 // setting alpha in the loop below then)
265 typedef wxAlphaPixelData Data;
266
267 Data data(m_bitmap, wxPoint(BORDER, BORDER), wxSize(REAL_SIZE, REAL_SIZE));
268 if ( !data )
269 {
270 wxLogError(_T("Failed to gain raw access to bitmap data"));
271 return;
272 }
273
274 data.UseAlpha();
275
276 Data::Iterator p(data);
277
278 for ( int y = 0; y < REAL_SIZE; ++y )
279 {
280 Data::Iterator rowStart = p;
281
282 int r = y < REAL_SIZE/3 ? 255 : 0,
283 g = (REAL_SIZE/3 <= y) && (y < 2*(REAL_SIZE/3)) ? 255 : 0,
284 b = 2*(REAL_SIZE/3) <= y ? 255 : 0;
285
286 for ( int x = 0; x < REAL_SIZE; ++x )
287 {
288 p.Red() = r;
289 p.Green() = g;
290 p.Blue() = b;
291 p.Alpha() = (Data::Iterator::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 , m_bmpSmileXpm((const char **) smile_xpm)
356 , m_iconSmileXpm((const char **) smile_xpm)
357 {
358 my_horse_png = (wxBitmap*) NULL;
359 my_horse_jpeg = (wxBitmap*) NULL;
360 my_horse_gif = (wxBitmap*) NULL;
361 my_horse_bmp = (wxBitmap*) NULL;
362 my_horse_bmp2 = (wxBitmap*) NULL;
363 my_horse_pcx = (wxBitmap*) NULL;
364 my_horse_pnm = (wxBitmap*) NULL;
365 my_horse_tiff = (wxBitmap*) NULL;
366 my_horse_xpm = (wxBitmap*) NULL;
367 my_horse_ico32 = (wxBitmap*) NULL;
368 my_horse_ico16 = (wxBitmap*) NULL;
369 my_horse_ico = (wxBitmap*) NULL;
370 my_horse_cur = (wxBitmap*) NULL;
371 my_horse_ani = (wxBitmap*) NULL;
372
373 my_smile_xbm = (wxBitmap*) NULL;
374 my_square = (wxBitmap*) NULL;
375 my_anti = (wxBitmap*) NULL;
376
377 m_ani_images = 0 ;
378
379 SetBackgroundColour(* wxWHITE);
380
381 wxBitmap bitmap( 100, 100 );
382
383 wxMemoryDC dc;
384 dc.SelectObject( bitmap );
385 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
386 dc.SetPen( *wxBLACK_PEN );
387 dc.DrawRectangle( 0, 0, 100, 100 );
388 dc.SetBrush( *wxWHITE_BRUSH );
389 dc.DrawRectangle( 20, 20, 60, 60 );
390 dc.SelectObject( wxNullBitmap );
391
392 // try to find the directory with our images
393 wxString dir;
394 if ( wxFile::Exists(wxT("./horse.png")) )
395 dir = wxT("./");
396 else if ( wxFile::Exists(wxT("../horse.png")) )
397 dir = wxT("../");
398 else
399 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
400
401 wxImage image = bitmap.ConvertToImage();
402
403 #if wxUSE_LIBPNG
404 if ( !image.SaveFile( dir + _T("test.png"), wxBITMAP_TYPE_PNG ))
405 wxLogError(wxT("Can't save file"));
406
407 image.Destroy();
408
409 if ( image.LoadFile( dir + _T("test.png") ) )
410 my_square = new wxBitmap( image );
411
412 image.Destroy();
413
414 if ( !image.LoadFile( dir + _T("horse.png")) )
415 wxLogError(wxT("Can't load PNG image"));
416 else
417 my_horse_png = new wxBitmap( image );
418 #endif // wxUSE_LIBPNG
419
420 #if wxUSE_LIBJPEG
421 image.Destroy();
422
423 if ( !image.LoadFile( dir + _T("horse.jpg")) )
424 wxLogError(wxT("Can't load JPG image"));
425 else
426 my_horse_jpeg = new wxBitmap( image );
427 #endif // wxUSE_LIBJPEG
428
429 #if wxUSE_GIF
430 image.Destroy();
431
432 if ( !image.LoadFile( dir + _T("horse.gif" )) )
433 wxLogError(wxT("Can't load GIF image"));
434 else
435 my_horse_gif = new wxBitmap( image );
436 #endif
437
438 #if wxUSE_PCX
439 image.Destroy();
440
441 if ( !image.LoadFile( dir + _T("horse.pcx"), wxBITMAP_TYPE_PCX ) )
442 wxLogError(wxT("Can't load PCX image"));
443 else
444 my_horse_pcx = new wxBitmap( image );
445 #endif
446
447 image.Destroy();
448
449 if ( !image.LoadFile( dir + _T("horse.bmp"), wxBITMAP_TYPE_BMP ) )
450 wxLogError(wxT("Can't load BMP image"));
451 else
452 my_horse_bmp = new wxBitmap( image );
453
454 #if wxUSE_XPM
455 image.Destroy();
456
457 if ( !image.LoadFile( dir + _T("horse.xpm"), wxBITMAP_TYPE_XPM ) )
458 wxLogError(wxT("Can't load XPM image"));
459 else
460 my_horse_xpm = new wxBitmap( image );
461
462 if ( !image.SaveFile( dir + _T("test.xpm"), wxBITMAP_TYPE_XPM ))
463 wxLogError(wxT("Can't save file"));
464 #endif
465
466 #if wxUSE_PNM
467 image.Destroy();
468
469 if ( !image.LoadFile( dir + _T("horse.pnm"), wxBITMAP_TYPE_PNM ) )
470 wxLogError(wxT("Can't load PNM image"));
471 else
472 my_horse_pnm = new wxBitmap( image );
473 #endif
474
475 #if wxUSE_LIBTIFF
476 image.Destroy();
477
478 if ( !image.LoadFile( dir + _T("horse.tif"), wxBITMAP_TYPE_TIF ) )
479 wxLogError(wxT("Can't load TIFF image"));
480 else
481 my_horse_tiff = new wxBitmap( image );
482 #endif
483
484 CreateAntiAliasedBitmap();
485
486 my_smile_xbm = new wxBitmap( (const char*)smile_bits, smile_width,
487 smile_height, 1 );
488
489 // demonstrates XPM automatically using the mask when saving
490 if ( m_bmpSmileXpm.Ok() )
491 m_bmpSmileXpm.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM);
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 size_t len = (size_t)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 #ifndef __WXGTK20__
682 // I cannot convince GTK2 to draw into mono bitmaps
683 memdc.DrawText( _T("Hi!"), 5, 5 );
684 #endif
685 memdc.SetBrush( *wxBLACK_BRUSH );
686 memdc.DrawRectangle( 33,5,20,20 );
687 memdc.SetPen( *wxRED_PEN );
688 memdc.DrawLine( 5, 42, 50, 42 );
689 memdc.SelectObject( wxNullBitmap );
690
691 if (mono.Ok())
692 {
693 dc.DrawText( _T("Mono bitmap"), 30, 2095 );
694 dc.DrawText( _T("(red on green)"), 30, 2110 );
695 dc.SetTextForeground( wxT("RED") );
696 dc.SetTextBackground( wxT("GREEN") );
697 dc.DrawBitmap( mono, 30, 2130 );
698
699 dc.SetTextForeground( wxT("BLACK") );
700 dc.DrawText( _T("After wxImage conversion"), 150, 2095 );
701 dc.DrawText( _T("(red on white)"), 150, 2110 );
702 dc.SetTextForeground( wxT("RED") );
703 wxImage i = mono.ConvertToImage();
704 i.SetMaskColour( 255,255,255 );
705 i.Replace( 0,0,0,
706 wxRED_PEN->GetColour().Red(),
707 wxRED_PEN->GetColour().Green(),
708 wxRED_PEN->GetColour().Blue() );
709 dc.DrawBitmap( wxBitmap(i), 150, 2130, true );
710 dc.SetTextForeground( wxT("BLACK") );
711 }
712
713 // For testing transparency
714 dc.SetBrush( *wxRED_BRUSH );
715 dc.DrawRectangle( 20, 2220, 560, 68 );
716
717 dc.DrawText(_T("XPM bitmap"), 30, 2230 );
718 if ( m_bmpSmileXpm.Ok() )
719 dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, true);
720
721 dc.DrawText(_T("XPM icon"), 110, 2230 );
722 if ( m_iconSmileXpm.Ok() )
723 dc.DrawIcon(m_iconSmileXpm, 110, 2250);
724
725 // testing icon -> bitmap conversion
726 wxBitmap to_blit( m_iconSmileXpm );
727 if (to_blit.Ok())
728 {
729 dc.DrawText( _T("SubBitmap"), 170, 2230 );
730 wxBitmap sub = to_blit.GetSubBitmap( wxRect(0,0,15,15) );
731 if (sub.Ok())
732 dc.DrawBitmap( sub, 170, 2250, true );
733
734 dc.DrawText( _T("Enlarged"), 250, 2230 );
735 dc.SetUserScale( 1.5, 1.5 );
736 dc.DrawBitmap( to_blit, (int)(250/1.5), (int)(2250/1.5), true );
737 dc.SetUserScale( 2, 2 );
738 dc.DrawBitmap( to_blit, (int)(300/2), (int)(2250/2), true );
739 dc.SetUserScale( 1.0, 1.0 );
740
741 dc.DrawText( _T("Blit"), 400, 2230);
742 wxMemoryDC blit_dc;
743 blit_dc.SelectObject( to_blit );
744 dc.Blit( 400, 2250, to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
745 dc.SetUserScale( 1.5, 1.5 );
746 dc.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
747 dc.SetUserScale( 2, 2 );
748 dc.Blit( (int)(500/2), (int)(2250/2), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
749 dc.SetUserScale( 1.0, 1.0 );
750 }
751
752 dc.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
753 if (my_horse_ico32 && my_horse_ico32->Ok())
754 dc.DrawBitmap( *my_horse_ico32, 30, 2330, true );
755
756 dc.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
757 if (my_horse_ico16 && my_horse_ico16->Ok())
758 dc.DrawBitmap( *my_horse_ico16, 230, 2330, true );
759
760 dc.DrawText( _T("ICO handler (best image)"), 430, 2290 );
761 if (my_horse_ico && my_horse_ico->Ok())
762 dc.DrawBitmap( *my_horse_ico, 430, 2330, true );
763
764 dc.DrawText( _T("CUR handler"), 30, 2390 );
765 if (my_horse_cur && my_horse_cur->Ok())
766 {
767 dc.DrawBitmap( *my_horse_cur, 30, 2420, true );
768 dc.SetPen (*wxRED_PEN);
769 dc.DrawLine (xH-10,yH,xH+10,yH);
770 dc.DrawLine (xH,yH-10,xH,yH+10);
771 }
772 dc.DrawText( _T("ANI handler"), 230, 2390 );
773 int i ;
774 for (i=0; i < m_ani_images; i ++)
775 if (my_horse_ani[i].Ok())
776 {
777 dc.DrawBitmap( my_horse_ani[i], 230 + i * 2 * my_horse_ani[i].GetWidth() , 2420, true );
778 }
779 }
780
781 void MyCanvas::CreateAntiAliasedBitmap()
782 {
783 wxBitmap bitmap( 300, 300 );
784
785 wxMemoryDC dc;
786
787 dc.SelectObject( bitmap );
788
789 dc.Clear();
790
791 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
792 dc.SetTextForeground( wxT("RED") );
793 dc.DrawText( _T("This is anti-aliased Text."), 20, 20 );
794 dc.DrawText( _T("And a Rectangle."), 20, 60 );
795
796 dc.SetBrush( *wxRED_BRUSH );
797 dc.SetPen( *wxTRANSPARENT_PEN );
798 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
799
800 wxImage original= bitmap.ConvertToImage();
801 wxImage anti( 150, 150 );
802
803 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
804
805 for (int y = 1; y < 149; y++)
806 for (int x = 1; x < 149; x++)
807 {
808 int red = original.GetRed( x*2, y*2 ) +
809 original.GetRed( x*2-1, y*2 ) +
810 original.GetRed( x*2, y*2+1 ) +
811 original.GetRed( x*2+1, y*2+1 );
812 red = red/4;
813
814 int green = original.GetGreen( x*2, y*2 ) +
815 original.GetGreen( x*2-1, y*2 ) +
816 original.GetGreen( x*2, y*2+1 ) +
817 original.GetGreen( x*2+1, y*2+1 );
818 green = green/4;
819
820 int blue = original.GetBlue( x*2, y*2 ) +
821 original.GetBlue( x*2-1, y*2 ) +
822 original.GetBlue( x*2, y*2+1 ) +
823 original.GetBlue( x*2+1, y*2+1 );
824 blue = blue/4;
825 anti.SetRGB( x, y, (unsigned char)red, (unsigned char)green, (unsigned char)blue );
826 }
827 my_anti = new wxBitmap(anti);
828 }
829
830 // MyFrame
831
832 enum
833 {
834 ID_QUIT = 108,
835 ID_ABOUT,
836 ID_NEW,
837 ID_SHOWRAW
838 };
839
840 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
841
842 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
843 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
844 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
845 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
846 #ifdef wxHAVE_RAW_BITMAP
847 EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap)
848 #endif
849
850 #if wxUSE_CLIPBOARD
851 EVT_MENU(wxID_COPY, MyFrame::OnCopy)
852 EVT_MENU(wxID_PASTE, MyFrame::OnPaste)
853 #endif // wxUSE_CLIPBOARD
854 END_EVENT_TABLE()
855
856 MyFrame::MyFrame()
857 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxImage sample"),
858 wxPoint(20,20), wxSize(470,360) )
859 {
860 wxMenuBar *menu_bar = new wxMenuBar();
861
862 wxMenu *menuImage = new wxMenu;
863 menuImage->Append( ID_NEW, _T("&Show any image...\tCtrl-O"));
864
865 #ifdef wxHAVE_RAW_BITMAP
866 menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R"));
867 #endif
868 menuImage->AppendSeparator();
869 menuImage->Append( ID_ABOUT, _T("&About..."));
870 menuImage->AppendSeparator();
871 menuImage->Append( ID_QUIT, _T("E&xit\tCtrl-Q"));
872 menu_bar->Append(menuImage, _T("&Image"));
873
874 #if wxUSE_CLIPBOARD
875 wxMenu *menuClipboard = new wxMenu;
876 menuClipboard->Append(wxID_COPY, _T("&Copy test image\tCtrl-C"));
877 menuClipboard->Append(wxID_PASTE, _T("&Paste image\tCtrl-V"));
878 menu_bar->Append(menuClipboard, _T("&Clipboard"));
879 #endif // wxUSE_CLIPBOARD
880
881 SetMenuBar( menu_bar );
882
883 #if wxUSE_STATUSBAR
884 CreateStatusBar(2);
885 int widths[] = { -1, 100 };
886 SetStatusWidths( 2, widths );
887 #endif // wxUSE_STATUSBAR
888
889 m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) );
890
891 // 500 width * 2500 height
892 m_canvas->SetScrollbars( 10, 10, 50, 250 );
893 }
894
895 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
896 {
897 Close( true );
898 }
899
900 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
901 {
902 (void)wxMessageBox( _T("wxImage demo\n")
903 _T("Robert Roebling (c) 1998,2000"),
904 _T("About wxImage Demo"), wxICON_INFORMATION | wxOK );
905 }
906
907 void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
908 {
909 wxString filename = wxFileSelector(_T("Select image file"));
910 if ( !filename )
911 return;
912
913 wxImage image;
914 if ( !image.LoadFile(filename) )
915 {
916 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
917
918 return;
919 }
920
921 (new MyImageFrame(this, wxBitmap(image)))->Show();
922 }
923
924 #ifdef wxHAVE_RAW_BITMAP
925
926 void MyFrame::OnTestRawBitmap( wxCommandEvent &WXUNUSED(event) )
927 {
928 (new MyRawBitmapFrame(this))->Show();
929 }
930
931 #endif // wxHAVE_RAW_BITMAP
932
933 #if wxUSE_CLIPBOARD
934
935 void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
936 {
937 wxBitmapDataObject *dobjBmp = new wxBitmapDataObject;
938 dobjBmp->SetBitmap(*m_canvas->my_horse_png);
939
940 wxTheClipboard->Open();
941
942 if ( !wxTheClipboard->SetData(dobjBmp) )
943 {
944 wxLogError(_T("Failed to copy bitmap to clipboard"));
945 }
946
947 wxTheClipboard->Close();
948 }
949
950 void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
951 {
952 wxBitmapDataObject dobjBmp;
953
954 wxTheClipboard->Open();
955 if ( !wxTheClipboard->GetData(dobjBmp) )
956 {
957 wxLogMessage(_T("No bitmap data in the clipboard"));
958 }
959 else
960 {
961 (new MyImageFrame(this, dobjBmp.GetBitmap()))->Show();
962 }
963 wxTheClipboard->Close();
964 }
965
966 #endif // wxUSE_CLIPBOARD
967
968 //-----------------------------------------------------------------------------
969 // MyApp
970 //-----------------------------------------------------------------------------
971
972 bool MyApp::OnInit()
973 {
974 #if wxUSE_LIBPNG
975 wxImage::AddHandler( new wxPNGHandler );
976 #endif
977
978 #if wxUSE_LIBJPEG
979 wxImage::AddHandler( new wxJPEGHandler );
980 #endif
981
982 #if wxUSE_LIBTIFF
983 wxImage::AddHandler( new wxTIFFHandler );
984 #endif
985
986 #if wxUSE_GIF
987 wxImage::AddHandler( new wxGIFHandler );
988 #endif
989
990 #if wxUSE_PCX
991 wxImage::AddHandler( new wxPCXHandler );
992 #endif
993
994 #if wxUSE_PNM
995 wxImage::AddHandler( new wxPNMHandler );
996 #endif
997
998 #if wxUSE_XPM
999 wxImage::AddHandler( new wxXPMHandler );
1000 #endif
1001
1002 #if wxUSE_ICO_CUR
1003 wxImage::AddHandler( new wxICOHandler );
1004 wxImage::AddHandler( new wxCURHandler );
1005 wxImage::AddHandler( new wxANIHandler );
1006 #endif
1007
1008 wxFrame *frame = new MyFrame();
1009 frame->Show( true );
1010
1011 return true;
1012 }
1013