📄 color.c
字号:
/* * GPAC - Multimedia Framework C SDK * * Copyright (c) Jean Le Feuvre 2000-2005 * All rights reserved * * This file is part of GPAC / common tools sub-project * * GPAC is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * GPAC is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; see the file COPYING. If not, write to * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * */#include <gpac/user.h>#include <gpac/constants.h>#include <gpac/color.h>/* original YUV table code from XviD colorspace module *//***************************************************************************** * * XVID MPEG-4 VIDEO CODEC * - colorspace conversion module - * * Copyright(C) 2002 Peter Ross <pross@xvid.org> * 2002 Michael Militzer <isibaar@xvid.org> * * follows the usual GPL license terms ****************************************************************************/#define col_clip(a) MAX(0, MIN(255, a))static s32 RGB_Y[256];static s32 B_U[256];static s32 G_U[256];static s32 G_V[256];static s32 R_V[256];#define SCALEBITS_OUT 13#define FIX_OUT(x) ((unsigned short) ((x) * (1L<<SCALEBITS_OUT) + 0.5))static s32 is_init = 0;/**/static void yuv2rgb_init(void) { s32 i; if (!is_init) { is_init = 1; for(i = 0; i < 256; i++) { RGB_Y[i] = FIX_OUT(1.164) * (i - 16); B_U[i] = FIX_OUT(2.018) * (i - 128); G_U[i] = FIX_OUT(0.391) * (i - 128); G_V[i] = FIX_OUT(0.813) * (i - 128); R_V[i] = FIX_OUT(1.596) * (i - 128); } }}static void gf_yuv_load_lines(unsigned char *dst, s32 dststride, unsigned char *y_src, unsigned char *u_src, unsigned char * v_src, s32 y_stride, s32 uv_stride, s32 width){ u32 x, hw; unsigned char *dst2 = (unsigned char *) dst + dststride; unsigned char *y_src2 = (unsigned char *) y_src + y_stride; hw = width / 2; for (x = 0; x < hw; x++) { s32 u, v; s32 b_u, g_uv, r_v, rgb_y; u = u_src[x]; v = v_src[x]; b_u = B_U[u]; g_uv = G_U[u] + G_V[v]; r_v = R_V[v]; rgb_y = RGB_Y[*y_src]; dst[0] = col_clip( (rgb_y + r_v) >> SCALEBITS_OUT); dst[1] = col_clip( (rgb_y - g_uv) >> SCALEBITS_OUT); dst[2] = col_clip( (rgb_y + b_u) >> SCALEBITS_OUT); dst[3] = 0xFF; y_src++; rgb_y = RGB_Y[*y_src]; dst[4] = col_clip( (rgb_y + r_v) >> SCALEBITS_OUT); dst[5] = col_clip( (rgb_y - g_uv) >> SCALEBITS_OUT); dst[6] = col_clip( (rgb_y + b_u) >> SCALEBITS_OUT); dst[7] = 0xFF; y_src++; rgb_y = RGB_Y[*y_src2]; dst2[0] = col_clip( (rgb_y + r_v) >> SCALEBITS_OUT); dst2[1] = col_clip( (rgb_y - g_uv) >> SCALEBITS_OUT); dst2[2] = col_clip( (rgb_y + b_u) >> SCALEBITS_OUT); dst2[3] = 0xFF; y_src2++; rgb_y = RGB_Y[*y_src2]; dst2[4] = col_clip( (rgb_y + r_v) >> SCALEBITS_OUT); dst2[5] = col_clip( (rgb_y - g_uv) >> SCALEBITS_OUT); dst2[6] = col_clip( (rgb_y + b_u) >> SCALEBITS_OUT); dst2[7] = 0xFF; y_src2++; dst += 8; dst2 += 8; }}static void gf_yuva_load_lines(unsigned char *dst, s32 dststride, unsigned char *y_src, unsigned char *v_src, unsigned char * u_src, unsigned char *a_src, s32 y_stride, s32 uv_stride, s32 width){ u32 x, hw; unsigned char *dst2 = dst + dststride; unsigned char *y_src2 = y_src + y_stride; unsigned char *a_src2 = a_src + y_stride; yuv2rgb_init(); hw = width / 2; for (x = 0; x < hw; x++) { s32 u, v; s32 b_u, g_uv, r_v, rgb_y; u = u_src[x]; v = v_src[x]; b_u = B_U[u]; g_uv = G_U[u] + G_V[v]; r_v = R_V[v]; rgb_y = RGB_Y[*y_src]; dst[0] = col_clip ( (rgb_y + r_v) >> SCALEBITS_OUT ); dst[1] = col_clip ( (rgb_y - g_uv) >> SCALEBITS_OUT ); dst[2] = col_clip ( (rgb_y + b_u) >> SCALEBITS_OUT ); dst[3] = *a_src; y_src++; a_src++; rgb_y = RGB_Y[*y_src]; dst[4] = col_clip ( (rgb_y + r_v) >> SCALEBITS_OUT ); dst[5] = col_clip ( (rgb_y - g_uv) >> SCALEBITS_OUT ); dst[6] = col_clip ( (rgb_y + b_u) >> SCALEBITS_OUT ); dst[7] = *a_src; y_src++; a_src++; rgb_y = RGB_Y[*y_src2]; dst2[0] = col_clip ( (rgb_y + r_v) >> SCALEBITS_OUT ); dst2[1] = col_clip ( (rgb_y - g_uv) >> SCALEBITS_OUT ); dst2[2] = col_clip ( (rgb_y + b_u) >> SCALEBITS_OUT ); dst2[3] = *a_src2; y_src2++; a_src2++; rgb_y = RGB_Y[*y_src2]; dst2[4] = col_clip ( (rgb_y + r_v) >> SCALEBITS_OUT ); dst2[5] = col_clip ( (rgb_y - g_uv) >> SCALEBITS_OUT ); dst2[6] = col_clip ( (rgb_y + b_u) >> SCALEBITS_OUT ); dst2[7] = *a_src2; y_src2++; a_src2++; dst += 8; dst2 += 8; }}static s32 mul255(s32 a, s32 b){ return ((a+1) * b) >> 8;}typedef void (*copy_row_proto)(u8 *src, u32 src_w, u8 *_dst, u32 dst_w, s32 h_inc, s32 x_pitch, u8 alpha);typedef void (*load_line_proto)(u8 *src_bits, u32 x_offset, u32 y_offset, u32 y_pitch, u32 width, u8 *dst_bits);static void copy_row_rgb_555(u8 *src, u32 src_w, u8 *_dst, u32 dst_w, s32 h_inc, s32 x_pitch, u8 alpha){ s32 pos; u16 *dst = (u16 *)_dst; u8 a, r, g, b; pos = 0x10000; while (dst_w) { while ( pos >= 0x10000L ) { r = *src++; g = *src++; b = *src++; a = *src++; pos -= 0x10000L; } if (a) *dst = GF_COL_555(r, g, b); dst += x_pitch; pos += h_inc; dst_w--; }}static void copy_row_rgb_565(u8 *src, u32 src_w, u8 *_dst, u32 dst_w, s32 h_inc, s32 x_pitch, u8 alpha){ s32 pos; u16 *dst = (u16 *)_dst; u8 a, r, g, b; pos = 0x10000; while (dst_w) { while ( pos >= 0x10000L ) { r = *src++; g = *src++; b = *src++; a = *src++; pos -= 0x10000L; } if (a) *dst = GF_COL_565(r, g, b); dst += x_pitch; pos += h_inc; dst_w--; }}static void copy_row_rgb_24(u8 *src, u32 src_w, u8 *dst, u32 dst_w, s32 h_inc, s32 x_pitch, u8 alpha){ s32 pos; u8 a, r, g, b; pos = 0x10000; while (dst_w) { while ( pos >= 0x10000L ) { r = *src++; g = *src++; b = *src++; a = *src++; pos -= 0x10000L; } if (a) { dst[0] = r; dst[1] = g; dst[2] = b; } dst += x_pitch; pos += h_inc; dst_w--; }}static void copy_row_bgr_24(u8 *src, u32 src_w, u8 *dst, u32 dst_w, s32 h_inc, s32 x_pitch, u8 alpha){ s32 pos; u8 a, r, g, b; pos = 0x10000; while (dst_w) { while ( pos >= 0x10000L ) { r = *src++; g = *src++; b = *src++; a = *src++; pos -= 0x10000L; } if (a) { dst[0] = b; dst[1] = g; dst[2] = r; } dst += x_pitch; pos += h_inc; dst_w--; }}static void copy_row_rgb_32(u8 *src, u32 src_w, u8 *_dst, u32 dst_w, s32 h_inc, s32 x_pitch, u8 alpha){ u8 a, r, g, b; s32 pos = 0x10000L; u32 *dst = (u32 *)_dst; while (dst_w) { while ( pos >= 0x10000L ) { r = *src++; g = *src++; b = *src++; a = *src++; pos -= 0x10000L; } if (a) *dst = GF_COL_ARGB(a, r, g, b); dst += x_pitch; pos += h_inc; dst_w--; }}static void copy_row_bgr_32(u8 *src, u32 src_w, u8 *_dst, u32 dst_w, s32 h_inc, s32 x_pitch, u8 alpha){ u8 a, r, g, b; s32 pos = 0x10000L; u32 *dst = (u32 *)_dst; while ( dst_w) { while ( pos >= 0x10000L ) { r = *src++; g = *src++; b = *src++; a = *src++; pos -= 0x10000L; } if (a) *dst = GF_COL_ARGB(b, g, r, a); dst+=x_pitch; pos += h_inc; dst_w--; }}static void merge_row_rgb_555(u8 *src, u32 src_w, u8 *_dst, u32 dst_w, s32 h_inc, s32 x_pitch, u8 alpha){ u32 _r, _g, _b, a, r, g, b; s32 pos; u16 col, *dst = (u16 *)_dst; pos = 0x10000; while (dst_w) { while ( pos >= 0x10000L ) { r = *src++; g = *src++; b = *src++; a = *src++; pos -= 0x10000L; a = mul255(a, alpha); } if (a && alpha) { col = *dst; _r = (col >> 7) & 0xf8; _g = (col >> 2) & 0xf8; _b = (col << 3) & 0xf8; _r = mul255(a, r - _r) + _r; _g = mul255(a, g - _g) + _g; _b = mul255(a, b - _b) + _b; *dst = GF_COL_555(_r, _g, _b); } dst += x_pitch; pos += h_inc; dst_w--; }}static void merge_row_rgb_565(u8 *src, u32 src_w, u8 *_dst, u32 dst_w, s32 h_inc, s32 x_pitch, u8 alpha){ u32 _r, _g, _b, a, r, g, b; s32 pos; u16 col, *dst = (u16 *)_dst; pos = 0x10000; while (dst_w) { while ( pos >= 0x10000L ) { r = *src++; g = *src++; b = *src++; a = *src++; pos -= 0x10000L; a = mul255(a, alpha); } if (a) { col = *dst; _r = (col >> 8) & 0xf8; _g = (col >> 3) & 0xfc; _b = (col << 3) & 0xf8; _r = mul255(a, r - _r) + _r; _g = mul255(a, g - _g) + _g; _b = mul255(a, b - _b) + _b; *dst = GF_COL_565(_r, _g, _b); } dst += x_pitch; pos += h_inc; dst_w--; }}static void merge_row_rgb_24(u8 *src, u32 src_w, u8 *dst, u32 dst_w, s32 h_inc, s32 x_pitch, u8 alpha){ u32 _r, _g, _b, a, r, g, b; s32 pos; pos = 0x10000; while (dst_w) { while ( pos >= 0x10000L ) { r = *src++; g = *src++; b = *src++; a = *src++; pos -= 0x10000L; a = mul255(a, alpha); } if (a) { _r = dst[0]; _g = dst[0]; _b = dst[0]; dst[0] = mul255(a, r - _r) + _r; dst[1] = mul255(a, g - _g) + _g; dst[2] = mul255(a, b - _b) + _b; } dst += x_pitch; pos += h_inc; dst_w--; }}static void merge_row_bgr_24(u8 *src, u32 src_w, u8 *dst, u32 dst_w, s32 h_inc, s32 x_pitch, u8 alpha){ u32 _r, _g, _b, a, r, g, b; s32 pos; pos = 0x10000; while (dst_w) { while ( pos >= 0x10000L ) { r = *src++; g = *src++; b = *src++; a = *src++; pos -= 0x10000L; } if (a && alpha) { _r = dst[0]; _g = dst[0]; _b = dst[0]; a = mul255(a, alpha); dst[2] = mul255(a, r - _r) + _r; dst[1] = mul255(a, g - _g) + _g; dst[0] = mul255(a, b - _b) + _b; } dst += x_pitch; pos += h_inc; dst_w--; }}static void merge_row_rgb_32(u8 *src, u32 src_w, u8 *_dst, u32 dst_w, s32 h_inc, s32 x_pitch, u8 alpha){ u32 _r, _g, _b, a, r, g, b, col; s32 pos; u32 *dst = (u32 *)_dst; pos = 0x10000; while (dst_w) { while ( pos >= 0x10000L ) { r = *src++; g = *src++; b = *src++; a = *src++; a = mul255(a, alpha); pos -= 0x10000L; } if (a) { col = *dst; _r = GF_COL_R(col); _g = GF_COL_G(col); _b = GF_COL_B(col); _r = mul255(a, r - _r) + _r; _g = mul255(a, g - _g) + _g; _b = mul255(a, b - _b) + _b; *dst = GF_COL_ARGB(0xFF, _r, _g, _b); } dst += x_pitch; pos += h_inc; dst_w--; }}static void merge_row_argb_32(u8 *src, u32 src_w, u8 *_dst, u32 dst_w, s32 h_inc, s32 x_pitch, u8 alpha){ u32 _a, _r, _g, _b, a, r, g, b, col; s32 pos; u32 *dst = (u32 *)_dst; pos = 0x10000; while (dst_w) { while ( pos >= 0x10000L ) { r = *src++; g = *src++; b = *src++; a = *src++; pos -= 0x10000L; a = mul255(a, alpha); } if (a) { col = *dst; _r = GF_COL_R(col); _g = GF_COL_G(col); _b = GF_COL_B(col); if (GF_COL_A(col)) { _a = mul255(a, a) + mul255(0xFF-a, 0xFF); _r = mul255(a, r - _r) + _r; _g = mul255(a, g - _g) + _g; _b = mul255(a, b - _b) + _b; *dst = GF_COL_ARGB(_a, _r, _g, _b); } else { *dst = GF_COL_ARGB(a, r, g, b); } } dst += x_pitch; pos += h_inc;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -