public static Bitmap ImageToBitmap(ColorFloatImage image) { Bitmap B = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb); LockBitmapInfo lbi = LockBitmap(B); try { for (int j = 0; j < image.Height; j++) { for (int i = 0; i < image.Width; i++) { ColorFloatPixel p = image[i, j]; lbi.data[lbi.linewidth * j + i * 4] = p.b <0.0f ? (byte)0 : p.b> 255.0f ? (byte)255 : (byte)p.b; lbi.data[lbi.linewidth * j + i * 4 + 1] = p.g <0.0f ? (byte)0 : p.g> 255.0f ? (byte)255 : (byte)p.g; lbi.data[lbi.linewidth * j + i * 4 + 2] = p.r <0.0f ? (byte)0 : p.r> 255.0f ? (byte)255 : (byte)p.r; } } } finally { UnlockBitmap(lbi); } return(B); }
public int CompareTo(object o) { ColorFloatPixel p = (ColorFloatPixel)o; float sum1 = this.b * this.b + this.g * this.g + this.r * this.r; float sum2 = p.b * p.b + p.g * p.g + p.r * p.r; float dif = sum1 - sum2; return(dif >= 0 ? (dif > 0 ? -1 : 0) : 1); }
public static ColorFloatImage BitmapToColorFloatImage(Bitmap B) { int W = B.Width, H = B.Height; ColorFloatImage res = new ColorFloatImage(W, H); if (B.PixelFormat == PixelFormat.Format8bppIndexed) { Color[] pi = B.Palette.Entries; byte[] pal = new byte[1024]; for (int i = 0; i < pi.Length; i++) { Color C = pi[i]; pal[i * 4] = C.B; pal[i * 4 + 1] = C.G; pal[i * 4 + 2] = C.R; pal[i * 4 + 3] = C.A; } LockBitmapInfo lbi = LockBitmap(B, PixelFormat.Format8bppIndexed, 1); try { for (int j = 0; j < H; j++) { for (int i = 0; i < W; i++) { int c = lbi.data[lbi.linewidth * j + i]; int b = pal[c * 4]; int g = pal[c * 4 + 1]; int r = pal[c * 4 + 2]; res[i, j] = new ColorFloatPixel() { b = b, g = g, r = r, a = 0.0f }; } } } finally { UnlockBitmap(lbi); } } else { LockBitmapInfo lbi = LockBitmap(B); try { for (int j = 0; j < H; j++) { for (int i = 0; i < W; i++) { int b = lbi.data[lbi.linewidth * j + i * 4]; int g = lbi.data[lbi.linewidth * j + i * 4 + 1]; int r = lbi.data[lbi.linewidth * j + i * 4 + 2]; res[i, j] = new ColorFloatPixel() { b = b, g = g, r = r, a = 0.0f }; } } } finally { UnlockBitmap(lbi); } } return(res); }