0N/A/*
2362N/A * Copyright (c) 1995, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.awt.image;
0N/A
0N/Aimport java.awt.image.ImageConsumer;
0N/Aimport java.awt.image.ImageProducer;
0N/Aimport java.awt.image.ColorModel;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.Vector;
0N/Aimport java.util.Enumeration;
0N/A
0N/A/**
0N/A * This class is an implementation of the ImageProducer interface which
0N/A * uses an array to produce pixel values for an Image. Here is an example
0N/A * which calculates a 100x100 image representing a fade from black to blue
0N/A * along the X axis and a fade from black to red along the Y axis:
0N/A * <pre>
0N/A *
0N/A * int w = 100;
0N/A * int h = 100;
0N/A * int pix[] = new int[w * h];
0N/A * int index = 0;
0N/A * for (int y = 0; y < h; y++) {
0N/A * int red = (y * 255) / (h - 1);
0N/A * for (int x = 0; x < w; x++) {
0N/A * int blue = (x * 255) / (w - 1);
0N/A * pix[index++] = (255 << 24) | (red << 16) | blue;
0N/A * }
0N/A * }
0N/A * Image img = createImage(new MemoryImageSource(w, h, pix, 0, w));
0N/A *
0N/A * </pre>
0N/A * The MemoryImageSource is also capable of managing a memory image which
0N/A * varies over time to allow animation or custom rendering. Here is an
0N/A * example showing how to set up the animation source and signal changes
0N/A * in the data (adapted from the MemoryAnimationSourceDemo by Garth Dickie):
0N/A * <pre>
0N/A *
0N/A * int pixels[];
0N/A * MemoryImageSource source;
0N/A *
0N/A * public void init() {
0N/A * int width = 50;
0N/A * int height = 50;
0N/A * int size = width * height;
0N/A * pixels = new int[size];
0N/A *
0N/A * int value = getBackground().getRGB();
0N/A * for (int i = 0; i < size; i++) {
0N/A * pixels[i] = value;
0N/A * }
0N/A *
0N/A * source = new MemoryImageSource(width, height, pixels, 0, width);
0N/A * source.setAnimated(true);
0N/A * image = createImage(source);
0N/A * }
0N/A *
0N/A * public void run() {
0N/A * Thread me = Thread.currentThread( );
0N/A * me.setPriority(Thread.MIN_PRIORITY);
0N/A *
0N/A * while (true) {
0N/A * try {
0N/A * Thread.sleep(10);
0N/A * } catch( InterruptedException e ) {
0N/A * return;
0N/A * }
0N/A *
0N/A * // Modify the values in the pixels array at (x, y, w, h)
0N/A *
0N/A * // Send the new data to the interested ImageConsumers
0N/A * source.newPixels(x, y, w, h);
0N/A * }
0N/A * }
0N/A *
0N/A * </pre>
0N/A *
0N/A * @see ImageProducer
0N/A *
0N/A * @author Jim Graham
0N/A * @author Animation capabilities inspired by the
0N/A * MemoryAnimationSource class written by Garth Dickie
0N/A */
0N/Apublic class MemoryImageSource implements ImageProducer {
0N/A int width;
0N/A int height;
0N/A ColorModel model;
0N/A Object pixels;
0N/A int pixeloffset;
0N/A int pixelscan;
0N/A Hashtable properties;
0N/A Vector theConsumers = new Vector();
0N/A boolean animating;
0N/A boolean fullbuffers;
0N/A
0N/A /**
0N/A * Constructs an ImageProducer object which uses an array of bytes
0N/A * to produce data for an Image object.
0N/A * @param w the width of the rectangle of pixels
0N/A * @param h the height of the rectangle of pixels
0N/A * @param cm the specified <code>ColorModel</code>
0N/A * @param pix an array of pixels
0N/A * @param off the offset into the array of where to store the
0N/A * first pixel
0N/A * @param scan the distance from one row of pixels to the next in
0N/A * the array
0N/A * @see java.awt.Component#createImage
0N/A */
0N/A public MemoryImageSource(int w, int h, ColorModel cm,
0N/A byte[] pix, int off, int scan) {
0N/A initialize(w, h, cm, (Object) pix, off, scan, null);
0N/A }
0N/A
0N/A /**
0N/A * Constructs an ImageProducer object which uses an array of bytes
0N/A * to produce data for an Image object.
0N/A * @param w the width of the rectangle of pixels
0N/A * @param h the height of the rectangle of pixels
0N/A * @param cm the specified <code>ColorModel</code>
0N/A * @param pix an array of pixels
0N/A * @param off the offset into the array of where to store the
0N/A * first pixel
0N/A * @param scan the distance from one row of pixels to the next in
0N/A * the array
0N/A * @param props a list of properties that the <code>ImageProducer</code>
0N/A * uses to process an image
0N/A * @see java.awt.Component#createImage
0N/A */
0N/A public MemoryImageSource(int w, int h, ColorModel cm,
0N/A byte[] pix, int off, int scan,
0N/A Hashtable<?,?> props)
0N/A {
0N/A initialize(w, h, cm, (Object) pix, off, scan, props);
0N/A }
0N/A
0N/A /**
0N/A * Constructs an ImageProducer object which uses an array of integers
0N/A * to produce data for an Image object.
0N/A * @param w the width of the rectangle of pixels
0N/A * @param h the height of the rectangle of pixels
0N/A * @param cm the specified <code>ColorModel</code>
0N/A * @param pix an array of pixels
0N/A * @param off the offset into the array of where to store the
0N/A * first pixel
0N/A * @param scan the distance from one row of pixels to the next in
0N/A * the array
0N/A * @see java.awt.Component#createImage
0N/A */
0N/A public MemoryImageSource(int w, int h, ColorModel cm,
0N/A int[] pix, int off, int scan) {
0N/A initialize(w, h, cm, (Object) pix, off, scan, null);
0N/A }
0N/A
0N/A /**
0N/A * Constructs an ImageProducer object which uses an array of integers
0N/A * to produce data for an Image object.
0N/A * @param w the width of the rectangle of pixels
0N/A * @param h the height of the rectangle of pixels
0N/A * @param cm the specified <code>ColorModel</code>
0N/A * @param pix an array of pixels
0N/A * @param off the offset into the array of where to store the
0N/A * first pixel
0N/A * @param scan the distance from one row of pixels to the next in
0N/A * the array
0N/A * @param props a list of properties that the <code>ImageProducer</code>
0N/A * uses to process an image
0N/A * @see java.awt.Component#createImage
0N/A */
0N/A public MemoryImageSource(int w, int h, ColorModel cm,
0N/A int[] pix, int off, int scan,
0N/A Hashtable<?,?> props)
0N/A {
0N/A initialize(w, h, cm, (Object) pix, off, scan, props);
0N/A }
0N/A
0N/A private void initialize(int w, int h, ColorModel cm,
0N/A Object pix, int off, int scan, Hashtable props) {
0N/A width = w;
0N/A height = h;
0N/A model = cm;
0N/A pixels = pix;
0N/A pixeloffset = off;
0N/A pixelscan = scan;
0N/A if (props == null) {
0N/A props = new Hashtable();
0N/A }
0N/A properties = props;
0N/A }
0N/A
0N/A /**
0N/A * Constructs an ImageProducer object which uses an array of integers
0N/A * in the default RGB ColorModel to produce data for an Image object.
0N/A * @param w the width of the rectangle of pixels
0N/A * @param h the height of the rectangle of pixels
0N/A * @param pix an array of pixels
0N/A * @param off the offset into the array of where to store the
0N/A * first pixel
0N/A * @param scan the distance from one row of pixels to the next in
0N/A * the array
0N/A * @see java.awt.Component#createImage
0N/A * @see ColorModel#getRGBdefault
0N/A */
0N/A public MemoryImageSource(int w, int h, int pix[], int off, int scan) {
0N/A initialize(w, h, ColorModel.getRGBdefault(),
0N/A (Object) pix, off, scan, null);
0N/A }
0N/A
0N/A /**
0N/A * Constructs an ImageProducer object which uses an array of integers
0N/A * in the default RGB ColorModel to produce data for an Image object.
0N/A * @param w the width of the rectangle of pixels
0N/A * @param h the height of the rectangle of pixels
0N/A * @param pix an array of pixels
0N/A * @param off the offset into the array of where to store the
0N/A * first pixel
0N/A * @param scan the distance from one row of pixels to the next in
0N/A * the array
0N/A * @param props a list of properties that the <code>ImageProducer</code>
0N/A * uses to process an image
0N/A * @see java.awt.Component#createImage
0N/A * @see ColorModel#getRGBdefault
0N/A */
0N/A public MemoryImageSource(int w, int h, int pix[], int off, int scan,
0N/A Hashtable<?,?> props)
0N/A {
0N/A initialize(w, h, ColorModel.getRGBdefault(),
0N/A (Object) pix, off, scan, props);
0N/A }
0N/A
0N/A /**
0N/A * Adds an ImageConsumer to the list of consumers interested in
0N/A * data for this image.
0N/A * @param ic the specified <code>ImageConsumer</code>
0N/A * @throws NullPointerException if the specified
0N/A * <code>ImageConsumer</code> is null
0N/A * @see ImageConsumer
0N/A */
0N/A public synchronized void addConsumer(ImageConsumer ic) {
0N/A if (theConsumers.contains(ic)) {
0N/A return;
0N/A }
0N/A theConsumers.addElement(ic);
0N/A try {
0N/A initConsumer(ic);
0N/A sendPixels(ic, 0, 0, width, height);
0N/A if (isConsumer(ic)) {
0N/A ic.imageComplete(animating
0N/A ? ImageConsumer.SINGLEFRAMEDONE
0N/A : ImageConsumer.STATICIMAGEDONE);
0N/A if (!animating && isConsumer(ic)) {
0N/A ic.imageComplete(ImageConsumer.IMAGEERROR);
0N/A removeConsumer(ic);
0N/A }
0N/A }
0N/A } catch (Exception e) {
0N/A if (isConsumer(ic)) {
0N/A ic.imageComplete(ImageConsumer.IMAGEERROR);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Determines if an ImageConsumer is on the list of consumers currently
0N/A * interested in data for this image.
0N/A * @param ic the specified <code>ImageConsumer</code>
0N/A * @return <code>true</code> if the <code>ImageConsumer</code>
0N/A * is on the list; <code>false</code> otherwise.
0N/A * @see ImageConsumer
0N/A */
0N/A public synchronized boolean isConsumer(ImageConsumer ic) {
0N/A return theConsumers.contains(ic);
0N/A }
0N/A
0N/A /**
0N/A * Removes an ImageConsumer from the list of consumers interested in
0N/A * data for this image.
0N/A * @param ic the specified <code>ImageConsumer</code>
0N/A * @see ImageConsumer
0N/A */
0N/A public synchronized void removeConsumer(ImageConsumer ic) {
0N/A theConsumers.removeElement(ic);
0N/A }
0N/A
0N/A /**
0N/A * Adds an ImageConsumer to the list of consumers interested in
0N/A * data for this image and immediately starts delivery of the
0N/A * image data through the ImageConsumer interface.
0N/A * @param ic the specified <code>ImageConsumer</code>
0N/A * image data through the ImageConsumer interface.
0N/A * @see ImageConsumer
0N/A */
0N/A public void startProduction(ImageConsumer ic) {
0N/A addConsumer(ic);
0N/A }
0N/A
0N/A /**
0N/A * Requests that a given ImageConsumer have the image data delivered
0N/A * one more time in top-down, left-right order.
0N/A * @param ic the specified <code>ImageConsumer</code>
0N/A * @see ImageConsumer
0N/A */
0N/A public void requestTopDownLeftRightResend(ImageConsumer ic) {
0N/A // Ignored. The data is either single frame and already in TDLR
0N/A // format or it is multi-frame and TDLR resends aren't critical.
0N/A }
0N/A
0N/A /**
0N/A * Changes this memory image into a multi-frame animation or a
0N/A * single-frame static image depending on the animated parameter.
0N/A * <p>This method should be called immediately after the
0N/A * MemoryImageSource is constructed and before an image is
0N/A * created with it to ensure that all ImageConsumers will
0N/A * receive the correct multi-frame data. If an ImageConsumer
0N/A * is added to this ImageProducer before this flag is set then
0N/A * that ImageConsumer will see only a snapshot of the pixel
0N/A * data that was available when it connected.
0N/A * @param animated <code>true</code> if the image is a
0N/A * multi-frame animation
0N/A */
0N/A public synchronized void setAnimated(boolean animated) {
0N/A this.animating = animated;
0N/A if (!animating) {
0N/A Enumeration enum_ = theConsumers.elements();
0N/A while (enum_.hasMoreElements()) {
0N/A ImageConsumer ic = (ImageConsumer) enum_.nextElement();
0N/A ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
0N/A if (isConsumer(ic)) {
0N/A ic.imageComplete(ImageConsumer.IMAGEERROR);
0N/A }
0N/A }
0N/A theConsumers.removeAllElements();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Specifies whether this animated memory image should always be
0N/A * updated by sending the complete buffer of pixels whenever
0N/A * there is a change.
0N/A * This flag is ignored if the animation flag is not turned on
0N/A * through the setAnimated() method.
0N/A * <p>This method should be called immediately after the
0N/A * MemoryImageSource is constructed and before an image is
0N/A * created with it to ensure that all ImageConsumers will
0N/A * receive the correct pixel delivery hints.
0N/A * @param fullbuffers <code>true</code> if the complete pixel
0N/A * buffer should always
0N/A * be sent
0N/A * @see #setAnimated
0N/A */
0N/A public synchronized void setFullBufferUpdates(boolean fullbuffers) {
0N/A if (this.fullbuffers == fullbuffers) {
0N/A return;
0N/A }
0N/A this.fullbuffers = fullbuffers;
0N/A if (animating) {
0N/A Enumeration enum_ = theConsumers.elements();
0N/A while (enum_.hasMoreElements()) {
0N/A ImageConsumer ic = (ImageConsumer) enum_.nextElement();
0N/A ic.setHints(fullbuffers
0N/A ? (ImageConsumer.TOPDOWNLEFTRIGHT |
0N/A ImageConsumer.COMPLETESCANLINES)
0N/A : ImageConsumer.RANDOMPIXELORDER);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sends a whole new buffer of pixels to any ImageConsumers that
0N/A * are currently interested in the data for this image and notify
0N/A * them that an animation frame is complete.
0N/A * This method only has effect if the animation flag has been
0N/A * turned on through the setAnimated() method.
0N/A * @see #newPixels(int, int, int, int, boolean)
0N/A * @see ImageConsumer
0N/A * @see #setAnimated
0N/A */
0N/A public void newPixels() {
0N/A newPixels(0, 0, width, height, true);
0N/A }
0N/A
0N/A /**
0N/A * Sends a rectangular region of the buffer of pixels to any
0N/A * ImageConsumers that are currently interested in the data for
0N/A * this image and notify them that an animation frame is complete.
0N/A * This method only has effect if the animation flag has been
0N/A * turned on through the setAnimated() method.
0N/A * If the full buffer update flag was turned on with the
0N/A * setFullBufferUpdates() method then the rectangle parameters
0N/A * will be ignored and the entire buffer will always be sent.
0N/A * @param x the x coordinate of the upper left corner of the rectangle
0N/A * of pixels to be sent
0N/A * @param y the y coordinate of the upper left corner of the rectangle
0N/A * of pixels to be sent
0N/A * @param w the width of the rectangle of pixels to be sent
0N/A * @param h the height of the rectangle of pixels to be sent
0N/A * @see #newPixels(int, int, int, int, boolean)
0N/A * @see ImageConsumer
0N/A * @see #setAnimated
0N/A * @see #setFullBufferUpdates
0N/A */
0N/A public synchronized void newPixels(int x, int y, int w, int h) {
0N/A newPixels(x, y, w, h, true);
0N/A }
0N/A
0N/A /**
0N/A * Sends a rectangular region of the buffer of pixels to any
0N/A * ImageConsumers that are currently interested in the data for
0N/A * this image.
0N/A * If the framenotify parameter is true then the consumers are
0N/A * also notified that an animation frame is complete.
0N/A * This method only has effect if the animation flag has been
0N/A * turned on through the setAnimated() method.
0N/A * If the full buffer update flag was turned on with the
0N/A * setFullBufferUpdates() method then the rectangle parameters
0N/A * will be ignored and the entire buffer will always be sent.
0N/A * @param x the x coordinate of the upper left corner of the rectangle
0N/A * of pixels to be sent
0N/A * @param y the y coordinate of the upper left corner of the rectangle
0N/A * of pixels to be sent
0N/A * @param w the width of the rectangle of pixels to be sent
0N/A * @param h the height of the rectangle of pixels to be sent
0N/A * @param framenotify <code>true</code> if the consumers should be sent a
0N/A * {@link ImageConsumer#SINGLEFRAMEDONE SINGLEFRAMEDONE} notification
0N/A * @see ImageConsumer
0N/A * @see #setAnimated
0N/A * @see #setFullBufferUpdates
0N/A */
0N/A public synchronized void newPixels(int x, int y, int w, int h,
0N/A boolean framenotify) {
0N/A if (animating) {
0N/A if (fullbuffers) {
0N/A x = y = 0;
0N/A w = width;
0N/A h = height;
0N/A } else {
0N/A if (x < 0) {
0N/A w += x;
0N/A x = 0;
0N/A }
0N/A if (x + w > width) {
0N/A w = width - x;
0N/A }
0N/A if (y < 0) {
0N/A h += y;
0N/A y = 0;
0N/A }
0N/A if (y + h > height) {
0N/A h = height - y;
0N/A }
0N/A }
0N/A if ((w <= 0 || h <= 0) && !framenotify) {
0N/A return;
0N/A }
0N/A Enumeration enum_ = theConsumers.elements();
0N/A while (enum_.hasMoreElements()) {
0N/A ImageConsumer ic = (ImageConsumer) enum_.nextElement();
0N/A if (w > 0 && h > 0) {
0N/A sendPixels(ic, x, y, w, h);
0N/A }
0N/A if (framenotify && isConsumer(ic)) {
0N/A ic.imageComplete(ImageConsumer.SINGLEFRAMEDONE);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Changes to a new byte array to hold the pixels for this image.
0N/A * If the animation flag has been turned on through the setAnimated()
0N/A * method, then the new pixels will be immediately delivered to any
0N/A * ImageConsumers that are currently interested in the data for
0N/A * this image.
0N/A * @param newpix the new pixel array
0N/A * @param newmodel the specified <code>ColorModel</code>
0N/A * @param offset the offset into the array
0N/A * @param scansize the distance from one row of pixels to the next in
0N/A * the array
0N/A * @see #newPixels(int, int, int, int, boolean)
0N/A * @see #setAnimated
0N/A */
0N/A public synchronized void newPixels(byte[] newpix, ColorModel newmodel,
0N/A int offset, int scansize) {
0N/A this.pixels = newpix;
0N/A this.model = newmodel;
0N/A this.pixeloffset = offset;
0N/A this.pixelscan = scansize;
0N/A newPixels();
0N/A }
0N/A
0N/A /**
0N/A * Changes to a new int array to hold the pixels for this image.
0N/A * If the animation flag has been turned on through the setAnimated()
0N/A * method, then the new pixels will be immediately delivered to any
0N/A * ImageConsumers that are currently interested in the data for
0N/A * this image.
0N/A * @param newpix the new pixel array
0N/A * @param newmodel the specified <code>ColorModel</code>
0N/A * @param offset the offset into the array
0N/A * @param scansize the distance from one row of pixels to the next in
0N/A * the array
0N/A * @see #newPixels(int, int, int, int, boolean)
0N/A * @see #setAnimated
0N/A */
0N/A public synchronized void newPixels(int[] newpix, ColorModel newmodel,
0N/A int offset, int scansize) {
0N/A this.pixels = newpix;
0N/A this.model = newmodel;
0N/A this.pixeloffset = offset;
0N/A this.pixelscan = scansize;
0N/A newPixels();
0N/A }
0N/A
0N/A private void initConsumer(ImageConsumer ic) {
0N/A if (isConsumer(ic)) {
0N/A ic.setDimensions(width, height);
0N/A }
0N/A if (isConsumer(ic)) {
0N/A ic.setProperties(properties);
0N/A }
0N/A if (isConsumer(ic)) {
0N/A ic.setColorModel(model);
0N/A }
0N/A if (isConsumer(ic)) {
0N/A ic.setHints(animating
0N/A ? (fullbuffers
0N/A ? (ImageConsumer.TOPDOWNLEFTRIGHT |
0N/A ImageConsumer.COMPLETESCANLINES)
0N/A : ImageConsumer.RANDOMPIXELORDER)
0N/A : (ImageConsumer.TOPDOWNLEFTRIGHT |
0N/A ImageConsumer.COMPLETESCANLINES |
0N/A ImageConsumer.SINGLEPASS |
0N/A ImageConsumer.SINGLEFRAME));
0N/A }
0N/A }
0N/A
0N/A private void sendPixels(ImageConsumer ic, int x, int y, int w, int h) {
0N/A int off = pixeloffset + pixelscan * y + x;
0N/A if (isConsumer(ic)) {
0N/A if (pixels instanceof byte[]) {
0N/A ic.setPixels(x, y, w, h, model,
0N/A ((byte[]) pixels), off, pixelscan);
0N/A } else {
0N/A ic.setPixels(x, y, w, h, model,
0N/A ((int[]) pixels), off, pixelscan);
0N/A }
0N/A }
0N/A }
0N/A}