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 ToRgb24(Argb32 *from, Rgb24 *to, int length = 1) { if (length < 1) { return; } Argb32 *end = from + length; while (from != end) { *to = *((Rgb24 *)from); from++; to++; } }
public static unsafe void ToByte(Rgb24 *from, byte *to, int length = 1) { if (length < 1) { return; } Rgb24 *end = from + length; while (from != end) { *to = (Byte)(from->Blue * 0.114 + from->Green * 0.587 + from->Red * 0.299); from++; to++; } }
// [Benchmark] public void Rgb24_Scalar_PerElement_Pinned() { fixed(byte *r = &this.rBuf[0]) fixed(byte *g = &this.gBuf[0]) fixed(byte *b = &this.bBuf[0]) fixed(Rgb24 * rgb = &this.rgbBuf[0]) { for (int i = 0; i < this.Count; i++) { Rgb24 *d = rgb + i; d->R = r[i]; d->G = g[i]; d->B = b[i]; } } }
public unsafe ImageInt32 ToGrayscaleImageInt32(double rCoeff, double gCoeff, double bCoeff) { ImageInt32 img = new ImageInt32(this.Width, this.Height); Rgb24 * p = Start; Int32 * to = img.Start; Rgb24 * end = p + Length; if (Length < 1024) { while (p != end) { *to = (Byte)(p->Red * rCoeff + p->Green * gCoeff + p->Blue * bCoeff); p++; to++; } } else { int *bCache = stackalloc int[256]; int *gCache = stackalloc int[256]; int *rCache = stackalloc int[256]; const int shift = 1 << 10; int rShift = (int)(rCoeff * shift); int gShift = (int)(gCoeff * shift); int bShift = shift - rShift - gShift; int r = 0, g = 0, b = 0; for (int i = 0; i < 256; i++) { bCache[i] = b; gCache[i] = g; rCache[i] = r; b += bShift; g += gShift; r += rShift; } while (p != end) { *to = (Byte)((bCache[p->Red] + gCache[p->Green] + rCache[p->Red]) >> 10); p++; to++; } } return(img); }
private static unsafe float[] Load(Image <Rgb24> image) { int resolution = image.Height * image.Width; float[] sample = new float[resolution * 3]; fixed(Rgb24 *p0 = &image.DangerousGetPinnableReferenceToPixelBuffer()) fixed(float *psample = sample) { for (int i = 0; i < resolution; i++) { Rgb24 *pxy = p0 + i; psample[i] = pxy->R / 255f; psample[i + resolution] = pxy->G / 255f; psample[i + 2 * resolution] = pxy->B / 255f; } } return(sample); }
public static unsafe void ToArgb32(Rgb24 *from, Argb32 *to, int length = 1) { if (length < 1) { return; } Rgb24 *end = from + length; while (from != end) { to->Blue = from->Blue; to->Green = from->Green; to->Red = from->Red; to->Alpha = 255; from++; to++; } }
public static unsafe void ToRgb24(byte *from, Rgb24 *to, int length = 1) { if (length < 1) { return; } Byte *end = from + length; while (from != end) { Byte val = *from; to->Blue = val; to->Green = val; to->Red = val; from++; to++; } }
public static unsafe void ToLab24(Rgb24 *from, Lab24 *to, int length = 1) { // 使用 OpenCV 中的算法实现 if (length < 1) { return; } Rgb24 *end = from + length; int x, y, z; int l, a, b; bool flag; while (from != end) { Byte red = from->Red; Byte green = from->Green; Byte blue = from->Blue; 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++; } }
public unsafe void Copy(Rgb24 *from, void *to, int length) { UnmanagedImageConverter.ToArgb32(from, (Argb32 *)to, length); }
public unsafe void Copy(Rgb24 *from, void *to, int length) { UnmanagedImageConverter.Copy((byte *)from, (byte *)to, 3 * length); }
public unsafe void Copy(Rgb24 *from, void *to, int length) { UnmanagedImageConverter.ToLab24(from, (Lab24 *)to, length); }