📄 rgb2yuv.c
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: rgb2yuv.c,v 1.2.48.1 2004/07/09 02:00:18 hubbe Exp $ * * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved. * * The contents of this file, and the files included with this file, * are subject to the current version of the RealNetworks Public * Source License (the "RPSL") available at * http://www.helixcommunity.org/content/rpsl unless you have licensed * the file under the current version of the RealNetworks Community * Source License (the "RCSL") available at * http://www.helixcommunity.org/content/rcsl, in which case the RCSL * will apply. You may also obtain the license terms directly from * RealNetworks. You may not use this file except in compliance with * the RPSL or, if you have a valid RCSL with RealNetworks applicable * to this file, the RCSL. Please see the applicable RPSL or RCSL for * the rights, obligations and limitations governing use of the * contents of the file. * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL") in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your version of * this file only under the terms of the GPL, and not to allow others * to use your version of this file under the terms of either the RPSL * or RCSL, indicate your decision by deleting the provisions above * and replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient may * use your version of this file under the terms of any one of the * RPSL, the RCSL or the GPL. * * This file is part of the Helix DNA Technology. RealNetworks is the * developer of the Original Code and owns the copyrights in the * portions it created. * * This file, and the files included with this file, is distributed * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET * ENJOYMENT OR NON-INFRINGEMENT. * * Technology Compatibility Kit Test Suite(s) Location: * http://www.helixcommunity.org/content/tck * * Contributor(s): * * ***** END LICENSE BLOCK ***** *//*** #includes: ********************************************/#include "env.h"#include "rgb.h" /* basic RGB-data definitions & macros */#include "yuv.h" /* YUV-to-RGB conversion tables & macros */#include "clip.h" /* macros for clipping & dithering */#include "scale.h" /* scale algorithms */#include "colorlib.h"/*** RGB to YUV color converters: **************************//* * These functions were borrowed from our "old" color conversion library, * and they do not support things like stretching or color adjustments. * Given some more time this module should be completely rewritten. * ***************//*-----------------2/24/2002 7:45AM----------------- * Replaced the old code with macros. * In-place resizing and color adjustments are not added since nobody * really asked for it, and it would have blown the code by factor of 6 (at least). * Yuriy. * --------------------------------------------------*//*-----------------9/19/2002 2:06PM----------------- * Added support for byte-swapped RGB32 (BGR_32) and BGR24 input formats. * To avoid changes (table reordering) in other modules, the definitions * related to these formats are placed here, instead of "rgb.h". * Yuriy. * --------------------------------------------------*//* new RGB format IDs: */#define BGR_32_ID RGB_FORMATS#define BGR24_ID (RGB_FORMATS + 1)/* bytes per pixel: */#define BGR_32_BPP 4#define BGR24_BPP 3/* local pixel representations: */#define BGR_32_PIXEL(a) RGBX_PIXEL(a)#define BGR24_PIXEL(a) RGB24_PIXEL(a)/* load/store/copy pixel macros: */#define BGR_32_LOAD(s,a) RGBX_LOAD(s,a,unsigned int)#define BGR_32_STORE(d,a) RGBX_STORE(d,a,unsigned int)#define BGR_32_COPY(da,sa) RGBX_COPY(da,sa)#define BGR24_LOAD(s,a) a##_r=(s)[0], a##_g=(s)[1], a##_b=(s)[2]#define BGR24_STORE(d,a) (d)[0]=a##_r, (d)[1]=a##_g, (d)[2]=a##_b#define BGR24_COPY(da,sa) da##_r=sa##_r, da##_g=sa##_g, da##_b=sa##_b/* BGR_32 bit fields: 0xBBGGRR00: */#define BGR_32_R_START 8#define BGR_32_R_BITS 8#define BGR_32_G_START 16#define BGR_32_G_BITS 8#define BGR_32_B_START 24#define BGR_32_B_BITS 8/* fields extraction/assignment macros: */#define BGR_32_GET_R(a) RGBX_GET_X(BGR_32,R,a)#define BGR_32_GET_G(a) RGBX_GET_X(BGR_32,G,a)#define BGR_32_GET_B(a) RGBX_GET_X(BGR_32,B,a)#define BGR_32_SET(a,r,g,b) RGBX_SET( BGR_32,a,r,g,b)#define BGR24_GET_R(a) (a##_r)#define BGR24_GET_G(a) (a##_g)#define BGR24_GET_B(a) (a##_b)#define BGR24_SET(a,r,g,b) a##_b=(b), a##_g=(g), a##_r=(r)/***************************************************************//* * YUV 4:2:0 chroma resampling modes: * Input chroma a b * samples: c d * * CRM_11_11: (a+b+c+d) / 4 * CRM_11_00: (a+b) / 2 * CRM_00_11: (c+d) / 2 */#define CHROMA_RESAMPLING_MODES 3/* * 2x2-block load-convert-store macros: *//* CRM_11_11 (both lines) chroma resampling: */#define CONVERT_2x2_CHROMA_11_11(dy1,dy2,du,dv,sf,s1,s2) \ { \ /* local variables: */ \ PIXEL(sf,x); \ register unsigned int r, b, y; \ register unsigned int r4, b4, y4; /* combined quant-s */ \ \ /* process lumas: */ \ LOAD(sf,s1,x); /* [0,0] */ \ r4 = (r = GET_R(sf,x)); \ b4 = (b = GET_B(sf,x)); \ y4 = (y = yrtab [r] + ygtab [GET_G(sf,x)] + ybtab [b]); \ dy1[0] = yytab [y]; \ \ LOAD(sf,s1+BPP(sf),x); /* [0,1] */ \ r4 += (r = GET_R(sf,x)); \ b4 += (b = GET_B(sf,x)); \ y4 += (y = yrtab [r] + ygtab [GET_G(sf,x)] + ybtab [b]); \ dy1[1] = yytab [y]; \ \ LOAD(sf,s2,x); /* [1,0] */ \ r4 += (r = GET_R(sf,x)); \ b4 += (b = GET_B(sf,x)); \ y4 += (y = yrtab [r] + ygtab [GET_G(sf,x)] + ybtab [b]); \ dy2[0] = yytab [y]; \ \ LOAD(sf,s2+BPP(sf),x); /* [1,1] */ \ r4 += (r = GET_R(sf,x)); \ b4 += (b = GET_B(sf,x)); \ y4 += (y = yrtab [r] + ygtab [GET_G(sf,x)] + ybtab [b]); \ dy2[1] = yytab [y]; \ \ /* average chromas: */ \ dv[0] = vrytab [VMIN+(r4-y4)/4]; \ du[0] = ubytab [UMIN+(b4-y4)/4]; \ }/* CRM_11_00 (upper line) chroma resampling: */#define CONVERT_2x2_CHROMA_11_00(dy1,dy2,du,dv,sf,s1,s2) \ { \ /* local variables: */ \ PIXEL(sf,x); \ register unsigned int r, b, y; \ register unsigned int r2, b2, y2; /* 1st line combined */ \ \ /* process lumas: */ \ LOAD(sf,s1,x); /* [0,0] */ \ r2 = (r = GET_R(sf,x)); \ b2 = (b = GET_B(sf,x)); \ y2 = (y = yrtab [r] + ygtab [GET_G(sf,x)] + ybtab [b]); \ dy1[0] = yytab [y]; \ \ LOAD(sf,s1+BPP(sf),x); /* [0,1] */ \ r2 += (r = GET_R(sf,x)); \ b2 += (b = GET_B(sf,x)); \ y2 += (y = yrtab [r] + ygtab [GET_G(sf,x)] + ybtab [b]); \ dy1[1] = yytab [y]; \ \ LOAD(sf,s2,x); /* [1,0] */ \ r = GET_R(sf,x); \ b = GET_B(sf,x); \ y = yrtab [r] + ygtab [GET_G(sf,x)] + ybtab [b]; \ dy2[0] = yytab [y]; \ \ LOAD(sf,s2+BPP(sf),x); /* [1,1] */ \ r = GET_R(sf,x); \ b = GET_B(sf,x); \ y = yrtab [r] + ygtab [GET_G(sf,x)] + ybtab [b]; \ dy2[1] = yytab [y]; \ \ /* average chromas: */ \ dv[0] = vrytab [VMIN+(r2-y2)/2]; \ du[0] = ubytab [UMIN+(b2-y2)/2]; \ }/* CRM_00_11 (lower line) chroma resampling: */#define CONVERT_2x2_CHROMA_00_11(dy1,dy2,du,dv,sf,s1,s2) \ { \ /* local variables: */ \ PIXEL(sf,x); \ register unsigned int r, b, y; \ register unsigned int r2, b2, y2; /* 2nd line combined */ \ \ /* process lumas: */ \ LOAD(sf,s1,x); /* [0,0] */ \ r = GET_R(sf,x); \ b = GET_B(sf,x); \ y = yrtab [r] + ygtab [GET_G(sf,x)] + ybtab [b]; \ dy1[0] = yytab [y]; \ \ LOAD(sf,s1+BPP(sf),x); /* [0,1] */ \ r = GET_R(sf,x); \ b = GET_B(sf,x); \ y = yrtab [r] + ygtab [GET_G(sf,x)] + ybtab [b]; \ dy1[1] = yytab [y]; \ \ LOAD(sf,s2,x); /* [1,0] */ \ r2 = (r = GET_R(sf,x)); \ b2 = (b = GET_B(sf,x)); \ y2 = (y = yrtab [r] + ygtab [GET_G(sf,x)] + ybtab [b]); \ dy2[0] = yytab [y]; \ \ LOAD(sf,s2+BPP(sf),x); /* [1,1] */ \ r2 += (r = GET_R(sf,x)); \ b2 += (b = GET_B(sf,x)); \ y2 += (y = yrtab [r] + ygtab [GET_G(sf,x)] + ybtab [b]); \ dy2[1] = yytab [y]; \ \ /* average chromas: */ \ dv[0] = vrytab [VMIN+(r2-y2)/2]; \ du[0] = ubytab [UMIN+(b2-y2)/2]; \ }/* * Generic 2x2 block converter: */#define CONVERT_2x2(dy1,dy2,du,dv,sf,s1,s2,crm) \ CONVERT_2x2_##crm(dy1,dy2,du,dv,sf,s1,s2)/* * Generic RGBtoYUV double-row converter: * * YUV-image pointers are assumed to be 2x2-block aligned, as well as * n -- the number of pixels to be converter, is assumed to be multiple of 2. */#define DBLROW(dy1,dy2,du,dv,sf,s1,s2,n,crm) \ { \ register int n2 = n/2; \ /* convert 2x2 blocks: */ \ while (n2) { \ CONVERT_2x2(dy1,dy2,du,dv,sf,s1,s2,crm); \ dy1 += 2; dy2 += 2; du ++; dv ++; \ s1 += BPP(sf) * 2; s2 += BPP(sf) * 2; \ n2 --; \ } \ }/* * Function names: */#define FN(df,sf) sf##to##df#define DBLROW_FN(df,sf,crm) sf##to##df##_DBLROW_##crm/* * Function replication macros: * (dblrow converters) */#define DBLROW_FUNC(df,sf,crm) \ static void DBLROW_FN(df,sf,crm) ( \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -