]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: samples/image/image.cpp | |
3 | // Purpose: sample showing operations with wxImage | |
4 | // Author: Robert Roebling | |
5 | // Modified by: | |
6 | // Created: 1998 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998-2005 Robert Roebling | |
9 | // License: 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 | ||
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 | ||
38 | #if defined(__WXMSW__) | |
39 | #ifdef wxHAVE_RAW_BITMAP | |
40 | #include "wx/rawbmp.h" | |
41 | #endif | |
42 | #endif | |
43 | ||
44 | #if defined(__WXMAC__) || defined(__WXGTK__) | |
45 | #define wxHAVE_RAW_BITMAP | |
46 | #include "wx/rawbmp.h" | |
47 | #endif | |
48 | ||
49 | // derived classes | |
50 | ||
51 | class MyFrame; | |
52 | class MyApp; | |
53 | ||
54 | // MyCanvas | |
55 | ||
56 | class MyCanvas: public wxScrolledWindow | |
57 | { | |
58 | public: | |
59 | MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size ); | |
60 | ~MyCanvas(); | |
61 | void OnPaint( wxPaintEvent &event ); | |
62 | void CreateAntiAliasedBitmap(); | |
63 | ||
64 | wxBitmap my_horse_png; | |
65 | wxBitmap my_horse_jpeg; | |
66 | wxBitmap my_horse_gif; | |
67 | wxBitmap my_horse_bmp; | |
68 | wxBitmap my_horse_bmp2; | |
69 | wxBitmap my_horse_pcx; | |
70 | wxBitmap my_horse_pnm; | |
71 | wxBitmap my_horse_tiff; | |
72 | wxBitmap my_horse_tga; | |
73 | wxBitmap my_horse_xpm; | |
74 | wxBitmap my_horse_ico32; | |
75 | wxBitmap my_horse_ico16; | |
76 | wxBitmap my_horse_ico; | |
77 | wxBitmap my_horse_cur; | |
78 | ||
79 | wxBitmap my_smile_xbm; | |
80 | wxBitmap my_square; | |
81 | wxBitmap my_anti; | |
82 | ||
83 | wxBitmap my_horse_asciigrey_pnm; | |
84 | wxBitmap my_horse_rawgrey_pnm; | |
85 | ||
86 | wxBitmap colorized_horse_jpeg; | |
87 | wxBitmap my_cmyk_jpeg; | |
88 | ||
89 | wxBitmap my_toucan; | |
90 | wxBitmap my_toucan_flipped_horiz; | |
91 | wxBitmap my_toucan_flipped_vert; | |
92 | wxBitmap my_toucan_flipped_both; | |
93 | wxBitmap my_toucan_grey; | |
94 | wxBitmap my_toucan_head; | |
95 | wxBitmap my_toucan_scaled_normal; | |
96 | wxBitmap my_toucan_scaled_high; | |
97 | wxBitmap my_toucan_blur; | |
98 | ||
99 | int xH, yH ; | |
100 | int m_ani_images; | |
101 | wxBitmap *my_horse_ani; | |
102 | ||
103 | private: | |
104 | wxBitmap m_bmpSmileXpm; | |
105 | wxIcon m_iconSmileXpm; | |
106 | ||
107 | DECLARE_EVENT_TABLE() | |
108 | }; | |
109 | ||
110 | ||
111 | // MyFrame | |
112 | ||
113 | ||
114 | class MyFrame: public wxFrame | |
115 | { | |
116 | public: | |
117 | MyFrame(); | |
118 | ||
119 | void OnAbout( wxCommandEvent &event ); | |
120 | void OnNewFrame( wxCommandEvent &event ); | |
121 | #ifdef wxHAVE_RAW_BITMAP | |
122 | void OnTestRawBitmap( wxCommandEvent &event ); | |
123 | #endif // wxHAVE_RAW_BITMAP | |
124 | void OnQuit( wxCommandEvent &event ); | |
125 | ||
126 | #if wxUSE_CLIPBOARD | |
127 | void OnCopy(wxCommandEvent& event); | |
128 | void OnPaste(wxCommandEvent& event); | |
129 | #endif // wxUSE_CLIPBOARD | |
130 | ||
131 | MyCanvas *m_canvas; | |
132 | ||
133 | private: | |
134 | DECLARE_DYNAMIC_CLASS(MyFrame) | |
135 | DECLARE_EVENT_TABLE() | |
136 | }; | |
137 | ||
138 | // ---------------------------------------------------------------------------- | |
139 | // Frame used for showing a standalone image | |
140 | // ---------------------------------------------------------------------------- | |
141 | ||
142 | enum | |
143 | { | |
144 | ID_ROTATE_LEFT = 100, | |
145 | ID_ROTATE_RIGHT, | |
146 | ID_RESIZE | |
147 | }; | |
148 | ||
149 | class MyImageFrame : public wxFrame | |
150 | { | |
151 | public: | |
152 | MyImageFrame(wxFrame *parent, const wxString& desc, const wxBitmap& bitmap) | |
153 | : wxFrame(parent, wxID_ANY, | |
154 | wxString::Format(_T("Image from %s"), desc.c_str()), | |
155 | wxDefaultPosition, wxDefaultSize, | |
156 | wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE), | |
157 | m_bitmap(bitmap) | |
158 | { | |
159 | wxMenu *menu = new wxMenu; | |
160 | menu->Append(wxID_SAVE); | |
161 | menu->AppendSeparator(); | |
162 | menu->Append(ID_RESIZE, _T("&Fit to window\tCtrl-F")); | |
163 | menu->AppendSeparator(); | |
164 | menu->Append(ID_ROTATE_LEFT, _T("Rotate &left\tCtrl-L")); | |
165 | menu->Append(ID_ROTATE_RIGHT, _T("Rotate &right\tCtrl-R")); | |
166 | ||
167 | wxMenuBar *mbar = new wxMenuBar; | |
168 | mbar->Append(menu, _T("&Image")); | |
169 | SetMenuBar(mbar); | |
170 | ||
171 | CreateStatusBar(); | |
172 | ||
173 | SetClientSize(bitmap.GetWidth(), bitmap.GetHeight()); | |
174 | ||
175 | UpdateStatusBar(); | |
176 | } | |
177 | ||
178 | void OnEraseBackground(wxEraseEvent& WXUNUSED(event)) | |
179 | { | |
180 | // do nothing here to be able to see how transparent images are shown | |
181 | } | |
182 | ||
183 | void OnPaint(wxPaintEvent& WXUNUSED(event)) | |
184 | { | |
185 | wxPaintDC dc(this); | |
186 | const wxSize size = GetClientSize(); | |
187 | dc.DrawBitmap(m_bitmap, | |
188 | (size.x - m_bitmap.GetWidth())/2, | |
189 | (size.y - m_bitmap.GetHeight())/2, | |
190 | true /* use mask */); | |
191 | } | |
192 | ||
193 | void OnSave(wxCommandEvent& WXUNUSED(event)) | |
194 | { | |
195 | #if wxUSE_FILEDLG | |
196 | wxImage image = m_bitmap.ConvertToImage(); | |
197 | ||
198 | wxString savefilename = wxFileSelector( wxT("Save Image"), | |
199 | wxEmptyString, | |
200 | wxEmptyString, | |
201 | (const wxChar *)NULL, | |
202 | wxT("BMP files (*.bmp)|*.bmp|") | |
203 | wxT("PNG files (*.png)|*.png|") | |
204 | wxT("JPEG files (*.jpg)|*.jpg|") | |
205 | wxT("GIF files (*.gif)|*.gif|") | |
206 | wxT("TIFF files (*.tif)|*.tif|") | |
207 | wxT("PCX files (*.pcx)|*.pcx|") | |
208 | wxT("ICO files (*.ico)|*.ico|") | |
209 | wxT("CUR files (*.cur)|*.cur"), | |
210 | wxFD_SAVE, | |
211 | this); | |
212 | ||
213 | if ( savefilename.empty() ) | |
214 | return; | |
215 | ||
216 | wxString extension; | |
217 | wxFileName::SplitPath(savefilename, NULL, NULL, &extension); | |
218 | ||
219 | bool saved = false; | |
220 | if ( extension == _T("bmp") ) | |
221 | { | |
222 | static const int bppvalues[] = | |
223 | { | |
224 | wxBMP_1BPP, | |
225 | wxBMP_1BPP_BW, | |
226 | wxBMP_4BPP, | |
227 | wxBMP_8BPP, | |
228 | wxBMP_8BPP_GREY, | |
229 | wxBMP_8BPP_RED, | |
230 | wxBMP_8BPP_PALETTE, | |
231 | wxBMP_24BPP | |
232 | }; | |
233 | ||
234 | const wxString bppchoices[] = | |
235 | { | |
236 | _T("1 bpp color"), | |
237 | _T("1 bpp B&W"), | |
238 | _T("4 bpp color"), | |
239 | _T("8 bpp color"), | |
240 | _T("8 bpp greyscale"), | |
241 | _T("8 bpp red"), | |
242 | _T("8 bpp own palette"), | |
243 | _T("24 bpp") | |
244 | }; | |
245 | ||
246 | int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"), | |
247 | _T("Image sample: save file"), | |
248 | WXSIZEOF(bppchoices), | |
249 | bppchoices, | |
250 | this); | |
251 | if ( bppselection != -1 ) | |
252 | { | |
253 | int format = bppvalues[bppselection]; | |
254 | image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, format); | |
255 | ||
256 | if ( format == wxBMP_8BPP_PALETTE ) | |
257 | { | |
258 | unsigned char *cmap = new unsigned char [256]; | |
259 | for ( int i = 0; i < 256; i++ ) | |
260 | cmap[i] = (unsigned char)i; | |
261 | image.SetPalette(wxPalette(256, cmap, cmap, cmap)); | |
262 | ||
263 | delete[] cmap; | |
264 | } | |
265 | } | |
266 | } | |
267 | else if ( extension == _T("png") ) | |
268 | { | |
269 | static const int pngvalues[] = | |
270 | { | |
271 | wxPNG_TYPE_COLOUR, | |
272 | wxPNG_TYPE_COLOUR, | |
273 | wxPNG_TYPE_GREY, | |
274 | wxPNG_TYPE_GREY, | |
275 | wxPNG_TYPE_GREY_RED, | |
276 | wxPNG_TYPE_GREY_RED, | |
277 | }; | |
278 | ||
279 | const wxString pngchoices[] = | |
280 | { | |
281 | _T("Colour 8bpp"), | |
282 | _T("Colour 16bpp"), | |
283 | _T("Grey 8bpp"), | |
284 | _T("Grey 16bpp"), | |
285 | _T("Grey red 8bpp"), | |
286 | _T("Grey red 16bpp"), | |
287 | }; | |
288 | ||
289 | int sel = wxGetSingleChoiceIndex(_T("Set PNG format"), | |
290 | _T("Image sample: save file"), | |
291 | WXSIZEOF(pngchoices), | |
292 | pngchoices, | |
293 | this); | |
294 | if ( sel != -1 ) | |
295 | { | |
296 | image.SetOption(wxIMAGE_OPTION_PNG_FORMAT, pngvalues[sel]); | |
297 | image.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH, sel % 2 ? 16 : 8); | |
298 | } | |
299 | } | |
300 | else if ( extension == _T("cur") ) | |
301 | { | |
302 | image.Rescale(32,32); | |
303 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0); | |
304 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0); | |
305 | // This shows how you can save an image with explicitly | |
306 | // specified image format: | |
307 | saved = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR); | |
308 | } | |
309 | ||
310 | if ( !saved ) | |
311 | { | |
312 | // This one guesses image format from filename extension | |
313 | // (it may fail if the extension is not recognized): | |
314 | image.SaveFile(savefilename); | |
315 | } | |
316 | #endif // wxUSE_FILEDLG | |
317 | } | |
318 | ||
319 | void OnResize(wxCommandEvent& WXUNUSED(event)) | |
320 | { | |
321 | wxImage img(m_bitmap.ConvertToImage()); | |
322 | ||
323 | const wxSize size = GetClientSize(); | |
324 | img.Rescale(size.x, size.y, wxIMAGE_QUALITY_HIGH); | |
325 | m_bitmap = wxBitmap(img); | |
326 | ||
327 | UpdateStatusBar(); | |
328 | Refresh(); | |
329 | } | |
330 | ||
331 | void OnRotate(wxCommandEvent& event) | |
332 | { | |
333 | double angle = 5; | |
334 | if ( event.GetId() == ID_ROTATE_LEFT ) | |
335 | angle = -angle; | |
336 | ||
337 | wxImage img(m_bitmap.ConvertToImage()); | |
338 | img = img.Rotate(angle, wxPoint(img.GetWidth() / 2, img.GetHeight() / 2)); | |
339 | if ( !img.Ok() ) | |
340 | { | |
341 | wxLogWarning(_T("Rotation failed")); | |
342 | return; | |
343 | } | |
344 | ||
345 | m_bitmap = wxBitmap(img); | |
346 | ||
347 | UpdateStatusBar(); | |
348 | Refresh(); | |
349 | } | |
350 | ||
351 | private: | |
352 | void UpdateStatusBar() | |
353 | { | |
354 | wxLogStatus(this, _T("Image size: (%d, %d)"), | |
355 | m_bitmap.GetWidth(), | |
356 | m_bitmap.GetHeight()); | |
357 | } | |
358 | ||
359 | wxBitmap m_bitmap; | |
360 | ||
361 | DECLARE_EVENT_TABLE() | |
362 | }; | |
363 | ||
364 | #ifdef wxHAVE_RAW_BITMAP | |
365 | ||
366 | #include "wx/rawbmp.h" | |
367 | ||
368 | class MyRawBitmapFrame : public wxFrame | |
369 | { | |
370 | public: | |
371 | enum | |
372 | { | |
373 | BORDER = 15, | |
374 | SIZE = 150, | |
375 | REAL_SIZE = SIZE - 2*BORDER | |
376 | }; | |
377 | ||
378 | MyRawBitmapFrame(wxFrame *parent) | |
379 | : wxFrame(parent, wxID_ANY, _T("Raw bitmaps (how exciting)")), | |
380 | m_bitmap(SIZE, SIZE, 24), | |
381 | m_alphaBitmap(SIZE, SIZE, 32) | |
382 | { | |
383 | SetClientSize(SIZE, SIZE*2+25); | |
384 | ||
385 | InitAlphaBitmap(); | |
386 | InitBitmap(); | |
387 | ||
388 | } | |
389 | ||
390 | void InitAlphaBitmap() | |
391 | { | |
392 | // First, clear the whole bitmap by making it alpha | |
393 | { | |
394 | wxAlphaPixelData data( m_alphaBitmap, wxPoint(0,0), wxSize(SIZE, SIZE) ); | |
395 | if ( !data ) | |
396 | { | |
397 | wxLogError(_T("Failed to gain raw access to bitmap data")); | |
398 | return; | |
399 | } | |
400 | wxAlphaPixelData::Iterator p(data); | |
401 | for ( int y = 0; y < SIZE; ++y ) | |
402 | { | |
403 | wxAlphaPixelData::Iterator rowStart = p; | |
404 | for ( int x = 0; x < SIZE; ++x ) | |
405 | { | |
406 | p.Alpha() = 0; | |
407 | ++p; // same as p.OffsetX(1) | |
408 | } | |
409 | p = rowStart; | |
410 | p.OffsetY(data, 1); | |
411 | } | |
412 | } | |
413 | ||
414 | // Then, draw colourful alpha-blended stripes | |
415 | wxAlphaPixelData data(m_alphaBitmap, wxPoint(BORDER, BORDER), | |
416 | wxSize(REAL_SIZE, REAL_SIZE)); | |
417 | if ( !data ) | |
418 | { | |
419 | wxLogError(_T("Failed to gain raw access to bitmap data")); | |
420 | return; | |
421 | } | |
422 | ||
423 | wxAlphaPixelData::Iterator p(data); | |
424 | ||
425 | for ( int y = 0; y < REAL_SIZE; ++y ) | |
426 | { | |
427 | wxAlphaPixelData::Iterator rowStart = p; | |
428 | ||
429 | int r = y < REAL_SIZE/3 ? 255 : 0, | |
430 | g = (REAL_SIZE/3 <= y) && (y < 2*(REAL_SIZE/3)) ? 255 : 0, | |
431 | b = 2*(REAL_SIZE/3) <= y ? 255 : 0; | |
432 | ||
433 | for ( int x = 0; x < REAL_SIZE; ++x ) | |
434 | { | |
435 | // note that RGB must be premultiplied by alpha | |
436 | unsigned a = (wxAlphaPixelData::Iterator::ChannelType)((x*255.)/REAL_SIZE); | |
437 | p.Red() = r * a / 256; | |
438 | p.Green() = g * a / 256; | |
439 | p.Blue() = b * a / 256; | |
440 | p.Alpha() = a; | |
441 | ||
442 | ++p; // same as p.OffsetX(1) | |
443 | } | |
444 | ||
445 | p = rowStart; | |
446 | p.OffsetY(data, 1); | |
447 | } | |
448 | } | |
449 | ||
450 | void InitBitmap() | |
451 | { | |
452 | // draw some colourful stripes without alpha | |
453 | wxNativePixelData data(m_bitmap); | |
454 | if ( !data ) | |
455 | { | |
456 | wxLogError(_T("Failed to gain raw access to bitmap data")); | |
457 | return; | |
458 | } | |
459 | ||
460 | wxNativePixelData::Iterator p(data); | |
461 | for ( int y = 0; y < SIZE; ++y ) | |
462 | { | |
463 | wxNativePixelData::Iterator rowStart = p; | |
464 | ||
465 | int r = y < SIZE/3 ? 255 : 0, | |
466 | g = (SIZE/3 <= y) && (y < 2*(SIZE/3)) ? 255 : 0, | |
467 | b = 2*(SIZE/3) <= y ? 255 : 0; | |
468 | ||
469 | for ( int x = 0; x < SIZE; ++x ) | |
470 | { | |
471 | p.Red() = r; | |
472 | p.Green() = g; | |
473 | p.Blue() = b; | |
474 | ++p; // same as p.OffsetX(1) | |
475 | } | |
476 | ||
477 | p = rowStart; | |
478 | p.OffsetY(data, 1); | |
479 | } | |
480 | } | |
481 | ||
482 | void OnPaint(wxPaintEvent& WXUNUSED(event)) | |
483 | { | |
484 | wxPaintDC dc( this ); | |
485 | dc.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER); | |
486 | dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE/2 - BORDER); | |
487 | dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE - 2*BORDER); | |
488 | dc.DrawBitmap( m_alphaBitmap, 0, 0, true /* use mask */ ); | |
489 | ||
490 | dc.DrawText(_T("Raw bitmap access without alpha"), 0, SIZE+5); | |
491 | dc.DrawBitmap( m_bitmap, 0, SIZE+5+dc.GetCharHeight()); | |
492 | } | |
493 | ||
494 | private: | |
495 | wxBitmap m_bitmap; | |
496 | wxBitmap m_alphaBitmap; | |
497 | ||
498 | DECLARE_EVENT_TABLE() | |
499 | }; | |
500 | ||
501 | #endif // wxHAVE_RAW_BITMAP | |
502 | ||
503 | // MyApp | |
504 | ||
505 | class MyApp: public wxApp | |
506 | { | |
507 | public: | |
508 | virtual bool OnInit(); | |
509 | }; | |
510 | ||
511 | // main program | |
512 | ||
513 | IMPLEMENT_APP(MyApp) | |
514 | ||
515 | // MyCanvas | |
516 | ||
517 | BEGIN_EVENT_TABLE(MyImageFrame, wxFrame) | |
518 | EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground) | |
519 | EVT_PAINT(MyImageFrame::OnPaint) | |
520 | ||
521 | EVT_MENU(wxID_SAVE, MyImageFrame::OnSave) | |
522 | EVT_MENU_RANGE(ID_ROTATE_LEFT, ID_ROTATE_RIGHT, MyImageFrame::OnRotate) | |
523 | EVT_MENU(ID_RESIZE, MyImageFrame::OnResize) | |
524 | END_EVENT_TABLE() | |
525 | ||
526 | #ifdef wxHAVE_RAW_BITMAP | |
527 | ||
528 | BEGIN_EVENT_TABLE(MyRawBitmapFrame, wxFrame) | |
529 | EVT_PAINT(MyRawBitmapFrame::OnPaint) | |
530 | END_EVENT_TABLE() | |
531 | ||
532 | #endif // wxHAVE_RAW_BITMAP | |
533 | ||
534 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
535 | EVT_PAINT(MyCanvas::OnPaint) | |
536 | END_EVENT_TABLE() | |
537 | ||
538 | MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id, | |
539 | const wxPoint &pos, const wxSize &size ) | |
540 | : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER ) | |
541 | , m_bmpSmileXpm(smile_xpm) | |
542 | , m_iconSmileXpm(smile_xpm) | |
543 | { | |
544 | my_horse_ani = NULL; | |
545 | m_ani_images = 0 ; | |
546 | ||
547 | SetBackgroundColour(* wxWHITE); | |
548 | ||
549 | wxBitmap bitmap( 100, 100 ); | |
550 | ||
551 | wxMemoryDC dc; | |
552 | dc.SelectObject( bitmap ); | |
553 | dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) ); | |
554 | dc.SetPen( *wxBLACK_PEN ); | |
555 | dc.DrawRectangle( 0, 0, 100, 100 ); | |
556 | dc.SetBrush( *wxWHITE_BRUSH ); | |
557 | dc.DrawRectangle( 20, 20, 60, 60 ); | |
558 | dc.SelectObject( wxNullBitmap ); | |
559 | ||
560 | // try to find the directory with our images | |
561 | wxString dir; | |
562 | if ( wxFile::Exists(wxT("./horse.png")) ) | |
563 | dir = wxT("./"); | |
564 | else if ( wxFile::Exists(wxT("../horse.png")) ) | |
565 | dir = wxT("../"); | |
566 | else | |
567 | wxLogWarning(wxT("Can't find image files in either '.' or '..'!")); | |
568 | ||
569 | wxImage image = bitmap.ConvertToImage(); | |
570 | ||
571 | #if wxUSE_LIBPNG | |
572 | if ( !image.SaveFile( dir + _T("test.png"), wxBITMAP_TYPE_PNG )) | |
573 | wxLogError(wxT("Can't save file")); | |
574 | ||
575 | image.Destroy(); | |
576 | ||
577 | if ( image.LoadFile( dir + _T("test.png") ) ) | |
578 | my_square = wxBitmap( image ); | |
579 | ||
580 | image.Destroy(); | |
581 | ||
582 | if ( !image.LoadFile( dir + _T("horse.png")) ) | |
583 | wxLogError(wxT("Can't load PNG image")); | |
584 | else | |
585 | my_horse_png = wxBitmap( image ); | |
586 | ||
587 | if ( !image.LoadFile( dir + _T("toucan.png")) ) | |
588 | wxLogError(wxT("Can't load PNG image")); | |
589 | else | |
590 | my_toucan = wxBitmap(image); | |
591 | ||
592 | my_toucan_flipped_horiz = wxBitmap(image.Mirror(true)); | |
593 | my_toucan_flipped_vert = wxBitmap(image.Mirror(false)); | |
594 | my_toucan_flipped_both = wxBitmap(image.Mirror(true).Mirror(false)); | |
595 | my_toucan_grey = wxBitmap(image.ConvertToGreyscale()); | |
596 | my_toucan_head = wxBitmap(image.GetSubImage(wxRect(40, 7, 80, 60))); | |
597 | my_toucan_scaled_normal = wxBitmap(image.Scale(110,90,wxIMAGE_QUALITY_NORMAL)); | |
598 | my_toucan_scaled_high = wxBitmap(image.Scale(110,90,wxIMAGE_QUALITY_HIGH)); | |
599 | my_toucan_blur = wxBitmap(image.Blur(10)); | |
600 | ||
601 | #endif // wxUSE_LIBPNG | |
602 | ||
603 | #if wxUSE_LIBJPEG | |
604 | image.Destroy(); | |
605 | ||
606 | if ( !image.LoadFile( dir + _T("horse.jpg")) ) | |
607 | wxLogError(wxT("Can't load JPG image")); | |
608 | else | |
609 | { | |
610 | my_horse_jpeg = wxBitmap( image ); | |
611 | ||
612 | // Colorize by rotating green hue to red | |
613 | wxImage::HSVValue greenHSV = wxImage::RGBtoHSV(wxImage::RGBValue(0, 255, 0)); | |
614 | wxImage::HSVValue redHSV = wxImage::RGBtoHSV(wxImage::RGBValue(255, 0, 0)); | |
615 | image.RotateHue(redHSV.hue - greenHSV.hue); | |
616 | colorized_horse_jpeg = wxBitmap( image ); | |
617 | } | |
618 | ||
619 | if ( !image.LoadFile( dir + _T("cmyk.jpg")) ) | |
620 | wxLogError(_T("Can't load CMYK JPG image")); | |
621 | else | |
622 | my_cmyk_jpeg = wxBitmap(image); | |
623 | #endif // wxUSE_LIBJPEG | |
624 | ||
625 | #if wxUSE_GIF | |
626 | image.Destroy(); | |
627 | ||
628 | if ( !image.LoadFile( dir + _T("horse.gif" )) ) | |
629 | wxLogError(wxT("Can't load GIF image")); | |
630 | else | |
631 | my_horse_gif = wxBitmap( image ); | |
632 | #endif | |
633 | ||
634 | #if wxUSE_PCX | |
635 | image.Destroy(); | |
636 | ||
637 | if ( !image.LoadFile( dir + _T("horse.pcx"), wxBITMAP_TYPE_PCX ) ) | |
638 | wxLogError(wxT("Can't load PCX image")); | |
639 | else | |
640 | my_horse_pcx = wxBitmap( image ); | |
641 | #endif | |
642 | ||
643 | image.Destroy(); | |
644 | ||
645 | if ( !image.LoadFile( dir + _T("horse.bmp"), wxBITMAP_TYPE_BMP ) ) | |
646 | wxLogError(wxT("Can't load BMP image")); | |
647 | else | |
648 | my_horse_bmp = wxBitmap( image ); | |
649 | ||
650 | #if wxUSE_XPM | |
651 | image.Destroy(); | |
652 | ||
653 | if ( !image.LoadFile( dir + _T("horse.xpm"), wxBITMAP_TYPE_XPM ) ) | |
654 | wxLogError(wxT("Can't load XPM image")); | |
655 | else | |
656 | my_horse_xpm = wxBitmap( image ); | |
657 | ||
658 | if ( !image.SaveFile( dir + _T("test.xpm"), wxBITMAP_TYPE_XPM )) | |
659 | wxLogError(wxT("Can't save file")); | |
660 | #endif | |
661 | ||
662 | #if wxUSE_PNM | |
663 | image.Destroy(); | |
664 | ||
665 | if ( !image.LoadFile( dir + _T("horse.pnm"), wxBITMAP_TYPE_PNM ) ) | |
666 | wxLogError(wxT("Can't load PNM image")); | |
667 | else | |
668 | my_horse_pnm = wxBitmap( image ); | |
669 | ||
670 | image.Destroy(); | |
671 | ||
672 | if ( !image.LoadFile( dir + _T("horse_ag.pnm"), wxBITMAP_TYPE_PNM ) ) | |
673 | wxLogError(wxT("Can't load PNM image")); | |
674 | else | |
675 | my_horse_asciigrey_pnm = wxBitmap( image ); | |
676 | ||
677 | image.Destroy(); | |
678 | ||
679 | if ( !image.LoadFile( dir + _T("horse_rg.pnm"), wxBITMAP_TYPE_PNM ) ) | |
680 | wxLogError(wxT("Can't load PNM image")); | |
681 | else | |
682 | my_horse_rawgrey_pnm = wxBitmap( image ); | |
683 | #endif | |
684 | ||
685 | #if wxUSE_LIBTIFF | |
686 | image.Destroy(); | |
687 | ||
688 | if ( !image.LoadFile( dir + _T("horse.tif"), wxBITMAP_TYPE_TIF ) ) | |
689 | wxLogError(wxT("Can't load TIFF image")); | |
690 | else | |
691 | my_horse_tiff = wxBitmap( image ); | |
692 | #endif | |
693 | ||
694 | #if wxUSE_LIBTIFF | |
695 | image.Destroy(); | |
696 | ||
697 | if ( !image.LoadFile( dir + _T("horse.tga"), wxBITMAP_TYPE_TGA ) ) | |
698 | wxLogError(wxT("Can't load TGA image")); | |
699 | else | |
700 | my_horse_tga = wxBitmap( image ); | |
701 | #endif | |
702 | ||
703 | CreateAntiAliasedBitmap(); | |
704 | ||
705 | my_smile_xbm = wxBitmap( (const char*)smile_bits, smile_width, | |
706 | smile_height, 1 ); | |
707 | ||
708 | // demonstrates XPM automatically using the mask when saving | |
709 | if ( m_bmpSmileXpm.Ok() ) | |
710 | m_bmpSmileXpm.SaveFile(_T("saved.xpm"), wxBITMAP_TYPE_XPM); | |
711 | ||
712 | #if wxUSE_ICO_CUR | |
713 | image.Destroy(); | |
714 | ||
715 | if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 0 ) ) | |
716 | wxLogError(wxT("Can't load first ICO image")); | |
717 | else | |
718 | my_horse_ico32 = wxBitmap( image ); | |
719 | ||
720 | image.Destroy(); | |
721 | ||
722 | if ( !image.LoadFile( dir + _T("horse.ico"), wxBITMAP_TYPE_ICO, 1 ) ) | |
723 | wxLogError(wxT("Can't load second ICO image")); | |
724 | else | |
725 | my_horse_ico16 = wxBitmap( image ); | |
726 | ||
727 | image.Destroy(); | |
728 | ||
729 | if ( !image.LoadFile( dir + _T("horse.ico") ) ) | |
730 | wxLogError(wxT("Can't load best ICO image")); | |
731 | else | |
732 | my_horse_ico = wxBitmap( image ); | |
733 | ||
734 | image.Destroy(); | |
735 | ||
736 | if ( !image.LoadFile( dir + _T("horse.cur"), wxBITMAP_TYPE_CUR ) ) | |
737 | wxLogError(wxT("Can't load best ICO image")); | |
738 | else | |
739 | { | |
740 | my_horse_cur = wxBitmap( image ); | |
741 | xH = 30 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X) ; | |
742 | yH = 2420 + image.GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y) ; | |
743 | } | |
744 | ||
745 | m_ani_images = wxImage::GetImageCount ( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI ); | |
746 | if (m_ani_images==0) | |
747 | wxLogError(wxT("No ANI-format images found")); | |
748 | else | |
749 | my_horse_ani = new wxBitmap [m_ani_images]; | |
750 | int i ; | |
751 | for (i=0; i < m_ani_images; i++) | |
752 | { | |
753 | image.Destroy(); | |
754 | if (!image.LoadFile( dir + _T("horse3.ani"), wxBITMAP_TYPE_ANI, i )) | |
755 | { | |
756 | wxString tmp = wxT("Can't load image number "); | |
757 | tmp << i ; | |
758 | wxLogError(tmp); | |
759 | } | |
760 | else | |
761 | my_horse_ani [i] = wxBitmap( image ); | |
762 | } | |
763 | #endif // wxUSE_ICO_CUR | |
764 | ||
765 | image.Destroy(); | |
766 | ||
767 | // test image loading from stream | |
768 | wxFile file(dir + _T("horse.bmp")); | |
769 | if ( file.IsOpened() ) | |
770 | { | |
771 | wxFileOffset len = file.Length(); | |
772 | size_t dataSize = (size_t)len; | |
773 | void *data = malloc(dataSize); | |
774 | if ( file.Read(data, dataSize) != len ) | |
775 | wxLogError(_T("Reading bitmap file failed")); | |
776 | else | |
777 | { | |
778 | wxMemoryInputStream mis(data, dataSize); | |
779 | if ( !image.LoadFile(mis) ) | |
780 | wxLogError(wxT("Can't load BMP image from stream")); | |
781 | else | |
782 | my_horse_bmp2 = wxBitmap( image ); | |
783 | } | |
784 | ||
785 | free(data); | |
786 | } | |
787 | } | |
788 | ||
789 | MyCanvas::~MyCanvas() | |
790 | { | |
791 | delete [] my_horse_ani; | |
792 | } | |
793 | ||
794 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
795 | { | |
796 | wxPaintDC dc( this ); | |
797 | PrepareDC( dc ); | |
798 | ||
799 | dc.DrawText( _T("Loaded image"), 30, 10 ); | |
800 | if (my_square.Ok()) | |
801 | dc.DrawBitmap( my_square, 30, 30 ); | |
802 | ||
803 | dc.DrawText( _T("Drawn directly"), 150, 10 ); | |
804 | dc.SetBrush( wxBrush( wxT("orange"), wxSOLID ) ); | |
805 | dc.SetPen( *wxBLACK_PEN ); | |
806 | dc.DrawRectangle( 150, 30, 100, 100 ); | |
807 | dc.SetBrush( *wxWHITE_BRUSH ); | |
808 | dc.DrawRectangle( 170, 50, 60, 60 ); | |
809 | ||
810 | if (my_anti.Ok()) | |
811 | dc.DrawBitmap( my_anti, 280, 30 ); | |
812 | ||
813 | dc.DrawText( _T("PNG handler"), 30, 135 ); | |
814 | if (my_horse_png.Ok()) | |
815 | { | |
816 | dc.DrawBitmap( my_horse_png, 30, 150 ); | |
817 | wxRect rect(0,0,100,100); | |
818 | wxBitmap sub( my_horse_png.GetSubBitmap(rect) ); | |
819 | dc.DrawText( _T("GetSubBitmap()"), 280, 175 ); | |
820 | dc.DrawBitmap( sub, 280, 195 ); | |
821 | } | |
822 | ||
823 | dc.DrawText( _T("JPEG handler"), 30, 365 ); | |
824 | if (my_horse_jpeg.Ok()) | |
825 | dc.DrawBitmap( my_horse_jpeg, 30, 380 ); | |
826 | ||
827 | dc.DrawText( _T("Green rotated to red"), 280, 365 ); | |
828 | if (colorized_horse_jpeg.Ok()) | |
829 | dc.DrawBitmap( colorized_horse_jpeg, 280, 380 ); | |
830 | ||
831 | dc.DrawText( _T("CMYK JPEG image"), 530, 365 ); | |
832 | if (my_cmyk_jpeg.Ok()) | |
833 | dc.DrawBitmap( my_cmyk_jpeg, 530, 380 ); | |
834 | ||
835 | dc.DrawText( _T("GIF handler"), 30, 595 ); | |
836 | if (my_horse_gif.Ok()) | |
837 | dc.DrawBitmap( my_horse_gif, 30, 610 ); | |
838 | ||
839 | dc.DrawText( _T("PCX handler"), 30, 825 ); | |
840 | if (my_horse_pcx.Ok()) | |
841 | dc.DrawBitmap( my_horse_pcx, 30, 840 ); | |
842 | ||
843 | dc.DrawText( _T("BMP handler"), 30, 1055 ); | |
844 | if (my_horse_bmp.Ok()) | |
845 | dc.DrawBitmap( my_horse_bmp, 30, 1070 ); | |
846 | ||
847 | dc.DrawText( _T("BMP read from memory"), 280, 1055 ); | |
848 | if (my_horse_bmp2.Ok()) | |
849 | dc.DrawBitmap( my_horse_bmp2, 280, 1070 ); | |
850 | ||
851 | dc.DrawText( _T("PNM handler"), 30, 1285 ); | |
852 | if (my_horse_pnm.Ok()) | |
853 | dc.DrawBitmap( my_horse_pnm, 30, 1300 ); | |
854 | ||
855 | dc.DrawText( _T("PNM handler (ascii grey)"), 280, 1285 ); | |
856 | if (my_horse_asciigrey_pnm.Ok()) | |
857 | dc.DrawBitmap( my_horse_asciigrey_pnm, 280, 1300 ); | |
858 | ||
859 | dc.DrawText( _T("PNM handler (raw grey)"), 530, 1285 ); | |
860 | if (my_horse_rawgrey_pnm.Ok()) | |
861 | dc.DrawBitmap( my_horse_rawgrey_pnm, 530, 1300 ); | |
862 | ||
863 | dc.DrawText( _T("TIFF handler"), 30, 1515 ); | |
864 | if (my_horse_tiff.Ok()) | |
865 | dc.DrawBitmap( my_horse_tiff, 30, 1530 ); | |
866 | ||
867 | dc.DrawText( _T("TGA handler"), 30, 1745 ); | |
868 | if (my_horse_tga.Ok()) | |
869 | dc.DrawBitmap( my_horse_tga, 30, 1760 ); | |
870 | ||
871 | dc.DrawText( _T("XPM handler"), 30, 1975 ); | |
872 | if (my_horse_xpm.Ok()) | |
873 | dc.DrawBitmap( my_horse_xpm, 30, 2000 ); | |
874 | ||
875 | // toucans | |
876 | { | |
877 | int x = 750, y = 10, yy = 170; | |
878 | ||
879 | dc.DrawText(wxT("Original toucan"), x+50, y); | |
880 | dc.DrawBitmap(my_toucan, x, y+15, true); | |
881 | y += yy; | |
882 | dc.DrawText(wxT("Flipped horizontally"), x+50, y); | |
883 | dc.DrawBitmap(my_toucan_flipped_horiz, x, y+15, true); | |
884 | y += yy; | |
885 | dc.DrawText(wxT("Flipped vertically"), x+50, y); | |
886 | dc.DrawBitmap(my_toucan_flipped_vert, x, y+15, true); | |
887 | y += yy; | |
888 | dc.DrawText(wxT("Flipped both h&v"), x+50, y); | |
889 | dc.DrawBitmap(my_toucan_flipped_both, x, y+15, true); | |
890 | ||
891 | y += yy; | |
892 | dc.DrawText(wxT("In greyscale"), x+50, y); | |
893 | dc.DrawBitmap(my_toucan_grey, x, y+15, true); | |
894 | ||
895 | y += yy; | |
896 | dc.DrawText(wxT("Toucan's head"), x+50, y); | |
897 | dc.DrawBitmap(my_toucan_head, x, y+15, true); | |
898 | ||
899 | y += yy; | |
900 | dc.DrawText(wxT("Scaled with normal quality"), x+50, y); | |
901 | dc.DrawBitmap(my_toucan_scaled_normal, x, y+15, true); | |
902 | ||
903 | y += yy; | |
904 | dc.DrawText(wxT("Scaled with high quality"), x+50, y); | |
905 | dc.DrawBitmap(my_toucan_scaled_high, x, y+15, true); | |
906 | ||
907 | y += yy; | |
908 | dc.DrawText(wxT("Blured"), x+50, y); | |
909 | dc.DrawBitmap(my_toucan_blur, x, y+15, true); | |
910 | } | |
911 | ||
912 | if (my_smile_xbm.Ok()) | |
913 | { | |
914 | int x = 300, y = 1800; | |
915 | ||
916 | dc.DrawText( _T("XBM bitmap"), x, y ); | |
917 | dc.DrawText( _T("(green on red)"), x, y + 15 ); | |
918 | dc.SetTextForeground( _T("GREEN") ); | |
919 | dc.SetTextBackground( _T("RED") ); | |
920 | dc.DrawBitmap( my_smile_xbm, x, y + 30 ); | |
921 | ||
922 | dc.SetTextForeground( *wxBLACK ); | |
923 | dc.DrawText( _T("After wxImage conversion"), x + 120, y ); | |
924 | dc.DrawText( _T("(red on white)"), x + 120, y + 15 ); | |
925 | dc.SetTextForeground( wxT("RED") ); | |
926 | wxImage i = my_smile_xbm.ConvertToImage(); | |
927 | i.SetMaskColour( 255, 255, 255 ); | |
928 | i.Replace( 0, 0, 0, | |
929 | wxRED_PEN->GetColour().Red(), | |
930 | wxRED_PEN->GetColour().Green(), | |
931 | wxRED_PEN->GetColour().Blue() ); | |
932 | dc.DrawBitmap( wxBitmap(i), x + 120, y + 30, true ); | |
933 | dc.SetTextForeground( *wxBLACK ); | |
934 | } | |
935 | ||
936 | ||
937 | wxBitmap mono( 60,50,1 ); | |
938 | wxMemoryDC memdc; | |
939 | memdc.SelectObject( mono ); | |
940 | memdc.SetPen( *wxBLACK_PEN ); | |
941 | memdc.SetBrush( *wxWHITE_BRUSH ); | |
942 | memdc.DrawRectangle( 0,0,60,50 ); | |
943 | memdc.SetTextForeground( *wxBLACK ); | |
944 | #ifndef __WXGTK20__ | |
945 | // I cannot convince GTK2 to draw into mono bitmaps | |
946 | memdc.DrawText( _T("Hi!"), 5, 5 ); | |
947 | #endif | |
948 | memdc.SetBrush( *wxBLACK_BRUSH ); | |
949 | memdc.DrawRectangle( 33,5,20,20 ); | |
950 | memdc.SetPen( *wxRED_PEN ); | |
951 | memdc.DrawLine( 5, 42, 50, 42 ); | |
952 | memdc.SelectObject( wxNullBitmap ); | |
953 | ||
954 | if (mono.Ok()) | |
955 | { | |
956 | int x = 300, y = 1900; | |
957 | ||
958 | dc.DrawText( _T("Mono bitmap"), x, y ); | |
959 | dc.DrawText( _T("(red on green)"), x, y + 15 ); | |
960 | dc.SetTextForeground( wxT("RED") ); | |
961 | dc.SetTextBackground( wxT("GREEN") ); | |
962 | dc.DrawBitmap( mono, x, y + 30 ); | |
963 | ||
964 | dc.SetTextForeground( *wxBLACK ); | |
965 | dc.DrawText( _T("After wxImage conversion"), x + 120, y ); | |
966 | dc.DrawText( _T("(red on white)"), x + 120, y + 15 ); | |
967 | dc.SetTextForeground( wxT("RED") ); | |
968 | wxImage i = mono.ConvertToImage(); | |
969 | i.SetMaskColour( 255,255,255 ); | |
970 | i.Replace( 0,0,0, | |
971 | wxRED_PEN->GetColour().Red(), | |
972 | wxRED_PEN->GetColour().Green(), | |
973 | wxRED_PEN->GetColour().Blue() ); | |
974 | dc.DrawBitmap( wxBitmap(i), x + 120, y + 30, true ); | |
975 | dc.SetTextForeground( *wxBLACK ); | |
976 | } | |
977 | ||
978 | // For testing transparency | |
979 | dc.SetBrush( *wxRED_BRUSH ); | |
980 | dc.DrawRectangle( 20, 2220, 560, 68 ); | |
981 | ||
982 | dc.DrawText(_T("XPM bitmap"), 30, 2230 ); | |
983 | if ( m_bmpSmileXpm.Ok() ) | |
984 | dc.DrawBitmap(m_bmpSmileXpm, 30, 2250, true); | |
985 | ||
986 | dc.DrawText(_T("XPM icon"), 110, 2230 ); | |
987 | if ( m_iconSmileXpm.Ok() ) | |
988 | dc.DrawIcon(m_iconSmileXpm, 110, 2250); | |
989 | ||
990 | // testing icon -> bitmap conversion | |
991 | wxBitmap to_blit( m_iconSmileXpm ); | |
992 | if (to_blit.Ok()) | |
993 | { | |
994 | dc.DrawText( _T("SubBitmap"), 170, 2230 ); | |
995 | wxBitmap sub = to_blit.GetSubBitmap( wxRect(0,0,15,15) ); | |
996 | if (sub.Ok()) | |
997 | dc.DrawBitmap( sub, 170, 2250, true ); | |
998 | ||
999 | dc.DrawText( _T("Enlarged"), 250, 2230 ); | |
1000 | dc.SetUserScale( 1.5, 1.5 ); | |
1001 | dc.DrawBitmap( to_blit, (int)(250/1.5), (int)(2250/1.5), true ); | |
1002 | dc.SetUserScale( 2, 2 ); | |
1003 | dc.DrawBitmap( to_blit, (int)(300/2), (int)(2250/2), true ); | |
1004 | dc.SetUserScale( 1.0, 1.0 ); | |
1005 | ||
1006 | dc.DrawText( _T("Blit"), 400, 2230); | |
1007 | wxMemoryDC blit_dc; | |
1008 | blit_dc.SelectObject( to_blit ); | |
1009 | dc.Blit( 400, 2250, to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true ); | |
1010 | dc.SetUserScale( 1.5, 1.5 ); | |
1011 | dc.Blit( (int)(450/1.5), (int)(2250/1.5), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true ); | |
1012 | dc.SetUserScale( 2, 2 ); | |
1013 | dc.Blit( (int)(500/2), (int)(2250/2), to_blit.GetWidth(), to_blit.GetHeight(), &blit_dc, 0, 0, wxCOPY, true ); | |
1014 | dc.SetUserScale( 1.0, 1.0 ); | |
1015 | } | |
1016 | ||
1017 | dc.DrawText( _T("ICO handler (1st image)"), 30, 2290 ); | |
1018 | if (my_horse_ico32.Ok()) | |
1019 | dc.DrawBitmap( my_horse_ico32, 30, 2330, true ); | |
1020 | ||
1021 | dc.DrawText( _T("ICO handler (2nd image)"), 230, 2290 ); | |
1022 | if (my_horse_ico16.Ok()) | |
1023 | dc.DrawBitmap( my_horse_ico16, 230, 2330, true ); | |
1024 | ||
1025 | dc.DrawText( _T("ICO handler (best image)"), 430, 2290 ); | |
1026 | if (my_horse_ico.Ok()) | |
1027 | dc.DrawBitmap( my_horse_ico, 430, 2330, true ); | |
1028 | ||
1029 | dc.DrawText( _T("CUR handler"), 30, 2390 ); | |
1030 | if (my_horse_cur.Ok()) | |
1031 | { | |
1032 | dc.DrawBitmap( my_horse_cur, 30, 2420, true ); | |
1033 | dc.SetPen (*wxRED_PEN); | |
1034 | dc.DrawLine (xH-10,yH,xH+10,yH); | |
1035 | dc.DrawLine (xH,yH-10,xH,yH+10); | |
1036 | } | |
1037 | ||
1038 | dc.DrawText( _T("ANI handler"), 230, 2390 ); | |
1039 | for ( int i=0; i < m_ani_images; i++ ) | |
1040 | { | |
1041 | if (my_horse_ani[i].Ok()) | |
1042 | { | |
1043 | dc.DrawBitmap( my_horse_ani[i], 230 + i * 2 * my_horse_ani[i].GetWidth() , 2420, true ); | |
1044 | } | |
1045 | } | |
1046 | } | |
1047 | ||
1048 | void MyCanvas::CreateAntiAliasedBitmap() | |
1049 | { | |
1050 | wxBitmap bitmap( 300, 300 ); | |
1051 | ||
1052 | wxMemoryDC dc; | |
1053 | ||
1054 | dc.SelectObject( bitmap ); | |
1055 | ||
1056 | dc.Clear(); | |
1057 | ||
1058 | dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL) ); | |
1059 | dc.SetTextForeground( wxT("RED") ); | |
1060 | dc.DrawText( _T("This is anti-aliased Text."), 20, 5 ); | |
1061 | dc.DrawText( _T("And a Rectangle."), 20, 45 ); | |
1062 | ||
1063 | dc.SetBrush( *wxRED_BRUSH ); | |
1064 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
1065 | dc.DrawRoundedRectangle( 20, 85, 200, 180, 20 ); | |
1066 | ||
1067 | wxImage original= bitmap.ConvertToImage(); | |
1068 | wxImage anti( 150, 150 ); | |
1069 | ||
1070 | /* This is quite slow, but safe. Use wxImage::GetData() for speed instead. */ | |
1071 | ||
1072 | for (int y = 1; y < 149; y++) | |
1073 | for (int x = 1; x < 149; x++) | |
1074 | { | |
1075 | int red = original.GetRed( x*2, y*2 ) + | |
1076 | original.GetRed( x*2-1, y*2 ) + | |
1077 | original.GetRed( x*2, y*2+1 ) + | |
1078 | original.GetRed( x*2+1, y*2+1 ); | |
1079 | red = red/4; | |
1080 | ||
1081 | int green = original.GetGreen( x*2, y*2 ) + | |
1082 | original.GetGreen( x*2-1, y*2 ) + | |
1083 | original.GetGreen( x*2, y*2+1 ) + | |
1084 | original.GetGreen( x*2+1, y*2+1 ); | |
1085 | green = green/4; | |
1086 | ||
1087 | int blue = original.GetBlue( x*2, y*2 ) + | |
1088 | original.GetBlue( x*2-1, y*2 ) + | |
1089 | original.GetBlue( x*2, y*2+1 ) + | |
1090 | original.GetBlue( x*2+1, y*2+1 ); | |
1091 | blue = blue/4; | |
1092 | anti.SetRGB( x, y, (unsigned char)red, (unsigned char)green, (unsigned char)blue ); | |
1093 | } | |
1094 | my_anti = wxBitmap(anti); | |
1095 | } | |
1096 | ||
1097 | // MyFrame | |
1098 | ||
1099 | enum | |
1100 | { | |
1101 | ID_QUIT = wxID_EXIT, | |
1102 | ID_ABOUT = wxID_ABOUT, | |
1103 | ID_NEW = 100, | |
1104 | ID_SHOWRAW = 101 | |
1105 | }; | |
1106 | ||
1107 | IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) | |
1108 | ||
1109 | BEGIN_EVENT_TABLE(MyFrame,wxFrame) | |
1110 | EVT_MENU (ID_ABOUT, MyFrame::OnAbout) | |
1111 | EVT_MENU (ID_QUIT, MyFrame::OnQuit) | |
1112 | EVT_MENU (ID_NEW, MyFrame::OnNewFrame) | |
1113 | #ifdef wxHAVE_RAW_BITMAP | |
1114 | EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap) | |
1115 | #endif | |
1116 | ||
1117 | #if wxUSE_CLIPBOARD | |
1118 | EVT_MENU(wxID_COPY, MyFrame::OnCopy) | |
1119 | EVT_MENU(wxID_PASTE, MyFrame::OnPaste) | |
1120 | #endif // wxUSE_CLIPBOARD | |
1121 | END_EVENT_TABLE() | |
1122 | ||
1123 | MyFrame::MyFrame() | |
1124 | : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxImage sample"), | |
1125 | wxPoint(20, 20), wxSize(950, 700) ) | |
1126 | { | |
1127 | wxMenuBar *menu_bar = new wxMenuBar(); | |
1128 | ||
1129 | wxMenu *menuImage = new wxMenu; | |
1130 | menuImage->Append( ID_NEW, _T("&Show any image...\tCtrl-O")); | |
1131 | ||
1132 | #ifdef wxHAVE_RAW_BITMAP | |
1133 | menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R")); | |
1134 | #endif | |
1135 | menuImage->AppendSeparator(); | |
1136 | menuImage->Append( ID_ABOUT, _T("&About...")); | |
1137 | menuImage->AppendSeparator(); | |
1138 | menuImage->Append( ID_QUIT, _T("E&xit\tCtrl-Q")); | |
1139 | menu_bar->Append(menuImage, _T("&Image")); | |
1140 | ||
1141 | #if wxUSE_CLIPBOARD | |
1142 | wxMenu *menuClipboard = new wxMenu; | |
1143 | menuClipboard->Append(wxID_COPY, _T("&Copy test image\tCtrl-C")); | |
1144 | menuClipboard->Append(wxID_PASTE, _T("&Paste image\tCtrl-V")); | |
1145 | menu_bar->Append(menuClipboard, _T("&Clipboard")); | |
1146 | #endif // wxUSE_CLIPBOARD | |
1147 | ||
1148 | SetMenuBar( menu_bar ); | |
1149 | ||
1150 | #if wxUSE_STATUSBAR | |
1151 | CreateStatusBar(2); | |
1152 | int widths[] = { -1, 100 }; | |
1153 | SetStatusWidths( 2, widths ); | |
1154 | #endif // wxUSE_STATUSBAR | |
1155 | ||
1156 | m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) ); | |
1157 | ||
1158 | // 500 width * 2750 height | |
1159 | m_canvas->SetScrollbars( 10, 10, 50, 275 ); | |
1160 | } | |
1161 | ||
1162 | void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) | |
1163 | { | |
1164 | Close( true ); | |
1165 | } | |
1166 | ||
1167 | void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) | |
1168 | { | |
1169 | (void)wxMessageBox( _T("wxImage demo\n") | |
1170 | _T("Robert Roebling (c) 1998,2000"), | |
1171 | _T("About wxImage Demo"), wxICON_INFORMATION | wxOK ); | |
1172 | } | |
1173 | ||
1174 | void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) ) | |
1175 | { | |
1176 | #if wxUSE_FILEDLG | |
1177 | wxString filename = wxFileSelector(_T("Select image file")); | |
1178 | if ( !filename ) | |
1179 | return; | |
1180 | ||
1181 | wxImage image; | |
1182 | if ( !image.LoadFile(filename) ) | |
1183 | { | |
1184 | wxLogError(_T("Couldn't load image from '%s'."), filename.c_str()); | |
1185 | ||
1186 | return; | |
1187 | } | |
1188 | ||
1189 | (new MyImageFrame(this, filename, wxBitmap(image)))->Show(); | |
1190 | #endif // wxUSE_FILEDLG | |
1191 | } | |
1192 | ||
1193 | #ifdef wxHAVE_RAW_BITMAP | |
1194 | ||
1195 | void MyFrame::OnTestRawBitmap( wxCommandEvent &WXUNUSED(event) ) | |
1196 | { | |
1197 | (new MyRawBitmapFrame(this))->Show(); | |
1198 | } | |
1199 | ||
1200 | #endif // wxHAVE_RAW_BITMAP | |
1201 | ||
1202 | #if wxUSE_CLIPBOARD | |
1203 | ||
1204 | void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
1205 | { | |
1206 | wxBitmapDataObject *dobjBmp = new wxBitmapDataObject; | |
1207 | dobjBmp->SetBitmap(m_canvas->my_horse_png); | |
1208 | ||
1209 | wxTheClipboard->Open(); | |
1210 | ||
1211 | if ( !wxTheClipboard->SetData(dobjBmp) ) | |
1212 | { | |
1213 | wxLogError(_T("Failed to copy bitmap to clipboard")); | |
1214 | } | |
1215 | ||
1216 | wxTheClipboard->Close(); | |
1217 | } | |
1218 | ||
1219 | void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
1220 | { | |
1221 | wxBitmapDataObject dobjBmp; | |
1222 | ||
1223 | wxTheClipboard->Open(); | |
1224 | if ( !wxTheClipboard->GetData(dobjBmp) ) | |
1225 | { | |
1226 | wxLogMessage(_T("No bitmap data in the clipboard")); | |
1227 | } | |
1228 | else | |
1229 | { | |
1230 | (new MyImageFrame(this, _T("Clipboard"), dobjBmp.GetBitmap()))->Show(); | |
1231 | } | |
1232 | wxTheClipboard->Close(); | |
1233 | } | |
1234 | ||
1235 | #endif // wxUSE_CLIPBOARD | |
1236 | ||
1237 | //----------------------------------------------------------------------------- | |
1238 | // MyApp | |
1239 | //----------------------------------------------------------------------------- | |
1240 | ||
1241 | bool MyApp::OnInit() | |
1242 | { | |
1243 | if ( !wxApp::OnInit() ) | |
1244 | return false; | |
1245 | ||
1246 | wxInitAllImageHandlers(); | |
1247 | ||
1248 | wxFrame *frame = new MyFrame(); | |
1249 | frame->Show( true ); | |
1250 | ||
1251 | return true; | |
1252 | } |