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