Jump to content
  • Advertisement
Sign in to follow this  
Anri

Getting to grips with bytes and targa files in Java

This topic is 2900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, this isn't actually a cry for help but something I just wanted to share with others who might be having trouble loading in data types from a binary file that are more than one byte.

Here was the problem I faced: Loading in a targa image file.

Here is the basic skeleton for opening a file(although could be written better no doubt)...

public void loadImageFromFile(String fileName)
{
try
{
FileInputStream theInputStream = new FileInputStream(fileName);
DataInputStream readerStream = new DataInputStream(theInputStream);

//Read in yer bytes from the file here(which we will do next...)

readerStream.close();
theInputStream.close();
}
catch( IOException e)
{
System.out.println("Error: Failed to open the file");
}
}

...so you would call this method by providing the usual string containing the name of the file you want to open. The next task was to read in the first three fields of the header of the targa file. Now all of these are fortunately a single byte in size. So here is how we would go about reading them in...

byte Field1 = readerStream.readByte();
byte Field2 = readerStream.readByte();
byte Field3 = readerStream.readByte();

...and we're done. However, we will hit a field that uses 2 bytes. One such field is located at bytes 12 and 13 - the Width of the targa image. Another difficulty is Java's lack of unsigned types, but it can be dealt with. One might think its easy to use readUnsignedShort() and simply slap it in an Integer, but its easier to read the two bytes in separately...

int partA = 0x000000FF & ((int)readerStream.readByte());
int partB = 0x000000FF & ((int)readerStream.readByte());

...so, now we have the two bytes, we need to add them together and store them in a third int variable. However, we need to do a bit of mucking about with bit-shifting. here is a method to take care of things...

public int getDoubleByteType(int a, int b)
{
return ((b << 8) | a);
}

...now, here is the gist of it - when using readUnsignedShort(), it seems to load the bytes in the order( A, B ), and store them into two bytes like that, but what we really need to do is store them as ( B, A ).

Anyway. Here is a method to load in the first 18 bytes that make up the header file of a targa file. With this method you can at least obtain the width and height of the image, which comes in handy for extracting the image data itself(but that's for another time)...

public void loadImageFromFile(String fileName)
{
try
{
FileInputStream theInputStream = new FileInputStream(fileName);
DataInputStream readerStream = new DataInputStream(theInputStream);

int[] myBytes = new int[18];

for(int i=0; i<myBytes.length; i++)
{
myBytes = 0x000000FF & ((int)readerStream.readByte()) ;
}

int width = (myBytes[13] << 8) | myBytes[12];
int height = (myBytes[15] << 8) | myBytes[14];

for(int i=0; i<myBytes.length; i++)
{
System.out.println("Byte " + i + ": " + myBytes);
}

System.out.println("\n\nWIDTH: " + width);
System.out.println("HEIGHT: " + height);

readerStream.close();
theInputStream.close();
}
catch( IOException e)
{
System.out.println("Error: Failed to open the file");
}
}

...so in the end, I hope this helps those who are reading in double-byte fields and wondering why the values are not quite right.

Share this post


Link to post
Share on other sites
Advertisement
Apache project offers a vast set of Java libraries, one of them deals with common stuff, appropriately called Apache Commons (home page).

Apache is to Java what Boost is to C++. It should be the first step when trying to solve a known problem.

Endianess.

Share this post


Link to post
Share on other sites
And bugger my old boots its got a swapDouble() method! That's brilliant! ^_^

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!