public static unsafe bool Read(Bitmap bitmap, out Tetromino nextPiece) { bool returner = false; BitmapData bmData = bitmap.LockBits(Rectangle, ImageLockMode.ReadOnly, PixelFormat); byte * scan0 = (byte *)bmData.Scan0.ToPointer(); nextPiece = null; foreach (var tet in Tetrominos.Values) { bool goodGuess = true; int i = 0; foreach (int address in tet.Addresses) { i++; if (scan0[address] < DigitPixels.ReadThreshold && scan0[address + 1] < DigitPixels.ReadThreshold && scan0[address + 2] < DigitPixels.ReadThreshold) { goodGuess = false; break; } } if (goodGuess) { returner = true; nextPiece = tet; break; } } bitmap.UnlockBits(bmData); return(returner); }
private static void SetAddresses(Tetromino tetromino) { int imageWidth = Image.Width; List <int> addresses = new List <int>(); foreach (Rectangle rect in tetromino.Rectangles) { int x0 = rect.X; int y0 = rect.Y; for (int x1 = 0; x1 < rect.Width; x1++) { for (int y1 = 0; y1 < rect.Width; y1++) { int x = x0 + x1; int y = y0 + y1; int address = BytesPerPixel * (x + imageWidth * y); addresses.Add(address); } } } tetromino.Addresses = addresses.ToArray(); }