Xeric
10-08-2007, 05:56 AM
I have to write this program that manipulates bitmap images. Right now it will take in all the header information correctly and it will correctly write the image out to a new file as well. My problem is that I need it to be able to rotate the image 180 degrees, and also to mirror the image, and I have no idea how to go about that. The way that this program is written is generally how it has to be done, so any help that doesn't require huge changes to the current code would be greatly appreciated. Thanks in advance. :thumbsup:
import java.io.*;
public class BMP {
byte bfType[];
int bfSize;
short bfReserved1;
short bfReserved2;
int bfOffBits;
int biSize;
int biWidth;
int biHeight;
short biPlanes;
short biBitCount;
int biCompression;
int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
int biClrUsed;
int biClrImportant;
byte data[];
//Constructor
public BMP(String fileName) {
try {
FileInputStream in = new FileInputStream(new File(fileName));
//BITMAPFILEHEADER
bfType = new byte[2];
in.read(bfType);
byte b4[] = new byte[4];
in.read(b4);
bfSize = byteArray2Int(b4);
byte[] b2 = new byte[2];
in.read(b2);
bfReserved1 = toShort(b2);
in.read(b2);
bfReserved2 = toShort(b2);
in.read(b4);
bfOffBits = byteArray2Int(b4);
//BITMAPINFOHEADER
in.read(b4);
biSize = byteArray2Int(b4);
in.read(b4);
biWidth = byteArray2Int(b4);
in.read(b4);
biHeight = byteArray2Int(b4);
in.read(b2);
biPlanes = toShort(b2);
in.read(b2);
biBitCount = toShort(b2);
in.read(b4);
biCompression = byteArray2Int(b4);
in.read(b4);
biSizeImage = byteArray2Int(b4);
in.read(b4);
biXPelsPerMeter = byteArray2Int(b4);
in.read(b4);
biYPelsPerMeter = byteArray2Int(b4);
in.read(b4);
biClrUsed = byteArray2Int(b4);
in.read(b4);
biClrImportant = byteArray2Int(b4);
data = new byte[biHeight * biWidth * (biBitCount / 8)];
in.read(data);
}
catch(IOException e) {
e.printStackTrace();
}
}
private byte[] toBytes(short s) {
return new byte[]{(byte)(s & 0x00FF),(byte)((s & 0xFF00)>>8)};
}
private short toShort(byte[] b) {
return (short)(b[1]<<8 | b[0]);
}
private int byteArray2Int(byte b[]){
int rtnInt;
rtnInt = 0;
rtnInt = rtnInt | (b[0] & 0x000000FF);
rtnInt = rtnInt | ((b[1] & 0x000000FF ) << 8);
rtnInt = rtnInt | ((b[2] & 0x000000FF ) << 16);
rtnInt = rtnInt | ((b[3] & 0x000000FF ) << 24);
return rtnInt;
}
private byte[] int2ByteArray(int num){
byte[] rtnArray = new byte[4];
rtnArray[0] = (byte) ((num & 0x000000FF) >> 0);
rtnArray[1] = (byte) ((num & 0x0000FF00) >> 8);
rtnArray[2] = (byte) ((num & 0x00FF0000) >> 16);
rtnArray[3] = (byte) ((num & 0xFF000000) >> 24);
return rtnArray;
}
//Rotate the bitmap 180 degrees
public void rotate180(){
}
//Mirrors the bitmap
public void mirror(){
}
//Writes the bitmap to a new file
public void write(String fileName) {
try {
FileOutputStream out = new FileOutputStream(new File(fileName));
out.write(bfType);
out.write(int2ByteArray(bfSize));
out.write(toBytes(bfReserved1));
out.write(toBytes(bfReserved2));
out.write(int2ByteArray(bfOffBits));
out.write(int2ByteArray(biSize));
out.write(int2ByteArray(biWidth));
out.write(int2ByteArray(biHeight));
out.write(toBytes(biPlanes));
out.write(toBytes(biBitCount));
out.write(int2ByteArray(biCompression));
out.write(int2ByteArray(biSizeImage));
out.write(int2ByteArray(biXPelsPerMeter));
out.write(int2ByteArray(biYPelsPerMeter));
out.write(int2ByteArray(biClrUsed));
out.write(int2ByteArray(biClrImportant));
out.write(data);
}
catch(FileNotFoundException e){
System.out.println("File Not Found.");
}
catch(IOException e){
System.out.println("Write problem.");
}
}
//Returns a string to print out the header info
public String toString(){
String str = "";
str += ("BITMAPFILEHEADER:\n");
str += ("bfType: " + new String(bfType) + "\n");
str += ("bfSize: " + bfSize + "\n");
str += ("bfReserved1: " + bfReserved1 + "\n");
str += ("bfReserved2: " + bfReserved2 + "\n");
str += ("bfOffBits: " + bfOffBits + "\n\n");
str += ("BITMAPINFOHEADER:\n");
str += ("biSize: " + biSize + "\n");
str += ("biWidth: " + biWidth + "\n");
str += ("biHeight: " + biHeight + "\n");
str += ("biPlanes: " + biPlanes + "\n");
str += ("biBitCount: " + biBitCount + "\n");
str += ("biCompression: " + biCompression + "\n");
str += ("biSizeImage: " + biSizeImage + "\n");
str += ("biXPelsPerMeter: " + biXPelsPerMeter + "\n");
str += ("biYPelsPerMeter: " + biYPelsPerMeter + "\n");
str += ("biClrUsed: " + biClrUsed + "\n");
str += ("biClrImportant: " + biClrImportant);
return str;
}
}
import java.io.*;
public class BMP {
byte bfType[];
int bfSize;
short bfReserved1;
short bfReserved2;
int bfOffBits;
int biSize;
int biWidth;
int biHeight;
short biPlanes;
short biBitCount;
int biCompression;
int biSizeImage;
int biXPelsPerMeter;
int biYPelsPerMeter;
int biClrUsed;
int biClrImportant;
byte data[];
//Constructor
public BMP(String fileName) {
try {
FileInputStream in = new FileInputStream(new File(fileName));
//BITMAPFILEHEADER
bfType = new byte[2];
in.read(bfType);
byte b4[] = new byte[4];
in.read(b4);
bfSize = byteArray2Int(b4);
byte[] b2 = new byte[2];
in.read(b2);
bfReserved1 = toShort(b2);
in.read(b2);
bfReserved2 = toShort(b2);
in.read(b4);
bfOffBits = byteArray2Int(b4);
//BITMAPINFOHEADER
in.read(b4);
biSize = byteArray2Int(b4);
in.read(b4);
biWidth = byteArray2Int(b4);
in.read(b4);
biHeight = byteArray2Int(b4);
in.read(b2);
biPlanes = toShort(b2);
in.read(b2);
biBitCount = toShort(b2);
in.read(b4);
biCompression = byteArray2Int(b4);
in.read(b4);
biSizeImage = byteArray2Int(b4);
in.read(b4);
biXPelsPerMeter = byteArray2Int(b4);
in.read(b4);
biYPelsPerMeter = byteArray2Int(b4);
in.read(b4);
biClrUsed = byteArray2Int(b4);
in.read(b4);
biClrImportant = byteArray2Int(b4);
data = new byte[biHeight * biWidth * (biBitCount / 8)];
in.read(data);
}
catch(IOException e) {
e.printStackTrace();
}
}
private byte[] toBytes(short s) {
return new byte[]{(byte)(s & 0x00FF),(byte)((s & 0xFF00)>>8)};
}
private short toShort(byte[] b) {
return (short)(b[1]<<8 | b[0]);
}
private int byteArray2Int(byte b[]){
int rtnInt;
rtnInt = 0;
rtnInt = rtnInt | (b[0] & 0x000000FF);
rtnInt = rtnInt | ((b[1] & 0x000000FF ) << 8);
rtnInt = rtnInt | ((b[2] & 0x000000FF ) << 16);
rtnInt = rtnInt | ((b[3] & 0x000000FF ) << 24);
return rtnInt;
}
private byte[] int2ByteArray(int num){
byte[] rtnArray = new byte[4];
rtnArray[0] = (byte) ((num & 0x000000FF) >> 0);
rtnArray[1] = (byte) ((num & 0x0000FF00) >> 8);
rtnArray[2] = (byte) ((num & 0x00FF0000) >> 16);
rtnArray[3] = (byte) ((num & 0xFF000000) >> 24);
return rtnArray;
}
//Rotate the bitmap 180 degrees
public void rotate180(){
}
//Mirrors the bitmap
public void mirror(){
}
//Writes the bitmap to a new file
public void write(String fileName) {
try {
FileOutputStream out = new FileOutputStream(new File(fileName));
out.write(bfType);
out.write(int2ByteArray(bfSize));
out.write(toBytes(bfReserved1));
out.write(toBytes(bfReserved2));
out.write(int2ByteArray(bfOffBits));
out.write(int2ByteArray(biSize));
out.write(int2ByteArray(biWidth));
out.write(int2ByteArray(biHeight));
out.write(toBytes(biPlanes));
out.write(toBytes(biBitCount));
out.write(int2ByteArray(biCompression));
out.write(int2ByteArray(biSizeImage));
out.write(int2ByteArray(biXPelsPerMeter));
out.write(int2ByteArray(biYPelsPerMeter));
out.write(int2ByteArray(biClrUsed));
out.write(int2ByteArray(biClrImportant));
out.write(data);
}
catch(FileNotFoundException e){
System.out.println("File Not Found.");
}
catch(IOException e){
System.out.println("Write problem.");
}
}
//Returns a string to print out the header info
public String toString(){
String str = "";
str += ("BITMAPFILEHEADER:\n");
str += ("bfType: " + new String(bfType) + "\n");
str += ("bfSize: " + bfSize + "\n");
str += ("bfReserved1: " + bfReserved1 + "\n");
str += ("bfReserved2: " + bfReserved2 + "\n");
str += ("bfOffBits: " + bfOffBits + "\n\n");
str += ("BITMAPINFOHEADER:\n");
str += ("biSize: " + biSize + "\n");
str += ("biWidth: " + biWidth + "\n");
str += ("biHeight: " + biHeight + "\n");
str += ("biPlanes: " + biPlanes + "\n");
str += ("biBitCount: " + biBitCount + "\n");
str += ("biCompression: " + biCompression + "\n");
str += ("biSizeImage: " + biSizeImage + "\n");
str += ("biXPelsPerMeter: " + biXPelsPerMeter + "\n");
str += ("biYPelsPerMeter: " + biYPelsPerMeter + "\n");
str += ("biClrUsed: " + biClrUsed + "\n");
str += ("biClrImportant: " + biClrImportant);
return str;
}
}