Java Code Examples for java.awt.image.Raster#getWidth()

The following examples show how to use java.awt.image.Raster#getWidth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BytePackedRaster.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Stores the Raster data at the specified location.
 * An ArrayIndexOutOfBounds exception will be thrown at runtime
 * if the pixel coordinates are out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    // Check if we can use fast code
    if (!(inRaster instanceof BytePackedRaster) ||
        ((BytePackedRaster)inRaster).pixelBitStride != pixelBitStride) {
        super.setDataElements(x, y, inRaster);
        return;
    }

    int srcOffX = inRaster.getMinX();
    int srcOffY = inRaster.getMinY();
    int dstOffX = srcOffX + x;
    int dstOffY = srcOffY + y;
    int width = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }
    setDataElements(dstOffX, dstOffY,
                    srcOffX, srcOffY,
                    width, height,
                    (BytePackedRaster)inRaster);
}
 
Example 2
Source File: MultipleGradientPaintContext.java    From Java8CN with Apache License 2.0 6 votes vote down vote up
/**
 * Took this cacheRaster code from GradientPaint. It appears to recycle
 * rasters for use by any other instance, as long as they are sufficiently
 * large.
 */
private static synchronized void putCachedRaster(ColorModel cm,
                                                 Raster ras)
{
    if (cached != null) {
        Raster cras = (Raster) cached.get();
        if (cras != null) {
            int cw = cras.getWidth();
            int ch = cras.getHeight();
            int iw = ras.getWidth();
            int ih = ras.getHeight();
            if (cw >= iw && ch >= ih) {
                return;
            }
            if (cw * ch >= iw * ih) {
                return;
            }
        }
    }
    cachedModel = cm;
    cached = new WeakReference<Raster>(ras);
}
 
Example 3
Source File: MultipleGradientPaintContext.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Took this cacheRaster code from GradientPaint. It appears to recycle
 * rasters for use by any other instance, as long as they are sufficiently
 * large.
 */
private static synchronized void putCachedRaster(ColorModel cm,
                                                 Raster ras)
{
    if (cached != null) {
        Raster cras = (Raster) cached.get();
        if (cras != null) {
            int cw = cras.getWidth();
            int ch = cras.getHeight();
            int iw = ras.getWidth();
            int ih = ras.getHeight();
            if (cw >= iw && ch >= ih) {
                return;
            }
            if (cw * ch >= iw * ih) {
                return;
            }
        }
    }
    cachedModel = cm;
    cached = new WeakReference<Raster>(ras);
}
 
Example 4
Source File: PartialImageEdit.java    From Pixelitor with GNU General Public License v3.0 6 votes vote down vote up
@Override
public DebugNode getDebugNode() {
    var node = super.getDebugNode();

    int width = -1;
    int height = -1;
    Raster backupRaster = backupRasterRef.get();
    if (backupRaster != null) {
        width = backupRaster.getWidth();
        height = backupRaster.getHeight();
    }

    node.addInt("backup image width", width);
    node.addInt("backup image height", height);

    return node;
}
 
Example 5
Source File: GradientPaintContext.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
static synchronized void putCachedRaster(ColorModel cm, Raster ras) {
    if (cached != null) {
        Raster cras = (Raster) cached.get();
        if (cras != null) {
            int cw = cras.getWidth();
            int ch = cras.getHeight();
            int iw = ras.getWidth();
            int ih = ras.getHeight();
            if (cw >= iw && ch >= ih) {
                return;
            }
            if (cw * ch >= iw * ih) {
                return;
            }
        }
    }
    cachedModel = cm;
    cached = new WeakReference<>(ras);
}
 
Example 6
Source File: BytePackedRaster.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Stores the Raster data at the specified location.
 * An ArrayIndexOutOfBounds exception will be thrown at runtime
 * if the pixel coordinates are out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    // Check if we can use fast code
    if (!(inRaster instanceof BytePackedRaster) ||
        ((BytePackedRaster)inRaster).pixelBitStride != pixelBitStride) {
        super.setDataElements(x, y, inRaster);
        return;
    }

    int srcOffX = inRaster.getMinX();
    int srcOffY = inRaster.getMinY();
    int dstOffX = srcOffX + x;
    int dstOffY = srcOffY + y;
    int width = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }
    setDataElements(dstOffX, dstOffY,
                    srcOffX, srcOffY,
                    width, height,
                    (BytePackedRaster)inRaster);
}
 
Example 7
Source File: ShortBandedRaster.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Stores the Raster data at the specified location.
 * An ArrayIndexOutOfBounds exception will be thrown at runtime
 * if the pixel coordinates are out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    int dstOffX = x + inRaster.getMinX();
    int dstOffY = y + inRaster.getMinY();
    int width  = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }

    setDataElements(dstOffX, dstOffY, width, height, inRaster);
}
 
Example 8
Source File: ShortInterleavedRaster.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Stores the Raster data at the specified location.
 * An ArrayIndexOutOfBounds exception will be thrown at runtime
 * if the pixel coordinates are out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    int dstOffX = x + inRaster.getMinX();
    int dstOffY = y + inRaster.getMinY();
    int width  = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }

    setDataElements(dstOffX, dstOffY, width, height, inRaster);
}
 
Example 9
Source File: GradientPaintContext.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
static synchronized Raster getCachedRaster(ColorModel cm, int w, int h) {
    if (cm == cachedModel) {
        if (cached != null) {
            Raster ras = cached.get();
            if (ras != null &&
                ras.getWidth() >= w &&
                ras.getHeight() >= h)
            {
                cached = null;
                return ras;
            }
        }
    }
    return cm.createCompatibleWritableRaster(w, h);
}
 
Example 10
Source File: GradientPaintContext.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static synchronized Raster getCachedRaster(ColorModel cm, int w, int h) {
    if (cm == cachedModel) {
        if (cached != null) {
            Raster ras = cached.get();
            if (ras != null &&
                ras.getWidth() >= w &&
                ras.getHeight() >= h)
            {
                cached = null;
                return ras;
            }
        }
    }
    return cm.createCompatibleWritableRaster(w, h);
}
 
Example 11
Source File: ByteComponentRaster.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Stores the Raster data at the specified location.
 * An ArrayIndexOutOfBounds exception will be thrown at runtime
 * if the pixel coordinates are out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    int dstOffX = inRaster.getMinX() + x;
    int dstOffY = inRaster.getMinY() + y;
    int width  = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }

    setDataElements(dstOffX, dstOffY, width, height, inRaster);
}
 
Example 12
Source File: IntegerComponentRaster.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Stores the Raster data at the specified location.
 * The transferType of the inputRaster must match this raster.
 * An ArrayIndexOutOfBoundsException will be thrown at runtime
 * if the pixel coordinates are out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    int dstOffX = x + inRaster.getMinX();
    int dstOffY = y + inRaster.getMinY();
    int width  = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }
    setDataElements(dstOffX, dstOffY, width, height, inRaster);
}
 
Example 13
Source File: ImageLoader.java    From Canova with Apache License 2.0 5 votes vote down vote up
public int[][] fromFile(File file) throws IOException {
    BufferedImage image = ImageIO.read(file);
    if (height > 0 && width > 0)
        image = toBufferedImage(image.getScaledInstance(height, width, Image.SCALE_SMOOTH));
    Raster raster = image.getData();
    int w = raster.getWidth(), h = raster.getHeight();
    int[][] ret = new int[w][h];
    for (int i = 0; i < w; i++)
        for (int j = 0; j < h; j++)
            ret[i][j] = raster.getSample(i, j, 0);

    return ret;
}
 
Example 14
Source File: ShortInterleavedRaster.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Stores the Raster data at the specified location.
 * An ArrayIndexOutOfBounds exception will be thrown at runtime
 * if the pixel coordinates are out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    int dstOffX = x + inRaster.getMinX();
    int dstOffY = y + inRaster.getMinY();
    int width  = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }

    setDataElements(dstOffX, dstOffY, width, height, inRaster);
}
 
Example 15
Source File: MultipleGradientPaintContext.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public final Raster getRaster(int x, int y, int w, int h) {
    // If working raster is big enough, reuse it. Otherwise,
    // build a large enough new one.
    Raster raster = saved;
    if (raster == null ||
        raster.getWidth() < w || raster.getHeight() < h)
    {
        raster = getCachedRaster(model, w, h);
        saved = raster;
    }

    // Access raster internal int array. Because we use a DirectColorModel,
    // we know the DataBuffer is of type DataBufferInt and the SampleModel
    // is SinglePixelPackedSampleModel.
    // Adjust for initial offset in DataBuffer and also for the scanline
    // stride.
    // These calls make the DataBuffer non-acceleratable, but the
    // Raster is never Stable long enough to accelerate anyway...
    DataBufferInt rasterDB = (DataBufferInt)raster.getDataBuffer();
    int[] pixels = rasterDB.getData(0);
    int off = rasterDB.getOffset();
    int scanlineStride = ((SinglePixelPackedSampleModel)
                          raster.getSampleModel()).getScanlineStride();
    int adjust = scanlineStride - w;

    fillRaster(pixels, off, adjust, x, y, w, h); // delegate to subclass

    return raster;
}
 
Example 16
Source File: MultipleGradientPaintContext.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public final Raster getRaster(int x, int y, int w, int h) {
    // If working raster is big enough, reuse it. Otherwise,
    // build a large enough new one.
    Raster raster = saved;
    if (raster == null ||
        raster.getWidth() < w || raster.getHeight() < h)
    {
        raster = getCachedRaster(model, w, h);
        saved = raster;
    }

    // Access raster internal int array. Because we use a DirectColorModel,
    // we know the DataBuffer is of type DataBufferInt and the SampleModel
    // is SinglePixelPackedSampleModel.
    // Adjust for initial offset in DataBuffer and also for the scanline
    // stride.
    // These calls make the DataBuffer non-acceleratable, but the
    // Raster is never Stable long enough to accelerate anyway...
    DataBufferInt rasterDB = (DataBufferInt)raster.getDataBuffer();
    int[] pixels = rasterDB.getData(0);
    int off = rasterDB.getOffset();
    int scanlineStride = ((SinglePixelPackedSampleModel)
                          raster.getSampleModel()).getScanlineStride();
    int adjust = scanlineStride - w;

    fillRaster(pixels, off, adjust, x, y, w, h); // delegate to subclass

    return raster;
}
 
Example 17
Source File: ByteBandedRaster.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Stores the Raster data at the specified location.
 * An ArrayIndexOutOfBounds exception will be thrown at runtime
 * if the pixel coordinate is out of bounds.
 * @param x          The X coordinate of the pixel location.
 * @param y          The Y coordinate of the pixel location.
 * @param inRaster   Raster of data to place at x,y location.
 */
public void setDataElements(int x, int y, Raster inRaster) {
    int dstOffX = inRaster.getMinX() + x;
    int dstOffY = inRaster.getMinY() + y;
    int width  = inRaster.getWidth();
    int height = inRaster.getHeight();
    if ((dstOffX < this.minX) || (dstOffY < this.minY) ||
        (dstOffX + width > this.maxX) || (dstOffY + height > this.maxY)) {
        throw new ArrayIndexOutOfBoundsException
            ("Coordinate out of bounds!");
    }

    setDataElements(dstOffX, dstOffY, width, height, inRaster);
}
 
Example 18
Source File: RenderableImageProducer.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The runnable method for this class. This will produce an image using
 * the current RenderableImage and RenderContext and send it to all the
 * ImageConsumer currently registered with this class.
 */
public void run() {
    // First get the rendered image
    RenderedImage rdrdImage;
    if (rc != null) {
        rdrdImage = rdblImage.createRendering(rc);
    } else {
        rdrdImage = rdblImage.createDefaultRendering();
    }

    // And its ColorModel
    ColorModel colorModel = rdrdImage.getColorModel();
    Raster raster = rdrdImage.getData();
    SampleModel sampleModel = raster.getSampleModel();
    DataBuffer dataBuffer = raster.getDataBuffer();

    if (colorModel == null) {
        colorModel = ColorModel.getRGBdefault();
    }
    int minX = raster.getMinX();
    int minY = raster.getMinY();
    int width = raster.getWidth();
    int height = raster.getHeight();

    Enumeration<ImageConsumer> icList;
    ImageConsumer ic;
    // Set up the ImageConsumers
    icList = ics.elements();
    while (icList.hasMoreElements()) {
        ic = icList.nextElement();
        ic.setDimensions(width,height);
        ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT |
                    ImageConsumer.COMPLETESCANLINES |
                    ImageConsumer.SINGLEPASS |
                    ImageConsumer.SINGLEFRAME);
    }

    // Get RGB pixels from the raster scanline by scanline and
    // send to consumers.
    int pix[] = new int[width];
    int i,j;
    int numBands = sampleModel.getNumBands();
    int tmpPixel[] = new int[numBands];
    for (j = 0; j < height; j++) {
        for(i = 0; i < width; i++) {
            sampleModel.getPixel(i, j, tmpPixel, dataBuffer);
            pix[i] = colorModel.getDataElement(tmpPixel, 0);
        }
        // Now send the scanline to the Consumers
        icList = ics.elements();
        while (icList.hasMoreElements()) {
            ic = icList.nextElement();
            ic.setPixels(0, j, width, 1, colorModel, pix, 0, width);
        }
    }

    // Now tell the consumers we're done.
    icList = ics.elements();
    while (icList.hasMoreElements()) {
        ic = icList.nextElement();
        ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
    }
}
 
Example 19
Source File: ImageUtils.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Compose src onto dst using the alpha of sel to interpolate between the two.
 * I can't think of a way to do this using AlphaComposite.
    * @param src the source raster
    * @param dst the destination raster
    * @param sel the mask raster
 */
public static void composeThroughMask(Raster src, WritableRaster dst, Raster sel) {
	int x = src.getMinX();
	int y = src.getMinY();
	int w = src.getWidth();
	int h = src.getHeight();

	int srcRGB[] = null;
	int selRGB[] = null;
	int dstRGB[] = null;

	for ( int i = 0; i < h; i++ ) {
		srcRGB = src.getPixels(x, y, w, 1, srcRGB);
		selRGB = sel.getPixels(x, y, w, 1, selRGB);
		dstRGB = dst.getPixels(x, y, w, 1, dstRGB);

		int k = x;
		for ( int j = 0; j < w; j++ ) {
			int sr = srcRGB[k];
			int dir = dstRGB[k];
			int sg = srcRGB[k+1];
			int dig = dstRGB[k+1];
			int sb = srcRGB[k+2];
			int dib = dstRGB[k+2];
			int sa = srcRGB[k+3];
			int dia = dstRGB[k+3];

			float a = selRGB[k+3]/255f;
			float ac = 1-a;

			dstRGB[k] = (int)(a*sr + ac*dir); 
			dstRGB[k+1] = (int)(a*sg + ac*dig); 
			dstRGB[k+2] = (int)(a*sb + ac*dib); 
			dstRGB[k+3] = (int)(a*sa + ac*dia);
			k += 4;
		}

		dst.setPixels(x, y, w, 1, dstRGB);
		y++;
	}
}
 
Example 20
Source File: BytePackedRaster.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Copies pixels from Raster srcRaster to this WritableRaster.
 * For each (x, y) address in srcRaster, the corresponding pixel
 * is copied to address (x+dx, y+dy) in this WritableRaster,
 * unless (x+dx, y+dy) falls outside the bounds of this raster.
 * srcRaster must have the same number of bands as this WritableRaster.
 * The copy is a simple copy of source samples to the corresponding
 * destination samples.  For details, see
 * {@link WritableRaster#setRect(Raster)}.
 *
 * @param dx        The X translation factor from src space to dst space
 *                  of the copy.
 * @param dy        The Y translation factor from src space to dst space
 *                  of the copy.
 * @param srcRaster The Raster from which to copy pixels.
 */
public void setRect(int dx, int dy, Raster srcRaster) {
    // Check if we can use fast code
    if (!(srcRaster instanceof BytePackedRaster) ||
        ((BytePackedRaster)srcRaster).pixelBitStride != pixelBitStride) {
        super.setRect(dx, dy, srcRaster);
        return;
    }

    int width  = srcRaster.getWidth();
    int height = srcRaster.getHeight();
    int srcOffX = srcRaster.getMinX();
    int srcOffY = srcRaster.getMinY();
    int dstOffX = dx+srcOffX;
    int dstOffY = dy+srcOffY;

    // Clip to this raster
    if (dstOffX < this.minX) {
        int skipX = this.minX - dstOffX;
        width -= skipX;
        srcOffX += skipX;
        dstOffX = this.minX;
    }
    if (dstOffY < this.minY) {
        int skipY = this.minY - dstOffY;
        height -= skipY;
        srcOffY += skipY;
        dstOffY = this.minY;
    }
    if (dstOffX+width > this.maxX) {
        width = this.maxX - dstOffX;
    }
    if (dstOffY+height > this.maxY) {
        height = this.maxY - dstOffY;
    }

    setDataElements(dstOffX, dstOffY,
                    srcOffX, srcOffY,
                    width, height,
                    (BytePackedRaster)srcRaster);
}