X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/09ddabf738ef243dc0f4965f69115eeb03a6dce2..9b35f81e4105ea250a6fd0533974cf6d1f722276:/samples/image/image.cpp diff --git a/samples/image/image.cpp b/samples/image/image.cpp index 584ad7aa5c..d265524368 100644 --- a/samples/image/image.cpp +++ b/samples/image/image.cpp @@ -6,7 +6,8 @@ // Created: 1998 // RCS-ID: $Id$ // Copyright: (c) 1998-2005 Robert Roebling -// License: wxWindows licence +// (c) 2005-2009 Vadim Zeitlin +// Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// // For compilers that support precompilation, includes "wx/wx.h". @@ -46,6 +47,9 @@ #include "canvas.h" +#ifndef __WXMSW__ + #include "../sample.xpm" +#endif // ============================================================================ // declarations @@ -114,34 +118,62 @@ enum class MyImageFrame : public wxFrame { public: + MyImageFrame(wxFrame *parent, const wxString& desc, const wxImage& image) + { + Create(parent, desc, wxBitmap(image), image.GetImageCount(desc)); + } + MyImageFrame(wxFrame *parent, const wxString& desc, const wxBitmap& bitmap) - : wxFrame(parent, wxID_ANY, - wxString::Format(_T("Image from %s"), desc.c_str()), - wxDefaultPosition, wxDefaultSize, - wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE), - m_bitmap(bitmap) { + Create(parent, desc, bitmap); + } + +private: + bool Create(wxFrame *parent, + const wxString& desc, + const wxBitmap& bitmap, + int numImages = 1) + { + if ( !wxFrame::Create(parent, wxID_ANY, + wxString::Format(wxT("Image from %s"), desc), + wxDefaultPosition, wxDefaultSize, + wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE) ) + return false; + + m_bitmap = bitmap; + m_zoom = 1.; + wxMenu *menu = new wxMenu; menu->Append(wxID_SAVE); menu->AppendSeparator(); - m_pClearBgMenu = menu->AppendCheckItem(ID_PAINT_BG, _T("&Paint background")); + menu->AppendCheckItem(ID_PAINT_BG, wxT("&Paint background"), + "Uncheck this for transparent images"); menu->AppendSeparator(); - menu->Append(ID_RESIZE, _T("&Fit to window\tCtrl-F")); + menu->Append(ID_RESIZE, wxT("&Fit to window\tCtrl-F")); + menu->Append(wxID_ZOOM_IN, "Zoom &in\tCtrl-+"); + menu->Append(wxID_ZOOM_OUT, "Zoom &out\tCtrl--"); + menu->Append(wxID_ZOOM_100, "Reset zoom to &100%\tCtrl-1"); menu->AppendSeparator(); - menu->Append(ID_ROTATE_LEFT, _T("Rotate &left\tCtrl-L")); - menu->Append(ID_ROTATE_RIGHT, _T("Rotate &right\tCtrl-R")); + menu->Append(ID_ROTATE_LEFT, wxT("Rotate &left\tCtrl-L")); + menu->Append(ID_ROTATE_RIGHT, wxT("Rotate &right\tCtrl-R")); wxMenuBar *mbar = new wxMenuBar; - mbar->Append(menu, _T("&Image")); + mbar->Append(menu, wxT("&Image")); SetMenuBar(mbar); - CreateStatusBar(); + mbar->Check(ID_PAINT_BG, true); + + CreateStatusBar(2); + if ( numImages != 1 ) + SetStatusText(wxString::Format("%d images", numImages), 1); SetClientSize(bitmap.GetWidth(), bitmap.GetHeight()); UpdateStatusBar(); -// SetBackgroundColour(*wxWHITE); + Show(); + + return true; } void OnEraseBackground(wxEraseEvent& WXUNUSED(event)) @@ -153,14 +185,19 @@ public: { wxPaintDC dc(this); - if (m_pClearBgMenu->IsChecked()) - ClearBackground(); + if ( GetMenuBar()->IsChecked(ID_PAINT_BG) ) + dc.Clear(); + + dc.SetUserScale(m_zoom, m_zoom); const wxSize size = GetClientSize(); - dc.DrawBitmap(m_bitmap, - (size.x - m_bitmap.GetWidth())/2, - (size.y - m_bitmap.GetHeight())/2, - true /* use mask */); + dc.DrawBitmap + ( + m_bitmap, + dc.DeviceToLogicalX((size.x - m_zoom*m_bitmap.GetWidth())/2), + dc.DeviceToLogicalY((size.y - m_zoom*m_bitmap.GetHeight())/2), + true /* use mask */ + ); } void OnSave(wxCommandEvent& WXUNUSED(event)) @@ -173,11 +210,21 @@ public: wxEmptyString, (const wxChar *)NULL, wxT("BMP files (*.bmp)|*.bmp|") +#if wxUSE_LIBPNG wxT("PNG files (*.png)|*.png|") +#endif +#if wxUSE_LIBJPEG wxT("JPEG files (*.jpg)|*.jpg|") +#endif +#if wxUSE_GIF wxT("GIF files (*.gif)|*.gif|") +#endif +#if wxUSE_LIBTIFF wxT("TIFF files (*.tif)|*.tif|") +#endif +#if wxUSE_PCX wxT("PCX files (*.pcx)|*.pcx|") +#endif wxT("ICO files (*.ico)|*.ico|") wxT("CUR files (*.cur)|*.cur"), wxFD_SAVE, @@ -190,7 +237,7 @@ public: wxFileName::SplitPath(savefilename, NULL, NULL, &extension); bool saved = false; - if ( extension == _T("bmp") ) + if ( extension == wxT("bmp") ) { static const int bppvalues[] = { @@ -206,18 +253,18 @@ public: const wxString bppchoices[] = { - _T("1 bpp color"), - _T("1 bpp B&W"), - _T("4 bpp color"), - _T("8 bpp color"), - _T("8 bpp greyscale"), - _T("8 bpp red"), - _T("8 bpp own palette"), - _T("24 bpp") + wxT("1 bpp color"), + wxT("1 bpp B&W"), + wxT("4 bpp color"), + wxT("8 bpp color"), + wxT("8 bpp greyscale"), + wxT("8 bpp red"), + wxT("8 bpp own palette"), + wxT("24 bpp") }; - int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"), - _T("Image sample: save file"), + int bppselection = wxGetSingleChoiceIndex(wxT("Set BMP BPP"), + wxT("Image sample: save file"), WXSIZEOF(bppchoices), bppchoices, this); @@ -237,7 +284,8 @@ public: } } } - else if ( extension == _T("png") ) +#if wxUSE_LIBPNG + else if ( extension == wxT("png") ) { static const int pngvalues[] = { @@ -251,16 +299,16 @@ public: const wxString pngchoices[] = { - _T("Colour 8bpp"), - _T("Colour 16bpp"), - _T("Grey 8bpp"), - _T("Grey 16bpp"), - _T("Grey red 8bpp"), - _T("Grey red 16bpp"), + wxT("Colour 8bpp"), + wxT("Colour 16bpp"), + wxT("Grey 8bpp"), + wxT("Grey 16bpp"), + wxT("Grey red 8bpp"), + wxT("Grey red 16bpp"), }; - int sel = wxGetSingleChoiceIndex(_T("Set PNG format"), - _T("Image sample: save file"), + int sel = wxGetSingleChoiceIndex(wxT("Set PNG format"), + wxT("Image sample: save file"), WXSIZEOF(pngchoices), pngchoices, this); @@ -268,9 +316,47 @@ public: { image.SetOption(wxIMAGE_OPTION_PNG_FORMAT, pngvalues[sel]); image.SetOption(wxIMAGE_OPTION_PNG_BITDEPTH, sel % 2 ? 16 : 8); + + // these values are taken from OptiPNG with -o3 switch + const wxString compressionChoices[] = + { + wxT("compression = 9, memory = 8, strategy = 0, filter = 0"), + wxT("compression = 9, memory = 9, strategy = 0, filter = 0"), + wxT("compression = 9, memory = 8, strategy = 1, filter = 0"), + wxT("compression = 9, memory = 9, strategy = 1, filter = 0"), + wxT("compression = 1, memory = 8, strategy = 2, filter = 0"), + wxT("compression = 1, memory = 9, strategy = 2, filter = 0"), + wxT("compression = 9, memory = 8, strategy = 0, filter = 5"), + wxT("compression = 9, memory = 9, strategy = 0, filter = 5"), + wxT("compression = 9, memory = 8, strategy = 1, filter = 5"), + wxT("compression = 9, memory = 9, strategy = 1, filter = 5"), + wxT("compression = 1, memory = 8, strategy = 2, filter = 5"), + wxT("compression = 1, memory = 9, strategy = 2, filter = 5"), + }; + + int sel = wxGetSingleChoiceIndex(wxT("Select compression option (Cancel to use default)\n"), + wxT("PNG Compression Options"), + WXSIZEOF(compressionChoices), + compressionChoices, + this); + if (sel != -1) + { + const int zc[] = {9, 9, 9, 9, 1, 1, 9, 9, 9, 9, 1, 1}; + const int zm[] = {8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9}; + const int zs[] = {0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2}; + const int f[] = {0x08, 0x08, 0x08, 0x08, 0x08, 0x08, + 0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8}; + + image.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_LEVEL , zc[sel]); + image.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_MEM_LEVEL , zm[sel]); + image.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_STRATEGY , zs[sel]); + image.SetOption(wxIMAGE_OPTION_PNG_FILTER , f[sel]); + image.SetOption(wxIMAGE_OPTION_PNG_COMPRESSION_BUFFER_SIZE, 1048576); // 1 MB + } } } - else if ( extension == _T("cur") ) +#endif // wxUSE_LIBPNG + else if ( extension == wxT("cur") ) { image.Rescale(32,32); image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0); @@ -298,7 +384,18 @@ public: m_bitmap = wxBitmap(img); UpdateStatusBar(); - Refresh(); + } + + void OnZoom(wxCommandEvent& event) + { + if ( event.GetId() == wxID_ZOOM_IN ) + m_zoom *= 1.2; + else if ( event.GetId() == wxID_ZOOM_OUT ) + m_zoom /= 1.2; + else // wxID_ZOOM_100 + m_zoom = 1.; + + UpdateStatusBar(); } void OnRotate(wxCommandEvent& event) @@ -311,26 +408,26 @@ public: img = img.Rotate(angle, wxPoint(img.GetWidth() / 2, img.GetHeight() / 2)); if ( !img.Ok() ) { - wxLogWarning(_T("Rotation failed")); + wxLogWarning(wxT("Rotation failed")); return; } m_bitmap = wxBitmap(img); UpdateStatusBar(); - Refresh(); } -private: void UpdateStatusBar() { - wxLogStatus(this, _T("Image size: (%d, %d)"), + wxLogStatus(this, wxT("Image size: (%d, %d), zoom %.2f"), m_bitmap.GetWidth(), - m_bitmap.GetHeight()); + m_bitmap.GetHeight(), + m_zoom); + Refresh(); } wxBitmap m_bitmap; - wxMenuItem* m_pClearBgMenu; + double m_zoom; DECLARE_EVENT_TABLE() }; @@ -350,7 +447,7 @@ public: }; MyRawBitmapFrame(wxFrame *parent) - : wxFrame(parent, wxID_ANY, _T("Raw bitmaps (how exciting)")), + : wxFrame(parent, wxID_ANY, wxT("Raw bitmaps (how exciting)")), m_bitmap(SIZE, SIZE, 24), m_alphaBitmap(SIZE, SIZE, 32) { @@ -368,7 +465,7 @@ public: wxAlphaPixelData data( m_alphaBitmap, wxPoint(0,0), wxSize(SIZE, SIZE) ); if ( !data ) { - wxLogError(_T("Failed to gain raw access to bitmap data")); + wxLogError(wxT("Failed to gain raw access to bitmap data")); return; } wxAlphaPixelData::Iterator p(data); @@ -390,7 +487,7 @@ public: wxSize(REAL_SIZE, REAL_SIZE)); if ( !data ) { - wxLogError(_T("Failed to gain raw access to bitmap data")); + wxLogError(wxT("Failed to gain raw access to bitmap data")); return; } @@ -427,7 +524,7 @@ public: wxNativePixelData data(m_bitmap); if ( !data ) { - wxLogError(_T("Failed to gain raw access to bitmap data")); + wxLogError(wxT("Failed to gain raw access to bitmap data")); return; } @@ -456,12 +553,12 @@ public: void OnPaint(wxPaintEvent& WXUNUSED(event)) { wxPaintDC dc( this ); - dc.DrawText(_T("This is alpha and raw bitmap test"), 0, BORDER); - dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE/2 - BORDER); - dc.DrawText(_T("This is alpha and raw bitmap test"), 0, SIZE - 2*BORDER); + dc.DrawText(wxT("This is alpha and raw bitmap test"), 0, BORDER); + dc.DrawText(wxT("This is alpha and raw bitmap test"), 0, SIZE/2 - BORDER); + dc.DrawText(wxT("This is alpha and raw bitmap test"), 0, SIZE - 2*BORDER); dc.DrawBitmap( m_alphaBitmap, 0, 0, true /* use mask */ ); - dc.DrawText(_T("Raw bitmap access without alpha"), 0, SIZE+5); + dc.DrawText(wxT("Raw bitmap access without alpha"), 0, SIZE+5); dc.DrawBitmap( m_bitmap, 0, SIZE+5+dc.GetCharHeight()); } @@ -490,6 +587,10 @@ BEGIN_EVENT_TABLE(MyImageFrame, wxFrame) EVT_MENU(wxID_SAVE, MyImageFrame::OnSave) EVT_MENU_RANGE(ID_ROTATE_LEFT, ID_ROTATE_RIGHT, MyImageFrame::OnRotate) EVT_MENU(ID_RESIZE, MyImageFrame::OnResize) + + EVT_MENU(wxID_ZOOM_IN, MyImageFrame::OnZoom) + EVT_MENU(wxID_ZOOM_OUT, MyImageFrame::OnZoom) + EVT_MENU(wxID_ZOOM_100, MyImageFrame::OnZoom) END_EVENT_TABLE() //----------------------------------------------------------------------------- @@ -535,32 +636,34 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) END_EVENT_TABLE() MyFrame::MyFrame() - : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxImage sample"), + : wxFrame( (wxFrame *)NULL, wxID_ANY, wxT("wxImage sample"), wxPoint(20, 20), wxSize(950, 700) ) { + SetIcon(wxICON(sample)); + wxMenuBar *menu_bar = new wxMenuBar(); wxMenu *menuImage = new wxMenu; - menuImage->Append( ID_NEW, _T("&Show any image...\tCtrl-O")); - menuImage->Append( ID_INFO, _T("Show image &information...\tCtrl-I")); + menuImage->Append( ID_NEW, wxT("&Show any image...\tCtrl-O")); + menuImage->Append( ID_INFO, wxT("Show image &information...\tCtrl-I")); #ifdef wxHAVE_RAW_BITMAP menuImage->AppendSeparator(); - menuImage->Append( ID_SHOWRAW, _T("Test &raw bitmap...\tCtrl-R")); + menuImage->Append( ID_SHOWRAW, wxT("Test &raw bitmap...\tCtrl-R")); #endif menuImage->AppendSeparator(); - menuImage->Append( ID_SHOWTHUMBNAIL, _T("Test &thumbnail...\tCtrl-T"), + menuImage->Append( ID_SHOWTHUMBNAIL, wxT("Test &thumbnail...\tCtrl-T"), "Test scaling the image during load (try with JPEG)"); menuImage->AppendSeparator(); - menuImage->Append( ID_ABOUT, _T("&About...")); + menuImage->Append( ID_ABOUT, wxT("&About...")); menuImage->AppendSeparator(); - menuImage->Append( ID_QUIT, _T("E&xit\tCtrl-Q")); - menu_bar->Append(menuImage, _T("&Image")); + menuImage->Append( ID_QUIT, wxT("E&xit\tCtrl-Q")); + menu_bar->Append(menuImage, wxT("&Image")); #if wxUSE_CLIPBOARD wxMenu *menuClipboard = new wxMenu; - menuClipboard->Append(wxID_COPY, _T("&Copy test image\tCtrl-C")); - menuClipboard->Append(wxID_PASTE, _T("&Paste image\tCtrl-V")); - menu_bar->Append(menuClipboard, _T("&Clipboard")); + menuClipboard->Append(wxID_COPY, wxT("&Copy test image\tCtrl-C")); + menuClipboard->Append(wxID_PASTE, wxT("&Paste image\tCtrl-V")); + menu_bar->Append(menuClipboard, wxT("&Clipboard")); #endif // wxUSE_CLIPBOARD SetMenuBar( menu_bar ); @@ -584,9 +687,11 @@ void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) { - (void)wxMessageBox( _T("wxImage demo\n") - _T("Robert Roebling (c) 1998,2000"), - _T("About wxImage Demo"), wxICON_INFORMATION | wxOK ); + (void)wxMessageBox( "wxImage demo\n" + "(c) Robert Roebling 1998-2005" + "(c) Vadim Zeitlin 2005-2009", + "About wxImage Demo", + wxICON_INFORMATION | wxOK ); } wxString MyFrame::LoadUserImage(wxImage& image) @@ -594,12 +699,12 @@ wxString MyFrame::LoadUserImage(wxImage& image) wxString filename; #if wxUSE_FILEDLG - filename = wxFileSelector(_T("Select image file")); + filename = wxLoadFileSelector(wxT("image"), wxEmptyString); if ( !filename.empty() ) { if ( !image.LoadFile(filename) ) { - wxLogError(_T("Couldn't load image from '%s'."), filename.c_str()); + wxLogError(wxT("Couldn't load image from '%s'."), filename.c_str()); return wxEmptyString; } @@ -614,7 +719,7 @@ void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) ) wxImage image; wxString filename = LoadUserImage(image); if ( !filename.empty() ) - (new MyImageFrame(this, filename, wxBitmap(image)))->Show(); + new MyImageFrame(this, filename, image); } void MyFrame::OnImageInfo( wxCommandEvent &WXUNUSED(event) ) @@ -676,7 +781,7 @@ void MyFrame::OnCopy(wxCommandEvent& WXUNUSED(event)) if ( !wxTheClipboard->SetData(dobjBmp) ) { - wxLogError(_T("Failed to copy bitmap to clipboard")); + wxLogError(wxT("Failed to copy bitmap to clipboard")); } wxTheClipboard->Close(); @@ -689,11 +794,11 @@ void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event)) wxTheClipboard->Open(); if ( !wxTheClipboard->GetData(dobjBmp) ) { - wxLogMessage(_T("No bitmap data in the clipboard")); + wxLogMessage(wxT("No bitmap data in the clipboard")); } else { - (new MyImageFrame(this, _T("Clipboard"), dobjBmp.GetBitmap()))->Show(); + new MyImageFrame(this, wxT("Clipboard"), dobjBmp.GetBitmap()); } wxTheClipboard->Close(); } @@ -703,7 +808,7 @@ void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnThumbnail( wxCommandEvent &WXUNUSED(event) ) { #if wxUSE_FILEDLG - wxString filename = wxFileSelector(_T("Select image file")); + wxString filename = wxLoadFileSelector(wxT("image"), wxEmptyString, wxEmptyString, this); if ( filename.empty() ) return; @@ -717,18 +822,16 @@ void MyFrame::OnThumbnail( wxCommandEvent &WXUNUSED(event) ) wxStopWatch sw; if ( !image.LoadFile(filename) ) { - wxLogError(_T("Couldn't load image from '%s'."), filename.c_str()); + wxLogError(wxT("Couldn't load image from '%s'."), filename.c_str()); return; } const long loadTime = sw.Time(); - MyImageFrame * const - frame = new MyImageFrame(this, filename, wxBitmap(image)); - frame->Show(); + MyImageFrame * const frame = new MyImageFrame(this, filename, image); wxLogStatus(frame, "Loaded \"%s\" in %ldms", filename, loadTime); #else - wxLogError( _T("Couldn't create file selector dialog") ); + wxLogError( wxT("Couldn't create file selector dialog") ); return; #endif // wxUSE_FILEDLG }