- Remove ALL the unneeded "author date id revision" svn properties.
[reactos.git] / reactos / dll / cpl / desk / dibitmap.c
1 /* $Id: dibitmap.c 54535 2011-11-29 14:55:58Z dgorbachev $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS Display Control Panel
5 * FILE: dll/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 {
42 CloseHandle(hFile);
43 return NULL;
44 }
45
46 lpBitmap->header = HeapAlloc(GetProcessHeap(), 0, dwFileSize);
47 if (lpBitmap->header == NULL)
48 {
49 HeapFree(GetProcessHeap(), 0, lpBitmap);
50 CloseHandle(hFile);
51 return NULL;
52 }
53
54 bSuccess = ReadFile(hFile, lpBitmap->header, dwFileSize, &dwBytesRead, NULL);
55 CloseHandle(hFile);
56
57 if (!bSuccess ||
58 (dwBytesRead != dwFileSize) ||
59 (lpBitmap->header->bfType != * (WORD *) "BM") ||
60 (lpBitmap->header->bfSize != dwFileSize))
61 {
62 HeapFree(GetProcessHeap(), 0, lpBitmap->header);
63 HeapFree(GetProcessHeap(), 0, lpBitmap);
64 return NULL;
65 }
66
67 lpBitmap->info = (BITMAPINFO *)(lpBitmap->header + 1);
68 lpBitmap->bits = (BYTE *)lpBitmap->header + lpBitmap->header->bfOffBits;
69
70 /* Get the DIB width and height */
71 if (lpBitmap->info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
72 {
73 lpBitmap->width = ((BITMAPCOREHEADER *)lpBitmap->info)->bcWidth;
74 lpBitmap->height = ((BITMAPCOREHEADER *)lpBitmap->info)->bcHeight;
75 }
76 else
77 {
78 lpBitmap->width = lpBitmap->info->bmiHeader.biWidth;
79 lpBitmap->height = abs(lpBitmap->info->bmiHeader.biHeight);
80 }
81
82 return lpBitmap;
83 }
84
85
86 VOID
87 DibFreeImage(PDIBITMAP lpBitmap)
88 {
89 if (lpBitmap == NULL)
90 return;
91
92 /* Free the header */
93 if (lpBitmap->header != NULL)
94 HeapFree(GetProcessHeap(), 0, lpBitmap->header);
95
96 /* Free the bitmap structure */
97 if (lpBitmap != NULL)
98 HeapFree(GetProcessHeap(), 0, lpBitmap);
99 }