/******************************************************************************

Copyright (c) 2005 Analog Devices.  All Rights Reserved.

This software is proprietary and confidential.  By using this software you agree
to the terms of the associated Analog Devices License Agreement.  

******************************************************************************/



/******************************************************************************

$File:		ImageProcess.c $
$Revision: 1.1 $
$Date: 2007/05/22 18:20:12 $


Description: This file contains functions convert a row of data to another
	format, and include an overlay.
	

Modification History:
====================
Revision 1.0 build 1 - BJ 2006/03/27
	Created file
	
	
static ADI_DMA_STREAM_HANDLE  StreamHandle;      // handle to the memory stream
	Result = adi_dma_MemoryOpen(DMAManagerHandle,
							ADI_DMA_MDMA_0, 
							(void *)0x12345678, 
							&StreamHandle, 
							NULL);

	// use DMA to copy the data, parameters are
	//      handle to stream, 
	//      destination address, 
	//      source address, 
	//      element width, 
	//      # bytes to transfer
	//      callback function
	Result = adi_dma_MemoryCopy(StreamHandle, 
							Dest, 
							Src, 
							1, 
							DATA_SIZE, 
							Callback);
	

******************************************************************************/



/******************************************************************************

Include files

******************************************************************************/

#include <ada_gfx/imageprocess.h>
#include <ccblkfn.h>




/******************************************************************************

	Function: ImageProcess_rgb2rgb
	
	Description: This function takes an input buffer, output buffer and
		overlay buffer and combines the input and overlay and writes back to
		the output.

******************************************************************************/

void ImageProcess_rgb2rgb(
	unsigned short InSize, unsigned long *Input,
	unsigned short OutSize, unsigned long *Output,
	unsigned long Transparent)
{
	unsigned short i,j,s=OutSize>InSize?InSize:OutSize;
	
	
	if (!Input) return;
	
	
	Transparent&=0xFFFF;
	Input+=OutSize<InSize?(InSize-OutSize)>>1:0;
	for (i=0,j=OutSize>InSize?(OutSize-InSize)>>1:0;i<s;i++,j++)
		Output[j]=
			(((unsigned short *)(Output+j))[0]==Transparent?((unsigned short *)(Input+i))[0]:((unsigned short *)(Output+j))[0])|
			((((unsigned short *)(Output+j))[1]==Transparent?((unsigned short *)(Input+i))[1]:((unsigned short *)(Output+j))[1])<<16);
}




/******************************************************************************

	Function: ImageProcess_grey2rgb
	
	Description: This function takes an input buffer, output buffer and
		overlay buffer and combines the input and overlay and writes back to
		the output.

******************************************************************************/

void ImageProcess_grey2rgb(
	unsigned short InSize, unsigned long *Input,
	unsigned short OutSize, unsigned long *Output,
	unsigned long Transparent)
{
	unsigned short i,j,s=OutSize>InSize?InSize:OutSize;
	
	
	if (!Input) return;
	
	
	Transparent=((Transparent&0xFFFF)<<16)|(Transparent&0x0000FFFF);
	Input+=OutSize<InSize?(InSize-OutSize)>>1:0;
	for (i=0,j=OutSize>InSize?(OutSize-InSize)>>1:0;i<s;i++,j++)
	{
	    unsigned long Col=Input[i];
	    Col=((Col>>5)&0x001F001F)|((Col<<1)&0x07E007E0)|((Col<<6)&0xF800F800);
		Output[j]=
			(((Output[j]^Transparent)&0x0000FFFF?Output[j]:Col)&0x0000FFFF)|
			(((Output[j]^Transparent)&0xFFFF0000?Output[j]:Col)&0xFFFF0000);
	}
}




/******************************************************************************

	Function: ImageProcess_yuv2yuv
	
	Description: This function takes an input buffer, output buffer and
		overlay buffer and combines the input and overlay and writes back to
		the output.

******************************************************************************/

void ImageProcess_yuv2yuv(
	unsigned short InSize, unsigned long *Input,
	unsigned short OutSize, unsigned long *Output,
	unsigned long Transparent)
{
	unsigned short i,j,s=OutSize>InSize?InSize:OutSize;
	
	
	if (!Input) return;
	
	
	Input+=OutSize<InSize?(InSize-OutSize)>>1:0;
	for (i=0,j=OutSize>InSize?(OutSize-InSize)>>1:0;i<s;i++,j++)
	    if (Output[j]==Transparent) Output[j]=Input[i];
}




/******************************************************************************

	Function: ImageProcess_grey2yuv
	
	Description: This function takes an input buffer, output buffer and
		overlay buffer and combines the input and overlay and writes back to
		the output.

******************************************************************************/

void ImageProcess_grey2yuv(
	unsigned short InSize, unsigned long *Input,
	unsigned short OutSize, unsigned long *Output,
	unsigned long Transparent)
{
	unsigned short i,j,s=OutSize>InSize?InSize:OutSize;
	
	
	if (!Input) return;
	
	
	Input+=OutSize<InSize?(InSize-OutSize)>>1:0;
	for (i=0,j=OutSize>InSize?(OutSize-InSize)>>1:0;i<s;i++,j++)
	    if (Output[j]==Transparent) Output[j]=(((Input[i]*54)&0xFF00FF00)+0x10001000)|0x00800080;
}




/******************************************************************************

	Function: ImageProcess_yuv2bgr
	
	Description: This function takes an input buffer, output buffer and
		overlay buffer and combines the input and overlay and writes back to
		the output.

		Cb=*(pYUV+n+j);
		Y0=*(pYUV+n+j+1);
		Cr=*(pYUV+n+j+2);
		Y1=*(pYUV+n+j+3); 
				
 		Red = Y0 + 1.397 * (Cr-RGBOFF);
 		Green = Y0 - 0.711 * (Cr-RGBOFF) - 0.343 * (Cb-RGBOFF);
	 	Blue = Y0 + 1.765 * (Cb-RGBOFF);

******************************************************************************/

void ImageProcess_yuv2bgr(
	unsigned short InSize, unsigned long *Input,
	unsigned short OutSize, unsigned long *Output,
	unsigned long Transparent)
{
	unsigned short i,j,s=OutSize>InSize?InSize:OutSize;
	
	
	if (!Input) return;
	
	
	Input+=OutSize<InSize?(InSize-OutSize)>>1:0;
	for (i=0,j=OutSize>InSize?(OutSize-InSize)>>1:0;i<s;i++,j++)
	{
	    unsigned long Col=Input[j]^0x00800080;
	    char *Tmp=(char *)&Col,
	    	r=(357*Tmp[2])>>8,
	    	g=(182*Tmp[2]+87*Tmp[0])>>8,
	    	b=(451*Tmp[0])>>8;
		if (Output[j]==Transparent) Output[j]=
			(((Tmp[1]+r)>>3)&0x0000001F)|
			(((Tmp[1]-g)<<3)&0x000007E0)|
			(((Tmp[1]+b)<<8)&0x0000F800)|
			(((Tmp[3]+r)<<13)&0x001F0000)|
			(((Tmp[3]-g)<<19)&0x07E00000)|
			(((Tmp[3]+b)<<24)&0xF8000000);
	}
}

/*****************************************************************************/
