public static Bitmap ImageToBitmap(ColorByteImage 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++) { ColorBytePixel p = image[i, j]; lbi.data[lbi.linewidth * j + i * 4] = p.b; lbi.data[lbi.linewidth * j + i * 4 + 1] = p.g; lbi.data[lbi.linewidth * j + i * 4 + 2] = p.r; } } } finally { UnlockBitmap(lbi); } return(B); }
public static Bitmap ImageToBitmap(GrayscaleFloatImage 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++) { byte c = image[i, j] < 0.0f ? (byte)0 : image[i, j] > 255.0f ? (byte)255 : (byte)image[i, j]; lbi.data[lbi.linewidth * j + i * 4] = c; lbi.data[lbi.linewidth * j + i * 4 + 1] = c; lbi.data[lbi.linewidth * j + i * 4 + 2] = c; } } } finally { UnlockBitmap(lbi); } return(B); }
public static GrayscaleFloatImage BitmapToGrayscaleFloatImage(Bitmap B) { int W = B.Width, H = B.Height; GrayscaleFloatImage res = new GrayscaleFloatImage(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] = 0.114f * b + 0.587f * g + 0.299f * r; } } } 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] = 0.114f * b + 0.587f * g + 0.299f * r; } } } finally { UnlockBitmap(lbi); } } return(res); }
public static void UnlockBitmap(LockBitmapInfo lbi) { lbi.B.UnlockBits(lbi.bitmapData); lbi.bitmapData = null; lbi.data = null; }
public static ColorByteImage BitmapToColorByteImage(Bitmap B) { int W = B.Width, H = B.Height; ColorByteImage res = new ColorByteImage(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]; byte b = pal[c * 4]; byte g = pal[c * 4 + 1]; byte r = pal[c * 4 + 2]; res[i, j] = new ColorBytePixel() { b = b, g = g, r = r, a = 255 }; } } } finally { UnlockBitmap(lbi); } } else { LockBitmapInfo lbi = LockBitmap(B); try { for (int j = 0; j < H; j++) { for (int i = 0; i < W; i++) { byte b = lbi.data[lbi.linewidth * j + i * 4]; byte g = lbi.data[lbi.linewidth * j + i * 4 + 1]; byte r = lbi.data[lbi.linewidth * j + i * 4 + 2]; res[i, j] = new ColorBytePixel() { b = b, g = g, r = r, a = 255 }; } } } finally { UnlockBitmap(lbi); } } return(res); }