aboutsummaryrefslogtreecommitdiff
path: root/Src/bmp/BMPLoader.cpp
blob: 38c85130452ab2a3aa184bb335864f4206ce7ecc (plain) (blame)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "BMPLoader.h"
#include "api__bmp.h"
#include <wchar.h>
#include <bfc/platform/strcmp.h>

static bool StringEnds(const wchar_t *a, const wchar_t *b)
{
	size_t aLen = wcslen(a);
	size_t bLen	= wcslen(b);
	if (aLen < bLen) return false;  // too short
	return !_wcsicmp(a + aLen- bLen, b);
}

int BMPLoader::isMine(const wchar_t *filename) 
{
	return (filename && StringEnds(filename, L".BMP"));
}

const wchar_t *BMPLoader::mimeType() 
{
	return L"image/bmp";
}

int BMPLoader::getHeaderSize() 
{
	return 2;
}

int BMPLoader::testData(const void *data, int datalen)
{
	if(datalen < 2) return 0;
	return *((WORD*)data) == (WORD)'MB';
}

static void writeFile(const wchar_t *file, const void * data, int length) {
	FILE *f = _wfopen(file,L"wb");
	if(!f) return;
	fwrite(data,length,1,f);
	fclose(f);
}

ARGB32 *BMPLoader::loadImage(const void *data, int datalen, int *w, int *h, ifc_xmlreaderparams *params)
{
	int w0=0,h0=0;
	if(!w) w = &w0;
	if(!h) h = &h0;

	wchar_t file[MAX_PATH] = {0};
	GetTempPath(MAX_PATH, file);
	GetTempFileName(file,L"wa5bmp",0,file);
	writeFile(file,data,datalen);
	
	HBITMAP hbmp = (HBITMAP)LoadImage(0, file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
	
	_wunlink(file);
	if(!hbmp) return 0;
	BITMAP bm;
	HDC hMemDC, hMemDC2;
	HBITMAP hsrcdib;
	void *srcdib;
	BITMAPINFO srcbmi = {0, };
	if(GetObject(hbmp, sizeof(BITMAP), &bm) == 0) { DeleteObject(hbmp); return 0; }

	*w = bm.bmWidth;
	*h = abs(bm.bmHeight);

	ARGB32 *newbits=NULL;
	srcbmi.bmiHeader.biSize = sizeof(srcbmi.bmiHeader);
	srcbmi.bmiHeader.biWidth = *w;
	srcbmi.bmiHeader.biHeight = -*h;
	srcbmi.bmiHeader.biPlanes = 1;
	srcbmi.bmiHeader.biBitCount = 32;
	srcbmi.bmiHeader.biCompression = BI_RGB;

	hMemDC = CreateCompatibleDC(NULL);
	hMemDC2 = CreateCompatibleDC(NULL);
	hsrcdib = CreateDIBSection(hMemDC, &srcbmi, DIB_RGB_COLORS, &srcdib, NULL, 0);
	if(hsrcdib) {
		HBITMAP hprev = (HBITMAP) SelectObject(hMemDC, hsrcdib);
		HBITMAP hprev2 = (HBITMAP) SelectObject(hMemDC2, hbmp);
		BitBlt(hMemDC, 0, 0, *w, *h, hMemDC2, 0, 0, SRCCOPY);
		newbits = (ARGB32*)WASABI_API_MEMMGR->sysMalloc((*w) * (*h) * sizeof(ARGB32));
		memcpy(newbits, srcdib, (*w)*(*h)*sizeof(ARGB32));
		{
			// put the alpha channel to 255
			unsigned char *b = (unsigned char *)newbits;
			int l = (*w) * (*h);
			for (int i = 0;i < l;i++)
				b[(i*4) + 3] = 0xff;
		}
		SelectObject(hMemDC, hprev);
		SelectObject(hMemDC2, hprev2);
		DeleteObject(hsrcdib);
	}
	DeleteDC(hMemDC2);
	DeleteDC(hMemDC);

	DeleteObject(hbmp);

	return newbits;
}

#define CBCLASS BMPLoader
START_DISPATCH;
  CB(ISMINE, isMine);
  CB(MIMETYPE, mimeType);
  CB(TESTDATA, testData);
  CB(GETHEADERSIZE, getHeaderSize);
  CB(GETDIMENSIONS, getDimensions);
  CB(LOADIMAGE, loadImage);
  CB(LOADIMAGEDATA, loadImageData);
END_DISPATCH;
#undef CBCLASS