private Image Read8Bit(int paletteEntries)
{
byte[] bdata = new byte[width * height];
// Padding bytes at the end of each scanline
int padding = 0;
// width * bitsPerPixel should be divisible by 32
int bitsPerScanline = width * 8;
if ( bitsPerScanline%32 != 0) {
padding = (bitsPerScanline/32 + 1)*32 - bitsPerScanline;
padding = (int)Math.Ceiling(padding/8.0);
}
int imSize = (width + padding) * height;
// Read till we have the whole image
byte[] values = new byte[imSize];
int bytesRead = 0;
while (bytesRead < imSize) {
bytesRead += inputStream.Read(values, bytesRead, imSize - bytesRead);
}
if (isBottomUp) {
// Convert the bottom up image to a top down format by copying
// one scanline from the bottom to the top at a time.
for (int i=0; i<height; i++) {
Array.Copy(values,
imSize - (i+1) * (width + padding),
bdata,
i * width,
width);
}
} else {
for (int i=0; i<height; i++) {
Array.Copy(values,
i * (width + padding),
bdata,
i * width,
width);
}
}
return IndexedModel(bdata, 8, paletteEntries);
}