7031957: DIB header of type BITMAPV2INFOHEADER & BITMAPV3INFOHEADER is not supported in BMPImageReader

Reviewed-by: prr, pnarayanan
This commit is contained in:
Jayathirth D V 2018-03-23 11:18:39 +05:30
parent 8d5fa0fad3
commit 948a1e2729
6 changed files with 138 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -30,6 +30,7 @@ public interface BMPConstants {
static final String VERSION_2 = "BMP v. 2.x";
static final String VERSION_3 = "BMP v. 3.x";
static final String VERSION_3_NT = "BMP v. 3.x NT";
static final String VERSION_3_EXT = "BMP V2/V3 INFO";
static final String VERSION_4 = "BMP v. 4.x";
static final String VERSION_5 = "BMP v. 5.x";
@ -48,3 +49,4 @@ public interface BMPConstants {
static final int BI_JPEG = 4;
static final int BI_PNG = 5;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -78,6 +78,8 @@ import com.sun.imageio.plugins.common.I18N;
*
* This class supports Microsoft Windows Bitmap Version 3-5,
* as well as OS/2 Bitmap Version 2.x (for single-image BMP file).
* It also supports undocumented DIB header of type BITMAPV2INFOHEADER
* & BITMAPV3INFOHEADER.
*/
public class BMPImageReader extends ImageReader implements BMPConstants {
// BMP Image types
@ -94,16 +96,25 @@ public class BMPImageReader extends ImageReader implements BMPConstants {
private static final int VERSION_3_NT_16_BIT = 8;
private static final int VERSION_3_NT_32_BIT = 9;
private static final int VERSION_4_1_BIT = 10;
private static final int VERSION_4_4_BIT = 11;
private static final int VERSION_4_8_BIT = 12;
private static final int VERSION_4_16_BIT = 13;
private static final int VERSION_4_24_BIT = 14;
private static final int VERSION_4_32_BIT = 15;
// All VERSION_3_EXT_* are for BITMAPV2INFOHEADER & BITMAPV3INFOHEADER
private static final int VERSION_3_EXT_1_BIT = 10;
private static final int VERSION_3_EXT_4_BIT = 11;
private static final int VERSION_3_EXT_8_BIT = 12;
private static final int VERSION_3_EXT_16_BIT = 13;
private static final int VERSION_3_EXT_24_BIT = 14;
private static final int VERSION_3_EXT_32_BIT = 15;
private static final int VERSION_3_XP_EMBEDDED = 16;
private static final int VERSION_4_XP_EMBEDDED = 17;
private static final int VERSION_5_XP_EMBEDDED = 18;
private static final int VERSION_4_1_BIT = 16;
private static final int VERSION_4_4_BIT = 17;
private static final int VERSION_4_8_BIT = 18;
private static final int VERSION_4_16_BIT = 19;
private static final int VERSION_4_24_BIT = 20;
private static final int VERSION_4_32_BIT = 21;
private static final int VERSION_3_XP_EMBEDDED = 22;
private static final int VERSION_3_EXT_EMBEDDED = 23;
private static final int VERSION_4_XP_EMBEDDED = 24;
private static final int VERSION_5_XP_EMBEDDED = 25;
// BMP variables
private long bitmapFileSize;
@ -409,6 +420,63 @@ public class BMPImageReader extends ImageReader implements BMPConstants {
throw new
IIOException(I18N.getString("BMPImageReader2"));
}
} else if (size == 52 || size == 56) {
// BITMAPV2INFOHEADER or BITMAPV3INFOHEADER
redMask = (int)iis.readUnsignedInt();
greenMask = (int)iis.readUnsignedInt();
blueMask = (int)iis.readUnsignedInt();
if (size == 56) {
alphaMask = (int)iis.readUnsignedInt();
}
metadata.bmpVersion = VERSION_3_EXT;
// Read in the palette
int numberOfEntries = (int)((bitmapOffset-14-size) / 4);
int sizeOfPalette = numberOfEntries*4;
palette = new byte[sizeOfPalette];
iis.readFully(palette, 0, sizeOfPalette);
metadata.palette = palette;
metadata.paletteSize = numberOfEntries;
switch((int)compression) {
case BI_JPEG:
case BI_PNG:
imageType = VERSION_3_EXT_EMBEDDED;
break;
default:
if (bitsPerPixel == 1) {
imageType = VERSION_3_EXT_1_BIT;
} else if (bitsPerPixel == 4) {
imageType = VERSION_3_EXT_4_BIT;
} else if (bitsPerPixel == 8) {
imageType = VERSION_3_EXT_8_BIT;
} else if (bitsPerPixel == 16) {
imageType = VERSION_3_EXT_16_BIT;
if ((int)compression == BI_RGB) {
redMask = 0x7C00;
greenMask = 0x3E0;
blueMask = 0x1F;
}
} else if (bitsPerPixel == 24) {
imageType = VERSION_3_EXT_24_BIT;
} else if (bitsPerPixel == 32) {
imageType = VERSION_3_EXT_32_BIT;
if ((int)compression == BI_RGB) {
redMask = 0x00FF0000;
greenMask = 0x0000FF00;
blueMask = 0x000000FF;
}
} else {
throw new
IIOException(I18N.getString("BMPImageReader8"));
}
metadata.redMask = redMask;
metadata.greenMask = greenMask;
metadata.blueMask = blueMask;
metadata.alphaMask = alphaMask;
}
} else if (size == 108 || size == 124) {
// Windows 4.x BMP
if (size == 108)
@ -908,15 +976,18 @@ public class BMPImageReader extends ImageReader implements BMPConstants {
break;
case VERSION_3_XP_EMBEDDED:
case VERSION_3_EXT_EMBEDDED:
case VERSION_4_XP_EMBEDDED:
case VERSION_5_XP_EMBEDDED:
bi = readEmbedded((int)compression, bi, param);
break;
case VERSION_3_EXT_1_BIT:
case VERSION_4_1_BIT:
read1Bit(bdata);
break;
case VERSION_3_EXT_4_BIT:
case VERSION_4_4_BIT:
switch((int)compression) {
@ -934,6 +1005,7 @@ public class BMPImageReader extends ImageReader implements BMPConstants {
}
break;
case VERSION_3_EXT_8_BIT:
case VERSION_4_8_BIT:
switch((int)compression) {
@ -951,14 +1023,17 @@ public class BMPImageReader extends ImageReader implements BMPConstants {
}
break;
case VERSION_3_EXT_16_BIT:
case VERSION_4_16_BIT:
read16Bit(sdata);
break;
case VERSION_3_EXT_24_BIT:
case VERSION_4_24_BIT:
read24Bit(bdata);
break;
case VERSION_3_EXT_32_BIT:
case VERSION_4_32_BIT:
read32Bit(idata);
break;
@ -2000,3 +2075,4 @@ public class BMPImageReader extends ImageReader implements BMPConstants {
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 B

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 7031957
* @summary Test verifies whether BMPImageReader is capable of reading
* images with DIB header type BITMAPV3INFOHEADER.
* @run main ReadBITMAPV3INFOHEADERTest
*/
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class ReadBITMAPV3INFOHEADERTest {
public static void main(String[] args) throws IOException {
String dir = System.getProperty("test.src");
String sep = System.getProperty("file.separator");
/*
* Try reading BITMAPV3INFOHEADER type BMP images and check whether
* read fails. If read fails we throw Exception.
*/
ImageIO.read(new File(dir + sep + "DIB_size-56_ARGB_16bits.bmp"));
ImageIO.read(new File(dir + sep + "DIB_size-56_RGB_16bits.bmp"));
ImageIO.read(new File(dir + sep + "DIB_size-56_XRGB_32bits.bmp"));
}
}