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