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