Blame libfreerdp/primitives/prim_alphaComp.c

Packit 1fb8d4
/* FreeRDP: A Remote Desktop Protocol Client
Packit 1fb8d4
 * Alpha blending routines.
Packit 1fb8d4
 * vi:ts=4 sw=4:
Packit 1fb8d4
 *
Packit 1fb8d4
 * (c) Copyright 2012 Hewlett-Packard Development Company, L.P.
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Packit 1fb8d4
 * not use this file except in compliance with the License. You may obtain
Packit 1fb8d4
 * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
Packit 1fb8d4
 * or implied. See the License for the specific language governing
Packit 1fb8d4
 * permissions and limitations under the License.
Packit 1fb8d4
 *
Packit 1fb8d4
 * Note: this code assumes the second operand is fully opaque,
Packit 1fb8d4
 * e.g.
Packit 1fb8d4
 *   newval = alpha1*val1 + (1-alpha1)*val2
Packit 1fb8d4
 * rather than
Packit 1fb8d4
 *   newval = alpha1*val1 + (1-alpha1)*alpha2*val2
Packit 1fb8d4
 * The IPP gives other options.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/types.h>
Packit 1fb8d4
#include <freerdp/primitives.h>
Packit 1fb8d4
Packit 1fb8d4
#include "prim_internal.h"
Packit 1fb8d4
Packit Service 5a9772
#define ALPHA(_k_) (((_k_)&0xFF000000U) >> 24)
Packit Service 5a9772
#define RED(_k_) (((_k_)&0x00FF0000U) >> 16)
Packit Service 5a9772
#define GRN(_k_) (((_k_)&0x0000FF00U) >> 8)
Packit Service 5a9772
#define BLU(_k_) (((_k_)&0x000000FFU))
Packit 1fb8d4
Packit 1fb8d4
/* ------------------------------------------------------------------------- */
Packit Service 5a9772
static pstatus_t general_alphaComp_argb(const BYTE* pSrc1, UINT32 src1Step, const BYTE* pSrc2,
Packit Service 5a9772
                                        UINT32 src2Step, BYTE* pDst, UINT32 dstStep, UINT32 width,
Packit Service 5a9772
                                        UINT32 height)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 y;
Packit 1fb8d4
Packit 1fb8d4
	for (y = 0; y < height; y++)
Packit 1fb8d4
	{
Packit Service 5a9772
		const UINT32* sptr1 = (const UINT32*)(pSrc1 + y * src1Step);
Packit Service 5a9772
		const UINT32* sptr2 = (const UINT32*)(pSrc2 + y * src2Step);
Packit Service 5a9772
		UINT32* dptr = (UINT32*)(pDst + y * dstStep);
Packit 1fb8d4
		UINT32 x;
Packit 1fb8d4
Packit 1fb8d4
		for (x = 0; x < width; x++)
Packit 1fb8d4
		{
Packit 1fb8d4
			const UINT32 src1 = *sptr1++;
Packit 1fb8d4
			const UINT32 src2 = *sptr2++;
Packit 1fb8d4
			UINT32 alpha = ALPHA(src1) + 1;
Packit 1fb8d4
Packit 1fb8d4
			if (alpha == 256)
Packit 1fb8d4
			{
Packit 1fb8d4
				/* If alpha is 255+1, just copy src1. */
Packit 1fb8d4
				*dptr++ = src1;
Packit 1fb8d4
			}
Packit 1fb8d4
			else if (alpha <= 1)
Packit 1fb8d4
			{
Packit 1fb8d4
				/* If alpha is 0+1, just copy src2. */
Packit 1fb8d4
				*dptr++ = src2;
Packit 1fb8d4
			}
Packit 1fb8d4
			else
Packit 1fb8d4
			{
Packit 1fb8d4
				/* A perfectly accurate blend would do (a*src + (255-a)*dst)/255
Packit 1fb8d4
				 * rather than adding one to alpha and dividing by 256, but this
Packit 1fb8d4
				 * is much faster and only differs by one 16% of the time.
Packit 1fb8d4
				 * I'm not sure who first designed the double-ops trick
Packit 1fb8d4
				 * (Red Blue and Alpha Green).
Packit 1fb8d4
				 */
Packit 1fb8d4
				UINT32 rb, ag;
Packit 1fb8d4
				UINT32 s2rb = src2 & 0x00FF00FFU;
Packit 1fb8d4
				UINT32 s2ag = (src2 >> 8) & 0x00FF00FFU;
Packit 1fb8d4
				UINT32 s1rb = src1 & 0x00FF00FFU;
Packit 1fb8d4
				UINT32 s1ag = (src1 >> 8) & 0x00FF00FFU;
Packit 1fb8d4
				UINT32 drb = s1rb - s2rb;
Packit 1fb8d4
				UINT32 dag = s1ag - s2ag;
Packit 1fb8d4
				drb *= alpha;
Packit 1fb8d4
				dag *= alpha;
Packit Service 5a9772
				rb = ((drb >> 8) + s2rb) & 0x00FF00FFU;
Packit Service 5a9772
				ag = (((dag >> 8) + s2ag) << 8) & 0xFF00FF00U;
Packit 1fb8d4
				*dptr++ = rb | ag;
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return PRIMITIVES_SUCCESS;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/* ------------------------------------------------------------------------- */
Packit 1fb8d4
void primitives_init_alphaComp(primitives_t* prims)
Packit 1fb8d4
{
Packit 1fb8d4
	prims->alphaComp_argb = general_alphaComp_argb;
Packit 1fb8d4
}