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