home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vrml2gl.zip / OS2 / LOAD_BMP.C next >
C/C++ Source or Header  |  1997-03-17  |  2KB  |  61 lines

  1. #include "Load_BMP.h"
  2. /*==========================================================================*/
  3. /*  This function is will return a pointer to the bitmap image. Also the    */
  4. /* height and width will be filled in. As I allocate memory for the image   */
  5. /* you must free it yourself when your done.                                */
  6. /*==========================================================================*/
  7. PCHAR LoadBitmapFile ( PSZ filename, int *height, int *width )
  8. {
  9.    PBITMAPFILEHEADER2   pbfh = NULL;
  10.    PBITMAPINFOHEADER2   pbih = NULL;
  11.    ULONG                ulcbSize = 0L, 
  12.                         cbRead = 0L;
  13.    PVOID                memoryBuffer = NULL;
  14.    FILESTATUS3          fileInfo;
  15.    HFILE                hfFile;
  16.    APIRET               returnCode;
  17.    PCHAR                pImage;          
  18.  
  19.    /* Open the Bitmap file. */
  20.    DosOpen( filename, &hfFile, &cbRead, 0L, FILE_READONLY, OPEN_ACTION_OPEN_IF_EXISTS, OPEN_SHARE_DENYNONE, 0L);
  21.  
  22.    if ( DosQueryFileInfo( hfFile, 1, (PVOID)&fileInfo, sizeof(FILESTATUS3)) != 0 ) 
  23.        DosClose( hfFile );
  24.  
  25.    /* Get the file size and allocate the memory to hold it. */
  26.    ulcbSize = fileInfo.cbFile;
  27.    returnCode = DosAllocMem( &memoryBuffer, ulcbSize, fALLOC );
  28.  
  29.    if ( returnCode == 0 )
  30.    {
  31.       /* Now we have a memory buffer the size of the Bitmap load it into memory. */
  32.       returnCode = DosRead( hfFile, memoryBuffer, ulcbSize, &cbRead );
  33.  
  34.       /* Cast the data to the structure which should be at the head of an OS/2 BMP2 file. */
  35.       if ( returnCode == 0 )
  36.       {
  37.          /* Assign pointers to the bitmap header information and the data for the image bits. */
  38.          pbfh = (PBITMAPFILEHEADER2)memoryBuffer;
  39.  
  40.          /* Set the return values. */
  41.          pbih = &pbfh->bmp2;
  42.          *height = pbih->cy;
  43.          *width = pbih->cx; 
  44.  
  45.          /* Set the pointer to image. */
  46.          pImage = (PBYTE)((PBYTE)pbfh + pbfh->offBits);
  47.       }
  48.  
  49.       /* Free the memory for the bitmap. */
  50.       DosFreeMem( &memoryBuffer );      
  51.    }
  52.  
  53.    /* Close the file. */
  54.    DosClose( hfFile );
  55.  
  56.    /* Return the pointer to the image. */
  57.    return pImage;
  58. }
  59.  
  60.  
  61.