+class MyImageFrame : public wxFrame
+{
+public:
+ MyImageFrame(wxFrame *parent, const wxBitmap& bitmap)
+ : wxFrame(parent, wxID_ANY, _T("Double click to save"),
+ wxDefaultPosition, wxDefaultSize,
+ wxCAPTION | wxSYSTEM_MENU | wxCLOSE_BOX),
+ m_bitmap(bitmap)
+ {
+ SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
+ }
+
+ 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 );
+ dc.DrawBitmap( m_bitmap, 0, 0, true /* use mask */ );
+ }
+
+ void OnSave(wxMouseEvent& 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|")
+ wxT("PNG files (*.png)|*.png|")
+ wxT("JPEG files (*.jpg)|*.jpg|")
+ wxT("GIF files (*.gif)|*.gif|")
+ wxT("TIFF files (*.tif)|*.tif|")
+ wxT("PCX files (*.pcx)|*.pcx|")
+ 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 == _T("bpp") )
+ {
+ 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[] =
+ {
+ _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")
+ };
+
+ int bppselection = wxGetSingleChoiceIndex(_T("Set BMP BPP"),
+ _T("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;
+ }
+ }
+ }
+ else if ( extension == _T("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[] =
+ {
+ _T("Colour 8bpp"),
+ _T("Colour 16bpp"),
+ _T("Grey 8bpp"),
+ _T("Grey 16bpp"),
+ _T("Grey red 8bpp"),
+ _T("Grey red 16bpp"),
+ };
+
+ int sel = wxGetSingleChoiceIndex(_T("Set PNG format"),
+ _T("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);
+ }
+ }
+ else if ( extension == _T("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
+ }
+
+private:
+ wxBitmap m_bitmap;
+
+ DECLARE_EVENT_TABLE()
+};
+
+#ifdef wxHAVE_RAW_BITMAP
+
+#include "wx/rawbmp.h"
+
+class MyRawBitmapFrame : public wxFrame
+{
+public:
+ enum
+ {
+ BORDER = 15,
+ SIZE = 150,
+ REAL_SIZE = SIZE - 2*BORDER
+ };
+
+ MyRawBitmapFrame(wxFrame *parent)
+ : wxFrame(parent, wxID_ANY, _T("Raw bitmaps (how exciting)")),
+ m_bitmap(SIZE, SIZE, 32)
+ {
+ SetClientSize(SIZE, SIZE);
+
+ // another possibility: wxNativePixelData (don't forget to remove code
+ // setting alpha in the loop below then)
+ typedef wxAlphaPixelData Data;
+ // typedef wxNativePixelData Data;
+
+ // First, clear the whole bitmap by making it alpha
+ {
+ Data data( m_bitmap, wxPoint(0,0), wxSize(SIZE, SIZE) );
+ if ( !data )
+ {
+ wxLogError(_T("Failed to gain raw access to bitmap data"));
+ return;
+ }
+ data.UseAlpha();
+ Data::Iterator p(data);
+ for ( int y = 0; y < SIZE; ++y )
+ {
+ Data::Iterator rowStart = p;
+ for ( int x = 0; x < SIZE; ++x )
+ {
+ p.Alpha() = 0;
+ ++p; // same as p.OffsetX(1)
+ }
+ p = rowStart;
+ p.OffsetY(data, 1);
+ }
+ }
+
+ // Then, draw colourful alpha-blended stripes
+ Data data(m_bitmap, wxPoint(BORDER, BORDER) , wxSize(REAL_SIZE, REAL_SIZE));
+ if ( !data )
+ {
+ wxLogError(_T("Failed to gain raw access to bitmap data"));
+ return;
+ }
+
+ data.UseAlpha();
+
+ Data::Iterator p(data);
+
+ for ( int y = 0; y < REAL_SIZE; ++y )
+ {
+ Data::Iterator rowStart = p;
+
+ int r = y < REAL_SIZE/3 ? 255 : 0,
+ g = (REAL_SIZE/3 <= y) && (y < 2*(REAL_SIZE/3)) ? 255 : 0,
+ b = 2*(REAL_SIZE/3) <= y ? 255 : 0;
+
+ for ( int x = 0; x < REAL_SIZE; ++x )
+ {
+ // note that RGB must be premultiplied by alpha
+ unsigned a = (Data::Iterator::ChannelType)((x*255.)/REAL_SIZE);
+ p.Red() = r * a / 256;
+ p.Green() = g * a / 256;
+ p.Blue() = b * a / 256;
+ p.Alpha() = a;
+
+ ++p; // same as p.OffsetX(1)
+ }
+
+ p = rowStart;
+ p.OffsetY(data, 1);
+ }
+ }
+
+ 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.DrawBitmap( m_bitmap, 0, 0, true /* use mask */ );
+ }
+
+private:
+ wxBitmap m_bitmap;
+
+ DECLARE_EVENT_TABLE()
+};
+
+#endif // wxHAVE_RAW_BITMAP
+