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