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