[DDK]: Merge 46183 from header-branch.
[reactos.git] / reactos / dll / cpl / desk / dibitmap.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS Display Control Panel
5 * FILE: lib/cpl/desk/dibitmap.c
6 * PURPOSE: DIB loading
7 *
8 * PROGRAMMERS: Trevor McCort (lycan359@gmail.com)
9 */
10
11 #include "desk.h"
12
13 PDIBITMAP
14 DibLoadImage(LPTSTR lpFilename)
15 {
16 BOOL bSuccess;
17 DWORD dwFileSize, dwHighSize, dwBytesRead;
18 HANDLE hFile;
19 PDIBITMAP lpBitmap;
20
21 hFile = CreateFile(lpFilename,
22 GENERIC_READ,
23 FILE_SHARE_READ,
24 NULL,
25 OPEN_EXISTING,
26 FILE_FLAG_SEQUENTIAL_SCAN,
27 NULL);
28 if (hFile == INVALID_HANDLE_VALUE)
29 return NULL;
30
31 dwFileSize = GetFileSize(hFile, &dwHighSize);
32
33 if (dwHighSize)
34 {
35 CloseHandle(hFile);
36 return NULL;
37 }
38
39 lpBitmap = HeapAlloc(GetProcessHeap(), 0, sizeof(DIBITMAP));
40 if (lpBitmap == NULL)
41 return NULL;
42
43 lpBitmap->header = HeapAlloc(GetProcessHeap(), 0, dwFileSize);
44 if (lpBitmap->header == NULL)
45 {
46 CloseHandle(hFile);
47 return NULL;
48 }
49
50 bSuccess = ReadFile(hFile, lpBitmap->header, dwFileSize, &dwBytesRead, NULL);
51 CloseHandle(hFile);
52
53 if (!bSuccess ||
54 (dwBytesRead != dwFileSize) ||
55 (lpBitmap->header->bfType != * (WORD *) "BM") ||
56 (lpBitmap->header->bfSize != dwFileSize))
57 {
58 HeapFree(GetProcessHeap(), 0, lpBitmap->header);
59 return NULL;
60 }
61
62 lpBitmap->info = (BITMAPINFO *)(lpBitmap->header + 1);
63 lpBitmap->bits = (BYTE *)lpBitmap->header + lpBitmap->header->bfOffBits;
64
65 /* Get the DIB width and height */
66 if (lpBitmap->info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
67 {
68 lpBitmap->width = ((BITMAPCOREHEADER *)lpBitmap->info)->bcWidth;
69 lpBitmap->height = ((BITMAPCOREHEADER *)lpBitmap->info)->bcHeight;
70 }
71 else
72 {
73 lpBitmap->width = lpBitmap->info->bmiHeader.biWidth;
74 lpBitmap->height = abs(lpBitmap->info->bmiHeader.biHeight);
75 }
76
77 return lpBitmap;
78 }
79
80
81 VOID
82 DibFreeImage(PDIBITMAP lpBitmap)
83 {
84 if (lpBitmap == NULL)
85 return;
86
87 /* Free the header */
88 if (lpBitmap->header != NULL)
89 HeapFree(GetProcessHeap(), 0, lpBitmap->header);
90
91 /* Free the bitmap structure */
92 if (lpBitmap != NULL)
93 HeapFree(GetProcessHeap(), 0, lpBitmap);
94 }