]> git.saurik.com Git - wxWidgets.git/blame - samples/image/image.cpp
Implement wxBG_STYLE_TRANSPARENT support for wxGTK.
[wxWidgets.git] / samples / image / image.cpp
CommitLineData
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
a2bbedf0 9// (c) 2005-2009 Vadim Zeitlin
526954c5 10// Licence: wxWindows licence
5a566d89 11///////////////////////////////////////////////////////////////////////////////
01111366 12
3d05544e
JS
13// For compilers that support precompilation, includes "wx/wx.h".
14#include "wx/wxprec.h"
15
16#ifdef __BORLANDC__
17#pragma hdrstop
18#endif
19
20#ifndef WX_PRECOMP
9061c15c 21 #include "wx/wx.h"
3d05544e
JS
22#endif
23
01111366 24#include "wx/image.h"
a23fd0e1 25#include "wx/file.h"
5a566d89 26#include "wx/filename.h"
0a470e5e 27#include "wx/graphics.h"
a8d4e3ac
VS
28#include "wx/mstream.h"
29#include "wx/wfstream.h"
1971d23c 30#include "wx/quantize.h"
0a470e5e 31#include "wx/scopedptr.h"
95706ac1 32#include "wx/stopwatch.h"
ccec9093 33#include "wx/versioninfo.h"
a23fd0e1 34
da9df1f5
VZ
35#if wxUSE_CLIPBOARD
36 #include "wx/dataobj.h"
37 #include "wx/clipbrd.h"
38#endif // wxUSE_CLIPBOARD
39
284f2b59 40#if defined(__WXMSW__)
481721e8 41 #ifdef wxHAVE_RAW_BITMAP
9b24b388 42 #include "wx/rawbmp.h"
481721e8 43 #endif
c698eae5 44#endif
a8d4e3ac 45
284f2b59
RR
46#if defined(__WXMAC__) || defined(__WXGTK__)
47 #define wxHAVE_RAW_BITMAP
48 #include "wx/rawbmp.h"
49#endif
50
09ddabf7 51#include "canvas.h"
01111366 52
41f02b9a
FM
53#ifndef __WXMSW__
54 #include "../sample.xpm"
55#endif
01111366 56
09ddabf7
FM
57// ============================================================================
58// declarations
59// ============================================================================
01111366 60
09ddabf7
FM
61//-----------------------------------------------------------------------------
62// MyApp
63//-----------------------------------------------------------------------------
64
65class MyApp: public wxApp
01111366 66{
1d5b7a0b 67public:
09ddabf7 68 virtual bool OnInit();
01111366
RR
69};
70
09ddabf7 71// ----------------------------------------------------------------------------
01111366 72// MyFrame
09ddabf7 73// ----------------------------------------------------------------------------
18134a1c 74
01111366
RR
75class MyFrame: public wxFrame
76{
1d5b7a0b
VZ
77public:
78 MyFrame();
01111366 79
01111366 80 void OnAbout( wxCommandEvent &event );
ab0f0386 81 void OnNewFrame( wxCommandEvent &event );
37ba70a5 82 void OnImageInfo( wxCommandEvent &event );
95706ac1
VZ
83 void OnThumbnail( wxCommandEvent &event );
84
b3f04dc2
VZ
85#ifdef wxHAVE_RAW_BITMAP
86 void OnTestRawBitmap( wxCommandEvent &event );
87#endif // wxHAVE_RAW_BITMAP
0a470e5e
VZ
88#if wxUSE_GRAPHICS_CONTEXT
89 void OnTestGraphics(wxCommandEvent& event);
90#endif // wxUSE_GRAPHICS_CONTEXT
01111366 91 void OnQuit( wxCommandEvent &event );
1d5b7a0b 92
dd38c875 93#if wxUSE_CLIPBOARD
da9df1f5
VZ
94 void OnCopy(wxCommandEvent& event);
95 void OnPaste(wxCommandEvent& event);
96#endif // wxUSE_CLIPBOARD
97
01111366 98 MyCanvas *m_canvas;
1d5b7a0b 99
60a41aee 100private:
37ba70a5
VZ
101 // ask user for the file name and try to load an image from it
102 //
103 // return the file path on success, empty string if we failed to load the
104 // image or were cancelled by user
105 static wxString LoadUserImage(wxImage& image);
106
107
1d5b7a0b
VZ
108 DECLARE_DYNAMIC_CLASS(MyFrame)
109 DECLARE_EVENT_TABLE()
01111366
RR
110};
111
1d8acb7d
VZ
112// ----------------------------------------------------------------------------
113// Frame used for showing a standalone image
114// ----------------------------------------------------------------------------
115
116enum
117{
8b6264b4 118 ID_ROTATE_LEFT = wxID_HIGHEST+1,
05a8831a 119 ID_ROTATE_RIGHT,
8b6264b4
FM
120 ID_RESIZE,
121 ID_PAINT_BG
1d8acb7d
VZ
122};
123
ab0f0386
VZ
124class MyImageFrame : public wxFrame
125{
126public:
85fcb94f
VZ
127 MyImageFrame(wxFrame *parent, const wxString& desc, const wxImage& image)
128 {
129 Create(parent, desc, wxBitmap(image), image.GetImageCount(desc));
130 }
131
1d8acb7d 132 MyImageFrame(wxFrame *parent, const wxString& desc, const wxBitmap& bitmap)
ab0f0386 133 {
85fcb94f
VZ
134 Create(parent, desc, bitmap);
135 }
136
a2bbedf0 137private:
85fcb94f
VZ
138 bool Create(wxFrame *parent,
139 const wxString& desc,
140 const wxBitmap& bitmap,
141 int numImages = 1)
142 {
143 if ( !wxFrame::Create(parent, wxID_ANY,
9a83f860 144 wxString::Format(wxT("Image from %s"), desc),
85fcb94f
VZ
145 wxDefaultPosition, wxDefaultSize,
146 wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE) )
147 return false;
148
149 m_bitmap = bitmap;
a2bbedf0 150 m_zoom = 1.;
85fcb94f 151
1d8acb7d
VZ
152 wxMenu *menu = new wxMenu;
153 menu->Append(wxID_SAVE);
154 menu->AppendSeparator();
9a83f860 155 menu->AppendCheckItem(ID_PAINT_BG, wxT("&Paint background"),
437950ce 156 "Uncheck this for transparent images");
8b6264b4 157 menu->AppendSeparator();
9a83f860 158 menu->Append(ID_RESIZE, wxT("&Fit to window\tCtrl-F"));
a2bbedf0
VZ
159 menu->Append(wxID_ZOOM_IN, "Zoom &in\tCtrl-+");
160 menu->Append(wxID_ZOOM_OUT, "Zoom &out\tCtrl--");
161 menu->Append(wxID_ZOOM_100, "Reset zoom to &100%\tCtrl-1");
05a8831a 162 menu->AppendSeparator();
9a83f860
VZ
163 menu->Append(ID_ROTATE_LEFT, wxT("Rotate &left\tCtrl-L"));
164 menu->Append(ID_ROTATE_RIGHT, wxT("Rotate &right\tCtrl-R"));
1d8acb7d
VZ
165
166 wxMenuBar *mbar = new wxMenuBar;
9a83f860 167 mbar->Append(menu, wxT("&Image"));
1d8acb7d
VZ
168 SetMenuBar(mbar);
169
437950ce
VZ
170 mbar->Check(ID_PAINT_BG, true);
171
85fcb94f
VZ
172 CreateStatusBar(2);
173 if ( numImages != 1 )
174 SetStatusText(wxString::Format("%d images", numImages), 1);
05a8831a 175
ab0f0386 176 SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
05a8831a
VZ
177
178 UpdateStatusBar();
8b6264b4 179
85fcb94f
VZ
180 Show();
181
182 return true;
ab0f0386
VZ
183 }
184
0c543b7a
VZ
185 void OnEraseBackground(wxEraseEvent& WXUNUSED(event))
186 {
187 // do nothing here to be able to see how transparent images are shown
188 }
189
ab0f0386
VZ
190 void OnPaint(wxPaintEvent& WXUNUSED(event))
191 {
05a8831a 192 wxPaintDC dc(this);
8b6264b4 193
437950ce 194 if ( GetMenuBar()->IsChecked(ID_PAINT_BG) )
f6b4a1b9 195 dc.Clear();
8b6264b4 196
a2bbedf0
VZ
197 dc.SetUserScale(m_zoom, m_zoom);
198
05a8831a 199 const wxSize size = GetClientSize();
a2bbedf0
VZ
200 dc.DrawBitmap
201 (
202 m_bitmap,
203 dc.DeviceToLogicalX((size.x - m_zoom*m_bitmap.GetWidth())/2),
204 dc.DeviceToLogicalY((size.y - m_zoom*m_bitmap.GetHeight())/2),
205 true /* use mask */
206 );
ab0f0386
VZ
207 }
208
1d8acb7d 209 void OnSave(wxCommandEvent& WXUNUSED(event))
1971d23c 210 {
71307412 211#if wxUSE_FILEDLG
368d59f0 212 wxImage image = m_bitmap.ConvertToImage();
1971d23c 213
4693b20c 214 wxString savefilename = wxFileSelector( wxT("Save Image"),
71307412
WS
215 wxEmptyString,
216 wxEmptyString,
4693b20c 217 (const wxChar *)NULL,
d0ee33f5 218 wxT("BMP files (*.bmp)|*.bmp|")
c8688139 219#if wxUSE_LIBPNG
d0ee33f5 220 wxT("PNG files (*.png)|*.png|")
c8688139
VZ
221#endif
222#if wxUSE_LIBJPEG
d0ee33f5 223 wxT("JPEG files (*.jpg)|*.jpg|")
c8688139
VZ
224#endif
225#if wxUSE_GIF
d0ee33f5 226 wxT("GIF files (*.gif)|*.gif|")
c8688139
VZ
227#endif
228#if wxUSE_LIBTIFF
d0ee33f5 229 wxT("TIFF files (*.tif)|*.tif|")
c8688139
VZ
230#endif
231#if wxUSE_PCX
d0ee33f5 232 wxT("PCX files (*.pcx)|*.pcx|")
c8688139 233#endif
d0ee33f5
WS
234 wxT("ICO files (*.ico)|*.ico|")
235 wxT("CUR files (*.cur)|*.cur"),
ff3e84ff 236 wxFD_SAVE,
47f797bd 237 this);
1971d23c
VZ
238
239 if ( savefilename.empty() )
240 return;
241
5a566d89
VZ
242 wxString extension;
243 wxFileName::SplitPath(savefilename, NULL, NULL, &extension);
1971d23c 244
5a566d89 245 bool saved = false;
9a83f860 246 if ( extension == wxT("bmp") )
5a566d89
VZ
247 {
248 static const int bppvalues[] =
249 {
250 wxBMP_1BPP,
251 wxBMP_1BPP_BW,
252 wxBMP_4BPP,
253 wxBMP_8BPP,
254 wxBMP_8BPP_GREY,
255 wxBMP_8BPP_RED,
256 wxBMP_8BPP_PALETTE,
257 wxBMP_24BPP
258 };
259
24207dfc 260 const wxString bppchoices[] =
5a566d89 261 {
9a83f860
VZ
262 wxT("1 bpp color"),
263 wxT("1 bpp B&W"),
264 wxT("4 bpp color"),
265 wxT("8 bpp color"),
266 wxT("8 bpp greyscale"),
267 wxT("8 bpp red"),
268 wxT("8 bpp own palette"),
269 wxT("24 bpp")
5a566d89
VZ
270 };
271
9a83f860
VZ
272 int bppselection = wxGetSingleChoiceIndex(wxT("Set BMP BPP"),
273 wxT("Image sample: save file"),
09ddabf7
FM
274 WXSIZEOF(bppchoices),
275 bppchoices,
276 this);
5a566d89
VZ
277 if ( bppselection != -1 )
278 {
279 int format = bppvalues[bppselection];
280 image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, format);
281
282 if ( format == wxBMP_8BPP_PALETTE )
283 {
284 unsigned char *cmap = new unsigned char [256];
285 for ( int i = 0; i < 256; i++ )
286 cmap[i] = (unsigned char)i;
287 image.SetPalette(wxPalette(256, cmap, cmap, cmap));
288
d0ee33f5 289 delete[] cmap;
5a566d89
VZ
290 }
291 }
1971d23c 292 }
c8688139 293#if wxUSE_LIBPNG
9a83f860 294 else if ( extension == wxT("png") )
5a566d89
VZ
295 {
296 static const int pngvalues[] =
297 {
298 wxPNG_TYPE_COLOUR,
299 wxPNG_TYPE_COLOUR,
300 wxPNG_TYPE_GREY,
301 wxPNG_TYPE_GREY,
302 wxPNG_TYPE_GREY_RED,
303 wxPNG_TYPE_GREY_RED,
304 };
305
24207dfc 306 const wxString pngchoices[] =
5a566d89 307 {
9a83f860
VZ
308 wxT("Colour 8bpp"),
309 wxT("Colour 16bpp"),
310 wxT("Grey 8bpp"),
311 wxT("Grey 16bpp"),
312 wxT("Grey red 8bpp"),
313 wxT("Grey red 16bpp"),
5a566d89
VZ
314 };
315
9a83f860
VZ
316 int sel = wxGetSingleChoiceIndex(wxT("Set PNG format"),
317 wxT("Image sample: save file"),
09ddabf7
FM
318 WXSIZEOF(pngchoices),
319 pngchoices,
320 this);
5a566d89
VZ
321 if ( sel != -1 )
322 {
323 image.SetOption(wxIMAGE_OPTION_PNG_FORMAT, pngvalues[sel]);
324 image.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH, sel % 2 ? 16 : 8);
d19ce8c4
FM
325
326 // these values are taken from OptiPNG with -o3 switch
327 const wxString compressionChoices[] =
328 {
9a83f860
VZ
329 wxT("compression = 9, memory = 8, strategy = 0, filter = 0"),
330 wxT("compression = 9, memory = 9, strategy = 0, filter = 0"),
331 wxT("compression = 9, memory = 8, strategy = 1, filter = 0"),
332 wxT("compression = 9, memory = 9, strategy = 1, filter = 0"),
333 wxT("compression = 1, memory = 8, strategy = 2, filter = 0"),
334 wxT("compression = 1, memory = 9, strategy = 2, filter = 0"),
335 wxT("compression = 9, memory = 8, strategy = 0, filter = 5"),
336 wxT("compression = 9, memory = 9, strategy = 0, filter = 5"),
337 wxT("compression = 9, memory = 8, strategy = 1, filter = 5"),
338 wxT("compression = 9, memory = 9, strategy = 1, filter = 5"),
339 wxT("compression = 1, memory = 8, strategy = 2, filter = 5"),
340 wxT("compression = 1, memory = 9, strategy = 2, filter = 5"),
d19ce8c4
FM
341 };
342
9a83f860
VZ
343 int sel = wxGetSingleChoiceIndex(wxT("Select compression option (Cancel to use default)\n"),
344 wxT("PNG Compression Options"),
d19ce8c4
FM
345 WXSIZEOF(compressionChoices),
346 compressionChoices,
347 this);
348 if (sel != -1)
349 {
350 const int zc[] = {9, 9, 9, 9, 1, 1, 9, 9, 9, 9, 1, 1};
351 const int zm[] = {8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9};
352 const int zs[] = {0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2};
353 const int f[] = {0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
354 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8};
355
356 image.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL , zc[sel]);
357 image.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL , zm[sel]);
358 image.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY , zs[sel]);
359 image.SetOption(wxIMAGE_OPTION_PNG_FILTER , f[sel]);
360 image.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE, 1048576); // 1 MB
361 }
5a566d89
VZ
362 }
363 }
c8688139 364#endif // wxUSE_LIBPNG
9a83f860 365 else if ( extension == wxT("cur") )
45647dcf 366 {
51b07644
VZ
367 image.Rescale(32,32);
368 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0);
45647dcf
VS
369 image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0);
370 // This shows how you can save an image with explicitly
371 // specified image format:
5a566d89 372 saved = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR);
45647dcf 373 }
5a566d89
VZ
374
375 if ( !saved )
45647dcf
VS
376 {
377 // This one guesses image format from filename extension
378 // (it may fail if the extension is not recognized):
5a566d89 379 image.SaveFile(savefilename);
45647dcf 380 }
71307412 381#endif // wxUSE_FILEDLG
1971d23c
VZ
382 }
383
05a8831a
VZ
384 void OnResize(wxCommandEvent& WXUNUSED(event))
385 {
386 wxImage img(m_bitmap.ConvertToImage());
387
388 const wxSize size = GetClientSize();
389 img.Rescale(size.x, size.y, wxIMAGE_QUALITY_HIGH);
390 m_bitmap = wxBitmap(img);
391
392 UpdateStatusBar();
a2bbedf0
VZ
393 }
394
395 void OnZoom(wxCommandEvent& event)
396 {
397 if ( event.GetId() == wxID_ZOOM_IN )
398 m_zoom *= 1.2;
399 else if ( event.GetId() == wxID_ZOOM_OUT )
400 m_zoom /= 1.2;
401 else // wxID_ZOOM_100
402 m_zoom = 1.;
403
404 UpdateStatusBar();
05a8831a
VZ
405 }
406
1d8acb7d
VZ
407 void OnRotate(wxCommandEvent& event)
408 {
409 double angle = 5;
410 if ( event.GetId() == ID_ROTATE_LEFT )
411 angle = -angle;
412
413 wxImage img(m_bitmap.ConvertToImage());
05a8831a 414 img = img.Rotate(angle, wxPoint(img.GetWidth() / 2, img.GetHeight() / 2));
a1b806b9 415 if ( !img.IsOk() )
1d8acb7d 416 {
9a83f860 417 wxLogWarning(wxT("Rotation failed"));
1d8acb7d
VZ
418 return;
419 }
420
421 m_bitmap = wxBitmap(img);
05a8831a
VZ
422
423 UpdateStatusBar();
1d8acb7d
VZ
424 }
425
05a8831a
VZ
426 void UpdateStatusBar()
427 {
9a83f860 428 wxLogStatus(this, wxT("Image size: (%d, %d), zoom %.2f"),
05a8831a 429 m_bitmap.GetWidth(),
a2bbedf0
VZ
430 m_bitmap.GetHeight(),
431 m_zoom);
432 Refresh();
05a8831a
VZ
433 }
434
ab0f0386 435 wxBitmap m_bitmap;
a2bbedf0 436 double m_zoom;
ab0f0386
VZ
437
438 DECLARE_EVENT_TABLE()
439};
440
b3f04dc2
VZ
441#ifdef wxHAVE_RAW_BITMAP
442
443#include "wx/rawbmp.h"
444
445class MyRawBitmapFrame : public wxFrame
446{
447public:
448 enum
449 {
450 BORDER = 15,
451 SIZE = 150,
452 REAL_SIZE = SIZE - 2*BORDER
453 };
454
455 MyRawBitmapFrame(wxFrame *parent)
9a83f860 456 : wxFrame(parent, wxID_ANY, wxT("Raw bitmaps (how exciting)")),
09ddabf7
FM
457 m_bitmap(SIZE, SIZE, 24),
458 m_alphaBitmap(SIZE, SIZE, 32)
b3f04dc2 459 {
8ef08948 460 SetClientSize(SIZE, SIZE*2+25);
b3f04dc2 461
8ef08948
RD
462 InitAlphaBitmap();
463 InitBitmap();
66df4ec6 464
8ef08948
RD
465 }
466
467 void InitAlphaBitmap()
468 {
6e5551ad
RR
469 // First, clear the whole bitmap by making it alpha
470 {
8ef08948 471 wxAlphaPixelData data( m_alphaBitmap, wxPoint(0,0), wxSize(SIZE, SIZE) );
6e5551ad
RR
472 if ( !data )
473 {
9a83f860 474 wxLogError(wxT("Failed to gain raw access to bitmap data"));
6e5551ad
RR
475 return;
476 }
8ef08948 477 wxAlphaPixelData::Iterator p(data);
6e5551ad
RR
478 for ( int y = 0; y < SIZE; ++y )
479 {
8ef08948 480 wxAlphaPixelData::Iterator rowStart = p;
6e5551ad
RR
481 for ( int x = 0; x < SIZE; ++x )
482 {
483 p.Alpha() = 0;
484 ++p; // same as p.OffsetX(1)
485 }
486 p = rowStart;
487 p.OffsetY(data, 1);
488 }
489 }
095980e1 490
6e5551ad 491 // Then, draw colourful alpha-blended stripes
8ef08948 492 wxAlphaPixelData data(m_alphaBitmap, wxPoint(BORDER, BORDER),
09ddabf7 493 wxSize(REAL_SIZE, REAL_SIZE));
b3f04dc2
VZ
494 if ( !data )
495 {
9a83f860 496 wxLogError(wxT("Failed to gain raw access to bitmap data"));
b3f04dc2
VZ
497 return;
498 }
499
8ef08948 500 wxAlphaPixelData::Iterator p(data);
b3f04dc2 501
b3f04dc2
VZ
502 for ( int y = 0; y < REAL_SIZE; ++y )
503 {
8ef08948 504 wxAlphaPixelData::Iterator rowStart = p;
b3f04dc2
VZ
505
506 int r = y < REAL_SIZE/3 ? 255 : 0,
507 g = (REAL_SIZE/3 <= y) && (y < 2*(REAL_SIZE/3)) ? 255 : 0,
508 b = 2*(REAL_SIZE/3) <= y ? 255 : 0;
509
510 for ( int x = 0; x < REAL_SIZE; ++x )
511 {
b068dfe2 512 // note that RGB must be premultiplied by alpha
8ef08948 513 unsigned a = (wxAlphaPixelData::Iterator::ChannelType)((x*255.)/REAL_SIZE);
5c99abbe
MW
514 p.Red() = r * a / 256;
515 p.Green() = g * a / 256;
516 p.Blue() = b * a / 256;
b068dfe2 517 p.Alpha() = a;
b3f04dc2
VZ
518
519 ++p; // same as p.OffsetX(1)
520 }
521
522 p = rowStart;
1d2f48b6 523 p.OffsetY(data, 1);
b3f04dc2
VZ
524 }
525 }
526
8ef08948
RD
527 void InitBitmap()
528 {
529 // draw some colourful stripes without alpha
530 wxNativePixelData data(m_bitmap);
531 if ( !data )
532 {
9a83f860 533 wxLogError(wxT("Failed to gain raw access to bitmap data"));
8ef08948
RD
534 return;
535 }
536
537 wxNativePixelData::Iterator p(data);
538 for ( int y = 0; y < SIZE; ++y )
539 {
540 wxNativePixelData::Iterator rowStart = p;
541
542 int r = y < SIZE/3 ? 255 : 0,
543 g = (SIZE/3 <= y) && (y < 2*(SIZE/3)) ? 255 : 0,
544 b = 2*(SIZE/3) <= y ? 255 : 0;
545
546 for ( int x = 0; x < SIZE; ++x )
547 {
548 p.Red() = r;
549 p.Green() = g;
550 p.Blue() = b;
551 ++p; // same as p.OffsetX(1)
552 }
553
554 p = rowStart;
555 p.OffsetY(data, 1);
556 }
557 }
558
b3f04dc2
VZ
559 void OnPaint(wxPaintEvent& WXUNUSED(event))
560 {
561 wxPaintDC dc( this );
9a83f860
VZ
562 dc.DrawText(wxT("This is alpha and raw bitmap test"), 0, BORDER);
563 dc.DrawText(wxT("This is alpha and raw bitmap test"), 0, SIZE/2 - BORDER);
564 dc.DrawText(wxT("This is alpha and raw bitmap test"), 0, SIZE - 2*BORDER);
8ef08948
RD
565 dc.DrawBitmap( m_alphaBitmap, 0, 0, true /* use mask */ );
566
9a83f860 567 dc.DrawText(wxT("Raw bitmap access without alpha"), 0, SIZE+5);
8ef08948 568 dc.DrawBitmap( m_bitmap, 0, SIZE+5+dc.GetCharHeight());
b3f04dc2
VZ
569 }
570
571private:
572 wxBitmap m_bitmap;
8ef08948 573 wxBitmap m_alphaBitmap;
b3f04dc2
VZ
574
575 DECLARE_EVENT_TABLE()
576};
577
578#endif // wxHAVE_RAW_BITMAP
579
01111366 580
09ddabf7
FM
581// ============================================================================
582// implementations
583// ============================================================================
01111366 584
09ddabf7
FM
585//-----------------------------------------------------------------------------
586// MyImageFrame
587//-----------------------------------------------------------------------------
01111366 588
ab0f0386 589BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
0c543b7a
VZ
590 EVT_ERASE_BACKGROUND(MyImageFrame::OnEraseBackground)
591 EVT_PAINT(MyImageFrame::OnPaint)
1d8acb7d
VZ
592
593 EVT_MENU(wxID_SAVE, MyImageFrame::OnSave)
594 EVT_MENU_RANGE(ID_ROTATE_LEFT, ID_ROTATE_RIGHT, MyImageFrame::OnRotate)
05a8831a 595 EVT_MENU(ID_RESIZE, MyImageFrame::OnResize)
a2bbedf0
VZ
596
597 EVT_MENU(wxID_ZOOM_IN, MyImageFrame::OnZoom)
598 EVT_MENU(wxID_ZOOM_OUT, MyImageFrame::OnZoom)
599 EVT_MENU(wxID_ZOOM_100, MyImageFrame::OnZoom)
ab0f0386
VZ
600END_EVENT_TABLE()
601
09ddabf7
FM
602//-----------------------------------------------------------------------------
603// MyRawBitmapFrame
604//-----------------------------------------------------------------------------
605
b3f04dc2
VZ
606#ifdef wxHAVE_RAW_BITMAP
607
608BEGIN_EVENT_TABLE(MyRawBitmapFrame, wxFrame)
609 EVT_PAINT(MyRawBitmapFrame::OnPaint)
610END_EVENT_TABLE()
611
612#endif // wxHAVE_RAW_BITMAP
613
09ddabf7 614//-----------------------------------------------------------------------------
01111366 615// MyFrame
09ddabf7 616//-----------------------------------------------------------------------------
01111366 617
b3f04dc2
VZ
618enum
619{
91b07357
JS
620 ID_QUIT = wxID_EXIT,
621 ID_ABOUT = wxID_ABOUT,
622 ID_NEW = 100,
37ba70a5 623 ID_INFO,
95706ac1 624 ID_SHOWRAW,
0a470e5e 625 ID_GRAPHICS,
95706ac1 626 ID_SHOWTHUMBNAIL
b3f04dc2 627};
01111366
RR
628
629IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
37ba70a5 630BEGIN_EVENT_TABLE(MyFrame, wxFrame)
09ddabf7
FM
631 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
632 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
633 EVT_MENU (ID_NEW, MyFrame::OnNewFrame)
634 EVT_MENU (ID_INFO, MyFrame::OnImageInfo)
635 EVT_MENU (ID_SHOWTHUMBNAIL, MyFrame::OnThumbnail)
b3f04dc2 636#ifdef wxHAVE_RAW_BITMAP
09ddabf7 637 EVT_MENU (ID_SHOWRAW, MyFrame::OnTestRawBitmap)
b3f04dc2 638#endif
0a470e5e
VZ
639#if wxUSE_GRAPHICS_CONTEXT
640 EVT_MENU (ID_GRAPHICS, MyFrame::OnTestGraphics)
641#endif // wxUSE_GRAPHICS_CONTEXT
dd38c875 642#if wxUSE_CLIPBOARD
da9df1f5
VZ
643 EVT_MENU(wxID_COPY, MyFrame::OnCopy)
644 EVT_MENU(wxID_PASTE, MyFrame::OnPaste)
645#endif // wxUSE_CLIPBOARD
01111366
RR
646END_EVENT_TABLE()
647
1d5b7a0b 648MyFrame::MyFrame()
9a83f860 649 : wxFrame( (wxFrame *)NULL, wxID_ANY, wxT("wxImage sample"),
09ddabf7 650 wxPoint(20, 20), wxSize(950, 700) )
01111366 651{
41f02b9a
FM
652 SetIcon(wxICON(sample));
653
09ddabf7 654 wxMenuBar *menu_bar = new wxMenuBar();
da9df1f5 655
09ddabf7 656 wxMenu *menuImage = new wxMenu;
9a83f860
VZ
657 menuImage->Append( ID_NEW, wxT("&Show any image...\tCtrl-O"));
658 menuImage->Append( ID_INFO, wxT("Show image &information...\tCtrl-I"));
b3f04dc2 659#ifdef wxHAVE_RAW_BITMAP
09ddabf7 660 menuImage->AppendSeparator();
9a83f860 661 menuImage->Append( ID_SHOWRAW, wxT("Test &raw bitmap...\tCtrl-R"));
b3f04dc2 662#endif
0a470e5e
VZ
663#if wxUSE_GRAPHICS_CONTEXT
664 menuImage->AppendSeparator();
665 menuImage->Append(ID_GRAPHICS, "Test &graphics context...\tCtrl-G");
666#endif // wxUSE_GRAPHICS_CONTEXT
09ddabf7 667 menuImage->AppendSeparator();
9a83f860 668 menuImage->Append( ID_SHOWTHUMBNAIL, wxT("Test &thumbnail...\tCtrl-T"),
09ddabf7
FM
669 "Test scaling the image during load (try with JPEG)");
670 menuImage->AppendSeparator();
2d143b66 671 menuImage->Append( ID_ABOUT, wxT("&About\tF1"));
09ddabf7 672 menuImage->AppendSeparator();
9a83f860
VZ
673 menuImage->Append( ID_QUIT, wxT("E&xit\tCtrl-Q"));
674 menu_bar->Append(menuImage, wxT("&Image"));
da9df1f5 675
dd38c875 676#if wxUSE_CLIPBOARD
09ddabf7 677 wxMenu *menuClipboard = new wxMenu;
9a83f860
VZ
678 menuClipboard->Append(wxID_COPY, wxT("&Copy test image\tCtrl-C"));
679 menuClipboard->Append(wxID_PASTE, wxT("&Paste image\tCtrl-V"));
680 menu_bar->Append(menuClipboard, wxT("&Clipboard"));
da9df1f5 681#endif // wxUSE_CLIPBOARD
3d05544e 682
09ddabf7 683 SetMenuBar( menu_bar );
1d5b7a0b 684
8520f137 685#if wxUSE_STATUSBAR
09ddabf7
FM
686 CreateStatusBar(2);
687 int widths[] = { -1, 100 };
688 SetStatusWidths( 2, widths );
8520f137 689#endif // wxUSE_STATUSBAR
1d5b7a0b 690
09ddabf7 691 m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) );
cbd4be25 692
09ddabf7
FM
693 // 500 width * 2750 height
694 m_canvas->SetScrollbars( 10, 10, 50, 275 );
9ea83ebc 695 m_canvas->SetCursor(wxImage("cursor.png"));
01111366
RR
696}
697
698void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
699{
09ddabf7 700 Close( true );
01111366
RR
701}
702
ccec9093
VZ
703#if wxUSE_ZLIB && wxUSE_STREAMS
704#include "wx/zstream.h"
705#endif
706
01111366
RR
707void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
708{
ccec9093
VZ
709 wxArrayString array;
710
711 array.Add("wxImage demo");
712 array.Add("(c) Robert Roebling 1998-2005");
713 array.Add("(c) Vadim Zeitlin 2005-2009");
714
715 array.Add(wxEmptyString);
716 array.Add("Version of the libraries used:");
717
718#if wxUSE_LIBPNG
719 array.Add(wxPNGHandler::GetLibraryVersionInfo().ToString());
720#endif
721#if wxUSE_LIBJPEG
722 array.Add(wxJPEGHandler::GetLibraryVersionInfo().ToString());
723#endif
724#if wxUSE_LIBTIFF
725 array.Add(wxTIFFHandler::GetLibraryVersionInfo().ToString());
726#endif
727#if wxUSE_ZLIB && wxUSE_STREAMS
728 // zlib is used by libpng
729 array.Add(wxGetZlibVersionInfo().ToString());
730#endif
731 (void)wxMessageBox( wxJoin(array, '\n'),
a2bbedf0
VZ
732 "About wxImage Demo",
733 wxICON_INFORMATION | wxOK );
fb1585ae
RR
734}
735
37ba70a5 736wxString MyFrame::LoadUserImage(wxImage& image)
ab0f0386 737{
37ba70a5
VZ
738 wxString filename;
739
71307412 740#if wxUSE_FILEDLG
9a83f860 741 filename = wxLoadFileSelector(wxT("image"), wxEmptyString);
37ba70a5
VZ
742 if ( !filename.empty() )
743 {
744 if ( !image.LoadFile(filename) )
745 {
9a83f860 746 wxLogError(wxT("Couldn't load image from '%s'."), filename.c_str());
f6bcfd97 747
37ba70a5
VZ
748 return wxEmptyString;
749 }
750 }
751#endif // wxUSE_FILEDLG
752
753 return filename;
754}
755
756void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
757{
f6bcfd97 758 wxImage image;
37ba70a5
VZ
759 wxString filename = LoadUserImage(image);
760 if ( !filename.empty() )
85fcb94f 761 new MyImageFrame(this, filename, image);
37ba70a5
VZ
762}
763
764void MyFrame::OnImageInfo( wxCommandEvent &WXUNUSED(event) )
765{
766 wxImage image;
767 if ( !LoadUserImage(image).empty() )
f6bcfd97 768 {
37ba70a5
VZ
769 // TODO: show more information about the file
770 wxString info = wxString::Format("Image size: %dx%d",
09ddabf7
FM
771 image.GetWidth(),
772 image.GetHeight());
37ba70a5
VZ
773
774 int xres = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX),
775 yres = image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY);
776 if ( xres || yres )
777 {
778 info += wxString::Format("\nResolution: %dx%d", xres, yres);
779 switch ( image.GetOptionInt(wxIMAGE_OPTION_RESOLUTIONUNIT) )
780 {
781 default:
782 wxFAIL_MSG( "unknown image resolution units" );
783 // fall through
f6bcfd97 784
37ba70a5
VZ
785 case wxIMAGE_RESOLUTION_NONE:
786 info += " in default units";
787 break;
f6bcfd97 788
37ba70a5
VZ
789 case wxIMAGE_RESOLUTION_INCHES:
790 info += " in";
791 break;
792
793 case wxIMAGE_RESOLUTION_CM:
794 info += " cm";
795 break;
796 }
797 }
798
799 wxLogMessage("%s", info);
800 }
ab0f0386
VZ
801}
802
b3f04dc2
VZ
803#ifdef wxHAVE_RAW_BITMAP
804
481721e8 805void MyFrame::OnTestRawBitmap( wxCommandEvent &WXUNUSED(event) )
b3f04dc2
VZ
806{
807 (new MyRawBitmapFrame(this))->Show();
808}
809
810#endif // wxHAVE_RAW_BITMAP
811
0a470e5e
VZ
812#if wxUSE_GRAPHICS_CONTEXT
813
814class MyGraphicsFrame : public wxFrame
815{
816public:
817 enum
818 {
819 WIDTH = 256,
820 HEIGHT = 90
821 };
822
823 MyGraphicsFrame(wxWindow* parent) :
824 wxFrame(parent, wxID_ANY, "Graphics context test"),
825 m_image(WIDTH, HEIGHT, false)
826 {
827 // Create a test image: it has 3 horizontal primary colour bands with
828 // alpha increasing from left to right.
829 m_image.SetAlpha();
830 unsigned char* alpha = m_image.GetAlpha();
831 unsigned char* data = m_image.GetData();
832
833 for ( int y = 0; y < HEIGHT; y++ )
834 {
835 unsigned char r = 0,
836 g = 0,
837 b = 0;
838 if ( y < HEIGHT/3 )
839 r = 0xff;
840 else if ( y < (2*HEIGHT)/3 )
841 g = 0xff;
842 else
843 b = 0xff;
844
845 for ( int x = 0; x < WIDTH; x++ )
846 {
847 *alpha++ = x;
848 *data++ = r;
849 *data++ = g;
850 *data++ = b;
851 }
852 }
853
854 m_bitmap = wxBitmap(m_image);
855
856 Connect(wxEVT_PAINT, wxPaintEventHandler(MyGraphicsFrame::OnPaint));
857
858 Show();
859 }
860
861private:
862 void OnPaint(wxPaintEvent& WXUNUSED(event))
863 {
864 wxPaintDC dc(this);
865 wxScopedPtr<wxGraphicsContext> gc(wxGraphicsContext::Create(dc));
866 wxGraphicsBitmap gb(gc->CreateBitmapFromImage(m_image));
867
868 gc->SetFont(*wxNORMAL_FONT, *wxBLACK);
0a470e5e
VZ
869 gc->DrawText("Bitmap", 0, HEIGHT/2);
870 gc->DrawBitmap(m_bitmap, 0, 0, WIDTH, HEIGHT);
871
fa378d36
VZ
872 wxGraphicsFont gf = gc->CreateFont(wxNORMAL_FONT->GetPixelSize().y, "");
873 gc->SetFont(gf);
0a470e5e
VZ
874 gc->DrawText("Graphics bitmap", 0, (3*HEIGHT)/2);
875 gc->DrawBitmap(gb, 0, HEIGHT, WIDTH, HEIGHT);
876 }
877
878 wxImage m_image;
879 wxBitmap m_bitmap;
880
881 wxDECLARE_NO_COPY_CLASS(MyGraphicsFrame);
882};
883
884void MyFrame::OnTestGraphics(wxCommandEvent& WXUNUSED(event))
885{
886 new MyGraphicsFrame(this);
887}
888
889#endif // wxUSE_GRAPHICS_CONTEXT
890
dd38c875 891#if wxUSE_CLIPBOARD
da9df1f5
VZ
892
893void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
894{
895 wxBitmapDataObject *dobjBmp = new wxBitmapDataObject;
bd981f27 896 dobjBmp->SetBitmap(m_canvas->my_horse_png);
da9df1f5 897
dd38c875
MB
898 wxTheClipboard->Open();
899
da9df1f5
VZ
900 if ( !wxTheClipboard->SetData(dobjBmp) )
901 {
9a83f860 902 wxLogError(wxT("Failed to copy bitmap to clipboard"));
da9df1f5 903 }
dd38c875
MB
904
905 wxTheClipboard->Close();
da9df1f5
VZ
906}
907
908void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
909{
910 wxBitmapDataObject dobjBmp;
dd38c875
MB
911
912 wxTheClipboard->Open();
da9df1f5
VZ
913 if ( !wxTheClipboard->GetData(dobjBmp) )
914 {
9a83f860 915 wxLogMessage(wxT("No bitmap data in the clipboard"));
da9df1f5
VZ
916 }
917 else
918 {
9a83f860 919 new MyImageFrame(this, wxT("Clipboard"), dobjBmp.GetBitmap());
da9df1f5 920 }
dd38c875 921 wxTheClipboard->Close();
da9df1f5
VZ
922}
923
924#endif // wxUSE_CLIPBOARD
925
95706ac1
VZ
926void MyFrame::OnThumbnail( wxCommandEvent &WXUNUSED(event) )
927{
928#if wxUSE_FILEDLG
9a83f860 929 wxString filename = wxLoadFileSelector(wxT("image"), wxEmptyString, wxEmptyString, this);
95706ac1
VZ
930 if ( filename.empty() )
931 return;
932
933 static const int THUMBNAIL_WIDTH = 320;
934 static const int THUMBNAIL_HEIGHT = 240;
935
936 wxImage image;
937 image.SetOption(wxIMAGE_OPTION_MAX_WIDTH, THUMBNAIL_WIDTH);
938 image.SetOption(wxIMAGE_OPTION_MAX_HEIGHT, THUMBNAIL_HEIGHT);
939
940 wxStopWatch sw;
941 if ( !image.LoadFile(filename) )
942 {
9a83f860 943 wxLogError(wxT("Couldn't load image from '%s'."), filename.c_str());
95706ac1
VZ
944 return;
945 }
946
b6963858
VZ
947 int origWidth = image.GetOptionInt( wxIMAGE_OPTION_ORIGINAL_WIDTH );
948 int origHeight = image.GetOptionInt( wxIMAGE_OPTION_ORIGINAL_HEIGHT );
949
95706ac1
VZ
950 const long loadTime = sw.Time();
951
85fcb94f 952 MyImageFrame * const frame = new MyImageFrame(this, filename, image);
b6963858
VZ
953 wxLogStatus(frame, "Loaded \"%s\" in %ldms; original size was (%d, %d)",
954 filename, loadTime, origWidth, origHeight);
95706ac1 955#else
9a83f860 956 wxLogError( wxT("Couldn't create file selector dialog") );
95706ac1
VZ
957 return;
958#endif // wxUSE_FILEDLG
959}
960
09ddabf7
FM
961//-----------------------------------------------------------------------------
962// MyApp
963//-----------------------------------------------------------------------------
964
965IMPLEMENT_APP(MyApp)
966
967bool MyApp::OnInit()
968{
969 if ( !wxApp::OnInit() )
970 return false;
971
972 wxInitAllImageHandlers();
973
974 wxFrame *frame = new MyFrame();
975 frame->Show( true );
976
977 return true;
978}