bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift/*
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift *
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * This code is free software; you can redistribute it and/or modify it
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * under the terms of the GNU General Public License version 2 only, as
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * published by the Free Software Foundation. Oracle designates this
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * particular file as subject to the "Classpath" exception as provided
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * by Oracle in the LICENSE file that accompanied this code.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift *
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * This code is distributed in the hope that it will be useful, but WITHOUT
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * version 2 for more details (a copy is included in the LICENSE file that
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * accompanied this code).
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift *
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * You should have received a copy of the GNU General Public License version
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * 2 along with this work; if not, write to the Free Software Foundation,
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift *
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * or visit www.oracle.com if you need additional information or have any
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * questions.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift */
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swiftpackage java.awt.image;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swiftimport java.util.Hashtable;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swiftimport java.awt.image.ImageProducer;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swiftimport java.awt.image.ImageConsumer;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swiftimport java.awt.image.ColorModel;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swiftimport java.awt.Image;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift/**
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * The PixelGrabber class implements an ImageConsumer which can be attached
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * to an Image or ImageProducer object to retrieve a subset of the pixels
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * in that image. Here is an example:
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * <pre>
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift *
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * public void handlesinglepixel(int x, int y, int pixel) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * int alpha = (pixel >> 24) & 0xff;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * int red = (pixel >> 16) & 0xff;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * int green = (pixel >> 8) & 0xff;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * int blue = (pixel ) & 0xff;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * // Deal with the pixel as necessary...
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * }
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift *
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * public void handlepixels(Image img, int x, int y, int w, int h) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * int[] pixels = new int[w * h];
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * try {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * pg.grabPixels();
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * } catch (InterruptedException e) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * System.err.println("interrupted waiting for pixels!");
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * return;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * }
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * System.err.println("image fetch aborted or errored");
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * return;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * }
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * for (int j = 0; j < h; j++) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * for (int i = 0; i < w; i++) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * handlesinglepixel(x+i, y+j, pixels[j * w + i]);
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * }
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * }
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * }
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift *
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * </pre>
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift *
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see ColorModel#getRGBdefault
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift *
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @author Jim Graham
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift */
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swiftpublic class PixelGrabber implements ImageConsumer {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift ImageProducer producer;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift int dstX;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift int dstY;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift int dstW;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift int dstH;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift ColorModel imageModel;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift byte[] bytePixels;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift int[] intPixels;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift int dstOff;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift int dstScan;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift private boolean grabbing;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift private int flags;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift private static final int GRABBEDBITS = (ImageObserver.FRAMEBITS
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift | ImageObserver.ALLBITS);
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift private static final int DONEBITS = (GRABBEDBITS
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift | ImageObserver.ERROR);
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift /**
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Create a PixelGrabber object to grab the (x, y, w, h) rectangular
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * section of pixels from the specified image into the given array.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * The pixels are stored into the array in the default RGB ColorModel.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * The RGB data for pixel (i, j) where (i, j) is inside the rectangle
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * (x, y, w, h) is stored in the array at
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * <tt>pix[(j - y) * scansize + (i - x) + off]</tt>.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see ColorModel#getRGBdefault
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param img the image to retrieve pixels from
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param x the x coordinate of the upper left corner of the rectangle
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * of pixels to retrieve from the image, relative to the default
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * (unscaled) size of the image
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param y the y coordinate of the upper left corner of the rectangle
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * of pixels to retrieve from the image
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param w the width of the rectangle of pixels to retrieve
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param h the height of the rectangle of pixels to retrieve
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param pix the array of integers which are to be used to hold the
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * RGB pixels retrieved from the image
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param off the offset into the array of where to store the first pixel
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param scansize the distance from one row of pixels to the next in
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * the array
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift */
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift public PixelGrabber(Image img, int x, int y, int w, int h,
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift int[] pix, int off, int scansize) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift this(img.getSource(), x, y, w, h, pix, off, scansize);
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift }
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift /**
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Create a PixelGrabber object to grab the (x, y, w, h) rectangular
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * section of pixels from the image produced by the specified
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * ImageProducer into the given array.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * The pixels are stored into the array in the default RGB ColorModel.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * The RGB data for pixel (i, j) where (i, j) is inside the rectangle
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * (x, y, w, h) is stored in the array at
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * <tt>pix[(j - y) * scansize + (i - x) + off]</tt>.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param ip the <code>ImageProducer</code> that produces the
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * image from which to retrieve pixels
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param x the x coordinate of the upper left corner of the rectangle
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * of pixels to retrieve from the image, relative to the default
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * (unscaled) size of the image
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param y the y coordinate of the upper left corner of the rectangle
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * of pixels to retrieve from the image
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param w the width of the rectangle of pixels to retrieve
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param h the height of the rectangle of pixels to retrieve
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param pix the array of integers which are to be used to hold the
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * RGB pixels retrieved from the image
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param off the offset into the array of where to store the first pixel
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param scansize the distance from one row of pixels to the next in
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * the array
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @see ColorModel#getRGBdefault
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift */
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift public PixelGrabber(ImageProducer ip, int x, int y, int w, int h,
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift int[] pix, int off, int scansize) {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift producer = ip;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift dstX = x;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift dstY = y;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift dstW = w;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift dstH = h;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift dstOff = off;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift dstScan = scansize;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift intPixels = pix;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift imageModel = ColorModel.getRGBdefault();
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift }
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift /**
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Create a PixelGrabber object to grab the (x, y, w, h) rectangular
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * section of pixels from the specified image. The pixels are
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * accumulated in the original ColorModel if the same ColorModel
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * is used for every call to setPixels, otherwise the pixels are
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * accumulated in the default RGB ColorModel. If the forceRGB
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * parameter is true, then the pixels will be accumulated in the
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * default RGB ColorModel anyway. A buffer is allocated by the
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * PixelGrabber to hold the pixels in either case. If (w < 0) or
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * (h < 0), then they will default to the remaining width and
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * height of the source data when that information is delivered.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @param img the image to retrieve the image data from
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @param x the x coordinate of the upper left corner of the rectangle
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * of pixels to retrieve from the image, relative to the default
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * (unscaled) size of the image
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @param y the y coordinate of the upper left corner of the rectangle
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * of pixels to retrieve from the image
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @param w the width of the rectangle of pixels to retrieve
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @param h the height of the rectangle of pixels to retrieve
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @param forceRGB true if the pixels should always be converted to
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * the default RGB ColorModel
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift */
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift public PixelGrabber(Image img, int x, int y, int w, int h,
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift boolean forceRGB)
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift producer = img.getSource();
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift dstX = x;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift dstY = y;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift dstW = w;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift dstH = h;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift if (forceRGB) {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift imageModel = ColorModel.getRGBdefault();
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift }
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift }
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift /**
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Request the PixelGrabber to start fetching the pixels.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift */
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift public synchronized void startGrabbing() {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift if ((flags & DONEBITS) != 0) {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift return;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift }
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift if (!grabbing) {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift grabbing = true;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift flags &= ~(ImageObserver.ABORT);
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift producer.startProduction(this);
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift }
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift }
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift /**
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Request the PixelGrabber to abort the image fetch.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift */
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift public synchronized void abortGrabbing() {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift imageComplete(IMAGEABORTED);
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift }
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift /**
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Request the Image or ImageProducer to start delivering pixels and
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * wait for all of the pixels in the rectangle of interest to be
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * delivered.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @return true if the pixels were successfully grabbed, false on
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * abort, error or timeout
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @exception InterruptedException
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Another thread has interrupted this thread.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift */
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift public boolean grabPixels() throws InterruptedException {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift return grabPixels(0);
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift }
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift /**
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Request the Image or ImageProducer to start delivering pixels and
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * wait for all of the pixels in the rectangle of interest to be
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * delivered or until the specified timeout has elapsed. This method
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * behaves in the following ways, depending on the value of
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * <code>ms</code>:
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * <ul>
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * <li> If <code>ms</code> == 0, waits until all pixels are delivered
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * <li> If <code>ms</code> > 0, waits until all pixels are delivered
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * as timeout expires.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * <li> If <code>ms</code> < 0, returns <code>true</code> if all pixels
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * are grabbed, <code>false</code> otherwise and does not wait.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * </ul>
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @param ms the number of milliseconds to wait for the image pixels
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * to arrive before timing out
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @return true if the pixels were successfully grabbed, false on
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * abort, error or timeout
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @exception InterruptedException
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Another thread has interrupted this thread.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift */
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift public synchronized boolean grabPixels(long ms)
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift throws InterruptedException
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift if ((flags & DONEBITS) != 0) {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift return (flags & GRABBEDBITS) != 0;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift }
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift long end = ms + System.currentTimeMillis();
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift if (!grabbing) {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift grabbing = true;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift flags &= ~(ImageObserver.ABORT);
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift producer.startProduction(this);
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift }
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift while (grabbing) {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift long timeout;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift if (ms == 0) {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift timeout = 0;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift } else {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift timeout = end - System.currentTimeMillis();
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift if (timeout <= 0) {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift break;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift }
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift }
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift wait(timeout);
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift }
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift return (flags & GRABBEDBITS) != 0;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift }
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift /**
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Return the status of the pixels. The ImageObserver flags
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * representing the available pixel information are returned.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @return the bitwise OR of all relevant ImageObserver flags
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * @see ImageObserver
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift */
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift public synchronized int getStatus() {
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift return flags;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift }
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift /**
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Get the width of the pixel buffer (after adjusting for image width).
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * If no width was specified for the rectangle of pixels to grab then
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * then this information will only be available after the image has
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * delivered the dimensions.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @return the final width used for the pixel buffer or -1 if the width
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * is not yet known
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see #getStatus
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift */
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift public synchronized int getWidth() {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift return (dstW < 0) ? -1 : dstW;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift }
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift /**
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Get the height of the pixel buffer (after adjusting for image height).
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * If no width was specified for the rectangle of pixels to grab then
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * then this information will only be available after the image has
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * delivered the dimensions.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @return the final height used for the pixel buffer or -1 if the height
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * is not yet known
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see #getStatus
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift */
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift public synchronized int getHeight() {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift return (dstH < 0) ? -1 : dstH;
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift }
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift /**
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Get the pixel buffer. If the PixelGrabber was not constructed
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * with an explicit pixel buffer to hold the pixels then this method
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * will return null until the size and format of the image data is
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * known.
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * Since the PixelGrabber may fall back on accumulating the data
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * in the default RGB ColorModel at any time if the source image
4a622c45ad6426287954754f34fc3ab8a4b9c2c5matthew_swift * uses more than one ColorModel to deliver the data, the array
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * object returned by this method may change over time until the
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * image grab is complete.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @return either a byte array or an int array
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see #getStatus
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see #setPixels(int, int, int, int, ColorModel, byte[], int, int)
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see #setPixels(int, int, int, int, ColorModel, int[], int, int)
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift */
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift public synchronized Object getPixels() {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift return (bytePixels == null)
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift ? ((Object) intPixels)
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift : ((Object) bytePixels);
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift }
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift /**
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Get the ColorModel for the pixels stored in the array. If the
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * PixelGrabber was constructed with an explicit pixel buffer then
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * this method will always return the default RGB ColorModel,
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * otherwise it may return null until the ColorModel used by the
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * ImageProducer is known.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Since the PixelGrabber may fall back on accumulating the data
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * in the default RGB ColorModel at any time if the source image
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * uses more than one ColorModel to deliver the data, the ColorModel
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * object returned by this method may change over time until the
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * image grab is complete and may not reflect any of the ColorModel
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * objects that was used by the ImageProducer to deliver the pixels.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @return the ColorModel object used for storing the pixels
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see #getStatus
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see ColorModel#getRGBdefault
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @see #setColorModel(ColorModel)
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift */
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift public synchronized ColorModel getColorModel() {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift return imageModel;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift }
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift /**
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * The setDimensions method is part of the ImageConsumer API which
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * this class must implement to retrieve the pixels.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * <p>
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Note: This method is intended to be called by the ImageProducer
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * of the Image whose pixels are being grabbed. Developers using
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * this class to retrieve pixels from an image should avoid calling
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * this method directly since that operation could result in problems
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * with retrieving the requested pixels.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param width the width of the dimension
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param height the height of the dimension
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift */
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift public void setDimensions(int width, int height) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift if (dstW < 0) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift dstW = width - dstX;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift }
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift if (dstH < 0) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift dstH = height - dstY;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift }
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift if (dstW <= 0 || dstH <= 0) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift imageComplete(STATICIMAGEDONE);
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift } else if (intPixels == null &&
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift imageModel == ColorModel.getRGBdefault()) {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift intPixels = new int[dstW * dstH];
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift dstScan = dstW;
dstOff = 0;
}
flags |= (ImageObserver.WIDTH | ImageObserver.HEIGHT);
}
/**
* The setHints method is part of the ImageConsumer API which
* this class must implement to retrieve the pixels.
* <p>
* Note: This method is intended to be called by the ImageProducer
* of the Image whose pixels are being grabbed. Developers using
* this class to retrieve pixels from an image should avoid calling
* this method directly since that operation could result in problems
* with retrieving the requested pixels.
* @param hints a set of hints used to process the pixels
*/
public void setHints(int hints) {
return;
}
/**
* The setProperties method is part of the ImageConsumer API which
* this class must implement to retrieve the pixels.
* <p>
* Note: This method is intended to be called by the ImageProducer
* of the Image whose pixels are being grabbed. Developers using
* this class to retrieve pixels from an image should avoid calling
* this method directly since that operation could result in problems
* with retrieving the requested pixels.
* @param props the list of properties
*/
public void setProperties(Hashtable<?,?> props) {
return;
}
/**
* The setColorModel method is part of the ImageConsumer API which
* this class must implement to retrieve the pixels.
* <p>
* Note: This method is intended to be called by the ImageProducer
* of the Image whose pixels are being grabbed. Developers using
* this class to retrieve pixels from an image should avoid calling
* this method directly since that operation could result in problems
* with retrieving the requested pixels.
* @param model the specified <code>ColorModel</code>
* @see #getColorModel
*/
public void setColorModel(ColorModel model) {
return;
}
private void convertToRGB() {
int size = dstW * dstH;
int newpixels[] = new int[size];
if (bytePixels != null) {
for (int i = 0; i < size; i++) {
newpixels[i] = imageModel.getRGB(bytePixels[i] & 0xff);
}
} else if (intPixels != null) {
for (int i = 0; i < size; i++) {
newpixels[i] = imageModel.getRGB(intPixels[i]);
}
}
bytePixels = null;
intPixels = newpixels;
dstScan = dstW;
dstOff = 0;
imageModel = ColorModel.getRGBdefault();
}
/**
* The setPixels method is part of the ImageConsumer API which
* this class must implement to retrieve the pixels.
* <p>
* Note: This method is intended to be called by the ImageProducer
* of the Image whose pixels are being grabbed. Developers using
* this class to retrieve pixels from an image should avoid calling
* this method directly since that operation could result in problems
* with retrieving the requested pixels.
* @param srcX the X coordinate of the upper-left corner
* of the area of pixels to be set
* @param srcY the Y coordinate of the upper-left corner
* of the area of pixels to be set
* @param srcW the width of the area of pixels
* @param srcH the height of the area of pixels
* @param model the specified <code>ColorModel</code>
* @param pixels the array of pixels
* @param srcOff the offset into the pixels array
* @param srcScan the distance from one row of pixels to the next
* in the pixels array
* @see #getPixels
*/
public void setPixels(int srcX, int srcY, int srcW, int srcH,
ColorModel model,
byte pixels[], int srcOff, int srcScan) {
if (srcY < dstY) {
int diff = dstY - srcY;
if (diff >= srcH) {
return;
}
srcOff += srcScan * diff;
srcY += diff;
srcH -= diff;
}
if (srcY + srcH > dstY + dstH) {
srcH = (dstY + dstH) - srcY;
if (srcH <= 0) {
return;
}
}
if (srcX < dstX) {
int diff = dstX - srcX;
if (diff >= srcW) {
return;
}
srcOff += diff;
srcX += diff;
srcW -= diff;
}
if (srcX + srcW > dstX + dstW) {
srcW = (dstX + dstW) - srcX;
if (srcW <= 0) {
return;
}
}
int dstPtr = dstOff + (srcY - dstY) * dstScan + (srcX - dstX);
if (intPixels == null) {
if (bytePixels == null) {
bytePixels = new byte[dstW * dstH];
dstScan = dstW;
dstOff = 0;
imageModel = model;
} else if (imageModel != model) {
convertToRGB();
}
if (bytePixels != null) {
for (int h = srcH; h > 0; h--) {
System.arraycopy(pixels, srcOff, bytePixels, dstPtr, srcW);
srcOff += srcScan;
dstPtr += dstScan;
}
}
}
if (intPixels != null) {
int dstRem = dstScan - srcW;
int srcRem = srcScan - srcW;
for (int h = srcH; h > 0; h--) {
for (int w = srcW; w > 0; w--) {
intPixels[dstPtr++] = model.getRGB(pixels[srcOff++]&0xff);
}
srcOff += srcRem;
dstPtr += dstRem;
}
}
flags |= ImageObserver.SOMEBITS;
}
/**
* The setPixels method is part of the ImageConsumer API which
* this class must implement to retrieve the pixels.
* <p>
* Note: This method is intended to be called by the ImageProducer
* of the Image whose pixels are being grabbed. Developers using
* this class to retrieve pixels from an image should avoid calling
* this method directly since that operation could result in problems
* with retrieving the requested pixels.
* @param srcX the X coordinate of the upper-left corner
* of the area of pixels to be set
* @param srcY the Y coordinate of the upper-left corner
* of the area of pixels to be set
* @param srcW the width of the area of pixels
* @param srcH the height of the area of pixels
* @param model the specified <code>ColorModel</code>
* @param pixels the array of pixels
* @param srcOff the offset into the pixels array
* @param srcScan the distance from one row of pixels to the next
* in the pixels array
* @see #getPixels
*/
public void setPixels(int srcX, int srcY, int srcW, int srcH,
ColorModel model,
int pixels[], int srcOff, int srcScan) {
if (srcY < dstY) {
int diff = dstY - srcY;
if (diff >= srcH) {
return;
}
srcOff += srcScan * diff;
srcY += diff;
srcH -= diff;
}
if (srcY + srcH > dstY + dstH) {
srcH = (dstY + dstH) - srcY;
if (srcH <= 0) {
return;
}
}
if (srcX < dstX) {
int diff = dstX - srcX;
if (diff >= srcW) {
return;
}
srcOff += diff;
srcX += diff;
srcW -= diff;
}
if (srcX + srcW > dstX + dstW) {
srcW = (dstX + dstW) - srcX;
if (srcW <= 0) {
return;
}
}
if (intPixels == null) {
if (bytePixels == null) {
intPixels = new int[dstW * dstH];
dstScan = dstW;
dstOff = 0;
imageModel = model;
} else {
convertToRGB();
}
}
int dstPtr = dstOff + (srcY - dstY) * dstScan + (srcX - dstX);
if (imageModel == model) {
for (int h = srcH; h > 0; h--) {
System.arraycopy(pixels, srcOff, intPixels, dstPtr, srcW);
srcOff += srcScan;
dstPtr += dstScan;
}
} else {
if (imageModel != ColorModel.getRGBdefault()) {
convertToRGB();
}
int dstRem = dstScan - srcW;
int srcRem = srcScan - srcW;
for (int h = srcH; h > 0; h--) {
for (int w = srcW; w > 0; w--) {
intPixels[dstPtr++] = model.getRGB(pixels[srcOff++]);
}
srcOff += srcRem;
dstPtr += dstRem;
}
}
flags |= ImageObserver.SOMEBITS;
}
/**
* The imageComplete method is part of the ImageConsumer API which
* this class must implement to retrieve the pixels.
* <p>
* Note: This method is intended to be called by the ImageProducer
* of the Image whose pixels are being grabbed. Developers using
* this class to retrieve pixels from an image should avoid calling
* this method directly since that operation could result in problems
* with retrieving the requested pixels.
* @param status the status of image loading
*/
public synchronized void imageComplete(int status) {
grabbing = false;
switch (status) {
default:
case IMAGEERROR:
flags |= ImageObserver.ERROR | ImageObserver.ABORT;
break;
case IMAGEABORTED:
flags |= ImageObserver.ABORT;
break;
case STATICIMAGEDONE:
flags |= ImageObserver.ALLBITS;
break;
case SINGLEFRAMEDONE:
flags |= ImageObserver.FRAMEBITS;
break;
}
producer.removeConsumer(this);
notifyAll();
}
/**
* Returns the status of the pixels. The ImageObserver flags
* representing the available pixel information are returned.
* This method and {@link #getStatus() getStatus} have the
* same implementation, but <code>getStatus</code> is the
* preferred method because it conforms to the convention of
* naming information-retrieval methods with the form
* "getXXX".
* @return the bitwise OR of all relevant ImageObserver flags
* @see ImageObserver
* @see #getStatus()
*/
public synchronized int status() {
return flags;
}
}