canvas.cpp 20 KB

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