baseport/syborg/webcamera/webcamer_convert.cpp
changeset 124 606eafc6d6a8
equal deleted inserted replaced
52:0dfaca43d90e 124:606eafc6d6a8
       
     1 /*
       
     2 * Copyright (c) 2010 ISB.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "Symbian Foundation License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * ISB - Initial contribution
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef WEBCAMERACONVERT_H__
       
    20 #include "webcamer_convert.h"
       
    21 #endif
       
    22 
       
    23 #define DP(format...) Kern::Printf(format)
       
    24 
       
    25 /**
       
    26 Constructor.
       
    27 */
       
    28 DWebCameraConvert::DWebCameraConvert(TWebcameraUVC* aUvcFormat)
       
    29 	{
       
    30 	TUint32 wGuid = aUvcFormat->iUVCDescriptor.iFormatDescriptor.iGuid.iData1;
       
    31 	switch (wGuid)
       
    32 		{
       
    33 	case KWebcameraYuy2:
       
    34 		//Create object of DWebCameraConvertYuv.
       
    35 		iConvertObject = new(ELeave) DWebCameraConvertYuv(aUvcFormat);
       
    36 		break;
       
    37 	default:
       
    38 		break;
       
    39 		}
       
    40 	}
       
    41 
       
    42 /**
       
    43 Destructor.
       
    44 */
       
    45 DWebCameraConvert::~DWebCameraConvert()
       
    46 	{
       
    47 	delete iConvertObject;
       
    48 	}
       
    49 
       
    50 TInt DWebCameraConvert::ConvertData(TUint8* aInBuf, TUint8* aOutBuf)
       
    51 	{
       
    52 	return iConvertObject->ConvertData(aInBuf, aOutBuf);
       
    53 	}
       
    54 
       
    55 /**
       
    56 Constructor.
       
    57 */
       
    58 DWebCameraConvertBase::DWebCameraConvertBase(TWebcameraUVC* aUvcFormat)
       
    59 					:iUvcFormat(aUvcFormat)
       
    60 	{
       
    61 	}
       
    62 
       
    63 /** 
       
    64 Destructor.
       
    65 */
       
    66 DWebCameraConvertBase::~DWebCameraConvertBase()
       
    67 	{
       
    68 	}
       
    69 
       
    70 /**
       
    71 Convert FrameData to BitmapData.
       
    72 
       
    73 @param	aInBuf	[in]					Data to convert.
       
    74 		aOutBuf	[out]					BitmapData after conversion.
       
    75 @return	KErrNotSupported.				Implement in sub-class, return unsupport in base-class.
       
    76 */
       
    77 TInt DWebCameraConvertBase::ConvertData(TUint8* /*aInBuf*/, TUint8* /*aOutBuf*/)
       
    78 	{
       
    79 	return KErrNotSupported;
       
    80 	}
       
    81 
       
    82 /**
       
    83 Constructor.
       
    84 */
       
    85 DWebCameraConvertYuv::DWebCameraConvertYuv(TWebcameraUVC* aUvcFormat)
       
    86 					:DWebCameraConvertBase(aUvcFormat)
       
    87 	{
       
    88 	}
       
    89 
       
    90 /**
       
    91 Destructor.
       
    92 */
       
    93 DWebCameraConvertYuv::~DWebCameraConvertYuv()
       
    94 	{
       
    95 	}
       
    96 
       
    97 /**
       
    98 Convert FrameData to BitmapData(yuv->Bitmap).
       
    99 
       
   100 @param	aInBuf	[in]					Data to convert.
       
   101 		aOutBuf	[out]					BitmapData after conversion.
       
   102 @return	KErrNone.
       
   103 */
       
   104 TInt DWebCameraConvertYuv::ConvertData(TUint8* aInBuf, TUint8* aOutBuf)
       
   105 	{
       
   106 	TUint wInPos = 0;							//Declaration to get FrameData before conversion.
       
   107 	TUint wOutPos = 0;							//Declaration to store BitmapData after conversion.
       
   108 	TUint wPixel32 = 0;							//Declaration to store data(1pixel32bit) after conversion from yuv to rgb.
       
   109 	TInt wY0, wU, wY1, wV;						//Declaration to store signal that get from rgb.
       
   110 	TUint16 wWidth, wHeight;					//Declaration to store width and height from argument aVideoData.
       
   111 
       
   112 	wWidth = iUvcFormat->iUVCDescriptor.iFrameDescriptor.iWidth;		//Store width information from aVideoData.
       
   113 	wHeight = iUvcFormat->iUVCDescriptor.iFrameDescriptor.iHeight;		//Store height information from aVideoData.
       
   114 
       
   115 	TUint wImageSize = wWidth * wHeight * 2;
       
   116 
       
   117 	//Get width and heigth data.
       
   118 	for (wInPos = 0; wInPos < wImageSize; wInPos += 4)
       
   119 		{
       
   120 		//Y16-bit to suitable location of YUYV.
       
   121 		wY0 = (static_cast<TUint>(aInBuf[wInPos + 0]) & KWebcameraBitMask);
       
   122 		wU  = (static_cast<TUint>(aInBuf[wInPos + 1]) & KWebcameraBitMask);
       
   123 		wY1 = (static_cast<TUint>(aInBuf[wInPos + 2]) & KWebcameraBitMask);
       
   124 		wV  = (static_cast<TUint>(aInBuf[wInPos + 3]) & KWebcameraBitMask);
       
   125 
       
   126 		//Store RGB by wPixel32 format, that was converted from yuv.
       
   127 		wPixel32 = ConvertYuvToBitmap(wY0, wU, wV);
       
   128 		//Calculate element of RGB from wPixel32.
       
   129 		aOutBuf[wOutPos++] = (wPixel32 & KWebcameraBitMask);
       
   130 		aOutBuf[wOutPos++] = (wPixel32 & KWebcameraBitMask8) >> 8;
       
   131 		aOutBuf[wOutPos++] = (wPixel32 & KWebcameraBitMask16) >> 16;
       
   132 
       
   133 		//Store RGB by wPixel32 format, that was converetd from yuv. 
       
   134 		wPixel32 = ConvertYuvToBitmap(wY1, wU, wV);
       
   135 		aOutBuf[wOutPos++] = (wPixel32 & KWebcameraBitMask);
       
   136 		aOutBuf[wOutPos++] = (wPixel32 & KWebcameraBitMask8) >> 8;
       
   137 		aOutBuf[wOutPos++] = (wPixel32 & KWebcameraBitMask16) >> 16;
       
   138 		}
       
   139 
       
   140 	return KErrNone;
       
   141 	}
       
   142 
       
   143 /**
       
   144 Convert yuv to rgb.
       
   145 
       
   146 @param	aY	[in]					Signal of brightness.
       
   147 		aU	[in]					Signal of colour difference(Cb)
       
   148 		aV	[in]					Signal of colour difference(Cr).
       
   149 @return	pixel.
       
   150 */
       
   151 TInt DWebCameraConvertYuv::ConvertYuvToBitmap(TInt aY, TInt aU, TInt aV)
       
   152 	{
       
   153 	TUint wPixel32 = 0;								//Declaration to use still image of wPixel32 format.
       
   154 	TUint8* wPixel = static_cast<TUint8*>&wPixel32;	//Cast still image of wPixel32 format to 8bit to convert. 
       
   155 	TInt wR = 0;									//Declaration to store still image after conversion.
       
   156 	TInt wG = 0;
       
   157 	TInt wB = 0;
       
   158 
       
   159 	//Convert yuv to rgb.
       
   160 	wR = aY + ((1370705 * (aV - 128)) / 1000000);
       
   161 	wG = aY - ((698001 * (aV - 128)) / 1000000) - ((337633 * (aU - 128)) / 1000000);
       
   162 	wB = aY + ((1732446 * (aU - 128)) / 1000000);
       
   163 
       
   164 	//Define the range of RGB(0~255).
       
   165 	if (!(0 <= wR && wR <= 255))
       
   166 		{
       
   167 		(wR > 255) ? (wR = 255) : (wR = 0);
       
   168 		}
       
   169 	if (!(0 <= wG && wG <= 255))
       
   170 		{
       
   171 		(wG > 255) ? (wG = 255) : (wG = 0);
       
   172 		}
       
   173 	if (!(0 <= wB && wB <= 255))
       
   174 		{
       
   175 		(wB > 255) ? (wB = 255) : (wB = 0);
       
   176 		}
       
   177 
       
   178 	//Compress RGB.
       
   179 	wPixel[0] = (wB * KWebcameraNeutralGrayValue) / KWebcameraColorGradation;
       
   180 	wPixel[1] = (wG * KWebcameraNeutralGrayValue) / KWebcameraColorGradation;
       
   181 	wPixel[2] = (wR * KWebcameraNeutralGrayValue) / KWebcameraColorGradation;
       
   182 
       
   183 	return wPixel32;
       
   184 	}