Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » Alpha composition OVER operator WITHOUT premultiplied alpha
Alpha composition OVER operator WITHOUT premultiplied alpha [message #4175] Thu, 27 July 2006 16:42
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
...well, I was not able to google the effective code for this one, so I had to invent it myself Smile :

struct sBlends {
	int16 m;
	byte  a;
};

sBlends *sblends;

void sOnceInitBlends()
{
	ONCELOCK {
		RTIMING("InitBlends");
		sblends = (sBlends *)MemoryAllocPermanent(256 * 256 * sizeof(sBlends));
		for(int Fa = 0; Fa <= 255; Fa++)
			for(int Ba = 0; Ba <= 255; Ba++) {
				double A = (Fa / 255.0 + Ba / 255.0 - Fa / 255.0 * Ba / 255.0);
				double dDa = 255 * A;
				sblends[(Ba << 8) + Fa].a = minmax((int)(255 * A + 0.5), 0, 255);
				sblends[(Ba << 8) + Fa].m = A > 0.001 ? int(256 * (Fa / 255.0) / A + 0.5) : 0;
			}
	}
}

inline void sInitBlends()
{
	if(!sblends)
		sOnceInitBlends();
}

void AlphaBlend(RGBA *b, const RGBA *f, int len)
{
	RTIMING("Full");
	sInitBlends();
	const RGBA *e = f + len;
	while(f < e) {
		sBlends& x = sblends[(b->a << 8) + f->a];
		int m = x.m;
		b->a = x.a;
		b->r += m * (f->r - b->r) >> 8;
		b->g += m * (f->g - b->g) >> 8;
		b->b += m * (f->b - b->b) >> 8;
		b++;
		f++;
	}
}


..my measurements show it only 20% slower on CPU with 128KB cache than premultiplied alpha code.

Mirek
Previous Topic: Inverse palette conversion algorithm...
Next Topic: C++ issues (reply to PM)
Goto Forum:
  


Current Time: Tue Apr 23 17:15:34 CEST 2024

Total time taken to generate the page: 0.02756 seconds