use of java.awt.image.RasterFormatException in project jdk8u_jdk by JetBrains.
the class ByteComponentRaster method verify.
/**
* Verify that the layout parameters are consistent with the data.
*
* The method verifies whether scanline stride and pixel stride do not
* cause an integer overflow during calculation of a position of the pixel
* in data buffer. It also verifies whether the data buffer has enough data
* to correspond the raster layout attributes.
*
* @throws RasterFormatException if an integer overflow is detected,
* or if data buffer has not enough capacity.
*/
protected final void verify() {
/* Need to re-verify the dimensions since a sample model may be
* specified to the constructor
*/
if (width <= 0 || height <= 0 || height > (Integer.MAX_VALUE / width)) {
throw new RasterFormatException("Invalid raster dimension");
}
for (int i = 0; i < dataOffsets.length; i++) {
if (dataOffsets[i] < 0) {
throw new RasterFormatException("Data offsets for band " + i + "(" + dataOffsets[i] + ") must be >= 0");
}
}
if ((long) minX - sampleModelTranslateX < 0 || (long) minY - sampleModelTranslateY < 0) {
throw new RasterFormatException("Incorrect origin/translate: (" + minX + ", " + minY + ") / (" + sampleModelTranslateX + ", " + sampleModelTranslateY + ")");
}
// we can be sure that width and height are greater than 0
if (scanlineStride < 0 || scanlineStride > (Integer.MAX_VALUE / height)) {
// integer overflow
throw new RasterFormatException("Incorrect scanline stride: " + scanlineStride);
}
if (height > 1 || minY - sampleModelTranslateY > 0) {
// buffer should contain at least one scanline
if (scanlineStride > data.length) {
throw new RasterFormatException("Incorrect scanline stride: " + scanlineStride);
}
}
int lastScanOffset = (height - 1) * scanlineStride;
if (pixelStride < 0 || pixelStride > (Integer.MAX_VALUE / width) || pixelStride > data.length) {
// integer overflow
throw new RasterFormatException("Incorrect pixel stride: " + pixelStride);
}
int lastPixelOffset = (width - 1) * pixelStride;
if (lastPixelOffset > (Integer.MAX_VALUE - lastScanOffset)) {
// integer overflow
throw new RasterFormatException("Incorrect raster attributes");
}
lastPixelOffset += lastScanOffset;
int index;
int maxIndex = 0;
for (int i = 0; i < numDataElements; i++) {
if (dataOffsets[i] > (Integer.MAX_VALUE - lastPixelOffset)) {
throw new RasterFormatException("Incorrect band offset: " + dataOffsets[i]);
}
index = lastPixelOffset + dataOffsets[i];
if (index > maxIndex) {
maxIndex = index;
}
}
if (data.length <= maxIndex) {
throw new RasterFormatException("Data array too small (should be > " + maxIndex + " )");
}
}
use of java.awt.image.RasterFormatException in project jdk8u_jdk by JetBrains.
the class RasterWithMinXTest method main.
public static void main(String[] args) {
String format = "jpeg";
// Set output file.
ImageOutputStream output = new MemoryCacheImageOutputStream(new ByteArrayOutputStream());
// Create image.
BufferedImage bi = new BufferedImage(256, 256, BufferedImage.TYPE_3BYTE_BGR);
// Populate image.
int[] rgbArray = new int[256];
for (int i = 0; i < 256; i++) {
Arrays.fill(rgbArray, i);
bi.setRGB(0, i, 256, 1, rgbArray, 0, 256);
}
// create translated raster in order to get non-zero minX and minY
WritableRaster r = (WritableRaster) bi.getRaster().createTranslatedChild(64, 64);
Iterator i = ImageIO.getImageWritersByFormatName(format);
ImageWriter iw = null;
while (i.hasNext() && iw == null) {
Object o = i.next();
if (o instanceof com.sun.imageio.plugins.jpeg.JPEGImageWriter) {
iw = (ImageWriter) o;
}
}
if (iw == null) {
throw new RuntimeException("No available image writer");
}
ImageWriteParam iwp = iw.getDefaultWriteParam();
IIOMetadata metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), r.getSampleModel()), iwp);
IIOImage img = new IIOImage(r, null, metadata);
iw.setOutput(output);
try {
iw.write(img);
} catch (RasterFormatException e) {
e.printStackTrace();
throw new RuntimeException("RasterException occurs. Test Failed!");
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("Unexpected Exception");
}
// test case of theImageWriteParam with non-null sourceRegion
iwp.setSourceRegion(new Rectangle(32, 32, 192, 192));
metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), r.getSampleModel()), iwp);
try {
iw.write(metadata, img, iwp);
} catch (RasterFormatException e) {
e.printStackTrace();
throw new RuntimeException("SetSourceRegion causes the RasterException. Test Failed!");
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("Unexpected Exception");
}
}
Aggregations