home *** CD-ROM | disk | FTP | other *** search
- /*----------------------------------------------------------------------------*\
- | Routines for dealing with Device independent bitmaps |
- \*----------------------------------------------------------------------------*/
-
- #include "stdafx.h"
-
- #include <windows.h>
- #include <windowsx.h>
- #include <memory.h>
-
- #ifndef __DIB_H__
- #include "dib.h"
- #endif
-
- //#define _huge
- //#define hmemcpy memcpy
-
- #define BFT_ICON 0x4349 /* 'IC' */
- #define BFT_BITMAP 0x4d42 /* 'BM' */
- #define BFT_CURSOR 0x5450 /* 'PT' */
-
- /* flags for _lseek */
- //#define SEEK_CUR 1
- //#define SEEK_END 2
- //#define SEEK_SET 0
-
- static BOOL ReadHeader( HFILE fh, BITMAPINFO* bm )
- {
- BITMAPFILEHEADER bf;
-
- if( sizeof(bf) != _lread( fh, (LPSTR)&bf, sizeof(bf) ) )
- return FALSE;
-
- if( bf.bfType != BFT_BITMAP )
- return FALSE;
-
- if( sizeof( bm->bmiHeader) != _lread( fh, (LPSTR)&bm->bmiHeader, sizeof(BITMAPINFOHEADER) ) )
- return FALSE;
-
- FixBitmapInfo(&bm->bmiHeader);
-
- if( DibNumColors(&bm->bmiHeader) )
- _lread( fh, &bm->bmiColors, DibNumColors(&bm->bmiHeader) * sizeof(RGBQUAD) );
-
- if( bf.bfOffBits != 0L )
- _llseek( fh, bf.bfOffBits, FILE_BEGIN );
-
- return TRUE;
- }
-
- /*
- * Open a DIB file and return a MEMORY DIB in striped format
- *
- * BITMAP INFO bi
- * palette data
- * bits....
- *
- */
- UINT ReadDib( LPCSTR szFile, BITMAPINFO* bm, BYTE ** ppbits )
- {
- HFILE fh;
- OFSTRUCT of;
- long lBytes;
-
- *ppbits = NULL;
-
- fh = OpenFile( szFile, &of, OF_READ );
- if( fh == HFILE_ERROR )
- return DIB_FILEERROR;
-
- if( !ReadHeader( fh, bm ) )
- return DIB_FILEERROR;
-
- // Make sure it's a DIB we can deal with
- if( !(DibBitCount(&bm->bmiHeader) == 8 ||
- DibBitCount(&bm->bmiHeader) == 24) ||
- (DibBitCount(&bm->bmiHeader) == 8 &&
- DibNumColors(&bm->bmiHeader) != 256) ||
- (bm->bmiHeader.biCompression != BI_RGB) )
- return DIB_UNSUPPORTED;
-
- // Allocate storage for the bits
- if( (*ppbits = (BYTE *)GlobalAllocPtr( GHND, bm->bmiHeader.biSizeImage )) == NULL )
- return DIB_OUTOFMEMORY;
-
- // Read in the bits
- lBytes = _hread( fh, (BYTE*)*ppbits, bm->bmiHeader.biSizeImage );
- if( (DWORD)lBytes < bm->bmiHeader.biSizeImage || lBytes == HFILE_ERROR )
- return DIB_FILEERROR;
-
- _lclose(fh);
-
- return DIB_OK;
- }
-
-