static void
init_graphics2d_as_pixbuf (struct graphics2d *gr)
{
  gint width, height;
  gint bits_per_sample = 8;
  gint total_channels = 4;
  gboolean has_alpha = TRUE;
  
  g_assert (gr != NULL);
  g_assert (gr->drawable != NULL);

  if (gr->debug) printf ("initializing graphics2d as pixbuf\n");
  gdk_drawable_get_size (gr->drawable, &width, &height);
  gr->drawbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 
				has_alpha, bits_per_sample,
				width, height);
  g_assert (gr->drawbuf != NULL);
  g_assert (gdk_pixbuf_get_bits_per_sample (gr->drawbuf) == bits_per_sample);
  g_assert (gdk_pixbuf_get_n_channels (gr->drawbuf) == total_channels);
  
  gr->surface = cairo_surface_create_for_image (gdk_pixbuf_get_pixels (gr->drawbuf), 
						CAIRO_FORMAT_ARGB32, 
						gdk_pixbuf_get_width (gr->drawbuf), 
						gdk_pixbuf_get_height (gr->drawbuf), 
						gdk_pixbuf_get_rowstride (gr->drawbuf));      
  g_assert (gr->surface != NULL);
  g_assert (gr->cr != NULL);
  cairo_set_target_surface (gr->cr, gr->surface);
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_drawPixels 
  (JNIEnv *env, jobject obj, jintArray java_pixels, 
   jint w, jint h, jint stride, jdoubleArray java_matrix)
{
  struct graphics2d *gr = NULL;
  jint *native_pixels = NULL;
  jdouble *native_matrix = NULL;

  gr = (struct graphics2d *) NSA_GET_G2D_PTR (env, obj);
  g_assert (gr != NULL);

  if (gr->debug) printf ("drawPixels (%d pixels, %dx%d, stride: %d)\n",
			 (*env)->GetArrayLength (env, java_pixels), w, h, stride);

  native_pixels = (*env)->GetIntArrayElements (env, java_pixels, NULL);
  native_matrix = (*env)->GetDoubleArrayElements (env, java_matrix, NULL);
  g_assert (native_pixels != NULL);
  g_assert (native_matrix != NULL);
  g_assert ((*env)->GetArrayLength (env, java_matrix) == 6);

  begin_drawing_operation (gr);
  
 {
   cairo_matrix_t *mat = NULL;
   cairo_surface_t *surf = cairo_surface_create_for_image ((char *)native_pixels, 
							   CAIRO_FORMAT_ARGB32, 
							   w, h, stride * 4);   
   mat = cairo_matrix_create ();
   cairo_matrix_set_affine (mat, 
			    native_matrix[0], native_matrix[1],
			    native_matrix[2], native_matrix[3],
			    native_matrix[4], native_matrix[5]);
   cairo_surface_set_matrix (surf, mat);
   if (native_matrix[0] != 1.
       || native_matrix[1] != 0.
       || native_matrix[2] != 0.
       || native_matrix[3] != 1.)
     {
       cairo_surface_set_filter (surf, CAIRO_FILTER_BILINEAR);
       cairo_surface_set_filter (gr->surface, CAIRO_FILTER_BILINEAR);
     }
   else
     {
       cairo_surface_set_filter (surf, CAIRO_FILTER_FAST);
       cairo_surface_set_filter (gr->surface, CAIRO_FILTER_FAST);
     }
   cairo_show_surface (gr->cr, surf, w, h);
   cairo_surface_set_filter (gr->surface, CAIRO_FILTER_FAST);
   cairo_matrix_destroy (mat);
   cairo_surface_destroy (surf);
 }
  
 end_drawing_operation (gr);

  (*env)->ReleaseIntArrayElements (env, java_pixels, native_pixels, 0);
  (*env)->ReleaseDoubleArrayElements (env, java_matrix, native_matrix, 0);

}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_setTexturePixels 
  (JNIEnv *env, jobject obj, jintArray jarr, jint w, jint h, jint stride)
{
  struct graphics2d *gr = NULL;
  jint *jpixels = NULL;

  gdk_threads_enter();
  if (peer_is_disposed(env, obj)) { gdk_threads_leave(); return; }
  gr = (struct graphics2d *) NSA_GET_G2D_PTR (env, obj);
  g_assert (gr != NULL);

  if (gr->debug) printf ("setTexturePixels (%d pixels, %dx%d, stride: %d)\n",
			 (*env)->GetArrayLength (env, jarr), w, h, stride);

  if (gr->pattern)
    cairo_pattern_destroy (gr->pattern);

  if (gr->pattern_surface)
    cairo_surface_destroy (gr->pattern_surface);

  if (gr->pattern_pixels)
    free (gr->pattern_pixels);

  gr->pattern = NULL;
  gr->pattern_surface = NULL;
  gr->pattern_pixels = NULL;

  gr->pattern_pixels = (char *) malloc (h * stride * 4);
  g_assert (gr->pattern_pixels != NULL);

  jpixels = (*env)->GetIntArrayElements (env, jarr, NULL);
  g_assert (jpixels != NULL);
  memcpy (gr->pattern_pixels, jpixels, h * stride * 4);
  (*env)->ReleaseIntArrayElements (env, jarr, jpixels, 0);

  gr->pattern_surface = cairo_surface_create_for_image (gr->pattern_pixels, 
							CAIRO_FORMAT_ARGB32, 
							w, h, stride * 4);
  g_assert (gr->pattern_surface != NULL);
  cairo_surface_set_repeat (gr->pattern_surface, 1);
  gr->pattern = cairo_pattern_create_for_surface (gr->pattern_surface);
  g_assert (gr->pattern != NULL);
  cairo_set_pattern (gr->cr, gr->pattern);
  gdk_threads_leave();
}