// BlinkGame - Hunt for the Lost Pixels
// an SDL game in honor of the BlinkenArea crew
//
// (C) 2005-06 Rene Schickbauer
//
// See License.txt for licensing information
//
#ifdef NOP_CODE
/*--------------------------------------------------------------------
					CVS AUTO-GENERATED INFO
$Author: cavac $
$Date: 2006/03/02 13:03:41 $
$Name: rel-0-3-patches $
$Revision: 1.4 $
$State: Exp $

$Log: blending.cpp,v $
Revision 1.4  2006/03/02 13:03:41  cavac
Re-formatted all .cpp and .h files a bit to get rid of the TAB/SPACE mess
that makes troubles with XCode's "re-format" feature

Revision 1.3  2006/01/19 19:19:15  cavac
Various bugfixes and performance tunings

Revision 1.2  2006/01/09 00:50:34  cavac
Bugfixed CVS autogenerated entries in background.cpp
Added CVS autogenerated entries to remaining .cpp and .h files
Updated copyright from "2005 Rene Schickbauer" to "2005-06 Rene Schickbauer"


--------------------------------------------------------------------*/
#endif //NOP_CODE

#include "globals.h"
#include "blending.h"

Uint32 blend_avg(Uint32 source, Uint32 target)
{
	Uint32 sourcer = (source >>  0) & 0xff;
	Uint32 sourceg = (source >>  8) & 0xff;
	Uint32 sourceb = (source >> 16) & 0xff;
	Uint32 targetr = (target >>  0) & 0xff;
	Uint32 targetg = (target >>  8) & 0xff;
	Uint32 targetb = (target >> 16) & 0xff;
	
	targetr = (sourcer + targetr) >> 1;
	targetg = (sourceg + targetg) >> 1;
	targetb = (sourceb + targetb) >> 1;
	
	return (targetr <<  0) |
		(targetg <<  8) |
		(targetb << 16);
}

Uint32 blend_mul(Uint32 source, Uint32 target)
{
	Uint32 sourcer = (source >>  0) & 0xff;
	Uint32 sourceg = (source >>  8) & 0xff;
	Uint32 sourceb = (source >> 16) & 0xff;
	Uint32 targetr = (target >>  0) & 0xff;
	Uint32 targetg = (target >>  8) & 0xff;
	Uint32 targetb = (target >> 16) & 0xff;
	
	targetr = (sourcer * targetr) >> 8;
	targetg = (sourceg * targetg) >> 8;
	targetb = (sourceb * targetb) >> 8;
	
	return (targetr <<  0) |
		(targetg <<  8) |
		(targetb << 16);
}

Uint32 blend_add(Uint32 source, Uint32 target)
{
	Uint32 sourcer = (source >>  0) & 0xff;
	Uint32 sourceg = (source >>  8) & 0xff;
	Uint32 sourceb = (source >> 16) & 0xff;
	Uint32 targetr = (target >>  0) & 0xff;
	Uint32 targetg = (target >>  8) & 0xff;
	Uint32 targetb = (target >> 16) & 0xff;
	
	targetr += sourcer;
	targetg += sourceg;
	targetb += sourceb;
	
	if (targetr > 0xff) targetr = 0xff;
	if (targetg > 0xff) targetg = 0xff;
	if (targetb > 0xff) targetb = 0xff;
	
	return (targetr <<  0) |
		(targetg <<  8) |
		(targetb << 16);
}
