home *** CD-ROM | disk | FTP | other *** search
/ Using Visual Basic 5 (Platinum Edition) / vb5.iso / ACTIVEX / SRDVID / DATA.Z / dib.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-10  |  2.3 KB  |  96 lines

  1. /*----------------------------------------------------------------------------*\
  2. |   Routines for dealing with Device independent bitmaps                       |
  3. \*----------------------------------------------------------------------------*/
  4.  
  5. #include "stdafx.h"
  6.  
  7. #include <windows.h>
  8. #include <windowsx.h>
  9. #include <memory.h>
  10.  
  11. #ifndef __DIB_H__
  12.     #include "dib.h"
  13. #endif
  14.  
  15. //#define _huge
  16. //#define hmemcpy memcpy
  17.  
  18. #define BFT_ICON   0x4349   /* 'IC' */
  19. #define BFT_BITMAP 0x4d42   /* 'BM' */
  20. #define BFT_CURSOR 0x5450   /* 'PT' */
  21.  
  22. /* flags for _lseek */
  23. //#define  SEEK_CUR 1
  24. //#define  SEEK_END 2
  25. //#define  SEEK_SET 0
  26.  
  27. static BOOL ReadHeader( HFILE fh, BITMAPINFO* bm )
  28. {
  29.     BITMAPFILEHEADER bf;
  30.  
  31.     if( sizeof(bf) != _lread( fh, (LPSTR)&bf, sizeof(bf) ) )
  32.         return FALSE;
  33.  
  34.     if( bf.bfType != BFT_BITMAP )
  35.         return FALSE;
  36.  
  37.     if( sizeof( bm->bmiHeader) != _lread( fh, (LPSTR)&bm->bmiHeader, sizeof(BITMAPINFOHEADER) ) )
  38.         return FALSE;
  39.  
  40.     FixBitmapInfo(&bm->bmiHeader);
  41.  
  42.     if( DibNumColors(&bm->bmiHeader) )
  43.         _lread( fh, &bm->bmiColors, DibNumColors(&bm->bmiHeader) * sizeof(RGBQUAD) );
  44.  
  45.     if( bf.bfOffBits != 0L )
  46.         _llseek( fh, bf.bfOffBits, FILE_BEGIN );
  47.     
  48.     return TRUE;
  49. }
  50.  
  51. /*
  52.  *   Open a DIB file and return a MEMORY DIB in striped format
  53.  *
  54.  *   BITMAP INFO    bi
  55.  *   palette data
  56.  *   bits....
  57.  *
  58.  */
  59. UINT ReadDib( LPCSTR szFile, BITMAPINFO* bm, BYTE ** ppbits )
  60. {
  61.     HFILE fh;
  62.     OFSTRUCT of;
  63.     long lBytes;
  64.  
  65.     *ppbits = NULL;
  66.     
  67.     fh = OpenFile( szFile, &of, OF_READ );
  68.     if( fh == HFILE_ERROR )
  69.         return DIB_FILEERROR;
  70.  
  71.     if( !ReadHeader( fh, bm ) )
  72.         return DIB_FILEERROR;
  73.  
  74.     // Make sure it's a DIB we can deal with
  75.     if( !(DibBitCount(&bm->bmiHeader) == 8 ||
  76.           DibBitCount(&bm->bmiHeader) == 24) ||
  77.          (DibBitCount(&bm->bmiHeader) == 8 &&
  78.           DibNumColors(&bm->bmiHeader) != 256) ||
  79.         (bm->bmiHeader.biCompression != BI_RGB) )
  80.         return DIB_UNSUPPORTED;
  81.  
  82.     // Allocate storage for the bits
  83.     if( (*ppbits = (BYTE *)GlobalAllocPtr( GHND, bm->bmiHeader.biSizeImage )) == NULL )
  84.         return DIB_OUTOFMEMORY;
  85.  
  86.     // Read in the bits
  87.     lBytes = _hread( fh, (BYTE*)*ppbits, bm->bmiHeader.biSizeImage );
  88.     if(  (DWORD)lBytes < bm->bmiHeader.biSizeImage || lBytes == HFILE_ERROR )
  89.         return DIB_FILEERROR;
  90.  
  91.     _lclose(fh);
  92.  
  93.     return DIB_OK;
  94. }
  95.  
  96.