converting byte array to bmp file
i am trying to generate texture models.
i have so far created a 2x2 integer array of the texture with the values representing height or darkness.
i want to convert this array into a bmp file so that its easier to appreciate.
i have converted the array to a byte[][] array and m trying to write it to a bmp file
i used The .bmp file format to guide me about the details of the format. and tried to write all the headers etc by myself. however the file i manage to create is unreadable by windows. is there an easier way to achieve it?
Quote:
public static void convImg(int[][] arr,int n) throws IOException{
//String filePath = "D://fract//cloud1.bmp";
String filePath = "cloud1.bmp";
System.out.println("going in to create file");
//File f;
//f= new File(filePath);
/*if(f.exists()){
System.out.println("filecreated");
}*/
System.out.println("file created");
FileOutputStream fos = new FileOutputStream(filePath);
System.out.println("output stream created");
//file header
fos.write("BM".getBytes()); //to specify that the file is a bitmap file
fos.write(1352); // specifies the size of the file
fos.write(0); // should be zero by default in bmp format
fos.write(1078); // mentions the offset of the start of data from file beginning
//file info header
fos.write(40); // size of the info header
fos.write(n); //width of image in pixels
fos.write(n); //height of image in pixels
/*
byte tt; //writing the number of planes
tt=(byte)0; //of target device
fos.write(tt); //should be
fos.write(tt); // set to zero
*/
fos.write((short)0); // number of planes in target device generally set to zero
fos.write((short)8); // number of bits used per pixel
fos.write(0); // type of compression, zero means no compression
fos.write(0); //-|
fos.write(0); // |
fos.write(0); // |--> some other values that need to be set to zero
fos.write(0); // |
fos.write(0); //-|
byte[][] bb = new byte[n][n];// converting int array to byte array
bb= toByteArr(arr,n);
int i,j;
for(i=0;i<n;i++){ // writing bitmap data to file
for(j=0;j<n;j++){
fos.write(bb[i][j]);
}
}
}