protected override unsafe void ProcessFilter(UnmanagedImage image, UnmanagedImage overlay)
{
// get image dimension
int width = image.Width;
int height = image.Height;
// width and height to process
int widthToProcess = width;
int heightToProcess = height;
// if generator was specified, then generate a texture
// otherwise use provided texture
if (textureGenerator != null)
{
texture = textureGenerator.Generate(width, height);
}
else
{
widthToProcess = Math.Min(width, texture.GetLength(1));
heightToProcess = Math.Min(height, texture.GetLength(0));
}
int pixelSize = Image.GetPixelFormatSize(image.PixelFormat) / 8;
int srcOffset = image.Stride - widthToProcess * pixelSize;
int ovrOffset = overlay.Stride - widthToProcess * pixelSize;
// do the job
byte* ptr = (byte*)image.ImageData.ToPointer();
byte* ovr = (byte*)overlay.ImageData.ToPointer();
// for each line
for (int y = 0; y < heightToProcess; y++)
{
// for each pixel
for (int x = 0; x < widthToProcess; x++)
{
double t1 = texture[y, x];
double t2 = 1 - t1;
for (int i = 0; i < pixelSize; i++, ptr++, ovr++)
{
*ptr = (byte)Math.Min(255.0f, *ptr * t1 + *ovr * t2);
}
}
ptr += srcOffset;
ovr += ovrOffset;
}
}
}