forked from eoserv/eomap-classic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhbitmap.cpp
More file actions
63 lines (45 loc) · 1.48 KB
/
hbitmap.cpp
File metadata and controls
63 lines (45 loc) · 1.48 KB
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
#include "hbitmap.hpp"
std::unique_ptr<a5::Bitmap> convert_hbitmap_to_bitmap(HBITMAP bitmap, a5::Pixel_Format format)
{
BITMAP bm;
if (!GetObject(bitmap, sizeof(bm), reinterpret_cast<LPSTR>(&bm)))
{
return 0;
}
std::unique_ptr<a5::Bitmap> bmp(new a5::Bitmap(bm.bmWidth, bm.bmHeight));
std::unique_ptr<a5::Bitmap_Lock> bmp_lock(bmp->Lock(format));
BYTE *data = reinterpret_cast<BYTE *>(bmp_lock->Data());
if (!bitmap)
{
return 0;
}
if (!GetObject(bitmap, sizeof(bm), reinterpret_cast<LPSTR>(&bm)))
{
return 0;
}
int pitch = bm.bmWidth * bmp_lock->Format().ByteSize();
pitch = (pitch + 3) & ~3;
std::unique_ptr<BYTE[]> pixels(new BYTE[bm.bmHeight * pitch]);
BITMAPINFOHEADER bi;
ZeroMemory(&bi, sizeof(BITMAPINFOHEADER));
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biBitCount = bmp_lock->Format().BitSize();
bi.biPlanes = 1;
bi.biWidth = bm.bmWidth;
bi.biHeight = -abs(bm.bmHeight);
bi.biClrUsed = 256;
bi.biCompression = BI_RGB;
BITMAPINFO *binfo = static_cast<BITMAPINFO *>(std::malloc(sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 256));
binfo->bmiHeader = bi;
HDC hdc = GetDC(NULL);
GetDIBits(hdc, bitmap, 0, bm.bmHeight, pixels.get(), binfo, DIB_RGB_COLORS);
std::free(binfo);
ReleaseDC(NULL, hdc);
int height = std::abs(std::min(int(bm.bmHeight), int(bmp->Height())));
int width = std::abs(std::min(pitch, int(bmp_lock->Pitch())));
for (int y = 0; y < height; ++y)
{
std::memcpy(data + bmp_lock->Pitch() * y, &pixels[pitch * y], width);
}
return bmp;
}