here is some java code to read a bmp file
/**
* Handles dealing with windows bitmap files. This class doesn't handle palettized files.
* +--------------------------------------+
* | Bitmap File Header |
* +--------------------------------------+
* | Bitmap Information Header |
* +--------------------------------------+
* | Palette Data (only in 8 bit files) |
* +--------------------------------------+
* | Bitmap Data |
* +--------------------------------------+
*
* @author Scott Shaver
*/
public class WindowsBitmapFile implements com.xith3d.imaging.ImageFile {
private int FHsize = 0;
private short FHreserved1 = 0;
private short FHreserved2 = 0;
private int FHoffsetBits = 0;
private int IHsize = 0;
private int IHwidth = 0;
private int IHheight = 0;
private short IHplanes = 0;
private short IHbitCount = 0;
private int IHcompression = 0;
private int IHsizeImage = 0;
private long IHxpelsPerMeter = 0;
private long IHypelsPerMeter = 0;
private int IHcolorsUsed = 0;
private int IHcolorsImportant = 0;
private int filePointer = 0;
private byte[] fileContents = null;
private byte[] data = null;
public WindowsBitmapFile() {
}
public byte[] getData() {
return data;
}
public int getWidth() {
return IHwidth;
}
public int getHeight() {
return IHheight;
}
public int getBPP() {
return IHbitCount;
}
public int getDataLength() {
return data.length;
}
public static BufferedImage getBufferedImage(String filename){
WindowsBitmapFile loader = new WindowsBitmapFile();
loader.load(filename);
int width = loader.getWidth(),
height = loader.getHeight();
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
byte[] imageData = loader.getData();
for(int j = height - 1; j >= 0; j--)
for(int i = 0; i < width; i++) {
int index = ((height - 1 - j)* width + i) * 3,
color= ( 255 & 0xFF) << 24|
(imageData[index + 2] & 0xFF) << 16|
(imageData[index + 1] & 0xFF) << 8|
(imageData[index + 0] & 0xFF);
bufferedImage.setRGB(i,j, color);
}
return bufferedImage;
}
public void printHeaders() {
System.out.println("-----------------------------------");
System.out.println("File Header");
System.out.println("-----------------------------------");
System.out.println(" File Size:"+FHsize);
System.out.println(" Reserved 1:"+FHreserved1);
System.out.println(" Reserved 2:"+FHreserved2);
System.out.println(" Data offset:"+FHoffsetBits);
System.out.println("-----------------------------------");
System.out.println("Info Header");
System.out.println("-----------------------------------");
System.out.println(" Info Header Size:"+IHsize);
System.out.println(" Width:"+IHwidth);
System.out.println(" Height:"+IHheight);
System.out.println(" Planes:"+IHplanes);
System.out.println(" BPP:"+IHbitCount);
System.out.println(" Compression:"+IHcompression);
System.out.println(" Image size:"+IHsizeImage);
System.out.println(" Pels Per Meter X:"+IHxpelsPerMeter);
System.out.println(" Pels Per Meter Y:"+IHypelsPerMeter);
System.out.println(" # of Colors Used:"+IHcolorsUsed);
System.out.println("# of Important Colors:"+IHcolorsImportant);
}
public void load(String filename) {
// reset everything
FHsize = 0;
FHreserved1 = 0;
FHreserved2 = 0;
FHoffsetBits = 0;
IHsize = 0;
IHwidth = 0;
IHheight = 0;
IHplanes = 0;
IHbitCount = 0;
IHcompression = 0;
IHsizeImage = 0;
IHxpelsPerMeter = 0;
IHypelsPerMeter = 0;
IHcolorsUsed = 0;
IHcolorsImportant = 0;
filePointer = 0;
InputStream dis = ClassLoader.getSystemResourceAsStream(filename);
try
{
if( dis == null)
dis = new FileInputStream(filename);
fileContents = new byte[dis.available()];
dis.read(fileContents);
try{dis.close();}catch(Exception x){}
short magicNumber = readShort();
// FHtype
// make sure it's windows bitmap file
if(magicNumber != 19778)
{
fileContents = null;
return;
}
// read the file header
FHsize = readInt();
FHreserved1 = readShort();
FHreserved2 = readShort();
FHoffsetBits = readInt();
// read the info header
IHsize = readInt();
IHwidth = readInt();
IHheight = readInt();
IHplanes = readShort();
IHbitCount = readShort();
IHcompression = readInt();
IHsizeImage = readInt();
IHxpelsPerMeter = readInt();
IHypelsPerMeter = readInt();
IHcolorsUsed = readInt();
IHcolorsImportant = readInt();
// allocate memory for the pixel data
data = new byte[IHsizeImage];
System.arraycopy(fileContents, FHoffsetBits, data, 0, IHsizeImage);
fileContents = null;
// swap the R and B values to get RGB, bitmap color format is BGR
for(int loop=0;loop<IHsizeImage;loop+=3)
{
byte btemp = data[loop];
data[loop] = data[loop+2];
data[loop+2] = btemp;
}
}
catch(Exception x)
{
x.printStackTrace();
System.out.println(x.getMessage());
}
}
private short readShort(){
int s1 = (fileContents[filePointer++] & 0xFF),
s2 = (fileContents[filePointer++] & 0xFF) << 8;
return ((short)(s1 | s2));
}
private int readInt(){
return (fileContents[filePointer++] & 0xFF) |
(fileContents[filePointer++] & 0xFF) << 8|
(fileContents[filePointer++] & 0xFF) << 16|
(fileContents[filePointer++] & 0xFF) << 24;
}
public static void main(String[] args) {
WindowsBitmapFile bf = new WindowsBitmapFile();
bf.load(args[0]);
bf.printHeaders();
}
}
here is a little snippet of blitz code reading a binary tilemap file.
Function Load:LayeredTileMap(filename:String)
Local majorVer=1
Local MinorVer=0
Local newmap:LayeredTileMap = Null
Local in:TStream = ReadStream(filename)
If Not in RuntimeError "Failed to open a ReadStream to file "+filename
Local mfs:TStream = LittleEndianStream(in)
' read the file header
Local fileid:String = Null
fileid = ReadString(mfs,6)
If fileid <> "SASMEF" RuntimeError filename+" is not an SAS Map Editor map file."
majorVer = ReadInt(mfs)
minorVer = ReadInt(mfs)
' read version 1.0 of the map format
If majorVer=1 And minorVer=0
' read the map header
Local mapid:String = Null
mapid = ReadString(mfs,6)
If mapid<>"SASMHD" RuntimeError filename+" is corrupt, unable to locate the SASMHD block."
Local titleLen:Int = ReadInt(mfs)
Local title:String = ReadString(mfs,titleLen)
Local editLen:Int = ReadInt(mfs)
Local edit:String = ReadString(mfs,editLen)
Local creatorLen:Int = ReadInt(mfs)
Local creator:String = ReadString(mfs,creatorLen)
Local layerCount:Int = ReadInt(mfs)
Local mcw:Int = ReadInt(mfs)
Local mch:Int = ReadInt(mfs)
' Read Tile Image
Local nameLen:Int = ReadInt(mfs)
Local name:String = ReadString(mfs,nameLen)
Local frames:Int = ReadInt(mfs)
Local cpw:Int = ReadInt(mfs)
Local cph:Int = ReadInt(mfs)
Local index:Int = filename.FindLast("/")
Local path:String = ""
If index<>-1 Then path=filename[..index]
Local TileImage:TImage = LoadAnimImage(path+"/"+name,cpw,cph,0,frames,FILTEREDIMAGE|MASKEDIMAGE)
' create the new map object
newmap = LayeredTileMap.Create(TileImage,frames,layerCount,mcw,mch,cpw,cph)
newmap.imageName = name
newmap.mapTitle = title
newmap.mapCreator = creator
newmap.mapLastEdit = edit
newmap.imageTileCount = frames
' read in layers
For Local l=0 Until layerCount
' read layer header
Local layerid:String = Null
layerid = ReadString(mfs,6)
If layerid<>"SASLHD" RuntimeError filename+" is corrupt, unable to locate the SASLHD block for layer "+l+"."
Local px:Int = ReadInt(mfs)
Local py:Int = ReadInt(mfs)
newmap.CreateLayer(l,px,py)
' this is a speed optimization, istead of reading each int one at a time
' we read them in in blocks, much faster but we have to deal with the endian stuff
' ourselves
Local temp:Int[] = New Int[mcw]
Local bptr:Byte Ptr = Varptr temp[0]
' cell tile index data
For Local y:Int=0 Until mch
'FlushMem
mfs.ReadBytes( bptr,mcw*4 )
For Local x:Int=0 Until mcw
?BigEndian
Local t ' only do the byte swapping if on a big endian system, file is little endian
t=bptr[(x*4)];bptr[(x*4)]=bptr[(x*4)+3];bptr[(x*4)+3]=t
t=bptr[1];bptr[(x*4)+1]=bptr[(x*4)+2];bptr[(x*4)+2]=t
?
newmap.layers[l].tiles[x,y]=temp[x]
Next
Next
Local btemp:Byte[] = New Byte[mcw]
bptr = Varptr btemp[0]
' cell alpha data
For Local y:Int=0 Until mch
'FlushMem
mfs.ReadBytes( bptr,mcw )
For Local x:Int=0 Until mcw
newmap.layers[l].alpha[x,y]=btemp[x]
Next
Next
Next
ElseIf majorVer=1 And minorVer=1
' read the map header
Local mapid:String = Null
mapid = ReadString(mfs,6)
If mapid<>"SASMHD" RuntimeError filename+" is corrupt, unable to locate the SASMHD block."
Local titleLen:Int = ReadInt(mfs)
Local title:String = ReadString(mfs,titleLen)
Local editLen:Int = ReadInt(mfs)
Local edit:String = ReadString(mfs,editLen)
Local creatorLen:Int = ReadInt(mfs)
Local creator:String = ReadString(mfs,creatorLen)
Local layerCount:Int = ReadInt(mfs)
Local mcw:Int = ReadInt(mfs)
Local mch:Int = ReadInt(mfs)
' Read Tile Image
Local nameLen:Int = ReadInt(mfs)
Local name:String = ReadString(mfs,nameLen)
Local frames:Int = ReadInt(mfs)
Local cpw:Int = ReadInt(mfs)
Local cph:Int = ReadInt(mfs)
Local transRed:Int = ReadInt(mfs)
Local transGreen:Int = ReadInt(mfs)
Local transBlue:Int = ReadInt(mfs)
Local index:Int = filename.FindLast("/")
Local path:String = ""
If index<>-1 Then path=filename[..index]
SetMaskColor(transRed,transGreen,transBlue)
Local TileImage:TImage = LoadAnimImage(path+"/"+name,cpw,cph,0,frames,FILTEREDIMAGE|MASKEDIMAGE)
' create the new map object
newmap = LayeredTileMap.Create(TileImage,frames,layerCount,mcw,mch,cpw,cph)
newmap.imageName = name
newmap.mapTitle = title
newmap.mapCreator = creator
newmap.mapLastEdit = edit
newmap.imageTileCount = frames
newmap.imageTransRed = transRed
newmap.imageTransGreen = transGreen
newmap.imageTransBlue = transBlue
' read in layers
For Local l=0 Until layerCount
' read layer header
Local layerid:String = Null
layerid = ReadString(mfs,6)
If layerid<>"SASLHD" RuntimeError filename+" is corrupt, unable to locate the SASLHD block for layer "+l+"."
Local px:Int = ReadInt(mfs)
Local py:Int = ReadInt(mfs)
newmap.CreateLayer(l,px,py)
' this is a speed optimization, istead of reading each int one at a time
' we read them in in blocks, much faster but we have to deal with the endian stuff
' ourselves
Local temp:Int[] = New Int[mcw]
Local bptr:Byte Ptr = Varptr temp[0]
' cell tile index data
For Local y:Int=0 Until mch
'FlushMem
mfs.ReadBytes( bptr,mcw*4 )
For Local x:Int=0 Until mcw
?BigEndian
Local t ' only do the byte swapping if on a big endian system, file is little endian
t=bptr[(x*4)];bptr[(x*4)]=bptr[(x*4)+3];bptr[(x*4)+3]=t
t=bptr[1];bptr[(x*4)+1]=bptr[(x*4)+2];bptr[(x*4)+2]=t
?
newmap.layers[l].tiles[x,y]=temp[x]
Next
Next
Local btemp:Byte[] = New Byte[mcw]
bptr = Varptr btemp[0]
' cell alpha data
For Local y:Int=0 Until mch
'FlushMem
mfs.ReadBytes( bptr,mcw )
For Local x:Int=0 Until mcw
newmap.layers[l].alpha[x,y]=btemp[x]
Next
Next
Next
Else
RuntimeError filename+" has an unknown version of "+majorVer+"."+minorVer
EndIf
CloseStream in
Return newmap
End Function
together they should give you an idea of how to go about it.