[4] | 1 | /******************************************************************************
|
---|
| 2 | * SAGE - Scalable Adaptive Graphics Environment
|
---|
| 3 | *
|
---|
| 4 | * Copyright (C) 2004 Electronic Visualization Laboratory,
|
---|
| 5 | * University of Illinois at Chicago
|
---|
| 6 | *
|
---|
| 7 | * All rights reserved.
|
---|
| 8 | *
|
---|
| 9 | * Redistribution and use in source and binary forms, with or without
|
---|
| 10 | * modification, are permitted provided that the following conditions are met:
|
---|
| 11 | *
|
---|
| 12 | * * Redistributions of source code must retain the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer.
|
---|
| 14 | * * Redistributions in binary form must reproduce the above
|
---|
| 15 | * copyright notice, this list of conditions and the following disclaimer
|
---|
| 16 | * in the documentation and/or other materials provided with the distribution.
|
---|
| 17 | * * Neither the name of the University of Illinois at Chicago nor
|
---|
| 18 | * the names of its contributors may be used to endorse or promote
|
---|
| 19 | * products derived from this software without specific prior written permission.
|
---|
| 20 | *
|
---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
---|
| 25 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
| 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
| 27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
| 28 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
| 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
| 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
| 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 32 | *
|
---|
| 33 | * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/
|
---|
| 34 | *
|
---|
| 35 | *****************************************************************************/
|
---|
| 36 |
|
---|
| 37 | #include "stdafx.h"
|
---|
| 38 | #include "YUV.h"
|
---|
| 39 | #include <windows.h>
|
---|
| 40 |
|
---|
| 41 | #define MYUCHAR(_flt_) ((_flt_>0 && _flt_<256)?_flt_:((_flt_<0)?0:255))
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | /*
|
---|
| 45 | -. YUV422ToRGB24() added by shoong
|
---|
| 46 | -. pbYUV : YUV array (Y0UY1V Y2UY3V ...)
|
---|
| 47 | -. pbRGB : RGB array (RGB º¯È¯µÈ °á°ú°ª)
|
---|
| 48 | -. nFileLen : YUV file length
|
---|
| 49 | */
|
---|
| 50 | void CYUVtoRGB24::YUV422YUY2_ToRGB24(BYTE *pbYUV, BYTE *pbRGB, int nFileLen)
|
---|
| 51 | {
|
---|
| 52 | float B_Cb128,R_Cr128,G_CrCb128;
|
---|
| 53 | float Y0,U,Y1,V;
|
---|
| 54 | float R,G,B;
|
---|
| 55 |
|
---|
| 56 | int j=0;
|
---|
| 57 |
|
---|
| 58 | for (int i = 0; i < m_yuv_len; )
|
---|
| 59 | {
|
---|
| 60 | Y0 = 1.164f * ((float)pbYUV[i++]-16.0f);
|
---|
| 61 | U = (float)pbYUV[i++]-128.0f;
|
---|
| 62 | Y1 = 1.164f * ((float)pbYUV[i++]-16.0f);
|
---|
| 63 | V = (float)pbYUV[i++]-128.0f;
|
---|
| 64 |
|
---|
| 65 | R_Cr128 = 1.596f*V;
|
---|
| 66 | G_CrCb128 = -0.813f*V - 0.391f*U;
|
---|
| 67 | B_Cb128 = 2.018f*U;
|
---|
| 68 |
|
---|
| 69 | R= Y0 + R_Cr128;
|
---|
| 70 | G = Y0 + G_CrCb128;
|
---|
| 71 | B = Y0 + B_Cb128;
|
---|
| 72 |
|
---|
| 73 | pbRGB[j++] = (unsigned char)MYUCHAR(B);
|
---|
| 74 | pbRGB[j++] = (unsigned char)MYUCHAR(G);
|
---|
| 75 | pbRGB[j++] = (unsigned char)MYUCHAR(R);
|
---|
| 76 |
|
---|
| 77 | R= Y1 + R_Cr128;
|
---|
| 78 | G = Y1 + G_CrCb128;
|
---|
| 79 | B = Y1 + B_Cb128;
|
---|
| 80 |
|
---|
| 81 | pbRGB[j++] = (unsigned char)MYUCHAR(B);
|
---|
| 82 | pbRGB[j++] = (unsigned char)MYUCHAR(G);
|
---|
| 83 | pbRGB[j++] = (unsigned char)MYUCHAR(R);
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 | void CYUVtoRGB24::YUV422UYVY_ToRGB24(BYTE *pbYUV, BYTE *pbRGB, int width, int height, int Stride)
|
---|
| 89 | {
|
---|
| 90 | float B_Cb128,R_Cr128,G_CrCb128;
|
---|
| 91 | float Y0,U,Y1,V;
|
---|
| 92 | float R,G,B;
|
---|
| 93 |
|
---|
| 94 | int j=0;
|
---|
| 95 |
|
---|
| 96 | for (DWORD y = 0; y < dwHeight; y++)
|
---|
| 97 | {
|
---|
| 98 | WORD *pwTarget = (WORD*)pbTarget;
|
---|
| 99 | WORD *pwSource = (WORD*)pbSource;
|
---|
| 100 |
|
---|
| 101 | for (DWORD x = 0; x < dwWidth; x++)
|
---|
| 102 | {
|
---|
| 103 |
|
---|
| 104 | // Each WORD is a 'UY' or 'VY' block.
|
---|
| 105 | // Set the low byte (chroma) to 0x80 and leave the high byte (luma)
|
---|
| 106 |
|
---|
| 107 | WORD pixel = pwSource[x] & 0xFF00;
|
---|
| 108 | pixel |= 0x0080;
|
---|
| 109 | pwTarget[x] = pixel;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | // Advance the stride on both buffers.
|
---|
| 113 |
|
---|
| 114 | pbTarget += lStrideOut;
|
---|
| 115 | pbSource += lStrideIn;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | for (int i = 0; i < m_yuv_len; )
|
---|
| 120 | {
|
---|
| 121 | Y0 = 1.164f * ((float)pbYUV[i++]-16.0f);
|
---|
| 122 | U = (float)pbYUV[i++]-128.0f;
|
---|
| 123 | Y1 = 1.164f * ((float)pbYUV[i++]-16.0f);
|
---|
| 124 | V = (float)pbYUV[i++]-128.0f;
|
---|
| 125 |
|
---|
| 126 | R_Cr128 = 1.596f*V;
|
---|
| 127 | G_CrCb128 = -0.813f*V - 0.391f*U;
|
---|
| 128 | B_Cb128 = 2.018f*U;
|
---|
| 129 |
|
---|
| 130 | R= Y0 + R_Cr128;
|
---|
| 131 | G = Y0 + G_CrCb128;
|
---|
| 132 | B = Y0 + B_Cb128;
|
---|
| 133 |
|
---|
| 134 | pbRGB[j++] = (unsigned char)MYUCHAR(B);
|
---|
| 135 | pbRGB[j++] = (unsigned char)MYUCHAR(G);
|
---|
| 136 | pbRGB[j++] = (unsigned char)MYUCHAR(R);
|
---|
| 137 |
|
---|
| 138 | R= Y1 + R_Cr128;
|
---|
| 139 | G = Y1 + G_CrCb128;
|
---|
| 140 | B = Y1 + B_Cb128;
|
---|
| 141 |
|
---|
| 142 | pbRGB[j++] = (unsigned char)MYUCHAR(B);
|
---|
| 143 | pbRGB[j++] = (unsigned char)MYUCHAR(G);
|
---|
| 144 | pbRGB[j++] = (unsigned char)MYUCHAR(R);
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 | /*
|
---|
| 150 | -. YUV420ToRGB24() added by shoong
|
---|
| 151 | -. ŸÆ·¡¿Í °°ÀÌ ÀúÀåµÈ YUV420 fileÀ» RGB·Î º¯È¯ (IMC4 ÇüÅÂ)
|
---|
| 152 | +------------------+
|
---|
| 153 | | |
|
---|
| 154 | | Y |
|
---|
| 155 | +------------------+
|
---|
| 156 | | Cb | Cr |
|
---|
| 157 | +--------+---------+
|
---|
| 158 | */
|
---|
| 159 | void CYUVtoRGB24::YUV420ToRGB24(BYTE *pbYUV, BYTE *pbRGB, int nWidth, int nHeight)
|
---|
| 160 | {
|
---|
| 161 | int i, j;
|
---|
| 162 | int dataLeng = nHeight * nWidth;
|
---|
| 163 | BYTE* yData = new BYTE[dataLeng];
|
---|
| 164 | BYTE* uData = new BYTE[dataLeng];
|
---|
| 165 | BYTE* vData = new BYTE[dataLeng];
|
---|
| 166 | int nIndex = dataLeng;
|
---|
| 167 | int offset;
|
---|
| 168 |
|
---|
| 169 | ZeroMemory(yData, dataLeng);
|
---|
| 170 | ZeroMemory(uData, dataLeng);
|
---|
| 171 | ZeroMemory(vData, dataLeng);
|
---|
| 172 |
|
---|
| 173 | // Y-Data ÀÔ·Â
|
---|
| 174 | for(i = 0; i < dataLeng; i++)
|
---|
| 175 | {
|
---|
| 176 | yData[i] = pbYUV[i];
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | offset = dataLeng;
|
---|
| 180 | // U-Data ÀÔ·Â
|
---|
| 181 | for(i = 0; i < nHeight -2; i++)
|
---|
| 182 | {
|
---|
| 183 | for(j = 0; j < nWidth; j++)
|
---|
| 184 | uData[i*nWidth + j] = pbYUV[dataLeng + (int(i/2))*nWidth/2 + (int(j/2))];
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | // V-Data ÀÔ·Â
|
---|
| 188 | for(i = 0; i < nHeight -2; i++)
|
---|
| 189 | {
|
---|
| 190 | for(j = 0; j < nWidth; j++)
|
---|
| 191 | vData[i*nWidth + j] = pbYUV[dataLeng * (int)5/4 + (int(i/2))*nWidth/2 + (int(j/2))];
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | // Convert to RGB
|
---|
| 195 | BYTE r = 0;
|
---|
| 196 | BYTE g = 0;
|
---|
| 197 | BYTE b = 0;
|
---|
| 198 |
|
---|
| 199 | /*
|
---|
| 200 |
|
---|
| 201 | Y=0.3R+0.59G +0.11B
|
---|
| 202 | U=(B-Y)x0.493
|
---|
| 203 | V=(R-Y)x0.877
|
---|
| 204 |
|
---|
| 205 | R=Y+0.956U+0.621V
|
---|
| 206 | G=Y+0.272U+0.647V
|
---|
| 207 | B=Y+1.1061U+1.703V
|
---|
| 208 |
|
---|
| 209 | */
|
---|
| 210 | for(i = 0; i < dataLeng; i++)
|
---|
| 211 | {
|
---|
| 212 |
|
---|
| 213 | // pbRGB[i*3] = max(0, min(yData[i] + 0.956 * uData[i] + 0.621 * vData[i], 255));
|
---|
| 214 | // pbRGB[i*3+1] = max(0, min(yData[i] + 0.272 * uData[i] + 0.647 * vData[i], 255));
|
---|
| 215 | // pbRGB[i*3+2] = max(0, min(yData[i] + 1.1061 * uData[i] + 1.703 * vData[i], 255));
|
---|
| 216 |
|
---|
| 217 | pbRGB[i*3] = CYUVTable::GetB((int)yData[i], (int)uData[i], (int)vData[i]);
|
---|
| 218 | pbRGB[i*3+1] = CYUVTable::GetG((int)yData[i], (int)uData[i], (int)vData[i]);
|
---|
| 219 | pbRGB[i*3+2] = CYUVTable::GetR((int)yData[i], (int)uData[i], (int)vData[i]);
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | delete [] yData;
|
---|
| 223 | delete [] uData;
|
---|
| 224 | delete [] vData;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 |
|
---|
| 228 |
|
---|
| 229 | CYUVtoRGB24::CYUVtoRGB24() {
|
---|
| 230 |
|
---|
| 231 | m_yuv_len = m_rgb_len = m_yuv_type = 0;
|
---|
| 232 | m_rgb_buff = m_yuv_buff = NULL;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | CYUVtoRGB24::~CYUVtoRGB24() {
|
---|
| 236 | free(m_yuv_buff);
|
---|
| 237 | free(m_rgb_buff);
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | // copy image data to yuv_buff
|
---|
| 241 | int CYUVtoRGB24::SetYuvImage(unsigned char* buff, int yuv_len, int width, int height, int type = YUV420) {
|
---|
| 242 |
|
---|
| 243 | if(!buff) return 0;
|
---|
| 244 |
|
---|
| 245 | m_width = width;
|
---|
| 246 | m_height = height;
|
---|
| 247 | m_yuv_len = yuv_len;
|
---|
| 248 | m_rgb_len = m_width * m_height * 3;
|
---|
| 249 |
|
---|
| 250 | if(type == YUV420) m_yuv_type = YUV420;
|
---|
| 251 | else if(type == YUV422) m_yuv_type = YUV422;
|
---|
| 252 | else return 0;
|
---|
| 253 |
|
---|
| 254 | // If previous yuv_image exist, delete and allocate at new
|
---|
| 255 | if(m_yuv_buff) free(m_yuv_buff);
|
---|
| 256 | m_yuv_buff = (unsigned char*)malloc(sizeof(char) * m_yuv_len);
|
---|
| 257 |
|
---|
| 258 | memcpy(m_yuv_buff, buff, m_yuv_len);
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 |
|
---|
| 262 | // Conversion yuv data to RGB32 as defined yuv_type
|
---|
| 263 | int CYUVtoRGB24::YUVtoRGB(void) {
|
---|
| 264 |
|
---|
| 265 | if(!m_yuv_buff) return 0;
|
---|
| 266 |
|
---|
| 267 | switch(m_yuv_type) {
|
---|
| 268 | case YUV420 :
|
---|
| 269 | if(!m_rgb_buff) free(m_rgb_buff);
|
---|
| 270 | m_rgb_buff = (unsigned char*)malloc(m_width * m_height * 3);
|
---|
| 271 |
|
---|
| 272 | YUV420ToRGB24(m_yuv_buff, m_rgb_buff, m_width, m_height);
|
---|
| 273 | break;
|
---|
| 274 | case YUV422 :
|
---|
| 275 | if(!m_rgb_buff) free(m_rgb_buff);
|
---|
| 276 | m_rgb_buff = (unsigned char*)malloc(m_width * m_height * 3);
|
---|
| 277 |
|
---|
| 278 | YUV422ToRGB24(m_yuv_buff, m_rgb_buff, m_yuv_len) ;
|
---|
| 279 | break;
|
---|
| 280 | default :
|
---|
| 281 | return 0;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | return 1;
|
---|
| 285 |
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | // return rgb image data
|
---|
| 289 | unsigned char* CYUVtoRGB24::GetRgbImage(void) {
|
---|
| 290 | return m_rgb_buff;
|
---|
| 291 | }
|
---|