Re: Getting header info of a BMP file
On Apr 5, 9:36 pm, "Jeff Higgins" <oohigg...@yahoo.com> wrote:
mmcc...@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...
read more =BB
Thanks everyone for your bits and pieces. Here is what I have so far
and yet still having troubles. I'm at the part now when I want to
output the contents of the colour table to the screen.
Here is class one:
import java.awt.*;
import java.io.*;
import java.awt.image.*;
public class CSE390Lab3BMPMcClafferty extends Component {
//--- Private constants
private final static int BITMAPFILEHEADER_SIZE = 14;
private final static int BITMAPINFOHEADER_SIZE = 40;
//--- Private variable declaration
//--- Bitmap file header
byte bitmapFileHeader [] = new byte [14];
byte bfType [] = {'B', 'M'};
int bfSize = 0;
int bfReserved1 = 0;
int bfReserved2 = 0;
int bfOffBits = BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_SIZE;
//--- Bitmap info header
byte bitmapInfoHeader [] = new byte [40];
int biSize = BITMAPINFOHEADER_SIZE;
int biWidth = 0;
int biHeight = 0;
int biPlanes = 1;
int biBitCount = 24;
int biCompression = 0;
int biSizeImage = 0x030000;
int biXPelsPerMeter = 0x0;
int biYPelsPerMeter = 0x0;
int biClrUsed = 0;
int biClrImportant = 0;
byte optionalPalette[];
byte imageData []= new byte [4];
//--- Bitmap raw data
private int bitmap [];
//--- File section
private FileOutputStream fo;
//--- Default constructor
public CSE390Lab3BMPMcClafferty() {}
public void displayFileHeader ()
{
System.out.println ("FILE HEADER");
System.out.println ("Type: " + bfType);
System.out.println ("Size: " + bfSize);
System.out.println ("Reserved 1: " + bfReserved1);
System.out.println ("Reserved 2: " + bfReserved2);
System.out.println ("Off Bits: " + bfOffBits);
}
public void displayInfoHeader ()
{
System.out.println ("INFO HEADER");
System.out.println ("Size of info header: " + biSize);
System.out.println ("Width: " + biWidth);
System.out.println ("Height: " + biHeight);
System.out.println ("Planes: " +biPlanes);
System.out.println ("Bit Count:" +biBitCount);
System.out.println ("Compression: "+ biCompression);
System.out.println ("Size: " + biSizeImage);
System.out.println ("Pixels per metre horizontally: " +
biXPelsPerMeter);
System.out.println ("Pixels per metre vertically: " +
biYPelsPerMeter);
System.out.println ("Number of colours used: " + biClrUsed);
System.out.println ("Important Colours: " + biClrImportant);
}
public void displayColourTable()
{
System.out.println ("Colour Table: ");
}
public void displayImageData(int row, int column, int height, int
width)
{
}
}
Class 2:
// The "CSE390Lab3McClafferty" class.
import java.io.*;
import java.lang.*;
public class CSE390Lab3McClafferty
{
public static int constructInt3 (byte[] in, int offset)
{
int ret = 0xff;
ret = (ret << 8) | ((int) in [offset + 2] & 0xff);
ret = (ret << 8) | ((int) in [offset + 1] & 0xff);
ret = (ret << 8) | ((int) in [offset + 0] & 0xff);
return (ret);
}
public static void main (String[] args)
{
CSE390Lab3BMPMcClafferty pic = new CSE390Lab3BMPMcClafferty
();
int nNumColors = 0;
try
{
File bmp = new File ("pic2.bmp");
FileInputStream bitStream = new FileInputStream (bmp);
bitStream.skip (2);
pic.bfSize = bitStream.read () + bitStream.read () * 256 +
bitStream.read () * 65536 + bitStream.read () * 16777216;
bitStream.skip (12);
pic.biWidth = bitStream.read () + bitStream.read () * 256
+ bitStream.read () * 65536 + bitStream.read () * 16777216;
pic.biHeight = bitStream.read () + bitStream.read () * 256
+ bitStream.read () * 65536 + bitStream.read () * 16777216;
pic.biPlanes = bitStream.read () + bitStream.read () *
256;
pic.biBitCount = bitStream.read () + bitStream.read () *
256;
pic.biCompression = bitStream.read () + bitStream.read ()
* 256 + bitStream.read () * 65536 + bitStream.read () * 16777216;
pic.biSizeImage = bitStream.read () + bitStream.read () *
256 + bitStream.read () * 65536 + bitStream.read () * 16777216;
pic.biXPelsPerMeter = bitStream.read () + bitStream.read
() * 256 + bitStream.read () * 65536 + bitStream.read () * 16777216;
pic.biYPelsPerMeter = bitStream.read () + bitStream.read
() * 256 + bitStream.read () * 65536 + bitStream.read () * 16777216;
pic.biClrUsed = bitStream.read () + bitStream.read () *
256 + bitStream.read () * 65536 + bitStream.read () * 16777216;
nNumColors = pic.biClrUsed;
pic.biClrImportant = bitStream.read () + bitStream.read ()
* 256 + bitStream.read () * 65536 + bitStream.read () * 16777216;
if (pic.biClrUsed > 0)
{
nNumColors = pic.biClrUsed;
}
else
{
nNumColors = (1 & 0xff) << pic.biBitCount;
}
pic.optionalPalette = new byte [nNumColors * 4];
int npalette[] = new int [nNumColors];
// System.out.println("The number of Colors
is"+nNumColors);
bitStream.read (pic.optionalPalette, 0, nNumColors * 4);
int nindex8 = 0;
for (int n = 0 ; n < nNumColors ; n++)
{
npalette [n] = constructInt3 (pic.optionalPalette,
nindex8);
nindex8 += 4;
}
// Read the image data (actually indices into the palette)
// Scan lines are still padded out to even 4-byte
// boundaries.
int npad8 = (pic.biSizeImage / pic.biHeight) -
pic.biWidth;
// System.out.println("nPad is:"+npad8);
int ndata8[] = new int [pic.biWidth * pic.biHeight];
byte bdata[] = new byte [(pic.biWidth + npad8) *
pic.biHeight];
bitStream.read (bdata, 0, (pic.biWidth + npad8) *
pic.biHeight);
nindex8 = 0;
for (int j8 = 0 ; j8 < pic.biHeight ; j8++)
{
for (int i8 = 0 ; i8 < pic.biWidth ; i8++)
{
ndata8 [pic.biWidth * (pic.biHeight - j8 - 1) +
i8] = npalette [((int) bdata [nindex8] & 0xff)];
nindex8++;
}
nindex8 += npad8;
}
pic.displayFileHeader ();
pic.displayInfoHeader ();
pic.displayColourTable ();
//pic.optionalPalette = bitStream.read () + bitStream.read
() * 256 + bitStream.read () * 65536 + bitStream.read () * 16777216;
/*bitStream.skip (28);
int index = bmp.toString ().lastIndexOf ("bmp");
String dFileName = bmp.toString ().substring (0, index) +
"txt";
File destFile = new File (dFileName);
FileOutputStream ostream = new FileOutputStream
(destFile);
ostream.write (new String
("{rtf1ansiansicpg1252deff0{fonttbl{f0fnilfcharset0 Courier New;}
{f1fmodernfprq1fcharset0 Courier New;}}").getBytes ());
ostream.write (0x0d);
ostream.write (0x0a);
ostream.write (new String ("{colortbl ;").getBytes ());
int red, green, blue, count = 0, dcount = 255;
for (int i = 0 ; i < 256 ; i++)
{
blue = bitStream.read ();
green = bitStream.read ();
red = bitStream.read ();
ostream.write (new String ("red" + red + "green" +
green + "blue" + blue + ";").getBytes ());
bitStream.read ();
}
ostream.write (new String ("}").getBytes ());
ostream.write (0x0d);
ostream.write (0x0a);
ostream.write (new String ("viewkind4uc1pardlang1033f0fs20
").getBytes ());
char ch[] = {'!', '#', '$', '%', '&', '*', '+', '0', '?',
'@',
'=A7', '=A5', '=A4', '=A3', '=A2', '=B5', '=B6', '=DF', '>',=
'<',
'=AA'};
int temp, colorCode = -1;
int orgWidth = width;
if (width % 4 != 0)
width = width + 4 - (width % 4);
byte allBytes[] = new byte [height * width];
bitStream.read (allBytes);
for (int j = height - 1 ; j >= 0 ; j--)
{
for (int i = 0 ; i < orgWidth ; i++)
{
temp = allBytes [(j * width) + i];
if (temp < 0)
{
temp *= -1;
temp = 128 + 128 - temp;
}
temp += 1;
if (colorCode != temp)
ostream.write (new String ("cf" + (temp) + "
").getBytes ());
ostream.write (ch [temp % 21]);
colorCode = temp;
}
ostream.write (new String ("par").getBytes ());
ostream.write (0x0d);
ostream.write (0x0a);
}
*/
}
catch (Exception e)
{
System.out.println ("Exception Thrown: " + e);
}
} // main method
// build an int from a byte array - convert little to big endian
// set high order bytes to 0xfff
} // CSE390Lab3McClafferty class
But I'm kind of lost as to how to even output the colourtable to screen