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