Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: I am in problem with my code

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post I am in problem with my code

    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");
    }
    }


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: I am in problem with my code

    Please, have some empathy. How would you feel if someone dumped their code onto a file and told you to help without giving any pointers or help whatsoever?

    If you want anyone to even attempt helping you, please give some pointers. What doesn't work? Does the code not compile or give errors during runs? What part of the conversion is stumping you?

    I tried to search the internet (which is your friend) for helping on RGB to grayscale conversions. Maybe you could look at this.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am in problem with my code

    why u dont live contact the expert one...?but,u have to pay $0.5 per minutes...

Similar Threads

  1. PROBLEM WITH MY CODE!!!!
    By vaughn00AoG in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 26th, 2012, 10:28 AM
  2. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  3. Problem with my jsp code .Need Help Please!!!
    By ur2cdanger in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: October 22nd, 2011, 01:10 AM
  4. Problem with my jsp code
    By ur2cdanger in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: October 16th, 2011, 01:12 AM
  5. Problem with the code
    By noFear in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 9th, 2010, 10:28 AM

Tags for this Thread