A codec to read and write Windows BMP image files.
Typical file extensions are
.bmp
and
.rle
(the latter is only used for compressed files).
Bounds
This codec supports the bounds concept for loading and saving.
Supported BMP types when loading
- Bilevel, 1 bit per pixel, uncompressed.
BMP supports palettes for bilevel images, but the content of that
palette is ignored and 0 is considered black and 1 white.
Any class implementing
BilevelImage
can be given to the codec and it will load the image to that object
(if the image's resolution is sufficient).
If no image object is given to the codec, a new
MemoryBilevelImage
will be created. - Paletted, 4 bits per pixel, uncompressed or RLE4 compression.
Both types are loaded to a
Paletted8Image
object.
This requires 50 % more space than is necessary, but there is
no dedicated 4 bit image data class in JIU. - Paletted, 8 bits per pixel, uncompressed or RLE8 compression.
Both types are loaded to a
Paletted8Image
object. - RGB truecolor, 24 bits per pixel, uncompressed.
This is loaded to a
RGB24Image
object.
There is no support for 16 bpp images or BI_BITFIELDS compression (for lack of test files).
Supported JIU image data classes when saving to BMP
BilevelImage
objects are stored as 1 bit per pixel BMP files.Gray8Image
and
Paletted8Image
objects are stored as
paletted 8 bits per pixel files.
It doesn't really matter how many entries the palette has, the BMP file's
palette will always have 256 entries, filled up with zero entries if necessary.RGB24Image
objects are stored as 24 bpp BMP files.
There is no support for compressed BMP files when saving.
I/O classes
BMPCodec works with all input and output classes supported by ImageCodec
(
java.io.InputStream
,
java.io.OutputStream
,
java.io.DataInput
,
java.io.DataOutput
,
java.io.RandomAccessFile
).
Problems
The RLE-compressed BMP files that I could test this codec on seem to
have an end-of-line code at the end of every line instead of relying
on the decoder to know when it has unpacked enough bytes for a line.
Whenever this codec encounters an EOL symbol and has a current column
value of
0
, the EOL is ignored.
Usage examples
Write an image to a BMP file.
BMPCodec codec = new BMPCodec();
codec.setImage(image);
codec.setFile("out.bmp", CodecMode.SAVE);
codec.process();
codec.close();
Read an image from a BMP file.
BMPCodec codec = new BMPCodec();
codec.setFile("image.bmp", CodecMode.LOAD);
codec.process();
codec.close();
PixelImage image = codec.getImage();
loadCompressedPaletted4Stream
private void loadCompressedPaletted4Stream()
throws IOException
loadCompressedPaletted8Stream
private void loadCompressedPaletted8Stream()
throws IOException
loadUncompressedPaletted4Stream
private void loadUncompressedPaletted4Stream()
throws IOException
loadUncompressedPaletted8Stream
private void loadUncompressedPaletted8Stream()
throws IOException
loadUncompressedRgb24Stream
private void loadUncompressedRgb24Stream()
throws IOException
writeHeader
private void writeHeader(PixelImage image,
int filesize,
int offset,
int numBits)
throws IOException
writeInt
private void writeInt(int value)
throws IOException
writePalette
private void writePalette()
throws IOException
Write the palette associated with the image getImage().
Required not only for image objects that implement PalettedImage
but also for BilevelImage and Grayscale8Image.
For the latter two the palette values must be explicitly written into the file.
writeShort
private void writeShort(int value)
throws IOException
writeStream
private void writeStream()
throws IOException