Recent articles:
Popular archives:
Java: A platform for platforms
Sun's reorg may seem promising to shareholders but it's also a scramble for position. The question now is whether Sun can,
or wants to, maintain its hold on Java technology. Especially with enterprise leaders like SpringSource and RedHat investing
heavily in Java's future as a platform for platforms
Also see:
Discuss: Java: A platform for platforms?
The Windows DIB (Device-Independent Bitmap) file format is relatively straightforward. The DIB format preserves explicit information unlike the plain bitmap format which is used for storage of the image in RAM. The problem is that there are so many image format varieties (1, 4, 8, and 16 bits, among others). The solution offered in this Java Tip will address only the 8- and 24-bit flavors. These two represent the most widely encountered varieties.
Regardless of the Windows DIB sub-type, the file format always consists of a 14-byte file header and a 40-byte information header. These two headers contain information about exactly what is stored in the file and in what order. Consult the Microsoft Software Development Kit (SDK) for the exact and precise meaning of each item in the headers. The remaining file contents vary depending on the data in the information header.
Let's look at the two sub-types we're addressing here. The 24-bit flavor is very simple: The RGB (Red-Green-Blue) color values (3 bytes, in BGR order) follow immediately after the information header. However, each scan line is padded out to an even 4-byte boundary. This "padding" is documented (see Microsoft SDK) to be an optimization for the Windows bitmap drawing APIs. Also, the bottom scan line is the first thing encountered in the file -- so the image has to be read backwards in relation to the usual graphics coordinate systems in which the positive vector orientation is down and to the right.
The 8-bit sub-type is complicated by the insertion of color palette information between the information header and the pixel data. Therefore, each pixel entry is simply an 8-bit index into the palette array of 24-bit RGB colors. Once again, the pixel information is padded out to an even 4-byte boundary for each scan line.
Note that the bitmap image-loading method presented here does not support decompression of compressed bitmap images. In fact, this routine does not even look for this possibility! This routine is sure to generate an exception if a compressed Windows DIB file is encountered. The compressed Windows DIB format is documented in the Windows SDK.
In terms of performance, this routine can read a 24-bit, 640 x 480 file (approximately 920 kilobytes) in less than 10 seconds
on a 486-DX2-66MHz running Microsoft Windows 95. Performance can be signficantly enhanced by using BufferedInputStream instead of FileInputStream.
TEXTBOX: TEXTBOX_HEAD: Follow-Up Tip! from Don McQuinn
The loadbitmap method in Java Tip 43 may have a bug. For the 24-bit format, it computes the pad ("npad"), which can be 4. When npad is computed as 4, it should be set to 0, as is done in the writeBitmap method in Java Tip 60. :END_TEXTBOX