📄 image_color.c
字号:
#include "fbv.h"void RGB2YUV(RGB *p_colRGB, YUV *p_YUV){ unsigned short localY; short localCb,localCr; localY = ( 76*(unsigned short)p_colRGB->r)+(150*(unsigned short)p_colRGB->g)+( 30*(unsigned short)p_colRGB->b); p_YUV->p_Y = localY & 0xFF; p_YUV->Y = (unsigned char)(localY>>8); localCr = p_colRGB->r - p_YUV->Y; /* Cr = R - Y */ p_YUV->p_Cr = localCr & 0x1; p_YUV->Cr = localCr>>1; localCb = p_colRGB->b - p_YUV->Y; /* Cb = B - Y */ p_YUV->p_Cb = localCr & 0x1; p_YUV->Cb = localCb>>1; /* on divise par 2 pour ne pas avoir */}void YUV2RGB(YUV *p_colYUV, RGB *p_RGB){ long localg; short localr,localb; localr = (p_colYUV->Cr<<1) + p_colYUV->p_Cr + p_colYUV->Y; /* R = Cr + Y */ if(localr<0) p_RGB->r = 0; else if(localr>255) p_RGB->r = 255; else p_RGB->r = (unsigned char)localr; p_RGB->p_r = localr; localb = (p_colYUV->Cb<<1) + p_colYUV->Y + p_colYUV->p_Cb; /* B = Cb + Y */ if(localb<0) p_RGB->b = 0; else if(localb>255) p_RGB->b = 255; else p_RGB->b = (unsigned char)localb; p_RGB->p_b = localb; localg = ((long)(p_colYUV->Y)*256 + (long)p_colYUV->p_Y - \ (long)((p_colYUV->Cr<<1) + p_colYUV->p_Cr + p_colYUV->Y)*76 - \ (long)((p_colYUV->Cb<<1) + p_colYUV->Y + p_colYUV->p_Cb)*30)/150; if(localg<0) p_RGB->g = 0; else if(localg>255) p_RGB->g = 255; else p_RGB->g = localg; p_RGB->p_g = localg;}float fooHuetoRGB(double m1, double m2, double h){ if (h < 0) h += 1.0 ; if (h > 1) h -= 1.0 ; if (6.0*h < 1) return (m1+(m2-m1)*h*6.0) ; if (2.0*h < 1) return m2 ; if (3.0*h < 2.0) return (m1+(m2-m1)*((2.0/3.0)-h)*6.0) ; return m1 ; }void HLStoRGB(unsigned char *pbuffer, const double H, const double L, const double S){ double r, g, b; double m1, m2; unsigned char *ptmp = pbuffer; if (S == 0) r = g = b = L ; else { if (L <= 0.5) m2 = L * (1.0+S); else m2 = L + S - L*S; m1 = 2.0*L - m2; r = fooHuetoRGB(m1, m2, H+1.0/3.0); g = fooHuetoRGB(m1, m2, H) ; b = fooHuetoRGB(m1, m2, H-1.0/3.0); } *(ptmp+0) = max(min((int)(r*255), 0xFF), 0); *(ptmp+1) = max(min((int)(g*255), 0xFF), 0); *(ptmp+2) = max(min((int)(b*255), 0xFF), 0);}void RGBtoHLS(unsigned char *pbuffer, double * H, double * L, double * S){ unsigned char *ptmp = pbuffer; double delta; double r = (double)(*ptmp+0)/255; double g = (double)(*ptmp+1)/255; double b = (double)(*ptmp+2)/255; double cmax = max(r, max(g, b)); double cmin = min(r, min(g, b)); *L = (cmax+cmin)/2.0 ; if (cmax == cmin) { *S = 0 ; *H = 0 ; } else { if (*L < 0.5) *S = (cmax-cmin)/(cmax+cmin); else *S = (cmax-cmin)/(2.0-cmax-cmin); delta = cmax - cmin; if (r == cmax) *H = (g-b)/delta; else if (g == cmax) *H = 2.0 + (b-r)/delta; else *H = 4.0 + (r-g)/delta; *H /= 6.0; if (*H < 0.0) *H += 1; }}//锐化处理void do_image_Sharpen(struct image *img, unsigned char *paddr){ int x_pos=0, y_pos=0, value=0; int img_width = img->width; int img_height = img->height; unsigned char *pimg = img->rgb; unsigned char *pcurr=NULL, *pprev=NULL, *p=NULL; unsigned char *psrc = (unsigned char *)malloc(img_width*img_height*3); assert(psrc); memset(psrc, 0x00, img_width*img_height*3); p = psrc; memcpy(psrc, pimg, img_width*img_height*3); psrc = psrc + img_width*3; for (y_pos=1; y_pos<img_height; y_pos++) { pcurr = pimg + (y_pos*img_width*3); pprev = pimg + ((y_pos-1)*img_width*3); for (x_pos=0; x_pos<img_width; x_pos++) { value = *(pcurr+0) + (*(pcurr+0) - *(pprev+0))/2; value = max(0, value); value = min(255, value); *(psrc+0) = *(pcurr+0) = (unsigned char)value; value = *(pcurr+1) + (*(pcurr+1) - *(pprev+1))/2; value = max(0, value); value = min(255, value); *(psrc+1) = *(pcurr+1) = (unsigned char)value; value = *(pcurr+2) + (*(pcurr+2) - *(pprev+2))/2; value = max(0, value); value = min(255, value); *(psrc+2) = *(pcurr+2) = (unsigned char)value; pcurr = pcurr + 3; pprev = pprev + 3; psrc = psrc + 3; } } if (img->rgb != paddr) free(img->rgb); img->rgb = p;}//模糊处理void do_image_muzzy(struct image *img, unsigned char *paddr){ int x_pos=0, y_pos=0, img_width=img->width, img_height=img->height; unsigned char *pRGBcurt=NULL, *pRGBprev=NULL, *pRGBnext=NULL; unsigned char *pimg = img->rgb, *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); memcpy(ptmp, pimg, img_width*img_height*3); p = ptmp; for (y_pos=1; y_pos<(img_height-2); y_pos++) { pRGBcurt = pimg + y_pos*img_width*3; pRGBprev = pimg + (y_pos-1)*img_width*3; pRGBnext = pimg + (y_pos+1)*img_width*3; ptmp = p + y_pos*img_width*3; for (x_pos=1; x_pos<(img_width-1); x_pos++) { value = (*(pRGBprev+(x_pos-1)*3+0)+*(pRGBcurt+(x_pos-1)*3+0)+*(pRGBnext+(x_pos-1)*3+0)+ *(pRGBprev+x_pos*3+0)+*(pRGBcurt+x_pos*3+0)+*(pRGBnext+x_pos*3+0)+ *(pRGBprev+(x_pos+1)*3+0)+*(pRGBcurt+(x_pos+1)*3+0)+*(pRGBnext+(x_pos+1)*3+0))/9; *(ptmp+0) = (unsigned char)value; value = (*(pRGBprev+(x_pos-1)*3+1)+*(pRGBcurt+(x_pos-1)*3+1)+*(pRGBnext+(x_pos-1)*3+1)+ *(pRGBprev+x_pos*3+1)+*(pRGBcurt+x_pos*3+1)+*(pRGBnext+x_pos*3+1)+ *(pRGBprev+(x_pos+1)*3+1)+*(pRGBcurt+(x_pos+1)*3+1)+*(pRGBnext+(x_pos+1)*3+1))/9; *(ptmp+1) = (unsigned char)value; value = (*(pRGBprev+(x_pos-1)*3+2)+*(pRGBcurt+(x_pos-1)*3+2)+*(pRGBnext+(x_pos-1)*3+2)+ *(pRGBprev+x_pos*3+2)+*(pRGBcurt+x_pos*3+2)+*(pRGBnext+x_pos*3+2)+ *(pRGBprev+(x_pos+1)*3+2)+*(pRGBcurt+(x_pos+1)*3+2)+*(pRGBnext+(x_pos+1)*3+2))/9; *(ptmp+2) = (unsigned char)value; ptmp = ptmp + 3; } } if (img->rgb != paddr) free(img->rgb); img->rgb = p;}//雕刻效果void do_image_Engrave(struct image *img, unsigned char *paddr){ int img_width=img->width, img_height=img->height, x_pos=0, y_pos=0, value=0; unsigned char *pimg=img->rgb, *pcurr=NULL, *pnext=NULL; unsigned char *ptmp=NULL, *p=NULL; ptmp = (unsigned char *)malloc(img_width*img_height*3); assert(ptmp); p = ptmp; for (y_pos=0; y_pos<img_height-1; y_pos++) { pcurr = pimg + y_pos*img_width*3; pnext = pimg + (y_pos+1)*img_width*3; ptmp = pcurr; for (x_pos=0; x_pos<img_width; x_pos++) { value = *(pnext+(x_pos+1)*3+0)-*(pcurr+x_pos*3+0)+128; value = max(0, value); value = min(255, value); *(ptmp+0) = value; value = *(pnext+(x_pos+1)*3+1)-*(pcurr+x_pos*3+1)+128; value = max(0, value); value = min(255, value); *(ptmp+1) = value; value = *(pnext+(x_pos+1)*3+2)-*(pcurr+x_pos*3+2)+128; value = max(0, value); value = min(255, value); *(ptmp+2) = value; ptmp = ptmp + 3; } } if (img->rgb != paddr) free(img->rgb); img->rgb = p;}//浮雕效果void do_image_Emboss(struct image *img, unsigned char *paddr){ int x_pos=0, y_pos=0, img_width=img->width, img_height=img->height, value=0; unsigned char *pimg=img->rgb, *pcurr=NULL, *pnext=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-1; y_pos++) { pcurr = pimg + y_pos*img_width*3; pnext = pimg + (y_pos+1)*img_width*3; ptmp = pcurr; for (x_pos=0; x_pos<img_width; x_pos++) { value = *(pcurr+x_pos*3+0) - *(pnext+(x_pos+1)*3+0)+128; value = max(0, value); value = min(255, value); *(ptmp+0) = value; value = *(pcurr+x_pos*3+1) - *(pnext+(x_pos+1)*3+1)+128; value = max(0, value); value = min(255, value); *(ptmp+1) = value; value = *(pcurr+x_pos*3+2) - *(pnext+(x_pos+1)*3+2)+128; value = max(0, value); value = min(255, value); *(ptmp+2) = value; ptmp = ptmp + 3; } } if (img->rgb != paddr) free(img->rgb); img->rgb = p;}//曝光效果void do_image_Exposure(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; unsigned char 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+x_pos*3+0); if (value < 128) *(ptmp+0) = ~value; else *(ptmp+0) = value; value = *(pcurr+x_pos*3+1); if (value < 128) *(ptmp+1) = ~value; else *(ptmp+1) = value; value = *(pcurr+x_pos*3+2); if (value < 128) *(ptmp+2) = ~value; else *(ptmp+2) = value; ptmp = ptmp + 3; } } if (img->rgb != paddr) free(img->rgb); img->rgb = p;}//反色,底片效果void do_image_reflect(struct image *img, unsigned char *paddr){ int img_width=img->width, img_height=img->height, y_pos=0; unsigned char *pimg=img->rgb, *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_width*img_height*3; y_pos++) *ptmp++ = ~(*pimg++); if (img->rgb != paddr) free(img->rgb); img->rgb = p;}//加权平均灰度化void do_image_GrayWeightAverage(struct image *img, unsigned char *paddr){ int img_width=img->width, img_height=img->height, x_pos=0, y_pos=0; int value=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++) { value = ((*(pcurr+0))*31 + (*(pcurr+1))*58 + (*(pcurr+2))*11)/100; *ptmp++ = value;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -