wpReadImageFile - Example Code

This example shows a simplified version of the code in the WPBitmap object class. This example assumes that the image file only contains a single image and it is already in bitmap format.

SOM_Scope BOOL32 bmp_wpReadImageFile(WPBitmap *somSelf)
{
   WPBitmapData *somThis = WPBitmapGetData(somSelf);

   CHAR        szFileName[CCHMAXPATH];
   HFILE       hFile = NULLHANDLE;
   ULONG       ulAction;
   FILESTATUS3 FileStatus;
   ULONG       ulFileSize;
   PBYTE       pBitmapData = NULL;
   APIRET      RC;
   ULONG       ulBytesRead;
   PBITMAPFILEHEADER pbfh;

   /* If the data has already been read, just return    */
   if(_pBitmapData)
   {
      return TRUE;
   }

   /* Find the bitmap data file
    */
   _wpQueryFilename(somSelf, szFileName, TRUE);

   /* Open the file and find out how big it is   */
   if (DosOpen(szFileName,
               &hFile,
               &ulAction,
               0L,
               FILE_NORMAL,
               FILE_OPEN,
               OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE,
               NULL))
   {
      return FALSE;
   }

   if (DosQueryFileInfo(hFile,
                        FIL_STANDARD,
                        &FileStatus,
                        sizeof(FileStatus)))
   {
      DosClose (hFile);
      return FALSE;
   }

   /* Allocate space for a buffer large enough to contain the entire file */
   ulFileSize = FileStatus.cbFile;
   pBitmapData = _wpAllocMem(somSelf,ulFileSize,FALSE);
   if (!pBitmapData)
   {
      DosClose (hFile);
      return FALSE;
   }

   /* Read the file into the buffer    */
   RC = DosRead (hFile, pBitmapData, ulFileSize, &ulBytesRead);
   DosClose(hFile);
   hFile = NULLHANDLE;
   if (RC || (ulBytesRead != ulFileSize))
   {
      _wpFreeMem (somSelf,pBitmapData);
      return FALSE;
   }

   /* Make sure the bitmap header has a valid type code and the header
    * fits within the file     */
   pbfh = (PBITMAPFILEHEADER) pBitmapData;
   if (pbfh->usType != BFT_BMAP ||
       (PCH)pbfh + pbfh->cbSize > pBitmapData + ulFileSize)
   {
      _wpFreeMem(somSelf,pBitmapData);
      return FALSE;
   }

   /* Save the bitmap data and exit    */
   _pBitmapData = pBitmapData;
   _ulBitmapDataSize = ulFileSize;
   return TRUE;
}


[Back: wpReadImageFile - Related Methods]
[Next: wpReadImageFile - Topics]