On Fri, 29 Oct 2021 09:36:37 GMT, Masanori Yano <my...@openjdk.org> wrote:

>> Could you please review the 8262297 bug fixes?
>> 
>> In this case, ImageIO.write() should throw java.io.IOException rather than 
>> java.lang.IndexOutOfBoundsException. IndexOutOfBoundsException is caught and 
>> wrapped in IIOException in ImageIO.write() with this fix. In addition, 
>> IndexOutOfBoundsException is not expected to throw by 
>> RandomAccessFile#write() according to its API specification. So it should be 
>> fixed.
>
> Masanori Yano has updated the pull request incrementally with one additional 
> commit since the last revision:
> 
>   8262297: ImageIO.write() method will throw IndexOutOfBoundsException

This problem occurs because the input png image depth is 2 bits per pixel.

Because the png to read is the correct image at 2 bits per pixel, the 
BufferedImage is generated correctly and no exception is thrown. However, bmp 
can only create images with a depth of 0, 1, 4, 8, 16, 24, or 32 bits per pixel.
https://docs.microsoft.com/en-us/previous-versions/dd183376(v=vs.85)

Because the BMPImageWriter determines bitsPerPixel in the range of the color 
palette without checking the depth, images with depths other than 0, 1, 4, 8, 
16, 24, 32 are set to the wrong bitsPerPixel and destScanlineBytes. Therefore, 
an incorrect length reference to the size of the Raster DataBuffer causes IIOBE 
to be thrown.

So the fix should change the canEncodeImage method to check the color depth and 
throw an IOException if it is not 0, 1, 4, 8, 16, 24, or 32.

-------------

PR: https://git.openjdk.java.net/jdk/pull/6151

Reply via email to