- int fh;
- LPBITMAPINFOHEADER lpbi;
- OFSTRUCT of;
- BITMAPFILEHEADER bf;
- WORD nNumColors;
- bool result = FALSE;
- WORD offBits;
- HDC hDC;
- bool bCoreHead = FALSE;
- HANDLE hDIB = 0;
-
- /* Open the file and get a handle to it's BITMAPINFO */
-
- fh = OpenFile (wxConvertWX2MB(lpFileName), &of, OF_READ);
- if (fh == -1) {
- wxLogError(_("Can't open file '%s'"), lpFileName);
- return (0);
- }
-
- hDIB = GlobalAlloc(GHND, (DWORD)(sizeof(BITMAPINFOHEADER) +
- 256 * sizeof(RGBQUAD)));
- if (!hDIB)
- return(0);
-
- lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
-
- /* read the BITMAPFILEHEADER */
- if (sizeof (bf) != _lread (fh, (LPSTR)&bf, sizeof (bf)))
- goto ErrExit;
-
- if (bf.bfType != 0x4d42) /* 'BM' */
- goto ErrExit;
-
- if (sizeof(BITMAPCOREHEADER) != _lread (fh, (LPSTR)lpbi, sizeof(BITMAPCOREHEADER)))
- goto ErrExit;
-
- if (lpbi->biSize == sizeof(BITMAPCOREHEADER))
- {
- lpbi->biSize = sizeof(BITMAPINFOHEADER);
- lpbi->biBitCount = ((LPBITMAPCOREHEADER)lpbi)->bcBitCount;
- lpbi->biPlanes = ((LPBITMAPCOREHEADER)lpbi)->bcPlanes;
- lpbi->biHeight = ((LPBITMAPCOREHEADER)lpbi)->bcHeight;
- lpbi->biWidth = ((LPBITMAPCOREHEADER)lpbi)->bcWidth;
- bCoreHead = TRUE;
- }
- else
- {
- // get to the start of the header and read INFOHEADER
- _llseek(fh,sizeof(BITMAPFILEHEADER),SEEK_SET);
- if (sizeof(BITMAPINFOHEADER) != _lread (fh, (LPSTR)lpbi, sizeof(BITMAPINFOHEADER)))
- goto ErrExit;
- }
-
- nNumColors = (WORD)lpbi->biClrUsed;
- if ( nNumColors == 0 )
- {
- /* no color table for 24-bit, default size otherwise */
- if (lpbi->biBitCount != 24)
- nNumColors = 1 << lpbi->biBitCount; /* standard size table */
- }
-
- /* fill in some default values if they are zero */
- if (lpbi->biClrUsed == 0)
- lpbi->biClrUsed = nNumColors;
-
- if (lpbi->biSizeImage == 0)
- {
- lpbi->biSizeImage = ((((lpbi->biWidth * (DWORD)lpbi->biBitCount) + 31) & ~31) >> 3)
- * lpbi->biHeight;
- }
-
- /* get a proper-sized buffer for header, color table and bits */
- GlobalUnlock(hDIB);
- hDIB = GlobalReAlloc(hDIB, lpbi->biSize +
- nNumColors * sizeof(RGBQUAD) +
- lpbi->biSizeImage, 0);
- if (!hDIB) /* can't resize buffer for loading */
- goto ErrExit2;
-
- lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
-
- /* read the color table */
- if (!bCoreHead)
- _lread(fh, (LPSTR)(lpbi) + lpbi->biSize, nNumColors * sizeof(RGBQUAD));
- else
+ wxCHECK_MSG( pbmi, 0, _T("invalid DIB in ConvertToBitmap") );
+
+ // here we get BITMAPINFO struct followed by the actual bitmap bits and
+ // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
+ const BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader;
+
+ // get the pointer to the start of the real image data if we have a plain
+ // DIB and not a DIB section (in the latter case the pointer must be passed
+ // to us by the caller)
+ if ( !bits )