aboutsummaryrefslogtreecommitdiff
path: root/Src/Wasabi/api/imgldr/imggen/solid.cpp
blob: 7f461eaa18464eed1843843db61ea74b9b15cf98 (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
#include "precomp.h"

#include "solid.h"
#include <api/xml/xmlparams.h>
#include <api/memmgr/api_memmgr.h>

#ifndef _WASABIRUNTIME

BEGIN_SERVICES(SolidGen_Svc);
DECLARE_SERVICETSINGLE(svc_imageGenerator, SolidImage);
END_SERVICES(SolidGen_Svc, _SolidGen_Svc);

#ifdef _X86_
extern "C" { int _link_SolidGen_Svc; }
#else
extern "C" { int __link_SolidGen_Svc; }
#endif

#endif

int SolidImage::testDesc(const wchar_t *desc)
{
	return !WCSICMP(desc, L"$solid");
}

void premultiply(ARGB32 *m_pBits, int nwords)
{
	for (; nwords > 0; nwords--, m_pBits++)
	{
		unsigned __int8 *pixel = (unsigned __int8 *)m_pBits;
		unsigned int alpha = pixel[3];
		if (alpha == 255) continue;
		pixel[0] = (pixel[0] * alpha) >> 8;	// blue
		pixel[1] = (pixel[1] * alpha) >> 8;	// green
		pixel[2] = (pixel[2] * alpha) >> 8;	// red
	}
}

ARGB32 *SolidImage::genImage(const wchar_t *desc, int *has_alpha, int *w, int *h, ifc_xmlreaderparams *params)
{
	int _w = params->getItemValueInt(L"w", 1);
	if (_w == 0) _w = 1;
	int _h = params->getItemValueInt(L"h", 1);
	if (_h == 0) _h = 1;
	if (_w <= 0 || _h <= 0) return NULL;
	ARGB32 color = _byteswap_ulong(WASABI_API_SKIN->parse(params->getItemValue(L"color"), L"color") << 8);

	unsigned int alpha = params->getItemValueInt(L"alpha", 255); 
	color |= ((alpha & 0xff) << 24);

	premultiply(&color, 1);

#ifdef WASABI_COMPILE_MEMMGR
	ARGB32 *ret = (ARGB32*)WASABI_API_MEMMGR->sysMalloc(_w * _h * sizeof(ARGB32));
#else
	ARGB32 *ret = (ARGB32*)MALLOC(_w * _h * sizeof(ARGB32));
#endif

	MEMFILL<ARGB32>(ret, color, _w * _h);

	*w = _w;
	*h = _h;

	*has_alpha = (alpha == 255) ? 0 : 1;

	return ret;
}