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