]> git.saurik.com Git - wxWidgets.git/blame - samples/image/image.cpp
version support
[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
da9df1f5
VZ
38#if defined(__WXMSW__)
39 #define wxHAVE_RAW_BITMAP
8f4b6405 40#endif
a8d4e3ac 41
01111366
RR
42// derived classes
43
44class MyFrame;
45class MyApp;
46
47// MyCanvas
48
49class MyCanvas: public wxScrolledWindow
50{
1d5b7a0b 51public:
51b07644 52 MyCanvas() {}
01111366 53 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
1d5b7a0b 54 ~MyCanvas();
01111366 55 void OnPaint( wxPaintEvent &event );
9390a202 56 void CreateAntiAliasedBitmap();
1d5b7a0b 57
e1929140
RR
58 wxBitmap *my_horse_png;
59 wxBitmap *my_horse_jpeg;
60 wxBitmap *my_horse_gif;
ca26177c 61 wxBitmap *my_horse_bmp;
51b07644 62 wxBitmap *my_horse_bmp2;
cbd4be25 63 wxBitmap *my_horse_pcx;
6319afe3 64 wxBitmap *my_horse_pnm;
60a41aee 65 wxBitmap *my_horse_tiff;
9d8bdf2d 66 wxBitmap *my_horse_xpm;
bf504f98
VS
67 wxBitmap *my_horse_ico32;
68 wxBitmap *my_horse_ico16;
69 wxBitmap *my_horse_ico;
70 wxBitmap *my_horse_cur;
2b5f62a0 71 wxBitmap *my_horse_ani;
51b07644 72
dc1dbfc6 73 wxBitmap *my_smile_xbm;
e8fdc264 74 wxBitmap *my_square;
9390a202 75 wxBitmap *my_anti;
1d5b7a0b 76
bf504f98 77 int xH, yH ;
2b5f62a0 78 int m_ani_images ;
bf504f98 79
bea56879
VZ
80protected:
81 wxBitmap m_bmpSmileXpm;
82 wxIcon m_iconSmileXpm;
83
60a41aee 84private:
1d5b7a0b
VZ
85 DECLARE_DYNAMIC_CLASS(MyCanvas)
86 DECLARE_EVENT_TABLE()
01111366
RR
87};
88
18134a1c
VZ
89
90const int nChoices = 8 ;
91static const wxString bppchoices[nChoices] =
92{
51b07644
VZ
93 _T("1 bpp color"),
94 _T("1 bpp B&W"),
95 _T("4 bpp color"),
96 _T("8 bpp color"),
97 _T("8 bpp greyscale"),
98 _T("8 bpp red"),
99 _T("8 bpp own palette"),
100 _T("24 bpp")
18134a1c
VZ
101};
102
103static const int bppvalues[nChoices] =
104{
105 wxBMP_1BPP,
106 wxBMP_1BPP_BW,
107 wxBMP_4BPP,
108 wxBMP_8BPP,
109 wxBMP_8BPP_GREY,
110 wxBMP_8BPP_RED,
111 wxBMP_8BPP_PALETTE,
112 wxBMP_24BPP
113};
114
01111366
RR
115// MyFrame
116
18134a1c 117
01111366
RR
118class MyFrame: public wxFrame
119{
1d5b7a0b
VZ
120public:
121 MyFrame();
01111366 122
01111366 123 void OnAbout( wxCommandEvent &event );
ab0f0386 124 void OnNewFrame( wxCommandEvent &event );
b3f04dc2
VZ
125#ifdef wxHAVE_RAW_BITMAP
126 void OnTestRawBitmap( wxCommandEvent &event );
127#endif // wxHAVE_RAW_BITMAP
01111366 128 void OnQuit( wxCommandEvent &event );
1d5b7a0b 129
da9df1f5
VZ
130#if wxUSE_CLIPBOARD
131 void OnCopy(wxCommandEvent& event);
132 void OnPaste(wxCommandEvent& event);
133#endif // wxUSE_CLIPBOARD
134
01111366 135 MyCanvas *m_canvas;
1d5b7a0b 136
60a41aee 137private:
1d5b7a0b
VZ
138 DECLARE_DYNAMIC_CLASS(MyFrame)
139 DECLARE_EVENT_TABLE()
01111366
RR
140};
141
ab0f0386
VZ
142class MyImageFrame : public wxFrame
143{
144public:
145 MyImageFrame(wxFrame *parent, const wxBitmap& bitmap)
1971d23c 146 : wxFrame(parent, -1, _T("Double click to save"),
ab0f0386 147 wxDefaultPosition, wxDefaultSize,
40c79fab 148 wxCAPTION | wxSYSTEM_MENU | wxCLOSE_BOX),
1971d23c 149 m_bitmap(bitmap)
ab0f0386
VZ
150 {
151 SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
152 }
153
0c543b7a
VZ
154 void OnEraseBackground(wxEraseEvent& WXUNUSED(event))
155 {
156 // do nothing here to be able to see how transparent images are shown
157 }
158
ab0f0386
VZ
159 void OnPaint(wxPaintEvent& WXUNUSED(event))
160 {
161 wxPaintDC dc( this );
0c543b7a 162 dc.DrawBitmap( m_bitmap, 0, 0, TRUE /* use mask */ );
ab0f0386
VZ
163 }
164
c5c843e0 165 void OnSave(wxMouseEvent& WXUNUSED(event))
1971d23c 166 {
368d59f0 167 wxImage image = m_bitmap.ConvertToImage();
1971d23c 168
51b07644
VZ
169 int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"),
170 _T("Set BMP BPP"),
18134a1c 171 nChoices,
1971d23c
VZ
172 bppchoices,
173 this);
174 if ( bppselection == -1 )
175 {
176 // cancelled
177 return;
178 }
179
fd94e8aa 180 image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, bppvalues[bppselection]);
1971d23c
VZ
181
182 wxString deffilename = bppchoices[bppselection];
4693b20c
MB
183 deffilename.Replace(wxT(" "), wxT("_"));
184 deffilename += wxT(".bmp");
185 wxString savefilename = wxFileSelector( wxT("Save Image"),
186 wxT(""),
1971d23c 187 deffilename,
4693b20c
MB
188 (const wxChar *)NULL,
189 wxT("BMP files (*.bmp)|*.bmp|")
190 wxT("PNG files (*.png)|*.png|")
191 wxT("JPEG files (*.jpg)|*.jpg|")
192 wxT("GIF files (*.gif)|*.gif|")
193 wxT("TIFF files (*.tif)|*.tif|")
bf504f98
VS
194 wxT("PCX files (*.pcx)|*.pcx|")
195 wxT("ICO files (*.ico)|*.ico|")
196 wxT("CUR files (*.cur)|*.cur"),
1971d23c
VZ
197 wxSAVE);
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)
258 : wxFrame(parent, -1, _T("Raw bitmaps (how exciting)")),
259 m_bitmap(SIZE, SIZE, 32)
260 {
261 SetClientSize(SIZE, SIZE);
262
263 wxRawBitmapData data(m_bitmap);
264 if ( !data )
265 {
266 wxLogError(_T("Failed to gain raw access to bitmap data"));
267 return;
268 }
269
270 wxRawBitmapIterator p(data);
271
272 p.Offset(BORDER, BORDER);
273
274 for ( int y = 0; y < REAL_SIZE; ++y )
275 {
276 wxRawBitmapIterator rowStart = p;
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;
287 p.Alpha() = x;
288
289 ++p; // same as p.OffsetX(1)
290 }
291
292 p = rowStart;
293 p.OffsetY(1);
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
51b07644 407 image.LoadFile( dir + _T("test.png") );
a8d4e3ac 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"));
550 off_t len = file.Length();
551 void *data = malloc(len);
552 if ( file.Read(data, len) != len )
553 wxLogError(_T("Reading bitmap file failed"));
554 else
555 {
556 wxMemoryInputStream mis(data, len);
557 if ( !image.LoadFile(mis) )
558 wxLogError(wxT("Can't load BMP image from stream"));
559 else
560 my_horse_bmp2 = new wxBitmap( image );
561 }
562
563 free(data);
01111366
RR
564}
565
1d5b7a0b 566MyCanvas::~MyCanvas()
01111366 567{
60a41aee
RR
568 delete my_horse_pnm;
569 delete my_horse_png;
570 delete my_horse_jpeg;
571 delete my_horse_gif;
572 delete my_horse_bmp;
51b07644 573 delete my_horse_bmp2;
60a41aee
RR
574 delete my_horse_pcx;
575 delete my_horse_tiff;
9d8bdf2d 576 delete my_horse_xpm;
bf504f98
VS
577 delete my_horse_ico32;
578 delete my_horse_ico16;
579 delete my_horse_ico;
2f6c54eb 580 delete my_horse_cur;
2b5f62a0 581 delete [] my_horse_ani;
dc1dbfc6 582 delete my_smile_xbm;
60a41aee
RR
583 delete my_square;
584 delete my_anti;
01111366
RR
585}
586
587void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
588{
be25e480
RR
589 wxPaintDC dc( this );
590 PrepareDC( dc );
a91b47e8 591
51b07644 592 dc.DrawText( _T("Loaded image"), 30, 10 );
be25e480 593 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
f048e32f 594
51b07644 595 dc.DrawText( _T("Drawn directly"), 150, 10 );
a60b1f5d 596 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
cf3da716 597 dc.SetPen( *wxBLACK_PEN );
be25e480 598 dc.DrawRectangle( 150, 30, 100, 100 );
cf3da716
RR
599 dc.SetBrush( *wxWHITE_BRUSH );
600 dc.DrawRectangle( 170, 50, 60, 60 );
be25e480 601
408b4168 602 if (my_anti && my_anti->Ok())
be25e480
RR
603 dc.DrawBitmap( *my_anti, 280, 30 );
604
51b07644 605 dc.DrawText( _T("PNG handler"), 30, 135 );
be25e480 606 if (my_horse_png && my_horse_png->Ok())
408b4168 607 {
be25e480
RR
608 dc.DrawBitmap( *my_horse_png, 30, 150 );
609 wxRect rect(0,0,100,100);
610 wxBitmap sub( my_horse_png->GetSubBitmap(rect) );
51b07644 611 dc.DrawText( _T("GetSubBitmap()"), 280, 190 );
be25e480
RR
612 dc.DrawBitmap( sub, 280, 210 );
613 }
614
51b07644 615 dc.DrawText( _T("JPEG handler"), 30, 365 );
408b4168 616 if (my_horse_jpeg && my_horse_jpeg->Ok())
be25e480
RR
617 dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
618
51b07644 619 dc.DrawText( _T("GIF handler"), 30, 595 );
408b4168 620 if (my_horse_gif && my_horse_gif->Ok())
be25e480
RR
621 dc.DrawBitmap( *my_horse_gif, 30, 610 );
622
51b07644 623 dc.DrawText( _T("PCX handler"), 30, 825 );
408b4168 624 if (my_horse_pcx && my_horse_pcx->Ok())
be25e480
RR
625 dc.DrawBitmap( *my_horse_pcx, 30, 840 );
626
51b07644 627 dc.DrawText( _T("BMP handler"), 30, 1055 );
408b4168 628 if (my_horse_bmp && my_horse_bmp->Ok())
be25e480
RR
629 dc.DrawBitmap( *my_horse_bmp, 30, 1070 );
630
51b07644
VZ
631 dc.DrawText( _T("BMP read from memory"), 280, 1055 );
632 if (my_horse_bmp2 && my_horse_bmp2->Ok())
633 dc.DrawBitmap( *my_horse_bmp2, 280, 1070 );
634
635 dc.DrawText( _T("PNM handler"), 30, 1285 );
408b4168 636 if (my_horse_pnm && my_horse_pnm->Ok())
be25e480 637 dc.DrawBitmap( *my_horse_pnm, 30, 1300 );
408b4168 638
51b07644 639 dc.DrawText( _T("TIFF handler"), 30, 1515 );
408b4168 640 if (my_horse_tiff && my_horse_tiff->Ok())
4b05d973 641 dc.DrawBitmap( *my_horse_tiff, 30, 1530 );
be25e480 642
51b07644 643 dc.DrawText( _T("XPM handler"), 30, 1745 );
9d8bdf2d
VS
644 if (my_horse_xpm && my_horse_xpm->Ok())
645 dc.DrawBitmap( *my_horse_xpm, 30, 1760 );
646
bf504f98 647
408b4168 648 if (my_smile_xbm && my_smile_xbm->Ok())
be25e480 649 {
51b07644
VZ
650 dc.DrawText( _T("XBM bitmap"), 30, 1975 );
651 dc.DrawText( _T("(green on red)"), 30, 1990 );
652 dc.SetTextForeground( _T("GREEN") );
653 dc.SetTextBackground( _T("RED") );
9d8bdf2d 654 dc.DrawBitmap( *my_smile_xbm, 30, 2010 );
408b4168 655
a60b1f5d 656 dc.SetTextForeground( wxT("BLACK") );
51b07644
VZ
657 dc.DrawText( _T("After wxImage conversion"), 150, 1975 );
658 dc.DrawText( _T("(red on white)"), 150, 1990 );
a60b1f5d 659 dc.SetTextForeground( wxT("RED") );
a8d4e3ac 660 wxImage i = my_smile_xbm->ConvertToImage();
176fe5af
GRG
661 i.SetMaskColour( 255, 255, 255 );
662 i.Replace( 0, 0, 0,
be25e480
RR
663 wxRED_PEN->GetColour().Red(),
664 wxRED_PEN->GetColour().Green(),
665 wxRED_PEN->GetColour().Blue() );
368d59f0 666 dc.DrawBitmap( wxBitmap(i), 150, 2010, TRUE );
a60b1f5d 667 dc.SetTextForeground( wxT("BLACK") );
be25e480 668 }
176fe5af 669
408b4168 670
41fbc841 671 wxBitmap mono( 60,50,1 );
82ea63e6
RR
672 wxMemoryDC memdc;
673 memdc.SelectObject( mono );
bfef20bf 674 memdc.SetPen( *wxBLACK_PEN );
82ea63e6 675 memdc.SetBrush( *wxWHITE_BRUSH );
41fbc841
RR
676 memdc.DrawRectangle( 0,0,60,50 );
677 memdc.SetTextForeground( *wxBLACK );
51b07644 678 memdc.DrawText( _T("Hi!"), 5, 5 );
41fbc841
RR
679 memdc.SetBrush( *wxBLACK_BRUSH );
680 memdc.DrawRectangle( 33,5,20,20 );
681 memdc.SetPen( *wxRED_PEN );
682 memdc.DrawLine( 5, 42, 50, 42 );
82ea63e6 683 memdc.SelectObject( wxNullBitmap );
176fe5af 684
408b4168 685 if (mono.Ok())
82ea63e6 686 {
51b07644
VZ
687 dc.DrawText( _T("Mono bitmap"), 30, 2095 );
688 dc.DrawText( _T("(red on green)"), 30, 2110 );
a60b1f5d
MB
689 dc.SetTextForeground( wxT("RED") );
690 dc.SetTextBackground( wxT("GREEN") );
9d8bdf2d 691 dc.DrawBitmap( mono, 30, 2130 );
408b4168 692
a60b1f5d 693 dc.SetTextForeground( wxT("BLACK") );
51b07644
VZ
694 dc.DrawText( _T("After wxImage conversion"), 150, 2095 );
695 dc.DrawText( _T("(red on white)"), 150, 2110 );
a60b1f5d 696 dc.SetTextForeground( wxT("RED") );
a8d4e3ac 697 wxImage i = mono.ConvertToImage();
408b4168
VZ
698 i.SetMaskColour( 255,255,255 );
699 i.Replace( 0,0,0,
82ea63e6
RR
700 wxRED_PEN->GetColour().Red(),
701 wxRED_PEN->GetColour().Green(),
702 wxRED_PEN->GetColour().Blue() );
368d59f0 703 dc.DrawBitmap( wxBitmap(i), 150, 2130, TRUE );
a60b1f5d 704 dc.SetTextForeground( wxT("BLACK") );
bea56879
VZ
705 }
706
51b07644 707 dc.DrawText(_T("XPM bitmap"), 30, 2230);
bea56879
VZ
708 if ( m_bmpSmileXpm.Ok() )
709 {
9d8bdf2d 710 dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, TRUE);
bea56879
VZ
711 }
712
51b07644 713 dc.DrawText(_T("XPM icon"), 150, 2230);
bea56879
VZ
714 if ( m_iconSmileXpm.Ok() )
715 {
9d8bdf2d 716 dc.DrawIcon(m_iconSmileXpm, 150, 2250);
82ea63e6 717 }
bf504f98 718
51b07644 719 dc.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
bf504f98
VS
720 if (my_horse_ico32 && my_horse_ico32->Ok())
721 dc.DrawBitmap( *my_horse_ico32, 30, 2330, TRUE );
722
51b07644 723 dc.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
bf504f98
VS
724 if (my_horse_ico16 && my_horse_ico16->Ok())
725 dc.DrawBitmap( *my_horse_ico16, 230, 2330, TRUE );
51b07644
VZ
726
727 dc.DrawText( _T("ICO handler (best image)"), 430, 2290 );
bf504f98
VS
728 if (my_horse_ico && my_horse_ico->Ok())
729 dc.DrawBitmap( *my_horse_ico, 430, 2330, TRUE );
730
51b07644 731 dc.DrawText( _T("CUR handler"), 30, 2390 );
bf504f98 732 if (my_horse_cur && my_horse_cur->Ok())
51b07644 733 {
bf504f98
VS
734 dc.DrawBitmap( *my_horse_cur, 30, 2420, TRUE );
735 dc.SetPen (*wxRED_PEN);
736 dc.DrawLine (xH-10,yH,xH+10,yH);
737 dc.DrawLine (xH,yH-10,xH,yH+10);
51b07644 738 }
2b5f62a0
VZ
739 dc.DrawText( _T("ANI handler"), 230, 2390 );
740 int i ;
741 for (i=0; i < m_ani_images; i ++)
742 if (my_horse_ani[i].Ok())
743 {
744 dc.DrawBitmap( my_horse_ani[i], 230 + i * 2 * my_horse_ani[i].GetWidth() , 2420, TRUE );
745 }
9390a202 746}
1d5b7a0b 747
9390a202
RR
748void MyCanvas::CreateAntiAliasedBitmap()
749{
750 wxBitmap bitmap( 300, 300 );
a91b47e8 751
9390a202 752 wxMemoryDC dc;
a91b47e8 753
9390a202 754 dc.SelectObject( bitmap );
a91b47e8 755
9390a202 756 dc.Clear();
f048e32f 757
6e47faf1 758 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
a60b1f5d 759 dc.SetTextForeground( wxT("RED") );
51b07644
VZ
760 dc.DrawText( _T("This is anti-aliased Text."), 20, 20 );
761 dc.DrawText( _T("And a Rectangle."), 20, 60 );
f048e32f 762
9390a202 763 dc.SetBrush( *wxRED_BRUSH );
91b8de8d 764 dc.SetPen( *wxTRANSPARENT_PEN );
9390a202 765 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
f048e32f 766
a8d4e3ac 767 wxImage original= bitmap.ConvertToImage();
9390a202 768 wxImage anti( 150, 150 );
a91b47e8 769
9390a202 770 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
f048e32f 771
9390a202
RR
772 for (int y = 1; y < 149; y++)
773 for (int x = 1; x < 149; x++)
774 {
775 int red = original.GetRed( x*2, y*2 ) +
f048e32f
VZ
776 original.GetRed( x*2-1, y*2 ) +
777 original.GetRed( x*2, y*2+1 ) +
9390a202 778 original.GetRed( x*2+1, y*2+1 );
f048e32f
VZ
779 red = red/4;
780
9390a202 781 int green = original.GetGreen( x*2, y*2 ) +
f048e32f
VZ
782 original.GetGreen( x*2-1, y*2 ) +
783 original.GetGreen( x*2, y*2+1 ) +
9390a202 784 original.GetGreen( x*2+1, y*2+1 );
f048e32f
VZ
785 green = green/4;
786
9390a202 787 int blue = original.GetBlue( x*2, y*2 ) +
f048e32f
VZ
788 original.GetBlue( x*2-1, y*2 ) +
789 original.GetBlue( x*2, y*2+1 ) +
9390a202 790 original.GetBlue( x*2+1, y*2+1 );
f048e32f 791 blue = blue/4;
9390a202
RR
792 anti.SetRGB( x, y, red, green, blue );
793 }
368d59f0 794 my_anti = new wxBitmap(anti);
01111366
RR
795}
796
797// MyFrame
798
b3f04dc2
VZ
799enum
800{
801 ID_QUIT = 108,
802 ID_ABOUT,
803 ID_NEW,
804 ID_SHOWRAW
805};
01111366
RR
806
807IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
808
809BEGIN_EVENT_TABLE(MyFrame,wxFrame)
810 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
811 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
ab0f0386 812 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
b3f04dc2
VZ
813#ifdef wxHAVE_RAW_BITMAP
814 EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap)
815#endif
da9df1f5
VZ
816
817#if wxUSE_CLIPBOARD
818 EVT_MENU(wxID_COPY, MyFrame::OnCopy)
819 EVT_MENU(wxID_PASTE, MyFrame::OnPaste)
820#endif // wxUSE_CLIPBOARD
01111366
RR
821END_EVENT_TABLE()
822
1d5b7a0b 823MyFrame::MyFrame()
51b07644 824 : wxFrame( (wxFrame *)NULL, -1, _T("wxImage sample"),
1d5b7a0b 825 wxPoint(20,20), wxSize(470,360) )
01111366 826{
da9df1f5
VZ
827 wxMenuBar *menu_bar = new wxMenuBar();
828
829 wxMenu *menuImage = new wxMenu;
830 menuImage->Append( ID_NEW, _T("&Show any image...\tCtrl-O"));
b3f04dc2 831#ifdef wxHAVE_RAW_BITMAP
da9df1f5 832 menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R"));
b3f04dc2 833#endif
da9df1f5
VZ
834 menuImage->AppendSeparator();
835 menuImage->Append( ID_ABOUT, _T("&About..."));
836 menuImage->AppendSeparator();
837 menuImage->Append( ID_QUIT, _T("E&xit\tCtrl-Q"));
838 menu_bar->Append(menuImage, _T("&Image"));
839
840#if wxUSE_CLIPBOARD
841 wxMenu *menuClipboard = new wxMenu;
842 menuClipboard->Append(wxID_COPY, _T("&Copy test image\tCtrl-C"));
843 menuClipboard->Append(wxID_PASTE, _T("&Paste image\tCtrl-V"));
844 menu_bar->Append(menuClipboard, _T("&Clipboard"));
845#endif // wxUSE_CLIPBOARD
3d05544e 846
01111366 847 SetMenuBar( menu_bar );
1d5b7a0b 848
917e533b
RR
849 CreateStatusBar(2);
850 int widths[] = { -1, 100 };
851 SetStatusWidths( 2, widths );
1d5b7a0b 852
fb1585ae 853 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
cbd4be25 854
bf504f98
VS
855 // 500 width * 2500 height
856 m_canvas->SetScrollbars( 10, 10, 50, 250 );
01111366
RR
857}
858
859void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
860{
861 Close( TRUE );
862}
863
864void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
865{
51b07644
VZ
866 (void)wxMessageBox( _T("wxImage demo\n")
867 _T("Robert Roebling (c) 1998,2000"),
868 _T("About wxImage Demo"), wxICON_INFORMATION | wxOK );
fb1585ae
RR
869}
870
ab0f0386
VZ
871void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
872{
f6bcfd97
BP
873 wxString filename = wxFileSelector(_T("Select image file"));
874 if ( !filename )
875 return;
876
877 wxImage image;
878 if ( !image.LoadFile(filename) )
879 {
880 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
881
882 return;
883 }
884
368d59f0 885 (new MyImageFrame(this, wxBitmap(image)))->Show();
ab0f0386
VZ
886}
887
b3f04dc2
VZ
888#ifdef wxHAVE_RAW_BITMAP
889
890void MyFrame::OnTestRawBitmap( wxCommandEvent &event )
891{
892 (new MyRawBitmapFrame(this))->Show();
893}
894
895#endif // wxHAVE_RAW_BITMAP
896
da9df1f5
VZ
897#if wxUSE_CLIPBOARD
898
899void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
900{
901 wxBitmapDataObject *dobjBmp = new wxBitmapDataObject;
902 dobjBmp->SetBitmap(*m_canvas->my_horse_png);
903
904 if ( !wxTheClipboard->SetData(dobjBmp) )
905 {
906 wxLogError(_T("Failed to copy bitmap to clipboard"));
907 }
908}
909
910void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
911{
912 wxBitmapDataObject dobjBmp;
913 if ( !wxTheClipboard->GetData(dobjBmp) )
914 {
915 wxLogMessage(_T("No bitmap data in the clipboard"));
916 }
917 else
918 {
919 (new MyImageFrame(this, dobjBmp.GetBitmap()))->Show();
920 }
921}
922
923#endif // wxUSE_CLIPBOARD
924
01111366
RR
925//-----------------------------------------------------------------------------
926// MyApp
927//-----------------------------------------------------------------------------
928
1d5b7a0b 929bool MyApp::OnInit()
01111366 930{
e9c4b1a2
JS
931#if wxUSE_LIBPNG
932 wxImage::AddHandler( new wxPNGHandler );
933#endif
934
935#if wxUSE_LIBJPEG
936 wxImage::AddHandler( new wxJPEGHandler );
937#endif
938
60a41aee
RR
939#if wxUSE_LIBTIFF
940 wxImage::AddHandler( new wxTIFFHandler );
941#endif
942
6e47faf1 943#if wxUSE_GIF
e1929140 944 wxImage::AddHandler( new wxGIFHandler );
6e47faf1
JS
945#endif
946
947#if wxUSE_PCX
cbd4be25 948 wxImage::AddHandler( new wxPCXHandler );
6e47faf1
JS
949#endif
950
951#if wxUSE_PNM
6319afe3 952 wxImage::AddHandler( new wxPNMHandler );
6e47faf1 953#endif
e1929140 954
a8d4e3ac
VS
955#if wxUSE_XPM
956 wxImage::AddHandler( new wxXPMHandler );
957#endif
958
bf504f98
VS
959#if wxUSE_ICO_CUR
960 wxImage::AddHandler( new wxICOHandler );
961 wxImage::AddHandler( new wxCURHandler );
2b5f62a0 962 wxImage::AddHandler( new wxANIHandler );
bf504f98
VS
963#endif
964
01111366
RR
965 wxFrame *frame = new MyFrame();
966 frame->Show( TRUE );
1d5b7a0b 967
01111366
RR
968 return TRUE;
969}
970