⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 color.c

📁 一个用于智能手机的多媒体库适合S60 WinCE的跨平台开发库
💻 C
📖 第 1 页 / 共 2 页
字号:
		dst_w--;	}}static GFINLINE u8 colmask(s32 a, s32 n){    s32 mask = (1 << n) - 1;    return (u8) (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);}static void load_line_rgb_555(u8 *src_bits, u32 x_offset, u32 y_offset, u32 y_pitch, u32 width, u8 *dst_bits){	u32 i;	src_bits += x_offset*3 + y_offset*y_pitch;	for (i=0; i<width; i++) {		u16 c = *((u16*)src_bits + i);		dst_bits[0] = colmask(c >> (10 - 3), 3);		dst_bits[1] = colmask(c >> (5 - 3), 3);		dst_bits[2] = colmask(c << 3, 3);		dst_bits[3] = 0xFF;		dst_bits+=4;	}}static void load_line_rgb_565(u8 *src_bits, u32 x_offset, u32 y_offset, u32 y_pitch, u32 width, u8 *dst_bits){	u32 i;	src_bits += x_offset*3 + y_offset*y_pitch;	for (i=0; i<width; i++) {		u16 c = *((u16*)src_bits + i);		dst_bits[0] = colmask(c >> (11 - 3), 3);		dst_bits[1] = colmask(c >> (5 - 2), 2);		dst_bits[2] = colmask(c << 3, 3);		dst_bits[3] = 0xFF;		dst_bits+=4;	}}static void load_line_rgb_24(u8 *src_bits, u32 x_offset, u32 y_offset, u32 y_pitch, u32 width, u8 *dst_bits){	u32 i;	src_bits += x_offset*3 + y_offset*y_pitch;	for (i=0; i<width; i++) {		dst_bits[0] = *src_bits++;		dst_bits[1] = *src_bits++;		dst_bits[2] = *src_bits++;		dst_bits[3] = 0xFF;		dst_bits+=4;	}}static void load_line_bgr_24(u8 *src_bits, u32 x_offset, u32 y_offset, u32 y_pitch, u32 width, u8 *dst_bits){	u32 i;	src_bits += x_offset*3 + y_offset*y_pitch;	for (i=0; i<width; i++) {		dst_bits[2] = *src_bits++;		dst_bits[1] = *src_bits++;		dst_bits[0] = *src_bits++;		dst_bits[3] = 0xFF;		dst_bits+=4;	}}static void load_line_rgb_32(u8 *src_bits, u32 x_offset, u32 y_offset, u32 y_pitch, u32 width, u8 *dst_bits){	u32 i;	src_bits += x_offset*4 + y_offset*y_pitch;	for (i=0; i<width; i++) {		dst_bits[2] = *src_bits++;		dst_bits[1] = *src_bits++;		dst_bits[0] = *src_bits++;		dst_bits[3] = *src_bits++;		dst_bits += 4;	}}static void load_line_bgr_32(u8 *src_bits, u32 x_offset, u32 y_offset, u32 y_pitch, u32 width, u8 *dst_bits){	src_bits += x_offset*4 + y_offset*y_pitch;	memcpy(dst_bits, src_bits, sizeof(char)*4*width);}static void load_line_yv12(char *src_bits, u32 x_offset, u32 y_offset, u32 y_pitch, u32 width, u32 height, u8 *dst_bits){	u8 *pY, *pU, *pV;	pY = (u8 *)src_bits;	pU = (u8 *)src_bits + y_pitch*height;	pV = (u8 *)src_bits + 5*y_pitch*height/4;	pY += x_offset + y_offset*y_pitch;	pU += x_offset/2 + y_offset*y_pitch/4;	pV += x_offset/2 + y_offset*y_pitch/4;	gf_yuv_load_lines((unsigned char*)dst_bits, 4*width, pY, pU, pV, y_pitch, y_pitch/2, width);}static void load_line_yuva(char *src_bits, u32 x_offset, u32 y_offset, u32 y_pitch, u32 width, u32 height, u8 *dst_bits){	u8 *pY, *pU, *pV, *pA;	pY = (u8*)src_bits;	pU = (u8*)src_bits + y_pitch*height;	pV = (u8*)src_bits + 5*y_pitch*height/4;	pA = (u8*)src_bits + 3*y_pitch*height/2;	pY += x_offset + y_offset*y_pitch;	pU += x_offset/2 + y_offset*y_pitch/4;	pV += x_offset/2 + y_offset*y_pitch/4;	pA += x_offset + y_offset*y_pitch;	gf_yuva_load_lines(dst_bits, 4*width, pY, pU, pV, pA, y_pitch, y_pitch/2, width);}static void gf_cmx_apply_argb(GF_ColorMatrix *_this, u8 *a_, u8 *r_, u8 *g_, u8 *b_);GF_EXPORTGF_Err gf_stretch_bits(GF_VideoSurface *dst, GF_VideoSurface *src, GF_Window *dst_wnd, GF_Window *src_wnd, s32 dst_x_pitch, u8 alpha, Bool flip, u32 *col_key, GF_ColorMatrix *cmat){	u8 *tmp, *rows;	u8 ka, kr, kg, kb;	s32 src_row;	u32 i, yuv_type = 0;	Bool has_alpha = (alpha!=0xFF) ? 1 : 0;	u32 dst_bpp, dst_w_size;	s32 pos_y, inc_y, inc_x, prev_row, x_off;	u32 src_w, src_h, dst_w, dst_h;	u8 *src_bits = NULL, *dst_bits = NULL, *dst_bits_prev = NULL, *dst_temp_bits = NULL;	copy_row_proto copy_row = NULL;	load_line_proto load_line = NULL;	if (cmat && (cmat->m[15] || cmat->m[16] || cmat->m[17] || (cmat->m[18]!=FIX_ONE) || cmat->m[19] )) has_alpha = 1;	else if (col_key) has_alpha = GF_COL_A(*col_key) ? 1 : 0;	switch (src->pixel_format) {	case GF_PIXEL_RGB_555:		load_line = load_line_rgb_555;		break;	case GF_PIXEL_RGB_565:		load_line = load_line_rgb_565;		break;	case GF_PIXEL_RGB_24:		load_line = load_line_rgb_24;		break;	case GF_PIXEL_BGR_24:		load_line = load_line_bgr_24;		break;	case GF_PIXEL_ARGB:		has_alpha = 1;	case GF_PIXEL_RGB_32:		load_line = load_line_rgb_32;		break;	case GF_PIXEL_RGBA:		has_alpha = 1;	case GF_PIXEL_BGR_32:		load_line = load_line_bgr_32;		break;	case GF_PIXEL_YV12:	case GF_PIXEL_IYUV:	case GF_PIXEL_I420:		yuv2rgb_init();		yuv_type = 1;		break;	case GF_PIXEL_YUVA:		yuv_type = 2;		break;	default:		return GF_NOT_SUPPORTED;	}	/*only RGB output supported*/	switch (dst->pixel_format) {	case GF_PIXEL_RGB_555:		dst_bpp = sizeof(unsigned char)*2;		copy_row = has_alpha ? merge_row_rgb_555 : copy_row_rgb_555;		break;	case GF_PIXEL_RGB_565:		dst_bpp = sizeof(unsigned char)*2;		copy_row = has_alpha ? merge_row_rgb_565 : copy_row_rgb_565;		break;	case GF_PIXEL_RGB_24:		dst_bpp = sizeof(unsigned char)*3;		copy_row = has_alpha ? merge_row_rgb_24 : copy_row_rgb_24;		break;	case GF_PIXEL_BGR_24:		dst_bpp = sizeof(unsigned char)*3;		copy_row = has_alpha ? merge_row_bgr_24 : copy_row_bgr_24;		break;	case GF_PIXEL_RGB_32:		dst_bpp = sizeof(unsigned char)*4;		copy_row = has_alpha ? merge_row_rgb_32: copy_row_rgb_32;		break;	case GF_PIXEL_ARGB:		dst_bpp = sizeof(unsigned char)*4;		copy_row = has_alpha ? merge_row_argb_32: copy_row_rgb_32;		break;	case GF_PIXEL_RGBA:		dst_bpp = sizeof(unsigned char)*4;		copy_row = has_alpha ? merge_row_argb_32: copy_row_bgr_32;		break;	case GF_PIXEL_BGR_32:		dst_bpp = sizeof(unsigned char)*4;		copy_row = has_alpha ? merge_row_rgb_32: copy_row_bgr_32;		break;	default:		return GF_NOT_SUPPORTED;	}	/*x_pitch 0 means linear framebuffer*/		if (!dst_x_pitch) dst_x_pitch = dst_bpp;		src_w = src_wnd ? src_wnd->w : src->width;	src_h = src_wnd ? src_wnd->h : src->height;	dst_w = dst_wnd ? dst_wnd->w : dst->width;	dst_h = dst_wnd ? dst_wnd->h : dst->height;	tmp = (u8 *) malloc(sizeof(u8) * src_w * (yuv_type ? 8 : 4) );	rows = tmp;	src_bits = (u8 *) src->video_buffer;	dst_bits = (u8 *) dst->video_buffer;	pos_y = 0x10000;	inc_y = (src_h << 16) / dst_h;	inc_x = (src_w << 16) / dst_w;	x_off = src_wnd ? src_wnd->x : 0;	src_row = src_wnd ? src_wnd->y : 0;	prev_row = -1;	dst_bits = (u8 *) dst->video_buffer;	if (dst_wnd) dst_bits += ((s32)dst_wnd->x) * dst_x_pitch + ((s32)dst_wnd->y) * dst->pitch;	dst_w_size = dst_bpp*dst_w;	/*small opt here: if we need to fetch data from destination, and if destination is 	hardware memory, we work on a copy of the destination line*/	if (has_alpha && dst->is_hardware_memory)		dst_temp_bits = (u8 *) malloc(sizeof(u8) * dst_bpp * dst_w);	/*for 2 and 4 bytes colors, precompute pitch for u16 and u32 type casting*/	if ((dst_bpp==2) || (dst_bpp==4) ) dst_x_pitch /= (s32) dst_bpp;	ka = kr = kg = kb = 0;	if (col_key) {		ka = GF_COL_A(*col_key);		kr = GF_COL_R(*col_key);		kg = GF_COL_G(*col_key);		kb = GF_COL_B(*col_key);	}	while (dst_h) {		while ( pos_y >= 0x10000L ) {			src_row++;			pos_y -= 0x10000L;		}		/*new row, check if conversion is needed*/		if (prev_row != src_row) {			u32 the_row = src_row - 1;			if (yuv_type) {				if ( (src_row - 1) % 2) {					rows = flip ? tmp : tmp + src_w * 4;				} else {					if (flip) the_row = src->height-2 - the_row;					if (yuv_type==1) {						load_line_yv12(src->video_buffer, x_off, the_row, src->pitch, src_w, src->height, tmp);					} else {						load_line_yuva(src->video_buffer, x_off, the_row, src->pitch, src_w, src->height, tmp);					}					rows = flip ? tmp + src_w * 4 : tmp;											if (cmat) {						for (i=0; i<2*src_w; i++) {							u32 idx = 4*i; 							gf_cmx_apply_argb(cmat, &tmp[idx+3], &tmp[idx], &tmp[idx+1], &tmp[idx+2]);						}					}					if (col_key) {						for (i=0; i<2*src_w; i++) {							u32 idx = 4*i; 							if ( (tmp[idx]==kr) && (tmp[idx+1]==kg) && (tmp[idx+2]==kb)) 								tmp[idx+3] = ka;						}					}				}			} else {				if (flip) the_row = src->height-1 - the_row;				load_line((u8*)src->video_buffer, x_off, the_row, src->pitch, src_w, tmp);				rows = tmp;				if (cmat) {					for (i=0; i<src_w; i++) {						u32 idx = 4*i; 						gf_cmx_apply_argb(cmat, &tmp[idx+3], &tmp[idx], &tmp[idx+1], &tmp[idx+2]);					}				}				if (col_key) {					for (i=0; i<src_w; i++) {						u32 idx = 4*i; 						if ( (tmp[idx]==kr) && (tmp[idx+1]==kg)  && (tmp[idx+2]==kb)) tmp[idx+3] = ka;					}				}			}			if (dst_temp_bits) {				/*load from video memory*/				memcpy(dst_temp_bits, dst_bits, dst_w_size);				/*merge*/				copy_row(rows, src_w, dst_temp_bits, dst_w, inc_x, dst_x_pitch, alpha);				/*copy to video memory*/				memcpy(dst_bits, dst_temp_bits, dst_w_size);			} else {				copy_row(rows, src_w, dst_bits, dst_w, inc_x, dst_x_pitch, alpha);			}		}		/*do NOT use memcpy if the target buffer is not in systems memory*/		else if (has_alpha || dst->is_hardware_memory) {			copy_row(rows, src_w, dst_bits, dst_w, inc_x, dst_x_pitch, alpha);		} else {			memcpy(dst_bits, dst_bits_prev, dst_w_size);		}				pos_y += inc_y;		prev_row = src_row;		dst_bits_prev = dst_bits;		dst_bits += dst->pitch;		dst_h--;	}	if (dst_temp_bits) free(dst_temp_bits);	free(tmp);	return GF_OK;}/*	COLOR MATRIX TOOLS */GF_EXPORTvoid gf_cmx_init(GF_ColorMatrix *_this){	if (!_this) return;	memset(_this->m, 0, sizeof(Fixed)*20);	_this->m[0] = _this->m[6] = _this->m[12] = _this->m[18] = FIX_ONE;	_this->identity = 1;}static void gf_cmx_identity(GF_ColorMatrix *_this){	GF_ColorMatrix mat;	gf_cmx_init(&mat);	_this->identity = memcmp(_this->m, mat.m, sizeof(Fixed)*20) ? 0 : 1;}GF_EXPORTvoid gf_cmx_set_all(GF_ColorMatrix *_this, Fixed *coefs){	if (!_this || !coefs) return;}GF_EXPORTvoid gf_cmx_set(GF_ColorMatrix *_this, 				 Fixed c1, Fixed c2, Fixed c3, Fixed c4, Fixed c5,				 Fixed c6, Fixed c7, Fixed c8, Fixed c9, Fixed c10,				 Fixed c11, Fixed c12, Fixed c13, Fixed c14, Fixed c15,				 Fixed c16, Fixed c17, Fixed c18, Fixed c19, Fixed c20){	if (!_this) return;	_this->m[0] = c1; _this->m[1] = c2; _this->m[2] = c3; _this->m[3] = c4; _this->m[4] = c5;	_this->m[5] = c6; _this->m[6] = c7; _this->m[7] = c8; _this->m[8] = c9; _this->m[9] = c10;	_this->m[10] = c11; _this->m[11] = c12; _this->m[12] = c13; _this->m[13] = c14; _this->m[14] = c15;	_this->m[15] = c16; _this->m[16] = c17; _this->m[17] = c18; _this->m[18] = c19; _this->m[19] = c20;	gf_cmx_identity(_this);}GF_EXPORTvoid gf_cmx_copy(GF_ColorMatrix *_this, GF_ColorMatrix *from){	if (!_this || !from) return;	memcpy(_this->m, from->m, sizeof(Fixed)*20);	gf_cmx_identity(_this);}GF_EXPORTvoid gf_cmx_multiply(GF_ColorMatrix *_this, GF_ColorMatrix *w){	Fixed res[20];	if (!_this || !w || w->identity) return;	if (_this->identity) {		gf_cmx_copy(_this, w);		return;	}	res[0] = gf_mulfix(_this->m[0], w->m[0]) + gf_mulfix(_this->m[1], w->m[5]) + gf_mulfix(_this->m[2], w->m[10]) + gf_mulfix(_this->m[3], w->m[15]);	res[1] = gf_mulfix(_this->m[0], w->m[1]) + gf_mulfix(_this->m[1], w->m[6]) + gf_mulfix(_this->m[2], w->m[11]) + gf_mulfix(_this->m[3], w->m[16]);	res[2] = gf_mulfix(_this->m[0], w->m[2]) + gf_mulfix(_this->m[1], w->m[7]) + gf_mulfix(_this->m[2], w->m[12]) + gf_mulfix(_this->m[3], w->m[17]);	res[3] = gf_mulfix(_this->m[0], w->m[3]) + gf_mulfix(_this->m[1], w->m[8]) + gf_mulfix(_this->m[2], w->m[13]) + gf_mulfix(_this->m[3], w->m[18]);	res[4] = gf_mulfix(_this->m[0], w->m[4]) + gf_mulfix(_this->m[1], w->m[9]) + gf_mulfix(_this->m[2], w->m[14]) + gf_mulfix(_this->m[3], w->m[19]) + _this->m[4];		res[5] = gf_mulfix(_this->m[5], w->m[0]) + gf_mulfix(_this->m[6], w->m[5]) + gf_mulfix(_this->m[7], w->m[10]) + gf_mulfix(_this->m[8], w->m[15]);	res[6] = gf_mulfix(_this->m[5], w->m[1]) + gf_mulfix(_this->m[6], w->m[6]) + gf_mulfix(_this->m[7], w->m[11]) + gf_mulfix(_this->m[8], w->m[16]);	res[7] = gf_mulfix(_this->m[5], w->m[2]) + gf_mulfix(_this->m[6], w->m[7]) + gf_mulfix(_this->m[7], w->m[12]) + gf_mulfix(_this->m[8], w->m[17]);	res[8] = gf_mulfix(_this->m[5], w->m[3]) + gf_mulfix(_this->m[6], w->m[8]) + gf_mulfix(_this->m[7], w->m[13]) + gf_mulfix(_this->m[8], w->m[18]);	res[9] = gf_mulfix(_this->m[5], w->m[4]) + gf_mulfix(_this->m[6], w->m[9]) + gf_mulfix(_this->m[7], w->m[14]) + gf_mulfix(_this->m[8], w->m[19]) + _this->m[9];		res[10] = gf_mulfix(_this->m[10], w->m[0]) + gf_mulfix(_this->m[11], w->m[5]) + gf_mulfix(_this->m[12], w->m[10]) + gf_mulfix(_this->m[13], w->m[15]);	res[11] = gf_mulfix(_this->m[10], w->m[1]) + gf_mulfix(_this->m[11], w->m[6]) + gf_mulfix(_this->m[12], w->m[11]) + gf_mulfix(_this->m[13], w->m[16]);	res[12] = gf_mulfix(_this->m[10], w->m[2]) + gf_mulfix(_this->m[11], w->m[7]) + gf_mulfix(_this->m[12], w->m[12]) + gf_mulfix(_this->m[13], w->m[17]);	res[13] = gf_mulfix(_this->m[10], w->m[3]) + gf_mulfix(_this->m[11], w->m[8]) + gf_mulfix(_this->m[12], w->m[13]) + gf_mulfix(_this->m[13], w->m[18]);	res[14] = gf_mulfix(_this->m[10], w->m[4]) + gf_mulfix(_this->m[11], w->m[9]) + gf_mulfix(_this->m[12], w->m[14]) + gf_mulfix(_this->m[13], w->m[19]) + _this->m[14];		res[15] = gf_mulfix(_this->m[15], w->m[0]) + gf_mulfix(_this->m[16], w->m[5]) + gf_mulfix(_this->m[17], w->m[10]) + gf_mulfix(_this->m[18], w->m[15]);	res[16] = gf_mulfix(_this->m[15], w->m[1]) + gf_mulfix(_this->m[16], w->m[6]) + gf_mulfix(_this->m[17], w->m[11]) + gf_mulfix(_this->m[18], w->m[16]);	res[17] = gf_mulfix(_this->m[15], w->m[2]) + gf_mulfix(_this->m[16], w->m[7]) + gf_mulfix(_this->m[17], w->m[12]) + gf_mulfix(_this->m[18], w->m[17]);	res[18] = gf_mulfix(_this->m[15], w->m[3]) + gf_mulfix(_this->m[16], w->m[8]) + gf_mulfix(_this->m[17], w->m[13]) + gf_mulfix(_this->m[18], w->m[18]);	res[19] = gf_mulfix(_this->m[15], w->m[4]) + gf_mulfix(_this->m[16], w->m[9]) + gf_mulfix(_this->m[17], w->m[14]) + gf_mulfix(_this->m[18], w->m[19]) + _this->m[19];	memcpy(_this->m, res, sizeof(Fixed)*20);	gf_cmx_identity(_this);}#define CLIP_COMP(val)	{ if (val<0) { val=0; } else if (val>FIX_ONE) { val=FIX_ONE;} }static void gf_cmx_apply_argb(GF_ColorMatrix *_this, u8 *a_, u8 *r_, u8 *g_, u8 *b_){	Fixed _a, _r, _g, _b, a, r, g, b;	if (!_this || _this->identity) return;	a = INT2FIX(*a_)/255;	r = INT2FIX(*r_)/255;	g = INT2FIX(*g_)/255;	b = INT2FIX(*b_)/255;	_r = gf_mulfix(r, _this->m[0]) + gf_mulfix(g, _this->m[1]) + gf_mulfix(b, _this->m[2]) + gf_mulfix(a, _this->m[3]) + _this->m[4];	_g = gf_mulfix(r, _this->m[5]) + gf_mulfix(g, _this->m[6]) + gf_mulfix(b, _this->m[7]) + gf_mulfix(a, _this->m[8]) + _this->m[9];	_b = gf_mulfix(r, _this->m[10]) + gf_mulfix(g, _this->m[11]) + gf_mulfix(b, _this->m[12]) + gf_mulfix(a, _this->m[13]) + _this->m[14];	_a = gf_mulfix(r, _this->m[15]) + gf_mulfix(g, _this->m[16]) + gf_mulfix(b, _this->m[17]) + gf_mulfix(a, _this->m[18]) + _this->m[19];	CLIP_COMP(_a);	CLIP_COMP(_r);	CLIP_COMP(_g);	CLIP_COMP(_b);	*a_ = FIX2INT(_a*255);	*r_ = FIX2INT(_r*255);	*g_ = FIX2INT(_g*255);	*b_ = FIX2INT(_b*255);}GF_EXPORTGF_Color gf_cmx_apply(GF_ColorMatrix *_this, GF_Color col){	Fixed _a, _r, _g, _b, a, r, g, b;	if (!_this || _this->identity) return col;	a = INT2FIX(col>>24); a /= 255;	r = INT2FIX((col>>16)&0xFF); r /= 255;	g = INT2FIX((col>>8)&0xFF); g /= 255;	b = INT2FIX((col)&0xFF); b /= 255;	_r = gf_mulfix(r, _this->m[0]) + gf_mulfix(g, _this->m[1]) + gf_mulfix(b, _this->m[2]) + gf_mulfix(a, _this->m[3]) + _this->m[4];	_g = gf_mulfix(r, _this->m[5]) + gf_mulfix(g, _this->m[6]) + gf_mulfix(b, _this->m[7]) + gf_mulfix(a, _this->m[8]) + _this->m[9];	_b = gf_mulfix(r, _this->m[10]) + gf_mulfix(g, _this->m[11]) + gf_mulfix(b, _this->m[12]) + gf_mulfix(a, _this->m[13]) + _this->m[14];	_a = gf_mulfix(r, _this->m[15]) + gf_mulfix(g, _this->m[16]) + gf_mulfix(b, _this->m[17]) + gf_mulfix(a, _this->m[18]) + _this->m[19];	CLIP_COMP(_a);	CLIP_COMP(_r);	CLIP_COMP(_g);	CLIP_COMP(_b);	return GF_COL_ARGB(FIX2INT(_a*255),FIX2INT(_r*255),FIX2INT(_g*255),FIX2INT(_b*255));}GF_EXPORTvoid gf_cmx_apply_fixed(GF_ColorMatrix *_this, Fixed *a, Fixed *r, Fixed *g, Fixed *b){	u32 col = GF_COL_ARGB_FIXED(*a, *r, *g, *b);	col = gf_cmx_apply(_this, col);	*a = INT2FIX(GF_COL_A(col)) / 255;	*r = INT2FIX(GF_COL_R(col)) / 255;	*g = INT2FIX(GF_COL_G(col)) / 255;	*b = INT2FIX(GF_COL_B(col)) / 255;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -