-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoadBMP.cpp
90 lines (67 loc) · 2.06 KB
/
LoadBMP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <metahook.h>
#include "LoadBMP.h"
#include "plugins.h"
#include "perf_counter.h"
#include "cvardef.h"
int LoadBMP(const char *szFilename, byte *buffer, int bufferSize, int *width, int *height)
{
FileHandle_t file = g_pFileSystem->Open(szFilename, "rb");
if (!file)
return FALSE;
bool debugTime = (developer && (int)developer->value > 2);
float startTime;
if (debugTime)
startTime = gPerformanceCounter.GetCurTime();
BITMAPFILEHEADER bmfHeader;
LPBITMAPINFO lpbmi;
DWORD dwFileSize = g_pFileSystem->Size(file);
if (!g_pFileSystem->Read(&bmfHeader, sizeof(bmfHeader), file))
{
*width = 0;
*height = 0;
g_pFileSystem->Close(file);
return FALSE;
}
if (bmfHeader.bfType == DIB_HEADER_MARKER)
{
DWORD dwBitsSize = dwFileSize - sizeof(bmfHeader);
HGLOBAL hDIB = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dwBitsSize);
char *pDIB = (LPSTR)::GlobalLock((HGLOBAL)hDIB);
if (!g_pFileSystem->Read(pDIB, dwBitsSize, file))
{
::GlobalUnlock(hDIB);
::GlobalFree((HGLOBAL)hDIB);
*width = 0;
*height = 0;
g_pFileSystem->Close(file);
return FALSE;
}
lpbmi = (LPBITMAPINFO)pDIB;
if (width)
*width = lpbmi->bmiHeader.biWidth;
if (height)
*height = lpbmi->bmiHeader.biHeight;
unsigned char *rgba = (unsigned char *)(pDIB + sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
for (int j = 0; j < lpbmi->bmiHeader.biHeight; j++)
{
for (int i = 0; i < lpbmi->bmiHeader.biWidth; i++)
{
int y = (lpbmi->bmiHeader.biHeight - j - 1);
int offs = (y * lpbmi->bmiHeader.biWidth + i);
int offsdest = (j * lpbmi->bmiHeader.biWidth + i) * 4;
unsigned char *src = rgba + offs;
unsigned char *dst = buffer + offsdest;
dst[0] = lpbmi->bmiColors[*src].rgbRed;
dst[1] = lpbmi->bmiColors[*src].rgbGreen;
dst[2] = lpbmi->bmiColors[*src].rgbBlue;
dst[3] = 255;
}
}
::GlobalUnlock(hDIB);
::GlobalFree((HGLOBAL)hDIB);
}
g_pFileSystem->Close(file);
if (debugTime)
gEngfuncs.Con_Printf("LoadBMP: load %s take %.4f sec.\n", szFilename, gPerformanceCounter.GetCurTime() - startTime);
return TRUE;
}