]> git.saurik.com Git - wxWidgets.git/blame - samples/image/canvas.cpp
Add wxBitmap::NewFromPNGData() for creating bitmaps from PNG data.
[wxWidgets.git] / samples / image / canvas.cpp
CommitLineData
09ddabf7
FM
1///////////////////////////////////////////////////////////////////////////////
2// Name: samples/image/canvas.cpp
3// Purpose: sample showing operations with wxImage
4// Author: Robert Roebling
5// Modified by: Francesco Montorsi
6// Created: 1998
7// RCS-ID: $Id$
8// Copyright: (c) 1998-2005 Robert Roebling
526954c5 9// Licence: wxWindows licence
09ddabf7
FM
10///////////////////////////////////////////////////////////////////////////////
11
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
20 #include "wx/wx.h"
21#endif
22
23#include "wx/image.h"
24#include "wx/file.h"
25#include "wx/filename.h"
26#include "wx/mstream.h"
27#include "wx/wfstream.h"
28#include "wx/quantize.h"
29#include "wx/stopwatch.h"
30
31#if wxUSE_CLIPBOARD
32 #include "wx/dataobj.h"
33 #include "wx/clipbrd.h"
34#endif // wxUSE_CLIPBOARD
35
36#include "smile.xbm"
37#include "smile.xpm"
38
39#include "canvas.h"
40
41
42//-----------------------------------------------------------------------------
43// MyCanvas
44//-----------------------------------------------------------------------------
45
46BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
47 EVT_PAINT(MyCanvas::OnPaint)
48END_EVENT_TABLE()
49
50MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
51 const wxPoint &pos, const wxSize &size )
52 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
53 , m_bmpSmileXpm(smile_xpm)
54 , m_iconSmileXpm(smile_xpm)
55{
56 my_horse_ani = NULL;
57 m_ani_images = 0 ;
58
59 SetBackgroundColour(* wxWHITE);
60
61 wxBitmap bitmap( 100, 100 );
62
63 wxMemoryDC dc;
64 dc.SelectObject( bitmap );
65 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
66 dc.SetPen( *wxBLACK_PEN );
67 dc.DrawRectangle( 0, 0, 100, 100 );
68 dc.SetBrush( *wxWHITE_BRUSH );
69 dc.DrawRectangle( 20, 20, 60, 60 );
70 dc.SelectObject( wxNullBitmap );
71
72 // try to find the directory with our images
73 wxString dir;
74 if ( wxFile::Exists(wxT("./horse.png")) )
75 dir = wxT("./");
76 else if ( wxFile::Exists(wxT("../horse.png")) )
77 dir = wxT("../");
78 else
79 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
80
81 wxImage image = bitmap.ConvertToImage();
82
83#if wxUSE_LIBPNG
9a83f860 84 if ( !image.SaveFile( dir + wxT("test.png"), wxBITMAP_TYPE_PNG ))
43b2d5e7 85 {
09ddabf7 86 wxLogError(wxT("Can't save file"));
43b2d5e7 87 }
09ddabf7
FM
88
89 image.Destroy();
90
9a83f860 91 if ( image.LoadFile( dir + wxT("test.png") ) )
09ddabf7
FM
92 my_square = wxBitmap( image );
93
94 image.Destroy();
95
9a83f860 96 if ( !image.LoadFile( dir + wxT("horse.png")) )
43b2d5e7 97 {
09ddabf7 98 wxLogError(wxT("Can't load PNG image"));
43b2d5e7 99 }
09ddabf7 100 else
43b2d5e7 101 {
09ddabf7 102 my_horse_png = wxBitmap( image );
43b2d5e7 103 }
09ddabf7 104
9a83f860 105 if ( !image.LoadFile( dir + wxT("toucan.png")) )
43b2d5e7 106 {
09ddabf7 107 wxLogError(wxT("Can't load PNG image"));
43b2d5e7 108 }
09ddabf7 109 else
43b2d5e7 110 {
09ddabf7 111 my_toucan = wxBitmap(image);
43b2d5e7 112 }
09ddabf7
FM
113
114 my_toucan_flipped_horiz = wxBitmap(image.Mirror(true));
115 my_toucan_flipped_vert = wxBitmap(image.Mirror(false));
116 my_toucan_flipped_both = wxBitmap(image.Mirror(true).Mirror(false));
117 my_toucan_grey = wxBitmap(image.ConvertToGreyscale());
118 my_toucan_head = wxBitmap(image.GetSubImage(wxRect(40, 7, 80, 60)));
119 my_toucan_scaled_normal = wxBitmap(image.Scale(110,90,wxIMAGE_QUALITY_NORMAL));
120 my_toucan_scaled_high = wxBitmap(image.Scale(110,90,wxIMAGE_QUALITY_HIGH));
121 my_toucan_blur = wxBitmap(image.Blur(10));
122
123#endif // wxUSE_LIBPNG
124
125#if wxUSE_LIBJPEG
126 image.Destroy();
127
9a83f860 128 if ( !image.LoadFile( dir + wxT("horse.jpg")) )
43b2d5e7 129 {
09ddabf7 130 wxLogError(wxT("Can't load JPG image"));
43b2d5e7 131 }
09ddabf7
FM
132 else
133 {
134 my_horse_jpeg = wxBitmap( image );
135
136 // Colorize by rotating green hue to red
137 wxImage::HSVValue greenHSV = wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0));
138 wxImage::HSVValue redHSV = wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0));
139 image.RotateHue(redHSV.hue - greenHSV.hue);
140 colorized_horse_jpeg = wxBitmap( image );
141 }
142
9a83f860 143 if ( !image.LoadFile( dir + wxT("cmyk.jpg")) )
43b2d5e7 144 {
9a83f860 145 wxLogError(wxT("Can't load CMYK JPG image"));
43b2d5e7 146 }
09ddabf7 147 else
43b2d5e7 148 {
09ddabf7 149 my_cmyk_jpeg = wxBitmap(image);
43b2d5e7 150 }
09ddabf7
FM
151#endif // wxUSE_LIBJPEG
152
153#if wxUSE_GIF
154 image.Destroy();
155
9a83f860 156 if ( !image.LoadFile( dir + wxT("horse.gif" )) )
43b2d5e7 157 {
09ddabf7 158 wxLogError(wxT("Can't load GIF image"));
43b2d5e7 159 }
09ddabf7 160 else
43b2d5e7 161 {
09ddabf7 162 my_horse_gif = wxBitmap( image );
43b2d5e7 163 }
09ddabf7
FM
164#endif
165
166#if wxUSE_PCX
167 image.Destroy();
168
9a83f860 169 if ( !image.LoadFile( dir + wxT("horse.pcx"), wxBITMAP_TYPE_PCX ) )
43b2d5e7 170 {
09ddabf7 171 wxLogError(wxT("Can't load PCX image"));
43b2d5e7 172 }
09ddabf7 173 else
43b2d5e7 174 {
09ddabf7 175 my_horse_pcx = wxBitmap( image );
43b2d5e7 176 }
09ddabf7
FM
177#endif
178
179 image.Destroy();
180
9a83f860 181 if ( !image.LoadFile( dir + wxT("horse.bmp"), wxBITMAP_TYPE_BMP ) )
43b2d5e7 182 {
09ddabf7 183 wxLogError(wxT("Can't load BMP image"));
43b2d5e7 184 }
09ddabf7 185 else
43b2d5e7 186 {
09ddabf7 187 my_horse_bmp = wxBitmap( image );
43b2d5e7 188 }
09ddabf7
FM
189
190#if wxUSE_XPM
191 image.Destroy();
192
9a83f860 193 if ( !image.LoadFile( dir + wxT("horse.xpm"), wxBITMAP_TYPE_XPM ) )
43b2d5e7 194 {
09ddabf7 195 wxLogError(wxT("Can't load XPM image"));
43b2d5e7 196 }
09ddabf7 197 else
43b2d5e7 198 {
09ddabf7 199 my_horse_xpm = wxBitmap( image );
43b2d5e7 200 }
09ddabf7 201
9a83f860 202 if ( !image.SaveFile( dir + wxT("test.xpm"), wxBITMAP_TYPE_XPM ))
43b2d5e7 203 {
09ddabf7 204 wxLogError(wxT("Can't save file"));
43b2d5e7 205 }
09ddabf7
FM
206#endif
207
208#if wxUSE_PNM
209 image.Destroy();
210
9a83f860 211 if ( !image.LoadFile( dir + wxT("horse.pnm"), wxBITMAP_TYPE_PNM ) )
43b2d5e7 212 {
09ddabf7 213 wxLogError(wxT("Can't load PNM image"));
43b2d5e7 214 }
09ddabf7 215 else
43b2d5e7 216 {
09ddabf7 217 my_horse_pnm = wxBitmap( image );
43b2d5e7 218 }
09ddabf7
FM
219
220 image.Destroy();
221
9a83f860 222 if ( !image.LoadFile( dir + wxT("horse_ag.pnm"), wxBITMAP_TYPE_PNM ) )
43b2d5e7 223 {
09ddabf7 224 wxLogError(wxT("Can't load PNM image"));
43b2d5e7 225 }
09ddabf7 226 else
43b2d5e7 227 {
09ddabf7 228 my_horse_asciigrey_pnm = wxBitmap( image );
43b2d5e7 229 }
09ddabf7
FM
230
231 image.Destroy();
232
9a83f860 233 if ( !image.LoadFile( dir + wxT("horse_rg.pnm"), wxBITMAP_TYPE_PNM ) )
43b2d5e7 234 {
09ddabf7 235 wxLogError(wxT("Can't load PNM image"));
43b2d5e7 236 }
09ddabf7 237 else
43b2d5e7 238 {
09ddabf7 239 my_horse_rawgrey_pnm = wxBitmap( image );
43b2d5e7 240 }
09ddabf7
FM
241#endif
242
243#if wxUSE_LIBTIFF
244 image.Destroy();
245
4ca8531f 246 if ( !image.LoadFile( dir + wxT("horse.tif"), wxBITMAP_TYPE_TIFF ) )
43b2d5e7 247 {
09ddabf7 248 wxLogError(wxT("Can't load TIFF image"));
43b2d5e7 249 }
09ddabf7 250 else
43b2d5e7 251 {
09ddabf7 252 my_horse_tiff = wxBitmap( image );
43b2d5e7 253 }
09ddabf7
FM
254#endif
255
256#if wxUSE_LIBTIFF
257 image.Destroy();
258
9a83f860 259 if ( !image.LoadFile( dir + wxT("horse.tga"), wxBITMAP_TYPE_TGA ) )
43b2d5e7 260 {
09ddabf7 261 wxLogError(wxT("Can't load TGA image"));
43b2d5e7 262 }
09ddabf7 263 else
43b2d5e7 264 {
09ddabf7 265 my_horse_tga = wxBitmap( image );
43b2d5e7 266 }
09ddabf7
FM
267#endif
268
269 CreateAntiAliasedBitmap();
270
271 my_smile_xbm = wxBitmap( (const char*)smile_bits, smile_width,
272 smile_height, 1 );
273
274 // demonstrates XPM automatically using the mask when saving
a1b806b9 275 if ( m_bmpSmileXpm.IsOk() )
9a83f860 276 m_bmpSmileXpm.SaveFile(wxT("saved.xpm"), wxBITMAP_TYPE_XPM);
09ddabf7
FM
277
278#if wxUSE_ICO_CUR
279 image.Destroy();
280
9a83f860 281 if ( !image.LoadFile( dir + wxT("horse.ico"), wxBITMAP_TYPE_ICO, 0 ) )
43b2d5e7 282 {
09ddabf7 283 wxLogError(wxT("Can't load first ICO image"));
43b2d5e7 284 }
09ddabf7 285 else
43b2d5e7 286 {
09ddabf7 287 my_horse_ico32 = wxBitmap( image );
43b2d5e7 288 }
09ddabf7
FM
289
290 image.Destroy();
291
9a83f860 292 if ( !image.LoadFile( dir + wxT("horse.ico"), wxBITMAP_TYPE_ICO, 1 ) )
43b2d5e7 293 {
09ddabf7 294 wxLogError(wxT("Can't load second ICO image"));
43b2d5e7 295 }
09ddabf7 296 else
43b2d5e7 297 {
09ddabf7 298 my_horse_ico16 = wxBitmap( image );
43b2d5e7 299 }
09ddabf7
FM
300
301 image.Destroy();
302
9a83f860 303 if ( !image.LoadFile( dir + wxT("horse.ico") ) )
43b2d5e7 304 {
09ddabf7 305 wxLogError(wxT("Can't load best ICO image"));
43b2d5e7 306 }
09ddabf7 307 else
43b2d5e7 308 {
09ddabf7 309 my_horse_ico = wxBitmap( image );
43b2d5e7 310 }
09ddabf7
FM
311
312 image.Destroy();
313
9a83f860 314 if ( !image.LoadFile( dir + wxT("horse.cur"), wxBITMAP_TYPE_CUR ) )
43b2d5e7 315 {
09ddabf7 316 wxLogError(wxT("Can't load best ICO image"));
43b2d5e7 317 }
09ddabf7
FM
318 else
319 {
320 my_horse_cur = wxBitmap( image );
321 xH = 30 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) ;
322 yH = 2420 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ;
323 }
324
9a83f860 325 m_ani_images = wxImage::GetImageCount ( dir + wxT("horse3.ani"), wxBITMAP_TYPE_ANI );
09ddabf7 326 if (m_ani_images==0)
43b2d5e7 327 {
09ddabf7 328 wxLogError(wxT("No ANI-format images found"));
43b2d5e7 329 }
09ddabf7 330 else
43b2d5e7 331 {
09ddabf7 332 my_horse_ani = new wxBitmap [m_ani_images];
43b2d5e7 333 }
09ddabf7
FM
334
335 int i;
336 for (i=0; i < m_ani_images; i++)
337 {
338 image.Destroy();
9a83f860 339 if (!image.LoadFile( dir + wxT("horse3.ani"), wxBITMAP_TYPE_ANI, i ))
09ddabf7
FM
340 {
341 wxString tmp = wxT("Can't load image number ");
342 tmp << i ;
343 wxLogError(tmp);
344 }
345 else
346 my_horse_ani [i] = wxBitmap( image );
347 }
348#endif // wxUSE_ICO_CUR
349
350
351 image.Destroy();
352
353 // test image loading from stream
9a83f860 354 wxFile file(dir + wxT("horse.bmp"));
09ddabf7
FM
355 if ( file.IsOpened() )
356 {
357 wxFileOffset len = file.Length();
358 size_t dataSize = (size_t)len;
359 void *data = malloc(dataSize);
360 if ( file.Read(data, dataSize) != len )
43b2d5e7 361 {
9a83f860 362 wxLogError(wxT("Reading bitmap file failed"));
43b2d5e7 363 }
09ddabf7
FM
364 else
365 {
366 wxMemoryInputStream mis(data, dataSize);
367 if ( !image.LoadFile(mis) )
43b2d5e7 368 {
09ddabf7 369 wxLogError(wxT("Can't load BMP image from stream"));
43b2d5e7 370 }
09ddabf7 371 else
43b2d5e7 372 {
09ddabf7 373 my_horse_bmp2 = wxBitmap( image );
43b2d5e7 374 }
09ddabf7
FM
375 }
376
377 free(data);
378 }
379}
380
381MyCanvas::~MyCanvas()
382{
383 delete [] my_horse_ani;
384}
385
386void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
387{
388 wxPaintDC dc( this );
389 PrepareDC( dc );
390
9a83f860 391 dc.DrawText( wxT("Loaded image"), 30, 10 );
a1b806b9 392 if (my_square.IsOk())
09ddabf7
FM
393 dc.DrawBitmap( my_square, 30, 30 );
394
9a83f860 395 dc.DrawText( wxT("Drawn directly"), 150, 10 );
09ddabf7
FM
396 dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) );
397 dc.SetPen( *wxBLACK_PEN );
398 dc.DrawRectangle( 150, 30, 100, 100 );
399 dc.SetBrush( *wxWHITE_BRUSH );
400 dc.DrawRectangle( 170, 50, 60, 60 );
401
a1b806b9 402 if (my_anti.IsOk())
09ddabf7
FM
403 dc.DrawBitmap( my_anti, 280, 30 );
404
9a83f860 405 dc.DrawText( wxT("PNG handler"), 30, 135 );
a1b806b9 406 if (my_horse_png.IsOk())
09ddabf7
FM
407 {
408 dc.DrawBitmap( my_horse_png, 30, 150 );
409 wxRect rect(0,0,100,100);
410 wxBitmap sub( my_horse_png.GetSubBitmap(rect) );
9a83f860 411 dc.DrawText( wxT("GetSubBitmap()"), 280, 175 );
09ddabf7
FM
412 dc.DrawBitmap( sub, 280, 195 );
413 }
414
9a83f860 415 dc.DrawText( wxT("JPEG handler"), 30, 365 );
a1b806b9 416 if (my_horse_jpeg.IsOk())
09ddabf7
FM
417 dc.DrawBitmap( my_horse_jpeg, 30, 380 );
418
9a83f860 419 dc.DrawText( wxT("Green rotated to red"), 280, 365 );
a1b806b9 420 if (colorized_horse_jpeg.IsOk())
09ddabf7
FM
421 dc.DrawBitmap( colorized_horse_jpeg, 280, 380 );
422
9a83f860 423 dc.DrawText( wxT("CMYK JPEG image"), 530, 365 );
a1b806b9 424 if (my_cmyk_jpeg.IsOk())
09ddabf7
FM
425 dc.DrawBitmap( my_cmyk_jpeg, 530, 380 );
426
9a83f860 427 dc.DrawText( wxT("GIF handler"), 30, 595 );
a1b806b9 428 if (my_horse_gif.IsOk())
09ddabf7
FM
429 dc.DrawBitmap( my_horse_gif, 30, 610 );
430
9a83f860 431 dc.DrawText( wxT("PCX handler"), 30, 825 );
a1b806b9 432 if (my_horse_pcx.IsOk())
09ddabf7
FM
433 dc.DrawBitmap( my_horse_pcx, 30, 840 );
434
9a83f860 435 dc.DrawText( wxT("BMP handler"), 30, 1055 );
a1b806b9 436 if (my_horse_bmp.IsOk())
09ddabf7
FM
437 dc.DrawBitmap( my_horse_bmp, 30, 1070 );
438
9a83f860 439 dc.DrawText( wxT("BMP read from memory"), 280, 1055 );
a1b806b9 440 if (my_horse_bmp2.IsOk())
09ddabf7
FM
441 dc.DrawBitmap( my_horse_bmp2, 280, 1070 );
442
9a83f860 443 dc.DrawText( wxT("PNM handler"), 30, 1285 );
a1b806b9 444 if (my_horse_pnm.IsOk())
09ddabf7
FM
445 dc.DrawBitmap( my_horse_pnm, 30, 1300 );
446
9a83f860 447 dc.DrawText( wxT("PNM handler (ascii grey)"), 280, 1285 );
a1b806b9 448 if (my_horse_asciigrey_pnm.IsOk())
09ddabf7
FM
449 dc.DrawBitmap( my_horse_asciigrey_pnm, 280, 1300 );
450
9a83f860 451 dc.DrawText( wxT("PNM handler (raw grey)"), 530, 1285 );
a1b806b9 452 if (my_horse_rawgrey_pnm.IsOk())
09ddabf7
FM
453 dc.DrawBitmap( my_horse_rawgrey_pnm, 530, 1300 );
454
9a83f860 455 dc.DrawText( wxT("TIFF handler"), 30, 1515 );
a1b806b9 456 if (my_horse_tiff.IsOk())
09ddabf7
FM
457 dc.DrawBitmap( my_horse_tiff, 30, 1530 );
458
9a83f860 459 dc.DrawText( wxT("TGA handler"), 30, 1745 );
a1b806b9 460 if (my_horse_tga.IsOk())
09ddabf7
FM
461 dc.DrawBitmap( my_horse_tga, 30, 1760 );
462
9a83f860 463 dc.DrawText( wxT("XPM handler"), 30, 1975 );
a1b806b9 464 if (my_horse_xpm.IsOk())
09ddabf7
FM
465 dc.DrawBitmap( my_horse_xpm, 30, 2000 );
466
467 // toucans
468 {
469 int x = 750, y = 10, yy = 170;
470
471 dc.DrawText(wxT("Original toucan"), x+50, y);
472 dc.DrawBitmap(my_toucan, x, y+15, true);
473 y += yy;
474 dc.DrawText(wxT("Flipped horizontally"), x+50, y);
475 dc.DrawBitmap(my_toucan_flipped_horiz, x, y+15, true);
476 y += yy;
477 dc.DrawText(wxT("Flipped vertically"), x+50, y);
478 dc.DrawBitmap(my_toucan_flipped_vert, x, y+15, true);
479 y += yy;
480 dc.DrawText(wxT("Flipped both h&v"), x+50, y);
481 dc.DrawBitmap(my_toucan_flipped_both, x, y+15, true);
482
483 y += yy;
484 dc.DrawText(wxT("In greyscale"), x+50, y);
485 dc.DrawBitmap(my_toucan_grey, x, y+15, true);
486
487 y += yy;
488 dc.DrawText(wxT("Toucan's head"), x+50, y);
489 dc.DrawBitmap(my_toucan_head, x, y+15, true);
490
491 y += yy;
492 dc.DrawText(wxT("Scaled with normal quality"), x+50, y);
493 dc.DrawBitmap(my_toucan_scaled_normal, x, y+15, true);
494
495 y += yy;
496 dc.DrawText(wxT("Scaled with high quality"), x+50, y);
497 dc.DrawBitmap(my_toucan_scaled_high, x, y+15, true);
498
499 y += yy;
500 dc.DrawText(wxT("Blured"), x+50, y);
501 dc.DrawBitmap(my_toucan_blur, x, y+15, true);
502 }
503
a1b806b9 504 if (my_smile_xbm.IsOk())
09ddabf7
FM
505 {
506 int x = 300, y = 1800;
507
9a83f860
VZ
508 dc.DrawText( wxT("XBM bitmap"), x, y );
509 dc.DrawText( wxT("(green on red)"), x, y + 15 );
510 dc.SetTextForeground( wxT("GREEN") );
511 dc.SetTextBackground( wxT("RED") );
09ddabf7
FM
512 dc.DrawBitmap( my_smile_xbm, x, y + 30 );
513
514 dc.SetTextForeground( *wxBLACK );
9a83f860
VZ
515 dc.DrawText( wxT("After wxImage conversion"), x + 120, y );
516 dc.DrawText( wxT("(red on white)"), x + 120, y + 15 );
09ddabf7
FM
517 dc.SetTextForeground( wxT("RED") );
518 wxImage i = my_smile_xbm.ConvertToImage();
519 i.SetMaskColour( 255, 255, 255 );
520 i.Replace( 0, 0, 0,
521 wxRED_PEN->GetColour().Red(),
522 wxRED_PEN->GetColour().Green(),
523 wxRED_PEN->GetColour().Blue() );
524 dc.DrawBitmap( wxBitmap(i), x + 120, y + 30, true );
525 dc.SetTextForeground( *wxBLACK );
526 }
527
528
529 wxBitmap mono( 60,50,1 );
530 wxMemoryDC memdc;
531 memdc.SelectObject( mono );
532 memdc.SetPen( *wxBLACK_PEN );
533 memdc.SetBrush( *wxWHITE_BRUSH );
534 memdc.DrawRectangle( 0,0,60,50 );
535 memdc.SetTextForeground( *wxBLACK );
536#ifndef __WXGTK20__
537 // I cannot convince GTK2 to draw into mono bitmaps
9a83f860 538 memdc.DrawText( wxT("Hi!"), 5, 5 );
09ddabf7
FM
539#endif
540 memdc.SetBrush( *wxBLACK_BRUSH );
541 memdc.DrawRectangle( 33,5,20,20 );
542 memdc.SetPen( *wxRED_PEN );
543 memdc.DrawLine( 5, 42, 50, 42 );
544 memdc.SelectObject( wxNullBitmap );
545
a1b806b9 546 if (mono.IsOk())
09ddabf7
FM
547 {
548 int x = 300, y = 1900;
549
9a83f860
VZ
550 dc.DrawText( wxT("Mono bitmap"), x, y );
551 dc.DrawText( wxT("(red on green)"), x, y + 15 );
09ddabf7
FM
552 dc.SetTextForeground( wxT("RED") );
553 dc.SetTextBackground( wxT("GREEN") );
554 dc.DrawBitmap( mono, x, y + 30 );
555
556 dc.SetTextForeground( *wxBLACK );
9a83f860
VZ
557 dc.DrawText( wxT("After wxImage conversion"), x + 120, y );
558 dc.DrawText( wxT("(red on white)"), x + 120, y + 15 );
09ddabf7
FM
559 dc.SetTextForeground( wxT("RED") );
560 wxImage i = mono.ConvertToImage();
561 i.SetMaskColour( 255,255,255 );
562 i.Replace( 0,0,0,
563 wxRED_PEN->GetColour().Red(),
564 wxRED_PEN->GetColour().Green(),
565 wxRED_PEN->GetColour().Blue() );
566 dc.DrawBitmap( wxBitmap(i), x + 120, y + 30, true );
567 dc.SetTextForeground( *wxBLACK );
568 }
569
570 // For testing transparency
571 dc.SetBrush( *wxRED_BRUSH );
572 dc.DrawRectangle( 20, 2220, 560, 68 );
573
9a83f860 574 dc.DrawText(wxT("XPM bitmap"), 30, 2230 );
a1b806b9 575 if ( m_bmpSmileXpm.IsOk() )
09ddabf7
FM
576 dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, true);
577
9a83f860 578 dc.DrawText(wxT("XPM icon"), 110, 2230 );
a1b806b9 579 if ( m_iconSmileXpm.IsOk() )
09ddabf7
FM
580 dc.DrawIcon(m_iconSmileXpm, 110, 2250);
581
582 // testing icon -> bitmap conversion
583 wxBitmap to_blit( m_iconSmileXpm );
a1b806b9 584 if (to_blit.IsOk())
09ddabf7 585 {
9a83f860 586 dc.DrawText( wxT("SubBitmap"), 170, 2230 );
09ddabf7 587 wxBitmap sub = to_blit.GetSubBitmap( wxRect(0,0,15,15) );
a1b806b9 588 if (sub.IsOk())
09ddabf7
FM
589 dc.DrawBitmap( sub, 170, 2250, true );
590
9a83f860 591 dc.DrawText( wxT("Enlarged"), 250, 2230 );
09ddabf7
FM
592 dc.SetUserScale( 1.5, 1.5 );
593 dc.DrawBitmap( to_blit, (int)(250/1.5), (int)(2250/1.5), true );
594 dc.SetUserScale( 2, 2 );
595 dc.DrawBitmap( to_blit, (int)(300/2), (int)(2250/2), true );
596 dc.SetUserScale( 1.0, 1.0 );
597
9a83f860 598 dc.DrawText( wxT("Blit"), 400, 2230);
09ddabf7
FM
599 wxMemoryDC blit_dc;
600 blit_dc.SelectObject( to_blit );
601 dc.Blit( 400, 2250, to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
602 dc.SetUserScale( 1.5, 1.5 );
603 dc.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
604 dc.SetUserScale( 2, 2 );
605 dc.Blit( (int)(500/2), (int)(2250/2), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true );
606 dc.SetUserScale( 1.0, 1.0 );
607 }
608
9a83f860 609 dc.DrawText( wxT("ICO handler (1st image)"), 30, 2290 );
a1b806b9 610 if (my_horse_ico32.IsOk())
09ddabf7
FM
611 dc.DrawBitmap( my_horse_ico32, 30, 2330, true );
612
9a83f860 613 dc.DrawText( wxT("ICO handler (2nd image)"), 230, 2290 );
a1b806b9 614 if (my_horse_ico16.IsOk())
09ddabf7
FM
615 dc.DrawBitmap( my_horse_ico16, 230, 2330, true );
616
9a83f860 617 dc.DrawText( wxT("ICO handler (best image)"), 430, 2290 );
a1b806b9 618 if (my_horse_ico.IsOk())
09ddabf7
FM
619 dc.DrawBitmap( my_horse_ico, 430, 2330, true );
620
9a83f860 621 dc.DrawText( wxT("CUR handler"), 30, 2390 );
a1b806b9 622 if (my_horse_cur.IsOk())
09ddabf7
FM
623 {
624 dc.DrawBitmap( my_horse_cur, 30, 2420, true );
625 dc.SetPen (*wxRED_PEN);
626 dc.DrawLine (xH-10,yH,xH+10,yH);
627 dc.DrawLine (xH,yH-10,xH,yH+10);
628 }
629
9a83f860 630 dc.DrawText( wxT("ANI handler"), 230, 2390 );
09ddabf7
FM
631 for ( int i=0; i < m_ani_images; i++ )
632 {
a1b806b9 633 if (my_horse_ani[i].IsOk())
09ddabf7
FM
634 {
635 dc.DrawBitmap( my_horse_ani[i], 230 + i * 2 * my_horse_ani[i].GetWidth() , 2420, true );
636 }
637 }
638}
639
640void MyCanvas::CreateAntiAliasedBitmap()
641{
642 wxBitmap bitmap( 300, 300 );
643
5ed0415e
VZ
644 {
645 wxMemoryDC dc(bitmap);
09ddabf7 646
5ed0415e 647 dc.Clear();
09ddabf7 648
5ed0415e
VZ
649 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) );
650 dc.SetTextForeground( wxT("RED") );
651 dc.DrawText( wxT("This is anti-aliased Text."), 20, 5 );
652 dc.DrawText( wxT("And a Rectangle."), 20, 45 );
09ddabf7 653
5ed0415e
VZ
654 dc.SetBrush( *wxRED_BRUSH );
655 dc.SetPen( *wxTRANSPARENT_PEN );
656 dc.DrawRoundedRectangle( 20, 85, 200, 180, 20 );
657 }
09ddabf7 658
5ed0415e 659 wxImage original = bitmap.ConvertToImage();
09ddabf7
FM
660 wxImage anti( 150, 150 );
661
662 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
663
664 for (int y = 1; y < 149; y++)
665 for (int x = 1; x < 149; x++)
666 {
667 int red = original.GetRed( x*2, y*2 ) +
668 original.GetRed( x*2-1, y*2 ) +
669 original.GetRed( x*2, y*2+1 ) +
670 original.GetRed( x*2+1, y*2+1 );
671 red = red/4;
672
673 int green = original.GetGreen( x*2, y*2 ) +
674 original.GetGreen( x*2-1, y*2 ) +
675 original.GetGreen( x*2, y*2+1 ) +
676 original.GetGreen( x*2+1, y*2+1 );
677 green = green/4;
678
679 int blue = original.GetBlue( x*2, y*2 ) +
680 original.GetBlue( x*2-1, y*2 ) +
681 original.GetBlue( x*2, y*2+1 ) +
682 original.GetBlue( x*2+1, y*2+1 );
683 blue = blue/4;
684 anti.SetRGB( x, y, (unsigned char)red, (unsigned char)green, (unsigned char)blue );
685 }
686
687 my_anti = wxBitmap(anti);
688}