private void CalculateIntegrals()
{
// Calculate integral and integral squared values.
int stride = this.bitmapData.Stride;
int offset = stride - this.bytesInARow;
byte* srcStart = this.pixelBase + this.channel;
// Do the job
byte* src = srcStart;
// For each line
for (int y = 1; y <= this.height; y++)
{
int yy = this.normalWidth * y;
int y1 = this.normalWidth * (y - 1);
// For each pixel
for (int x = 1; x <= this.width; x++, src += this.pixelSize)
{
int pixel = *src;
int pixelSquared = pixel * pixel;
int r = yy + x;
int a = yy + (x - 1);
int b = y1 + x;
int g = y1 + (x - 1);
this.normalSum[r] = pixel + this.normalSum[a] + this.normalSum[b] - this.normalSum[g];
this.squaredSum[r] = pixelSquared + this.squaredSum[a] + this.squaredSum[b] - this.squaredSum[g];
}
src += offset;
}
if (this.computeTilted)
{
src = srcStart;
// Left-to-right, top-to-bottom pass
for (int y = 1; y <= this.height; y++, src += offset)
{
int yy = this.tiltedWidth * y;
int y1 = this.tiltedWidth * (y - 1);
for (int x = 2; x < this.width + 2; x++, src += this.pixelSize)
{
int a = y1 + (x - 1);
int b = yy + (x - 1);
int g = y1 + (x - 2);
int r = yy + x;
this.tiltedSum[r] = *src + this.tiltedSum[a] + this.tiltedSum[b] - this.tiltedSum[g];
}
}
{
int yy = this.tiltedWidth * this.height;
int y1 = this.tiltedWidth * (this.height + 1);
for (int x = 2; x < this.width + 2; x++, src += this.pixelSize)
{
int a = yy + (x - 1);
int c = yy + (x - 2);
int b = y1 + (x - 1);
int r = y1 + x;
this.tiltedSum[r] = this.tiltedSum[a] + this.tiltedSum[b] - this.tiltedSum[c];
}
}
// Right-to-left, bottom-to-top pass
for (int y = this.height; y >= 0; y--)
{
int yy = this.tiltedWidth * y;
int y1 = this.tiltedWidth * (y + 1);
for (int x = this.width + 1; x >= 1; x--)
{
int r = yy + x;
int b = y1 + (x - 1);
this.tiltedSum[r] += this.tiltedSum[b];
}
}
for (int y = this.height + 1; y >= 0; y--)
{
int yy = this.tiltedWidth * y;
for (int x = this.width + 1; x >= 2; x--)
{
int r = yy + x;
int b = yy + (x - 2);
this.tiltedSum[r] -= this.tiltedSum[b];
}
}
}
}