throw new WrongFileFormatException("Not a BMP file (first two bytes are not 0x42 0x4d).");
}
dataOffset = ArrayConverter.getIntLE(header, 0x0a);
if (dataOffset < 54)
{
throw new InvalidFileStructureException("BMP data expected to be 54dec or larger, got " + dataOffset);
}
imageWidth = ArrayConverter.getIntLE(header, 0x12);
imageHeight = ArrayConverter.getIntLE(header, 0x16);
if (imageWidth < 1 || imageHeight < 1)
{
throw new InvalidFileStructureException("BMP image width and height must be larger than 0, got " + imageWidth + " x " + imageHeight);
}
int planes = ArrayConverter.getShortLE(header, 0x1a);
if (planes != 1)
{
throw new InvalidFileStructureException("Can only handle BMP number of planes = 1, got " + planes);
}
colorDepth = ArrayConverter.getShortLE(header, 0x1c);
if (colorDepth != 1 && colorDepth != 4 && colorDepth != 8 && colorDepth != 24)
{
// TO DO: add support for 16 bpp BMP reading
throw new InvalidFileStructureException("Unsupported BMP color depth: " + colorDepth);
}
compression = ArrayConverter.getIntLE(header, 0x1e);
if (compression != 0 && !(compression == 1 && colorDepth == 8) && !(compression == 2 && colorDepth == 4))
{
throw new InvalidFileStructureException("Unsupported BMP compression type / color depth combination: " +
compression + " / " + colorDepth);
}
float dpiXValue = ArrayConverter.getIntLE(header, 0x26) / (100.0f / 2.54f);
float dpiYValue = ArrayConverter.getIntLE(header, 0x2a) / (100.0f / 2.54f);
setDpi((int)dpiXValue, (int)dpiYValue);