classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] fix image loading speeds


From: Thomas Fitzsimmons
Subject: [cp-patches] fix image loading speeds
Date: Thu, 01 Sep 2005 23:17:53 -0400

Hi,

I committed this fix for PR23536.  The patch imports some code from
QtImage.java and adds a new pixbuf loading method to GtkImage,
loadImageFromData.

Tom

2005-09-01  Thomas Fitzsimmons  <address@hidden>

        PR awt/23536
        * gnu/java/awt/peer/gtk/GtkImage.java,
        gnu_java_awt_peer_gtk_GtkImage.c (GtkImage(URL)): New constructor.
        (GtkImage(byte[])) New constructor.
        (loadImageFromData): New method.
        * gnu/java/awt/peer/gtk/GtkToolkit.java (createImage(URL)): Call
        GtkImage(URL) constructor.
        (createImage(byte[],int,int)): Call GtkImage(byte[]) constructor.
        * include/gnu_java_awt_peer_gtk_GtkImage.h: Regenerate.

Index: gnu/java/awt/peer/gtk/GtkImage.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/awt/peer/gtk/GtkImage.java,v
retrieving revision 1.23
diff -u -r1.23 GtkImage.java
--- gnu/java/awt/peer/gtk/GtkImage.java 16 Aug 2005 18:16:26 -0000      1.23
+++ gnu/java/awt/peer/gtk/GtkImage.java 2 Sep 2005 03:12:22 -0000
@@ -51,6 +51,9 @@
 import java.io.IOException;
 import java.util.Hashtable;
 import java.util.Vector;
+import java.io.ByteArrayOutputStream;
+import java.io.BufferedInputStream;
+import java.net.URL;
 import gnu.classpath.Pointer;
 
 /**
@@ -129,11 +132,16 @@
   private native void setPixels(int[] pixels);
 
   /**
-   * Loads an image using gdk-pixbuf.
+   * Loads an image using gdk-pixbuf from a file.
    */
   private native boolean loadPixbuf(String name);
 
   /**
+   * Loads an image using gdk-pixbuf from data.
+   */
+  private native boolean loadImageFromData(byte[] data);
+
+  /**
    * Allocates a Gtk Pixbuf or pixmap
    */
   private native void createPixmap();
@@ -207,6 +215,58 @@
     isLoaded = true;
     observers = null;
     offScreen = false;
+    props = new Hashtable();
+  }
+
+  /**
+   * Constructs a GtkImage from a byte array of an image file.
+   *
+   * @throws IllegalArgumentException if the image could not be
+   * loaded.
+   */
+  public GtkImage (byte[] data)
+  {
+    if (loadImageFromData (data) != true)
+      throw new IllegalArgumentException ("Couldn't load image.");
+
+    isLoaded = true;
+    observers = null;
+    offScreen = false;
+    props = new Hashtable();
+    errorLoading = false;
+  }
+
+  /**
+   * Constructs a GtkImage from a URL. May result in an error image.
+   */
+  public GtkImage (URL url)
+  {
+    isLoaded = false;
+    observers = new Vector();
+    errorLoading = false;
+    if( url == null)
+      return;
+    ByteArrayOutputStream baos = new ByteArrayOutputStream (5000);
+    try
+      {
+        BufferedInputStream bis = new BufferedInputStream (url.openStream());
+
+        byte[] buf = new byte[5000];
+        int n = 0;
+
+        while ((n = bis.read(buf)) != -1)
+         baos.write(buf, 0, n); 
+        bis.close();
+      }
+    catch(IOException e)
+      {
+       throw new IllegalArgumentException ("Couldn't load image.");
+      }
+    if (loadImageFromData (baos.toByteArray()) != true)
+      throw new IllegalArgumentException ("Couldn't load image.");
+
+    isLoaded = true;
+    observers = null;
     props = new Hashtable();
   }
 
Index: gnu/java/awt/peer/gtk/GtkToolkit.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/awt/peer/gtk/GtkToolkit.java,v
retrieving revision 1.76
diff -u -r1.76 GtkToolkit.java
--- gnu/java/awt/peer/gtk/GtkToolkit.java       19 Aug 2005 01:29:26 -0000      
1.76
+++ gnu/java/awt/peer/gtk/GtkToolkit.java       2 Sep 2005 03:12:22 -0000
@@ -266,11 +266,7 @@
     if (useGraphics2D())
       return bufferedImageOrError(GdkPixbufDecoder.createBufferedImage (url));
     else
-      {
-        GdkPixbufDecoder d = new GdkPixbufDecoder (url);
-        GtkImage image = new GtkImage (d);
-        return image;        
-      }
+      return new GtkImage (url);
   }
 
   public Image createImage (ImageProducer producer) 
@@ -290,11 +286,9 @@
                                                                         
imagelength));
     else
       {
-        GdkPixbufDecoder d = new GdkPixbufDecoder (imagedata,
-                                                   imageoffset, 
-                                                   imagelength);
-        GtkImage image = new GtkImage (d);
-        return image;        
+        byte[] datacopy = new byte[imagelength];
+        System.arraycopy (imagedata, imageoffset, datacopy, 0, imagelength);
+        return new GtkImage (datacopy);
       }
   }
   
Index: include/gnu_java_awt_peer_gtk_GtkImage.h
===================================================================
RCS file: 
/cvsroot/classpath/classpath/include/gnu_java_awt_peer_gtk_GtkImage.h,v
retrieving revision 1.3
diff -u -r1.3 gnu_java_awt_peer_gtk_GtkImage.h
--- include/gnu_java_awt_peer_gtk_GtkImage.h    16 Aug 2005 18:16:27 -0000      
1.3
+++ include/gnu_java_awt_peer_gtk_GtkImage.h    2 Sep 2005 03:12:22 -0000
@@ -13,6 +13,7 @@
 JNIEXPORT jintArray JNICALL Java_gnu_java_awt_peer_gtk_GtkImage_getPixels 
(JNIEnv *env, jobject);
 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GtkImage_setPixels (JNIEnv 
*env, jobject, jintArray);
 JNIEXPORT jboolean JNICALL Java_gnu_java_awt_peer_gtk_GtkImage_loadPixbuf 
(JNIEnv *env, jobject, jstring);
+JNIEXPORT jboolean JNICALL 
Java_gnu_java_awt_peer_gtk_GtkImage_loadImageFromData (JNIEnv *env, jobject, 
jbyteArray);
 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GtkImage_createPixmap 
(JNIEnv *env, jobject);
 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GtkImage_freePixmap (JNIEnv 
*env, jobject);
 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GtkImage_createScaledPixmap 
(JNIEnv *env, jobject, jobject, jint);
Index: native/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkImage.c
===================================================================
RCS file: 
/cvsroot/classpath/classpath/native/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkImage.c,v
retrieving revision 1.13
diff -u -r1.13 gnu_java_awt_peer_gtk_GtkImage.c
--- native/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkImage.c        18 Aug 2005 
03:15:15 -0000      1.13
+++ native/jni/gtk-peer/gnu_java_awt_peer_gtk_GtkImage.c        2 Sep 2005 
03:12:22 -0000
@@ -97,6 +97,54 @@
   return JNI_TRUE;
 }
 
+/*
+ * Creates the image from an array of java bytes.
+ */
+JNIEXPORT jboolean JNICALL
+Java_gnu_java_awt_peer_gtk_GtkImage_loadImageFromData
+  (JNIEnv *env, jobject obj, jbyteArray data)
+{
+  jbyte *src;
+  GdkPixbuf* pixbuf;
+  GdkPixbufLoader* loader;
+  int len;
+  int width;
+  int height;
+
+  gdk_threads_enter ();
+
+  src = (*env)->GetByteArrayElements (env, data, NULL);
+  len = (*env)->GetArrayLength (env, data);
+
+  loader = gdk_pixbuf_loader_new ();
+
+  gdk_pixbuf_loader_write (loader, (guchar *)src, len, NULL);
+  gdk_pixbuf_loader_close (loader, NULL);
+
+  (*env)->ReleaseByteArrayElements (env, data, src, 0);
+
+  pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
+
+  if (pixbuf == NULL)
+    {
+      createRawData (env, obj, NULL);
+
+      gdk_threads_leave ();
+
+      return JNI_FALSE;
+    }
+
+  width =  gdk_pixbuf_get_width (pixbuf);
+  height = gdk_pixbuf_get_height (pixbuf);
+
+  createRawData (env, obj, pixbuf);
+  setWidthHeight(env, obj, width, height);
+
+  gdk_threads_leave ();
+
+  return JNI_TRUE;
+}
+
 JNIEXPORT void JNICALL
 Java_gnu_java_awt_peer_gtk_GtkImage_createFromPixbuf
 (JNIEnv *env, jobject obj)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]