// BytesPerPixel = 2;
// BytesPerPaletteEntry = 4;
break;
default:
throw new ImageReadException("BMP: Unknown Compression: "
+ bhi.compression);
}
byte colorTable[] = null;
if (paletteLength > 0)
colorTable = this.readByteArray("ColorTable", paletteLength, is,
"Not a Valid BMP File");
if (verbose)
{
this.debugNumber("paletteLength", paletteLength, 4);
System.out.println("ColorTable: "
+ ((colorTable == null) ? "null" : "" + colorTable.length));
}
int pixelCount = bhi.width * bhi.height;
int imageLineLength = ((((bhi.bitsPerPixel) * bhi.width) + 7) / 8);
if (verbose)
{
// this.debugNumber("Total BitsPerPixel",
// (ExtraBitsPerPixel + bhi.BitsPerPixel), 4);
// this.debugNumber("Total Bit Per Line",
// ((ExtraBitsPerPixel + bhi.BitsPerPixel) * bhi.Width), 4);
// this.debugNumber("ExtraBitsPerPixel", ExtraBitsPerPixel, 4);
this.debugNumber("bhi.Width", bhi.width, 4);
this.debugNumber("bhi.Height", bhi.height, 4);
this.debugNumber("ImageLineLength", imageLineLength, 4);
// this.debugNumber("imageDataSize", imageDataSize, 4);
this.debugNumber("PixelCount", pixelCount, 4);
}
// int ImageLineLength = BytesPerPixel * bhi.Width;
while ((imageLineLength % 4) != 0)
imageLineLength++;
final int headerSize = BITMAP_FILE_HEADER_SIZE
+ bhi.bitmapHeaderSize
+ (bhi.bitmapHeaderSize == 40 && bhi.compression == BI_BITFIELDS ? 3*4 : 0);
int expectedDataOffset = headerSize + paletteLength;
if (verbose)
{
this.debugNumber("bhi.BitmapDataOffset", bhi.bitmapDataOffset, 4);
this.debugNumber("expectedDataOffset", expectedDataOffset, 4);
}
int extraBytes = bhi.bitmapDataOffset - expectedDataOffset;
if (extraBytes < 0)
throw new ImageReadException("BMP has invalid image data offset: "
+ bhi.bitmapDataOffset + " (expected: "
+ expectedDataOffset + ", paletteLength: " + paletteLength
+ ", headerSize: " + headerSize + ")");
else if (extraBytes > 0)
this.readByteArray("BitmapDataOffset", extraBytes, is,
"Not a Valid BMP File");
int imageDataSize = bhi.height * imageLineLength;
if (verbose)
this.debugNumber("imageDataSize", imageDataSize, 4);
byte imageData[];
if (rle)
imageData = getRLEBytes(is, rleSamplesPerByte);
else
imageData = this.readByteArray("ImageData", imageDataSize, is,
"Not a Valid BMP File");
if (verbose)
this.debugNumber("ImageData.length", imageData.length, 4);
PixelParser pixelParser;
switch (bhi.compression)
{
case BI_RLE4:
case BI_RLE8:
pixelParser = new PixelParserRle(bhi, colorTable, imageData);
break;
case BI_RGB:
pixelParser = new PixelParserRgb(bhi, colorTable, imageData);
break;
case BI_BITFIELDS:
pixelParser = new PixelParserBitFields(bhi, colorTable, imageData);
break;
default:
throw new ImageReadException("BMP: Unknown Compression: "
+ bhi.compression);
}
return new ImageContents(bhi, colorTable, imageData, pixelParser);
}