Search in sources :

Example 16 with RasterFormatException

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 + " )");
    }
}
Also used : RasterFormatException(java.awt.image.RasterFormatException) Point(java.awt.Point)

Example 17 with RasterFormatException

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");
    }
}
Also used : ImageWriter(javax.imageio.ImageWriter) Rectangle(java.awt.Rectangle) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) RasterFormatException(java.awt.image.RasterFormatException) IIOImage(javax.imageio.IIOImage) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) WritableRaster(java.awt.image.WritableRaster) Iterator(java.util.Iterator) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) RasterFormatException(java.awt.image.RasterFormatException)

Aggregations

RasterFormatException (java.awt.image.RasterFormatException)17 Point (java.awt.Point)14 Rectangle (java.awt.Rectangle)10 SampleModel (java.awt.image.SampleModel)9 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)6 ComponentSampleModel (java.awt.image.ComponentSampleModel)4 BandedSampleModel (java.awt.image.BandedSampleModel)2 BufferedImage (java.awt.image.BufferedImage)2 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)2 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)1 WritableRaster (java.awt.image.WritableRaster)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 IIOImage (javax.imageio.IIOImage)1 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)1 ImageWriteParam (javax.imageio.ImageWriteParam)1 ImageWriter (javax.imageio.ImageWriter)1 IIOMetadata (javax.imageio.metadata.IIOMetadata)1 ImageOutputStream (javax.imageio.stream.ImageOutputStream)1