Basic Objective
A windows BMP file is a common image format that Java does not handle. While BMP images are used only on windows machines, they are reasonably common. Reading these shows how to read complex structures in Java and
how to alter they byte order from the big endian order used by Java to the little endian order used by the windows and the intel processor.
--------------------------------------------------------
//
//This code was taken and cleaned up from a
//Javaworld tips and tricks column
//
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.MemoryImageSource;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
//
//really just a collection of methods to read a BMP file
//
public class BMPLoader
{
// build an int from a byte array - convert little to big endian
public static int constructInt( byte[] in, int offset) {
int ret = (( int) in[offset + 3] & 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);
}
// build an int from a byte array - convert little to big endian
// set high order bytes to 0xfff
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);
}
// build an int from a byte array - convert little to big endian
public static long constructLong( byte[] in, int offset) {
long ret = (( long) in[offset + 7] & 0xff);
ret |= (ret << 8) | (( long) in[offset + 6] & 0xff);
ret |= (ret << 8) | (( long) in[offset + 5] & 0xff);
ret |= (ret << 8) | (( long) in[offset + 4] & 0xff);
ret |= (ret << 8) | (( long) in[offset + 3] & 0xff);
ret |= (ret << 8) | (( long) in[offset + 2] & 0xff);
ret |= (ret << 8) | (( long) in[offset + 1] & 0xff);
ret |= (ret << 8) | (( long) in[offset + 0] & 0xff);
return (ret);
}
// build an double from a byte array - convert little to big endian
public static double constructDouble( byte[] in, int offset) {
long ret = constructLong(in, offset);
return</
A windows BMP file is a common image format that Java does not handle. While BMP images are used only on windows machines, they are reasonably common. Reading these shows how to read complex structures in Java and
how to alter they byte order from the big endian order used by Java to the little endian order used by the windows and the intel processor.
--------------------------------------------------------
//
//This code was taken and cleaned up from a
//Javaworld tips and tricks column
//
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.MemoryImageSource;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
//
//really just a collection of methods to read a BMP file
//
public class BMPLoader
{
// build an int from a byte array - convert little to big endian
public static int constructInt( byte[] in, int offset) {
int ret = (( int) in[offset + 3] & 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);
}
// build an int from a byte array - convert little to big endian
// set high order bytes to 0xfff
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);
}
// build an int from a byte array - convert little to big endian
public static long constructLong( byte[] in, int offset) {
long ret = (( long) in[offset + 7] & 0xff);
ret |= (ret << 8) | (( long) in[offset + 6] & 0xff);
ret |= (ret << 8) | (( long) in[offset + 5] & 0xff);
ret |= (ret << 8) | (( long) in[offset + 4] & 0xff);
ret |= (ret << 8) | (( long) in[offset + 3] & 0xff);
ret |= (ret << 8) | (( long) in[offset + 2] & 0xff);
ret |= (ret << 8) | (( long) in[offset + 1] & 0xff);
ret |= (ret << 8) | (( long) in[offset + 0] & 0xff);
return (ret);
}
// build an double from a byte array - convert little to big endian
public static double constructDouble( byte[] in, int offset) {
long ret = constructLong(in, offset);
return</