X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/debe6624c1e9d4bf3243381153d1e173c849bcd8..2d120f8391920145647ec10e84629bc21fa9f1bb:/src/msw/bitmap.cpp?ds=sidebyside diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index aa0f6ed699..e935db2c76 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -27,6 +27,7 @@ #include "wx/utils.h" #include "wx/app.h" #include "wx/palette.h" +#include "wx/dcmemory.h" #include "wx/bitmap.h" #include "wx/icon.h" #endif @@ -36,9 +37,9 @@ #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" +#include "../src/xpm/xpm34.h" #endif #include "wx/msw/dib.h" @@ -78,14 +79,13 @@ wxBitmapRefData::~wxBitmapRefData(void) if (m_bitmapMask) delete m_bitmapMask; m_bitmapMask = NULL; + } wxList wxBitmap::sm_handlers; wxBitmap::wxBitmap(void) { - m_refData = NULL; // new wxBitmapRefData; - if ( wxTheBitmapList ) wxTheBitmapList->AddBitmap(this); } @@ -133,7 +133,7 @@ wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits 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; @@ -170,9 +170,9 @@ wxBitmap::wxBitmap(const wxString& filename, 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)) +wxBitmap::wxBitmap(char **data, wxItem *WXUNUSED(anItem)) { (void) Create((void *)data, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0); } @@ -190,7 +190,7 @@ bool wxBitmap::Create(int w, int h, 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 { @@ -256,7 +256,7 @@ bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *pal void wxBitmap::SetWidth(int w) { if (!M_BITMAPDATA) - m_refData = new wxBitmapRefData; + m_refData = new wxBitmapRefData; M_BITMAPDATA->m_width = w; } @@ -379,6 +379,65 @@ 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(); +*/ + if( 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(); + } + else + { + hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE); + wxPalette palette; + palette.SetHPALETTE( (WXHPALETTE)hPal ); + tmpBitmap.SetPalette( palette ); + memDC.SelectObject(tmpBitmap); + memDC.SetPalette( palette ); + } + + // 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 */ @@ -573,7 +632,7 @@ bool wxBMPResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long 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; } @@ -603,7 +662,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler, wxBitmapHandler) 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; /* @@ -627,7 +686,7 @@ bool wxBMPFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long fla 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); @@ -657,7 +716,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler) 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; @@ -699,36 +758,36 @@ bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long fla bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette) { -#if USE_XPM_IN_MSW - HDC dc = NULL; - - Visual *visual = NULL; - XImage ximage; - - dc = CreateCompatibleDC(NULL); - if (dc) - { +#if wxUSE_XPM_IN_MSW + HDC dc = NULL; + + XImage ximage; + + dc = CreateCompatibleDC(NULL); + if (dc) + { if (SelectObject(dc, (HBITMAP) M_BITMAPHANDLERDATA->m_hBitmap)) - { /* for following SetPixel */ - /* fill the XImage struct 'by hand' */ - ximage.width = M_BITMAPHANDLERDATA->m_width; - ximage.height = M_BITMAPHANDLERDATA->m_height; - ximage.depth = M_BITMAPHANDLERDATA->m_depth; - ximage.bitmap = (void *)M_BITMAPHANDLERDATA->m_hBitmap; - int errorStatus = XpmWriteFileFromImage(&dc, WXSTRINGCAST name, - &ximage, (XImage *) NULL, (XpmAttributes *) NULL); - - if (dc) - DeleteDC(dc); - - if (errorStatus == XpmSuccess) - return TRUE; /* no error */ - else - return FALSE; + { + /* for following SetPixel */ + /* fill the XImage struct 'by hand' */ + ximage.width = M_BITMAPHANDLERDATA->m_width; + ximage.height = M_BITMAPHANDLERDATA->m_height; + ximage.depth = M_BITMAPHANDLERDATA->m_depth; + ximage.bitmap = (HBITMAP)M_BITMAPHANDLERDATA->m_hBitmap; + int errorStatus = XpmWriteFileFromImage(&dc, WXSTRINGCAST name, + &ximage, (XImage *) NULL, (XpmAttributes *) NULL); + + if (dc) + DeleteDC(dc); + + if (errorStatus == XpmSuccess) + return TRUE; /* no error */ + else + return FALSE; } else return FALSE; - } else return FALSE; + } else return FALSE; #else - return FALSE; + return FALSE; #endif } @@ -749,7 +808,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler) 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 +880,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); +} + +