]> git.saurik.com Git - wxWidgets.git/blame - samples/image/image.cpp
new template-based more flexible and hopefully more efficient raw bitmap implementation
[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
be05eab6 130#if wxUSE_CLIPBOARD && !defined(__WXMOTIF_)
da9df1f5
VZ
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
10b41b53
VZ
270 data.UseAlpha();
271
b3f04dc2
VZ
272 wxRawBitmapIterator p(data);
273
274 p.Offset(BORDER, BORDER);
275
276 for ( int y = 0; y < REAL_SIZE; ++y )
277 {
278 wxRawBitmapIterator rowStart = p;
279
280 int r = y < REAL_SIZE/3 ? 255 : 0,
281 g = (REAL_SIZE/3 <= y) && (y < 2*(REAL_SIZE/3)) ? 255 : 0,
282 b = 2*(REAL_SIZE/3) <= y ? 255 : 0;
283
284 for ( int x = 0; x < REAL_SIZE; ++x )
285 {
286 p.Red() = r;
287 p.Green() = g;
288 p.Blue() = b;
12338247 289 p.Alpha() = (x*255.)/REAL_SIZE;
b3f04dc2
VZ
290
291 ++p; // same as p.OffsetX(1)
292 }
293
294 p = rowStart;
295 p.OffsetY(1);
296 }
297 }
298
299 void OnPaint(wxPaintEvent& WXUNUSED(event))
300 {
301 wxPaintDC dc( this );
302 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER);
303 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE/2 - BORDER);
304 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE - 2*BORDER);
305 dc.DrawBitmap( m_bitmap, 0, 0, TRUE /* use mask */ );
306 }
307
308private:
309 wxBitmap m_bitmap;
310
311 DECLARE_EVENT_TABLE()
312};
313
314#endif // wxHAVE_RAW_BITMAP
315
01111366
RR
316// MyApp
317
318class MyApp: public wxApp
319{
1d5b7a0b
VZ
320public:
321 virtual bool OnInit();
01111366
RR
322};
323
324// main program
325
326IMPLEMENT_APP(MyApp)
327
328// MyCanvas
329
330IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
331
ab0f0386 332BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
0c543b7a
VZ
333 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground)
334 EVT_PAINT(MyImageFrame::OnPaint)
335 EVT_LEFT_DCLICK(MyImageFrame::OnSave)
ab0f0386
VZ
336END_EVENT_TABLE()
337
b3f04dc2
VZ
338#ifdef wxHAVE_RAW_BITMAP
339
340BEGIN_EVENT_TABLE(MyRawBitmapFrame, wxFrame)
341 EVT_PAINT(MyRawBitmapFrame::OnPaint)
342END_EVENT_TABLE()
343
344#endif // wxHAVE_RAW_BITMAP
345
1d5b7a0b
VZ
346BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
347 EVT_PAINT(MyCanvas::OnPaint)
01111366
RR
348END_EVENT_TABLE()
349
acbd13a3 350MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
1d5b7a0b 351 const wxPoint &pos, const wxSize &size )
81278df2
VZ
352 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
353#if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
354 , m_bmpSmileXpm((const char **) smile_xpm)
355 , m_iconSmileXpm((const char **) smile_xpm)
356#endif
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 488
81278df2 489#if !defined(__WINDOWS__) || wxUSE_XPM_IN_MSW
bfef20bf
GRG
490 // demonstrates XPM automatically using the mask when saving
491 if ( m_bmpSmileXpm.Ok() )
51b07644 492 m_bmpSmileXpm.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM);
81278df2 493#endif
bf504f98
VS
494
495#if wxUSE_ICO_CUR
496 image.Destroy();
497
51b07644 498 if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 0 ) )
bf504f98
VS
499 wxLogError(wxT("Can't load first ICO image"));
500 else
501 my_horse_ico32 = new wxBitmap( image );
51b07644 502
bf504f98
VS
503 image.Destroy();
504
51b07644 505 if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 1 ) )
bf504f98
VS
506 wxLogError(wxT("Can't load second ICO image"));
507 else
508 my_horse_ico16 = new wxBitmap( image );
509
510 image.Destroy();
511
51b07644 512 if ( !image.LoadFile( dir + _T("horse.ico") ) )
bf504f98
VS
513 wxLogError(wxT("Can't load best ICO image"));
514 else
515 my_horse_ico = new wxBitmap( image );
516
517 image.Destroy();
518
51b07644 519 if ( !image.LoadFile( dir + _T("horse.cur"), wxBITMAP_TYPE_CUR ) )
bf504f98
VS
520 wxLogError(wxT("Can't load best ICO image"));
521 else
51b07644 522 {
bf504f98 523 my_horse_cur = new wxBitmap( image );
fd94e8aa
VS
524 xH = 30 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) ;
525 yH = 2420 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ;
51b07644 526 }
2b5f62a0 527
08811762 528 m_ani_images = wxImage::GetImageCount ( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI );
2b5f62a0
VZ
529 if (m_ani_images==0)
530 wxLogError(wxT("No ANI-format images found"));
531 else
532 my_horse_ani = new wxBitmap [m_ani_images];
533 int i ;
534 for (i=0; i < m_ani_images; i++)
535 {
536 image.Destroy();
08811762 537 if (!image.LoadFile( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI, i ))
2b5f62a0
VZ
538 {
539 wxString tmp = wxT("Can't load image number ");
540 tmp << i ;
541 wxLogError(tmp);
542 }
543 else
544 my_horse_ani [i] = wxBitmap( image );
545 }
da9df1f5 546#endif // wxUSE_ICO_CUR
bf504f98 547
51b07644
VZ
548 image.Destroy();
549
550 // test image loading from stream
551 wxFile file(dir + _T("horse.bmp"));
30ad0a14 552 if ( file.IsOpened() )
51b07644 553 {
30ad0a14
VZ
554 off_t len = file.Length();
555 void *data = malloc(len);
556 if ( file.Read(data, len) != len )
557 wxLogError(_T("Reading bitmap file failed"));
51b07644 558 else
30ad0a14
VZ
559 {
560 wxMemoryInputStream mis(data, len);
561 if ( !image.LoadFile(mis) )
562 wxLogError(wxT("Can't load BMP image from stream"));
563 else
564 my_horse_bmp2 = new wxBitmap( image );
565 }
51b07644 566
30ad0a14
VZ
567 free(data);
568 }
01111366
RR
569}
570
1d5b7a0b 571MyCanvas::~MyCanvas()
01111366 572{
60a41aee
RR
573 delete my_horse_pnm;
574 delete my_horse_png;
575 delete my_horse_jpeg;
576 delete my_horse_gif;
577 delete my_horse_bmp;
51b07644 578 delete my_horse_bmp2;
60a41aee
RR
579 delete my_horse_pcx;
580 delete my_horse_tiff;
9d8bdf2d 581 delete my_horse_xpm;
bf504f98
VS
582 delete my_horse_ico32;
583 delete my_horse_ico16;
584 delete my_horse_ico;
2f6c54eb 585 delete my_horse_cur;
2b5f62a0 586 delete [] my_horse_ani;
dc1dbfc6 587 delete my_smile_xbm;
60a41aee
RR
588 delete my_square;
589 delete my_anti;
01111366
RR
590}
591
592void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
593{
be25e480
RR
594 wxPaintDC dc( this );
595 PrepareDC( dc );
a91b47e8 596
51b07644 597 dc.DrawText( _T("Loaded image"), 30, 10 );
be25e480 598 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
f048e32f 599
51b07644 600 dc.DrawText( _T("Drawn directly"), 150, 10 );
a60b1f5d 601 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
cf3da716 602 dc.SetPen( *wxBLACK_PEN );
be25e480 603 dc.DrawRectangle( 150, 30, 100, 100 );
cf3da716
RR
604 dc.SetBrush( *wxWHITE_BRUSH );
605 dc.DrawRectangle( 170, 50, 60, 60 );
be25e480 606
408b4168 607 if (my_anti && my_anti->Ok())
be25e480
RR
608 dc.DrawBitmap( *my_anti, 280, 30 );
609
51b07644 610 dc.DrawText( _T("PNG handler"), 30, 135 );
be25e480 611 if (my_horse_png && my_horse_png->Ok())
408b4168 612 {
be25e480
RR
613 dc.DrawBitmap( *my_horse_png, 30, 150 );
614 wxRect rect(0,0,100,100);
615 wxBitmap sub( my_horse_png->GetSubBitmap(rect) );
51b07644 616 dc.DrawText( _T("GetSubBitmap()"), 280, 190 );
be25e480
RR
617 dc.DrawBitmap( sub, 280, 210 );
618 }
619
51b07644 620 dc.DrawText( _T("JPEG handler"), 30, 365 );
408b4168 621 if (my_horse_jpeg && my_horse_jpeg->Ok())
be25e480
RR
622 dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
623
51b07644 624 dc.DrawText( _T("GIF handler"), 30, 595 );
408b4168 625 if (my_horse_gif && my_horse_gif->Ok())
be25e480
RR
626 dc.DrawBitmap( *my_horse_gif, 30, 610 );
627
51b07644 628 dc.DrawText( _T("PCX handler"), 30, 825 );
408b4168 629 if (my_horse_pcx && my_horse_pcx->Ok())
be25e480
RR
630 dc.DrawBitmap( *my_horse_pcx, 30, 840 );
631
51b07644 632 dc.DrawText( _T("BMP handler"), 30, 1055 );
408b4168 633 if (my_horse_bmp && my_horse_bmp->Ok())
be25e480
RR
634 dc.DrawBitmap( *my_horse_bmp, 30, 1070 );
635
51b07644
VZ
636 dc.DrawText( _T("BMP read from memory"), 280, 1055 );
637 if (my_horse_bmp2 && my_horse_bmp2->Ok())
638 dc.DrawBitmap( *my_horse_bmp2, 280, 1070 );
639
640 dc.DrawText( _T("PNM handler"), 30, 1285 );
408b4168 641 if (my_horse_pnm && my_horse_pnm->Ok())
be25e480 642 dc.DrawBitmap( *my_horse_pnm, 30, 1300 );
408b4168 643
51b07644 644 dc.DrawText( _T("TIFF handler"), 30, 1515 );
408b4168 645 if (my_horse_tiff && my_horse_tiff->Ok())
4b05d973 646 dc.DrawBitmap( *my_horse_tiff, 30, 1530 );
be25e480 647
51b07644 648 dc.DrawText( _T("XPM handler"), 30, 1745 );
9d8bdf2d
VS
649 if (my_horse_xpm && my_horse_xpm->Ok())
650 dc.DrawBitmap( *my_horse_xpm, 30, 1760 );
651
bf504f98 652
408b4168 653 if (my_smile_xbm && my_smile_xbm->Ok())
be25e480 654 {
51b07644
VZ
655 dc.DrawText( _T("XBM bitmap"), 30, 1975 );
656 dc.DrawText( _T("(green on red)"), 30, 1990 );
657 dc.SetTextForeground( _T("GREEN") );
658 dc.SetTextBackground( _T("RED") );
9d8bdf2d 659 dc.DrawBitmap( *my_smile_xbm, 30, 2010 );
408b4168 660
a60b1f5d 661 dc.SetTextForeground( wxT("BLACK") );
51b07644
VZ
662 dc.DrawText( _T("After wxImage conversion"), 150, 1975 );
663 dc.DrawText( _T("(red on white)"), 150, 1990 );
a60b1f5d 664 dc.SetTextForeground( wxT("RED") );
a8d4e3ac 665 wxImage i = my_smile_xbm->ConvertToImage();
176fe5af
GRG
666 i.SetMaskColour( 255, 255, 255 );
667 i.Replace( 0, 0, 0,
be25e480
RR
668 wxRED_PEN->GetColour().Red(),
669 wxRED_PEN->GetColour().Green(),
670 wxRED_PEN->GetColour().Blue() );
368d59f0 671 dc.DrawBitmap( wxBitmap(i), 150, 2010, TRUE );
a60b1f5d 672 dc.SetTextForeground( wxT("BLACK") );
be25e480 673 }
176fe5af 674
408b4168 675
41fbc841 676 wxBitmap mono( 60,50,1 );
82ea63e6
RR
677 wxMemoryDC memdc;
678 memdc.SelectObject( mono );
bfef20bf 679 memdc.SetPen( *wxBLACK_PEN );
82ea63e6 680 memdc.SetBrush( *wxWHITE_BRUSH );
41fbc841
RR
681 memdc.DrawRectangle( 0,0,60,50 );
682 memdc.SetTextForeground( *wxBLACK );
51b07644 683 memdc.DrawText( _T("Hi!"), 5, 5 );
41fbc841
RR
684 memdc.SetBrush( *wxBLACK_BRUSH );
685 memdc.DrawRectangle( 33,5,20,20 );
686 memdc.SetPen( *wxRED_PEN );
687 memdc.DrawLine( 5, 42, 50, 42 );
82ea63e6 688 memdc.SelectObject( wxNullBitmap );
176fe5af 689
408b4168 690 if (mono.Ok())
82ea63e6 691 {
51b07644
VZ
692 dc.DrawText( _T("Mono bitmap"), 30, 2095 );
693 dc.DrawText( _T("(red on green)"), 30, 2110 );
a60b1f5d
MB
694 dc.SetTextForeground( wxT("RED") );
695 dc.SetTextBackground( wxT("GREEN") );
9d8bdf2d 696 dc.DrawBitmap( mono, 30, 2130 );
408b4168 697
a60b1f5d 698 dc.SetTextForeground( wxT("BLACK") );
51b07644
VZ
699 dc.DrawText( _T("After wxImage conversion"), 150, 2095 );
700 dc.DrawText( _T("(red on white)"), 150, 2110 );
a60b1f5d 701 dc.SetTextForeground( wxT("RED") );
a8d4e3ac 702 wxImage i = mono.ConvertToImage();
408b4168
VZ
703 i.SetMaskColour( 255,255,255 );
704 i.Replace( 0,0,0,
82ea63e6
RR
705 wxRED_PEN->GetColour().Red(),
706 wxRED_PEN->GetColour().Green(),
707 wxRED_PEN->GetColour().Blue() );
368d59f0 708 dc.DrawBitmap( wxBitmap(i), 150, 2130, TRUE );
a60b1f5d 709 dc.SetTextForeground( wxT("BLACK") );
bea56879
VZ
710 }
711
51b07644 712 dc.DrawText(_T("XPM bitmap"), 30, 2230);
bea56879
VZ
713 if ( m_bmpSmileXpm.Ok() )
714 {
9d8bdf2d 715 dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, TRUE);
bea56879
VZ
716 }
717
51b07644 718 dc.DrawText(_T("XPM icon"), 150, 2230);
bea56879
VZ
719 if ( m_iconSmileXpm.Ok() )
720 {
9d8bdf2d 721 dc.DrawIcon(m_iconSmileXpm, 150, 2250);
82ea63e6 722 }
bf504f98 723
51b07644 724 dc.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
bf504f98
VS
725 if (my_horse_ico32 && my_horse_ico32->Ok())
726 dc.DrawBitmap( *my_horse_ico32, 30, 2330, TRUE );
727
51b07644 728 dc.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
bf504f98
VS
729 if (my_horse_ico16 && my_horse_ico16->Ok())
730 dc.DrawBitmap( *my_horse_ico16, 230, 2330, TRUE );
51b07644
VZ
731
732 dc.DrawText( _T("ICO handler (best image)"), 430, 2290 );
bf504f98
VS
733 if (my_horse_ico && my_horse_ico->Ok())
734 dc.DrawBitmap( *my_horse_ico, 430, 2330, TRUE );
735
51b07644 736 dc.DrawText( _T("CUR handler"), 30, 2390 );
bf504f98 737 if (my_horse_cur && my_horse_cur->Ok())
51b07644 738 {
bf504f98
VS
739 dc.DrawBitmap( *my_horse_cur, 30, 2420, TRUE );
740 dc.SetPen (*wxRED_PEN);
741 dc.DrawLine (xH-10,yH,xH+10,yH);
742 dc.DrawLine (xH,yH-10,xH,yH+10);
51b07644 743 }
2b5f62a0
VZ
744 dc.DrawText( _T("ANI handler"), 230, 2390 );
745 int i ;
746 for (i=0; i < m_ani_images; i ++)
747 if (my_horse_ani[i].Ok())
748 {
749 dc.DrawBitmap( my_horse_ani[i], 230 + i * 2 * my_horse_ani[i].GetWidth() , 2420, TRUE );
750 }
9390a202 751}
1d5b7a0b 752
9390a202
RR
753void MyCanvas::CreateAntiAliasedBitmap()
754{
755 wxBitmap bitmap( 300, 300 );
a91b47e8 756
9390a202 757 wxMemoryDC dc;
a91b47e8 758
9390a202 759 dc.SelectObject( bitmap );
a91b47e8 760
9390a202 761 dc.Clear();
f048e32f 762
6e47faf1 763 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
a60b1f5d 764 dc.SetTextForeground( wxT("RED") );
51b07644
VZ
765 dc.DrawText( _T("This is anti-aliased Text."), 20, 20 );
766 dc.DrawText( _T("And a Rectangle."), 20, 60 );
f048e32f 767
9390a202 768 dc.SetBrush( *wxRED_BRUSH );
91b8de8d 769 dc.SetPen( *wxTRANSPARENT_PEN );
9390a202 770 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
f048e32f 771
a8d4e3ac 772 wxImage original= bitmap.ConvertToImage();
9390a202 773 wxImage anti( 150, 150 );
a91b47e8 774
9390a202 775 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
f048e32f 776
9390a202
RR
777 for (int y = 1; y < 149; y++)
778 for (int x = 1; x < 149; x++)
779 {
780 int red = original.GetRed( x*2, y*2 ) +
f048e32f
VZ
781 original.GetRed( x*2-1, y*2 ) +
782 original.GetRed( x*2, y*2+1 ) +
9390a202 783 original.GetRed( x*2+1, y*2+1 );
f048e32f
VZ
784 red = red/4;
785
9390a202 786 int green = original.GetGreen( x*2, y*2 ) +
f048e32f
VZ
787 original.GetGreen( x*2-1, y*2 ) +
788 original.GetGreen( x*2, y*2+1 ) +
9390a202 789 original.GetGreen( x*2+1, y*2+1 );
f048e32f
VZ
790 green = green/4;
791
9390a202 792 int blue = original.GetBlue( x*2, y*2 ) +
f048e32f
VZ
793 original.GetBlue( x*2-1, y*2 ) +
794 original.GetBlue( x*2, y*2+1 ) +
9390a202 795 original.GetBlue( x*2+1, y*2+1 );
f048e32f 796 blue = blue/4;
9390a202
RR
797 anti.SetRGB( x, y, red, green, blue );
798 }
368d59f0 799 my_anti = new wxBitmap(anti);
01111366
RR
800}
801
802// MyFrame
803
b3f04dc2
VZ
804enum
805{
806 ID_QUIT = 108,
807 ID_ABOUT,
808 ID_NEW,
809 ID_SHOWRAW
810};
01111366
RR
811
812IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
813
814BEGIN_EVENT_TABLE(MyFrame,wxFrame)
815 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
816 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
ab0f0386 817 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
b3f04dc2
VZ
818#ifdef wxHAVE_RAW_BITMAP
819 EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap)
820#endif
da9df1f5 821
be05eab6 822#if wxUSE_CLIPBOARD && !defined(__WXMOTIF__)
da9df1f5
VZ
823 EVT_MENU(wxID_COPY, MyFrame::OnCopy)
824 EVT_MENU(wxID_PASTE, MyFrame::OnPaste)
825#endif // wxUSE_CLIPBOARD
01111366
RR
826END_EVENT_TABLE()
827
1d5b7a0b 828MyFrame::MyFrame()
51b07644 829 : wxFrame( (wxFrame *)NULL, -1, _T("wxImage sample"),
1d5b7a0b 830 wxPoint(20,20), wxSize(470,360) )
01111366 831{
da9df1f5
VZ
832 wxMenuBar *menu_bar = new wxMenuBar();
833
834 wxMenu *menuImage = new wxMenu;
835 menuImage->Append( ID_NEW, _T("&Show any image...\tCtrl-O"));
b3f04dc2 836#ifdef wxHAVE_RAW_BITMAP
da9df1f5 837 menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R"));
b3f04dc2 838#endif
da9df1f5
VZ
839 menuImage->AppendSeparator();
840 menuImage->Append( ID_ABOUT, _T("&About..."));
841 menuImage->AppendSeparator();
842 menuImage->Append( ID_QUIT, _T("E&xit\tCtrl-Q"));
843 menu_bar->Append(menuImage, _T("&Image"));
844
be05eab6 845#if wxUSE_CLIPBOARD && !defined(__WXMOTIF__)
da9df1f5
VZ
846 wxMenu *menuClipboard = new wxMenu;
847 menuClipboard->Append(wxID_COPY, _T("&Copy test image\tCtrl-C"));
848 menuClipboard->Append(wxID_PASTE, _T("&Paste image\tCtrl-V"));
849 menu_bar->Append(menuClipboard, _T("&Clipboard"));
850#endif // wxUSE_CLIPBOARD
3d05544e 851
01111366 852 SetMenuBar( menu_bar );
1d5b7a0b 853
917e533b
RR
854 CreateStatusBar(2);
855 int widths[] = { -1, 100 };
856 SetStatusWidths( 2, widths );
1d5b7a0b 857
fb1585ae 858 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
cbd4be25 859
bf504f98
VS
860 // 500 width * 2500 height
861 m_canvas->SetScrollbars( 10, 10, 50, 250 );
01111366
RR
862}
863
864void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
865{
866 Close( TRUE );
867}
868
869void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
870{
51b07644
VZ
871 (void)wxMessageBox( _T("wxImage demo\n")
872 _T("Robert Roebling (c) 1998,2000"),
873 _T("About wxImage Demo"), wxICON_INFORMATION | wxOK );
fb1585ae
RR
874}
875
ab0f0386
VZ
876void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
877{
f6bcfd97
BP
878 wxString filename = wxFileSelector(_T("Select image file"));
879 if ( !filename )
880 return;
881
882 wxImage image;
883 if ( !image.LoadFile(filename) )
884 {
885 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
886
887 return;
888 }
889
368d59f0 890 (new MyImageFrame(this, wxBitmap(image)))->Show();
ab0f0386
VZ
891}
892
b3f04dc2
VZ
893#ifdef wxHAVE_RAW_BITMAP
894
895void MyFrame::OnTestRawBitmap( wxCommandEvent &event )
896{
897 (new MyRawBitmapFrame(this))->Show();
898}
899
900#endif // wxHAVE_RAW_BITMAP
901
be05eab6 902#if wxUSE_CLIPBOARD && !defined(__WXMOTIF__)
da9df1f5
VZ
903
904void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
905{
906 wxBitmapDataObject *dobjBmp = new wxBitmapDataObject;
907 dobjBmp->SetBitmap(*m_canvas->my_horse_png);
908
909 if ( !wxTheClipboard->SetData(dobjBmp) )
910 {
911 wxLogError(_T("Failed to copy bitmap to clipboard"));
912 }
913}
914
915void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
916{
917 wxBitmapDataObject dobjBmp;
918 if ( !wxTheClipboard->GetData(dobjBmp) )
919 {
920 wxLogMessage(_T("No bitmap data in the clipboard"));
921 }
922 else
923 {
924 (new MyImageFrame(this, dobjBmp.GetBitmap()))->Show();
925 }
926}
927
928#endif // wxUSE_CLIPBOARD
929
01111366
RR
930//-----------------------------------------------------------------------------
931// MyApp
932//-----------------------------------------------------------------------------
933
1d5b7a0b 934bool MyApp::OnInit()
01111366 935{
e9c4b1a2
JS
936#if wxUSE_LIBPNG
937 wxImage::AddHandler( new wxPNGHandler );
938#endif
939
940#if wxUSE_LIBJPEG
941 wxImage::AddHandler( new wxJPEGHandler );
942#endif
943
60a41aee
RR
944#if wxUSE_LIBTIFF
945 wxImage::AddHandler( new wxTIFFHandler );
946#endif
947
6e47faf1 948#if wxUSE_GIF
e1929140 949 wxImage::AddHandler( new wxGIFHandler );
6e47faf1
JS
950#endif
951
952#if wxUSE_PCX
cbd4be25 953 wxImage::AddHandler( new wxPCXHandler );
6e47faf1
JS
954#endif
955
956#if wxUSE_PNM
6319afe3 957 wxImage::AddHandler( new wxPNMHandler );
6e47faf1 958#endif
e1929140 959
a8d4e3ac
VS
960#if wxUSE_XPM
961 wxImage::AddHandler( new wxXPMHandler );
962#endif
963
bf504f98
VS
964#if wxUSE_ICO_CUR
965 wxImage::AddHandler( new wxICOHandler );
966 wxImage::AddHandler( new wxCURHandler );
2b5f62a0 967 wxImage::AddHandler( new wxANIHandler );
bf504f98
VS
968#endif
969
01111366
RR
970 wxFrame *frame = new MyFrame();
971 frame->Show( TRUE );
1d5b7a0b 972
01111366
RR
973 return TRUE;
974}
975