Search in sources :

Example 6 with RasterFormatException

use of java.awt.image.RasterFormatException in project jdk8u_jdk by JetBrains.

the class ShortInterleavedRaster method createWritableChild.

/**
     * Creates a Writable subRaster given a region of the Raster. The x and y
     * coordinates specify the horizontal and vertical offsets
     * from the upper-left corner of this Raster to the upper-left corner
     * of the subRaster.  A subset of the bands of the parent Raster may
     * be specified.  If this is null, then all the bands are present in the
     * subRaster. A translation to the subRaster may also be specified.
     * Note that the subRaster will reference the same
     * DataBuffers as the parent Raster, but using different offsets.
     * @param x               X offset.
     * @param y               Y offset.
     * @param width           Width (in pixels) of the subraster.
     * @param height          Height (in pixels) of the subraster.
     * @param x0              Translated X origin of the subraster.
     * @param y0              Translated Y origin of the subraster.
     * @param bandList        Array of band indices.
     * @exception RasterFormatException
     *            if the specified bounding box is outside of the parent Raster.
     */
public WritableRaster createWritableChild(int x, int y, int width, int height, int x0, int y0, int[] bandList) {
    if (x < this.minX) {
        throw new RasterFormatException("x lies outside the raster");
    }
    if (y < this.minY) {
        throw new RasterFormatException("y lies outside the raster");
    }
    if ((x + width < x) || (x + width > this.minX + this.width)) {
        throw new RasterFormatException("(x + width) is outside of Raster");
    }
    if ((y + height < y) || (y + height > this.minY + this.height)) {
        throw new RasterFormatException("(y + height) is outside of Raster");
    }
    SampleModel sm;
    if (bandList != null)
        sm = sampleModel.createSubsetSampleModel(bandList);
    else
        sm = sampleModel;
    int deltaX = x0 - x;
    int deltaY = y0 - y;
    return new ShortInterleavedRaster(sm, dataBuffer, new Rectangle(x0, y0, width, height), new Point(sampleModelTranslateX + deltaX, sampleModelTranslateY + deltaY), this);
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SampleModel(java.awt.image.SampleModel) Rectangle(java.awt.Rectangle) Point(java.awt.Point) RasterFormatException(java.awt.image.RasterFormatException) Point(java.awt.Point)

Example 7 with RasterFormatException

use of java.awt.image.RasterFormatException in project android by JetBrains.

the class RemoveAlgorithms method floodFill.

/**
   * The color are removed using a flood fill algorithm from the point defined by (ox,oy) .
   * If the point is outside the raster area, nothing happens and an error is logged.
   *
   * The flood fill algorithm is implemented using the scanline version to improve performances.
   *
   * @param src         The source Raster given by {@link CompositeContext#compose(Raster, Raster, WritableRaster)}
   * @param dstIn       The destination Raster given by {@link CompositeContext#compose(Raster, Raster, WritableRaster)}
   * @param dstOut      The destination  {@link WritableRaster} given by {@link CompositeContext#compose(Raster, Raster, WritableRaster)}
   * @param ox          X coordinate of the flood-fill origin point
   * @param oy          Y coordinate of the flood-fill origin point
   * @param removeColor The color to set transparent in the raster
   * @param threshold   The threshold such as every color whose distance from
   *                    removeColor <= threshold will be set to transparent
   */
static void floodFill(Raster src, Raster dstIn, WritableRaster dstOut, int ox, int oy, int[] removeColor, double threshold) {
    int height = Math.min(src.getHeight(), dstIn.getHeight());
    int width = Math.min(src.getWidth(), dstIn.getWidth());
    int[] dstPixels = new int[width];
    int[] srcDownPixels = new int[width];
    int[] srcUpPixels = new int[width];
    int[] srcPixels = new int[width];
    int[] stack = new int[(width * height)];
    boolean[] visited = new boolean[width * height];
    int size = 0;
    boolean spanUp, spanDown;
    // Check if the origin point is inside the rater
    if (oy > height || ox > width) {
        Logger.getInstance(RemoveColorComposite.class).error(new RasterFormatException("The origin point is outside the raster area"));
    }
    // Put the origin point index in the stack
    stack[size++] = oy * width + ox % width;
    // Copy all src pixel in the output Raster since
    // the flood fill won't go through all the pixel
    dstOut.setDataElements(0, 0, src);
    // Begin the flood fill
    int nx;
    while (size > 0) {
        // Pop the pixel index from the stash
        int seed = stack[--size];
        int y = seed / width;
        int x = seed % width;
        // Load the current line inside the buffers
        dstIn.getDataElements(0, y, width, 1, dstPixels);
        src.getDataElements(0, y, width, 1, srcPixels);
        // load the lines over and below
        if (y > 0) {
            src.getDataElements(0, y - 1, width, 1, srcUpPixels);
        }
        if (y < height - 1) {
            src.getDataElements(0, y + 1, width, 1, srcDownPixels);
        }
        nx = x;
        // "Rewind" to the first pixel of the region
        while (nx >= 0 && dist(srcPixels[nx], removeColor) <= threshold) nx--;
        nx++;
        // Reset the span up and down flags
        spanUp = spanDown = false;
        // Loop on the whole line until wwe "hit" a different color
        double dist;
        while (nx < width && (dist = dist(srcPixels[nx], removeColor)) <= threshold) {
            if (!visited[y * width + nx % width]) {
                visited[y * width + nx % width] = true;
                // Set this pixel to be transparent
                double alpha;
                if (((dstPixels[nx] >> 24) & 0xFF) == 0xFF && dist > threshold / 2.0) {
                    alpha = 0xFF * (dist / threshold);
                } else {
                    alpha = 0;
                }
                dstPixels[nx] = (srcPixels[nx] & 0x00FFFFFF) | (Math.round((float) alpha) << 24);
            } else {
                // If the pixel is already transparent, skip to the next
                nx++;
                continue;
            }
            // Find the distance between the current pixel an the one over it
            double distUp = dist(srcUpPixels[nx], removeColor);
            if (!spanUp && y > 0 && distUp <= threshold) {
                // If the two color are close enough and
                // If we have not already added a seed for this span
                // add the seed to the stack and set the flag to stop looking for a seed in this span
                stack[size++] = (y - 1) * width + nx % width;
                spanUp = true;
            } else if (spanUp && y > 0 && distUp > threshold) {
                // If the two colors are different, reset the flag
                spanUp = false;
            }
            // Find the distance between the current pixel an the one below it
            double distDown = dist(srcDownPixels[nx], removeColor);
            if (!spanDown && y < height - 1 && distDown <= threshold) {
                // If the two color are close enough and
                // If we have not already added a seed for this span
                // add the seed to the stack and set the flag to stop looking for a seed in this span
                stack[size++] = (y + 1) * width + nx % width;
                spanDown = true;
            } else if (spanDown && y < height - 1 && distDown > threshold) {
                // If the two colors are different, reset the flag
                spanDown = false;
            }
            nx++;
        }
        dstOut.setDataElements(0, y, width, 1, dstPixels);
    }
}
Also used : RasterFormatException(java.awt.image.RasterFormatException)

Example 8 with RasterFormatException

use of java.awt.image.RasterFormatException in project jdk8u_jdk by JetBrains.

the class ShortComponentRaster 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 9 with RasterFormatException

use of java.awt.image.RasterFormatException in project jdk8u_jdk by JetBrains.

the class ByteBandedRaster method verify.

/**
     * Verify that the layout parameters are consistent with the data.
     * Verifies whether the data buffer has enough data for the raster,
     * taking into account offsets, after ensuring all offsets are >=0.
     * @throws RasterFormatException if a problem is detected.
     */
private 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");
    }
    if (scanlineStride < 0 || scanlineStride > (Integer.MAX_VALUE / height)) {
        // integer overflow
        throw new RasterFormatException("Incorrect scanline stride: " + scanlineStride);
    }
    if ((long) minX - sampleModelTranslateX < 0 || (long) minY - sampleModelTranslateY < 0) {
        throw new RasterFormatException("Incorrect origin/translate: (" + minX + ", " + minY + ") / (" + sampleModelTranslateX + ", " + sampleModelTranslateY + ")");
    }
    if (height > 1 || minY - sampleModelTranslateY > 0) {
        // buffer should contain at least one scanline
        for (int i = 0; i < data.length; i++) {
            if (scanlineStride > data[i].length) {
                throw new RasterFormatException("Incorrect scanline stride: " + scanlineStride);
            }
        }
    }
    // Make sure data for Raster is in a legal range
    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");
        }
    }
    int lastScanOffset = (height - 1) * scanlineStride;
    if ((width - 1) > (Integer.MAX_VALUE - lastScanOffset)) {
        throw new RasterFormatException("Invalid raster dimension");
    }
    int lastPixelOffset = lastScanOffset + (width - 1);
    int maxIndex = 0;
    int index;
    for (int i = 0; i < numDataElements; i++) {
        if (dataOffsets[i] > (Integer.MAX_VALUE - lastPixelOffset)) {
            throw new RasterFormatException("Invalid raster dimension");
        }
        index = lastPixelOffset + dataOffsets[i];
        if (index > maxIndex) {
            maxIndex = index;
        }
    }
    if (data.length == 1) {
        if (data[0].length <= maxIndex * numDataElements) {
            throw new RasterFormatException("Data array too small " + "(it is " + data[0].length + " and should be > " + (maxIndex * numDataElements) + " )");
        }
    } else {
        for (int i = 0; i < numDataElements; i++) {
            if (data[i].length <= maxIndex) {
                throw new RasterFormatException("Data array too small " + "(it is " + data[i].length + " and should be > " + maxIndex + " )");
            }
        }
    }
}
Also used : RasterFormatException(java.awt.image.RasterFormatException) Point(java.awt.Point)

Example 10 with RasterFormatException

use of java.awt.image.RasterFormatException in project jdk8u_jdk by JetBrains.

the class ByteBandedRaster method createWritableChild.

/**
     * Creates a Writable subraster given a region of the raster.  The x and y
     * coordinates specify the horizontal and vertical offsets
     * from the upper-left corner of this raster to the upper-left corner
     * of the subraster.  A subset of the bands of the parent Raster may
     * be specified.  If this is null, then all the bands are present in the
     * subRaster. A translation to the subRaster may also be specified.
     * Note that the subraster will reference the same
     * DataBuffers as the parent raster, but using different offsets.
     * @param x               X offset.
     * @param y               Y offset.
     * @param width           Width of the subraster.
     * @param height          Height of the subraster.
     * @param x0              Translated X origin of the subraster.
     * @param y0              Translated Y origin of the subraster.
     * @param bandList        Array of band indices.
     * @exception RasterFormatException
     *            if the specified bounding box is outside of the parent raster.
     */
public WritableRaster createWritableChild(int x, int y, int width, int height, int x0, int y0, int[] bandList) {
    if (x < this.minX) {
        throw new RasterFormatException("x lies outside raster");
    }
    if (y < this.minY) {
        throw new RasterFormatException("y lies outside raster");
    }
    if ((x + width < x) || (x + width > this.width + this.minX)) {
        throw new RasterFormatException("(x + width) is outside raster");
    }
    if ((y + height < y) || (y + height > this.height + this.minY)) {
        throw new RasterFormatException("(y + height) is outside raster");
    }
    SampleModel sm;
    if (bandList != null)
        sm = sampleModel.createSubsetSampleModel(bandList);
    else
        sm = sampleModel;
    int deltaX = x0 - x;
    int deltaY = y0 - y;
    return new ByteBandedRaster(sm, dataBuffer, new Rectangle(x0, y0, width, height), new Point(sampleModelTranslateX + deltaX, sampleModelTranslateY + deltaY), this);
}
Also used : BandedSampleModel(java.awt.image.BandedSampleModel) SampleModel(java.awt.image.SampleModel) Rectangle(java.awt.Rectangle) Point(java.awt.Point) RasterFormatException(java.awt.image.RasterFormatException) Point(java.awt.Point)

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