public double CorrelationWith(Bitmap24 b) { double avg1 = 0; double avg2 = 0; for (int i = 0; i < data.Length; i++) { avg1 += data[i]; avg2 += b.data[i]; } avg1 /= data.Length; avg2 /= b.data.Length; double sum1 = 0; double sum2 = 0; double sum3 = 0; for (int i = 0; i < data.Length; i++) { double d1 = data[i] - avg1; double d2 = b.data[i] - avg2; double prod = d1 * d2; sum1 += prod; sum2 += (d1 * d1); sum3 += (d2 * d2); } double r = sum1 / Math.Sqrt(sum2 * sum3); return(r); }
public static bool Same(Bitmap24 b1, Bitmap24 b2) { for (int i = 0; i < b1.data.Length; i++) { if (b1.data[i] != b2.data[i]) { return(false); } } return(true); }
public override bool HasColor(int[] color, int[] threshold) { for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { if (Bitmap24.PixelEqual(color, this.GetPixel(x, y), threshold)) { return(true); } } } return(false); }
public override int CountColor(int[] color, int[] threshold) { int count = 0; for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { if (Bitmap24.PixelEqual(color, this.GetPixel(x, y), threshold)) { count++; } } } return(count); }
// This helper function assumes that the input Bitmap24 has already been locked bits. private int CountDark(Bitmap24 bmp24, System.Drawing.Rectangle rect) { int cnt = 0; for (int x = rect.Left; x < rect.Right; ++x) { for (int y = rect.Top; y < rect.Bottom; ++y) { int[] px = bmp24.GetPixel(x, y); if (IsDarkPixel(px)) { ++cnt; } } } return(cnt); }
public bool SameAs(Bitmap24 b) { return(Bitmap24.Same(this, b)); }
private void ProcessGlobalKeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if (e.Control && e.Shift && e.Alt) { switch (e.KeyCode) { // Shortcuts for changing bot settings. case Keys.F6: this.chkEnableConveienceKeys.IsChecked ^= true; break; // Shortcuts for setting the bounds of the region. case Keys.I: { System.Drawing.Point pt = System.Windows.Forms.Cursor.Position; SetTop(pt.Y); } break; case Keys.J: { System.Drawing.Point pt = System.Windows.Forms.Cursor.Position; SetLeft(pt.X); } break; case Keys.K: { System.Drawing.Point pt = System.Windows.Forms.Cursor.Position; SetRight(pt.X); } break; case Keys.M: { System.Drawing.Point pt = System.Windows.Forms.Cursor.Position; SetBottom(pt.Y); } break; // Shortcuts for defininig the top-left and bottom-right points of the region. case Keys.U: { System.Drawing.Point pt = System.Windows.Forms.Cursor.Position; SetLeft(pt.X); SetTop(pt.Y); } break; case Keys.Oemcomma: { System.Drawing.Point pt = System.Windows.Forms.Cursor.Position; SetRight(pt.X); SetBottom(pt.Y); } break; // Debugging shortcuts case Keys.L: { System.Drawing.Point pt = System.Windows.Forms.Cursor.Position; System.Drawing.Rectangle rect = new System.Drawing.Rectangle(pt.X, pt.Y, 1, 1); using (System.Drawing.Bitmap bmp = WindowWrapper.ScreenCapture(rect)) { using (Bitmap24 bmp24 = Bitmap24.FromImage(bmp)) { bmp24.Lock(); int[] px = bmp24.GetPixel(0, 0); double lum = Bitmap24.CalculateLuminance(px); Console.WriteLine("({0}, {1}) -> ({1}) -> {2}", pt.X, pt.Y, string.Join(", ", px), lum); } } } break; case Keys.O: { System.Drawing.Rectangle bounds = new System.Drawing.Rectangle( _left, _top, _right - _left + 1, _bottom - _top + 1 ); using (System.Drawing.Bitmap bmp = WindowWrapper.ScreenCapture(bounds)) { bmp.Save(@"tmp.bmp"); } } break; } } }
private bool IsDarkPixel(int[] px) { return(Bitmap24.CalculateLuminance(px) <= 40.96); }
private void btnShrinkToFit_Click(object sender, RoutedEventArgs e) { // Create a Rectangle defining the region. System.Drawing.Rectangle bounds = new System.Drawing.Rectangle( _left - 1, _top - 1, _right - _left + 3, _bottom - _top + 3 ); // Get the width and height of the region. int width = bounds.Width; int height = bounds.Height; // Quit if the width and the height are too small. if (width < 20 || height < 20) { MessageBox.Show("The defined region is too small. Please update the region and try again.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } // Get a screenshot of the defined region to process. using (System.Drawing.Bitmap bmp = WindowWrapper.ScreenCapture(bounds)) { using (Bitmap24 bmp24 = Bitmap24.FromImage(bmp)) { // Lock the bits in the Bitmap24 to directly access the raw data. bmp24.Lock(); // Compute how much to shift the left coordinate by. int leftShift = -1; int leftDark = 0; for (int x = 0; x < width; ++x) { System.Drawing.Rectangle rect = new System.Drawing.Rectangle(x, (height / 2) - CHECK_AROUND, 1, CHECK_SIZE); int currDark = CountDark(bmp24, rect); if (leftDark == CHECK_SIZE && currDark == 0) { leftShift = x - 1; break; } leftDark = currDark; } // Compute how much to shift the right coordinate by. int rightShift = -1; int rightDark = 0; for (int x = width - 1; x >= 0; --x) { System.Drawing.Rectangle rect = new System.Drawing.Rectangle(x, (height / 2) - CHECK_AROUND, 1, CHECK_SIZE); int currDark = CountDark(bmp24, rect); if (rightDark == CHECK_SIZE && currDark == 0) { rightShift = width - x - 2; break; } rightDark = currDark; } // Compute how much to shift the top coordinate by. int topShift = -1; int topDark = 0; for (int y = 0; y < height; ++y) { System.Drawing.Rectangle rect = new System.Drawing.Rectangle((width / 2) - CHECK_AROUND, y, CHECK_SIZE, 1); int currDark = CountDark(bmp24, rect); if (topDark == CHECK_SIZE && currDark == 0) { topShift = y - 1; break; } topDark = currDark; } // Compute how much to shift the bottom coordinate by. int bottomShift = -1; int bottomDark = 0; for (int y = height - 1; y >= 0; --y) { System.Drawing.Rectangle rect = new System.Drawing.Rectangle((width / 2) - CHECK_AROUND, y, CHECK_SIZE, 1); int currDark = CountDark(bmp24, rect); if (bottomDark == CHECK_SIZE && currDark == 0) { bottomShift = height - y - 2; break; } bottomDark = currDark; } // Define a list containing the ones that were not found. List <string> notFound = new List <string>(); // Check if a valid left shift was found. if (leftShift > 0) { SetLeft(_left + leftShift); } else if (leftShift < 0) { notFound.Add("Left"); } // Check if a valid right shift was found. if (rightShift > 0) { SetRight(_right - rightShift); } else if (rightShift < 0) { notFound.Add("Right"); } // Check if a valid top shift was found. if (topShift > 0) { SetTop(_top + topShift); } else if (topShift < 0) { notFound.Add("Top"); } // Check if a valid bottom shift was found. if (bottomShift > 0) { SetBottom(_bottom - bottomShift); } else if (bottomShift < 0) { notFound.Add("Bottom"); } if (notFound.Count > 0) { string errorMessage = "Unable to auto-detect the bounds. Please make sure that the defined region completely contains the game window, and that the game is displaying a bright image, such as the main screen.\n\nThe following dimensions were not found:\n\n" + string.Join("\n", notFound) + "\n\n"; MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } } }