public static unsafe StdInput FromBitmap(Bitmap bm) { int r = 2, g = 1, b = 0; float cr = 0.5f, cg = 0.419f, cb = 0.081f; // RMY convert method StdInput lenetInput = new StdInput(bm.Height, bm.Width); byte[] buffer = lenetInput.GetDataBuffer(); // lock source bitmap data BitmapData srcData = bm.LockBits( new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, bm.PixelFormat); int pixelSize = (srcData.PixelFormat == PixelFormat.Format8bppIndexed) ? 1 : 3; byte *src = (byte *)srcData.Scan0.ToPointer(); int srcOffset = srcData.Stride - bm.Width * pixelSize; int width = bm.Width; int height = bm.Height; int yres; // --- GrayScale --- if (srcData.PixelFormat == PixelFormat.Format8bppIndexed) { // for each line for (int y = 0; y < height; y++) { // for each pixel for (int x = 0; x < width; x++, src++) { yres = y; buffer[yres * width + x] = Convert.ToByte(255 - *src); } src += srcOffset; } } else //--- RGB --- { // for each line for (int y = 0; y < height; y++) { // for each pixel for (int x = 0; x < width; x++, src += pixelSize) { yres = y; buffer[yres * width + x] = Convert.ToByte(255 - Convert.ToByte(cr * src[r] + cg * src[g] + cb * src[b])); } src += srcOffset; } } // unlock source image bm.UnlockBits(srcData); return(lenetInput); }
public static unsafe StdInput FromBitmap(Bitmap bm) { int r = 2, g = 1, b = 0; float cr = 0.5f, cg = 0.419f, cb = 0.081f; // RMY convert method StdInput lenetInput = new StdInput(bm.Height, bm.Width); byte[] buffer = lenetInput.GetDataBuffer(); // lock source bitmap data BitmapData srcData = bm.LockBits( new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, bm.PixelFormat); int pixelSize = (srcData.PixelFormat == PixelFormat.Format8bppIndexed) ? 1 : 3; byte* src = (byte*)srcData.Scan0.ToPointer(); int srcOffset = srcData.Stride - bm.Width * pixelSize; int width = bm.Width; int height = bm.Height; int yres; // --- GrayScale --- if (srcData.PixelFormat == PixelFormat.Format8bppIndexed) { // for each line for (int y = 0; y < height; y++) { // for each pixel for (int x = 0; x < width; x++, src++) { yres = y; buffer[yres * width + x] = Convert.ToByte(255 - *src); } src += srcOffset; } } else //--- RGB --- { // for each line for (int y = 0; y < height; y++) { // for each pixel for (int x = 0; x < width; x++, src += pixelSize) { yres = y; buffer[yres * width + x] = Convert.ToByte(255 - Convert.ToByte(cr * src[r] + cg * src[g] + cb * src[b])); } src += srcOffset; } } // unlock source image bm.UnlockBits(srcData); return lenetInput; }