[code] if (x<0)x=0;else if (x>255)x=255;

//颜色范围0-255;
// 1.原始:
if (tem_b<0)tem_b=0;else if (tem_b>255)tem_b=255;
if (tem_g<0)tem_g=0;else if (tem_g>255)tem_g=255;
if (tem_r<0)tem_r=0;else if (tem_r>255)tem_r=255;
//2.使用条件状态值生成掩码来移除条件分支 tem_b &=-(tem_b>=0);//求负是为了生成掩码,也可以减1来生成掩码 <0 tem_b = (tem_b | -(tem_b>255) ) & 0xFF;//>255 tem_g &=-(tem_g>=0); tem_g = (tem_g | -(tem_g>255) ) & 0xFF; tem_r &=-(tem_r>=0); tem_r = (tem_r | -(tem_r>255) ) & 0xFF;
//3.使用带符号的移位生成掩码来移除条件分支 (建议使用该方案替代上面的条件状态值方案) tem_b &=~(tem_b>>31); tem_b = (tem_b | ((255-tem_b)>>31) ) & 0xFF; tem_g &=~(tem_g>>31); tem_g = (tem_g | ((255-tem_g)>>31) ) & 0xFF; tem_r &=~(tem_r>>31); tem_r = (tem_r | ((255-tem_r)>>31) ) & 0xFF; //4.查表移除分支; unsigned char_color_table[256+768+1];//假设color属于[-256..512] unsigned char*color_table=&_color_table[256];//先偏移256 //初始化 color_table; for ( int i = -256;i<768 ; i++) { if (i<0) color_table[i]=0; else if (i>255) color_table[i]=255; else color_table[i]=i; } ... resultRow[3*x]=color_table[tem_b]; resultRow[3*x+1]=color_table[tem_g]; resultRow[3*x+2]=color_table[tem_r];
原文地址:https://www.cnblogs.com/eaglediao/p/7136501.html