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