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