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

📄 image_color.c

📁 一个已经移植到嵌入式平台上的图象处理程序源代码,可以处理gif,bmp,jpg,png
💻 C
📖 第 1 页 / 共 2 页
字号:
			*ptmp++ = value;			*ptmp++ = value;			pcurr = pcurr + 3;		}	}	if (img->rgb != paddr)		free(img->rgb);	img->rgb = p;}//平均灰度化void do_image_GrayAverage(struct image *img, unsigned char *paddr){	int img_width=img->width, img_height=img->height, x_pos=0, y_pos=0;	unsigned char *pimg=img->rgb, *pcurr=NULL, *ptmp=NULL, *p=NULL;	int value=0;	ptmp = (unsigned char *)malloc(img_width*img_height*3);	assert(ptmp);	memset(ptmp, 0x00, img_width*img_height*3);	p = ptmp;	for (y_pos=0; y_pos<img_height; y_pos++)	{		pcurr = pimg + (y_pos*img_width*3);		for (x_pos=0; x_pos<img_width; x_pos++)		{			value = (*(pcurr+0) + *(pcurr+1) + *(pcurr+2))/3;			*ptmp++ = value;			*ptmp++ = value;			*ptmp++ = value;			pcurr = pcurr + 3;		}	}	if (img->rgb != paddr)		free(img->rgb);	img->rgb = p;}//调整饱和度void do_image_Saturation(struct image *img, int step, unsigned char *paddr){	int image_width=0, image_height=0;	int x_pos=0, y_pos=0, idex=0, inum=0, tmp_value=0;	unsigned char *pbuffer=NULL, *ptmp=NULL, *pimage=NULL;	int pGrays[256*3];	short pAlpha[256];	do_image_BrightnessChange(img, 5, paddr);		do_image_BrightnessChange(img, -5, paddr);	pimage=img->rgb;	image_width = img->width;	image_height = img->height;	pbuffer = (unsigned char *)malloc(image_width*image_height*3);	assert(pbuffer);	memset(pbuffer, 0x00, image_width*image_height*3);	memcpy(pbuffer, pimage, image_width*image_height*3);	ptmp = pbuffer;	step = step + 255;	for (idex=0; idex<255; idex++)	{		pAlpha[idex] = (idex*step)>>8;	}	for (idex=0; idex<255; idex++)	{		tmp_value = idex-pAlpha[idex];		pGrays[inum++] = tmp_value;		pGrays[inum++] = tmp_value;		pGrays[inum++] = tmp_value;	}	for (y_pos=0; y_pos<image_height; y_pos++)	{		ptmp = pbuffer + y_pos*image_width*3;		for (x_pos=0; x_pos<image_width; x_pos++)		{			tmp_value = pGrays[*(pimage+0)+*(pimage+1)+*(pimage+2)];			if ((tmp_value+pAlpha[*(pimage+0)])>0)				*(ptmp+0) = min(255, (tmp_value+pAlpha[*(pimage+0)]));			else				*(ptmp+0) = 10;			if ((tmp_value+pAlpha[*(pimage+1)])>0)				*(ptmp+1) = min(255, (tmp_value+pAlpha[*(pimage+1)]));			else				*(ptmp+1) = 10;							if ((tmp_value+pAlpha[*(pimage+2)])>0)				*(ptmp+2) = min(255, (tmp_value+pAlpha[*(pimage+2)]));			else				*(ptmp+2) = 10;						ptmp = ptmp + 3;			pimage = pimage + 3;		}	}	if (img->rgb!=paddr)		free(img->rgb);	img->rgb = pbuffer;}//调整对比度void  do_image_ContrastChange(struct image *img, int step, unsigned char *paddr){	int x_pos=0, y_pos=0, img_width=0, img_height=0;	unsigned char color_value=0;	unsigned char *pimage=NULL, *ptmp=NULL, *pimg=NULL, *pbuf=NULL;	img_width = img->width;	img_height = img->height;	pimage = img->rgb, 	pbuf=(unsigned char *)malloc(img_height*img_width*3);	assert(pbuf);	memset(pbuf, 0x00, img_height*img_width*3);	pimg = pbuf;	for (y_pos=0; y_pos<img_height; y_pos++)	{		ptmp = pimage+(img_width*3)*y_pos;		for (x_pos=0; x_pos<img_width; x_pos++)		{			if (step>=0)			{				color_value = *(ptmp+0);				if (color_value >= 128)					*(pbuf+0) = min(255, color_value+(abs(128-color_value)*step)/128);				else					*(pbuf+0) = max(0, color_value-(abs(128-color_value)*step)/128);									color_value = *(ptmp+1);				if (color_value >= 128)					*(pbuf+1) = min(255, color_value+(abs(128-color_value)*step)/128);				else					*(pbuf+1) = max(0, color_value-(abs(128-color_value)*step)/128);									color_value = *(ptmp+2);				if (color_value >= 128)					*(pbuf+2) = min(255, color_value+(abs(128-color_value)*step)/128);				else					*(pbuf+2) = max(0, color_value-(abs(128-color_value)*step)/128);			}			else			{				color_value = *(ptmp+0);				if (color_value >= 128)					*(pbuf+0) = max(128,color_value+(abs(128-color_value)*step)/128);				else					*(pbuf+0) = min(128,color_value-(abs(128-color_value)*step)/128);									color_value = *(ptmp+1);				if (color_value >= 128)					*(pbuf+1) = max(128,color_value+(abs(128-color_value)*step)/128);				else					*(pbuf+1) = min(128,color_value-(abs(128-color_value)*step)/128);									color_value = *(ptmp+2);				if (color_value >= 128)					*(pbuf+2) = max(128,color_value+(abs(128-color_value)*step)/128);				else					*(pbuf+2) = min(128,color_value-(abs(128-color_value)*step)/128);			}			ptmp = ptmp + 3;			pbuf = pbuf + 3;		}	}	if (img->rgb!=paddr)		free(img->rgb);	img->rgb = pimg;}//调整亮度void do_image_BrightnessChange(struct image *img, int step, unsigned char *paddr){	int img_width=img->width, img_height=img->height, x_pos=0, y_pos=0;	unsigned char *pimg=img->rgb, *pcurr=NULL, *ptmp=NULL, *p=NULL;	ptmp = (unsigned char *)malloc(img_width*img_height*3);	assert(ptmp);	memset(ptmp, 0x00, img_width*img_height*3);	p = ptmp;	for (y_pos=0; y_pos<img_height; y_pos++)	{		pcurr = pimg + y_pos*img_width*3;		for (x_pos=0; x_pos<img_width; x_pos++)		{			if (step > 0)			{				*ptmp++ = min(255, *(pcurr+x_pos*3+0)+step);				*ptmp++ = min(255, *(pcurr+x_pos*3+1)+step);				*ptmp++ = min(255, *(pcurr+x_pos*3+2)+step);			}			else			{				*ptmp++ = max(0, *(pcurr+x_pos*3+0)+step);				*ptmp++ = max(0, *(pcurr+x_pos*3+1)+step);				*ptmp++ = max(0, *(pcurr+x_pos*3+2)+step);			}		}	}	if (img->rgb != paddr)		free(img->rgb);	img->rgb = p;}//调整RGBvoid	do_image_RGBChange(struct image *img, int red, int green, int blue, unsigned char *paddr){	int img_width=img->width, img_height=img->height, x_pos=0, y_pos=0;	unsigned char *pimg=img->rgb, *pcurr=NULL, *ptmp=NULL, *p=NULL;	ptmp = (unsigned char *)malloc(img_width*img_height*3);	assert(ptmp);	memset(ptmp, 0x00, img_width*img_height*3);	p = ptmp;	for (y_pos=0; y_pos<img_height; y_pos++)	{		pcurr = pimg + y_pos*img_width*3;		for (x_pos=0; x_pos<img_width; x_pos++)		{			if (red >0)				*(ptmp+0) = min(255, *(pcurr+x_pos*3+0)+red);			else				*(ptmp+0) = max(0, *(pcurr+x_pos*3+0)+red);							if (green >0)				*(ptmp+1) = min(255, *(pcurr+x_pos*3+1)+green);			else				*(ptmp+1) = max(0, *(pcurr+x_pos*3+1)+green);				if (blue >0)				*(ptmp+2) = min(255, *(pcurr+x_pos*3+2)+blue);			else				*(ptmp+2) = max(0, *(pcurr+x_pos*3+2)+blue);			ptmp = ptmp + 3;		}	}	if (img->rgb != paddr)		free(img->rgb);	img->rgb = p;}void	float_SaturationtChange(struct image *img, int step, unsigned char *paddr){	int iPercentSat=0, iPercentHue=0, x_pos=0, y_pos=0, img_width=0, img_height=0;	unsigned char *pimg=img->rgb, *pcurr=NULL, *ptmp=NULL, *p=NULL;	double		H, L, S ;	img_width = img->width;	img_height = img->height;	ptmp = (unsigned char *)malloc(img_width*img_height*3);	assert(ptmp);	memset(ptmp, 0x00, img_width*img_height*3);	p = ptmp;	iPercentHue = 128 * step / 100;	iPercentSat = 128 * step / 100;	for (y_pos=0; y_pos<img_height; y_pos++)	{		pcurr = pimg +y_pos*img_width*3;		for (x_pos=0; x_pos<img_width; x_pos++)		{			RGBtoHLS(pcurr, &H, &L, &S);			if (iPercentHue != 128)				H = H * iPercentHue / 128 ;			if (iPercentSat != 128)				S = S * iPercentSat / 128 ;			HLStoRGB(pcurr, H, L, S);			*p++ = *pcurr++;			*p++ = *pcurr++;			*p++ = *pcurr++;		}	}	if (img->rgb != paddr)		free(img->rgb);	img->rgb = ptmp;}void  AdjustContrast(struct image *img, int step, unsigned char *paddr){	int img_width=img->width, img_height=img->height, x_pos=0, y_pos=0;	unsigned char *pimg=img->rgb, *pcurr=NULL, *ptmp=NULL, *p=NULL;	int iPercent=(128*step)/100 ;	ptmp = (unsigned char *)malloc(img_width*img_height*3);	assert(ptmp);	memset(ptmp, 0x00, img_width*img_height*3);	p = ptmp;	for (y_pos=0; y_pos<img_height; y_pos++)	{		pcurr = pimg + y_pos*img_width*3;		for (x_pos=0; x_pos<img_width; x_pos++)		{			*(ptmp+0) = max(min(128 + (*(pcurr+x_pos*3+0)-128)* iPercent/128, 255), 0);			*(ptmp+1) = max(min(128 + (*(pcurr+x_pos*3+1)-128)* iPercent/128, 255), 0);			*(ptmp+2) = max(min(128 + (*(pcurr+x_pos*3+2)-128)* iPercent/128, 255), 0);			ptmp = ptmp + 3;		}	}	if (img->rgb != paddr)		free(img->rgb);	img->rgb = p;}void  Brightness_adjust(unsigned char *pdata, int length, int step){	unsigned char *ptmp = pdata;	unsigned char pchar = 0;	if (step > 0)	{		while (length--)		{			//ptmp++;			pchar = *ptmp;			*ptmp++ = min(255, pchar+step);			pchar = *ptmp;			*ptmp++ = min(255, pchar+step);			pchar = *ptmp;			*ptmp++ = min(255, pchar+step);			//ptmp++;		}	}	else	{		while (length--)		{			//ptmp++;			pchar = *ptmp;			*ptmp++ = max(0, pchar+step);			pchar = *ptmp;			*ptmp++ = max(0, pchar+step);			pchar = *ptmp;			*ptmp++ = max(0, pchar+step);			//ptmp++;		}	}}void	RGBChange_adjust(unsigned char *img, int width, int height, int red, int green, int blue){	int img_width=width, img_height=height, x_pos=0, y_pos=0;	unsigned char *pimg=img, *pcurr=NULL, *ptmp=NULL;	unsigned char pchar = 0;	for (y_pos=0; y_pos<img_height; y_pos++)	{		pcurr = pimg + y_pos*img_width*3;		ptmp = pcurr;		//pcurr = pimg + y_pos*img_width*4;		for (x_pos=0; x_pos<img_width; x_pos++)		{			//pcurr++;			if (red >0)				pchar = min(255, *(pcurr+x_pos*3+0)+red);			else				pchar = max(0, *(pcurr+x_pos*3+0)+red);			*ptmp++ = pchar;							if (green >0)				pchar = min(255, *(pcurr+x_pos*3+1)+green);			else				pchar = max(0, *(pcurr+x_pos*3+1)+green);			*ptmp++ = pchar;							if (blue >0)				pchar = min(255, *(pcurr+x_pos*3+2)+blue);			else				pchar = max(0, *(pcurr+x_pos*3+2)+blue);			*ptmp++ = pchar;				//pcurr++;		}	}}

⌨️ 快捷键说明

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