]> git.saurik.com Git - wxWidgets.git/blame - samples/image/image.cpp
port updates for newer Intel compiler support from bakefile svn and rebake
[wxWidgets.git] / samples / image / image.cpp
CommitLineData
5a566d89
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: samples/image/image.cpp
3// Purpose: sample showing operations with wxImage
4// Author: Robert Roebling
5// Modified by:
6// Created: 1998
7// RCS-ID: $Id$
8// Copyright: (c) 1998-2005 Robert Roebling
9// License: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
01111366 11
3d05544e
JS
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
9061c15c 20 #include "wx/wx.h"
3d05544e
JS
21#endif
22
01111366 23#include "wx/image.h"
a23fd0e1 24#include "wx/file.h"
5a566d89 25#include "wx/filename.h"
a8d4e3ac
VS
26#include "wx/mstream.h"
27#include "wx/wfstream.h"
1971d23c 28#include "wx/quantize.h"
95706ac1 29#include "wx/stopwatch.h"
a23fd0e1 30
da9df1f5
VZ
31#if wxUSE_CLIPBOARD
32 #include "wx/dataobj.h"
33 #include "wx/clipbrd.h"
34#endif // wxUSE_CLIPBOARD
35
dc1dbfc6 36#include "smile.xbm"
3ef37e7f 37#include "smile.xpm"
dc1dbfc6 38
284f2b59 39#if defined(__WXMSW__)
481721e8 40 #ifdef wxHAVE_RAW_BITMAP
9b24b388 41 #include "wx/rawbmp.h"
481721e8 42 #endif
c698eae5 43#endif
a8d4e3ac 44
284f2b59
RR
45#if defined(__WXMAC__) || defined(__WXGTK__)
46 #define wxHAVE_RAW_BITMAP
47 #include "wx/rawbmp.h"
48#endif
49
01111366
RR
50// derived classes
51
52class MyFrame;
53class MyApp;
54
55// MyCanvas
56
57class MyCanvas: public wxScrolledWindow
58{
1d5b7a0b 59public:
01111366 60 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
1d5b7a0b 61 ~MyCanvas();
01111366 62 void OnPaint( wxPaintEvent &event );
9390a202 63 void CreateAntiAliasedBitmap();
1d5b7a0b 64
bd981f27
VZ
65 wxBitmap my_horse_png;
66 wxBitmap my_horse_jpeg;
67 wxBitmap my_horse_gif;
68 wxBitmap my_horse_bmp;
69 wxBitmap my_horse_bmp2;
70 wxBitmap my_horse_pcx;
71 wxBitmap my_horse_pnm;
72 wxBitmap my_horse_tiff;
3af706cc 73 wxBitmap my_horse_tga;
bd981f27
VZ
74 wxBitmap my_horse_xpm;
75 wxBitmap my_horse_ico32;
76 wxBitmap my_horse_ico16;
77 wxBitmap my_horse_ico;
78 wxBitmap my_horse_cur;
79
80 wxBitmap my_smile_xbm;
81 wxBitmap my_square;
82 wxBitmap my_anti;
83
84 wxBitmap my_horse_asciigrey_pnm;
85 wxBitmap my_horse_rawgrey_pnm;
86
87 wxBitmap colorized_horse_jpeg;
234fedd2 88 wxBitmap my_cmyk_jpeg;
978d3d36 89
66df4ec6
VZ
90 wxBitmap my_toucan;
91 wxBitmap my_toucan_flipped_horiz;
92 wxBitmap my_toucan_flipped_vert;
93 wxBitmap my_toucan_flipped_both;
9061c15c 94 wxBitmap my_toucan_grey;
66df4ec6 95 wxBitmap my_toucan_head;
59185e29
WS
96 wxBitmap my_toucan_scaled_normal;
97 wxBitmap my_toucan_scaled_high;
98 wxBitmap my_toucan_blur;
66df4ec6 99
bf504f98 100 int xH, yH ;
bd981f27
VZ
101 int m_ani_images;
102 wxBitmap *my_horse_ani;
bf504f98 103
bd981f27 104private:
bea56879
VZ
105 wxBitmap m_bmpSmileXpm;
106 wxIcon m_iconSmileXpm;
107
1d5b7a0b 108 DECLARE_EVENT_TABLE()
01111366
RR
109};
110
18134a1c 111
01111366
RR
112// MyFrame
113
18134a1c 114
01111366
RR
115class MyFrame: public wxFrame
116{
1d5b7a0b
VZ
117public:
118 MyFrame();
01111366 119
01111366 120 void OnAbout( wxCommandEvent &event );
ab0f0386 121 void OnNewFrame( wxCommandEvent &event );
37ba70a5 122 void OnImageInfo( wxCommandEvent &event );
95706ac1
VZ
123 void OnThumbnail( wxCommandEvent &event );
124
b3f04dc2
VZ
125#ifdef wxHAVE_RAW_BITMAP
126 void OnTestRawBitmap( wxCommandEvent &event );
127#endif // wxHAVE_RAW_BITMAP
01111366 128 void OnQuit( wxCommandEvent &event );
1d5b7a0b 129
dd38c875 130#if wxUSE_CLIPBOARD
da9df1f5
VZ
131 void OnCopy(wxCommandEvent& event);
132 void OnPaste(wxCommandEvent& event);
133#endif // wxUSE_CLIPBOARD
134
01111366 135 MyCanvas *m_canvas;
1d5b7a0b 136
60a41aee 137private:
37ba70a5
VZ
138 // ask user for the file name and try to load an image from it
139 //
140 // return the file path on success, empty string if we failed to load the
141 // image or were cancelled by user
142 static wxString LoadUserImage(wxImage& image);
143
144
1d5b7a0b
VZ
145 DECLARE_DYNAMIC_CLASS(MyFrame)
146 DECLARE_EVENT_TABLE()
01111366
RR
147};
148
1d8acb7d
VZ
149// ----------------------------------------------------------------------------
150// Frame used for showing a standalone image
151// ----------------------------------------------------------------------------
152
153enum
154{
155 ID_ROTATE_LEFT = 100,
05a8831a
VZ
156 ID_ROTATE_RIGHT,
157 ID_RESIZE
1d8acb7d
VZ
158};
159
ab0f0386
VZ
160class MyImageFrame : public wxFrame
161{
162public:
1d8acb7d
VZ
163 MyImageFrame(wxFrame *parent, const wxString& desc, const wxBitmap& bitmap)
164 : wxFrame(parent, wxID_ANY,
165 wxString::Format(_T("Image from %s"), desc.c_str()),
ab0f0386 166 wxDefaultPosition, wxDefaultSize,
05a8831a 167 wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE),
1971d23c 168 m_bitmap(bitmap)
ab0f0386 169 {
1d8acb7d
VZ
170 wxMenu *menu = new wxMenu;
171 menu->Append(wxID_SAVE);
172 menu->AppendSeparator();
05a8831a
VZ
173 menu->Append(ID_RESIZE, _T("&Fit to window\tCtrl-F"));
174 menu->AppendSeparator();
1d8acb7d
VZ
175 menu->Append(ID_ROTATE_LEFT, _T("Rotate &left\tCtrl-L"));
176 menu->Append(ID_ROTATE_RIGHT, _T("Rotate &right\tCtrl-R"));
177
178 wxMenuBar *mbar = new wxMenuBar;
179 mbar->Append(menu, _T("&Image"));
180 SetMenuBar(mbar);
181
05a8831a
VZ
182 CreateStatusBar();
183
ab0f0386 184 SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
05a8831a
VZ
185
186 UpdateStatusBar();
ab0f0386
VZ
187 }
188
0c543b7a
VZ
189 void OnEraseBackground(wxEraseEvent& WXUNUSED(event))
190 {
191 // do nothing here to be able to see how transparent images are shown
192 }
193
ab0f0386
VZ
194 void OnPaint(wxPaintEvent& WXUNUSED(event))
195 {
05a8831a
VZ
196 wxPaintDC dc(this);
197 const wxSize size = GetClientSize();
198 dc.DrawBitmap(m_bitmap,
199 (size.x - m_bitmap.GetWidth())/2,
200 (size.y - m_bitmap.GetHeight())/2,
201 true /* use mask */);
ab0f0386
VZ
202 }
203
1d8acb7d 204 void OnSave(wxCommandEvent& WXUNUSED(event))
1971d23c 205 {
71307412 206#if wxUSE_FILEDLG
368d59f0 207 wxImage image = m_bitmap.ConvertToImage();
1971d23c 208
4693b20c 209 wxString savefilename = wxFileSelector( wxT("Save Image"),
71307412
WS
210 wxEmptyString,
211 wxEmptyString,
4693b20c 212 (const wxChar *)NULL,
d0ee33f5
WS
213 wxT("BMP files (*.bmp)|*.bmp|")
214 wxT("PNG files (*.png)|*.png|")
215 wxT("JPEG files (*.jpg)|*.jpg|")
216 wxT("GIF files (*.gif)|*.gif|")
217 wxT("TIFF files (*.tif)|*.tif|")
218 wxT("PCX files (*.pcx)|*.pcx|")
219 wxT("ICO files (*.ico)|*.ico|")
220 wxT("CUR files (*.cur)|*.cur"),
ff3e84ff 221 wxFD_SAVE,
47f797bd 222 this);
1971d23c
VZ
223
224 if ( savefilename.empty() )
225 return;
226
5a566d89
VZ
227 wxString extension;
228 wxFileName::SplitPath(savefilename, NULL, NULL, &extension);
1971d23c 229
5a566d89 230 bool saved = false;
c66972cc 231 if ( extension == _T("bmp") )
5a566d89
VZ
232 {
233 static const int bppvalues[] =
234 {
235 wxBMP_1BPP,
236 wxBMP_1BPP_BW,
237 wxBMP_4BPP,
238 wxBMP_8BPP,
239 wxBMP_8BPP_GREY,
240 wxBMP_8BPP_RED,
241 wxBMP_8BPP_PALETTE,
242 wxBMP_24BPP
243 };
244
24207dfc 245 const wxString bppchoices[] =
5a566d89
VZ
246 {
247 _T("1 bpp color"),
248 _T("1 bpp B&W"),
249 _T("4 bpp color"),
250 _T("8 bpp color"),
251 _T("8 bpp greyscale"),
252 _T("8 bpp red"),
253 _T("8 bpp own palette"),
254 _T("24 bpp")
255 };
256
257 int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"),
258 _T("Image sample: save file"),
259 WXSIZEOF(bppchoices),
260 bppchoices,
261 this);
262 if ( bppselection != -1 )
263 {
264 int format = bppvalues[bppselection];
265 image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, format);
266
267 if ( format == wxBMP_8BPP_PALETTE )
268 {
269 unsigned char *cmap = new unsigned char [256];
270 for ( int i = 0; i < 256; i++ )
271 cmap[i] = (unsigned char)i;
272 image.SetPalette(wxPalette(256, cmap, cmap, cmap));
273
d0ee33f5 274 delete[] cmap;
5a566d89
VZ
275 }
276 }
1971d23c 277 }
5a566d89
VZ
278 else if ( extension == _T("png") )
279 {
280 static const int pngvalues[] =
281 {
282 wxPNG_TYPE_COLOUR,
283 wxPNG_TYPE_COLOUR,
284 wxPNG_TYPE_GREY,
285 wxPNG_TYPE_GREY,
286 wxPNG_TYPE_GREY_RED,
287 wxPNG_TYPE_GREY_RED,
288 };
289
24207dfc 290 const wxString pngchoices[] =
5a566d89
VZ
291 {
292 _T("Colour 8bpp"),
293 _T("Colour 16bpp"),
294 _T("Grey 8bpp"),
295 _T("Grey 16bpp"),
296 _T("Grey red 8bpp"),
297 _T("Grey red 16bpp"),
298 };
299
300 int sel = wxGetSingleChoiceIndex(_T("Set PNG format"),
301 _T("Image sample: save file"),
302 WXSIZEOF(pngchoices),
303 pngchoices,
304 this);
305 if ( sel != -1 )
306 {
307 image.SetOption(wxIMAGE_OPTION_PNG_FORMAT, pngvalues[sel]);
308 image.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH, sel % 2 ? 16 : 8);
309 }
310 }
311 else if ( extension == _T("cur") )
45647dcf 312 {
51b07644
VZ
313 image.Rescale(32,32);
314 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0);
45647dcf
VS
315 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0);
316 // This shows how you can save an image with explicitly
317 // specified image format:
5a566d89 318 saved = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR);
45647dcf 319 }
5a566d89
VZ
320
321 if ( !saved )
45647dcf
VS
322 {
323 // This one guesses image format from filename extension
324 // (it may fail if the extension is not recognized):
5a566d89 325 image.SaveFile(savefilename);
45647dcf 326 }
71307412 327#endif // wxUSE_FILEDLG
1971d23c
VZ
328 }
329
05a8831a
VZ
330 void OnResize(wxCommandEvent& WXUNUSED(event))
331 {
332 wxImage img(m_bitmap.ConvertToImage());
333
334 const wxSize size = GetClientSize();
335 img.Rescale(size.x, size.y, wxIMAGE_QUALITY_HIGH);
336 m_bitmap = wxBitmap(img);
337
338 UpdateStatusBar();
339 Refresh();
340 }
341
1d8acb7d
VZ
342 void OnRotate(wxCommandEvent& event)
343 {
344 double angle = 5;
345 if ( event.GetId() == ID_ROTATE_LEFT )
346 angle = -angle;
347
348 wxImage img(m_bitmap.ConvertToImage());
05a8831a 349 img = img.Rotate(angle, wxPoint(img.GetWidth() / 2, img.GetHeight() / 2));
1d8acb7d
VZ
350 if ( !img.Ok() )
351 {
352 wxLogWarning(_T("Rotation failed"));
353 return;
354 }
355
356 m_bitmap = wxBitmap(img);
05a8831a
VZ
357
358 UpdateStatusBar();
1d8acb7d
VZ
359 Refresh();
360 }
361
ab0f0386 362private:
05a8831a
VZ
363 void UpdateStatusBar()
364 {
365 wxLogStatus(this, _T("Image size: (%d, %d)"),
366 m_bitmap.GetWidth(),
367 m_bitmap.GetHeight());
368 }
369
ab0f0386
VZ
370 wxBitmap m_bitmap;
371
372 DECLARE_EVENT_TABLE()
373};
374
b3f04dc2
VZ
375#ifdef wxHAVE_RAW_BITMAP
376
377#include "wx/rawbmp.h"
378
379class MyRawBitmapFrame : public wxFrame
380{
381public:
382 enum
383 {
384 BORDER = 15,
385 SIZE = 150,
386 REAL_SIZE = SIZE - 2*BORDER
387 };
388
389 MyRawBitmapFrame(wxFrame *parent)
47f797bd 390 : wxFrame(parent, wxID_ANY, _T("Raw bitmaps (how exciting)")),
8ef08948
RD
391 m_bitmap(SIZE, SIZE, 24),
392 m_alphaBitmap(SIZE, SIZE, 32)
b3f04dc2 393 {
8ef08948 394 SetClientSize(SIZE, SIZE*2+25);
b3f04dc2 395
8ef08948
RD
396 InitAlphaBitmap();
397 InitBitmap();
66df4ec6 398
8ef08948
RD
399 }
400
401 void InitAlphaBitmap()
402 {
6e5551ad
RR
403 // First, clear the whole bitmap by making it alpha
404 {
8ef08948 405 wxAlphaPixelData data( m_alphaBitmap, wxPoint(0,0), wxSize(SIZE, SIZE) );
6e5551ad
RR
406 if ( !data )
407 {
408 wxLogError(_T("Failed to gain raw access to bitmap data"));
409 return;
410 }
8ef08948 411 wxAlphaPixelData::Iterator p(data);
6e5551ad
RR
412 for ( int y = 0; y < SIZE; ++y )
413 {
8ef08948 414 wxAlphaPixelData::Iterator rowStart = p;
6e5551ad
RR
415 for ( int x = 0; x < SIZE; ++x )
416 {
417 p.Alpha() = 0;
418 ++p; // same as p.OffsetX(1)
419 }
420 p = rowStart;
421 p.OffsetY(data, 1);
422 }
423 }
095980e1 424
6e5551ad 425 // Then, draw colourful alpha-blended stripes
8ef08948
RD
426 wxAlphaPixelData data(m_alphaBitmap, wxPoint(BORDER, BORDER),
427 wxSize(REAL_SIZE, REAL_SIZE));
b3f04dc2
VZ
428 if ( !data )
429 {
430 wxLogError(_T("Failed to gain raw access to bitmap data"));
431 return;
432 }
433
8ef08948 434 wxAlphaPixelData::Iterator p(data);
b3f04dc2 435
b3f04dc2
VZ
436 for ( int y = 0; y < REAL_SIZE; ++y )
437 {
8ef08948 438 wxAlphaPixelData::Iterator rowStart = p;
b3f04dc2
VZ
439
440 int r = y < REAL_SIZE/3 ? 255 : 0,
441 g = (REAL_SIZE/3 <= y) && (y < 2*(REAL_SIZE/3)) ? 255 : 0,
442 b = 2*(REAL_SIZE/3) <= y ? 255 : 0;
443
444 for ( int x = 0; x < REAL_SIZE; ++x )
445 {
b068dfe2 446 // note that RGB must be premultiplied by alpha
8ef08948 447 unsigned a = (wxAlphaPixelData::Iterator::ChannelType)((x*255.)/REAL_SIZE);
5c99abbe
MW
448 p.Red() = r * a / 256;
449 p.Green() = g * a / 256;
450 p.Blue() = b * a / 256;
b068dfe2 451 p.Alpha() = a;
b3f04dc2
VZ
452
453 ++p; // same as p.OffsetX(1)
454 }
455
456 p = rowStart;
1d2f48b6 457 p.OffsetY(data, 1);
b3f04dc2
VZ
458 }
459 }
460
8ef08948
RD
461 void InitBitmap()
462 {
463 // draw some colourful stripes without alpha
464 wxNativePixelData data(m_bitmap);
465 if ( !data )
466 {
467 wxLogError(_T("Failed to gain raw access to bitmap data"));
468 return;
469 }
470
471 wxNativePixelData::Iterator p(data);
472 for ( int y = 0; y < SIZE; ++y )
473 {
474 wxNativePixelData::Iterator rowStart = p;
475
476 int r = y < SIZE/3 ? 255 : 0,
477 g = (SIZE/3 <= y) && (y < 2*(SIZE/3)) ? 255 : 0,
478 b = 2*(SIZE/3) <= y ? 255 : 0;
479
480 for ( int x = 0; x < SIZE; ++x )
481 {
482 p.Red() = r;
483 p.Green() = g;
484 p.Blue() = b;
485 ++p; // same as p.OffsetX(1)
486 }
487
488 p = rowStart;
489 p.OffsetY(data, 1);
490 }
491 }
492
b3f04dc2
VZ
493 void OnPaint(wxPaintEvent& WXUNUSED(event))
494 {
495 wxPaintDC dc( this );
496 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER);
497 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE/2 - BORDER);
498 dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE - 2*BORDER);
8ef08948
RD
499 dc.DrawBitmap( m_alphaBitmap, 0, 0, true /* use mask */ );
500
501 dc.DrawText(_T("Raw bitmap access without alpha"), 0, SIZE+5);
502 dc.DrawBitmap( m_bitmap, 0, SIZE+5+dc.GetCharHeight());
b3f04dc2
VZ
503 }
504
505private:
506 wxBitmap m_bitmap;
8ef08948 507 wxBitmap m_alphaBitmap;
b3f04dc2
VZ
508
509 DECLARE_EVENT_TABLE()
510};
511
512#endif // wxHAVE_RAW_BITMAP
513
01111366
RR
514// MyApp
515
516class MyApp: public wxApp
517{
1d5b7a0b
VZ
518public:
519 virtual bool OnInit();
01111366
RR
520};
521
522// main program
523
524IMPLEMENT_APP(MyApp)
525
526// MyCanvas
527
ab0f0386 528BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
0c543b7a
VZ
529 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground)
530 EVT_PAINT(MyImageFrame::OnPaint)
1d8acb7d
VZ
531
532 EVT_MENU(wxID_SAVE, MyImageFrame::OnSave)
533 EVT_MENU_RANGE(ID_ROTATE_LEFT, ID_ROTATE_RIGHT, MyImageFrame::OnRotate)
05a8831a 534 EVT_MENU(ID_RESIZE, MyImageFrame::OnResize)
ab0f0386
VZ
535END_EVENT_TABLE()
536
b3f04dc2
VZ
537#ifdef wxHAVE_RAW_BITMAP
538
539BEGIN_EVENT_TABLE(MyRawBitmapFrame, wxFrame)
540 EVT_PAINT(MyRawBitmapFrame::OnPaint)
541END_EVENT_TABLE()
542
543#endif // wxHAVE_RAW_BITMAP
544
1d5b7a0b 545BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
adf45b5d 546 EVT_PAINT(MyCanvas::OnPaint)
01111366
RR
547END_EVENT_TABLE()
548
acbd13a3 549MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
1d5b7a0b 550 const wxPoint &pos, const wxSize &size )
adf45b5d
PC
551 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
552 , m_bmpSmileXpm(smile_xpm)
553 , m_iconSmileXpm(smile_xpm)
01111366 554{
bd981f27 555 my_horse_ani = NULL;
2b5f62a0
VZ
556 m_ani_images = 0 ;
557
60a41aee
RR
558 SetBackgroundColour(* wxWHITE);
559
560 wxBitmap bitmap( 100, 100 );
561
562 wxMemoryDC dc;
563 dc.SelectObject( bitmap );
a60b1f5d 564 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
cf3da716 565 dc.SetPen( *wxBLACK_PEN );
60a41aee 566 dc.DrawRectangle( 0, 0, 100, 100 );
cf3da716
RR
567 dc.SetBrush( *wxWHITE_BRUSH );
568 dc.DrawRectangle( 20, 20, 60, 60 );
60a41aee
RR
569 dc.SelectObject( wxNullBitmap );
570
571 // try to find the directory with our images
572 wxString dir;
4693b20c 573 if ( wxFile::Exists(wxT("./horse.png")) )
2b5f62a0 574 dir = wxT("./");
4693b20c 575 else if ( wxFile::Exists(wxT("../horse.png")) )
2b5f62a0 576 dir = wxT("../");
60a41aee 577 else
4693b20c 578 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
60a41aee 579
a8d4e3ac 580 wxImage image = bitmap.ConvertToImage();
60a41aee 581
bea56879 582#if wxUSE_LIBPNG
51b07644 583 if ( !image.SaveFile( dir + _T("test.png"), wxBITMAP_TYPE_PNG ))
4693b20c 584 wxLogError(wxT("Can't save file"));
f6bcfd97 585
176fe5af 586 image.Destroy();
60a41aee 587
30ad0a14 588 if ( image.LoadFile( dir + _T("test.png") ) )
bd981f27 589 my_square = wxBitmap( image );
f6bcfd97 590
176fe5af 591 image.Destroy();
cc977e5f 592
51b07644 593 if ( !image.LoadFile( dir + _T("horse.png")) )
4693b20c 594 wxLogError(wxT("Can't load PNG image"));
60a41aee 595 else
bd981f27 596 my_horse_png = wxBitmap( image );
66df4ec6 597
9061c15c
WS
598 if ( !image.LoadFile( dir + _T("toucan.png")) )
599 wxLogError(wxT("Can't load PNG image"));
600 else
601 my_toucan = wxBitmap(image);
602
66df4ec6
VZ
603 my_toucan_flipped_horiz = wxBitmap(image.Mirror(true));
604 my_toucan_flipped_vert = wxBitmap(image.Mirror(false));
605 my_toucan_flipped_both = wxBitmap(image.Mirror(true).Mirror(false));
9061c15c 606 my_toucan_grey = wxBitmap(image.ConvertToGreyscale());
66df4ec6 607 my_toucan_head = wxBitmap(image.GetSubImage(wxRect(40, 7, 80, 60)));
59185e29
WS
608 my_toucan_scaled_normal = wxBitmap(image.Scale(110,90,wxIMAGE_QUALITY_NORMAL));
609 my_toucan_scaled_high = wxBitmap(image.Scale(110,90,wxIMAGE_QUALITY_HIGH));
610 my_toucan_blur = wxBitmap(image.Blur(10));
66df4ec6 611
bea56879 612#endif // wxUSE_LIBPNG
60a41aee 613
bea56879 614#if wxUSE_LIBJPEG
176fe5af 615 image.Destroy();
279ababf 616
51b07644 617 if ( !image.LoadFile( dir + _T("horse.jpg")) )
4693b20c 618 wxLogError(wxT("Can't load JPG image"));
60a41aee 619 else
978d3d36 620 {
bd981f27 621 my_horse_jpeg = wxBitmap( image );
234fedd2 622
978d3d36
VZ
623 // Colorize by rotating green hue to red
624 wxImage::HSVValue greenHSV = wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0));
625 wxImage::HSVValue redHSV = wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0));
626 image.RotateHue(redHSV.hue - greenHSV.hue);
bd981f27 627 colorized_horse_jpeg = wxBitmap( image );
978d3d36 628 }
234fedd2
VZ
629
630 if ( !image.LoadFile( dir + _T("cmyk.jpg")) )
631 wxLogError(_T("Can't load CMYK JPG image"));
632 else
633 my_cmyk_jpeg = wxBitmap(image);
bea56879 634#endif // wxUSE_LIBJPEG
6e47faf1
JS
635
636#if wxUSE_GIF
176fe5af 637 image.Destroy();
279ababf 638
2b5f62a0 639 if ( !image.LoadFile( dir + _T("horse.gif" )) )
4693b20c 640 wxLogError(wxT("Can't load GIF image"));
60a41aee 641 else
bd981f27 642 my_horse_gif = wxBitmap( image );
6e47faf1 643#endif
cbd4be25 644
6e47faf1 645#if wxUSE_PCX
176fe5af 646 image.Destroy();
279ababf 647
51b07644 648 if ( !image.LoadFile( dir + _T("horse.pcx"), wxBITMAP_TYPE_PCX ) )
4693b20c 649 wxLogError(wxT("Can't load PCX image"));
60a41aee 650 else
bd981f27 651 my_horse_pcx = wxBitmap( image );
6e47faf1 652#endif
cbd4be25 653
176fe5af 654 image.Destroy();
279ababf 655
51b07644 656 if ( !image.LoadFile( dir + _T("horse.bmp"), wxBITMAP_TYPE_BMP ) )
4693b20c 657 wxLogError(wxT("Can't load BMP image"));
60a41aee 658 else
bd981f27 659 my_horse_bmp = wxBitmap( image );
6319afe3 660
a8d4e3ac 661#if wxUSE_XPM
9d8bdf2d
VS
662 image.Destroy();
663
51b07644 664 if ( !image.LoadFile( dir + _T("horse.xpm"), wxBITMAP_TYPE_XPM ) )
4693b20c 665 wxLogError(wxT("Can't load XPM image"));
9d8bdf2d 666 else
bd981f27 667 my_horse_xpm = wxBitmap( image );
9d8bdf2d 668
51b07644 669 if ( !image.SaveFile( dir + _T("test.xpm"), wxBITMAP_TYPE_XPM ))
4693b20c 670 wxLogError(wxT("Can't save file"));
a8d4e3ac
VS
671#endif
672
6e47faf1 673#if wxUSE_PNM
176fe5af 674 image.Destroy();
279ababf 675
51b07644 676 if ( !image.LoadFile( dir + _T("horse.pnm"), wxBITMAP_TYPE_PNM ) )
4693b20c 677 wxLogError(wxT("Can't load PNM image"));
60a41aee 678 else
bd981f27 679 my_horse_pnm = wxBitmap( image );
3e8711ce
JS
680
681 image.Destroy();
682
683 if ( !image.LoadFile( dir + _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM ) )
684 wxLogError(wxT("Can't load PNM image"));
685 else
bd981f27 686 my_horse_asciigrey_pnm = wxBitmap( image );
3e8711ce
JS
687
688 image.Destroy();
689
690 if ( !image.LoadFile( dir + _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM ) )
691 wxLogError(wxT("Can't load PNM image"));
692 else
bd981f27 693 my_horse_rawgrey_pnm = wxBitmap( image );
6e47faf1 694#endif
f048e32f 695
60a41aee 696#if wxUSE_LIBTIFF
176fe5af 697 image.Destroy();
279ababf 698
51b07644 699 if ( !image.LoadFile( dir + _T("horse.tif"), wxBITMAP_TYPE_TIF ) )
4693b20c 700 wxLogError(wxT("Can't load TIFF image"));
60a41aee 701 else
bd981f27 702 my_horse_tiff = wxBitmap( image );
60a41aee
RR
703#endif
704
3af706cc
VZ
705#if wxUSE_LIBTIFF
706 image.Destroy();
707
708 if ( !image.LoadFile( dir + _T("horse.tga"), wxBITMAP_TYPE_TGA ) )
709 wxLogError(wxT("Can't load TGA image"));
710 else
711 my_horse_tga = wxBitmap( image );
712#endif
713
60a41aee 714 CreateAntiAliasedBitmap();
dc1dbfc6 715
bd981f27 716 my_smile_xbm = wxBitmap( (const char*)smile_bits, smile_width,
dc1dbfc6 717 smile_height, 1 );
bfef20bf
GRG
718
719 // demonstrates XPM automatically using the mask when saving
720 if ( m_bmpSmileXpm.Ok() )
51b07644 721 m_bmpSmileXpm.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM);
bf504f98
VS
722
723#if wxUSE_ICO_CUR
724 image.Destroy();
725
51b07644 726 if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 0 ) )
bf504f98
VS
727 wxLogError(wxT("Can't load first ICO image"));
728 else
bd981f27 729 my_horse_ico32 = wxBitmap( image );
51b07644 730
bf504f98
VS
731 image.Destroy();
732
51b07644 733 if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 1 ) )
bf504f98
VS
734 wxLogError(wxT("Can't load second ICO image"));
735 else
bd981f27 736 my_horse_ico16 = wxBitmap( image );
bf504f98
VS
737
738 image.Destroy();
739
51b07644 740 if ( !image.LoadFile( dir + _T("horse.ico") ) )
bf504f98
VS
741 wxLogError(wxT("Can't load best ICO image"));
742 else
bd981f27 743 my_horse_ico = wxBitmap( image );
bf504f98
VS
744
745 image.Destroy();
746
51b07644 747 if ( !image.LoadFile( dir + _T("horse.cur"), wxBITMAP_TYPE_CUR ) )
bf504f98
VS
748 wxLogError(wxT("Can't load best ICO image"));
749 else
51b07644 750 {
bd981f27 751 my_horse_cur = wxBitmap( image );
fd94e8aa
VS
752 xH = 30 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) ;
753 yH = 2420 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ;
51b07644 754 }
2b5f62a0 755
08811762 756 m_ani_images = wxImage::GetImageCount ( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI );
2b5f62a0
VZ
757 if (m_ani_images==0)
758 wxLogError(wxT("No ANI-format images found"));
759 else
760 my_horse_ani = new wxBitmap [m_ani_images];
761 int i ;
762 for (i=0; i < m_ani_images; i++)
763 {
764 image.Destroy();
08811762 765 if (!image.LoadFile( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI, i ))
2b5f62a0
VZ
766 {
767 wxString tmp = wxT("Can't load image number ");
768 tmp << i ;
769 wxLogError(tmp);
770 }
925e9792 771 else
2b5f62a0
VZ
772 my_horse_ani [i] = wxBitmap( image );
773 }
da9df1f5 774#endif // wxUSE_ICO_CUR
bf504f98 775
51b07644
VZ
776 image.Destroy();
777
778 // test image loading from stream
779 wxFile file(dir + _T("horse.bmp"));
30ad0a14 780 if ( file.IsOpened() )
51b07644 781 {
9386cb75
WS
782 wxFileOffset len = file.Length();
783 size_t dataSize = (size_t)len;
784 void *data = malloc(dataSize);
785 if ( file.Read(data, dataSize) != len )
30ad0a14 786 wxLogError(_T("Reading bitmap file failed"));
51b07644 787 else
30ad0a14 788 {
9386cb75 789 wxMemoryInputStream mis(data, dataSize);
30ad0a14
VZ
790 if ( !image.LoadFile(mis) )
791 wxLogError(wxT("Can't load BMP image from stream"));
792 else
bd981f27 793 my_horse_bmp2 = wxBitmap( image );
30ad0a14 794 }
51b07644 795
30ad0a14
VZ
796 free(data);
797 }
01111366
RR
798}
799
1d5b7a0b 800MyCanvas::~MyCanvas()
01111366 801{
2b5f62a0 802 delete [] my_horse_ani;
01111366
RR
803}
804
805void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
806{
be25e480
RR
807 wxPaintDC dc( this );
808 PrepareDC( dc );
a91b47e8 809
51b07644 810 dc.DrawText( _T("Loaded image"), 30, 10 );
bd981f27
VZ
811 if (my_square.Ok())
812 dc.DrawBitmap( my_square, 30, 30 );
f048e32f 813
51b07644 814 dc.DrawText( _T("Drawn directly"), 150, 10 );
a60b1f5d 815 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
cf3da716 816 dc.SetPen( *wxBLACK_PEN );
be25e480 817 dc.DrawRectangle( 150, 30, 100, 100 );
cf3da716
RR
818 dc.SetBrush( *wxWHITE_BRUSH );
819 dc.DrawRectangle( 170, 50, 60, 60 );
be25e480 820
bd981f27
VZ
821 if (my_anti.Ok())
822 dc.DrawBitmap( my_anti, 280, 30 );
be25e480 823
51b07644 824 dc.DrawText( _T("PNG handler"), 30, 135 );
bd981f27 825 if (my_horse_png.Ok())
408b4168 826 {
bd981f27 827 dc.DrawBitmap( my_horse_png, 30, 150 );
be25e480 828 wxRect rect(0,0,100,100);
bd981f27 829 wxBitmap sub( my_horse_png.GetSubBitmap(rect) );
66df4ec6
VZ
830 dc.DrawText( _T("GetSubBitmap()"), 280, 175 );
831 dc.DrawBitmap( sub, 280, 195 );
be25e480
RR
832 }
833
51b07644 834 dc.DrawText( _T("JPEG handler"), 30, 365 );
bd981f27
VZ
835 if (my_horse_jpeg.Ok())
836 dc.DrawBitmap( my_horse_jpeg, 30, 380 );
be25e480 837
234fedd2
VZ
838 dc.DrawText( _T("Green rotated to red"), 280, 365 );
839 if (colorized_horse_jpeg.Ok())
840 dc.DrawBitmap( colorized_horse_jpeg, 280, 380 );
841
842 dc.DrawText( _T("CMYK JPEG image"), 530, 365 );
843 if (my_cmyk_jpeg.Ok())
844 dc.DrawBitmap( my_cmyk_jpeg, 530, 380 );
845
51b07644 846 dc.DrawText( _T("GIF handler"), 30, 595 );
bd981f27
VZ
847 if (my_horse_gif.Ok())
848 dc.DrawBitmap( my_horse_gif, 30, 610 );
be25e480 849
51b07644 850 dc.DrawText( _T("PCX handler"), 30, 825 );
bd981f27
VZ
851 if (my_horse_pcx.Ok())
852 dc.DrawBitmap( my_horse_pcx, 30, 840 );
be25e480 853
51b07644 854 dc.DrawText( _T("BMP handler"), 30, 1055 );
bd981f27
VZ
855 if (my_horse_bmp.Ok())
856 dc.DrawBitmap( my_horse_bmp, 30, 1070 );
be25e480 857
51b07644 858 dc.DrawText( _T("BMP read from memory"), 280, 1055 );
bd981f27
VZ
859 if (my_horse_bmp2.Ok())
860 dc.DrawBitmap( my_horse_bmp2, 280, 1070 );
51b07644
VZ
861
862 dc.DrawText( _T("PNM handler"), 30, 1285 );
bd981f27
VZ
863 if (my_horse_pnm.Ok())
864 dc.DrawBitmap( my_horse_pnm, 30, 1300 );
d0ee33f5 865
3e8711ce 866 dc.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 );
bd981f27
VZ
867 if (my_horse_asciigrey_pnm.Ok())
868 dc.DrawBitmap( my_horse_asciigrey_pnm, 280, 1300 );
3e8711ce
JS
869
870 dc.DrawText( _T("PNM handler (raw grey)"), 530, 1285 );
bd981f27
VZ
871 if (my_horse_rawgrey_pnm.Ok())
872 dc.DrawBitmap( my_horse_rawgrey_pnm, 530, 1300 );
408b4168 873
51b07644 874 dc.DrawText( _T("TIFF handler"), 30, 1515 );
bd981f27
VZ
875 if (my_horse_tiff.Ok())
876 dc.DrawBitmap( my_horse_tiff, 30, 1530 );
be25e480 877
3af706cc
VZ
878 dc.DrawText( _T("TGA handler"), 30, 1745 );
879 if (my_horse_tga.Ok())
880 dc.DrawBitmap( my_horse_tga, 30, 1760 );
881
882 dc.DrawText( _T("XPM handler"), 30, 1975 );
bd981f27 883 if (my_horse_xpm.Ok())
3af706cc 884 dc.DrawBitmap( my_horse_xpm, 30, 2000 );
9d8bdf2d 885
234fedd2 886 // toucans
66df4ec6 887 {
234fedd2 888 int x = 750, y = 10, yy = 170;
66df4ec6
VZ
889
890 dc.DrawText(wxT("Original toucan"), x+50, y);
59b7a280 891 dc.DrawBitmap(my_toucan, x, y+15, true);
66df4ec6
VZ
892 y += yy;
893 dc.DrawText(wxT("Flipped horizontally"), x+50, y);
59b7a280 894 dc.DrawBitmap(my_toucan_flipped_horiz, x, y+15, true);
66df4ec6
VZ
895 y += yy;
896 dc.DrawText(wxT("Flipped vertically"), x+50, y);
59b7a280 897 dc.DrawBitmap(my_toucan_flipped_vert, x, y+15, true);
66df4ec6
VZ
898 y += yy;
899 dc.DrawText(wxT("Flipped both h&v"), x+50, y);
59b7a280 900 dc.DrawBitmap(my_toucan_flipped_both, x, y+15, true);
66df4ec6 901
9061c15c
WS
902 y += yy;
903 dc.DrawText(wxT("In greyscale"), x+50, y);
59b7a280 904 dc.DrawBitmap(my_toucan_grey, x, y+15, true);
9061c15c 905
66df4ec6
VZ
906 y += yy;
907 dc.DrawText(wxT("Toucan's head"), x+50, y);
59b7a280 908 dc.DrawBitmap(my_toucan_head, x, y+15, true);
59185e29
WS
909
910 y += yy;
911 dc.DrawText(wxT("Scaled with normal quality"), x+50, y);
912 dc.DrawBitmap(my_toucan_scaled_normal, x, y+15, true);
913
914 y += yy;
915 dc.DrawText(wxT("Scaled with high quality"), x+50, y);
916 dc.DrawBitmap(my_toucan_scaled_high, x, y+15, true);
917
918 y += yy;
919 dc.DrawText(wxT("Blured"), x+50, y);
920 dc.DrawBitmap(my_toucan_blur, x, y+15, true);
66df4ec6 921 }
bf504f98 922
bd981f27 923 if (my_smile_xbm.Ok())
be25e480 924 {
3af706cc
VZ
925 int x = 300, y = 1800;
926
927 dc.DrawText( _T("XBM bitmap"), x, y );
928 dc.DrawText( _T("(green on red)"), x, y + 15 );
51b07644
VZ
929 dc.SetTextForeground( _T("GREEN") );
930 dc.SetTextBackground( _T("RED") );
3af706cc 931 dc.DrawBitmap( my_smile_xbm, x, y + 30 );
408b4168 932
19bc1514 933 dc.SetTextForeground( *wxBLACK );
3af706cc
VZ
934 dc.DrawText( _T("After wxImage conversion"), x + 120, y );
935 dc.DrawText( _T("(red on white)"), x + 120, y + 15 );
a60b1f5d 936 dc.SetTextForeground( wxT("RED") );
bd981f27 937 wxImage i = my_smile_xbm.ConvertToImage();
176fe5af
GRG
938 i.SetMaskColour( 255, 255, 255 );
939 i.Replace( 0, 0, 0,
be25e480
RR
940 wxRED_PEN->GetColour().Red(),
941 wxRED_PEN->GetColour().Green(),
942 wxRED_PEN->GetColour().Blue() );
3af706cc 943 dc.DrawBitmap( wxBitmap(i), x + 120, y + 30, true );
19bc1514 944 dc.SetTextForeground( *wxBLACK );
be25e480 945 }
176fe5af 946
408b4168 947
41fbc841 948 wxBitmap mono( 60,50,1 );
82ea63e6
RR
949 wxMemoryDC memdc;
950 memdc.SelectObject( mono );
bfef20bf 951 memdc.SetPen( *wxBLACK_PEN );
82ea63e6 952 memdc.SetBrush( *wxWHITE_BRUSH );
41fbc841
RR
953 memdc.DrawRectangle( 0,0,60,50 );
954 memdc.SetTextForeground( *wxBLACK );
9fe7d430
RR
955#ifndef __WXGTK20__
956 // I cannot convince GTK2 to draw into mono bitmaps
51b07644 957 memdc.DrawText( _T("Hi!"), 5, 5 );
9fe7d430 958#endif
41fbc841
RR
959 memdc.SetBrush( *wxBLACK_BRUSH );
960 memdc.DrawRectangle( 33,5,20,20 );
961 memdc.SetPen( *wxRED_PEN );
962 memdc.DrawLine( 5, 42, 50, 42 );
82ea63e6 963 memdc.SelectObject( wxNullBitmap );
176fe5af 964
408b4168 965 if (mono.Ok())
82ea63e6 966 {
3af706cc
VZ
967 int x = 300, y = 1900;
968
969 dc.DrawText( _T("Mono bitmap"), x, y );
970 dc.DrawText( _T("(red on green)"), x, y + 15 );
a60b1f5d
MB
971 dc.SetTextForeground( wxT("RED") );
972 dc.SetTextBackground( wxT("GREEN") );
3af706cc 973 dc.DrawBitmap( mono, x, y + 30 );
408b4168 974
19bc1514 975 dc.SetTextForeground( *wxBLACK );
3af706cc
VZ
976 dc.DrawText( _T("After wxImage conversion"), x + 120, y );
977 dc.DrawText( _T("(red on white)"), x + 120, y + 15 );
a60b1f5d 978 dc.SetTextForeground( wxT("RED") );
a8d4e3ac 979 wxImage i = mono.ConvertToImage();
408b4168
VZ
980 i.SetMaskColour( 255,255,255 );
981 i.Replace( 0,0,0,
82ea63e6
RR
982 wxRED_PEN->GetColour().Red(),
983 wxRED_PEN->GetColour().Green(),
984 wxRED_PEN->GetColour().Blue() );
3af706cc 985 dc.DrawBitmap( wxBitmap(i), x + 120, y + 30, true );
19bc1514 986 dc.SetTextForeground( *wxBLACK );
bea56879
VZ
987 }
988
f9ebac93
RR
989 // For testing transparency
990 dc.SetBrush( *wxRED_BRUSH );
991 dc.DrawRectangle( 20, 2220, 560, 68 );
925e9792 992
f9ebac93 993 dc.DrawText(_T("XPM bitmap"), 30, 2230 );
bea56879 994 if ( m_bmpSmileXpm.Ok() )
47f797bd 995 dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, true);
bea56879 996
f9ebac93 997 dc.DrawText(_T("XPM icon"), 110, 2230 );
bea56879 998 if ( m_iconSmileXpm.Ok() )
f9ebac93 999 dc.DrawIcon(m_iconSmileXpm, 110, 2250);
925e9792
WS
1000
1001 // testing icon -> bitmap conversion
f9ebac93 1002 wxBitmap to_blit( m_iconSmileXpm );
3ef37e7f
JS
1003 if (to_blit.Ok())
1004 {
1005 dc.DrawText( _T("SubBitmap"), 170, 2230 );
1006 wxBitmap sub = to_blit.GetSubBitmap( wxRect(0,0,15,15) );
1007 if (sub.Ok())
47f797bd 1008 dc.DrawBitmap( sub, 170, 2250, true );
3ef37e7f
JS
1009
1010 dc.DrawText( _T("Enlarged"), 250, 2230 );
1011 dc.SetUserScale( 1.5, 1.5 );
47f797bd 1012 dc.DrawBitmap( to_blit, (int)(250/1.5), (int)(2250/1.5), true );
3ef37e7f 1013 dc.SetUserScale( 2, 2 );
47f797bd 1014 dc.DrawBitmap( to_blit, (int)(300/2), (int)(2250/2), true );
3ef37e7f 1015 dc.SetUserScale( 1.0, 1.0 );
925e9792 1016
3ef37e7f
JS
1017 dc.DrawText( _T("Blit"), 400, 2230);
1018 wxMemoryDC blit_dc;
1019 blit_dc.SelectObject( to_blit );
47f797bd 1020 dc.Blit( 400, 2250, to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
3ef37e7f 1021 dc.SetUserScale( 1.5, 1.5 );
47f797bd 1022 dc.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
3ef37e7f 1023 dc.SetUserScale( 2, 2 );
47f797bd 1024 dc.Blit( (int)(500/2), (int)(2250/2), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
3ef37e7f
JS
1025 dc.SetUserScale( 1.0, 1.0 );
1026 }
bf504f98 1027
51b07644 1028 dc.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
bd981f27
VZ
1029 if (my_horse_ico32.Ok())
1030 dc.DrawBitmap( my_horse_ico32, 30, 2330, true );
bf504f98 1031
51b07644 1032 dc.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
bd981f27
VZ
1033 if (my_horse_ico16.Ok())
1034 dc.DrawBitmap( my_horse_ico16, 230, 2330, true );
51b07644
VZ
1035
1036 dc.DrawText( _T("ICO handler (best image)"), 430, 2290 );
bd981f27
VZ
1037 if (my_horse_ico.Ok())
1038 dc.DrawBitmap( my_horse_ico, 430, 2330, true );
bf504f98 1039
51b07644 1040 dc.DrawText( _T("CUR handler"), 30, 2390 );
bd981f27 1041 if (my_horse_cur.Ok())
51b07644 1042 {
bd981f27 1043 dc.DrawBitmap( my_horse_cur, 30, 2420, true );
bf504f98
VS
1044 dc.SetPen (*wxRED_PEN);
1045 dc.DrawLine (xH-10,yH,xH+10,yH);
1046 dc.DrawLine (xH,yH-10,xH,yH+10);
51b07644 1047 }
bd981f27 1048
2b5f62a0 1049 dc.DrawText( _T("ANI handler"), 230, 2390 );
bd981f27
VZ
1050 for ( int i=0; i < m_ani_images; i++ )
1051 {
2b5f62a0
VZ
1052 if (my_horse_ani[i].Ok())
1053 {
47f797bd 1054 dc.DrawBitmap( my_horse_ani[i], 230 + i * 2 * my_horse_ani[i].GetWidth() , 2420, true );
2b5f62a0 1055 }
bd981f27 1056 }
9390a202 1057}
1d5b7a0b 1058
9390a202
RR
1059void MyCanvas::CreateAntiAliasedBitmap()
1060{
1061 wxBitmap bitmap( 300, 300 );
a91b47e8 1062
9390a202 1063 wxMemoryDC dc;
a91b47e8 1064
9390a202 1065 dc.SelectObject( bitmap );
a91b47e8 1066
9390a202 1067 dc.Clear();
f048e32f 1068
6e47faf1 1069 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
a60b1f5d 1070 dc.SetTextForeground( wxT("RED") );
66df4ec6
VZ
1071 dc.DrawText( _T("This is anti-aliased Text."), 20, 5 );
1072 dc.DrawText( _T("And a Rectangle."), 20, 45 );
f048e32f 1073
9390a202 1074 dc.SetBrush( *wxRED_BRUSH );
91b8de8d 1075 dc.SetPen( *wxTRANSPARENT_PEN );
66df4ec6 1076 dc.DrawRoundedRectangle( 20, 85, 200, 180, 20 );
f048e32f 1077
a8d4e3ac 1078 wxImage original= bitmap.ConvertToImage();
9390a202 1079 wxImage anti( 150, 150 );
a91b47e8 1080
9390a202 1081 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
f048e32f 1082
9390a202
RR
1083 for (int y = 1; y < 149; y++)
1084 for (int x = 1; x < 149; x++)
1085 {
1086 int red = original.GetRed( x*2, y*2 ) +
f048e32f
VZ
1087 original.GetRed( x*2-1, y*2 ) +
1088 original.GetRed( x*2, y*2+1 ) +
9390a202 1089 original.GetRed( x*2+1, y*2+1 );
f048e32f
VZ
1090 red = red/4;
1091
9390a202 1092 int green = original.GetGreen( x*2, y*2 ) +
f048e32f
VZ
1093 original.GetGreen( x*2-1, y*2 ) +
1094 original.GetGreen( x*2, y*2+1 ) +
9390a202 1095 original.GetGreen( x*2+1, y*2+1 );
f048e32f
VZ
1096 green = green/4;
1097
9390a202 1098 int blue = original.GetBlue( x*2, y*2 ) +
f048e32f
VZ
1099 original.GetBlue( x*2-1, y*2 ) +
1100 original.GetBlue( x*2, y*2+1 ) +
9390a202 1101 original.GetBlue( x*2+1, y*2+1 );
f048e32f 1102 blue = blue/4;
925e9792 1103 anti.SetRGB( x, y, (unsigned char)red, (unsigned char)green, (unsigned char)blue );
9390a202 1104 }
bd981f27 1105 my_anti = wxBitmap(anti);
01111366
RR
1106}
1107
1108// MyFrame
1109
b3f04dc2
VZ
1110enum
1111{
91b07357
JS
1112 ID_QUIT = wxID_EXIT,
1113 ID_ABOUT = wxID_ABOUT,
1114 ID_NEW = 100,
37ba70a5 1115 ID_INFO,
95706ac1
VZ
1116 ID_SHOWRAW,
1117 ID_SHOWTHUMBNAIL
b3f04dc2 1118};
01111366
RR
1119
1120IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
1121
37ba70a5 1122BEGIN_EVENT_TABLE(MyFrame, wxFrame)
01111366
RR
1123 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
1124 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
37ba70a5
VZ
1125 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
1126 EVT_MENU (ID_INFO, MyFrame::OnImageInfo)
95706ac1 1127 EVT_MENU (ID_SHOWTHUMBNAIL, MyFrame::OnThumbnail)
b3f04dc2 1128#ifdef wxHAVE_RAW_BITMAP
37ba70a5 1129 EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap)
b3f04dc2 1130#endif
da9df1f5 1131
dd38c875 1132#if wxUSE_CLIPBOARD
da9df1f5
VZ
1133 EVT_MENU(wxID_COPY, MyFrame::OnCopy)
1134 EVT_MENU(wxID_PASTE, MyFrame::OnPaste)
1135#endif // wxUSE_CLIPBOARD
01111366
RR
1136END_EVENT_TABLE()
1137
1d5b7a0b 1138MyFrame::MyFrame()
47f797bd 1139 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxImage sample"),
234fedd2 1140 wxPoint(20, 20), wxSize(950, 700) )
01111366 1141{
da9df1f5
VZ
1142 wxMenuBar *menu_bar = new wxMenuBar();
1143
1144 wxMenu *menuImage = new wxMenu;
1145 menuImage->Append( ID_NEW, _T("&Show any image...\tCtrl-O"));
37ba70a5 1146 menuImage->Append( ID_INFO, _T("Show image &information...\tCtrl-I"));
b3f04dc2 1147#ifdef wxHAVE_RAW_BITMAP
37ba70a5 1148 menuImage->AppendSeparator();
da9df1f5 1149 menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R"));
b3f04dc2 1150#endif
da9df1f5 1151 menuImage->AppendSeparator();
95706ac1
VZ
1152 menuImage->Append( ID_SHOWTHUMBNAIL, _T("Test &thumbnail...\tCtrl-T"),
1153 "Test scaling the image during load (try with JPEG)");
1154 menuImage->AppendSeparator();
da9df1f5
VZ
1155 menuImage->Append( ID_ABOUT, _T("&About..."));
1156 menuImage->AppendSeparator();
1157 menuImage->Append( ID_QUIT, _T("E&xit\tCtrl-Q"));
1158 menu_bar->Append(menuImage, _T("&Image"));
1159
dd38c875 1160#if wxUSE_CLIPBOARD
da9df1f5
VZ
1161 wxMenu *menuClipboard = new wxMenu;
1162 menuClipboard->Append(wxID_COPY, _T("&Copy test image\tCtrl-C"));
1163 menuClipboard->Append(wxID_PASTE, _T("&Paste image\tCtrl-V"));
1164 menu_bar->Append(menuClipboard, _T("&Clipboard"));
1165#endif // wxUSE_CLIPBOARD
3d05544e 1166
01111366 1167 SetMenuBar( menu_bar );
1d5b7a0b 1168
8520f137 1169#if wxUSE_STATUSBAR
917e533b
RR
1170 CreateStatusBar(2);
1171 int widths[] = { -1, 100 };
1172 SetStatusWidths( 2, widths );
8520f137 1173#endif // wxUSE_STATUSBAR
1d5b7a0b 1174
47f797bd 1175 m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) );
cbd4be25 1176
978d3d36
VZ
1177 // 500 width * 2750 height
1178 m_canvas->SetScrollbars( 10, 10, 50, 275 );
01111366
RR
1179}
1180
1181void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
1182{
47f797bd 1183 Close( true );
01111366
RR
1184}
1185
1186void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
1187{
51b07644
VZ
1188 (void)wxMessageBox( _T("wxImage demo\n")
1189 _T("Robert Roebling (c) 1998,2000"),
1190 _T("About wxImage Demo"), wxICON_INFORMATION | wxOK );
fb1585ae
RR
1191}
1192
37ba70a5 1193wxString MyFrame::LoadUserImage(wxImage& image)
ab0f0386 1194{
37ba70a5
VZ
1195 wxString filename;
1196
71307412 1197#if wxUSE_FILEDLG
37ba70a5
VZ
1198 filename = wxFileSelector(_T("Select image file"));
1199 if ( !filename.empty() )
1200 {
1201 if ( !image.LoadFile(filename) )
1202 {
1203 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
f6bcfd97 1204
37ba70a5
VZ
1205 return wxEmptyString;
1206 }
1207 }
1208#endif // wxUSE_FILEDLG
1209
1210 return filename;
1211}
1212
1213void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
1214{
f6bcfd97 1215 wxImage image;
37ba70a5
VZ
1216 wxString filename = LoadUserImage(image);
1217 if ( !filename.empty() )
1218 (new MyImageFrame(this, filename, wxBitmap(image)))->Show();
1219}
1220
1221void MyFrame::OnImageInfo( wxCommandEvent &WXUNUSED(event) )
1222{
1223 wxImage image;
1224 if ( !LoadUserImage(image).empty() )
f6bcfd97 1225 {
37ba70a5
VZ
1226 // TODO: show more information about the file
1227 wxString info = wxString::Format("Image size: %dx%d",
1228 image.GetWidth(),
1229 image.GetHeight());
1230
1231 int xres = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX),
1232 yres = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY);
1233 if ( xres || yres )
1234 {
1235 info += wxString::Format("\nResolution: %dx%d", xres, yres);
1236 switch ( image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT) )
1237 {
1238 default:
1239 wxFAIL_MSG( "unknown image resolution units" );
1240 // fall through
f6bcfd97 1241
37ba70a5
VZ
1242 case wxIMAGE_RESOLUTION_NONE:
1243 info += " in default units";
1244 break;
f6bcfd97 1245
37ba70a5
VZ
1246 case wxIMAGE_RESOLUTION_INCHES:
1247 info += " in";
1248 break;
1249
1250 case wxIMAGE_RESOLUTION_CM:
1251 info += " cm";
1252 break;
1253 }
1254 }
1255
1256 wxLogMessage("%s", info);
1257 }
ab0f0386
VZ
1258}
1259
b3f04dc2
VZ
1260#ifdef wxHAVE_RAW_BITMAP
1261
481721e8 1262void MyFrame::OnTestRawBitmap( wxCommandEvent &WXUNUSED(event) )
b3f04dc2
VZ
1263{
1264 (new MyRawBitmapFrame(this))->Show();
1265}
1266
1267#endif // wxHAVE_RAW_BITMAP
1268
dd38c875 1269#if wxUSE_CLIPBOARD
da9df1f5
VZ
1270
1271void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
1272{
1273 wxBitmapDataObject *dobjBmp = new wxBitmapDataObject;
bd981f27 1274 dobjBmp->SetBitmap(m_canvas->my_horse_png);
da9df1f5 1275
dd38c875
MB
1276 wxTheClipboard->Open();
1277
da9df1f5
VZ
1278 if ( !wxTheClipboard->SetData(dobjBmp) )
1279 {
1280 wxLogError(_T("Failed to copy bitmap to clipboard"));
1281 }
dd38c875
MB
1282
1283 wxTheClipboard->Close();
da9df1f5
VZ
1284}
1285
1286void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
1287{
1288 wxBitmapDataObject dobjBmp;
dd38c875
MB
1289
1290 wxTheClipboard->Open();
da9df1f5
VZ
1291 if ( !wxTheClipboard->GetData(dobjBmp) )
1292 {
1293 wxLogMessage(_T("No bitmap data in the clipboard"));
1294 }
1295 else
1296 {
1d8acb7d 1297 (new MyImageFrame(this, _T("Clipboard"), dobjBmp.GetBitmap()))->Show();
da9df1f5 1298 }
dd38c875 1299 wxTheClipboard->Close();
da9df1f5
VZ
1300}
1301
1302#endif // wxUSE_CLIPBOARD
1303
01111366
RR
1304//-----------------------------------------------------------------------------
1305// MyApp
1306//-----------------------------------------------------------------------------
1307
1d5b7a0b 1308bool MyApp::OnInit()
01111366 1309{
45e6e6f8
VZ
1310 if ( !wxApp::OnInit() )
1311 return false;
1312
3af706cc 1313 wxInitAllImageHandlers();
bf504f98 1314
3af706cc
VZ
1315 wxFrame *frame = new MyFrame();
1316 frame->Show( true );
1d5b7a0b 1317
3af706cc 1318 return true;
01111366 1319}
95706ac1
VZ
1320
1321void MyFrame::OnThumbnail( wxCommandEvent &WXUNUSED(event) )
1322{
1323#if wxUSE_FILEDLG
1324 wxString filename = wxFileSelector(_T("Select image file"));
1325 if ( filename.empty() )
1326 return;
1327
1328 static const int THUMBNAIL_WIDTH = 320;
1329 static const int THUMBNAIL_HEIGHT = 240;
1330
1331 wxImage image;
1332 image.SetOption(wxIMAGE_OPTION_MAX_WIDTH, THUMBNAIL_WIDTH);
1333 image.SetOption(wxIMAGE_OPTION_MAX_HEIGHT, THUMBNAIL_HEIGHT);
1334
1335 wxStopWatch sw;
1336 if ( !image.LoadFile(filename) )
1337 {
1338 wxLogError(_T("Couldn't load image from '%s'."), filename.c_str());
1339 return;
1340 }
1341
1342 const long loadTime = sw.Time();
1343
1344 MyImageFrame * const
1345 frame = new MyImageFrame(this, filename, wxBitmap(image));
1346 frame->Show();
1347 wxLogStatus(frame, "Loaded \"%s\" in %ldms", filename, loadTime);
1348#else
1349 wxLogError( _T("Couldn't create file selector dialog") );
1350 return;
1351#endif // wxUSE_FILEDLG
1352}
1353