I want to convert RGB bmp to Gratscale bmp.
But it cant give good output.
I need help.
Can anyone help me.
My Code:
public final class AverageGrayScaleConversionInBmpImage {
private byte[] readImage = null;
private byte[] writeImage = null;
private FileOutputStream FOS;
private RandomAccessFile input = null;
private int Filesize = 0;
private byte[] ImageFilesize = null;
private int Imagesize = 0;
private byte [] ImageImagesize = null;
private int BitPerPixel = 0;
private byte[] ImageBitPerPixel = null;
private int ColorPalate = 0;
private byte[] ImageColorPalate = null;
private int ImportantColor = 0;
private byte[] ImageImportantColor = null;
private int Height = 0;
private int Width = 0;
private int OffsetBit= 54;
private int OffsetAddress= 0;
private byte[] ImageOffsetAddress = null;
private int PixelArray= 0;
private int PixelArrayIndex = OffsetBit;
public AverageGrayScaleConversionInBmpImage(String WriteImageFileName){
try {
try {
input = new RandomAccessFile("secret.bmp","r");
readImage = new byte[(int) input.length()];
input.read(readImage);
System.out.println("Read: " + readImage.length + " bytes");
} catch (FileNotFoundException ex) {
Logger.getLogger(AverageGrayScaleConversionInBmpIm age.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(AverageGrayScaleConversionInBmpIm age.class.getName()).log(Level.SEVERE, null, ex);
}
ReadInputBmpFile();
ReadInputBmpFileforEdit();
WriteBmpAllHeaderInfo();
WriteBmpAllPixelInfo();
FOS = new FileOutputStream(WriteImageFileName);
FOS.write(writeImage);
FOS.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(AverageGrayScaleConversionInBmpIm age.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(AverageGrayScaleConversionInBmpIm age.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void ReadInputBmpFile(){
for(int i = 0; i < OffsetBit; i++){
System.out.println("Read Index: ["+i+"]"+readImage[i]);
}
}
public void ReadInputBmpFileforEdit(){
Width = readImage[18] & 0xFF;
Width |= (readImage[19] & 0xFF) << 8;
Width |= (readImage[20] & 0xFF) << 16;
Width |= (readImage[21] & 0xFF) << 24;
System.out.println("Width: "+Width);
Height = readImage[22] & 0xFF;
Height |= (readImage[23] & 0xFF) << 8;
Height |= (readImage[24] & 0xFF) << 16;
Height |= (readImage[25] & 0xFF) << 24;
System.out.println("Height: "+Height);
OffsetAddress = 1078;
System.out.println("OffsetAddress: "+OffsetAddress);
PixelArray = Width * Height;
System.out.println("PixelArray: "+PixelArray);
BitPerPixel = 8;
System.out.println("BitPerPixel: "+BitPerPixel);
Filesize = OffsetBit+4*(int)Math.pow(2.0, BitPerPixel)+PixelArray;
System.out.println("Filesize: "+Filesize);
Imagesize = Width * Height;
System.out.println("Imagesize: "+Imagesize);
ColorPalate = 256;
System.out.println("ColorPalate: "+ColorPalate);
ImportantColor = 256;
System.out.println("ImportantColor: "+ImportantColor);
writeImage = new byte[Filesize];
System.out.println("writeImage Array Length: "+writeImage.length);
}
public void WriteBmpAllHeaderInfo(){
System.arraycopy(readImage, 0, writeImage, 0, OffsetBit);
ImageFilesize = intToDWord(Filesize);
System.arraycopy(ImageFilesize, 0, writeImage, 2, ImageFilesize.length);
ImageOffsetAddress = intToDWord(OffsetAddress);
System.arraycopy(ImageOffsetAddress, 0, writeImage, 10, ImageOffsetAddress.length);
ImageBitPerPixel = intToWord(BitPerPixel);
System.arraycopy(ImageBitPerPixel, 0, writeImage, 28, ImageBitPerPixel.length);
ImageImagesize = intToDWord(Imagesize);
System.arraycopy(ImageImagesize, 0, writeImage, 34, ImageImagesize.length);
ImageColorPalate = intToDWord(ColorPalate);
System.arraycopy(ImageColorPalate, 0, writeImage, 46, ImageColorPalate.length);
ImageImportantColor = intToDWord(ImportantColor);
System.arraycopy(ImageImportantColor, 0, writeImage, 50, ImageImportantColor.length);
for(int i = 0; i < OffsetBit; i++){
System.out.println("Array Index: ["+i+"] "+readImage[i]+" "+writeImage[i]);
}
}
public void WriteBmpAllPixelInfo(){
//System.out.println("Color Portion: "+(int)(readImage[54] & 0xFF)+" "+(int)(readImage[55] & 0xFF)+" "+(int)(readImage[56] & 0xFF));
for(int i = OffsetBit; i < readImage.length; i+=3,PixelArrayIndex++){
writeImage[PixelArrayIndex] = (byte)(((readImage[i] & 0xFF)+(readImage[i+1] & 0xFF)+(readImage[i+2] & 0xFF)/3));
//System.out.println("PixelArray Index: "+writeImage[PixelArrayIndex]);
}
}
private byte [] intToWord (int parValue) {
byte retValue [] = new byte [2];
retValue [0] = (byte) (parValue & 0x00FF);
retValue [1] = (byte) ((parValue >> 8) & 0x00FF);
return (retValue);
}
private byte [] intToDWord (int parValue) {
byte retValue [] = new byte [4];
retValue [0] = (byte) (parValue & 0x00FF);
retValue [1] = (byte) ((parValue >> 8) & 0x000000FF);
retValue [2] = (byte) ((parValue >> 16) & 0x000000FF);
retValue [3] = (byte) ((parValue >> 24) & 0x000000FF);
return (retValue);
}
public static void main(String[] args) {
AverageGrayScaleConversionInBmpImage HoldingObject = new AverageGrayScaleConversionInBmpImage("EditedImageS .bmp");
}
}