Results 1 to 3 of 3
  1. #1
    cbr400aero is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Arrow Read a bitmap byte by byte

    Hi, I'm trying to load and view a bitmap from a byte arrray. It gets all grey and displays a warp to the right...

    It should be quite straightforward. I "read" the BFH and BIH in the BufferedInputStream. bwc just reads the BufferedInputStream.

    Java Code:
    pixelData = bwc.read(getImageDataSize());
    
            int pixelStride = 3;         
            int scanlineStride = 3 * getBMWidth(); 
            int[] bandOffsets = new int[]{2,1,0};
            
    
            DataBufferByte dataBuffer = new DataBufferByte(pixelData,
                    getImageDataSize());
            
            ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    
            ColorModel colorModel = new ComponentColorModel(cs, new int[] { 8, 8, 8}, false,
                    false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    
            WritableRaster raster = Raster.createInterleavedRaster(dataBuffer,
                    getBMWidth(), getBMHeight(),
                    scanlineStride, pixelStride,
                   bandOffsets, new Point(0,0));
    
            imageBits = new BufferedImage(colorModel, raster, false, null);
    Does anyone know how to display the bitmap correctly?

    Thanks.

  2. #2
    cbr400aero is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    More code:

    Java Code:
    try{
                bfHeader = new BitmapFileHeader();
                infoSize = bfHeader.bfOffBits - (long)BITMAP_FILE_HEADER_SIZE;
            } catch(Exception e){
                System.out.println("BitmapFileHeader loading failed - " + e.getMessage());
            }
    
            try{
                biHeader = new BitmapInfo();
            } catch(Exception e){
                System.out.println("BitmapFileHeader loading failed - " + e.getMessage());
            }
        
            try{
                in = new BufferedInputStream(new FileInputStream(filename));
            } catch (FileNotFoundException fnfe){
                    System.out.println(fnfe.getMessage() + "...");
            }
    
            bwc = new BitWiseCast(in);
            
            for(int i = 0;i < 2; i++){
                try{
                    bfHeader.bfType[i] = (char)in.read();
                } catch(Exception e){
                    System.out.println(e.getMessage() + " failed to read file.");
                    System.out.println("at char" + i);
                }
            }
    
            if(bfHeader.bfType[0]=='B'&&bfHeader.bfType[1]=='M'){
                System.out.println("We have a Bitmap!");
            } else {
                System.out.println("Opened file is not a bitmap.");
                System.out.println("First two chars not 'BM' - not a bitmap.");
            }
    
            try{
                bfHeader.bfSize = bwc.readUnsignedInt();
                bfHeader.bfReserved1 = bwc.readUnsignedShort();
                bfHeader.bfReserved2 = bwc.readUnsignedShort();
                bfHeader.bfOffBits = bwc.readUnsignedInt();         
                biHeader.biSize = bwc.readUnsignedInt();
                biHeader.biWidth = bwc.readInt();
                biHeader.biHeight = bwc.readInt();      
                biHeader.biPlanes = bwc.readUnsignedShort();
                biHeader.biBitCount = bwc.readUnsignedShort();
                biHeader.biCompression = bwc.readUnsignedInt();
                biHeader.biSizeImage = bwc.readUnsignedInt();
                biHeader.biXPelsPerMeter = bwc.readInt();
                biHeader.biYPelsPerMeter = bwc.readInt();
                biHeader.biClrUsed = bwc.readUnsignedInt();
                biHeader.biClrImportant = bwc.readUnsignedInt();
    
            } catch(Exception e){
                System.out.println(e.getMessage() + " reading fileheader failed.");
            }
    
            
    
            pixelData = bwc.read(getImageDataSize());

  3. #3
    cbr400aero is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Talking into myself....

    bla bla bla


    I've figured it out......

    should be:
    Java Code:
    int scanlineStride = 3 * getBMWidth(); +1
    array thingy. Now for a smilie: :cool:

Similar Threads

  1. byte[]
    By bobo67 in forum New To Java
    Replies: 10
    Last Post: 09-11-2010, 03:06 PM
  2. Replies: 1
    Last Post: 04-21-2010, 03:28 AM
  3. write/read two byte arrays to one file- help!
    By leshep in forum New To Java
    Replies: 2
    Last Post: 11-27-2009, 11:10 AM
  4. BufferedImage to Byte
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-22-2008, 09:17 PM
  5. int to byte
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 08:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •