private void ProcessImage(UnmanagedImage image)
{
PixelFormat pixelFormat = image.PixelFormat;
// get image dimension
int width = image.Width;
int height = image.Height;
red = green = blue = gray = null;
// do the job
unsafe
{
// check pixel format
if (pixelFormat == PixelFormat.Format8bppIndexed)
{
// 8 bpp grayscale image
byte* p = (byte*)image.ImageData.ToPointer();
int offset = image.Stride - width;
// histogram array
int[] g = new int[width];
// for each pixel
for (int y = 0; y < height; y++)
{
// for each pixel
for (int x = 0; x < width; x++, p++)
{
g[x] += *p;
}
p += offset;
}
// create histogram for gray level
gray = new Histogram(g);
}
else if (pixelFormat == PixelFormat.Format16bppGrayScale)
{
// 16 bpp grayscale image
byte* basePtr = (byte*)image.ImageData.ToPointer();
int stride = image.Stride;
// histogram array
int[] g = new int[width];
// for each pixel
for (int y = 0; y < height; y++)
{
ushort* p = (ushort*)(basePtr + stride * y);
// for each pixel
for (int x = 0; x < width; x++, p++)
{
g[x] += *p;
}
}
// create historgram for gray level
gray = new Histogram(g);
}
else if (
(pixelFormat == PixelFormat.Format24bppRgb) ||
(pixelFormat == PixelFormat.Format32bppRgb) ||
(pixelFormat == PixelFormat.Format32bppArgb))
{
// 24/32 bpp color image
byte* p = (byte*)image.ImageData.ToPointer();
int pixelSize = (pixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
int offset = image.Stride - width * pixelSize;
// histogram arrays
int[] r = new int[width];
int[] g = new int[width];
int[] b = new int[width];
// for each line
for (int y = 0; y < height; y++)
{
// for each pixel
for (int x = 0; x < width; x++, p += pixelSize)
{
r[x] += p[RGB.R];
g[x] += p[RGB.G];
b[x] += p[RGB.B];
}
p += offset;
}
// create histograms
red = new Histogram(r);
green = new Histogram(g);
blue = new Histogram(b);
}
else if (
(pixelFormat == PixelFormat.Format48bppRgb) ||
(pixelFormat == PixelFormat.Format64bppArgb))
{
// 48/64 bpp color image
byte* basePtr = (byte*)image.ImageData.ToPointer();
int stride = image.Stride;
int pixelSize = (pixelFormat == PixelFormat.Format48bppRgb) ? 3 : 4;
// histogram arrays
int[] r = new int[width];
int[] g = new int[width];
int[] b = new int[width];
// for each line
for (int y = 0; y < height; y++)
{
ushort* p = (ushort*)(basePtr + stride * y);
// for each pixel
for (int x = 0; x < width; x++, p += pixelSize)
{
r[x] += p[RGB.R];
g[x] += p[RGB.G];
b[x] += p[RGB.B];
}
}
// create histograms
red = new Histogram(r);
green = new Histogram(g);
blue = new Histogram(b);
}
}
}
}