Problem loading BMP files

I got an array size with negative bounds exception every time I tried to load a 24bit BMP file into JME.



There was an uninitialized variable for the size of the file in BitmapHeader.readMap24. This turned out to be because BMP files, that are uncompressed, do not have to specify a size of image in the header as it can be worked out from the file size. Sometimes they have the field set, sometimes they don’t.



Anyway, a small modification to the BitmapHeader class fixes it as shown below.


    /**
     * Reads in a bitmap header from the byte[] and chops it up into BitemapHeader variables
     * @param data The byte[] to read.
     */
    public final void read(byte[] data){
        final int bflen = 14;
        byte bf[] = new byte[bflen];
        for(int i = 0; i < bf.length; i++) {
            bf[i] = data[i];
        }
        final int bilen = 40;
        byte bi[] = new byte[bilen];
        for(int i = 0; i < bi.length; i++) {
            bi[i] = data[i + 14];
        }

        size = constructInt(bf, 2);
        bisize = constructInt(bi, 2);
        width = constructInt(bi, 4);
        height = constructInt(bi, 8);
        planes = constructShort(bi, 12);
        bitcount = constructShort(bi, 14);
        compression = constructInt(bi, 16);
        sizeimage = constructInt(bi, 20) == 0 ? size - constructInt(bf,10) : constructInt(bi, 20); 
        xpm = constructInt(bi, 24);
        ypm = constructInt(bi, 28);
        clrused = constructInt(bi, 32);
        clrimp = constructInt(bi, 36);

    }
}

Thanks for the tip… The only line you changed was:


        sizeimage = constructInt(bi, 20) == 0 ? size - constructInt(bf,10) : constructInt(bi, 20);



right? I've added this to cvs.

Yep, that was it.