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