reactos/dll/cpl/desk/dibitmap.c
Amine Khaldi 527f2f9057 [SHELL/EXPERIMENTS]
* Create a branch for some evul shell experiments.

svn path=/branches/shell-experiments/; revision=61927
2014-02-02 19:37:27 +00:00

98 lines
2.5 KiB
C

/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS Display Control Panel
* FILE: dll/cpl/desk/dibitmap.c
* PURPOSE: DIB loading
*
* PROGRAMMERS: Trevor McCort (lycan359@gmail.com)
*/
#include "desk.h"
PDIBITMAP
DibLoadImage(LPTSTR lpFilename)
{
BOOL bSuccess;
DWORD dwFileSize, dwHighSize, dwBytesRead;
HANDLE hFile;
PDIBITMAP lpBitmap;
hFile = CreateFile(lpFilename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
return NULL;
dwFileSize = GetFileSize(hFile, &dwHighSize);
if (dwHighSize)
{
CloseHandle(hFile);
return NULL;
}
lpBitmap = HeapAlloc(GetProcessHeap(), 0, sizeof(DIBITMAP));
if (lpBitmap == NULL)
{
CloseHandle(hFile);
return NULL;
}
lpBitmap->header = HeapAlloc(GetProcessHeap(), 0, dwFileSize);
if (lpBitmap->header == NULL)
{
HeapFree(GetProcessHeap(), 0, lpBitmap);
CloseHandle(hFile);
return NULL;
}
bSuccess = ReadFile(hFile, lpBitmap->header, dwFileSize, &dwBytesRead, NULL);
CloseHandle(hFile);
if (!bSuccess ||
(dwBytesRead != dwFileSize) ||
(lpBitmap->header->bfType != * (WORD *) "BM") ||
(lpBitmap->header->bfSize != dwFileSize))
{
HeapFree(GetProcessHeap(), 0, lpBitmap->header);
HeapFree(GetProcessHeap(), 0, lpBitmap);
return NULL;
}
lpBitmap->info = (BITMAPINFO *)(lpBitmap->header + 1);
lpBitmap->bits = (BYTE *)lpBitmap->header + lpBitmap->header->bfOffBits;
/* Get the DIB width and height */
if (lpBitmap->info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
{
lpBitmap->width = ((BITMAPCOREHEADER *)lpBitmap->info)->bcWidth;
lpBitmap->height = ((BITMAPCOREHEADER *)lpBitmap->info)->bcHeight;
}
else
{
lpBitmap->width = lpBitmap->info->bmiHeader.biWidth;
lpBitmap->height = abs(lpBitmap->info->bmiHeader.biHeight);
}
return lpBitmap;
}
VOID
DibFreeImage(PDIBITMAP lpBitmap)
{
if (lpBitmap == NULL)
return;
/* Free the header */
if (lpBitmap->header != NULL)
HeapFree(GetProcessHeap(), 0, lpBitmap->header);
/* Free the bitmap structure */
if (lpBitmap != NULL)
HeapFree(GetProcessHeap(), 0, lpBitmap);
}