Re: Getting header info of a BMP file
mmcclaf@gmail.com wrote:
Does anyone know how to get all of the header file from a .BMP file
using java?
I took a look at them but I'm still having troubles with a
FileInputStream, or perhaps its the understanding of how it works. And
then the trouble of trying to split it up into the different sections.
HAs anyone done anything like this with coding before?
There may be easier ways of getting the information you seek,
but just as a kick-start:
[TODO: needs work]
import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.BitSet;
public class BMPTest {
public static void main(String[] args)
throws Exception {
File file = new File(args[0]);
byte[] fileHeaderBytes = new byte[14];
byte[] infoSizeBytes = new byte[4];
int infoSize;
byte[] infoHeader;
InfoHeader header;
FileInputStream inputStream =
new FileInputStream(file);
inputStream.read(fileHeaderBytes);
inputStream.read(infoSizeBytes, 0, 4);
infoSize =
(((int)infoSizeBytes[3]&0xff)<<24) |
(((int)infoSizeBytes[2]&0xff)<<16) |
(((int)infoSizeBytes[1]&0xff)<<8) |
(int)infoSizeBytes[0]&0xff;
if(infoSize == 40) {
infoHeader = new byte[40];
infoHeader[0] = infoSizeBytes[0];
infoHeader[1] = infoSizeBytes[1];
infoHeader[2] = infoSizeBytes[2];
infoHeader[3] = infoSizeBytes[3];
inputStream.read(infoHeader, 4, 36);
}
else if(infoSize == 120) {
infoHeader = new byte[120];
infoHeader[0] = infoSizeBytes[0];
infoHeader[1] = infoSizeBytes[1];
infoHeader[2] = infoSizeBytes[2];
infoHeader[3] = infoSizeBytes[3];
inputStream.read(infoHeader, 4, 116);
}
else if(infoSize == 136) {
infoHeader = new byte[136];
infoHeader[0] = infoSizeBytes[0];
infoHeader[1] = infoSizeBytes[1];
infoHeader[2] = infoSizeBytes[2];
infoHeader[3] = infoSizeBytes[3];
inputStream.read(infoHeader, 4, 132);
}
else throw new Exception();
header = new InfoHeader(infoHeader);
System.out.println(header.toString());
}
}
/**
* <p>The InfoHeader class contains information
* about the dimensions and color format of a
* Windows Device Independant Bitmap.</p>
* <p>For Windows prior to Windows 95:
* Applications can use the BITMAPV4HEADER
* structure for added functionality.</p>
* <p>For Windows 95, Windows NT 4.0:
* Applications can use the BITMAPV4HEADER
* structure for added functionality.</p>
* <p>For Windows 98/Me, Windows 2000/XP:
* Applications can use the BITMAPV5HEADER
* structure for added functionality.</p>
*/
public class InfoHeader {
private final byte[] data;
/**
* The sole constructor for the InfoHeader class.
* @param b
*/
public InfoHeader(byte[] data) {
if( data.length == 40 ||
data.length == 120 ||
data.length != 136) {
this.data = data;
}
else
throw new IllegalArgumentException();
}
/**
*
* @return
*/
public InfoHeaderVersion getVersion() {
if(data.length == 40) {
return InfoHeaderVersion.V0;
}
else if(data.length == 120) {
return InfoHeaderVersion.V4;
}
else {
return InfoHeaderVersion.V5;
}
}
/**
* Specifies the number of bytes required by the structure.
* Applications should use this member to determine which
* bitmap information header structure is being used.
*
* @return
*/
public Long getSize() {
return null;
}
/**
* Specifies the width of the bitmap, in pixels.
*
* <p>If bV5Compression is BI_JPEG or BI_PNG,
* the bV5Width member specifies the width of the
* decompressed JPEG or PNG image in pixels.</p>
*
* @return
*/
public Long getWidth() {
return null;
}
/**
* Specifies the height of the bitmap, in pixels.
* If the value of bV5Height is positive, the bitmap is a
* bottom-up DIB and its origin is the lower-left corner.
* If bV5Height value is negative, the bitmap is a top-down
* DIB and its origin is the upper-left corner.
*
* <p>If bV5Height is negative, indicating a top-down DIB,
* bV5Compression must be either BI_RGB or BI_BITFIELDS.
* Top-down DIBs cannot be compressed.</p>
*
* <p>If bV5Compression is BI_JPEG or BI_PNG,
* the bV5Height member specifies the height of the
* decompressed JPEG or PNG image in pixels.</p>
*
* @return
*/
public Long getHeight() {
return null;
}
/**
* Specifies the number of planes for the target device.
* This value must be set to 1.
*
* @return
*/
public Integer getPlanes() {
return Integer.valueOf(1);
}
/**
* Specifies the number of bits that define each pixel
* and the maximum number of colors in the bitmap.
*
* @return
*/
public Integer getBitCount() {
return null;
}
/**
* Specifies that the bitmap is not compressed.
* The bV5RedMask, bV5GreenMask, and bV5BlueMask members
* specify the red, green, and blue components of each pixel.
* This is valid when used with 16- and 32-bpp bitmaps.
*
* @return
*/
public Compression getV5Compression() {
return null;
}
/**
* Specifies the size, in bytes, of the image.
* This may be set to zero for BI_RGB bitmaps.
*
* <p>If bV5Compression is BI_JPEG or BI_PNG,
* bV5SizeImage is the size of the JPEG or PNG image buffer.</p>
*
* @return
*/
public Long getSizeImage() {
return null;
}
/**
* Specifies the horizontal resolution, in pixels-per-meter,
* of the target device for the bitmap.
* An application can use this value to select a bitmap from
* a resource group that best matches the characteristics of
* the current device.
*
* @return
*/
public Integer getXPelsPerMeter() {
return null;
}
/**
* Specifies the vertical resolution, in pixels-per-meter,
* of the target device for the bitmap.
*
* @return
*/
public Integer getYPelsPerMeter() {
return null;
}
/**
* Specifies the number of color indexes in the color table
* that are actually used by the bitmap. If this value is
* zero, the bitmap uses the maximum number of colors
* corresponding to the value of the bV5BitCount member for
* the compression mode specified by bV5Compression.
*
* <p>If bV5ClrUsed is nonzero and bV5BitCount is less than
* 16, the bV5ClrUsed member specifies the actual number of
* colors the graphics engine or device driver accesses.
* If bV5BitCount is 16 or greater, the bV5ClrUsed member
* specifies the size of the color table used to optimize
* performance of the system color palettes.
* If bV5BitCount equals 16 or 32, the optimal color palette
* starts immediately following the BITMAPV5HEADER.
* If bV5ClrUsed is nonzero,
* the color table is used on palettized devices, and
* bV5ClrUsed specifies the number of entries.</p>
*
* @return
*/
public Integer getClrUsed() {
return null;
}
/**
* Specifies the number of color indexes that are
* required for displaying the bitmap.
* If this value is zero, all colors are required.
*
* @return
*/
public Integer getClrImportant() {
return null;
}
/**
* Color mask that specifies the red component of each pixel,
* valid only if bV5Compression is set to BI_BITFIELDS.
*
* @return
*/
public BitSet getRedMask() {
if(getVersion() == InfoHeaderVersion.V0)
return null;
else
return null;
}
/**
* Color mask that specifies the green component of each pixel,
* valid only if bV5Compression is set to BI_BITFIELDS.
*
* @return
*/
public BitSet getGreenMask() {
if(getVersion() == InfoHeaderVersion.V0)
return null;
else
return null;
}
/**
* Color mask that specifies the blue component of each pixel,
* valid only if bV5Compression is set to BI_BITFIELDS.
*
* @return
*/
public BitSet getBlueMask() {
if(getVersion() == InfoHeaderVersion.V0)
return null;
else
return null;
}
/**
* Color mask that specifies the alpha component of each pixel.
*
* @return
*/
public BitSet getAlphaMask() {
if(getVersion() == InfoHeaderVersion.V0)
return null;
else
return null;
}
/**
* Specifies the color space of the DIB.
*
* @return
*/
public LogicalColorSpace getCSType() {
if(getVersion() == InfoHeaderVersion.V0)
return null;
else
return null;
}
/**
* A CIEXYZTRIPLE structure that specifies the x, y,
* and z coordinates of the three colors that correspond
* to the red, green, and blue endpoints for the logical
* color space associated with the bitmap.
* This member is ignored unless the bV5CSType member
* specifies LCS_CALIBRATED_RGB.
*
* @return
*/
public CIEXYZTriple getEndpoints() {
if(getVersion() == InfoHeaderVersion.V0)
return null;
else
return null;
}
/**
* Toned response curve for red.
* Used if bV5CSType is set to LCS_CALIBRATED_RGB.
* Specified in 16^16 format.
*
* @return
*/
public FixedPoint8Dot8 getGammaRed() {
if(getVersion() == InfoHeaderVersion.V0)
return null;
else
return null;
}
/**
* Toned response curve for green.
* Used if bV5CSType is set to LCS_CALIBRATED_RGB.
* Specified in 16^16 format.
*
* @return
*/
public FixedPoint8Dot8 getGammaGreen() {
if(getVersion() == InfoHeaderVersion.V0)
return null;
else
return null;
}
/**
* Toned response curve for blue.
* Used if bV5CSType is set to LCS_CALIBRATED_RGB.
* Specified in 16^16 format.
*
* @return
*/
public FixedPoint8Dot8 getGammaBlue() {
if(getVersion() == InfoHeaderVersion.V0)
return null;
else
return null;
}
/**
* Rendering intent for bitmap.
*
* @return
*/
public GamutMappingIntent getIntent() {
if(getVersion() == InfoHeaderVersion.V0 ||
getVersion() == InfoHeaderVersion.V4)
return null;
else
return null;
}
/**
* The offset, in bytes, from the beginning of the
* BITMAPV5HEADER structure to the start of the profile data.
* If the profile is embedded, profile data is the actual profile,
* and it is linked.
* (The profile data is the null-terminated file name of the profile.)
* This cannot be a Unicode string.
* It must be composed exclusively of characters from the
* Windows character set (code page 1252).
* These profile members are ignored unless the bV5CSType member
* specifies PROFILE_LINKED or PROFILE_EMBEDDED.
*
* @return
*/
public Long getProfileData() {
if(getVersion() == InfoHeaderVersion.V0 ||
getVersion() == InfoHeaderVersion.V4)
return null;
else
return null;
}
/**
* Size, in bytes, of embedded profile data.
*
* @return
*/
public Long getProfileSize() {
if(getVersion() == InfoHeaderVersion.V0 ||
getVersion() == InfoHeaderVersion.V4)
return null;
else
return null;
}
public String toString() {
return "TODO: implement InfoHeader.toString() method.\n" +
Arrays.toString(data);
}
}
/**
* The CIEXYZ class represents the x, y, and z coordinates
* of a specific color in a specified color space.
*
*/
public class CIEXYZ {
/**
* The x chromaticity value.
*/
private FixedPoint2Dot30 x;
/**
* The y chromaticity value.
*/
private FixedPoint2Dot30 y;
/**
* The z chromaticity value.
*/
private FixedPoint2Dot30 z;
/**
* @param value
* @return
*/
public static CIEXYZ valueOf(byte[] value) {
CIEXYZ ret = new CIEXYZ();
return ret;
}
/**
* @param value
* @return
*/
public static CIEXYZ valueOf(String value) {
CIEXYZ ret = new CIEXYZ();
return ret;
}
/**
* @return
*/
public byte[] write() {
byte[] ret = new byte[12];
return ret;
}
/**
*
*/
public String toString() {
return null;
}
}
/**
* The CIEXYZTRIPLE class represents the x, y, and z coordinates
* of the three colors that correspond to the red, green, and blue
* endpoints for a specified logical color space.
*/
public class CIEXYZTriple {
/**
* The xyz coordinates of the red endpoint.
*/
private CIEXYZ red;
/**
* The xyz coordinates of the green endpoint.
*/
private CIEXYZ green;
/**
* The xyz coordinates of the blue endpoint.
*/
private CIEXYZ blue;
/**
* @param value
* @return
*/
public static CIEXYZTriple valueOf(byte[] value) {
CIEXYZTriple ret = new CIEXYZTriple();
return ret;
}
/**
* @param value
* @return
*/
public static CIEXYZTriple valueOf(String value) {
CIEXYZTriple ret = new CIEXYZTriple();
return ret;
}
/**
* @return
*/
public byte[] write() {
byte[] ret = new byte[12];
return ret;
}
/**
* @return
*/
public String toString() {
return null;
}
}
/**
*
*/
public class FixedPoint2Dot30 {
/**
*
*/
private BigDecimal value =
BigDecimal.valueOf(0.0);
/**
* @param value
* @return
*/
public static FixedPoint2Dot30 valueOf(byte[] value) {
FixedPoint2Dot30 ret = new FixedPoint2Dot30();
return ret;
}
/**
* @param value
* @return
*/
public static FixedPoint2Dot30 valueOf(String value) {
FixedPoint2Dot30 ret = new FixedPoint2Dot30();
return ret;
}
/**
* @param value
* @return
*/
public static FixedPoint2Dot30 valueOf(BigDecimal value) {
FixedPoint2Dot30 ret = new FixedPoint2Dot30();
return ret;
}
/**
* @return
*/
public byte[] write() {
byte[] ret = new byte[4];
return ret;
}
/**
* @return
*/
public String toString() {
return null;
}
}
/**
*
*/
public class FixedPoint8Dot8 {
/**
*
*/
private BigDecimal value =
BigDecimal.valueOf(0.0);
/**
* @param value
* @return
*/
public static FixedPoint8Dot8 valueOf(byte[] value) {
FixedPoint8Dot8 ret = new FixedPoint8Dot8();
return ret;
}
/**
* @param value
* @return
*/
public static FixedPoint8Dot8 valueOf(String value) {
FixedPoint8Dot8 ret = new FixedPoint8Dot8();
return ret;
}
/**
* @param value
* @return
*/
public static FixedPoint8Dot8 valueOf(BigDecimal value) {
FixedPoint8Dot8 ret = new FixedPoint8Dot8();
return ret;
}
/**
* @return
*/
public byte[] write() {
byte[] ret = new byte[4];
return ret;
}
/**
* @return
*/
public String toString() {
return null;
}
}
/**
* Specifies the version of thid DIB header.
*/
public enum InfoHeaderVersion {
/**
* Equates to a
* <a
href="http://msdn2.microsoft.com/en-us/library/ms532290(VS.85).aspx">
* BITMAPINFOHEADER</a>
*/
V0,
/**
* Equates to a
* <a
href="http://msdn2.microsoft.com/en-us/library/ms532300(VS.85).aspx">
* BITMAPV4HEADER</a>
*/
V4,
/**
* Equates to a
* <a
href="http://msdn2.microsoft.com/en-us/library/ms532331(VS.85).aspx">
* BITMAPV5HEADER</a>
*/
V5;
}
/**
* Windows supports formats for compressing bitmaps that
* define their colors with 8 or 4 bits-per-pixel.
* Compression reduces the disk and memory storage
* required for the bitmap.
*/
public enum Compression
{
/**
* An uncompressed format.
*/
BI_RGB(0x0000),
/**
* A run-length encoded (RLE) format for bitmaps with 8 bpp.
* The compression format is a two-byte format consisting of
* a count byte followed by a byte containing a color index.
* If bV5Compression is BI_RGB and the bV5BitCount member is
* 16, 24, or 32, the bitmap array specifies the actual intensities
* of blue, green, and red rather than using color table indexes.
* For more information, see Bitmap Compression.
*/
BI_RLE8(0x0001),
/**
* An RLE format for bitmaps with 4 bpp.
* The compression format is a two-byte format consisting of a
* count byte followed by two word-length color indexes.
* For more information, see Bitmap Compression.
*/
BI_RLE4(0x0002),
/**
* Specifies that the bitmap is not compressed and that the
* color table consists of three DWORD color masks that specify the
* red, green, and blue components of each pixel.
* Valid when used with 16- and 32-bpp bitmaps.
*/
BI_BITFIELDS(0x0003),
/**
* Specifies that the image is compressed using the JPEG
* file Interchange Format.
* JPEG compression trades off compression against loss;
* it can achieve a compression ratio of 20:1 with little
* noticeable loss.
*/
BI_JPEG(0x0004),
/**
* Specifies that the image is compressed using the
* PNG file Interchange Format.
*/
BI_PNG(0x0005);
private int value;
private Compression(int value) {
this.value = value;
}
}
/**
* Specifies the relationship between
* logical and physical colors.
*/
public enum GamutMappingIntent
{
/**
* Maintains the white point.
* Matches the colors to their nearest
* color in the destination gamut.
*/
LCS_GM_ABS_COLORIMETRIC(0x00000008,
"Match", "Absolute Colorimetric"),
/**
* Maintains saturation.
* Used for business charts and other situations
* in which undithered colors are required.
*/
LCS_GM_BUSINESS(0x00000001, "Graphic", "Saturation"),
/**
* Maintains colorimetric match.
* Used for graphic designs and named colors.
*/
LCS_GM_GRAPHICS(0x00000002, "Proof",
"Relative Colorimetric"),
/**
* Maintains contrast.
* Used for photographs and natural images.
*/
LCS_GM_IMAGES(0x00000004, "Picture", "Perceptual");
private long value;
private String intent;
private String iccName;
/**
* @param value
* @param intent
* @param iccName
*/
private GamutMappingIntent(long value,
String intent, String iccName) {
this.value = value;
this.intent = intent;
this.iccName = iccName;
}
/**
* @return
*/
public long getValue() {
return value;
}
/**
* @return
*/
public String getIntent() {
return intent;
}
/**
* @return
*/
public String getICCName() {
return iccName;
}
}
/**
* Specifies where to find color profile information for this DIB.
*/
public enum LogicalColorSpace
{
/**
* Indicates that endpoints and
* gamma values are given in the appropriate fields.
*/
LCS_CALIBRATED_RGB(0x00000000),
/**
* Indicates that the bitmap is in sRGB color space.
*/
LCS_sRGB(0x73524742),
/**
* Indicates that the bitmap is in
* the system default color space, sRGB.
*/
LCS_WINDOWS_COLOR_SPACE(0x57696E20),
/**
* Indicates that bV5ProfileData points to the
* file name of the profile to use
* (gamma and endpoints values are ignored).
*/
LCS_PROFILE_LINKED(0x4C494E4B),
/**
* This value indicates that bV5ProfileData points to a
* memory buffer that contains the profile to be used
* (gamma and endpoints values are ignored).
*/
LCS_PROFILE_EMBEDDED(0x4D424544);
private long value;
private LogicalColorSpace(long value) {
this.value = value;
}
}