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