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