Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 382175

Summary: [GK3][Cairo] Images created with external image handle (GdkPixmap* or cairo_surface_t*) can't be drawn using GC.drawImage()
Product: [Eclipse Project] Platform Reporter: Alexander Mitin <Alexander.Mitin>
Component: SWTAssignee: Platform-SWT-Inbox <platform-swt-inbox>
Status: CLOSED WORKSFORME QA Contact:
Severity: normal    
Priority: P3 CC: clayberg, ericwill, markus.kell.r, Silenio_Quarti
Version: 4.2Keywords: triaged
Target Milestone: ---   
Hardware: PC   
OS: Linux-GTK   
Whiteboard:
Attachments:
Description Flags
Incorrectly drawn image.
none
Image drawn using getImageData() workaround, correctly drawn. none

Description Alexander Mitin CLA 2012-06-09 16:54:20 EDT
In WindowBuilder project I have to create an Image instance using external GdkPixmap* value.
I used Image.gtk_new() method to create such Image. Then I need to draw this image on some GC. This works fine with Cairo disabled. When Cairo is enabled, the image is not drawn.
The workaround is to invoke Image.getImageData() method. After this invocation it works fine.

Sample code:

  Image image = Image.gtk_new(null, SWT.BITMAP, pixmapHandle, 0);
          
  Image mainImage = new Image(null, 240, 160);
  GC gc = new GC(mainImage);
  // image.getImageData(); // uncomment to get it working
  gc.drawImage(image, 0, 0);
  gc.dispose();
  // save mainImage using ImageLoader
Comment 1 Alexander Mitin CLA 2012-06-09 17:01:19 EDT
Created attachment 217112 [details]
Incorrectly drawn image.
Comment 2 Alexander Mitin CLA 2012-06-09 17:02:37 EDT
Created attachment 217113 [details]
Image drawn using getImageData() workaround, correctly drawn.
Comment 3 Eric Clayberg CLA 2012-06-09 17:17:15 EDT
+1
Comment 4 Silenio Quarti CLA 2012-09-06 01:30:20 EDT
Fixed

http://git.eclipse.org/c/platform/eclipse.platform.swt.git/commit/?id=572d213ab35a497c10eab2f88d0a1f11c36bb71c

Simple test:


import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.internal.gtk.GdkColor;
import org.eclipse.swt.internal.gtk.OS;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;


public class Test {
public static void main(String[] args) {
	Display display = new Display();
	Shell shell = new Shell(display);
	shell.setLayout(new GridLayout(1, false));
	
	long pixmap = OS.gdk_pixmap_new(OS.gdk_get_default_root_window(), 100, 100, -1);
	GdkColor white = new GdkColor();
	white.red = (short)0xFFFF;
	white.green = (short)0;
	white.blue = (short)0;
	int /*long*/ colormap = OS.gdk_colormap_get_system();
	OS.gdk_colormap_alloc_color(colormap, white, true, true);
	int /*long*/ gdkGC = OS.gdk_gc_new(pixmap);
	OS.gdk_gc_set_foreground(gdkGC, white);
	OS.gdk_draw_rectangle(pixmap, gdkGC, 1, 0, 0, 100, 100);
	OS.g_object_unref(gdkGC);
	OS.gdk_colormap_free_colors(colormap, white, 1);
	
	final Image image = Image.gtk_new(null, SWT.BITMAP, pixmap, 0);

	shell.addListener(SWT.Paint, new Listener() {
		public void handleEvent(Event event) {
			event.gc.drawImage(image, 10, 10);
		}
	});

	shell.open();
	while (!shell.isDisposed()) {
		if (!display.readAndDispatch())
			display.sleep();
	}
	display.dispose();
}
}
Comment 5 Alexander Mitin CLA 2015-05-16 10:24:33 EDT
Reopening this.

While using gtk3 if I create an Image instance with external Cairo surface handle at least the following don't work:
1. Image.getBounds() returns Rectangle of (0, 0, 0, 0).
2. gc.drawImage(image, 0, 0) does nothing.

It seems to me the problem is that width/height fields are not initialized.

The workaround is to use Image(Device device, ImageData data) constructor:
    Image image = Image.gtk_new(null, SWT.BITMAP, surfaceHandle, 0);
    Image newImage = new Image(null, image.getImageData());
    image.dispose();
Comment 6 Eric Williams CLA 2018-05-11 14:44:34 EDT
Is this still reproducible?
Comment 7 Eric Williams CLA 2018-07-04 11:12:10 EDT
(In reply to Eric Williams from comment #6)
> Is this still reproducible?

No response in awhile -- closing. Please reopen this ticket if the issue occurs on 4.8 with GTK3.22.