diff options
Diffstat (limited to 'libjava/classpath/gnu/java/awt/peer/gtk/GtkImage.java')
-rw-r--r-- | libjava/classpath/gnu/java/awt/peer/gtk/GtkImage.java | 273 |
1 files changed, 72 insertions, 201 deletions
diff --git a/libjava/classpath/gnu/java/awt/peer/gtk/GtkImage.java b/libjava/classpath/gnu/java/awt/peer/gtk/GtkImage.java index 5e5f1de0113..ef96518a1c0 100644 --- a/libjava/classpath/gnu/java/awt/peer/gtk/GtkImage.java +++ b/libjava/classpath/gnu/java/awt/peer/gtk/GtkImage.java @@ -57,14 +57,7 @@ import java.net.URL; import gnu.classpath.Pointer; /** - * GtkImage - wraps a GdkPixbuf or GdkPixmap. - * - * The constructor GtkImage(int, int) creates an 'off-screen' GdkPixmap, - * this can be drawn to (it's a GdkDrawable), and correspondingly, you can - * create a GdkGraphics object for it. - * - * This corresponds to the Image implementation returned by - * Component.createImage(int, int). + * GtkImage - wraps a GdkPixbuf. * * A GdkPixbuf is 'on-screen' and the gdk cannot draw to it, * this is used for the other constructors (and other createImage methods), and @@ -88,9 +81,10 @@ public class GtkImage extends Image boolean isLoaded; /** - * Pointer to the GdkPixbuf + * Pointer to the GdkPixbuf - + * don't change the name without changing the native code. */ - Pointer pixmap; + Pointer pixbuf; /** * Observer queue. @@ -98,11 +92,6 @@ public class GtkImage extends Image Vector observers; /** - * If offScreen is set, a GdkBitmap is wrapped and not a Pixbuf. - */ - boolean offScreen; - - /** * Error flag for loading. */ boolean errorLoading; @@ -122,71 +111,64 @@ public class GtkImage extends Image 0xFF000000); /** + * The singleton GtkImage that is returned on errors by GtkToolkit. + */ + private static GtkImage errorImage; + + /** + * Lock that should be held for all gdkpixbuf operations. We don't use + * the global gdk_threads_enter/leave functions in most places since + * most gdkpixbuf operations can be done in parallel to drawing and + * manipulating gtk widgets. + */ + static Object pixbufLock = new Object(); + + /** + * Allocate a PixBuf from a given ARGB32 buffer pointer. + */ + private native void initFromBuffer( long bufferPointer ); + + /** * Returns a copy of the pixel data as a java array. - * Should be called with the GdkPixbufDecoder.pixbufLock held. + * Should be called with the pixbufLock held. */ - private native int[] getPixels(); + native int[] getPixels(); /** * Sets the pixel data from a java array. - * Should be called with the GdkPixbufDecoder.pixbufLock held. + * Should be called with the pixbufLock held. */ private native void setPixels(int[] pixels); /** * Loads an image using gdk-pixbuf from a file. - * Should be called with the GdkPixbufDecoder.pixbufLock held. + * Should be called with the pixbufLock held. */ private native boolean loadPixbuf(String name); /** * Loads an image using gdk-pixbuf from data. - * Should be called with the GdkPixbufDecoder.pixbufLock held. + * Should be called with the pixbufLock held. */ private native boolean loadImageFromData(byte[] data); /** - * Allocates a Gtk Pixbuf or pixmap - * Should be called with the GdkPixbufDecoder.pixbufLock held. + * Allocates a Gtk Pixbuf + * Should be called with the pixbufLock held. */ - private native void createPixmap(); + private native void createPixbuf(); /** * Frees the above. - * Should be called with the GdkPixbufDecoder.pixbufLock held. - */ - private native void freePixmap(); - - /** - * Sets the pixmap to scaled copy of src image. hints are rendering hints. - * Should be called with the GdkPixbufDecoder.pixbufLock held. - */ - private native void createScaledPixmap(GtkImage src, int hints); - - /** - * Draws the image, optionally scaled and composited. - * Should be called with the GdkPixbufDecoder.pixbufLock held. - * Also acquires global gdk lock for drawing. + * Should be called with the pixbufLock held. */ - private native void drawPixelsScaled (GdkGraphics gc, - int bg_red, int bg_green, int bg_blue, - int x, int y, int width, int height, - boolean composite); + private native void freePixbuf(); /** - * Draws the image, optionally scaled flipped and composited. - * Should be called with the GdkPixbufDecoder.pixbufLock held. - * Also acquires global gdk lock for drawing. + * Sets the pixbuf to scaled copy of src image. hints are rendering hints. + * Should be called with the pixbufLock held. */ - private native void drawPixelsScaledFlipped (GdkGraphics gc, - int bg_red, int bg_green, - int bg_blue, - boolean flipX, boolean flipY, - int srcX, int srcY, - int srcWidth, int srcHeight, - int dstX, int dstY, - int dstWidth, int dstHeight, - boolean composite); + private native void createScaledPixbuf(GtkImage src, int hints); /** * Constructs a GtkImage from an ImageProducer. Asynchronity is handled in @@ -202,7 +184,6 @@ public class GtkImage extends Image source = producer; errorLoading = false; source.startProduction(new GtkImageConsumer(this, source)); - offScreen = false; } /** @@ -215,7 +196,6 @@ public class GtkImage extends Image { isLoaded = true; observers = null; - offScreen = false; props = new Hashtable(); errorLoading = false; } @@ -231,7 +211,7 @@ public class GtkImage extends Image try { String path = f.getCanonicalPath(); - synchronized(GdkPixbufDecoder.pixbufLock) + synchronized(pixbufLock) { if (loadPixbuf(f.getCanonicalPath()) != true) throw new IllegalArgumentException("Couldn't load image: " @@ -249,7 +229,6 @@ public class GtkImage extends Image isLoaded = true; observers = null; - offScreen = false; props = new Hashtable(); } @@ -261,7 +240,7 @@ public class GtkImage extends Image */ public GtkImage (byte[] data) { - synchronized(GdkPixbufDecoder.pixbufLock) + synchronized(pixbufLock) { if (loadImageFromData (data) != true) throw new IllegalArgumentException ("Couldn't load image."); @@ -269,7 +248,6 @@ public class GtkImage extends Image isLoaded = true; observers = null; - offScreen = false; props = new Hashtable(); errorLoading = false; } @@ -301,7 +279,7 @@ public class GtkImage extends Image throw new IllegalArgumentException ("Couldn't load image."); } byte[] array = baos.toByteArray(); - synchronized(GdkPixbufDecoder.pixbufLock) + synchronized(pixbufLock) { if (loadImageFromData(array) != true) throw new IllegalArgumentException ("Couldn't load image."); @@ -313,23 +291,6 @@ public class GtkImage extends Image } /** - * Constructs an empty GtkImage. - */ - public GtkImage (int width, int height) - { - this.width = width; - this.height = height; - props = new Hashtable(); - isLoaded = true; - observers = null; - offScreen = true; - synchronized(GdkPixbufDecoder.pixbufLock) - { - createPixmap(); - } - } - - /** * Constructs a scaled version of the src bitmap, using the GDK. */ private GtkImage (GtkImage src, int width, int height, int hints) @@ -339,12 +300,11 @@ public class GtkImage extends Image props = new Hashtable(); isLoaded = true; observers = null; - offScreen = false; // Use the GDK scaling method. - synchronized(GdkPixbufDecoder.pixbufLock) + synchronized(pixbufLock) { - createScaledPixmap(src, hints); + createScaledPixbuf(src, hints); } } @@ -354,19 +314,30 @@ public class GtkImage extends Image */ GtkImage (Pointer pixbuf) { - pixmap = pixbuf; - synchronized(GdkPixbufDecoder.pixbufLock) + this.pixbuf = pixbuf; + synchronized(pixbufLock) { createFromPixbuf(); } isLoaded = true; observers = null; - offScreen = false; props = new Hashtable(); } - // The singleton GtkImage that is returned on errors by GtkToolkit. - private static GtkImage errorImage; + /** + * Wraps a buffer with a GtkImage. + * + * @param bufferPointer a pointer to an ARGB32 buffer + */ + GtkImage(int width, int height, long bufferPointer) + { + this.width = width; + this.height = height; + props = new Hashtable(); + isLoaded = true; + observers = null; + initFromBuffer( bufferPointer ); + } /** * Returns an empty GtkImage with the errorLoading flag set. @@ -385,7 +356,7 @@ public class GtkImage extends Image /** * Native helper function for constructor that takes a pixbuf Pointer. - * Should be called with the GdkPixbufDecoder.pixbufLock held. + * Should be called with the pixbufLock held. */ private native void createFromPixbuf(); @@ -407,9 +378,9 @@ public class GtkImage extends Image isLoaded = true; deliver(); - synchronized(GdkPixbufDecoder.pixbufLock) + synchronized(pixbufLock) { - createPixmap(); + createPixbuf(); setPixels(pixels); } } @@ -450,30 +421,28 @@ public class GtkImage extends Image return null; int[] pixels; - synchronized(GdkPixbufDecoder.pixbufLock) + synchronized (pixbufLock) { - pixels = getPixels(); + if (!errorLoading) + pixels = getPixels(); + else + return null; } return new MemoryImageSource(width, height, nativeModel, pixels, 0, width); } /** - * Creates a GdkGraphics context for this pixmap. + * Does nothing. Should not be called. */ public Graphics getGraphics () { - if (!isLoaded) - return null; - if (offScreen) - return new GdkGraphics(this); - else - throw new IllegalAccessError("This method only works for off-screen" - +" Images."); + throw new IllegalAccessError("This method only works for off-screen" + +" Images."); } /** - * Returns a scaled instance of this pixmap. + * Returns a scaled instance of this pixbuf. */ public Image getScaledInstance(int width, int height, @@ -500,9 +469,9 @@ public class GtkImage extends Image { observers = new Vector(); isLoaded = false; - synchronized(GdkPixbufDecoder.pixbufLock) + synchronized(pixbufLock) { - freePixmap(); + freePixbuf(); } source.startProduction(new GtkImageConsumer(this, source)); } @@ -512,9 +481,9 @@ public class GtkImage extends Image { if (isLoaded) { - synchronized(GdkPixbufDecoder.pixbufLock) + synchronized(pixbufLock) { - freePixmap(); + freePixbuf(); } } } @@ -535,104 +504,6 @@ public class GtkImage extends Image return ImageObserver.ALLBITS | ImageObserver.WIDTH | ImageObserver.HEIGHT; } - // Drawing methods //////////////////////////////////////////////// - - /** - * Draws an image with eventual scaling/transforming. - */ - public boolean drawImage (GdkGraphics g, int dx1, int dy1, int dx2, int dy2, - int sx1, int sy1, int sx2, int sy2, - Color bgcolor, ImageObserver observer) - { - if (addObserver(observer)) - return false; - - boolean flipX = (dx1 > dx2)^(sx1 > sx2); - boolean flipY = (dy1 > dy2)^(sy1 > sy2); - int dstWidth = Math.abs (dx2 - dx1); - int dstHeight = Math.abs (dy2 - dy1); - int srcWidth = Math.abs (sx2 - sx1); - int srcHeight = Math.abs (sy2 - sy1); - int srcX = (sx1 < sx2) ? sx1 : sx2; - int srcY = (sy1 < sy2) ? sy1 : sy2; - int dstX = (dx1 < dx2) ? dx1 : dx2; - int dstY = (dy1 < dy2) ? dy1 : dy2; - - // Clipping. This requires the dst to be scaled as well, - if (srcWidth > width) - { - dstWidth = (int)((double)dstWidth*((double)width/(double)srcWidth)); - srcWidth = width - srcX; - } - - if (srcHeight > height) - { - dstHeight = (int)((double)dstHeight*((double)height/(double)srcHeight)); - srcHeight = height - srcY; - } - - if (srcWidth + srcX > width) - { - dstWidth = (int)((double)dstWidth * (double)(width - srcX)/(double)srcWidth); - srcWidth = width - srcX; - } - - if (srcHeight + srcY > height) - { - dstHeight = (int)((double)dstHeight * (double)(width - srcY)/(double)srcHeight); - srcHeight = height - srcY; - } - - if ( this.width <= 0 || this.height <= 0 ) - return true; - - if ( srcWidth <= 0 || srcHeight <= 0 || dstWidth <= 0 || dstHeight <= 0) - return true; - - synchronized(GdkPixbufDecoder.pixbufLock) - { - if(bgcolor != null) - drawPixelsScaledFlipped (g, bgcolor.getRed (), bgcolor.getGreen (), - bgcolor.getBlue (), - flipX, flipY, - srcX, srcY, - srcWidth, srcHeight, - dstX, dstY, - dstWidth, dstHeight, - true); - else - drawPixelsScaledFlipped (g, 0, 0, 0, flipX, flipY, - srcX, srcY, srcWidth, srcHeight, - dstX, dstY, dstWidth, dstHeight, - false); - } - return true; - } - - /** - * Draws an image to the GdkGraphics context, at (x,y) scaled to - * width and height, with optional compositing with a background color. - */ - public boolean drawImage (GdkGraphics g, int x, int y, int width, int height, - Color bgcolor, ImageObserver observer) - { - if (addObserver(observer)) - return false; - - if ( this.width <= 0 || this.height <= 0 ) - return true; - - synchronized(GdkPixbufDecoder.pixbufLock) - { - if(bgcolor != null) - drawPixelsScaled(g, bgcolor.getRed (), bgcolor.getGreen (), - bgcolor.getBlue (), x, y, width, height, true); - else - drawPixelsScaled(g, 0, 0, 0, x, y, width, height, false); - } - - return true; - } // Private methods //////////////////////////////////////////////// |