X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c4e7c2aa5822f9a6e97d8d7848b95ea2eadcd98b..3b9e3455225b670d30ee0fb67f8821ada9640f6d:/src/msw/dib.cpp diff --git a/src/msw/dib.cpp b/src/msw/dib.cpp index d86337576a..2bc530afeb 100644 --- a/src/msw/dib.cpp +++ b/src/msw/dib.cpp @@ -6,12 +6,12 @@ * * * FUNCTIONS : * * * - * ReadDIB() - Reads a DIB * + * wxReadDIB() - Reads a DIB * * * * WriteDIB() - Writes a global handle in CF_DIB format* * to a file. * * * - * PaletteSize() - Calculates the palette size in bytes * + * wxPaletteSize() - Calculates the palette size in bytes * * of given DIB * * * * DibNumColors() - Determines the number of colors in DIB * @@ -26,7 +26,6 @@ *******************************************************************************/ // For compilers that support precompilation, includes "wx.h". -#define IN_WX_MAIN_CPP #include "wx/wxprec.h" #if defined(__BORLANDC__) @@ -36,18 +35,26 @@ #ifndef WX_PRECOMP #include "wx/setup.h" #include "wx/defs.h" +#include "wx/bitmap.h" #endif #include #include #include + +#if !defined(__MWERKS__) && !defined(__SALFORDC__) #include +#endif #include "wx/msw/dib.h" +#ifndef __TWIN32__ +#ifndef wxUSE_NORLANDER_HEADERS #ifdef __GNUWIN32__ #include "wx/msw/gnuwin32/extra.h" #endif +#endif +#endif #ifndef SEEK_CUR /* flags for _lseek */ @@ -62,7 +69,7 @@ /* Header signatutes for various resources */ #define BFT_ICON 0x4349 /* 'IC' */ #define BFT_BITMAP 0x4d42 /* 'BM' */ -#define BFT_CURSOR 0x5450 /* 'PT' */ +#define BFT_CURSOR 0x5450 /* 'PT(' */ /* macro to determine if resource is a DIB */ #define ISDIB(bft) ((bft) == BFT_BITMAP) @@ -76,16 +83,14 @@ #define PALVERSION 0x300 #define MAXPALETTE 256 /* max. # supported palette entries */ -DWORD PASCAL lread(int fh, VOID FAR *pv, DWORD ul); -DWORD PASCAL lwrite(int fh, VOID FAR *pv, DWORD ul); +static DWORD PASCAL lread(int fh, VOID FAR *pv, DWORD ul); +static DWORD PASCAL lwrite(int fh, VOID FAR *pv, DWORD ul); -BOOL WriteDIB (LPSTR szFile,HANDLE hdib); -WORD PaletteSize (VOID FAR * pv); -WORD DibNumColors (VOID FAR * pv); -// HANDLE DibFromBitmap (HBITMAP hbm, DWORD biStyle, WORD biBits, HPALETTE hpal); -BOOL PASCAL MakeBitmapAndPalette(HDC,HANDLE,HPALETTE *,HBITMAP *); -HPALETTE MakeDIBPalette(LPBITMAPINFOHEADER); -BOOL ReadDIB(LPSTR lpFileName, HBITMAP *bitmap, HPALETTE *palette); +static BOOL WriteDIB (LPTSTR szFile,HANDLE hdib); +WORD wxPaletteSize (VOID FAR * pv); // This is non-static as some apps use it externally +static WORD DibNumColors (VOID FAR * pv); +// HANDLE DibFromBitmap (HBITMAP hbm, DWORD biStyle, WORD biBits, HPALETTE hpal); +static BOOL PASCAL MakeBitmapAndPalette(HDC,HANDLE,HPALETTE *,HBITMAP *); /**************************************************************************** * * @@ -98,7 +103,7 @@ BOOL ReadDIB(LPSTR lpFileName, HBITMAP *bitmap, HPALETTE *palette); * * ****************************************************************************/ -BOOL WriteDIB(LPSTR szFile, HANDLE hdib) +static BOOL WriteDIB(LPTSTR szFile, HANDLE hdib) { BITMAPFILEHEADER hdr; LPBITMAPINFOHEADER lpbi; @@ -108,7 +113,7 @@ BOOL WriteDIB(LPSTR szFile, HANDLE hdib) if (!hdib) return FALSE; - fh = OpenFile(szFile, &of, OF_CREATE | OF_READWRITE); + fh = OpenFile(wxFNCONV(szFile), &of, OF_CREATE | OF_READWRITE); if (fh == -1) return FALSE; @@ -122,8 +127,8 @@ BOOL WriteDIB(LPSTR szFile, HANDLE hdib) hdr.bfSize = GlobalSize(hdib) + sizeof(BITMAPFILEHEADER); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; - hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + lpbi->biSize + - PaletteSize(lpbi); + hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + lpbi->biSize + + wxPaletteSize(lpbi); /* Write the file header */ _lwrite(fh, (LPSTR) &hdr, sizeof(BITMAPFILEHEADER)); @@ -138,7 +143,7 @@ BOOL WriteDIB(LPSTR szFile, HANDLE hdib) /**************************************************************************** * * - * FUNCTION : PaletteSize(VOID FAR * pv) * + * FUNCTION : wxPaletteSize(VOID FAR * pv) * * * * PURPOSE : Calculates the palette size in bytes. If the info. block * * is of the BITMAPCOREHEADER type, the number of colors is * @@ -149,7 +154,7 @@ BOOL WriteDIB(LPSTR szFile, HANDLE hdib) * * ****************************************************************************/ -WORD PaletteSize(VOID FAR * pv) +WORD wxPaletteSize(VOID FAR * pv) { LPBITMAPINFOHEADER lpbi; WORD NumColors; @@ -159,7 +164,7 @@ WORD PaletteSize(VOID FAR * pv) if (lpbi->biSize == sizeof(BITMAPCOREHEADER)) return NumColors * sizeof(RGBTRIPLE); - else + else return NumColors * sizeof(RGBQUAD); } @@ -174,7 +179,7 @@ WORD PaletteSize(VOID FAR * pv) * * ****************************************************************************/ -WORD DibNumColors(VOID FAR *pv) +static WORD DibNumColors(VOID FAR *pv) { int bits; BITMAPINFOHEADER *lpbi; @@ -193,7 +198,7 @@ WORD DibNumColors(VOID FAR *pv) return (WORD) lpbi->biClrUsed; bits = lpbi->biBitCount; } - else + else bits = lpbc->bcBitCount; switch (bits) { @@ -221,7 +226,7 @@ WORD DibNumColors(VOID FAR *pv) ****************************************************************************/ #if NOTHING -HANDLE DibFromBitmap(HBITMAP hbm, DWORD biStyle, WORD biBits, HPALETTE hpal) +static HANDLE DibFromBitmap(HBITMAP hbm, DWORD biStyle, WORD biBits, HPALETTE hpal) { BITMAP bm; BITMAPINFOHEADER bi; @@ -254,9 +259,9 @@ HANDLE DibFromBitmap(HBITMAP hbm, DWORD biStyle, WORD biBits, HPALETTE hpal) bi.biClrUsed = 0; bi.biClrImportant = 0; - dwLen = bi.biSize + PaletteSize(&bi); + dwLen = bi.biSize + wxPaletteSize(&bi); - hdc = GetDC(NULL); + hdc = GetDC((HWND) NULL); hpal = SelectPalette(hdc, hpal, FALSE); RealizePalette(hdc); @@ -294,7 +299,7 @@ HANDLE DibFromBitmap(HBITMAP hbm, DWORD biStyle, WORD biBits, HPALETTE hpal) } /* realloc the buffer big enough to hold all the bits */ - dwLen = bi.biSize + PaletteSize(&bi) + bi.biSizeImage; + dwLen = bi.biSize + wxPaletteSize(&bi) + bi.biSizeImage; if (h = GlobalReAlloc(hdib, dwLen, 0)) hdib = h; else { @@ -319,12 +324,12 @@ HANDLE DibFromBitmap(HBITMAP hbm, DWORD biStyle, WORD biBits, HPALETTE hpal) hbm, 0, (WORD) bi.biHeight, - (LPSTR) lpbi + (WORD) lpbi->biSize + PaletteSize(lpbi), + (LPSTR) lpbi + (WORD) lpbi->biSize + wxPaletteSize(lpbi), (LPBITMAPINFO) lpbi, DIB_RGB_COLORS) == 0) { GlobalUnlock(hdib); hdib = NULL; SelectPalette(hdc, hpal, FALSE); - ReleaseDC(NULL, hdc); + ReleaseDC((HWND) NULL, hdc); return NULL; } @@ -349,10 +354,10 @@ HANDLE DibFromBitmap(HBITMAP hbm, DWORD biStyle, WORD biBits, HPALETTE hpal) * * ****************************************************************************/ -DWORD PASCAL lread(int fh, void far *pv, DWORD ul) +static DWORD PASCAL lread(int fh, void far *pv, DWORD ul) { DWORD ulT = ul; -#if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__) +#if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__) || defined(__WXWINE__) BYTE *hp = (BYTE *) pv; #else BYTE huge *hp = (BYTE huge *) pv; @@ -379,10 +384,10 @@ DWORD PASCAL lread(int fh, void far *pv, DWORD ul) * * ****************************************************************************/ -DWORD PASCAL lwrite(int fh, VOID FAR *pv, DWORD ul) +static DWORD PASCAL lwrite(int fh, VOID FAR *pv, DWORD ul) { DWORD ulT = ul; -#if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__) +#if defined(WINNT) || defined(__WIN32__) || defined(__WIN32__) || defined(__WXWINE__) BYTE *hp = (BYTE *) pv; #else BYTE huge *hp = (BYTE huge *) pv; @@ -407,13 +412,13 @@ DWORD PASCAL lwrite(int fh, VOID FAR *pv, DWORD ul) * is loaded, the function also creates a bitmap and * palette out of the DIB for a device-dependent form. * - * RETURNS : TRUE - DIB loaded and bitmap/palette created + * RETURNS : TRUE - DIB loaded and bitmap/palette created * The DIBINIT structure pointed to by pInfo is * filled with the appropriate handles. * FALSE - otherwise * ****************************************************************************/ -BOOL ReadDIB(LPSTR lpFileName, HBITMAP *bitmap, HPALETTE *palette) +BOOL wxReadDIB(LPTSTR lpFileName, HBITMAP *bitmap, HPALETTE *palette) { int fh; LPBITMAPINFOHEADER lpbi; @@ -421,7 +426,7 @@ BOOL ReadDIB(LPSTR lpFileName, HBITMAP *bitmap, HPALETTE *palette) BITMAPFILEHEADER bf; WORD nNumColors; BOOL result = FALSE; - char str[128]; + wxChar str[128]; WORD offBits; HDC hDC; BOOL bCoreHead = FALSE; @@ -429,14 +434,14 @@ BOOL ReadDIB(LPSTR lpFileName, HBITMAP *bitmap, HPALETTE *palette) /* Open the file and get a handle to it's BITMAPINFO */ - fh = OpenFile (lpFileName, &of, OF_READ); + fh = OpenFile (wxFNCONV(lpFileName), &of, OF_READ); if (fh == -1) { - wsprintf(str,"Can't open file '%ls'", (LPSTR)lpFileName); - MessageBox(NULL, str, "Error", MB_ICONSTOP | MB_OK); + wsprintf(str,wxT("Can't open file '%s'"), lpFileName); + MessageBox(NULL, str, wxT("Error"), MB_ICONSTOP | MB_OK); return (0); } - - hDIB = GlobalAlloc(GHND, (DWORD)(sizeof(BITMAPINFOHEADER) + + + hDIB = GlobalAlloc(GHND, (DWORD)(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD))); if (!hDIB) return(0); @@ -474,7 +479,8 @@ BOOL ReadDIB(LPSTR lpFileName, HBITMAP *bitmap, HPALETTE *palette) goto ErrExit; } - if (!(nNumColors = (WORD)lpbi->biClrUsed)) + nNumColors = (WORD)lpbi->biClrUsed; + if ( nNumColors == 0 ) { /* no color table for 24-bit, default size otherwise */ if (lpbi->biBitCount != 24) @@ -491,7 +497,7 @@ BOOL ReadDIB(LPSTR lpFileName, HBITMAP *bitmap, HPALETTE *palette) * lpbi->biHeight; } - /* get a proper-sized buffer for header, color table and bits */ + /* get a proper-sized buffer for header, color table and bits */ GlobalUnlock(hDIB); hDIB = GlobalReAlloc(hDIB, lpbi->biSize + nNumColors * sizeof(RGBQUAD) + @@ -573,14 +579,14 @@ ErrExit2: * to be used for a device-dependent representation of * of the image. * - * RETURNS : TRUE --> success. phPal and phBitmap are filled with + * RETURNS : TRUE --> success. phPal and phBitmap are filled with * appropriate handles. Caller is responsible * for freeing objects. * FALSE --> unable to create objects. both pointer are * not valid * ****************************************************************************/ -BOOL PASCAL MakeBitmapAndPalette(HDC hDC, HANDLE hDIB, +static BOOL PASCAL MakeBitmapAndPalette(HDC hDC, HANDLE hDIB, HPALETTE * phPal, HBITMAP * phBitmap) { LPBITMAPINFOHEADER lpInfo; @@ -595,15 +601,16 @@ BOOL PASCAL MakeBitmapAndPalette(HDC hDC, HANDLE hDIB, lpInfo = (LPBITMAPINFOHEADER) GlobalLock(hDIB); #endif - if ((hPalette = MakeDIBPalette(lpInfo))) + hPalette = wxMakeDIBPalette(lpInfo); + if ( hPalette ) { // Need to realize palette for converting DIB to bitmap. hOldPal = SelectPalette(hDC, hPalette, TRUE); RealizePalette(hDC); - lpBits = (LPSTR)lpInfo + (WORD)lpInfo->biSize + + lpBits = (LPSTR)lpInfo + (WORD)lpInfo->biSize + (WORD)lpInfo->biClrUsed * sizeof(RGBQUAD); - hBitmap = CreateDIBitmap(hDC, lpInfo, CBM_INIT, lpBits, + hBitmap = CreateDIBitmap(hDC, lpInfo, CBM_INIT, lpBits, (LPBITMAPINFO)lpInfo, DIB_RGB_COLORS); SelectPalette(hDC, hOldPal, TRUE); @@ -623,21 +630,24 @@ BOOL PASCAL MakeBitmapAndPalette(HDC hDC, HANDLE hDIB, /**************************************************************************** * * - * FUNCTION : MakeDIBPalette(lpInfo) * + * FUNCTION : wxMakeDIBPalette(lpInfo) * * * - * PURPOSE : Given a BITMAPINFOHEADER, create a palette based on + * PURPOSE : Given a BITMAPINFOHEADER, create a palette based on * the color table. - * + * * * - * RETURNS : non-zero - handle of a corresponding palette + * RETURNS : non-zero - handle of a corresponding palette * zero - unable to create palette * * ****************************************************************************/ -HPALETTE MakeDIBPalette(LPBITMAPINFOHEADER lpInfo) +HPALETTE wxMakeDIBPalette(LPBITMAPINFOHEADER lpInfo) { - NPLOGPALETTE npPal; +#ifdef __WXWINE__ + return (FALSE); +#else + LPLOGPALETTE npPal; RGBQUAD far *lpRGB; - HPALETTE hLogPal; + HPALETTE hLogPal; WORD i; /* since biClrUsed field was filled during the loading of the DIB, @@ -646,12 +656,11 @@ HPALETTE MakeDIBPalette(LPBITMAPINFOHEADER lpInfo) if (lpInfo->biClrUsed) { /* - npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) + + npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) + (WORD)lpInfo->biClrUsed * sizeof(PALETTEENTRY)); */ - npPal = (NPLOGPALETTE)malloc(sizeof(LOGPALETTE) + + npPal = (LPLOGPALETTE)malloc(sizeof(LOGPALETTE) + (WORD)lpInfo->biClrUsed * sizeof(PALETTEENTRY)); - if (!npPal) return(FALSE); @@ -673,7 +682,7 @@ HPALETTE MakeDIBPalette(LPBITMAPINFOHEADER lpInfo) hLogPal = CreatePalette((LPLOGPALETTE)npPal); // LocalFree((HANDLE)npPal); free(npPal); - + return(hLogPal); } @@ -683,14 +692,16 @@ HPALETTE MakeDIBPalette(LPBITMAPINFOHEADER lpInfo) */ else return((HPALETTE) GetStockObject(DEFAULT_PALETTE)); +#endif + } -bool wxLoadIntoBitmap(char *filename, wxBitmap *bitmap, wxColourMap **pal) +bool wxLoadIntoBitmap(wxChar *filename, wxBitmap *bitmap, wxPalette **pal) { HBITMAP hBitmap; HPALETTE hPalette; - bool success = (ReadDIB(filename, &hBitmap, &hPalette) != 0); + bool success = (wxReadDIB(filename, &hBitmap, &hPalette) != 0); if (!success) { @@ -702,7 +713,7 @@ bool wxLoadIntoBitmap(char *filename, wxBitmap *bitmap, wxColourMap **pal) { if (pal) { - *pal = new wxColourMap; + *pal = new wxPalette; (*pal)->SetHPALETTE((WXHPALETTE) hPalette); } else @@ -720,13 +731,15 @@ bool wxLoadIntoBitmap(char *filename, wxBitmap *bitmap, wxColourMap **pal) bitmap->SetWidth(bm.bmWidth); bitmap->SetHeight(bm.bmHeight); bitmap->SetDepth(bm.bmPlanes * bm.bmBitsPixel); +#if WXWIN_COMPATIBILITY_2 bitmap->SetOk(TRUE); +#endif // WXWIN_COMPATIBILITY_2 return TRUE; } else return FALSE; } -wxBitmap *wxLoadBitmap(char *filename, wxColourMap **pal) +wxBitmap *wxLoadBitmap(wxChar *filename, wxPalette **pal) { wxBitmap *bitmap = new wxBitmap; if (wxLoadIntoBitmap(filename, bitmap, pal)) @@ -766,10 +779,10 @@ wxBitmap *wxLoadBitmap(char *filename, wxColourMap **pal) // // History: Date Reason // 11/07/91 Created -// +// //--------------------------------------------------------------------- -void InitBitmapInfoHeader (LPBITMAPINFOHEADER lpBmInfoHdr, +static void InitBitmapInfoHeader (LPBITMAPINFOHEADER lpBmInfoHdr, DWORD dwWidth, DWORD dwHeight, int nBPP) @@ -802,9 +815,9 @@ void InitBitmapInfoHeader (LPBITMAPINFOHEADER lpBmInfoHdr, -LPSTR FindDIBBits (LPSTR lpbi) +LPSTR wxFindDIBBits (LPSTR lpbi) { - return (lpbi + *(LPDWORD)lpbi + PaletteSize (lpbi)); + return (lpbi + *(LPDWORD)lpbi + wxPaletteSize (lpbi)); } //--------------------------------------------------------------------- @@ -824,10 +837,10 @@ LPSTR FindDIBBits (LPSTR lpbi) // // History: Date Reason // 6/01/91 Created -// +// //--------------------------------------------------------------------- -HANDLE BitmapToDIB (HBITMAP hBitmap, HPALETTE hPal) +HANDLE wxBitmapToDIB (HBITMAP hBitmap, HPALETTE hPal) { BITMAP Bitmap; BITMAPINFOHEADER bmInfoHdr; @@ -847,17 +860,17 @@ HANDLE BitmapToDIB (HBITMAP hBitmap, HPALETTE hPal) if (!GetObject (hBitmap, sizeof (Bitmap), (LPSTR) &Bitmap)) return NULL; - InitBitmapInfoHeader (&bmInfoHdr, - Bitmap.bmWidth, - Bitmap.bmHeight, + InitBitmapInfoHeader (&bmInfoHdr, + Bitmap.bmWidth, + Bitmap.bmHeight, Bitmap.bmPlanes * Bitmap.bmBitsPixel); // Now allocate memory for the DIB. Then, set the BITMAPINFOHEADER // into this memory, and find out where the bitmap bits go. - hDIB = GlobalAlloc (GHND, sizeof (BITMAPINFOHEADER) + - PaletteSize ((LPSTR) &bmInfoHdr) + bmInfoHdr.biSizeImage); + hDIB = GlobalAlloc (GHND, sizeof (BITMAPINFOHEADER) + + wxPaletteSize ((LPSTR) &bmInfoHdr) + bmInfoHdr.biSizeImage); if (!hDIB) return NULL; @@ -869,7 +882,7 @@ HANDLE BitmapToDIB (HBITMAP hBitmap, HPALETTE hPal) #endif *lpbmInfoHdr = bmInfoHdr; - lpBits = FindDIBBits ((LPSTR) lpbmInfoHdr); + lpBits = wxFindDIBBits ((LPSTR) lpbmInfoHdr); // Now, we need a DC to hold our bitmap. If the app passed us @@ -889,12 +902,12 @@ HANDLE BitmapToDIB (HBITMAP hBitmap, HPALETTE hPal) // it party on our bitmap. It will fill in the color table, // and bitmap bits of our global memory block. - if (!GetDIBits (hMemDC, - hBitmap, - 0, - Bitmap.bmHeight, - lpBits, - (LPBITMAPINFO) lpbmInfoHdr, + if (!GetDIBits (hMemDC, + hBitmap, + 0, + Bitmap.bmHeight, + lpBits, + (LPBITMAPINFO) lpbmInfoHdr, DIB_RGB_COLORS)) { GlobalUnlock (hDIB); @@ -915,13 +928,13 @@ HANDLE BitmapToDIB (HBITMAP hBitmap, HPALETTE hPal) return hDIB; } -bool wxSaveBitmap(char *filename, wxBitmap *bitmap, wxColourMap *colourmap) +bool wxSaveBitmap(wxChar *filename, wxBitmap *bitmap, wxPalette *colourmap) { HPALETTE hPalette = 0; if (colourmap) hPalette = (HPALETTE) colourmap->GetHPALETTE(); - HANDLE dibHandle = BitmapToDIB((HBITMAP) bitmap->GetHBITMAP(), hPalette); + HANDLE dibHandle = wxBitmapToDIB((HBITMAP) bitmap->GetHBITMAP(), hPalette); if (dibHandle) { bool success = (WriteDIB(filename, dibHandle) != 0);