diff options
| author | doko <doko@138bc75d-0d04-0410-961f-82ee72b054a4> | 2008-06-28 13:29:13 +0000 |
|---|---|---|
| committer | doko <doko@138bc75d-0d04-0410-961f-82ee72b054a4> | 2008-06-28 13:29:13 +0000 |
| commit | 1020ce5944edde4364baef4d371cd4f9b0dae721 (patch) | |
| tree | 602cd7aa7c947386134690d8e0f6b53abcdeacb9 /libjava/classpath/gnu/java/awt/image | |
| parent | 9f41ce98ce6f4f7c8ac5e2c4b6e5d27e10201015 (diff) | |
| download | ppe42-gcc-1020ce5944edde4364baef4d371cd4f9b0dae721.tar.gz ppe42-gcc-1020ce5944edde4364baef4d371cd4f9b0dae721.zip | |
libjava/
2008-06-28 Matthias Klose <doko@ubuntu.com>
Import GNU Classpath (classpath-0_97_2-release).
* Regenerate class and header files.
* Regenerate auto* files.
* gcj/javaprims.h: Define jobjectRefType.
* jni.cc (_Jv_JNI_GetObjectRefType): New (stub only).
(_Jv_JNIFunctions): Initialize GetObjectRefType.
* gnu/classpath/jdwp/VMVirtualMachine.java,
java/security/VMSecureRandom.java: Merge from classpath.
* HACKING: Fix typo.
* ChangeLog-2007: New file.
* configure.ac: Set JAVAC, pass --disable-regen-headers to classpath.
libjava/classpath/
2008-06-28 Matthias Klose <doko@ubuntu.com>
* m4/ac_prog_javac.m4: Disable check for JAVAC, when
not configured with --enable-java-maintainer-mode.
* aclocal.m4, configure: Regenerate.
* native/jni/gstreamer-peer/Makefile.am: Do not link with
libclasspathnative.
* native/jni/gstreamer-peer/Makefile.in: Regenerate.
* tools/Makefile.am, lib/Makefile.am: Use JAVAC for setting
JCOMPILER, drop flags not understood by gcj.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@137223 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libjava/classpath/gnu/java/awt/image')
| -rw-r--r-- | libjava/classpath/gnu/java/awt/image/AsyncImage.java | 300 | ||||
| -rw-r--r-- | libjava/classpath/gnu/java/awt/image/ImageConverter.java | 528 |
2 files changed, 828 insertions, 0 deletions
diff --git a/libjava/classpath/gnu/java/awt/image/AsyncImage.java b/libjava/classpath/gnu/java/awt/image/AsyncImage.java new file mode 100644 index 00000000000..935601a8898 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/image/AsyncImage.java @@ -0,0 +1,300 @@ +/* AsyncImage.java -- Loads images asynchronously + Copyright (C) 2008 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.image; + + +import java.awt.Graphics; +import java.awt.Image; +import java.awt.image.ImageConsumer; +import java.awt.image.ImageObserver; +import java.awt.image.ImageProducer; +import java.util.HashSet; +import java.util.Iterator; + +/** + * Supports asynchronous loading of images. + */ +public class AsyncImage + extends Image +{ + + /** + * The image source for AsyncImages. + */ + private class AsyncImageSource + implements ImageProducer + { + /** + * The real image source, if already present, or <code>null</code> + * otherwise. + */ + private ImageProducer realSource; + + public void addConsumer(ImageConsumer ic) + { + startProduction(ic); + } + + public boolean isConsumer(ImageConsumer ic) + { + return false; + } + + public void removeConsumer(ImageConsumer ic) + { + // Nothing to do here. + } + + public void requestTopDownLeftRightResend(ImageConsumer ic) + { + startProduction(ic); + } + + public void startProduction(ImageConsumer ic) + { + ImageProducer ip = getRealSource(); + if (ip == null) + { + ic.setDimensions(1, 1); + ic.imageComplete(ImageConsumer.SINGLEFRAMEDONE); + } + else + { + ip.startProduction(ic); + } + } + + /** + * Returns the real image source, if already present. Otherwise, this + * returns <code>null</code>. + * + * @return the real image source, or <code>null</code> if not present + */ + private ImageProducer getRealSource() + { + synchronized (AsyncImage.this) + { + ImageProducer source = realSource; + if (source == null) + { + Image ri = realImage; + if (ri != null) + { + realSource = source = ri.getSource(); + } + } + return source; + } + } + } + + /** + * The real image. This is null as long as the image is not complete. + */ + private volatile Image realImage; + + /** + * The image observers. + * + * This is package private to avoid accessor methods. + */ + HashSet<ImageObserver> observers; + + private volatile boolean complete = false; + + /** + * Creates a new AsyncImage. + */ + AsyncImage() + { + observers = new HashSet<ImageObserver>(); + } + + public void flush() + { + // Nothing to do here. + } + + public Graphics getGraphics() + { + Image r = realImage; + Graphics g = null; + if (r != null) + g = r.getGraphics(); // Should we return some dummy graphics instead? + return g; + } + + public boolean isComplete() { + return complete; + } + + public int getHeight(ImageObserver observer) + { + addObserver(observer); + int height = -1; + waitForImage(observer); + Image r = realImage; + if (r != null) + height = r.getHeight(observer); + return height; + } + + public Object getProperty(String name, ImageObserver observer) + { + addObserver(observer); + Image r = realImage; + Object prop = null; + if (r != null) + prop = r.getProperty(name, observer); + return prop; + } + + public ImageProducer getSource() + { + return new AsyncImageSource(); + } + + public int getWidth(ImageObserver observer) + { + addObserver(observer); + int width = -1; + waitForImage(observer); + Image r = realImage; + if (r != null) + width = r.getWidth(observer); + return width; + } + + public void addObserver(ImageObserver obs) + { + if (obs != null) + { + synchronized (this) + { + // This field gets null when image loading is complete and we don't + // need to store any more observers. + HashSet<ImageObserver> observs = observers; + if (observs != null) + { + observs.add(obs); + } + } + } + } + + public boolean prepareImage(int w, int h, ImageObserver obs) + { + addObserver(obs); + return realImage != null; + } + + public int checkImage(int w, int h, ImageObserver obs) + { + addObserver(obs); + int flags = 0; + if (realImage != null) + flags = ImageObserver.ALLBITS | ImageObserver.WIDTH + | ImageObserver.HEIGHT | ImageObserver.PROPERTIES; + return flags; + } + + public Image getRealImage() + { + return realImage; + } + + public void setRealImage(Image im) + { + realImage = im; + int status = ImageObserver.HEIGHT | ImageObserver.WIDTH; + notifyObservers(status, 0, 0, im.getWidth(null), im.getHeight(null)); + } + + public void notifyObservers(int status, int x, int y, int w, int h) + { + synchronized (this) + { + HashSet observs = observers; + if (observs != null) + { + Iterator i = observs.iterator(); + while (i.hasNext()) + { + ImageObserver obs = (ImageObserver) i.next(); + boolean complete = obs.imageUpdate(this, status, x, y, realImage.getWidth(obs), realImage.getHeight(obs)); + if (complete) // Remove completed observers. + i.remove(); + } + } + if ((status & ImageObserver.ALLBITS) != 0) + { + complete = true; + notifyAll(); + } + } + } + + /** + * Waits for the image to be loaded completely, if the image observer + * is <code>null</code>. Otherwise this is not necessary, because the + * image observer can be notified about later completion. + * + * @param observer the image observer + */ + public void waitForImage(ImageObserver observer) + { + if (!complete && observer == null) + { + synchronized (this) + { + while (! complete) + { + try + { + wait(); + } + catch (InterruptedException ex) + { + Thread.currentThread().interrupt(); + } + } + } + } + } +} diff --git a/libjava/classpath/gnu/java/awt/image/ImageConverter.java b/libjava/classpath/gnu/java/awt/image/ImageConverter.java new file mode 100644 index 00000000000..f607b349630 --- /dev/null +++ b/libjava/classpath/gnu/java/awt/image/ImageConverter.java @@ -0,0 +1,528 @@ +/* ImageConverter.java -- Loads images asynchronously + Copyright (C) 2008 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.awt.image; + +import gnu.java.awt.image.AsyncImage; + +import java.awt.GraphicsEnvironment; +import java.awt.Image; +import java.awt.Transparency; +import java.awt.image.BufferedImage; +import java.awt.image.ColorModel; +import java.awt.image.DataBuffer; +import java.awt.image.ImageConsumer; +import java.awt.image.IndexColorModel; +import java.awt.image.ImageObserver; +import java.awt.image.SinglePixelPackedSampleModel; +import java.awt.image.WritableRaster; +import java.util.Hashtable; + +/** + * Convert an Image to a BufferedImage. + * + * @author Roman Kennke (kennke@aicas.com) + */ +public class ImageConverter implements ImageConsumer +{ + + public static final String IMAGE_TRANSPARENCY_PROPERTY = + "gnu.awt.image.transparency"; + + public static final String IMAGE_PROPERTIES_PROPERTY = + "gnu.awt.image.properties"; + + private AsyncImage image; + private BufferedImage bImage; + private Hashtable imageProperties; + private int width, height; + private ColorModel colorModel; + private ColorModel targetColorModel; + + public ImageConverter() + { + width = 0; + height = 0; + image = new AsyncImage(); + } + + public void setDimensions(int w, int h) + { + width = w; + height = h; + } + + public void setProperties(Hashtable props) + { + // Ignore for now. + } + + public void setColorModel(ColorModel model) + { + colorModel = model; + } + + public void setHints(int flags) + { + // Ignore for now. + } + + public void setPixels(int x, int y, int w, int h, ColorModel model, + byte[] pixels, int offset, int scansize) + { + model = setupColorModel(model); + + if (bImage == null) + { + createImage(); + } + + Integer t = (Integer) imageProperties.get("gnu.awt.image.transparency"); + int transparency = t.intValue(); + + if(targetColorModel.equals(model)) + { + transparency = transferPixels(x, y, w, h, model, pixels, offset, + scansize, transparency); + } + else if (model instanceof IndexColorModel + && targetColorModel.equals(ColorModel.getRGBdefault())) + { + transparency = convertIndexColorModelToSRGB(x, y, w, h, + (IndexColorModel) model, + pixels, offset, scansize, + transparency); + } + else + { + transparency = convertPixels(x, y, w, h, model, pixels, offset, + scansize, transparency); + } + + imageProperties.put("gnu.awt.image.transparency", + Integer.valueOf(transparency)); + } + + public void setPixels(int x, int y, int w, int h, ColorModel model, + int[] pixels, int offset, int scansize) + { + model = setupColorModel(model); + if (bImage == null) + { + createImage(); + } + + Integer t = (Integer) imageProperties.get(IMAGE_TRANSPARENCY_PROPERTY); + int transparency= t.intValue(); + + if (targetColorModel.equals(model)) + { + transparency = transferPixels(x, y, w, h, model, pixels, offset, + scansize, transparency); + } + else if (model instanceof IndexColorModel + && targetColorModel.equals(ColorModel.getRGBdefault())) + { + transparency = convertIndexColorModelToSRGB(x, y, w, h, + (IndexColorModel) model, + pixels, offset, scansize, + transparency); + } + else + { + transparency = convertPixels(x, y, w, h, model, pixels, offset, + scansize, transparency); + } + + imageProperties.put(IMAGE_TRANSPARENCY_PROPERTY, + Integer.valueOf(transparency)); + + } + + /** + * Initialize the color model for this setPixels run: <br/> + * 1. if no color model was given use the hinted color model <br/> + * 2. if no color model was given and non was hinted use the default sRGB color model. <br/> + * Also:<br/> + * If no target color model was set use the color model of the given pixels. + * @param model + * @return + */ + private ColorModel setupColorModel(ColorModel model) + { + // If the given color model is null use the previously hinted color model. + if (model == null) + model = colorModel; + + // If no color model was given or hinted use default sRGB. + if (model == null) + model = ColorModel.getRGBdefault(); + + // If no specific color model was requested for the target use the current + // pixels model. + if (targetColorModel == null) + targetColorModel = model; + targetColorModel = ColorModel.getRGBdefault(); + return model; + } + + /** + * Creates the image instance into which the pixel data is converted. + */ + private void createImage() + { + if (imageProperties == null) + { + imageProperties = new Hashtable(); + } + + imageProperties.put(IMAGE_TRANSPARENCY_PROPERTY, + Integer.valueOf(Transparency.OPAQUE)); + imageProperties.put(IMAGE_PROPERTIES_PROPERTY, imageProperties); + + // For the sRGB case let the GraphicsEnvironment create an image for us. + if (ColorModel.getRGBdefault().equals(targetColorModel)) + { + bImage = GraphicsEnvironment.getLocalGraphicsEnvironment() + .getDefaultScreenDevice() + .getDefaultConfiguration() + .createCompatibleImage(width, height, Transparency.TRANSLUCENT); + } + else + { + WritableRaster raster = + targetColorModel.createCompatibleWritableRaster(width, height); + bImage = new BufferedImage(targetColorModel, raster, false, + imageProperties); + } + image.setRealImage(bImage); + return; + } + + /** + * Transfers pixels into a raster of the same color model. + * + * @param x the X coordinate of the source pixel rectangle + * @param y the Y coordinate of the source pixel rectangle + * @param w the width of the source pixel rectangle + * @param h the height of the source pixel rectangle + * @param model the color model of the source pixels + * @param pixels the pixel data + * @param offset the offset in the pixel array + * @param scansize the scanline size + * @param transparency the assumed transparency + * + * @return the determined transparency + */ + private int transferPixels(int x, int y, int w, int h, ColorModel model, + byte[] pixels, int offset, int scansize, + int transparency) + { + // If we have the same color model, then we can simply drop + // the pixel value into the target raster. + bImage.getRaster().setDataElements(x, y, w, h, pixels); + + for (int yy = 0; yy < h; yy++) + { + for (int xx = 0; xx < w; xx++) + { + int pixel = 0xFF & pixels[yy * scansize + xx + offset]; + int alpha = model.getAlpha(pixel); + transparency = updateTransparency(alpha, transparency); + } + } + return transparency; + } + + /** + * Transfers pixels into a raster of the same color model. + * + * @param x the X coordinate of the source pixel rectangle + * @param y the Y coordinate of the source pixel rectangle + * @param w the width of the source pixel rectangle + * @param h the height of the source pixel rectangle + * @param model the color model of the source pixels + * @param pixels the pixel data + * @param offset the offset in the pixel array + * @param scansize the scanline size + * @param transparency the assumed transparency + * + * @return the determined transparency + */ + private int transferPixels(int x, int y, int w, int h, ColorModel model, + int[] pixels, int offset, int scansize, + int transparency) + { + // If we have the same color model, then we can simply drop + // the pixel value into the target raster. + bImage.getRaster().setDataElements(x, y, w, h, pixels); + + for (int yy = 0; yy < h; yy++) + { + for (int xx = 0; xx < w; xx++) + { + int pixel = pixels[yy * scansize + xx + offset]; + int alpha = model.getAlpha(pixel); + transparency = updateTransparency(alpha, transparency); + } + } + return transparency; + } + + /** + * Converts pixel from one color model to another, and stores them in the + * target image. + * + * @param x the X coordinate of the source pixel rectangle + * @param y the Y coordinate of the source pixel rectangle + * @param w the width of the source pixel rectangle + * @param h the height of the source pixel rectangle + * @param model the color model of the source pixels + * @param pixels the pixel data + * @param offset the offset in the pixel array + * @param scansize the scanline size + * @param transparency the assumed transparency + * + * @return the determined transparency + */ + private int convertPixels(int x, int y, int w, int h, ColorModel model, + byte[] pixels, int offset, int scansize, + int transparency) + { + // If the color models are not the same, we must convert the + // pixel values from one model to the other. + Object dataEl = null; + // Convert pixels to the destination color model. + for (int yy = 0; yy < h; yy++) + { + for (int xx = 0; xx < w; xx++) + { + int pixel = 0xFF & pixels[yy * scansize + xx + offset]; + int rgb = model.getRGB(pixel); + int alpha = model.getAlpha(pixel); + transparency = updateTransparency(alpha, transparency); + dataEl = targetColorModel.getDataElements(rgb, dataEl); + bImage.getRaster().setDataElements(x + xx, y + yy, dataEl); + } + } + return transparency; + } + + /** + * Converts pixel from one color model to another, and stores them in the + * target image. + * + * @param x the X coordinate of the source pixel rectangle + * @param y the Y coordinate of the source pixel rectangle + * @param w the width of the source pixel rectangle + * @param h the height of the source pixel rectangle + * @param model the color model of the source pixels + * @param pixels the pixel data + * @param offset the offset in the pixel array + * @param scansize the scanline size + * @param transparency the assumed transparency + * + * @return the determined transparency + */ + private int convertPixels(int x, int y, int w, int h, ColorModel model, + int[] pixels, int offset, int scansize, + int transparency) + { + // If the color models are not the same, we must convert the + // pixel values from one model to the other. + Object dataEl = null; + // Convert pixels to the destination color model. + for (int yy = 0; yy < h; yy++) + { + for (int xx = 0; xx < w; xx++) + { + int pixel = pixels[yy * scansize + xx + offset]; + int rgb = model.getRGB(pixel); + int alpha = model.getAlpha(pixel); + transparency = updateTransparency(alpha, transparency); + dataEl = targetColorModel.getDataElements(rgb, dataEl); + bImage.getRaster().setDataElements(x + xx, y + yy, dataEl); + } + } + return transparency; + } + + /** + * Converts pixels from an index color model to the target image. + * + * @param x the X coordinate of the source pixel rectangle + * @param y the Y coordinate of the source pixel rectangle + * @param w the width of the source pixel rectangle + * @param h the height of the source pixel rectangle + * @param model the color model of the source pixels + * @param pixels the pixel data + * @param offset the offset in the pixel array + * @param scansize the scanline size + * @param transparency the assumed transparency + * + * @return the determined transparency + */ + private int convertIndexColorModelToSRGB(int x, int y, int w, int h, + IndexColorModel model, + byte[] pixels, int offset, + int scansize, int transparency) + { + + int mapSize = model.getMapSize(); + int[] colorMap = new int[mapSize]; + for(int i=0; i < mapSize; i++) + { + colorMap[i] = model.getRGB(i); + } + + WritableRaster raster = bImage.getRaster(); + SinglePixelPackedSampleModel sampleMode = + (SinglePixelPackedSampleModel) raster.getSampleModel(); + DataBuffer dataBuffer = (DataBuffer) raster.getDataBuffer(); + + int rasterOffset = sampleMode.getOffset(x,y)+dataBuffer.getOffset(); + int rasterScanline = sampleMode.getScanlineStride(); + + for (int yy = 0; yy < h; yy++) + { + int xoffset = offset; + for (int xx = 0; xx < w; xx++) + { + int argb = colorMap[(pixels[xoffset++] & 0xFF)]; + dataBuffer.setElem(rasterOffset+xx, argb); + int alpha = (argb >>> 24); + transparency = updateTransparency(alpha, transparency); + } + offset += scansize; + rasterOffset += rasterScanline; + } + + return transparency; + } + + /** + * Converts pixels from an index color model to the target image. + * + * @param x the X coordinate of the source pixel rectangle + * @param y the Y coordinate of the source pixel rectangle + * @param w the width of the source pixel rectangle + * @param h the height of the source pixel rectangle + * @param model the color model of the source pixels + * @param pixels the pixel data + * @param offset the offset in the pixel array + * @param scansize the scanline size + * @param transparency the assumed transparency + * + * @return the determined transparency + */ + private int convertIndexColorModelToSRGB(int x, int y, int w, int h, + IndexColorModel model, int[] pixels, + int offset, int scansize, + int transparency) + { + int mapSize = model.getMapSize(); + int[] colorMap = new int[mapSize]; + for(int i=0; i < mapSize; i++) + { + colorMap[i] = model.getRGB(i); + } + + WritableRaster raster = bImage.getRaster(); + SinglePixelPackedSampleModel sampleMode = + (SinglePixelPackedSampleModel) raster.getSampleModel(); + DataBuffer dataBuffer = (DataBuffer)raster.getDataBuffer(); + + int rasterOffset = sampleMode.getOffset(x, y) + dataBuffer.getOffset(); + int rasterScanline = sampleMode.getScanlineStride(); + + for (int yy = 0; yy < h; yy++) + { + int xoffset = offset; + for (int xx = 0; xx < w; xx++) + { + int argb = colorMap[pixels[xoffset++]]; + dataBuffer.setElem(rasterOffset + xx, argb); + int alpha = (argb >>> 24); + transparency = updateTransparency(alpha, transparency); + } + offset += scansize; + rasterOffset += rasterScanline; + } + + return transparency; + } + + /** + * Updates the transparency information according to the alpha pixel value. + * + * @param alpha the alpha pixel value + * @param transparency the old transparency + * + * @return the updated transparency + */ + private int updateTransparency(int alpha, int transparency) + { + if (alpha != 0xFF) + { + if (alpha == 0x00 && transparency <= Transparency.BITMASK) + { + transparency = Transparency.BITMASK; + } + else if (transparency < Transparency.TRANSLUCENT) + { + transparency = Transparency.TRANSLUCENT; + } + } + return transparency; + } + + public void imageComplete(int status) + { + image.notifyObservers(ImageObserver.ALLBITS, 0, 0, width, height); + } + + public void setTargetColorModel(ColorModel model) + { + targetColorModel = model; + } + + public Image getImage() + { + return image; + } +} |

