At almost the lowest level, everything can be thought of as bytes. (At the absolutely lowest level is bits, but I have never seen any API that lets you write individual bits to a file before so we’ll just ignore that for now…)
A long in Java is made up of 4 bytes if I remember correctly. In C/C++ if you want to write those exact bytes to a file you do something like…
long n = 1;
fwrite(file, &n, 4);
The bytes that would get written would depend on weather the system was little-endian or big-endian. Intel/AMD chips for instance are little endian and so if you were to look at the above file in a hex editor you would see bytes like so…
Byte 0: 0x01
Byte 1: 0x00
Byte 2: 0x00
Byte 3: 0x00
On a big-endian system it would be
Byte 0: 0x00
Byte 1: 0x00
Byte 2: 0x00
Byte 3: 0x01
So now you have the problem that you are working with Java, which is strongly typed. So far as I know, you can’t just cast a long to a byte array. You can mask out the individual bytes by doing something like so…
long n = 10;
byte b0;
byte b1;
byte b2;
byte b3;
b3 = (byte)(n & 0xFF);
b2 = (byte)((n & 0xFF00) >> 8);
b1 = (byte)((n & 0xFF0000) >> 16);
b0 = (byte)((n & 0xFF000000) >> 24);
But that seems like a lot of work, so I’d look for some class that lets you do the conversion from different data types to a byte array before doing that. I would think one would have to exist.
All the stuff up to this point is mainly going to apply to writing the file format headers.
For getting the pixel data, you should be getting a byte array rather than a long array so you don’t have to do the long to byte conversions all over. RGB image formats tend to have 1 byte for each component. (e.g. if you got a byte array for 1 pixel of an RGB format, you’d have 3 bytes returned, the first R, second G, third B)
So… with all that in mind, it seems to me that writing an image file reader/writer in Java is a bit more challenging than in C/C++. If you want to attempt to do so, I’d recommend looking at a fairly simple format like TGA first. www.wotsit.org has the full specification for how the bytes should be written to a file for that and many other formats.
If you don’t feel up to trying all that yourself, there may already be libraries someone has written that you could use.
Whew… this is getting longer than I originally intended. It’s easy to see why I much prefer C/C++ to Java when it comes to low-level graphics programming…