提交 7fa33389 编写于 作者: B bae

8012601: Better validation of image layouts

Reviewed-by: prr
上级 66d85601
...@@ -776,6 +776,7 @@ public class BufferedImage extends java.awt.Image ...@@ -776,6 +776,7 @@ public class BufferedImage extends java.awt.Image
} }
} }
if (is8bit && if (is8bit &&
braster.getPixelStride() == numBands &&
offs[0] == numBands-1 && offs[0] == numBands-1 &&
offs[1] == numBands-2 && offs[1] == numBands-2 &&
offs[2] == numBands-3) offs[2] == numBands-3)
......
...@@ -76,6 +76,7 @@ class LCMSImageLayout { ...@@ -76,6 +76,7 @@ class LCMSImageLayout {
int width; int width;
int height; int height;
int nextRowOffset; int nextRowOffset;
private int nextPixelOffset;
int offset; int offset;
/* This flag indicates whether the image can be processed /* This flag indicates whether the image can be processed
...@@ -93,6 +94,7 @@ class LCMSImageLayout { ...@@ -93,6 +94,7 @@ class LCMSImageLayout {
this.pixelType = pixelType; this.pixelType = pixelType;
width = np; width = np;
height = 1; height = 1;
nextPixelOffset = pixelSize;
nextRowOffset = safeMult(pixelSize, np); nextRowOffset = safeMult(pixelSize, np);
offset = 0; offset = 0;
} }
...@@ -104,6 +106,7 @@ class LCMSImageLayout { ...@@ -104,6 +106,7 @@ class LCMSImageLayout {
this.pixelType = pixelType; this.pixelType = pixelType;
this.width = width; this.width = width;
this.height = height; this.height = height;
nextPixelOffset = pixelSize;
nextRowOffset = safeMult(pixelSize, width); nextRowOffset = safeMult(pixelSize, width);
offset = 0; offset = 0;
} }
...@@ -221,6 +224,7 @@ class LCMSImageLayout { ...@@ -221,6 +224,7 @@ class LCMSImageLayout {
IntegerComponentRaster intRaster = (IntegerComponentRaster) IntegerComponentRaster intRaster = (IntegerComponentRaster)
image.getRaster(); image.getRaster();
l.nextRowOffset = safeMult(4, intRaster.getScanlineStride()); l.nextRowOffset = safeMult(4, intRaster.getScanlineStride());
l.nextPixelOffset = safeMult(4, intRaster.getPixelStride());
l.offset = safeMult(4, intRaster.getDataOffset(0)); l.offset = safeMult(4, intRaster.getDataOffset(0));
l.dataArray = intRaster.getDataStorage(); l.dataArray = intRaster.getDataStorage();
l.dataArrayLength = 4 * intRaster.getDataStorage().length; l.dataArrayLength = 4 * intRaster.getDataStorage().length;
...@@ -238,6 +242,8 @@ class LCMSImageLayout { ...@@ -238,6 +242,8 @@ class LCMSImageLayout {
ByteComponentRaster byteRaster = (ByteComponentRaster) ByteComponentRaster byteRaster = (ByteComponentRaster)
image.getRaster(); image.getRaster();
l.nextRowOffset = byteRaster.getScanlineStride(); l.nextRowOffset = byteRaster.getScanlineStride();
l.nextPixelOffset = byteRaster.getPixelStride();
int firstBand = image.getSampleModel().getNumBands() - 1; int firstBand = image.getSampleModel().getNumBands() - 1;
l.offset = byteRaster.getDataOffset(firstBand); l.offset = byteRaster.getDataOffset(firstBand);
l.dataArray = byteRaster.getDataStorage(); l.dataArray = byteRaster.getDataStorage();
...@@ -254,6 +260,8 @@ class LCMSImageLayout { ...@@ -254,6 +260,8 @@ class LCMSImageLayout {
ByteComponentRaster byteRaster = (ByteComponentRaster) ByteComponentRaster byteRaster = (ByteComponentRaster)
image.getRaster(); image.getRaster();
l.nextRowOffset = byteRaster.getScanlineStride(); l.nextRowOffset = byteRaster.getScanlineStride();
l.nextPixelOffset = byteRaster.getPixelStride();
l.dataArrayLength = byteRaster.getDataStorage().length; l.dataArrayLength = byteRaster.getDataStorage().length;
l.offset = byteRaster.getDataOffset(0); l.offset = byteRaster.getDataOffset(0);
l.dataArray = byteRaster.getDataStorage(); l.dataArray = byteRaster.getDataStorage();
...@@ -270,6 +278,8 @@ class LCMSImageLayout { ...@@ -270,6 +278,8 @@ class LCMSImageLayout {
ShortComponentRaster shortRaster = (ShortComponentRaster) ShortComponentRaster shortRaster = (ShortComponentRaster)
image.getRaster(); image.getRaster();
l.nextRowOffset = safeMult(2, shortRaster.getScanlineStride()); l.nextRowOffset = safeMult(2, shortRaster.getScanlineStride());
l.nextPixelOffset = safeMult(2, shortRaster.getPixelStride());
l.offset = safeMult(2, shortRaster.getDataOffset(0)); l.offset = safeMult(2, shortRaster.getDataOffset(0));
l.dataArray = shortRaster.getDataStorage(); l.dataArray = shortRaster.getDataStorage();
l.dataArrayLength = 2 * shortRaster.getDataStorage().length; l.dataArrayLength = 2 * shortRaster.getDataStorage().length;
...@@ -331,9 +341,15 @@ class LCMSImageLayout { ...@@ -331,9 +341,15 @@ class LCMSImageLayout {
throw new ImageLayoutException("Invalid image layout"); throw new ImageLayoutException("Invalid image layout");
} }
int lastPixelOffset = safeMult(nextRowOffset, (height - 1)); if (nextPixelOffset != getBytesPerPixel(pixelType)) {
throw new ImageLayoutException("Invalid image layout");
}
int lastScanOffset = safeMult(nextRowOffset, (height - 1));
lastPixelOffset = safeAdd(lastPixelOffset, (width - 1)); int lastPixelOffset = safeMult(nextPixelOffset, (width -1 ));
lastPixelOffset = safeAdd(lastPixelOffset, lastScanOffset);
int off = safeAdd(offset, lastPixelOffset); int off = safeAdd(offset, lastPixelOffset);
...@@ -392,6 +408,8 @@ class LCMSImageLayout { ...@@ -392,6 +408,8 @@ class LCMSImageLayout {
} }
l.nextRowOffset = br.getScanlineStride(); l.nextRowOffset = br.getScanlineStride();
l.nextPixelOffset = br.getPixelStride();
l.offset = br.getDataOffset(firstBand); l.offset = br.getDataOffset(firstBand);
l.dataArray = br.getDataStorage(); l.dataArray = br.getDataStorage();
l.dataType = DT_BYTE; l.dataType = DT_BYTE;
...@@ -406,4 +424,25 @@ class LCMSImageLayout { ...@@ -406,4 +424,25 @@ class LCMSImageLayout {
} }
return null; return null;
} }
/**
* Derives number of bytes per pixel from the pixel format.
* Following bit fields are used here:
* [0..2] - bytes per sample
* [3..6] - number of color samples per pixel
* [7..9] - number of non-color samples per pixel
*
* A complete description of the pixel format can be found
* here: lcms2.h, lines 651 - 667.
*
* @param pixelType pixel format in lcms2 notation.
* @return number of bytes per pixel for given pixel format.
*/
private static int getBytesPerPixel(int pixelType) {
int bytesPerSample = (0x7 & pixelType);
int colorSamplesPerPixel = 0xF & (pixelType >> 3);
int extraSamplesPerPixel = 0x7 & (pixelType >> 7);
return bytesPerSample * (colorSamplesPerPixel + extraSamplesPerPixel);
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册