X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/dabeb02181178d15b6914992a34eb223a98aaa0a..ec45f8ee19190fce57180381611075d3a5b63435:/src/msw/bitmap.cpp?ds=sidebyside diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index f0688b5454..44099f7598 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -36,7 +36,7 @@ #include "assert.h" -#if USE_XPM_IN_MSW +#if wxUSE_XPM_IN_MSW #define FOR_MSW 1 #include "../../contrib/wxxpm/libxpm.34b/lib/xpm34.h" #endif @@ -84,8 +84,6 @@ wxList wxBitmap::sm_handlers; wxBitmap::wxBitmap(void) { - m_refData = NULL; // new wxBitmapRefData; - if ( wxTheBitmapList ) wxTheBitmapList->AddBitmap(this); } @@ -124,7 +122,7 @@ bool wxBitmap::FreeResource(bool force) } -wxBitmap::wxBitmap(const char bits[], const int the_width, const int the_height, const int no_bits) +wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits) { m_refData = new wxBitmapRefData; @@ -133,7 +131,7 @@ wxBitmap::wxBitmap(const char bits[], const int the_width, const int the_height, M_BITMAPDATA->m_depth = no_bits ; M_BITMAPDATA->m_numColors = 0; - M_BITMAPDATA->m_hBitmap = (WXHBITMAP) CreateBitmap(the_width, the_height, no_bits, 1, bits); + M_BITMAPDATA->m_hBitmap = (WXHBITMAP) CreateBitmap(the_width, the_height, 1, no_bits, bits); if (M_BITMAPDATA->m_hBitmap) M_BITMAPDATA->m_ok = TRUE; @@ -146,7 +144,7 @@ wxBitmap::wxBitmap(const char bits[], const int the_width, const int the_height, wxTheBitmapList->AddBitmap(this); } -wxBitmap::wxBitmap(const int w, const int h, const int d) +wxBitmap::wxBitmap(int w, int h, int d) { (void)Create(w, h, d); @@ -154,7 +152,7 @@ wxBitmap::wxBitmap(const int w, const int h, const int d) wxTheBitmapList->AddBitmap(this); } -wxBitmap::wxBitmap(void *data, const long type, const int width, const int height, const int depth) +wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth) { (void) Create(data, type, width, height, depth); @@ -162,7 +160,7 @@ wxBitmap::wxBitmap(void *data, const long type, const int width, const int heigh wxTheBitmapList->AddBitmap(this); } -wxBitmap::wxBitmap(const wxString& filename, const long type) +wxBitmap::wxBitmap(const wxString& filename, long type) { LoadFile(filename, (int)type); @@ -170,7 +168,7 @@ wxBitmap::wxBitmap(const wxString& filename, const long type) wxTheBitmapList->AddBitmap(this); } -#if USE_XPM_IN_MSW +#if wxUSE_XPM_IN_MSW // Create from data wxBitmap::wxBitmap(const char **data, wxItem *WXUNUSED(anItem)) { @@ -178,7 +176,7 @@ wxBitmap::wxBitmap(const char **data, wxItem *WXUNUSED(anItem)) } #endif -bool wxBitmap::Create(const int w, const int h, const int d) +bool wxBitmap::Create(int w, int h, int d) { UnRef(); @@ -190,7 +188,7 @@ bool wxBitmap::Create(const int w, const int h, const int d) if (d > 0) { - M_BITMAPDATA->m_hBitmap = (WXHBITMAP) CreateBitmap(w, h, d, 1, NULL); + M_BITMAPDATA->m_hBitmap = (WXHBITMAP) CreateBitmap(w, h, 1, d, NULL); } else { @@ -206,7 +204,7 @@ bool wxBitmap::Create(const int w, const int h, const int d) return M_BITMAPDATA->m_ok; } -bool wxBitmap::LoadFile(const wxString& filename, const long type) +bool wxBitmap::LoadFile(const wxString& filename, long type) { UnRef(); @@ -223,7 +221,7 @@ bool wxBitmap::LoadFile(const wxString& filename, const long type) return handler->LoadFile(this, filename, type, -1, -1); } -bool wxBitmap::Create(void *data, const long type, const int width, const int height, const int depth) +bool wxBitmap::Create(void *data, long type, int width, int height, int depth) { UnRef(); @@ -240,7 +238,7 @@ bool wxBitmap::Create(void *data, const long type, const int width, const int he return handler->Create(this, data, type, width, height, depth); } -bool wxBitmap::SaveFile(const wxString& filename, const int type, const wxPalette *palette) +bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *palette) { wxBitmapHandler *handler = FindHandler(type); @@ -256,7 +254,7 @@ bool wxBitmap::SaveFile(const wxString& filename, const int type, const wxPalett void wxBitmap::SetWidth(int w) { if (!M_BITMAPDATA) - m_refData = new wxBitmapRefData; + m_refData = new wxBitmapRefData; M_BITMAPDATA->m_width = w; } @@ -379,6 +377,47 @@ wxBitmapHandler *wxBitmap::FindHandler(long bitmapType) return NULL; } +// New Create/FreeDIB functions since ones in dibutils.cpp are confusing +static long createDIB(long xSize, long ySize, long bitsPerPixel, + HPALETTE hPal, LPBITMAPINFO* lpDIBHeader); +static long freeDIB(LPBITMAPINFO lpDIBHeader); + +// Creates a bitmap that matches the device context, from +// an arbitray bitmap. At present, the original bitmap must have an +// associated palette. TODO: use a default palette if no palette exists. +// Contributed by Frederic Villeneuve +wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const +{ + wxMemoryDC memDC; + wxBitmap tmpBitmap(this->GetWidth(), this->GetHeight(), dc.GetDepth()); + HPALETTE hPal = NULL; + LPBITMAPINFO lpDib; + void *lpBits = NULL; + + wxASSERT( this->GetPalette() && this->GetPalette()->Ok() && (this->GetPalette()->GetHPALETTE() != 0) ); + + tmpBitmap.SetPalette(this->GetPalette()); + memDC.SelectObject(tmpBitmap); + memDC.SetPalette(this->GetPalette()); + + hPal = (HPALETTE) this->GetPalette()->GetHPALETTE(); + + // set the height negative because in a DIB the order of the lines is reversed + createDIB(this->GetWidth(), -this->GetHeight(), this->GetDepth(), hPal, &lpDib); + + lpBits = malloc(lpDib->bmiHeader.biSizeImage); + + ::GetBitmapBits((HBITMAP)GetHBITMAP(), lpDib->bmiHeader.biSizeImage, lpBits); + + ::SetDIBitsToDevice((HDC) memDC.GetHDC(), 0, 0, this->GetWidth(), this->GetHeight(), + 0, 0, 0, this->GetHeight(), lpBits, lpDib, DIB_RGB_COLORS); + + free(lpBits); + + freeDIB(lpDib); + return (tmpBitmap); +} + /* * wxMask */ @@ -398,7 +437,7 @@ wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour) // Construct a mask from a bitmap and a palette index indicating // the transparent area -wxMask::wxMask(const wxBitmap& bitmap, const int paletteIndex) +wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex) { m_maskBitmap = 0; Create(bitmap, paletteIndex); @@ -448,7 +487,7 @@ bool wxMask::Create(const wxBitmap& bitmap) // Create a mask from a bitmap and a palette index indicating // the transparent area -bool wxMask::Create(const wxBitmap& bitmap, const int paletteIndex) +bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) { if ( m_maskBitmap ) { @@ -525,18 +564,18 @@ bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject) -bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, const long type, const int width, const int height, const int depth) +bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth) { return FALSE; } -bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, const long type, +bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long type, int desiredWidth, int desiredHeight) { return FALSE; } -bool wxBitmapHandler::SaveFile(wxBitmap *bitmap, const wxString& name, const int type, const wxPalette *palette) +bool wxBitmapHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette) { return FALSE; } @@ -556,12 +595,12 @@ public: m_type = wxBITMAP_TYPE_BMP_RESOURCE; }; - virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, const long flags, + virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, int desiredWidth, int desiredHeight); }; IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler) -bool wxBMPResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, const long flags, +bool wxBMPResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, int desiredWidth, int desiredHeight) { // TODO: load colourmap. @@ -573,7 +612,7 @@ bool wxBMPResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, cons GetObject((HBITMAP) M_BITMAPHANDLERDATA->m_hBitmap, sizeof(BITMAP), (LPSTR) &bm); M_BITMAPHANDLERDATA->m_width = bm.bmWidth; M_BITMAPHANDLERDATA->m_height = bm.bmHeight; - M_BITMAPHANDLERDATA->m_depth = bm.bmPlanes; + M_BITMAPHANDLERDATA->m_depth = bm.bmBitsPixel; return TRUE; } @@ -594,16 +633,16 @@ public: m_type = wxBITMAP_TYPE_BMP; }; - virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, const long flags, + virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, int desiredWidth, int desiredHeight); - virtual bool SaveFile(wxBitmap *bitmap, const wxString& name, const int type, const wxPalette *palette = NULL); + virtual bool SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette = NULL); }; IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler, wxBitmapHandler) -bool wxBMPFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, const long flags, +bool wxBMPFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, int desiredWidth, int desiredHeight) { -#if USE_IMAGE_LOADING_IN_MSW +#if wxUSE_IMAGE_LOADING_IN_MSW wxPalette *palette = NULL; bool success = FALSE; /* @@ -625,9 +664,9 @@ bool wxBMPFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, const lo #endif } -bool wxBMPFileHandler::SaveFile(wxBitmap *bitmap, const wxString& name, const int type, const wxPalette *pal) +bool wxBMPFileHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *pal) { -#if USE_IMAGE_LOADING_IN_MSW +#if wxUSE_IMAGE_LOADING_IN_MSW wxPalette *actualPalette = (wxPalette *)pal; if (!actualPalette && (!M_BITMAPHANDLERDATA->m_bitmapPalette.IsNull())) actualPalette = & (M_BITMAPHANDLERDATA->m_bitmapPalette); @@ -648,16 +687,16 @@ public: m_type = wxBITMAP_TYPE_XPM; }; - virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, const long flags, + virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, int desiredWidth = -1, int desiredHeight = -1); - virtual bool SaveFile(wxBitmap *bitmap, const wxString& name, const int type, const wxPalette *palette = NULL); + virtual bool SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette = NULL); }; IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler) -bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, const long flags, +bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, int desiredWidth, int desiredHeight) { -#if USE_XPM_IN_MSW +#if wxUSE_XPM_IN_MSW XImage *ximage; XpmAttributes xpmAttr; HDC dc; @@ -697,9 +736,9 @@ bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, const lo return FALSE; } -bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap, const wxString& name, const int type, const wxPalette *palette) +bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette) { -#if USE_XPM_IN_MSW +#if wxUSE_XPM_IN_MSW HDC dc = NULL; Visual *visual = NULL; @@ -743,13 +782,13 @@ public: m_type = wxBITMAP_TYPE_XPM_DATA; }; - virtual bool Create(wxBitmap *bitmap, void *data, const long flags, const int width, const int height, const int depth = 1); + virtual bool Create(wxBitmap *bitmap, void *data, long flags, int width, int height, int depth = 1); }; IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler) -bool wxXPMDataHandler::Create(wxBitmap *bitmap, void *data, const long flags, const int width, const int height, const int depth) +bool wxXPMDataHandler::Create(wxBitmap *bitmap, void *data, long flags, int width, int height, int depth) { -#if USE_XPM_IN_MSW +#if wxUSE_XPM_IN_MSW XImage *ximage; int ErrorStatus; XpmAttributes xpmAttr; @@ -821,4 +860,66 @@ void wxBitmap::InitStandardHandlers(void) AddHandler(new wxXPMDataHandler); AddHandler(new wxICOResourceHandler); AddHandler(new wxICOFileHandler); -} \ No newline at end of file +} + +static long createDIB(long xSize, long ySize, long bitsPerPixel, + HPALETTE hPal, LPBITMAPINFO* lpDIBHeader) +{ + unsigned long i, headerSize; + LPBITMAPINFO lpDIBheader = NULL; + LPPALETTEENTRY lpPe = NULL; + + + // Allocate space for a DIB header + headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY))); + lpDIBheader = (BITMAPINFO *) malloc(headerSize); + lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER)); + + GetPaletteEntries(hPal, 0, 256, lpPe); + + + memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER)); + + + // Fill in the static parts of the DIB header + lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + lpDIBheader->bmiHeader.biWidth = xSize; + lpDIBheader->bmiHeader.biHeight = ySize; + lpDIBheader->bmiHeader.biPlanes = 1; + + // this value must be 1, 4, 8 or 24 so PixelDepth can only be + lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel); + lpDIBheader->bmiHeader.biCompression = BI_RGB; + lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> +3; + lpDIBheader->bmiHeader.biClrUsed = 256; + + + // Initialize the DIB palette + for (i = 0; i < 256; i++) { + lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags; + lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed; + lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen; + lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue; + } + + *lpDIBHeader = lpDIBheader; + + + return (0); + +} + + + +static long freeDIB(LPBITMAPINFO lpDIBHeader) +{ + + if (lpDIBHeader != NULL) { + free(lpDIBHeader); + } + + return (0); +} + +