]>
Commit | Line | Data |
---|---|---|
5a566d89 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: samples/image/image.cpp | |
3 | // Purpose: sample showing operations with wxImage | |
4 | // Author: Robert Roebling | |
09ddabf7 | 5 | // Modified by: Francesco Montorsi |
5a566d89 VZ |
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" |
95706ac1 | 29 | #include "wx/stopwatch.h" |
a23fd0e1 | 30 | |
da9df1f5 VZ |
31 | #if wxUSE_CLIPBOARD |
32 | #include "wx/dataobj.h" | |
33 | #include "wx/clipbrd.h" | |
34 | #endif // wxUSE_CLIPBOARD | |
35 | ||
284f2b59 | 36 | #if defined(__WXMSW__) |
481721e8 | 37 | #ifdef wxHAVE_RAW_BITMAP |
9b24b388 | 38 | #include "wx/rawbmp.h" |
481721e8 | 39 | #endif |
c698eae5 | 40 | #endif |
a8d4e3ac | 41 | |
284f2b59 RR |
42 | #if defined(__WXMAC__) || defined(__WXGTK__) |
43 | #define wxHAVE_RAW_BITMAP | |
44 | #include "wx/rawbmp.h" | |
45 | #endif | |
46 | ||
09ddabf7 | 47 | #include "canvas.h" |
01111366 | 48 | |
01111366 | 49 | |
09ddabf7 FM |
50 | // ============================================================================ |
51 | // declarations | |
52 | // ============================================================================ | |
01111366 | 53 | |
09ddabf7 FM |
54 | //----------------------------------------------------------------------------- |
55 | // MyApp | |
56 | //----------------------------------------------------------------------------- | |
57 | ||
58 | class MyApp: public wxApp | |
01111366 | 59 | { |
1d5b7a0b | 60 | public: |
09ddabf7 | 61 | virtual bool OnInit(); |
01111366 RR |
62 | }; |
63 | ||
09ddabf7 | 64 | // ---------------------------------------------------------------------------- |
01111366 | 65 | // MyFrame |
09ddabf7 | 66 | // ---------------------------------------------------------------------------- |
18134a1c | 67 | |
01111366 RR |
68 | class MyFrame: public wxFrame |
69 | { | |
1d5b7a0b VZ |
70 | public: |
71 | MyFrame(); | |
01111366 | 72 | |
01111366 | 73 | void OnAbout( wxCommandEvent &event ); |
ab0f0386 | 74 | void OnNewFrame( wxCommandEvent &event ); |
37ba70a5 | 75 | void OnImageInfo( wxCommandEvent &event ); |
95706ac1 VZ |
76 | void OnThumbnail( wxCommandEvent &event ); |
77 | ||
b3f04dc2 VZ |
78 | #ifdef wxHAVE_RAW_BITMAP |
79 | void OnTestRawBitmap( wxCommandEvent &event ); | |
80 | #endif // wxHAVE_RAW_BITMAP | |
01111366 | 81 | void OnQuit( wxCommandEvent &event ); |
1d5b7a0b | 82 | |
dd38c875 | 83 | #if wxUSE_CLIPBOARD |
da9df1f5 VZ |
84 | void OnCopy(wxCommandEvent& event); |
85 | void OnPaste(wxCommandEvent& event); | |
86 | #endif // wxUSE_CLIPBOARD | |
87 | ||
01111366 | 88 | MyCanvas *m_canvas; |
1d5b7a0b | 89 | |
60a41aee | 90 | private: |
37ba70a5 VZ |
91 | // ask user for the file name and try to load an image from it |
92 | // | |
93 | // return the file path on success, empty string if we failed to load the | |
94 | // image or were cancelled by user | |
95 | static wxString LoadUserImage(wxImage& image); | |
96 | ||
97 | ||
1d5b7a0b VZ |
98 | DECLARE_DYNAMIC_CLASS(MyFrame) |
99 | DECLARE_EVENT_TABLE() | |
01111366 RR |
100 | }; |
101 | ||
1d8acb7d VZ |
102 | // ---------------------------------------------------------------------------- |
103 | // Frame used for showing a standalone image | |
104 | // ---------------------------------------------------------------------------- | |
105 | ||
106 | enum | |
107 | { | |
8b6264b4 | 108 | ID_ROTATE_LEFT = wxID_HIGHEST+1, |
05a8831a | 109 | ID_ROTATE_RIGHT, |
8b6264b4 FM |
110 | ID_RESIZE, |
111 | ID_PAINT_BG | |
1d8acb7d VZ |
112 | }; |
113 | ||
ab0f0386 VZ |
114 | class MyImageFrame : public wxFrame |
115 | { | |
116 | public: | |
1d8acb7d VZ |
117 | MyImageFrame(wxFrame *parent, const wxString& desc, const wxBitmap& bitmap) |
118 | : wxFrame(parent, wxID_ANY, | |
09ddabf7 FM |
119 | wxString::Format(_T("Image from %s"), desc.c_str()), |
120 | wxDefaultPosition, wxDefaultSize, | |
121 | wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE), | |
122 | m_bitmap(bitmap) | |
ab0f0386 | 123 | { |
1d8acb7d VZ |
124 | wxMenu *menu = new wxMenu; |
125 | menu->Append(wxID_SAVE); | |
126 | menu->AppendSeparator(); | |
8b6264b4 FM |
127 | m_pClearBgMenu = menu->AppendCheckItem(ID_PAINT_BG, _T("&Paint background")); |
128 | menu->AppendSeparator(); | |
05a8831a VZ |
129 | menu->Append(ID_RESIZE, _T("&Fit to window\tCtrl-F")); |
130 | menu->AppendSeparator(); | |
1d8acb7d VZ |
131 | menu->Append(ID_ROTATE_LEFT, _T("Rotate &left\tCtrl-L")); |
132 | menu->Append(ID_ROTATE_RIGHT, _T("Rotate &right\tCtrl-R")); | |
133 | ||
134 | wxMenuBar *mbar = new wxMenuBar; | |
135 | mbar->Append(menu, _T("&Image")); | |
136 | SetMenuBar(mbar); | |
137 | ||
05a8831a VZ |
138 | CreateStatusBar(); |
139 | ||
ab0f0386 | 140 | SetClientSize(bitmap.GetWidth(), bitmap.GetHeight()); |
05a8831a VZ |
141 | |
142 | UpdateStatusBar(); | |
8b6264b4 FM |
143 | |
144 | // SetBackgroundColour(*wxWHITE); | |
ab0f0386 VZ |
145 | } |
146 | ||
0c543b7a VZ |
147 | void OnEraseBackground(wxEraseEvent& WXUNUSED(event)) |
148 | { | |
149 | // do nothing here to be able to see how transparent images are shown | |
150 | } | |
151 | ||
ab0f0386 VZ |
152 | void OnPaint(wxPaintEvent& WXUNUSED(event)) |
153 | { | |
05a8831a | 154 | wxPaintDC dc(this); |
8b6264b4 FM |
155 | |
156 | if (m_pClearBgMenu->IsChecked()) | |
157 | ClearBackground(); | |
158 | ||
05a8831a VZ |
159 | const wxSize size = GetClientSize(); |
160 | dc.DrawBitmap(m_bitmap, | |
09ddabf7 FM |
161 | (size.x - m_bitmap.GetWidth())/2, |
162 | (size.y - m_bitmap.GetHeight())/2, | |
163 | true /* use mask */); | |
ab0f0386 VZ |
164 | } |
165 | ||
1d8acb7d | 166 | void OnSave(wxCommandEvent& WXUNUSED(event)) |
1971d23c | 167 | { |
71307412 | 168 | #if wxUSE_FILEDLG |
368d59f0 | 169 | wxImage image = m_bitmap.ConvertToImage(); |
1971d23c | 170 | |
4693b20c | 171 | wxString savefilename = wxFileSelector( wxT("Save Image"), |
71307412 WS |
172 | wxEmptyString, |
173 | wxEmptyString, | |
4693b20c | 174 | (const wxChar *)NULL, |
d0ee33f5 WS |
175 | wxT("BMP files (*.bmp)|*.bmp|") |
176 | wxT("PNG files (*.png)|*.png|") | |
177 | wxT("JPEG files (*.jpg)|*.jpg|") | |
178 | wxT("GIF files (*.gif)|*.gif|") | |
179 | wxT("TIFF files (*.tif)|*.tif|") | |
180 | wxT("PCX files (*.pcx)|*.pcx|") | |
181 | wxT("ICO files (*.ico)|*.ico|") | |
182 | wxT("CUR files (*.cur)|*.cur"), | |
ff3e84ff | 183 | wxFD_SAVE, |
47f797bd | 184 | this); |
1971d23c VZ |
185 | |
186 | if ( savefilename.empty() ) | |
187 | return; | |
188 | ||
5a566d89 VZ |
189 | wxString extension; |
190 | wxFileName::SplitPath(savefilename, NULL, NULL, &extension); | |
1971d23c | 191 | |
5a566d89 | 192 | bool saved = false; |
c66972cc | 193 | if ( extension == _T("bmp") ) |
5a566d89 VZ |
194 | { |
195 | static const int bppvalues[] = | |
196 | { | |
197 | wxBMP_1BPP, | |
198 | wxBMP_1BPP_BW, | |
199 | wxBMP_4BPP, | |
200 | wxBMP_8BPP, | |
201 | wxBMP_8BPP_GREY, | |
202 | wxBMP_8BPP_RED, | |
203 | wxBMP_8BPP_PALETTE, | |
204 | wxBMP_24BPP | |
205 | }; | |
206 | ||
24207dfc | 207 | const wxString bppchoices[] = |
5a566d89 VZ |
208 | { |
209 | _T("1 bpp color"), | |
210 | _T("1 bpp B&W"), | |
211 | _T("4 bpp color"), | |
212 | _T("8 bpp color"), | |
213 | _T("8 bpp greyscale"), | |
214 | _T("8 bpp red"), | |
215 | _T("8 bpp own palette"), | |
216 | _T("24 bpp") | |
217 | }; | |
218 | ||
219 | int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"), | |
09ddabf7 FM |
220 | _T("Image sample: save file"), |
221 | WXSIZEOF(bppchoices), | |
222 | bppchoices, | |
223 | this); | |
5a566d89 VZ |
224 | if ( bppselection != -1 ) |
225 | { | |
226 | int format = bppvalues[bppselection]; | |
227 | image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, format); | |
228 | ||
229 | if ( format == wxBMP_8BPP_PALETTE ) | |
230 | { | |
231 | unsigned char *cmap = new unsigned char [256]; | |
232 | for ( int i = 0; i < 256; i++ ) | |
233 | cmap[i] = (unsigned char)i; | |
234 | image.SetPalette(wxPalette(256, cmap, cmap, cmap)); | |
235 | ||
d0ee33f5 | 236 | delete[] cmap; |
5a566d89 VZ |
237 | } |
238 | } | |
1971d23c | 239 | } |
5a566d89 VZ |
240 | else if ( extension == _T("png") ) |
241 | { | |
242 | static const int pngvalues[] = | |
243 | { | |
244 | wxPNG_TYPE_COLOUR, | |
245 | wxPNG_TYPE_COLOUR, | |
246 | wxPNG_TYPE_GREY, | |
247 | wxPNG_TYPE_GREY, | |
248 | wxPNG_TYPE_GREY_RED, | |
249 | wxPNG_TYPE_GREY_RED, | |
250 | }; | |
251 | ||
24207dfc | 252 | const wxString pngchoices[] = |
5a566d89 VZ |
253 | { |
254 | _T("Colour 8bpp"), | |
255 | _T("Colour 16bpp"), | |
256 | _T("Grey 8bpp"), | |
257 | _T("Grey 16bpp"), | |
258 | _T("Grey red 8bpp"), | |
259 | _T("Grey red 16bpp"), | |
260 | }; | |
261 | ||
262 | int sel = wxGetSingleChoiceIndex(_T("Set PNG format"), | |
09ddabf7 FM |
263 | _T("Image sample: save file"), |
264 | WXSIZEOF(pngchoices), | |
265 | pngchoices, | |
266 | this); | |
5a566d89 VZ |
267 | if ( sel != -1 ) |
268 | { | |
269 | image.SetOption(wxIMAGE_OPTION_PNG_FORMAT, pngvalues[sel]); | |
270 | image.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH, sel % 2 ? 16 : 8); | |
d19ce8c4 FM |
271 | |
272 | // these values are taken from OptiPNG with -o3 switch | |
273 | const wxString compressionChoices[] = | |
274 | { | |
275 | _T("compression = 9, memory = 8, strategy = 0, filter = 0"), | |
276 | _T("compression = 9, memory = 9, strategy = 0, filter = 0"), | |
277 | _T("compression = 9, memory = 8, strategy = 1, filter = 0"), | |
278 | _T("compression = 9, memory = 9, strategy = 1, filter = 0"), | |
279 | _T("compression = 1, memory = 8, strategy = 2, filter = 0"), | |
280 | _T("compression = 1, memory = 9, strategy = 2, filter = 0"), | |
281 | _T("compression = 9, memory = 8, strategy = 0, filter = 5"), | |
282 | _T("compression = 9, memory = 9, strategy = 0, filter = 5"), | |
283 | _T("compression = 9, memory = 8, strategy = 1, filter = 5"), | |
284 | _T("compression = 9, memory = 9, strategy = 1, filter = 5"), | |
285 | _T("compression = 1, memory = 8, strategy = 2, filter = 5"), | |
286 | _T("compression = 1, memory = 9, strategy = 2, filter = 5"), | |
287 | }; | |
288 | ||
289 | int sel = wxGetSingleChoiceIndex(_T("Select compression option (Cancel to use default)\n"), | |
290 | _T("PNG Compression Options"), | |
291 | WXSIZEOF(compressionChoices), | |
292 | compressionChoices, | |
293 | this); | |
294 | if (sel != -1) | |
295 | { | |
296 | const int zc[] = {9, 9, 9, 9, 1, 1, 9, 9, 9, 9, 1, 1}; | |
297 | const int zm[] = {8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9}; | |
298 | const int zs[] = {0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2}; | |
299 | const int f[] = {0x08, 0x08, 0x08, 0x08, 0x08, 0x08, | |
300 | 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8}; | |
301 | ||
302 | image.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL , zc[sel]); | |
303 | image.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL , zm[sel]); | |
304 | image.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY , zs[sel]); | |
305 | image.SetOption(wxIMAGE_OPTION_PNG_FILTER , f[sel]); | |
306 | image.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE, 1048576); // 1 MB | |
307 | } | |
5a566d89 VZ |
308 | } |
309 | } | |
310 | else if ( extension == _T("cur") ) | |
45647dcf | 311 | { |
51b07644 VZ |
312 | image.Rescale(32,32); |
313 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0); | |
45647dcf VS |
314 | image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0); |
315 | // This shows how you can save an image with explicitly | |
316 | // specified image format: | |
5a566d89 | 317 | saved = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR); |
45647dcf | 318 | } |
5a566d89 VZ |
319 | |
320 | if ( !saved ) | |
45647dcf VS |
321 | { |
322 | // This one guesses image format from filename extension | |
323 | // (it may fail if the extension is not recognized): | |
5a566d89 | 324 | image.SaveFile(savefilename); |
45647dcf | 325 | } |
71307412 | 326 | #endif // wxUSE_FILEDLG |
1971d23c VZ |
327 | } |
328 | ||
05a8831a VZ |
329 | void OnResize(wxCommandEvent& WXUNUSED(event)) |
330 | { | |
331 | wxImage img(m_bitmap.ConvertToImage()); | |
332 | ||
333 | const wxSize size = GetClientSize(); | |
334 | img.Rescale(size.x, size.y, wxIMAGE_QUALITY_HIGH); | |
335 | m_bitmap = wxBitmap(img); | |
336 | ||
337 | UpdateStatusBar(); | |
338 | Refresh(); | |
339 | } | |
340 | ||
1d8acb7d VZ |
341 | void OnRotate(wxCommandEvent& event) |
342 | { | |
343 | double angle = 5; | |
344 | if ( event.GetId() == ID_ROTATE_LEFT ) | |
345 | angle = -angle; | |
346 | ||
347 | wxImage img(m_bitmap.ConvertToImage()); | |
05a8831a | 348 | img = img.Rotate(angle, wxPoint(img.GetWidth() / 2, img.GetHeight() / 2)); |
1d8acb7d VZ |
349 | if ( !img.Ok() ) |
350 | { | |
351 | wxLogWarning(_T("Rotation failed")); | |
352 | return; | |
353 | } | |
354 | ||
355 | m_bitmap = wxBitmap(img); | |
05a8831a VZ |
356 | |
357 | UpdateStatusBar(); | |
1d8acb7d VZ |
358 | Refresh(); |
359 | } | |
360 | ||
ab0f0386 | 361 | private: |
05a8831a VZ |
362 | void UpdateStatusBar() |
363 | { | |
364 | wxLogStatus(this, _T("Image size: (%d, %d)"), | |
365 | m_bitmap.GetWidth(), | |
366 | m_bitmap.GetHeight()); | |
367 | } | |
368 | ||
ab0f0386 | 369 | wxBitmap m_bitmap; |
8b6264b4 | 370 | wxMenuItem* m_pClearBgMenu; |
ab0f0386 VZ |
371 | |
372 | DECLARE_EVENT_TABLE() | |
373 | }; | |
374 | ||
b3f04dc2 VZ |
375 | #ifdef wxHAVE_RAW_BITMAP |
376 | ||
377 | #include "wx/rawbmp.h" | |
378 | ||
379 | class MyRawBitmapFrame : public wxFrame | |
380 | { | |
381 | public: | |
382 | enum | |
383 | { | |
384 | BORDER = 15, | |
385 | SIZE = 150, | |
386 | REAL_SIZE = SIZE - 2*BORDER | |
387 | }; | |
388 | ||
389 | MyRawBitmapFrame(wxFrame *parent) | |
47f797bd | 390 | : wxFrame(parent, wxID_ANY, _T("Raw bitmaps (how exciting)")), |
09ddabf7 FM |
391 | m_bitmap(SIZE, SIZE, 24), |
392 | m_alphaBitmap(SIZE, SIZE, 32) | |
b3f04dc2 | 393 | { |
8ef08948 | 394 | SetClientSize(SIZE, SIZE*2+25); |
b3f04dc2 | 395 | |
8ef08948 RD |
396 | InitAlphaBitmap(); |
397 | InitBitmap(); | |
66df4ec6 | 398 | |
8ef08948 RD |
399 | } |
400 | ||
401 | void InitAlphaBitmap() | |
402 | { | |
6e5551ad RR |
403 | // First, clear the whole bitmap by making it alpha |
404 | { | |
8ef08948 | 405 | wxAlphaPixelData data( m_alphaBitmap, wxPoint(0,0), wxSize(SIZE, SIZE) ); |
6e5551ad RR |
406 | if ( !data ) |
407 | { | |
408 | wxLogError(_T("Failed to gain raw access to bitmap data")); | |
409 | return; | |
410 | } | |
8ef08948 | 411 | wxAlphaPixelData::Iterator p(data); |
6e5551ad RR |
412 | for ( int y = 0; y < SIZE; ++y ) |
413 | { | |
8ef08948 | 414 | wxAlphaPixelData::Iterator rowStart = p; |
6e5551ad RR |
415 | for ( int x = 0; x < SIZE; ++x ) |
416 | { | |
417 | p.Alpha() = 0; | |
418 | ++p; // same as p.OffsetX(1) | |
419 | } | |
420 | p = rowStart; | |
421 | p.OffsetY(data, 1); | |
422 | } | |
423 | } | |
095980e1 | 424 | |
6e5551ad | 425 | // Then, draw colourful alpha-blended stripes |
8ef08948 | 426 | wxAlphaPixelData data(m_alphaBitmap, wxPoint(BORDER, BORDER), |
09ddabf7 | 427 | wxSize(REAL_SIZE, REAL_SIZE)); |
b3f04dc2 VZ |
428 | if ( !data ) |
429 | { | |
430 | wxLogError(_T("Failed to gain raw access to bitmap data")); | |
431 | return; | |
432 | } | |
433 | ||
8ef08948 | 434 | wxAlphaPixelData::Iterator p(data); |
b3f04dc2 | 435 | |
b3f04dc2 VZ |
436 | for ( int y = 0; y < REAL_SIZE; ++y ) |
437 | { | |
8ef08948 | 438 | wxAlphaPixelData::Iterator rowStart = p; |
b3f04dc2 VZ |
439 | |
440 | int r = y < REAL_SIZE/3 ? 255 : 0, | |
441 | g = (REAL_SIZE/3 <= y) && (y < 2*(REAL_SIZE/3)) ? 255 : 0, | |
442 | b = 2*(REAL_SIZE/3) <= y ? 255 : 0; | |
443 | ||
444 | for ( int x = 0; x < REAL_SIZE; ++x ) | |
445 | { | |
b068dfe2 | 446 | // note that RGB must be premultiplied by alpha |
8ef08948 | 447 | unsigned a = (wxAlphaPixelData::Iterator::ChannelType)((x*255.)/REAL_SIZE); |
5c99abbe MW |
448 | p.Red() = r * a / 256; |
449 | p.Green() = g * a / 256; | |
450 | p.Blue() = b * a / 256; | |
b068dfe2 | 451 | p.Alpha() = a; |
b3f04dc2 VZ |
452 | |
453 | ++p; // same as p.OffsetX(1) | |
454 | } | |
455 | ||
456 | p = rowStart; | |
1d2f48b6 | 457 | p.OffsetY(data, 1); |
b3f04dc2 VZ |
458 | } |
459 | } | |
460 | ||
8ef08948 RD |
461 | void InitBitmap() |
462 | { | |
463 | // draw some colourful stripes without alpha | |
464 | wxNativePixelData data(m_bitmap); | |
465 | if ( !data ) | |
466 | { | |
467 | wxLogError(_T("Failed to gain raw access to bitmap data")); | |
468 | return; | |
469 | } | |
470 | ||
471 | wxNativePixelData::Iterator p(data); | |
472 | for ( int y = 0; y < SIZE; ++y ) | |
473 | { | |
474 | wxNativePixelData::Iterator rowStart = p; | |
475 | ||
476 | int r = y < SIZE/3 ? 255 : 0, | |
477 | g = (SIZE/3 <= y) && (y < 2*(SIZE/3)) ? 255 : 0, | |
478 | b = 2*(SIZE/3) <= y ? 255 : 0; | |
479 | ||
480 | for ( int x = 0; x < SIZE; ++x ) | |
481 | { | |
482 | p.Red() = r; | |
483 | p.Green() = g; | |
484 | p.Blue() = b; | |
485 | ++p; // same as p.OffsetX(1) | |
486 | } | |
487 | ||
488 | p = rowStart; | |
489 | p.OffsetY(data, 1); | |
490 | } | |
491 | } | |
492 | ||
b3f04dc2 VZ |
493 | void OnPaint(wxPaintEvent& WXUNUSED(event)) |
494 | { | |
495 | wxPaintDC dc( this ); | |
496 | dc.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER); | |
497 | dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE/2 - BORDER); | |
498 | dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE - 2*BORDER); | |
8ef08948 RD |
499 | dc.DrawBitmap( m_alphaBitmap, 0, 0, true /* use mask */ ); |
500 | ||
501 | dc.DrawText(_T("Raw bitmap access without alpha"), 0, SIZE+5); | |
502 | dc.DrawBitmap( m_bitmap, 0, SIZE+5+dc.GetCharHeight()); | |
b3f04dc2 VZ |
503 | } |
504 | ||
505 | private: | |
506 | wxBitmap m_bitmap; | |
8ef08948 | 507 | wxBitmap m_alphaBitmap; |
b3f04dc2 VZ |
508 | |
509 | DECLARE_EVENT_TABLE() | |
510 | }; | |
511 | ||
512 | #endif // wxHAVE_RAW_BITMAP | |
513 | ||
01111366 | 514 | |
09ddabf7 FM |
515 | // ============================================================================ |
516 | // implementations | |
517 | // ============================================================================ | |
01111366 | 518 | |
09ddabf7 FM |
519 | //----------------------------------------------------------------------------- |
520 | // MyImageFrame | |
521 | //----------------------------------------------------------------------------- | |
01111366 | 522 | |
ab0f0386 | 523 | BEGIN_EVENT_TABLE(MyImageFrame, wxFrame) |
0c543b7a VZ |
524 | EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground) |
525 | EVT_PAINT(MyImageFrame::OnPaint) | |
1d8acb7d VZ |
526 | |
527 | EVT_MENU(wxID_SAVE, MyImageFrame::OnSave) | |
528 | EVT_MENU_RANGE(ID_ROTATE_LEFT, ID_ROTATE_RIGHT, MyImageFrame::OnRotate) | |
05a8831a | 529 | EVT_MENU(ID_RESIZE, MyImageFrame::OnResize) |
ab0f0386 VZ |
530 | END_EVENT_TABLE() |
531 | ||
09ddabf7 FM |
532 | //----------------------------------------------------------------------------- |
533 | // MyRawBitmapFrame | |
534 | //----------------------------------------------------------------------------- | |
535 | ||
b3f04dc2 VZ |
536 | #ifdef wxHAVE_RAW_BITMAP |
537 | ||
538 | BEGIN_EVENT_TABLE(MyRawBitmapFrame, wxFrame) | |
539 | EVT_PAINT(MyRawBitmapFrame::OnPaint) | |
540 | END_EVENT_TABLE() | |
541 | ||
542 | #endif // wxHAVE_RAW_BITMAP | |
543 | ||
09ddabf7 | 544 | //----------------------------------------------------------------------------- |
01111366 | 545 | // MyFrame |
09ddabf7 | 546 | //----------------------------------------------------------------------------- |
01111366 | 547 | |
b3f04dc2 VZ |
548 | enum |
549 | { | |
91b07357 JS |
550 | ID_QUIT = wxID_EXIT, |
551 | ID_ABOUT = wxID_ABOUT, | |
552 | ID_NEW = 100, | |
37ba70a5 | 553 | ID_INFO, |
95706ac1 VZ |
554 | ID_SHOWRAW, |
555 | ID_SHOWTHUMBNAIL | |
b3f04dc2 | 556 | }; |
01111366 RR |
557 | |
558 | IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) | |
37ba70a5 | 559 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
09ddabf7 FM |
560 | EVT_MENU (ID_ABOUT, MyFrame::OnAbout) |
561 | EVT_MENU (ID_QUIT, MyFrame::OnQuit) | |
562 | EVT_MENU (ID_NEW, MyFrame::OnNewFrame) | |
563 | EVT_MENU (ID_INFO, MyFrame::OnImageInfo) | |
564 | EVT_MENU (ID_SHOWTHUMBNAIL, MyFrame::OnThumbnail) | |
b3f04dc2 | 565 | #ifdef wxHAVE_RAW_BITMAP |
09ddabf7 | 566 | EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap) |
b3f04dc2 | 567 | #endif |
dd38c875 | 568 | #if wxUSE_CLIPBOARD |
da9df1f5 VZ |
569 | EVT_MENU(wxID_COPY, MyFrame::OnCopy) |
570 | EVT_MENU(wxID_PASTE, MyFrame::OnPaste) | |
571 | #endif // wxUSE_CLIPBOARD | |
01111366 RR |
572 | END_EVENT_TABLE() |
573 | ||
1d5b7a0b | 574 | MyFrame::MyFrame() |
09ddabf7 FM |
575 | : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxImage sample"), |
576 | wxPoint(20, 20), wxSize(950, 700) ) | |
01111366 | 577 | { |
09ddabf7 | 578 | wxMenuBar *menu_bar = new wxMenuBar(); |
da9df1f5 | 579 | |
09ddabf7 FM |
580 | wxMenu *menuImage = new wxMenu; |
581 | menuImage->Append( ID_NEW, _T("&Show any image...\tCtrl-O")); | |
582 | menuImage->Append( ID_INFO, _T("Show image &information...\tCtrl-I")); | |
b3f04dc2 | 583 | #ifdef wxHAVE_RAW_BITMAP |
09ddabf7 FM |
584 | menuImage->AppendSeparator(); |
585 | menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R")); | |
b3f04dc2 | 586 | #endif |
09ddabf7 FM |
587 | menuImage->AppendSeparator(); |
588 | menuImage->Append( ID_SHOWTHUMBNAIL, _T("Test &thumbnail...\tCtrl-T"), | |
589 | "Test scaling the image during load (try with JPEG)"); | |
590 | menuImage->AppendSeparator(); | |
591 | menuImage->Append( ID_ABOUT, _T("&About...")); | |
592 | menuImage->AppendSeparator(); | |
593 | menuImage->Append( ID_QUIT, _T("E&xit\tCtrl-Q")); | |
594 | menu_bar->Append(menuImage, _T("&Image")); | |
da9df1f5 | 595 | |
dd38c875 | 596 | #if wxUSE_CLIPBOARD |
09ddabf7 FM |
597 | wxMenu *menuClipboard = new wxMenu; |
598 | menuClipboard->Append(wxID_COPY, _T("&Copy test image\tCtrl-C")); | |
599 | menuClipboard->Append(wxID_PASTE, _T("&Paste image\tCtrl-V")); | |
600 | menu_bar->Append(menuClipboard, _T("&Clipboard")); | |
da9df1f5 | 601 | #endif // wxUSE_CLIPBOARD |
3d05544e | 602 | |
09ddabf7 | 603 | SetMenuBar( menu_bar ); |
1d5b7a0b | 604 | |
8520f137 | 605 | #if wxUSE_STATUSBAR |
09ddabf7 FM |
606 | CreateStatusBar(2); |
607 | int widths[] = { -1, 100 }; | |
608 | SetStatusWidths( 2, widths ); | |
8520f137 | 609 | #endif // wxUSE_STATUSBAR |
1d5b7a0b | 610 | |
09ddabf7 | 611 | m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) ); |
cbd4be25 | 612 | |
09ddabf7 FM |
613 | // 500 width * 2750 height |
614 | m_canvas->SetScrollbars( 10, 10, 50, 275 ); | |
01111366 RR |
615 | } |
616 | ||
617 | void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) | |
618 | { | |
09ddabf7 | 619 | Close( true ); |
01111366 RR |
620 | } |
621 | ||
622 | void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) | |
623 | { | |
09ddabf7 FM |
624 | (void)wxMessageBox( _T("wxImage demo\n") |
625 | _T("Robert Roebling (c) 1998,2000"), | |
626 | _T("About wxImage Demo"), wxICON_INFORMATION | wxOK ); | |
fb1585ae RR |
627 | } |
628 | ||
37ba70a5 | 629 | wxString MyFrame::LoadUserImage(wxImage& image) |
ab0f0386 | 630 | { |
37ba70a5 VZ |
631 | wxString filename; |
632 | ||
71307412 | 633 | #if wxUSE_FILEDLG |
37ba70a5 VZ |
634 | filename = wxFileSelector(_T("Select image file")); |
635 | if ( !filename.empty() ) | |
636 | { | |
637 | if ( !image.LoadFile(filename) ) | |
638 | { | |
639 | wxLogError(_T("Couldn't load image from '%s'."), filename.c_str()); | |
f6bcfd97 | 640 | |
37ba70a5 VZ |
641 | return wxEmptyString; |
642 | } | |
643 | } | |
644 | #endif // wxUSE_FILEDLG | |
645 | ||
646 | return filename; | |
647 | } | |
648 | ||
649 | void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) ) | |
650 | { | |
f6bcfd97 | 651 | wxImage image; |
37ba70a5 VZ |
652 | wxString filename = LoadUserImage(image); |
653 | if ( !filename.empty() ) | |
654 | (new MyImageFrame(this, filename, wxBitmap(image)))->Show(); | |
655 | } | |
656 | ||
657 | void MyFrame::OnImageInfo( wxCommandEvent &WXUNUSED(event) ) | |
658 | { | |
659 | wxImage image; | |
660 | if ( !LoadUserImage(image).empty() ) | |
f6bcfd97 | 661 | { |
37ba70a5 VZ |
662 | // TODO: show more information about the file |
663 | wxString info = wxString::Format("Image size: %dx%d", | |
09ddabf7 FM |
664 | image.GetWidth(), |
665 | image.GetHeight()); | |
37ba70a5 VZ |
666 | |
667 | int xres = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX), | |
668 | yres = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY); | |
669 | if ( xres || yres ) | |
670 | { | |
671 | info += wxString::Format("\nResolution: %dx%d", xres, yres); | |
672 | switch ( image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT) ) | |
673 | { | |
674 | default: | |
675 | wxFAIL_MSG( "unknown image resolution units" ); | |
676 | // fall through | |
f6bcfd97 | 677 | |
37ba70a5 VZ |
678 | case wxIMAGE_RESOLUTION_NONE: |
679 | info += " in default units"; | |
680 | break; | |
f6bcfd97 | 681 | |
37ba70a5 VZ |
682 | case wxIMAGE_RESOLUTION_INCHES: |
683 | info += " in"; | |
684 | break; | |
685 | ||
686 | case wxIMAGE_RESOLUTION_CM: | |
687 | info += " cm"; | |
688 | break; | |
689 | } | |
690 | } | |
691 | ||
692 | wxLogMessage("%s", info); | |
693 | } | |
ab0f0386 VZ |
694 | } |
695 | ||
b3f04dc2 VZ |
696 | #ifdef wxHAVE_RAW_BITMAP |
697 | ||
481721e8 | 698 | void MyFrame::OnTestRawBitmap( wxCommandEvent &WXUNUSED(event) ) |
b3f04dc2 VZ |
699 | { |
700 | (new MyRawBitmapFrame(this))->Show(); | |
701 | } | |
702 | ||
703 | #endif // wxHAVE_RAW_BITMAP | |
704 | ||
dd38c875 | 705 | #if wxUSE_CLIPBOARD |
da9df1f5 VZ |
706 | |
707 | void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
708 | { | |
709 | wxBitmapDataObject *dobjBmp = new wxBitmapDataObject; | |
bd981f27 | 710 | dobjBmp->SetBitmap(m_canvas->my_horse_png); |
da9df1f5 | 711 | |
dd38c875 MB |
712 | wxTheClipboard->Open(); |
713 | ||
da9df1f5 VZ |
714 | if ( !wxTheClipboard->SetData(dobjBmp) ) |
715 | { | |
716 | wxLogError(_T("Failed to copy bitmap to clipboard")); | |
717 | } | |
dd38c875 MB |
718 | |
719 | wxTheClipboard->Close(); | |
da9df1f5 VZ |
720 | } |
721 | ||
722 | void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
723 | { | |
724 | wxBitmapDataObject dobjBmp; | |
dd38c875 MB |
725 | |
726 | wxTheClipboard->Open(); | |
da9df1f5 VZ |
727 | if ( !wxTheClipboard->GetData(dobjBmp) ) |
728 | { | |
729 | wxLogMessage(_T("No bitmap data in the clipboard")); | |
730 | } | |
731 | else | |
732 | { | |
1d8acb7d | 733 | (new MyImageFrame(this, _T("Clipboard"), dobjBmp.GetBitmap()))->Show(); |
da9df1f5 | 734 | } |
dd38c875 | 735 | wxTheClipboard->Close(); |
da9df1f5 VZ |
736 | } |
737 | ||
738 | #endif // wxUSE_CLIPBOARD | |
739 | ||
95706ac1 VZ |
740 | void MyFrame::OnThumbnail( wxCommandEvent &WXUNUSED(event) ) |
741 | { | |
742 | #if wxUSE_FILEDLG | |
743 | wxString filename = wxFileSelector(_T("Select image file")); | |
744 | if ( filename.empty() ) | |
745 | return; | |
746 | ||
747 | static const int THUMBNAIL_WIDTH = 320; | |
748 | static const int THUMBNAIL_HEIGHT = 240; | |
749 | ||
750 | wxImage image; | |
751 | image.SetOption(wxIMAGE_OPTION_MAX_WIDTH, THUMBNAIL_WIDTH); | |
752 | image.SetOption(wxIMAGE_OPTION_MAX_HEIGHT, THUMBNAIL_HEIGHT); | |
753 | ||
754 | wxStopWatch sw; | |
755 | if ( !image.LoadFile(filename) ) | |
756 | { | |
757 | wxLogError(_T("Couldn't load image from '%s'."), filename.c_str()); | |
758 | return; | |
759 | } | |
760 | ||
761 | const long loadTime = sw.Time(); | |
762 | ||
763 | MyImageFrame * const | |
764 | frame = new MyImageFrame(this, filename, wxBitmap(image)); | |
765 | frame->Show(); | |
766 | wxLogStatus(frame, "Loaded \"%s\" in %ldms", filename, loadTime); | |
767 | #else | |
768 | wxLogError( _T("Couldn't create file selector dialog") ); | |
769 | return; | |
770 | #endif // wxUSE_FILEDLG | |
771 | } | |
772 | ||
09ddabf7 FM |
773 | //----------------------------------------------------------------------------- |
774 | // MyApp | |
775 | //----------------------------------------------------------------------------- | |
776 | ||
777 | IMPLEMENT_APP(MyApp) | |
778 | ||
779 | bool MyApp::OnInit() | |
780 | { | |
781 | if ( !wxApp::OnInit() ) | |
782 | return false; | |
783 | ||
784 | wxInitAllImageHandlers(); | |
785 | ||
786 | wxFrame *frame = new MyFrame(); | |
787 | frame->Show( true ); | |
788 | ||
789 | return true; | |
790 | } |