X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/09ddabf738ef243dc0f4965f69115eeb03a6dce2..43b2d5e7c3b3e3d6b245e279dde73d96e0075911:/samples/image/image.cpp diff --git a/samples/image/image.cpp b/samples/image/image.cpp index 584ad7aa5c..0466355e71 100644 --- a/samples/image/image.cpp +++ b/samples/image/image.cpp @@ -6,6 +6,7 @@ // Created: 1998 // RCS-ID: $Id$ // Copyright: (c) 1998-2005 Robert Roebling +// (c) 2005-2009 Vadim Zeitlin // License: wxWindows licence /////////////////////////////////////////////////////////////////////////////// @@ -46,6 +47,9 @@ #include "canvas.h" +#ifndef __WXMSW__ + #include "../sample.xpm" +#endif // ============================================================================ // declarations @@ -114,19 +118,41 @@ 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(_T("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, _T("&Paint background"), + "Uncheck this for transparent images"); menu->AppendSeparator(); menu->Append(ID_RESIZE, _T("&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")); @@ -135,13 +161,19 @@ public: mbar->Append(menu, _T("&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()) + if ( GetMenuBar()->IsChecked(ID_PAINT_BG) ) ClearBackground(); + 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)) @@ -268,6 +305,43 @@ 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[] = + { + _T("compression = 9, memory = 8, strategy = 0, filter = 0"), + _T("compression = 9, memory = 9, strategy = 0, filter = 0"), + _T("compression = 9, memory = 8, strategy = 1, filter = 0"), + _T("compression = 9, memory = 9, strategy = 1, filter = 0"), + _T("compression = 1, memory = 8, strategy = 2, filter = 0"), + _T("compression = 1, memory = 9, strategy = 2, filter = 0"), + _T("compression = 9, memory = 8, strategy = 0, filter = 5"), + _T("compression = 9, memory = 9, strategy = 0, filter = 5"), + _T("compression = 9, memory = 8, strategy = 1, filter = 5"), + _T("compression = 9, memory = 9, strategy = 1, filter = 5"), + _T("compression = 1, memory = 8, strategy = 2, filter = 5"), + _T("compression = 1, memory = 9, strategy = 2, filter = 5"), + }; + + int sel = wxGetSingleChoiceIndex(_T("Select compression option (Cancel to use default)\n"), + _T("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") ) @@ -298,7 +372,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) @@ -318,19 +403,19 @@ public: m_bitmap = wxBitmap(img); UpdateStatusBar(); - Refresh(); } -private: void UpdateStatusBar() { - wxLogStatus(this, _T("Image size: (%d, %d)"), + wxLogStatus(this, _T("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() }; @@ -490,6 +575,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() //----------------------------------------------------------------------------- @@ -538,6 +627,8 @@ MyFrame::MyFrame() : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxImage sample"), wxPoint(20, 20), wxSize(950, 700) ) { + SetIcon(wxICON(sample)); + wxMenuBar *menu_bar = new wxMenuBar(); wxMenu *menuImage = new wxMenu; @@ -584,9 +675,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,7 +687,7 @@ wxString MyFrame::LoadUserImage(wxImage& image) wxString filename; #if wxUSE_FILEDLG - filename = wxFileSelector(_T("Select image file")); + filename = wxLoadFileSelector(_T("image"), wxEmptyString); if ( !filename.empty() ) { if ( !image.LoadFile(filename) ) @@ -614,7 +707,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) ) @@ -693,7 +786,7 @@ void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event)) } else { - (new MyImageFrame(this, _T("Clipboard"), dobjBmp.GetBitmap()))->Show(); + new MyImageFrame(this, _T("Clipboard"), dobjBmp.GetBitmap()); } wxTheClipboard->Close(); } @@ -703,7 +796,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(_T("image"), wxEmptyString, wxEmptyString, this); if ( filename.empty() ) return; @@ -723,9 +816,7 @@ void MyFrame::OnThumbnail( wxCommandEvent &WXUNUSED(event) ) 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") );