Java EOFException when using BMPImageReader after performing RLE compression on Byte Array

608 views Asked by At

I'm getting an EOFException when trying to compress a Bitmap image using RLE. I basically take a bytearray of a bitmap image, then perform RLE compression on everything after the 54th Byte since that is all Header File stuff. I then use BMPImageReader to read in the new Byte Array and then BMPImageWriter to save it as a new RLE8 compressed bitmap.

However in the section

        File file = new File("runLength.txt");
        ... 
        BMPImageWriteParam imageWriteParam = new BMPImageWriteParam();
        imageWriteParam.setCompressionMode(2);
        imageWriteParam.setCompressionType("BI_RLE8");
        imageWriteParam.setTopDown(true);
        BMPImageWriter bmpImageWriter = (BMPImageWriter) ImageIO.getImageWritersByFormatName("bmp").next();                   
        FileImageOutputStream output = new FileImageOutputStream(file);            
        bmpImageWriter.setOutput(output);            


        BMPImageReader bmpImageReader = (BMPImageReader) ImageIO.getImageReader(bmpImageWriter);
        ImageInputStream iis = ImageIO.createImageInputStream(bais);
        bmpImageReader.setInput(iis);
        ImageReadParam param = bmpImageReader.getDefaultReadParam();
        imageRLE = bmpImageReader.read(0, param);

it throws the exception on the last line. I've stepped through the code and it gets passed the header fine but then throws the exception when reading the first byte (I think ... it is kind of difficult to follow).

Here is the stacktrace:

java.io.EOFException
at javax.imageio.stream.ImageInputStreamImpl.readFully(ImageInputStreamImpl.java:353)
at com.sun.imageio.plugins.bmp.BMPImageReader.read24Bit(BMPImageReader.java:1227)
at com.sun.imageio.plugins.bmp.BMPImageReader.read(BMPImageReader.java:882)
at cs24010.RLECompression.stringToImage(RLECompression.java:139)
at cs24010.RLECompression.compress(RLECompression.java:63)
at cs24010.RLECompression.run(RLECompression.java:219)
at java.lang.Thread.run(Thread.java:745)

Does anyone have any idea what might be causing the error?

0

There are 0 answers