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