public static unsafe void ToRgb24(Lab24* from, Rgb24* to, int length=1) { if (length < 1) return; // 使用 OpenCV 中的算法实现 const float coeff0 = 0.39215686274509809f; const float coeff1 = 0.0f; const float coeff2 = 1.0f; const float coeff3 = (-128.0f); const float coeff4 = 1.0f; const float coeff5 = (-128.0f); Lab24* end = from + length; float x, y, z,l,a,b; int blue, green, red; while (from != end) { l = from->L * coeff0 + coeff1; a = from->A * coeff2 + coeff3; b = from->B * coeff4 + coeff5; l = (l + labLShift_32f) * (1.0f / labLScale_32f); x = (l + a * 0.002f); z = (l - b * 0.005f); y = l * l * l; x = x * x * x; z = z * z * z; blue = (int)((x * labBx_32f + y * labBy_32f + z * labBz_32f) * 255 + 0.5); green = (int)((x * labGx_32f + y * labGy_32f + z * labGz_32f) * 255 + 0.5); red = (int)((x * labRx_32f + y * labRy_32f + z * labRz_32f) * 255 + 0.5); red = red < 0 ? 0 : red > 255 ? 255 : red; green = green < 0 ? 0 : green > 255 ? 255 : green; blue = blue < 0 ? 0 : blue > 255 ? 255 : blue; to->Red = (byte)red; to->Green = (byte)green; to->Blue = (byte)blue; from++; to++; } }
public static unsafe void ToLab24(Byte* from, Lab24* to, int length = 1) { // 使用 OpenCV 中的算法实现 if (length < 1) return; Byte* end = from + length; int x, y, z; int l, a, b; bool flag; while (from != end) { Byte val = *from; Byte red = val; Byte green = val; Byte blue = val; x = blue * labXb + green * labXg + red * labXr; y = blue * labYb + green * labYg + red * labYr; z = blue * labZb + green * labZg + red * labZr; flag = x > labT; x = (((x) + (1 << ((lab_shift) - 1))) >> (lab_shift)); if (flag) x = icvLabCubeRootTab[x]; else x = (((x * labSmallScale + labSmallShift) + (1 << ((lab_shift) - 1))) >> (lab_shift)); flag = z > labT; z = (((z) + (1 << ((lab_shift) - 1))) >> (lab_shift)); if (flag == true) z = icvLabCubeRootTab[z]; else z = (((z * labSmallScale + labSmallShift) + (1 << ((lab_shift) - 1))) >> (lab_shift)); flag = y > labT; y = (((y) + (1 << ((lab_shift) - 1))) >> (lab_shift)); if (flag == true) { y = icvLabCubeRootTab[y]; l = (((y * labLScale - labLShift) + (1 << ((2 * lab_shift) - 1))) >> (2 * lab_shift)); } else { l = (((y * labLScale2) + (1 << ((lab_shift) - 1))) >> (lab_shift)); y = (((y * labSmallScale + labSmallShift) + (1 << ((lab_shift) - 1))) >> (lab_shift)); } a = (((500 * (x - y)) + (1 << ((lab_shift) - 1))) >> (lab_shift)) + 129; b = (((200 * (y - z)) + (1 << ((lab_shift) - 1))) >> (lab_shift)) + 128; l = l > 255 ? 255 : l < 0 ? 0 : l; a = a > 255 ? 255 : a < 0 ? 0 : a; b = b > 255 ? 255 : b < 0 ? 0 : b; to->L = (byte)l; to->A = (byte)a; to->B = (byte)b; from++; to++; } }