]> git.saurik.com Git - wxWidgets.git/blame - samples/image/image.cpp
delete-->delete[]
[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++ )
925e9792 206 cmap[i] = (unsigned char)i;
1971d23c
VZ
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 }
925e9792 541 else
2b5f62a0
VZ
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 {
9386cb75
WS
552 wxFileOffset len = file.Length();
553 size_t dataSize = (size_t)len;
554 void *data = malloc(dataSize);
555 if ( file.Read(data, dataSize) != len )
30ad0a14 556 wxLogError(_T("Reading bitmap file failed"));
51b07644 557 else
30ad0a14 558 {
9386cb75 559 wxMemoryInputStream mis(data, dataSize);
30ad0a14
VZ
560 if ( !image.LoadFile(mis) )
561 wxLogError(wxT("Can't load BMP image from stream"));
562 else
563 my_horse_bmp2 = new wxBitmap( image );
564 }
51b07644 565
30ad0a14
VZ
566 free(data);
567 }
01111366
RR
568}
569
1d5b7a0b 570MyCanvas::~MyCanvas()
01111366 571{
60a41aee
RR
572 delete my_horse_pnm;
573 delete my_horse_png;
574 delete my_horse_jpeg;
575 delete my_horse_gif;
576 delete my_horse_bmp;
51b07644 577 delete my_horse_bmp2;
60a41aee
RR
578 delete my_horse_pcx;
579 delete my_horse_tiff;
9d8bdf2d 580 delete my_horse_xpm;
bf504f98
VS
581 delete my_horse_ico32;
582 delete my_horse_ico16;
583 delete my_horse_ico;
2f6c54eb 584 delete my_horse_cur;
2b5f62a0 585 delete [] my_horse_ani;
dc1dbfc6 586 delete my_smile_xbm;
60a41aee
RR
587 delete my_square;
588 delete my_anti;
01111366
RR
589}
590
591void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
592{
be25e480
RR
593 wxPaintDC dc( this );
594 PrepareDC( dc );
a91b47e8 595
51b07644 596 dc.DrawText( _T("Loaded image"), 30, 10 );
be25e480 597 if (my_square && my_square->Ok()) dc.DrawBitmap( *my_square, 30, 30 );
f048e32f 598
51b07644 599 dc.DrawText( _T("Drawn directly"), 150, 10 );
a60b1f5d 600 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
cf3da716 601 dc.SetPen( *wxBLACK_PEN );
be25e480 602 dc.DrawRectangle( 150, 30, 100, 100 );
cf3da716
RR
603 dc.SetBrush( *wxWHITE_BRUSH );
604 dc.DrawRectangle( 170, 50, 60, 60 );
be25e480 605
408b4168 606 if (my_anti && my_anti->Ok())
be25e480
RR
607 dc.DrawBitmap( *my_anti, 280, 30 );
608
51b07644 609 dc.DrawText( _T("PNG handler"), 30, 135 );
be25e480 610 if (my_horse_png && my_horse_png->Ok())
408b4168 611 {
be25e480
RR
612 dc.DrawBitmap( *my_horse_png, 30, 150 );
613 wxRect rect(0,0,100,100);
614 wxBitmap sub( my_horse_png->GetSubBitmap(rect) );
51b07644 615 dc.DrawText( _T("GetSubBitmap()"), 280, 190 );
be25e480
RR
616 dc.DrawBitmap( sub, 280, 210 );
617 }
618
51b07644 619 dc.DrawText( _T("JPEG handler"), 30, 365 );
408b4168 620 if (my_horse_jpeg && my_horse_jpeg->Ok())
be25e480
RR
621 dc.DrawBitmap( *my_horse_jpeg, 30, 380 );
622
51b07644 623 dc.DrawText( _T("GIF handler"), 30, 595 );
408b4168 624 if (my_horse_gif && my_horse_gif->Ok())
be25e480
RR
625 dc.DrawBitmap( *my_horse_gif, 30, 610 );
626
51b07644 627 dc.DrawText( _T("PCX handler"), 30, 825 );
408b4168 628 if (my_horse_pcx && my_horse_pcx->Ok())
be25e480
RR
629 dc.DrawBitmap( *my_horse_pcx, 30, 840 );
630
51b07644 631 dc.DrawText( _T("BMP handler"), 30, 1055 );
408b4168 632 if (my_horse_bmp && my_horse_bmp->Ok())
be25e480
RR
633 dc.DrawBitmap( *my_horse_bmp, 30, 1070 );
634
51b07644
VZ
635 dc.DrawText( _T("BMP read from memory"), 280, 1055 );
636 if (my_horse_bmp2 && my_horse_bmp2->Ok())
637 dc.DrawBitmap( *my_horse_bmp2, 280, 1070 );
638
639 dc.DrawText( _T("PNM handler"), 30, 1285 );
408b4168 640 if (my_horse_pnm && my_horse_pnm->Ok())
be25e480 641 dc.DrawBitmap( *my_horse_pnm, 30, 1300 );
408b4168 642
51b07644 643 dc.DrawText( _T("TIFF handler"), 30, 1515 );
408b4168 644 if (my_horse_tiff && my_horse_tiff->Ok())
4b05d973 645 dc.DrawBitmap( *my_horse_tiff, 30, 1530 );
be25e480 646
51b07644 647 dc.DrawText( _T("XPM handler"), 30, 1745 );
9d8bdf2d
VS
648 if (my_horse_xpm && my_horse_xpm->Ok())
649 dc.DrawBitmap( *my_horse_xpm, 30, 1760 );
650
bf504f98 651
408b4168 652 if (my_smile_xbm && my_smile_xbm->Ok())
be25e480 653 {
51b07644
VZ
654 dc.DrawText( _T("XBM bitmap"), 30, 1975 );
655 dc.DrawText( _T("(green on red)"), 30, 1990 );
656 dc.SetTextForeground( _T("GREEN") );
657 dc.SetTextBackground( _T("RED") );
9d8bdf2d 658 dc.DrawBitmap( *my_smile_xbm, 30, 2010 );
408b4168 659
a60b1f5d 660 dc.SetTextForeground( wxT("BLACK") );
51b07644
VZ
661 dc.DrawText( _T("After wxImage conversion"), 150, 1975 );
662 dc.DrawText( _T("(red on white)"), 150, 1990 );
a60b1f5d 663 dc.SetTextForeground( wxT("RED") );
a8d4e3ac 664 wxImage i = my_smile_xbm->ConvertToImage();
176fe5af
GRG
665 i.SetMaskColour( 255, 255, 255 );
666 i.Replace( 0, 0, 0,
be25e480
RR
667 wxRED_PEN->GetColour().Red(),
668 wxRED_PEN->GetColour().Green(),
669 wxRED_PEN->GetColour().Blue() );
47f797bd 670 dc.DrawBitmap( wxBitmap(i), 150, 2010, true );
a60b1f5d 671 dc.SetTextForeground( wxT("BLACK") );
be25e480 672 }
176fe5af 673
408b4168 674
41fbc841 675 wxBitmap mono( 60,50,1 );
82ea63e6
RR
676 wxMemoryDC memdc;
677 memdc.SelectObject( mono );
bfef20bf 678 memdc.SetPen( *wxBLACK_PEN );
82ea63e6 679 memdc.SetBrush( *wxWHITE_BRUSH );
41fbc841
RR
680 memdc.DrawRectangle( 0,0,60,50 );
681 memdc.SetTextForeground( *wxBLACK );
9fe7d430
RR
682#ifndef __WXGTK20__
683 // I cannot convince GTK2 to draw into mono bitmaps
51b07644 684 memdc.DrawText( _T("Hi!"), 5, 5 );
9fe7d430 685#endif
41fbc841
RR
686 memdc.SetBrush( *wxBLACK_BRUSH );
687 memdc.DrawRectangle( 33,5,20,20 );
688 memdc.SetPen( *wxRED_PEN );
689 memdc.DrawLine( 5, 42, 50, 42 );
82ea63e6 690 memdc.SelectObject( wxNullBitmap );
176fe5af 691
408b4168 692 if (mono.Ok())
82ea63e6 693 {
51b07644
VZ
694 dc.DrawText( _T("Mono bitmap"), 30, 2095 );
695 dc.DrawText( _T("(red on green)"), 30, 2110 );
a60b1f5d
MB
696 dc.SetTextForeground( wxT("RED") );
697 dc.SetTextBackground( wxT("GREEN") );
9d8bdf2d 698 dc.DrawBitmap( mono, 30, 2130 );
408b4168 699
a60b1f5d 700 dc.SetTextForeground( wxT("BLACK") );
51b07644
VZ
701 dc.DrawText( _T("After wxImage conversion"), 150, 2095 );
702 dc.DrawText( _T("(red on white)"), 150, 2110 );
a60b1f5d 703 dc.SetTextForeground( wxT("RED") );
a8d4e3ac 704 wxImage i = mono.ConvertToImage();
408b4168
VZ
705 i.SetMaskColour( 255,255,255 );
706 i.Replace( 0,0,0,
82ea63e6
RR
707 wxRED_PEN->GetColour().Red(),
708 wxRED_PEN->GetColour().Green(),
709 wxRED_PEN->GetColour().Blue() );
47f797bd 710 dc.DrawBitmap( wxBitmap(i), 150, 2130, true );
a60b1f5d 711 dc.SetTextForeground( wxT("BLACK") );
bea56879
VZ
712 }
713
f9ebac93
RR
714 // For testing transparency
715 dc.SetBrush( *wxRED_BRUSH );
716 dc.DrawRectangle( 20, 2220, 560, 68 );
925e9792 717
f9ebac93 718 dc.DrawText(_T("XPM bitmap"), 30, 2230 );
bea56879 719 if ( m_bmpSmileXpm.Ok() )
47f797bd 720 dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, true);
bea56879 721
f9ebac93 722 dc.DrawText(_T("XPM icon"), 110, 2230 );
bea56879 723 if ( m_iconSmileXpm.Ok() )
f9ebac93 724 dc.DrawIcon(m_iconSmileXpm, 110, 2250);
925e9792
WS
725
726 // testing icon -> bitmap conversion
f9ebac93 727 wxBitmap to_blit( m_iconSmileXpm );
3ef37e7f
JS
728 if (to_blit.Ok())
729 {
730 dc.DrawText( _T("SubBitmap"), 170, 2230 );
731 wxBitmap sub = to_blit.GetSubBitmap( wxRect(0,0,15,15) );
732 if (sub.Ok())
47f797bd 733 dc.DrawBitmap( sub, 170, 2250, true );
3ef37e7f
JS
734
735 dc.DrawText( _T("Enlarged"), 250, 2230 );
736 dc.SetUserScale( 1.5, 1.5 );
47f797bd 737 dc.DrawBitmap( to_blit, (int)(250/1.5), (int)(2250/1.5), true );
3ef37e7f 738 dc.SetUserScale( 2, 2 );
47f797bd 739 dc.DrawBitmap( to_blit, (int)(300/2), (int)(2250/2), true );
3ef37e7f 740 dc.SetUserScale( 1.0, 1.0 );
925e9792 741
3ef37e7f
JS
742 dc.DrawText( _T("Blit"), 400, 2230);
743 wxMemoryDC blit_dc;
744 blit_dc.SelectObject( to_blit );
47f797bd 745 dc.Blit( 400, 2250, to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
3ef37e7f 746 dc.SetUserScale( 1.5, 1.5 );
47f797bd 747 dc.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
3ef37e7f 748 dc.SetUserScale( 2, 2 );
47f797bd 749 dc.Blit( (int)(500/2), (int)(2250/2), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
3ef37e7f
JS
750 dc.SetUserScale( 1.0, 1.0 );
751 }
bf504f98 752
51b07644 753 dc.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
bf504f98 754 if (my_horse_ico32 && my_horse_ico32->Ok())
47f797bd 755 dc.DrawBitmap( *my_horse_ico32, 30, 2330, true );
bf504f98 756
51b07644 757 dc.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
bf504f98 758 if (my_horse_ico16 && my_horse_ico16->Ok())
47f797bd 759 dc.DrawBitmap( *my_horse_ico16, 230, 2330, true );
51b07644
VZ
760
761 dc.DrawText( _T("ICO handler (best image)"), 430, 2290 );
bf504f98 762 if (my_horse_ico && my_horse_ico->Ok())
47f797bd 763 dc.DrawBitmap( *my_horse_ico, 430, 2330, true );
bf504f98 764
51b07644 765 dc.DrawText( _T("CUR handler"), 30, 2390 );
bf504f98 766 if (my_horse_cur && my_horse_cur->Ok())
51b07644 767 {
47f797bd 768 dc.DrawBitmap( *my_horse_cur, 30, 2420, true );
bf504f98
VS
769 dc.SetPen (*wxRED_PEN);
770 dc.DrawLine (xH-10,yH,xH+10,yH);
771 dc.DrawLine (xH,yH-10,xH,yH+10);
51b07644 772 }
2b5f62a0
VZ
773 dc.DrawText( _T("ANI handler"), 230, 2390 );
774 int i ;
775 for (i=0; i < m_ani_images; i ++)
776 if (my_horse_ani[i].Ok())
777 {
47f797bd 778 dc.DrawBitmap( my_horse_ani[i], 230 + i * 2 * my_horse_ani[i].GetWidth() , 2420, true );
2b5f62a0 779 }
9390a202 780}
1d5b7a0b 781
9390a202
RR
782void MyCanvas::CreateAntiAliasedBitmap()
783{
784 wxBitmap bitmap( 300, 300 );
a91b47e8 785
9390a202 786 wxMemoryDC dc;
a91b47e8 787
9390a202 788 dc.SelectObject( bitmap );
a91b47e8 789
9390a202 790 dc.Clear();
f048e32f 791
6e47faf1 792 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
a60b1f5d 793 dc.SetTextForeground( wxT("RED") );
51b07644
VZ
794 dc.DrawText( _T("This is anti-aliased Text."), 20, 20 );
795 dc.DrawText( _T("And a Rectangle."), 20, 60 );
f048e32f 796
9390a202 797 dc.SetBrush( *wxRED_BRUSH );
91b8de8d 798 dc.SetPen( *wxTRANSPARENT_PEN );
9390a202 799 dc.DrawRoundedRectangle( 20, 100, 200, 180, 20 );
f048e32f 800
a8d4e3ac 801 wxImage original= bitmap.ConvertToImage();
9390a202 802 wxImage anti( 150, 150 );
a91b47e8 803
9390a202 804 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
f048e32f 805
9390a202
RR
806 for (int y = 1; y < 149; y++)
807 for (int x = 1; x < 149; x++)
808 {
809 int red = original.GetRed( x*2, y*2 ) +
f048e32f
VZ
810 original.GetRed( x*2-1, y*2 ) +
811 original.GetRed( x*2, y*2+1 ) +
9390a202 812 original.GetRed( x*2+1, y*2+1 );
f048e32f
VZ
813 red = red/4;
814
9390a202 815 int green = original.GetGreen( x*2, y*2 ) +
f048e32f
VZ
816 original.GetGreen( x*2-1, y*2 ) +
817 original.GetGreen( x*2, y*2+1 ) +
9390a202 818 original.GetGreen( x*2+1, y*2+1 );
f048e32f
VZ
819 green = green/4;
820
9390a202 821 int blue = original.GetBlue( x*2, y*2 ) +
f048e32f
VZ
822 original.GetBlue( x*2-1, y*2 ) +
823 original.GetBlue( x*2, y*2+1 ) +
9390a202 824 original.GetBlue( x*2+1, y*2+1 );
f048e32f 825 blue = blue/4;
925e9792 826 anti.SetRGB( x, y, (unsigned char)red, (unsigned char)green, (unsigned char)blue );
9390a202 827 }
368d59f0 828 my_anti = new wxBitmap(anti);
01111366
RR
829}
830
831// MyFrame
832
b3f04dc2
VZ
833enum
834{
835 ID_QUIT = 108,
836 ID_ABOUT,
837 ID_NEW,
838 ID_SHOWRAW
839};
01111366
RR
840
841IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
842
843BEGIN_EVENT_TABLE(MyFrame,wxFrame)
844 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
845 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
ab0f0386 846 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
b3f04dc2
VZ
847#ifdef wxHAVE_RAW_BITMAP
848 EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap)
849#endif
da9df1f5 850
dd38c875 851#if wxUSE_CLIPBOARD
da9df1f5
VZ
852 EVT_MENU(wxID_COPY, MyFrame::OnCopy)
853 EVT_MENU(wxID_PASTE, MyFrame::OnPaste)
854#endif // wxUSE_CLIPBOARD
01111366
RR
855END_EVENT_TABLE()
856
1d5b7a0b 857MyFrame::MyFrame()
47f797bd 858 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxImage sample"),
1d5b7a0b 859 wxPoint(20,20), wxSize(470,360) )
01111366 860{
da9df1f5
VZ
861 wxMenuBar *menu_bar = new wxMenuBar();
862
863 wxMenu *menuImage = new wxMenu;
864 menuImage->Append( ID_NEW, _T("&Show any image...\tCtrl-O"));
1e3c12d7 865
b3f04dc2 866#ifdef wxHAVE_RAW_BITMAP
da9df1f5 867 menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R"));
b3f04dc2 868#endif
da9df1f5
VZ
869 menuImage->AppendSeparator();
870 menuImage->Append( ID_ABOUT, _T("&About..."));
871 menuImage->AppendSeparator();
872 menuImage->Append( ID_QUIT, _T("E&xit\tCtrl-Q"));
873 menu_bar->Append(menuImage, _T("&Image"));
874
dd38c875 875#if wxUSE_CLIPBOARD
da9df1f5
VZ
876 wxMenu *menuClipboard = new wxMenu;
877 menuClipboard->Append(wxID_COPY, _T("&Copy test image\tCtrl-C"));
878 menuClipboard->Append(wxID_PASTE, _T("&Paste image\tCtrl-V"));
879 menu_bar->Append(menuClipboard, _T("&Clipboard"));
880#endif // wxUSE_CLIPBOARD
3d05544e 881
01111366 882 SetMenuBar( menu_bar );
1d5b7a0b 883
8520f137 884#if wxUSE_STATUSBAR
917e533b
RR
885 CreateStatusBar(2);
886 int widths[] = { -1, 100 };
887 SetStatusWidths( 2, widths );
8520f137 888#endif // wxUSE_STATUSBAR
1d5b7a0b 889
47f797bd 890 m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) );
cbd4be25 891
bf504f98
VS
892 // 500 width * 2500 height
893 m_canvas->SetScrollbars( 10, 10, 50, 250 );
01111366
RR
894}
895
896void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
897{
47f797bd 898 Close( true );
01111366
RR
899}
900
901void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
902{
51b07644
VZ
903 (void)wxMessageBox( _T("wxImage demo\n")
904 _T("Robert Roebling (c) 1998,2000"),
905 _T("About wxImage Demo"), wxICON_INFORMATION | wxOK );
fb1585ae
RR
906}
907
ab0f0386
VZ
908void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
909{
f6bcfd97
BP
910 wxString filename = wxFileSelector(_T("Select image file"));
911 if ( !filename )
912 return;
913
914 wxImage image;
915 if ( !image.LoadFile(filename) )
916 {
917 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
918
919 return;
920 }
921
368d59f0 922 (new MyImageFrame(this, wxBitmap(image)))->Show();
ab0f0386
VZ
923}
924
b3f04dc2
VZ
925#ifdef wxHAVE_RAW_BITMAP
926
481721e8 927void MyFrame::OnTestRawBitmap( wxCommandEvent &WXUNUSED(event) )
b3f04dc2
VZ
928{
929 (new MyRawBitmapFrame(this))->Show();
930}
931
932#endif // wxHAVE_RAW_BITMAP
933
dd38c875 934#if wxUSE_CLIPBOARD
da9df1f5
VZ
935
936void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
937{
938 wxBitmapDataObject *dobjBmp = new wxBitmapDataObject;
939 dobjBmp->SetBitmap(*m_canvas->my_horse_png);
940
dd38c875
MB
941 wxTheClipboard->Open();
942
da9df1f5
VZ
943 if ( !wxTheClipboard->SetData(dobjBmp) )
944 {
945 wxLogError(_T("Failed to copy bitmap to clipboard"));
946 }
dd38c875
MB
947
948 wxTheClipboard->Close();
da9df1f5
VZ
949}
950
951void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
952{
953 wxBitmapDataObject dobjBmp;
dd38c875
MB
954
955 wxTheClipboard->Open();
da9df1f5
VZ
956 if ( !wxTheClipboard->GetData(dobjBmp) )
957 {
958 wxLogMessage(_T("No bitmap data in the clipboard"));
959 }
960 else
961 {
962 (new MyImageFrame(this, dobjBmp.GetBitmap()))->Show();
963 }
dd38c875 964 wxTheClipboard->Close();
da9df1f5
VZ
965}
966
967#endif // wxUSE_CLIPBOARD
968
01111366
RR
969//-----------------------------------------------------------------------------
970// MyApp
971//-----------------------------------------------------------------------------
972
1d5b7a0b 973bool MyApp::OnInit()
01111366 974{
e9c4b1a2
JS
975#if wxUSE_LIBPNG
976 wxImage::AddHandler( new wxPNGHandler );
977#endif
978
979#if wxUSE_LIBJPEG
980 wxImage::AddHandler( new wxJPEGHandler );
981#endif
982
60a41aee
RR
983#if wxUSE_LIBTIFF
984 wxImage::AddHandler( new wxTIFFHandler );
985#endif
986
6e47faf1 987#if wxUSE_GIF
e1929140 988 wxImage::AddHandler( new wxGIFHandler );
6e47faf1
JS
989#endif
990
991#if wxUSE_PCX
cbd4be25 992 wxImage::AddHandler( new wxPCXHandler );
6e47faf1
JS
993#endif
994
995#if wxUSE_PNM
6319afe3 996 wxImage::AddHandler( new wxPNMHandler );
6e47faf1 997#endif
e1929140 998
a8d4e3ac
VS
999#if wxUSE_XPM
1000 wxImage::AddHandler( new wxXPMHandler );
1001#endif
1002
bf504f98
VS
1003#if wxUSE_ICO_CUR
1004 wxImage::AddHandler( new wxICOHandler );
1005 wxImage::AddHandler( new wxCURHandler );
2b5f62a0 1006 wxImage::AddHandler( new wxANIHandler );
bf504f98
VS
1007#endif
1008
01111366 1009 wxFrame *frame = new MyFrame();
47f797bd 1010 frame->Show( true );
1d5b7a0b 1011
47f797bd 1012 return true;
01111366
RR
1013}
1014