using System;
using System.Diagnostics;
using System.Drawing;
namespace DesertPaintCodex.Util
{
public class Pixels
{
private readonly Bitmap _bitmap;
private readonly int _pixelMultiplier;
public int Height => _bitmap.Height / _pixelMultiplier;
public int Width => _bitmap.Width / _pixelMultiplier;
public Pixels(Bitmap bitmap, int pixellMutliplier)
{
_bitmap = bitmap;
_pixelMultiplier = pixellMutliplier;
}
public Color ColorAt(int x, int y)
{
return _bitmap.GetPixel(x * _pixelMultiplier, y * _pixelMultiplier);
}
public bool DoesPixelMatch(int x, int y, Func<Color, bool> matchFunc)
{
int xMult = x * _pixelMultiplier;
int yMult = y * _pixelMultiplier;
if (xMult < 0 || xMult >= _bitmap.Width || yMult < 0 || yMult >= _bitmap.Height)
{
Debug.WriteLine("Problem at position [" + x + ", " + y + "]");
}
return matchFunc(_bitmap.GetPixel(x * _pixelMultiplier, y * _pixelMultiplier));
}
// Compute length of horizontal bar starting at x,y using matching function
public int LengthOfColorAt(int x, int y, Func<Color, bool> matchFunc)
{
int count = 0;
for (int xVal = x; xVal < Width; ++xVal)
{
if (!matchFunc(_bitmap.GetPixel(xVal * _pixelMultiplier, y * _pixelMultiplier))) break;
++count;
}
return count;
}
public bool IsSolidPatchAt(int x, int y, int patchWidth, int patchHeight)
{
if ((x + patchWidth >= Width) || (y + patchHeight >= Height)) return false;
Color color = _bitmap.GetPixel(x * _pixelMultiplier, y * _pixelMultiplier);
// Debug.WriteLine("color at {0},{1} = {2},{3},{4}", x, y, color.R, color.G, color.B);
for (int pX = 0; pX < patchWidth; pX++)
{
for (int pY = 0; pY < patchHeight; pY++)
{
if (!PixelColor.IsMatch(color, _bitmap.GetPixel((x + pX) * _pixelMultiplier, (y + pY) * _pixelMultiplier)))
return false;
}
}
return true;
}
}
}