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