baseport/syborg/webcamera/webcamer_convert.cpp
author Shimizu Satoshi <s_shimizu@isb.co.jp>
Mon, 18 Oct 2010 19:39:25 +0900
changeset 124 606eafc6d6a8
permissions -rw-r--r--
Obtain an image of Webcamera from QEMU and add the Bitmap change display function.

/*
* Copyright (c) 2010 ISB.
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of the "Symbian Foundation License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
*
* Initial Contributors:
* ISB - Initial contribution
*
* Contributors:
*
* Description:
*
*/


#ifndef WEBCAMERACONVERT_H__
#include "webcamer_convert.h"
#endif

#define DP(format...) Kern::Printf(format)

/**
Constructor.
*/
DWebCameraConvert::DWebCameraConvert(TWebcameraUVC* aUvcFormat)
	{
	TUint32 wGuid = aUvcFormat->iUVCDescriptor.iFormatDescriptor.iGuid.iData1;
	switch (wGuid)
		{
	case KWebcameraYuy2:
		//Create object of DWebCameraConvertYuv.
		iConvertObject = new(ELeave) DWebCameraConvertYuv(aUvcFormat);
		break;
	default:
		break;
		}
	}

/**
Destructor.
*/
DWebCameraConvert::~DWebCameraConvert()
	{
	delete iConvertObject;
	}

TInt DWebCameraConvert::ConvertData(TUint8* aInBuf, TUint8* aOutBuf)
	{
	return iConvertObject->ConvertData(aInBuf, aOutBuf);
	}

/**
Constructor.
*/
DWebCameraConvertBase::DWebCameraConvertBase(TWebcameraUVC* aUvcFormat)
					:iUvcFormat(aUvcFormat)
	{
	}

/** 
Destructor.
*/
DWebCameraConvertBase::~DWebCameraConvertBase()
	{
	}

/**
Convert FrameData to BitmapData.

@param	aInBuf	[in]					Data to convert.
		aOutBuf	[out]					BitmapData after conversion.
@return	KErrNotSupported.				Implement in sub-class, return unsupport in base-class.
*/
TInt DWebCameraConvertBase::ConvertData(TUint8* /*aInBuf*/, TUint8* /*aOutBuf*/)
	{
	return KErrNotSupported;
	}

/**
Constructor.
*/
DWebCameraConvertYuv::DWebCameraConvertYuv(TWebcameraUVC* aUvcFormat)
					:DWebCameraConvertBase(aUvcFormat)
	{
	}

/**
Destructor.
*/
DWebCameraConvertYuv::~DWebCameraConvertYuv()
	{
	}

/**
Convert FrameData to BitmapData(yuv->Bitmap).

@param	aInBuf	[in]					Data to convert.
		aOutBuf	[out]					BitmapData after conversion.
@return	KErrNone.
*/
TInt DWebCameraConvertYuv::ConvertData(TUint8* aInBuf, TUint8* aOutBuf)
	{
	TUint wInPos = 0;							//Declaration to get FrameData before conversion.
	TUint wOutPos = 0;							//Declaration to store BitmapData after conversion.
	TUint wPixel32 = 0;							//Declaration to store data(1pixel32bit) after conversion from yuv to rgb.
	TInt wY0, wU, wY1, wV;						//Declaration to store signal that get from rgb.
	TUint16 wWidth, wHeight;					//Declaration to store width and height from argument aVideoData.

	wWidth = iUvcFormat->iUVCDescriptor.iFrameDescriptor.iWidth;		//Store width information from aVideoData.
	wHeight = iUvcFormat->iUVCDescriptor.iFrameDescriptor.iHeight;		//Store height information from aVideoData.

	TUint wImageSize = wWidth * wHeight * 2;

	//Get width and heigth data.
	for (wInPos = 0; wInPos < wImageSize; wInPos += 4)
		{
		//Y16-bit to suitable location of YUYV.
		wY0 = (static_cast<TUint>(aInBuf[wInPos + 0]) & KWebcameraBitMask);
		wU  = (static_cast<TUint>(aInBuf[wInPos + 1]) & KWebcameraBitMask);
		wY1 = (static_cast<TUint>(aInBuf[wInPos + 2]) & KWebcameraBitMask);
		wV  = (static_cast<TUint>(aInBuf[wInPos + 3]) & KWebcameraBitMask);

		//Store RGB by wPixel32 format, that was converted from yuv.
		wPixel32 = ConvertYuvToBitmap(wY0, wU, wV);
		//Calculate element of RGB from wPixel32.
		aOutBuf[wOutPos++] = (wPixel32 & KWebcameraBitMask);
		aOutBuf[wOutPos++] = (wPixel32 & KWebcameraBitMask8) >> 8;
		aOutBuf[wOutPos++] = (wPixel32 & KWebcameraBitMask16) >> 16;

		//Store RGB by wPixel32 format, that was converetd from yuv. 
		wPixel32 = ConvertYuvToBitmap(wY1, wU, wV);
		aOutBuf[wOutPos++] = (wPixel32 & KWebcameraBitMask);
		aOutBuf[wOutPos++] = (wPixel32 & KWebcameraBitMask8) >> 8;
		aOutBuf[wOutPos++] = (wPixel32 & KWebcameraBitMask16) >> 16;
		}

	return KErrNone;
	}

/**
Convert yuv to rgb.

@param	aY	[in]					Signal of brightness.
		aU	[in]					Signal of colour difference(Cb)
		aV	[in]					Signal of colour difference(Cr).
@return	pixel.
*/
TInt DWebCameraConvertYuv::ConvertYuvToBitmap(TInt aY, TInt aU, TInt aV)
	{
	TUint wPixel32 = 0;								//Declaration to use still image of wPixel32 format.
	TUint8* wPixel = static_cast<TUint8*>&wPixel32;	//Cast still image of wPixel32 format to 8bit to convert. 
	TInt wR = 0;									//Declaration to store still image after conversion.
	TInt wG = 0;
	TInt wB = 0;

	//Convert yuv to rgb.
	wR = aY + ((1370705 * (aV - 128)) / 1000000);
	wG = aY - ((698001 * (aV - 128)) / 1000000) - ((337633 * (aU - 128)) / 1000000);
	wB = aY + ((1732446 * (aU - 128)) / 1000000);

	//Define the range of RGB(0~255).
	if (!(0 <= wR && wR <= 255))
		{
		(wR > 255) ? (wR = 255) : (wR = 0);
		}
	if (!(0 <= wG && wG <= 255))
		{
		(wG > 255) ? (wG = 255) : (wG = 0);
		}
	if (!(0 <= wB && wB <= 255))
		{
		(wB > 255) ? (wB = 255) : (wB = 0);
		}

	//Compress RGB.
	wPixel[0] = (wB * KWebcameraNeutralGrayValue) / KWebcameraColorGradation;
	wPixel[1] = (wG * KWebcameraNeutralGrayValue) / KWebcameraColorGradation;
	wPixel[2] = (wR * KWebcameraNeutralGrayValue) / KWebcameraColorGradation;

	return wPixel32;
	}