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