+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)
+ {
+ 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();
+ menu->AppendCheckItem(ID_PAINT_BG, wxT("&Paint background"),
+ "Uncheck this for transparent images");
+ menu->AppendSeparator();
+ 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, wxT("Rotate &left\tCtrl-L"));
+ menu->Append(ID_ROTATE_RIGHT, wxT("Rotate &right\tCtrl-R"));
+
+ wxMenuBar *mbar = new wxMenuBar;
+ mbar->Append(menu, wxT("&Image"));
+ SetMenuBar(mbar);
+
+ mbar->Check(ID_PAINT_BG, true);
+
+ CreateStatusBar(2);
+ if ( numImages != 1 )
+ SetStatusText(wxString::Format("%d images", numImages), 1);
+
+ SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
+
+ UpdateStatusBar();
+
+ Show();
+
+ return true;
+ }
+
+ void OnEraseBackground(wxEraseEvent& WXUNUSED(event))
+ {
+ // do nothing here to be able to see how transparent images are shown
+ }
+
+ void OnPaint(wxPaintEvent& WXUNUSED(event))
+ {
+ wxPaintDC dc(this);
+
+ if ( GetMenuBar()->IsChecked(ID_PAINT_BG) )
+ dc.Clear();
+
+ dc.SetUserScale(m_zoom, m_zoom);
+
+ const wxSize size = GetClientSize();
+ 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))
+ {
+#if wxUSE_FILEDLG
+ wxImage image = m_bitmap.ConvertToImage();
+
+ wxString savefilename = wxFileSelector( wxT("Save Image"),
+ wxEmptyString,
+ 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,
+ this);
+
+ if ( savefilename.empty() )
+ return;
+
+ wxString extension;
+ wxFileName::SplitPath(savefilename, NULL, NULL, &extension);
+
+ bool saved = false;
+ if ( extension == wxT("bmp") )
+ {
+ static const int bppvalues[] =
+ {
+ wxBMP_1BPP,
+ wxBMP_1BPP_BW,
+ wxBMP_4BPP,
+ wxBMP_8BPP,
+ wxBMP_8BPP_GREY,
+ wxBMP_8BPP_RED,
+ wxBMP_8BPP_PALETTE,
+ wxBMP_24BPP
+ };
+
+ const wxString bppchoices[] =
+ {
+ 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(wxT("Set BMP BPP"),
+ wxT("Image sample: save file"),
+ WXSIZEOF(bppchoices),
+ bppchoices,
+ this);
+ if ( bppselection != -1 )
+ {
+ int format = bppvalues[bppselection];
+ image.SetOption(wxIMAGE_OPTION_BMP_FORMAT, format);
+
+ if ( format == wxBMP_8BPP_PALETTE )
+ {
+ unsigned char *cmap = new unsigned char [256];
+ for ( int i = 0; i < 256; i++ )
+ cmap[i] = (unsigned char)i;
+ image.SetPalette(wxPalette(256, cmap, cmap, cmap));
+
+ delete[] cmap;
+ }
+ }
+ }
+#if wxUSE_LIBPNG
+ else if ( extension == wxT("png") )
+ {
+ static const int pngvalues[] =
+ {
+ wxPNG_TYPE_COLOUR,
+ wxPNG_TYPE_COLOUR,
+ wxPNG_TYPE_GREY,
+ wxPNG_TYPE_GREY,
+ wxPNG_TYPE_GREY_RED,
+ wxPNG_TYPE_GREY_RED,
+ };
+
+ const wxString pngchoices[] =
+ {
+ wxT("Colour 8bpp"),
+ wxT("Colour 16bpp"),
+ wxT("Grey 8bpp"),
+ wxT("Grey 16bpp"),
+ wxT("Grey red 8bpp"),
+ wxT("Grey red 16bpp"),
+ };
+
+ int sel = wxGetSingleChoiceIndex(wxT("Set PNG format"),
+ wxT("Image sample: save file"),
+ WXSIZEOF(pngchoices),
+ pngchoices,
+ this);
+ if ( sel != -1 )
+ {
+ 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
+ }
+ }
+ }
+#endif // wxUSE_LIBPNG
+ else if ( extension == wxT("cur") )
+ {
+ image.Rescale(32,32);
+ image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, 0);
+ image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, 0);
+ // This shows how you can save an image with explicitly
+ // specified image format:
+ saved = image.SaveFile(savefilename, wxBITMAP_TYPE_CUR);
+ }
+
+ if ( !saved )
+ {
+ // This one guesses image format from filename extension
+ // (it may fail if the extension is not recognized):
+ image.SaveFile(savefilename);
+ }
+#endif // wxUSE_FILEDLG
+ }
+
+ void OnResize(wxCommandEvent& WXUNUSED(event))
+ {
+ wxImage img(m_bitmap.ConvertToImage());
+
+ const wxSize size = GetClientSize();
+ img.Rescale(size.x, size.y, wxIMAGE_QUALITY_HIGH);
+ m_bitmap = wxBitmap(img);
+
+ UpdateStatusBar();
+ }
+
+ 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)
+ {
+ double angle = 5;
+ if ( event.GetId() == ID_ROTATE_LEFT )
+ angle = -angle;
+
+ wxImage img(m_bitmap.ConvertToImage());
+ img = img.Rotate(angle, wxPoint(img.GetWidth() / 2, img.GetHeight() / 2));
+ if ( !img.Ok() )
+ {
+ wxLogWarning(wxT("Rotation failed"));
+ return;
+ }
+
+ m_bitmap = wxBitmap(img);
+
+ UpdateStatusBar();
+ }
+
+ void UpdateStatusBar()
+ {
+ wxLogStatus(this, wxT("Image size: (%d, %d), zoom %.2f"),
+ m_bitmap.GetWidth(),
+ m_bitmap.GetHeight(),
+ m_zoom);
+ Refresh();
+ }
+
+ wxBitmap m_bitmap;
+ double m_zoom;
+
+ DECLARE_EVENT_TABLE()