public static float[,] FromBitmap( UnmanagedImage image )
{
// check source image
if ( image.PixelFormat != PixelFormat.Format8bppIndexed )
throw new UnsupportedImageFormatException( "Only grayscale (8 bpp indexed images) are supported." );
// get source image dimension
int width = image.Width;
int height = image.Height;
// create texture array
float[,] texture = new float[height, width];
// do the job
unsafe
{
byte* src = (byte*) image.ImageData.ToPointer( );
int offset = image.Stride - width;
// for each line
for ( int y = 0; y < height; y++ )
{
// for each pixel
for ( int x = 0; x < width; x++, src++ )
{
texture[y, x] = (float) *src / 255.0f;
}
src += offset;
}
}
return texture;
}
}