private void ProcessorThread()
{
while (true)
{
try
{
var total = 0;
foreach (var color in _colorsToDetect.Values)
{
color.UnlockedTotalDetected = 0;
}
int width, height;
var copy = _source.GetSourceAsBytes(out width, out height);
if (copy != null)
{
for (var x = 0; x < width/ChunkSize; x++)
{
for (var y = 0; y < height/ChunkSize; y++)
{
foreach (var color in _colorsToDetect.Values)
{
var rT = color.Color.R;
var gT = color.Color.G;
var bT = color.Color.B;
var rI = 255 - color.Color.R;
var gI = 255 - color.Color.G;
var bI = 255 - color.Color.B;
if (color.RecognisedArray == null ||
color.RecognisedArray.GetLength(0) != width/_chunkSize ||
color.RecognisedArray.GetLength(1) != height/_chunkSize)
{
color.RecognisedArray =
new int[width/_chunkSize,
height/_chunkSize];
}
var chunkScore = 0;
for (var xx = 0; xx < _chunkSize; xx++)
{
for (var yy = 0; yy < _chunkSize; yy++)
{
var idx = (x*_chunkSize + xx + (y*_chunkSize + yy)*width)*4;
var rA = copy[idx];
var gA = copy[idx + 1];
var bA = copy[idx + 2];
var illumination = (rA + gA + bA)/3f/255f;
var redOnTarget = 255 - Math.Abs(rT - rA);
var greenOnTarget = 255 - Math.Abs(gT - gA);
var blueOnTarget = 255 - Math.Abs(bT - bA);
var onTarget = (int) ((redOnTarget + greenOnTarget + blueOnTarget)/1f);
var redOffTarget = 255 - Math.Abs(rI - rA);
var greenOffTarget = 255 - Math.Abs(gI - gA);
var blueOffTarget = 255 - Math.Abs(bI - bA);
var offTarget = (int) ((redOffTarget + greenOffTarget + blueOffTarget)/0.5f);
chunkScore += (int) ((onTarget - offTarget)*illumination);
color.UnlockedTotalDetected += (int) (illumination*255f);
}
}
color.RecognisedArray[x, y] = chunkScore;
}
}
}
}
foreach (var color in _colorsToDetect.Values)
{
color.Sensitivity = Math.Max(0, color.TotalDetected)*GlobalSensitivity;
color.TotalDetected = color.UnlockedTotalDetected;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
Thread.Sleep(20);
}
}