1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: samples/image/canvas.cpp
3 // Purpose: sample showing operations with wxImage
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi
8 // Copyright: (c) 1998-2005 Robert Roebling
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.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"
32 #include "wx/dataobj.h"
33 #include "wx/clipbrd.h"
34 #endif // wxUSE_CLIPBOARD
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
47 EVT_PAINT(MyCanvas::OnPaint
)
50 MyCanvas::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
)
59 SetBackgroundColour(* wxWHITE
);
61 wxBitmap
bitmap( 100, 100 );
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
);
72 // try to find the directory with our images
74 if ( wxFile::Exists(wxT("./horse.png")) )
76 else if ( wxFile::Exists(wxT("../horse.png")) )
79 wxLogWarning(wxT("Can't find image files in either '.' or '..'!"));
81 wxImage image
= bitmap
.ConvertToImage();
84 if ( !image
.SaveFile( dir
+ _T("test.png"), wxBITMAP_TYPE_PNG
))
85 wxLogError(wxT("Can't save file"));
89 if ( image
.LoadFile( dir
+ _T("test.png") ) )
90 my_square
= wxBitmap( image
);
94 if ( !image
.LoadFile( dir
+ _T("horse.png")) )
95 wxLogError(wxT("Can't load PNG image"));
97 my_horse_png
= wxBitmap( image
);
99 if ( !image
.LoadFile( dir
+ _T("toucan.png")) )
100 wxLogError(wxT("Can't load PNG image"));
102 my_toucan
= wxBitmap(image
);
104 my_toucan_flipped_horiz
= wxBitmap(image
.Mirror(true));
105 my_toucan_flipped_vert
= wxBitmap(image
.Mirror(false));
106 my_toucan_flipped_both
= wxBitmap(image
.Mirror(true).Mirror(false));
107 my_toucan_grey
= wxBitmap(image
.ConvertToGreyscale());
108 my_toucan_head
= wxBitmap(image
.GetSubImage(wxRect(40, 7, 80, 60)));
109 my_toucan_scaled_normal
= wxBitmap(image
.Scale(110,90,wxIMAGE_QUALITY_NORMAL
));
110 my_toucan_scaled_high
= wxBitmap(image
.Scale(110,90,wxIMAGE_QUALITY_HIGH
));
111 my_toucan_blur
= wxBitmap(image
.Blur(10));
113 #endif // wxUSE_LIBPNG
118 if ( !image
.LoadFile( dir
+ _T("horse.jpg")) )
119 wxLogError(wxT("Can't load JPG image"));
122 my_horse_jpeg
= wxBitmap( image
);
124 // Colorize by rotating green hue to red
125 wxImage::HSVValue greenHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0));
126 wxImage::HSVValue redHSV
= wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0));
127 image
.RotateHue(redHSV
.hue
- greenHSV
.hue
);
128 colorized_horse_jpeg
= wxBitmap( image
);
131 if ( !image
.LoadFile( dir
+ _T("cmyk.jpg")) )
132 wxLogError(_T("Can't load CMYK JPG image"));
134 my_cmyk_jpeg
= wxBitmap(image
);
135 #endif // wxUSE_LIBJPEG
140 if ( !image
.LoadFile( dir
+ _T("horse.gif" )) )
141 wxLogError(wxT("Can't load GIF image"));
143 my_horse_gif
= wxBitmap( image
);
149 if ( !image
.LoadFile( dir
+ _T("horse.pcx"), wxBITMAP_TYPE_PCX
) )
150 wxLogError(wxT("Can't load PCX image"));
152 my_horse_pcx
= wxBitmap( image
);
157 if ( !image
.LoadFile( dir
+ _T("horse.bmp"), wxBITMAP_TYPE_BMP
) )
158 wxLogError(wxT("Can't load BMP image"));
160 my_horse_bmp
= wxBitmap( image
);
165 if ( !image
.LoadFile( dir
+ _T("horse.xpm"), wxBITMAP_TYPE_XPM
) )
166 wxLogError(wxT("Can't load XPM image"));
168 my_horse_xpm
= wxBitmap( image
);
170 if ( !image
.SaveFile( dir
+ _T("test.xpm"), wxBITMAP_TYPE_XPM
))
171 wxLogError(wxT("Can't save file"));
177 if ( !image
.LoadFile( dir
+ _T("horse.pnm"), wxBITMAP_TYPE_PNM
) )
178 wxLogError(wxT("Can't load PNM image"));
180 my_horse_pnm
= wxBitmap( image
);
184 if ( !image
.LoadFile( dir
+ _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM
) )
185 wxLogError(wxT("Can't load PNM image"));
187 my_horse_asciigrey_pnm
= wxBitmap( image
);
191 if ( !image
.LoadFile( dir
+ _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM
) )
192 wxLogError(wxT("Can't load PNM image"));
194 my_horse_rawgrey_pnm
= wxBitmap( image
);
200 if ( !image
.LoadFile( dir
+ _T("horse.tif"), wxBITMAP_TYPE_TIF
) )
201 wxLogError(wxT("Can't load TIFF image"));
203 my_horse_tiff
= wxBitmap( image
);
209 if ( !image
.LoadFile( dir
+ _T("horse.tga"), wxBITMAP_TYPE_TGA
) )
210 wxLogError(wxT("Can't load TGA image"));
212 my_horse_tga
= wxBitmap( image
);
215 CreateAntiAliasedBitmap();
217 my_smile_xbm
= wxBitmap( (const char*)smile_bits
, smile_width
,
220 // demonstrates XPM automatically using the mask when saving
221 if ( m_bmpSmileXpm
.Ok() )
222 m_bmpSmileXpm
.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM
);
227 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 0 ) )
228 wxLogError(wxT("Can't load first ICO image"));
230 my_horse_ico32
= wxBitmap( image
);
234 if ( !image
.LoadFile( dir
+ _T("horse.ico"), wxBITMAP_TYPE_ICO
, 1 ) )
235 wxLogError(wxT("Can't load second ICO image"));
237 my_horse_ico16
= wxBitmap( image
);
241 if ( !image
.LoadFile( dir
+ _T("horse.ico") ) )
242 wxLogError(wxT("Can't load best ICO image"));
244 my_horse_ico
= wxBitmap( image
);
248 if ( !image
.LoadFile( dir
+ _T("horse.cur"), wxBITMAP_TYPE_CUR
) )
249 wxLogError(wxT("Can't load best ICO image"));
252 my_horse_cur
= wxBitmap( image
);
253 xH
= 30 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ;
254 yH
= 2420 + image
.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ;
257 m_ani_images
= wxImage::GetImageCount ( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
);
259 wxLogError(wxT("No ANI-format images found"));
261 my_horse_ani
= new wxBitmap
[m_ani_images
];
264 for (i
=0; i
< m_ani_images
; i
++)
267 if (!image
.LoadFile( dir
+ _T("horse3.ani"), wxBITMAP_TYPE_ANI
, i
))
269 wxString tmp
= wxT("Can't load image number ");
274 my_horse_ani
[i
] = wxBitmap( image
);
276 #endif // wxUSE_ICO_CUR
281 // test image loading from stream
282 wxFile
file(dir
+ _T("horse.bmp"));
283 if ( file
.IsOpened() )
285 wxFileOffset len
= file
.Length();
286 size_t dataSize
= (size_t)len
;
287 void *data
= malloc(dataSize
);
288 if ( file
.Read(data
, dataSize
) != len
)
289 wxLogError(_T("Reading bitmap file failed"));
292 wxMemoryInputStream
mis(data
, dataSize
);
293 if ( !image
.LoadFile(mis
) )
294 wxLogError(wxT("Can't load BMP image from stream"));
296 my_horse_bmp2
= wxBitmap( image
);
303 MyCanvas::~MyCanvas()
305 delete [] my_horse_ani
;
308 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
310 wxPaintDC
dc( this );
313 dc
.DrawText( _T("Loaded image"), 30, 10 );
315 dc
.DrawBitmap( my_square
, 30, 30 );
317 dc
.DrawText( _T("Drawn directly"), 150, 10 );
318 dc
.SetBrush( wxBrush( wxT("orange"), wxSOLID
) );
319 dc
.SetPen( *wxBLACK_PEN
);
320 dc
.DrawRectangle( 150, 30, 100, 100 );
321 dc
.SetBrush( *wxWHITE_BRUSH
);
322 dc
.DrawRectangle( 170, 50, 60, 60 );
325 dc
.DrawBitmap( my_anti
, 280, 30 );
327 dc
.DrawText( _T("PNG handler"), 30, 135 );
328 if (my_horse_png
.Ok())
330 dc
.DrawBitmap( my_horse_png
, 30, 150 );
331 wxRect
rect(0,0,100,100);
332 wxBitmap
sub( my_horse_png
.GetSubBitmap(rect
) );
333 dc
.DrawText( _T("GetSubBitmap()"), 280, 175 );
334 dc
.DrawBitmap( sub
, 280, 195 );
337 dc
.DrawText( _T("JPEG handler"), 30, 365 );
338 if (my_horse_jpeg
.Ok())
339 dc
.DrawBitmap( my_horse_jpeg
, 30, 380 );
341 dc
.DrawText( _T("Green rotated to red"), 280, 365 );
342 if (colorized_horse_jpeg
.Ok())
343 dc
.DrawBitmap( colorized_horse_jpeg
, 280, 380 );
345 dc
.DrawText( _T("CMYK JPEG image"), 530, 365 );
346 if (my_cmyk_jpeg
.Ok())
347 dc
.DrawBitmap( my_cmyk_jpeg
, 530, 380 );
349 dc
.DrawText( _T("GIF handler"), 30, 595 );
350 if (my_horse_gif
.Ok())
351 dc
.DrawBitmap( my_horse_gif
, 30, 610 );
353 dc
.DrawText( _T("PCX handler"), 30, 825 );
354 if (my_horse_pcx
.Ok())
355 dc
.DrawBitmap( my_horse_pcx
, 30, 840 );
357 dc
.DrawText( _T("BMP handler"), 30, 1055 );
358 if (my_horse_bmp
.Ok())
359 dc
.DrawBitmap( my_horse_bmp
, 30, 1070 );
361 dc
.DrawText( _T("BMP read from memory"), 280, 1055 );
362 if (my_horse_bmp2
.Ok())
363 dc
.DrawBitmap( my_horse_bmp2
, 280, 1070 );
365 dc
.DrawText( _T("PNM handler"), 30, 1285 );
366 if (my_horse_pnm
.Ok())
367 dc
.DrawBitmap( my_horse_pnm
, 30, 1300 );
369 dc
.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 );
370 if (my_horse_asciigrey_pnm
.Ok())
371 dc
.DrawBitmap( my_horse_asciigrey_pnm
, 280, 1300 );
373 dc
.DrawText( _T("PNM handler (raw grey)"), 530, 1285 );
374 if (my_horse_rawgrey_pnm
.Ok())
375 dc
.DrawBitmap( my_horse_rawgrey_pnm
, 530, 1300 );
377 dc
.DrawText( _T("TIFF handler"), 30, 1515 );
378 if (my_horse_tiff
.Ok())
379 dc
.DrawBitmap( my_horse_tiff
, 30, 1530 );
381 dc
.DrawText( _T("TGA handler"), 30, 1745 );
382 if (my_horse_tga
.Ok())
383 dc
.DrawBitmap( my_horse_tga
, 30, 1760 );
385 dc
.DrawText( _T("XPM handler"), 30, 1975 );
386 if (my_horse_xpm
.Ok())
387 dc
.DrawBitmap( my_horse_xpm
, 30, 2000 );
391 int x
= 750, y
= 10, yy
= 170;
393 dc
.DrawText(wxT("Original toucan"), x
+50, y
);
394 dc
.DrawBitmap(my_toucan
, x
, y
+15, true);
396 dc
.DrawText(wxT("Flipped horizontally"), x
+50, y
);
397 dc
.DrawBitmap(my_toucan_flipped_horiz
, x
, y
+15, true);
399 dc
.DrawText(wxT("Flipped vertically"), x
+50, y
);
400 dc
.DrawBitmap(my_toucan_flipped_vert
, x
, y
+15, true);
402 dc
.DrawText(wxT("Flipped both h&v"), x
+50, y
);
403 dc
.DrawBitmap(my_toucan_flipped_both
, x
, y
+15, true);
406 dc
.DrawText(wxT("In greyscale"), x
+50, y
);
407 dc
.DrawBitmap(my_toucan_grey
, x
, y
+15, true);
410 dc
.DrawText(wxT("Toucan's head"), x
+50, y
);
411 dc
.DrawBitmap(my_toucan_head
, x
, y
+15, true);
414 dc
.DrawText(wxT("Scaled with normal quality"), x
+50, y
);
415 dc
.DrawBitmap(my_toucan_scaled_normal
, x
, y
+15, true);
418 dc
.DrawText(wxT("Scaled with high quality"), x
+50, y
);
419 dc
.DrawBitmap(my_toucan_scaled_high
, x
, y
+15, true);
422 dc
.DrawText(wxT("Blured"), x
+50, y
);
423 dc
.DrawBitmap(my_toucan_blur
, x
, y
+15, true);
426 if (my_smile_xbm
.Ok())
428 int x
= 300, y
= 1800;
430 dc
.DrawText( _T("XBM bitmap"), x
, y
);
431 dc
.DrawText( _T("(green on red)"), x
, y
+ 15 );
432 dc
.SetTextForeground( _T("GREEN") );
433 dc
.SetTextBackground( _T("RED") );
434 dc
.DrawBitmap( my_smile_xbm
, x
, y
+ 30 );
436 dc
.SetTextForeground( *wxBLACK
);
437 dc
.DrawText( _T("After wxImage conversion"), x
+ 120, y
);
438 dc
.DrawText( _T("(red on white)"), x
+ 120, y
+ 15 );
439 dc
.SetTextForeground( wxT("RED") );
440 wxImage i
= my_smile_xbm
.ConvertToImage();
441 i
.SetMaskColour( 255, 255, 255 );
443 wxRED_PEN
->GetColour().Red(),
444 wxRED_PEN
->GetColour().Green(),
445 wxRED_PEN
->GetColour().Blue() );
446 dc
.DrawBitmap( wxBitmap(i
), x
+ 120, y
+ 30, true );
447 dc
.SetTextForeground( *wxBLACK
);
451 wxBitmap
mono( 60,50,1 );
453 memdc
.SelectObject( mono
);
454 memdc
.SetPen( *wxBLACK_PEN
);
455 memdc
.SetBrush( *wxWHITE_BRUSH
);
456 memdc
.DrawRectangle( 0,0,60,50 );
457 memdc
.SetTextForeground( *wxBLACK
);
459 // I cannot convince GTK2 to draw into mono bitmaps
460 memdc
.DrawText( _T("Hi!"), 5, 5 );
462 memdc
.SetBrush( *wxBLACK_BRUSH
);
463 memdc
.DrawRectangle( 33,5,20,20 );
464 memdc
.SetPen( *wxRED_PEN
);
465 memdc
.DrawLine( 5, 42, 50, 42 );
466 memdc
.SelectObject( wxNullBitmap
);
470 int x
= 300, y
= 1900;
472 dc
.DrawText( _T("Mono bitmap"), x
, y
);
473 dc
.DrawText( _T("(red on green)"), x
, y
+ 15 );
474 dc
.SetTextForeground( wxT("RED") );
475 dc
.SetTextBackground( wxT("GREEN") );
476 dc
.DrawBitmap( mono
, x
, y
+ 30 );
478 dc
.SetTextForeground( *wxBLACK
);
479 dc
.DrawText( _T("After wxImage conversion"), x
+ 120, y
);
480 dc
.DrawText( _T("(red on white)"), x
+ 120, y
+ 15 );
481 dc
.SetTextForeground( wxT("RED") );
482 wxImage i
= mono
.ConvertToImage();
483 i
.SetMaskColour( 255,255,255 );
485 wxRED_PEN
->GetColour().Red(),
486 wxRED_PEN
->GetColour().Green(),
487 wxRED_PEN
->GetColour().Blue() );
488 dc
.DrawBitmap( wxBitmap(i
), x
+ 120, y
+ 30, true );
489 dc
.SetTextForeground( *wxBLACK
);
492 // For testing transparency
493 dc
.SetBrush( *wxRED_BRUSH
);
494 dc
.DrawRectangle( 20, 2220, 560, 68 );
496 dc
.DrawText(_T("XPM bitmap"), 30, 2230 );
497 if ( m_bmpSmileXpm
.Ok() )
498 dc
.DrawBitmap(m_bmpSmileXpm
, 30, 2250, true);
500 dc
.DrawText(_T("XPM icon"), 110, 2230 );
501 if ( m_iconSmileXpm
.Ok() )
502 dc
.DrawIcon(m_iconSmileXpm
, 110, 2250);
504 // testing icon -> bitmap conversion
505 wxBitmap
to_blit( m_iconSmileXpm
);
508 dc
.DrawText( _T("SubBitmap"), 170, 2230 );
509 wxBitmap sub
= to_blit
.GetSubBitmap( wxRect(0,0,15,15) );
511 dc
.DrawBitmap( sub
, 170, 2250, true );
513 dc
.DrawText( _T("Enlarged"), 250, 2230 );
514 dc
.SetUserScale( 1.5, 1.5 );
515 dc
.DrawBitmap( to_blit
, (int)(250/1.5), (int)(2250/1.5), true );
516 dc
.SetUserScale( 2, 2 );
517 dc
.DrawBitmap( to_blit
, (int)(300/2), (int)(2250/2), true );
518 dc
.SetUserScale( 1.0, 1.0 );
520 dc
.DrawText( _T("Blit"), 400, 2230);
522 blit_dc
.SelectObject( to_blit
);
523 dc
.Blit( 400, 2250, to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
524 dc
.SetUserScale( 1.5, 1.5 );
525 dc
.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
526 dc
.SetUserScale( 2, 2 );
527 dc
.Blit( (int)(500/2), (int)(2250/2), to_blit
.GetWidth(), to_blit
.GetHeight(), &blit_dc
, 0, 0, wxCOPY
, true );
528 dc
.SetUserScale( 1.0, 1.0 );
531 dc
.DrawText( _T("ICO handler (1st image)"), 30, 2290 );
532 if (my_horse_ico32
.Ok())
533 dc
.DrawBitmap( my_horse_ico32
, 30, 2330, true );
535 dc
.DrawText( _T("ICO handler (2nd image)"), 230, 2290 );
536 if (my_horse_ico16
.Ok())
537 dc
.DrawBitmap( my_horse_ico16
, 230, 2330, true );
539 dc
.DrawText( _T("ICO handler (best image)"), 430, 2290 );
540 if (my_horse_ico
.Ok())
541 dc
.DrawBitmap( my_horse_ico
, 430, 2330, true );
543 dc
.DrawText( _T("CUR handler"), 30, 2390 );
544 if (my_horse_cur
.Ok())
546 dc
.DrawBitmap( my_horse_cur
, 30, 2420, true );
547 dc
.SetPen (*wxRED_PEN
);
548 dc
.DrawLine (xH
-10,yH
,xH
+10,yH
);
549 dc
.DrawLine (xH
,yH
-10,xH
,yH
+10);
552 dc
.DrawText( _T("ANI handler"), 230, 2390 );
553 for ( int i
=0; i
< m_ani_images
; i
++ )
555 if (my_horse_ani
[i
].Ok())
557 dc
.DrawBitmap( my_horse_ani
[i
], 230 + i
* 2 * my_horse_ani
[i
].GetWidth() , 2420, true );
562 void MyCanvas::CreateAntiAliasedBitmap()
564 wxBitmap
bitmap( 300, 300 );
568 dc
.SelectObject( bitmap
);
572 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
) );
573 dc
.SetTextForeground( wxT("RED") );
574 dc
.DrawText( _T("This is anti-aliased Text."), 20, 5 );
575 dc
.DrawText( _T("And a Rectangle."), 20, 45 );
577 dc
.SetBrush( *wxRED_BRUSH
);
578 dc
.SetPen( *wxTRANSPARENT_PEN
);
579 dc
.DrawRoundedRectangle( 20, 85, 200, 180, 20 );
581 wxImage original
= bitmap
.ConvertToImage();
582 wxImage
anti( 150, 150 );
584 /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */
586 for (int y
= 1; y
< 149; y
++)
587 for (int x
= 1; x
< 149; x
++)
589 int red
= original
.GetRed( x
*2, y
*2 ) +
590 original
.GetRed( x
*2-1, y
*2 ) +
591 original
.GetRed( x
*2, y
*2+1 ) +
592 original
.GetRed( x
*2+1, y
*2+1 );
595 int green
= original
.GetGreen( x
*2, y
*2 ) +
596 original
.GetGreen( x
*2-1, y
*2 ) +
597 original
.GetGreen( x
*2, y
*2+1 ) +
598 original
.GetGreen( x
*2+1, y
*2+1 );
601 int blue
= original
.GetBlue( x
*2, y
*2 ) +
602 original
.GetBlue( x
*2-1, y
*2 ) +
603 original
.GetBlue( x
*2, y
*2+1 ) +
604 original
.GetBlue( x
*2+1, y
*2+1 );
606 anti
.SetRGB( x
, y
, (unsigned char)red
, (unsigned char)green
, (unsigned char)blue
);
609 my_anti
= wxBitmap(anti
);