]> git.saurik.com Git - wxWidgets.git/blame - samples/image/image.cpp
Fixes for wxUSE_STATUSBAR.
[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)
47f797bd 145 : wxFrame(parent, wxID_ANY, _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 );
47f797bd 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"),
47f797bd
WS
196 wxSAVE,
197 this);
1971d23c
VZ
198
199 if ( savefilename.empty() )
200 return;
201
fd94e8aa 202 if ( image.GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT) == wxBMP_8BPP_PALETTE )
1971d23c
VZ
203 {
204 unsigned char *cmap = new unsigned char [256];
205 for ( int i = 0; i < 256; i++ )
206 cmap[i] = i;
207 image.SetPalette(wxPalette(256, cmap, cmap, cmap));
208
209 delete cmap;
210 }
211
45647dcf 212 bool loaded;
1971d23c
VZ
213 wxString extension = savefilename.AfterLast('.').Lower();
214
51b07644 215 if (extension == _T("cur"))
45647dcf 216 {
51b07644
VZ
217 image.Rescale(32,32);
218 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0);
45647dcf
VS
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 }
51b07644 224 else
45647dcf
VS
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 }
51b07644 230
45647dcf 231 if ( !loaded )
51b07644
VZ
232 wxMessageBox(_T("No handler for this file type."),
233 _T("File was not saved"),
1971d23c
VZ
234 wxOK|wxCENTRE, this);
235 }
236
ab0f0386
VZ
237private:
238 wxBitmap m_bitmap;
239
240 DECLARE_EVENT_TABLE()
241};
242
b3f04dc2
VZ
243#ifdef wxHAVE_RAW_BITMAP
244
245#include "wx/rawbmp.h"
246
247class MyRawBitmapFrame : public wxFrame
248{
249public:
250 enum
251 {
252 BORDER = 15,
253 SIZE = 150,
254 REAL_SIZE = SIZE - 2*BORDER
255 };
256
257 MyRawBitmapFrame(wxFrame *parent)
47f797bd 258 : wxFrame(parent, wxID_ANY, _T("Raw bitmaps (how exciting)")),
b3f04dc2
VZ
259 m_bitmap(SIZE, SIZE, 32)
260 {
261 SetClientSize(SIZE, SIZE);
262
095980e1
VZ
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));
b3f04dc2
VZ
268 if ( !data )
269 {
270 wxLogError(_T("Failed to gain raw access to bitmap data"));
271 return;
272 }
273
10b41b53
VZ
274 data.UseAlpha();
275
095980e1 276 Data::Iterator p(data);
b3f04dc2 277
b3f04dc2
VZ
278 for ( int y = 0; y < REAL_SIZE; ++y )
279 {
095980e1 280 Data::Iterator rowStart = p;
b3f04dc2
VZ
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;
095980e1 291 p.Alpha() = (Data::Iterator::ChannelType)((x*255.)/REAL_SIZE);
b3f04dc2
VZ
292
293 ++p; // same as p.OffsetX(1)
294 }
295
296 p = rowStart;
1d2f48b6 297 p.OffsetY(data, 1);
b3f04dc2
VZ
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);
47f797bd 307 dc.DrawBitmap( m_bitmap, 0, 0, true /* use mask */ );
b3f04dc2
VZ
308 }
309
310private:
311 wxBitmap m_bitmap;
312
313 DECLARE_EVENT_TABLE()
314};
315
316#endif // wxHAVE_RAW_BITMAP
317
01111366
RR
318// MyApp
319
320class MyApp: public wxApp
321{
1d5b7a0b
VZ
322public:
323 virtual bool OnInit();
01111366
RR
324};
325
326// main program
327
328IMPLEMENT_APP(MyApp)
329
330// MyCanvas
331
332IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
333
ab0f0386 334BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
0c543b7a
VZ
335 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground)
336 EVT_PAINT(MyImageFrame::OnPaint)
337 EVT_LEFT_DCLICK(MyImageFrame::OnSave)
ab0f0386
VZ
338END_EVENT_TABLE()
339
b3f04dc2
VZ
340#ifdef wxHAVE_RAW_BITMAP
341
342BEGIN_EVENT_TABLE(MyRawBitmapFrame, wxFrame)
343 EVT_PAINT(MyRawBitmapFrame::OnPaint)
344END_EVENT_TABLE()
345
346#endif // wxHAVE_RAW_BITMAP
347
1d5b7a0b
VZ
348BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
349 EVT_PAINT(MyCanvas::OnPaint)
01111366
RR
350END_EVENT_TABLE()
351
acbd13a3 352MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
1d5b7a0b 353 const wxPoint &pos, const wxSize &size )
81278df2 354 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
81278df2
VZ
355 , m_bmpSmileXpm((const char **) smile_xpm)
356 , m_iconSmileXpm((const char **) smile_xpm)
01111366 357{
60a41aee
RR
358 my_horse_png = (wxBitmap*) NULL;
359 my_horse_jpeg = (wxBitmap*) NULL;
360 my_horse_gif = (wxBitmap*) NULL;
361 my_horse_bmp = (wxBitmap*) NULL;
51b07644 362 my_horse_bmp2 = (wxBitmap*) NULL;
60a41aee
RR
363 my_horse_pcx = (wxBitmap*) NULL;
364 my_horse_pnm = (wxBitmap*) NULL;
365 my_horse_tiff = (wxBitmap*) NULL;
9d8bdf2d 366 my_horse_xpm = (wxBitmap*) NULL;
bf504f98
VS
367 my_horse_ico32 = (wxBitmap*) NULL;
368 my_horse_ico16 = (wxBitmap*) NULL;
369 my_horse_ico = (wxBitmap*) NULL;
370 my_horse_cur = (wxBitmap*) NULL;
2b5f62a0 371 my_horse_ani = (wxBitmap*) NULL;
bf504f98 372
dc1dbfc6 373 my_smile_xbm = (wxBitmap*) NULL;
60a41aee
RR
374 my_square = (wxBitmap*) NULL;
375 my_anti = (wxBitmap*) NULL;
376
2b5f62a0
VZ
377 m_ani_images = 0 ;
378
60a41aee
RR
379 SetBackgroundColour(* wxWHITE);
380
381 wxBitmap bitmap( 100, 100 );
382
383 wxMemoryDC dc;
384 dc.SelectObject( bitmap );
a60b1f5d 385 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
cf3da716 386 dc.SetPen( *wxBLACK_PEN );
60a41aee 387 dc.DrawRectangle( 0, 0, 100, 100 );
cf3da716
RR
388 dc.SetBrush( *wxWHITE_BRUSH );
389 dc.DrawRectangle( 20, 20, 60, 60 );
60a41aee
RR
390 dc.SelectObject( wxNullBitmap );
391
392 // try to find the directory with our images
393 wxString dir;
4693b20c 394 if ( wxFile::Exists(wxT("./horse.png")) )
2b5f62a0 395 dir = wxT("./");
4693b20c 396 else if ( wxFile::Exists(wxT("../horse.png")) )
2b5f62a0 397 dir = wxT("../");
60a41aee 398 else
4693b20c 399 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
60a41aee 400
a8d4e3ac 401 wxImage image = bitmap.ConvertToImage();
60a41aee 402
bea56879 403#if wxUSE_LIBPNG
51b07644 404 if ( !image.SaveFile( dir + _T("test.png"), wxBITMAP_TYPE_PNG ))
4693b20c 405 wxLogError(wxT("Can't save file"));
f6bcfd97 406
176fe5af 407 image.Destroy();
60a41aee 408
30ad0a14
VZ
409 if ( image.LoadFile( dir + _T("test.png") ) )
410 my_square = new wxBitmap( image );
f6bcfd97 411
176fe5af 412 image.Destroy();
cc977e5f 413
51b07644 414 if ( !image.LoadFile( dir + _T("horse.png")) )
4693b20c 415 wxLogError(wxT("Can't load PNG image"));
60a41aee 416 else
a8d4e3ac 417 my_horse_png = new wxBitmap( image );
bea56879 418#endif // wxUSE_LIBPNG
60a41aee 419
bea56879 420#if wxUSE_LIBJPEG
176fe5af 421 image.Destroy();
279ababf 422
51b07644 423 if ( !image.LoadFile( dir + _T("horse.jpg")) )
4693b20c 424 wxLogError(wxT("Can't load JPG image"));
60a41aee 425 else
9d8bdf2d 426 my_horse_jpeg = new wxBitmap( image );
bea56879 427#endif // wxUSE_LIBJPEG
6e47faf1
JS
428
429#if wxUSE_GIF
176fe5af 430 image.Destroy();
279ababf 431
2b5f62a0 432 if ( !image.LoadFile( dir + _T("horse.gif" )) )
4693b20c 433 wxLogError(wxT("Can't load GIF image"));
60a41aee 434 else
9d8bdf2d 435 my_horse_gif = new wxBitmap( image );
6e47faf1 436#endif
cbd4be25 437
6e47faf1 438#if wxUSE_PCX
176fe5af 439 image.Destroy();
279ababf 440
51b07644 441 if ( !image.LoadFile( dir + _T("horse.pcx"), wxBITMAP_TYPE_PCX ) )
4693b20c 442 wxLogError(wxT("Can't load PCX image"));
60a41aee 443 else
9d8bdf2d 444 my_horse_pcx = new wxBitmap( image );
6e47faf1 445#endif
cbd4be25 446
176fe5af 447 image.Destroy();
279ababf 448
51b07644 449 if ( !image.LoadFile( dir + _T("horse.bmp"), wxBITMAP_TYPE_BMP ) )
4693b20c 450 wxLogError(wxT("Can't load BMP image"));
60a41aee 451 else
9d8bdf2d 452 my_horse_bmp = new wxBitmap( image );
6319afe3 453
a8d4e3ac 454#if wxUSE_XPM
9d8bdf2d
VS
455 image.Destroy();
456
51b07644 457 if ( !image.LoadFile( dir + _T("horse.xpm"), wxBITMAP_TYPE_XPM ) )
4693b20c 458 wxLogError(wxT("Can't load XPM image"));
9d8bdf2d
VS
459 else
460 my_horse_xpm = new wxBitmap( image );
461
51b07644 462 if ( !image.SaveFile( dir + _T("test.xpm"), wxBITMAP_TYPE_XPM ))
4693b20c 463 wxLogError(wxT("Can't save file"));
a8d4e3ac
VS
464#endif
465
6e47faf1 466#if wxUSE_PNM
176fe5af 467 image.Destroy();
279ababf 468
51b07644 469 if ( !image.LoadFile( dir + _T("horse.pnm"), wxBITMAP_TYPE_PNM ) )
4693b20c 470 wxLogError(wxT("Can't load PNM image"));
60a41aee 471 else
9d8bdf2d 472 my_horse_pnm = new wxBitmap( image );
6e47faf1 473#endif
f048e32f 474
60a41aee 475#if wxUSE_LIBTIFF
176fe5af 476 image.Destroy();
279ababf 477
51b07644 478 if ( !image.LoadFile( dir + _T("horse.tif"), wxBITMAP_TYPE_TIF ) )
4693b20c 479 wxLogError(wxT("Can't load TIFF image"));
60a41aee 480 else
9d8bdf2d 481 my_horse_tiff = new wxBitmap( image );
60a41aee
RR
482#endif
483
60a41aee 484 CreateAntiAliasedBitmap();
dc1dbfc6
RL
485
486 my_smile_xbm = new wxBitmap( (const char*)smile_bits, smile_width,
487 smile_height, 1 );
bfef20bf
GRG
488
489 // demonstrates XPM automatically using the mask when saving
490 if ( m_bmpSmileXpm.Ok() )
51b07644 491 m_bmpSmileXpm.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM);
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() );
47f797bd 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 );
9fe7d430
RR
681#ifndef __WXGTK20__
682 // I cannot convince GTK2 to draw into mono bitmaps
51b07644 683 memdc.DrawText( _T("Hi!"), 5, 5 );
9fe7d430 684#endif
41fbc841
RR
685 memdc.SetBrush( *wxBLACK_BRUSH );
686 memdc.DrawRectangle( 33,5,20,20 );
687 memdc.SetPen( *wxRED_PEN );
688 memdc.DrawLine( 5, 42, 50, 42 );
82ea63e6 689 memdc.SelectObject( wxNullBitmap );
176fe5af 690
408b4168 691 if (mono.Ok())
82ea63e6 692 {
51b07644
VZ
693 dc.DrawText( _T("Mono bitmap"), 30, 2095 );
694 dc.DrawText( _T("(red on green)"), 30, 2110 );
a60b1f5d
MB
695 dc.SetTextForeground( wxT("RED") );
696 dc.SetTextBackground( wxT("GREEN") );
9d8bdf2d 697 dc.DrawBitmap( mono, 30, 2130 );
408b4168 698
a60b1f5d 699 dc.SetTextForeground( wxT("BLACK") );
51b07644
VZ
700 dc.DrawText( _T("After wxImage conversion"), 150, 2095 );
701 dc.DrawText( _T("(red on white)"), 150, 2110 );
a60b1f5d 702 dc.SetTextForeground( wxT("RED") );
a8d4e3ac 703 wxImage i = mono.ConvertToImage();
408b4168
VZ
704 i.SetMaskColour( 255,255,255 );
705 i.Replace( 0,0,0,
82ea63e6
RR
706 wxRED_PEN->GetColour().Red(),
707 wxRED_PEN->GetColour().Green(),
708 wxRED_PEN->GetColour().Blue() );
47f797bd 709 dc.DrawBitmap( wxBitmap(i), 150, 2130, true );
a60b1f5d 710 dc.SetTextForeground( wxT("BLACK") );
bea56879
VZ
711 }
712
f9ebac93
RR
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 );
bea56879 718 if ( m_bmpSmileXpm.Ok() )
47f797bd 719 dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, true);
bea56879 720
f9ebac93 721 dc.DrawText(_T("XPM icon"), 110, 2230 );
bea56879 722 if ( m_iconSmileXpm.Ok() )
f9ebac93
RR
723 dc.DrawIcon(m_iconSmileXpm, 110, 2250);
724
725 // testing icon -> bitmap conversion
726 wxBitmap to_blit( m_iconSmileXpm );
3ef37e7f
JS
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())
47f797bd 732 dc.DrawBitmap( sub, 170, 2250, true );
3ef37e7f
JS
733
734 dc.DrawText( _T("Enlarged"), 250, 2230 );
735 dc.SetUserScale( 1.5, 1.5 );
47f797bd 736 dc.DrawBitmap( to_blit, (int)(250/1.5), (int)(2250/1.5), true );
3ef37e7f 737 dc.SetUserScale( 2, 2 );
47f797bd 738 dc.DrawBitmap( to_blit, (int)(300/2), (int)(2250/2), true );
3ef37e7f
JS
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 );
47f797bd 744 dc.Blit( 400, 2250, to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
3ef37e7f 745 dc.SetUserScale( 1.5, 1.5 );
47f797bd 746 dc.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
3ef37e7f 747 dc.SetUserScale( 2, 2 );
47f797bd 748 dc.Blit( (int)(500/2), (int)(2250/2), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
3ef37e7f
JS
749 dc.SetUserScale( 1.0, 1.0 );
750 }
bf504f98 751
51b07644 752 dc.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
bf504f98 753 if (my_horse_ico32 && my_horse_ico32->Ok())
47f797bd 754 dc.DrawBitmap( *my_horse_ico32, 30, 2330, true );
bf504f98 755
51b07644 756 dc.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
bf504f98 757 if (my_horse_ico16 && my_horse_ico16->Ok())
47f797bd 758 dc.DrawBitmap( *my_horse_ico16, 230, 2330, true );
51b07644
VZ
759
760 dc.DrawText( _T("ICO handler (best image)"), 430, 2290 );
bf504f98 761 if (my_horse_ico && my_horse_ico->Ok())
47f797bd 762 dc.DrawBitmap( *my_horse_ico, 430, 2330, true );
bf504f98 763
51b07644 764 dc.DrawText( _T("CUR handler"), 30, 2390 );
bf504f98 765 if (my_horse_cur && my_horse_cur->Ok())
51b07644 766 {
47f797bd 767 dc.DrawBitmap( *my_horse_cur, 30, 2420, true );
bf504f98
VS
768 dc.SetPen (*wxRED_PEN);
769 dc.DrawLine (xH-10,yH,xH+10,yH);
770 dc.DrawLine (xH,yH-10,xH,yH+10);
51b07644 771 }
2b5f62a0
VZ
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 {
47f797bd 777 dc.DrawBitmap( my_horse_ani[i], 230 + i * 2 * my_horse_ani[i].GetWidth() , 2420, true );
2b5f62a0 778 }
9390a202 779}
1d5b7a0b 780
9390a202
RR
781void MyCanvas::CreateAntiAliasedBitmap()
782{
783 wxBitmap bitmap( 300, 300 );
a91b47e8 784
9390a202 785 wxMemoryDC dc;
a91b47e8 786
9390a202 787 dc.SelectObject( bitmap );
a91b47e8 788
9390a202 789 dc.Clear();
f048e32f 790
6e47faf1 791 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
a60b1f5d 792 dc.SetTextForeground( wxT("RED") );
51b07644
VZ
793 dc.DrawText( _T("This is anti-aliased Text."), 20, 20 );
794 dc.DrawText( _T("And a Rectangle."), 20, 60 );
f048e32f 795
9390a202 796 dc.SetBrush( *wxRED_BRUSH );
91b8de8d 797 dc.SetPen( *wxTRANSPARENT_PEN );
9390a202 798 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
f048e32f 799
a8d4e3ac 800 wxImage original= bitmap.ConvertToImage();
9390a202 801 wxImage anti( 150, 150 );
a91b47e8 802
9390a202 803 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
f048e32f 804
9390a202
RR
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 ) +
f048e32f
VZ
809 original.GetRed( x*2-1, y*2 ) +
810 original.GetRed( x*2, y*2+1 ) +
9390a202 811 original.GetRed( x*2+1, y*2+1 );
f048e32f
VZ
812 red = red/4;
813
9390a202 814 int green = original.GetGreen( x*2, y*2 ) +
f048e32f
VZ
815 original.GetGreen( x*2-1, y*2 ) +
816 original.GetGreen( x*2, y*2+1 ) +
9390a202 817 original.GetGreen( x*2+1, y*2+1 );
f048e32f
VZ
818 green = green/4;
819
9390a202 820 int blue = original.GetBlue( x*2, y*2 ) +
f048e32f
VZ
821 original.GetBlue( x*2-1, y*2 ) +
822 original.GetBlue( x*2, y*2+1 ) +
9390a202 823 original.GetBlue( x*2+1, y*2+1 );
f048e32f 824 blue = blue/4;
9390a202
RR
825 anti.SetRGB( x, y, red, green, blue );
826 }
368d59f0 827 my_anti = new wxBitmap(anti);
01111366
RR
828}
829
830// MyFrame
831
b3f04dc2
VZ
832enum
833{
834 ID_QUIT = 108,
835 ID_ABOUT,
836 ID_NEW,
837 ID_SHOWRAW
838};
01111366
RR
839
840IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
841
842BEGIN_EVENT_TABLE(MyFrame,wxFrame)
843 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
844 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
ab0f0386 845 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
b3f04dc2
VZ
846#ifdef wxHAVE_RAW_BITMAP
847 EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap)
848#endif
da9df1f5 849
dd38c875 850#if wxUSE_CLIPBOARD
da9df1f5
VZ
851 EVT_MENU(wxID_COPY, MyFrame::OnCopy)
852 EVT_MENU(wxID_PASTE, MyFrame::OnPaste)
853#endif // wxUSE_CLIPBOARD
01111366
RR
854END_EVENT_TABLE()
855
1d5b7a0b 856MyFrame::MyFrame()
47f797bd 857 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxImage sample"),
1d5b7a0b 858 wxPoint(20,20), wxSize(470,360) )
01111366 859{
da9df1f5
VZ
860 wxMenuBar *menu_bar = new wxMenuBar();
861
862 wxMenu *menuImage = new wxMenu;
863 menuImage->Append( ID_NEW, _T("&Show any image...\tCtrl-O"));
1e3c12d7 864
b3f04dc2 865#ifdef wxHAVE_RAW_BITMAP
da9df1f5 866 menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R"));
b3f04dc2 867#endif
da9df1f5
VZ
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
dd38c875 874#if wxUSE_CLIPBOARD
da9df1f5
VZ
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
3d05544e 880
01111366 881 SetMenuBar( menu_bar );
1d5b7a0b 882
917e533b
RR
883 CreateStatusBar(2);
884 int widths[] = { -1, 100 };
885 SetStatusWidths( 2, widths );
1d5b7a0b 886
47f797bd 887 m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) );
cbd4be25 888
bf504f98
VS
889 // 500 width * 2500 height
890 m_canvas->SetScrollbars( 10, 10, 50, 250 );
01111366
RR
891}
892
893void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
894{
47f797bd 895 Close( true );
01111366
RR
896}
897
898void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
899{
51b07644
VZ
900 (void)wxMessageBox( _T("wxImage demo\n")
901 _T("Robert Roebling (c) 1998,2000"),
902 _T("About wxImage Demo"), wxICON_INFORMATION | wxOK );
fb1585ae
RR
903}
904
ab0f0386
VZ
905void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
906{
f6bcfd97
BP
907 wxString filename = wxFileSelector(_T("Select image file"));
908 if ( !filename )
909 return;
910
911 wxImage image;
912 if ( !image.LoadFile(filename) )
913 {
914 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
915
916 return;
917 }
918
368d59f0 919 (new MyImageFrame(this, wxBitmap(image)))->Show();
ab0f0386
VZ
920}
921
b3f04dc2
VZ
922#ifdef wxHAVE_RAW_BITMAP
923
481721e8 924void MyFrame::OnTestRawBitmap( wxCommandEvent &WXUNUSED(event) )
b3f04dc2
VZ
925{
926 (new MyRawBitmapFrame(this))->Show();
927}
928
929#endif // wxHAVE_RAW_BITMAP
930
dd38c875 931#if wxUSE_CLIPBOARD
da9df1f5
VZ
932
933void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
934{
935 wxBitmapDataObject *dobjBmp = new wxBitmapDataObject;
936 dobjBmp->SetBitmap(*m_canvas->my_horse_png);
937
dd38c875
MB
938 wxTheClipboard->Open();
939
da9df1f5
VZ
940 if ( !wxTheClipboard->SetData(dobjBmp) )
941 {
942 wxLogError(_T("Failed to copy bitmap to clipboard"));
943 }
dd38c875
MB
944
945 wxTheClipboard->Close();
da9df1f5
VZ
946}
947
948void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
949{
950 wxBitmapDataObject dobjBmp;
dd38c875
MB
951
952 wxTheClipboard->Open();
da9df1f5
VZ
953 if ( !wxTheClipboard->GetData(dobjBmp) )
954 {
955 wxLogMessage(_T("No bitmap data in the clipboard"));
956 }
957 else
958 {
959 (new MyImageFrame(this, dobjBmp.GetBitmap()))->Show();
960 }
dd38c875 961 wxTheClipboard->Close();
da9df1f5
VZ
962}
963
964#endif // wxUSE_CLIPBOARD
965
01111366
RR
966//-----------------------------------------------------------------------------
967// MyApp
968//-----------------------------------------------------------------------------
969
1d5b7a0b 970bool MyApp::OnInit()
01111366 971{
e9c4b1a2
JS
972#if wxUSE_LIBPNG
973 wxImage::AddHandler( new wxPNGHandler );
974#endif
975
976#if wxUSE_LIBJPEG
977 wxImage::AddHandler( new wxJPEGHandler );
978#endif
979
60a41aee
RR
980#if wxUSE_LIBTIFF
981 wxImage::AddHandler( new wxTIFFHandler );
982#endif
983
6e47faf1 984#if wxUSE_GIF
e1929140 985 wxImage::AddHandler( new wxGIFHandler );
6e47faf1
JS
986#endif
987
988#if wxUSE_PCX
cbd4be25 989 wxImage::AddHandler( new wxPCXHandler );
6e47faf1
JS
990#endif
991
992#if wxUSE_PNM
6319afe3 993 wxImage::AddHandler( new wxPNMHandler );
6e47faf1 994#endif
e1929140 995
a8d4e3ac
VS
996#if wxUSE_XPM
997 wxImage::AddHandler( new wxXPMHandler );
998#endif
999
bf504f98
VS
1000#if wxUSE_ICO_CUR
1001 wxImage::AddHandler( new wxICOHandler );
1002 wxImage::AddHandler( new wxCURHandler );
2b5f62a0 1003 wxImage::AddHandler( new wxANIHandler );
bf504f98
VS
1004#endif
1005
01111366 1006 wxFrame *frame = new MyFrame();
47f797bd 1007 frame->Show( true );
1d5b7a0b 1008
47f797bd 1009 return true;
01111366
RR
1010}
1011