Bug 571242 - [GTK] Missing icons for p2 import wizards

Change-Id: Ia8d23c5221fea4b66f086430a7e17742ba9b16bf
Signed-off-by: Sravan Kumar Lakkimsetti <sravankumarl@in.ibm.com>
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/ImageList.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/ImageList.java
index afd04d5..cdefa7e 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/ImageList.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/ImageList.java
@@ -64,6 +64,59 @@
 	return newSurface;
 }
 
+public static long createPixbuf(long surface) {
+	int format = Cairo.cairo_image_surface_get_format(surface);
+	int width = Cairo.cairo_image_surface_get_width(surface);
+	int height = Cairo.cairo_image_surface_get_height(surface);
+	boolean hasAlpha = format == Cairo.CAIRO_FORMAT_ARGB32;
+	long pixbuf = GDK.gdk_pixbuf_new (GDK.GDK_COLORSPACE_RGB, hasAlpha, 8, width, height);
+	if (pixbuf == 0) SWT.error (SWT.ERROR_NO_HANDLES);
+	int stride = GDK.gdk_pixbuf_get_rowstride (pixbuf);
+	long pixels = GDK.gdk_pixbuf_get_pixels (pixbuf);
+	int oa, or, og, ob;
+	if (OS.BIG_ENDIAN) {
+		oa = 0; or = 1; og = 2; ob = 3;
+	} else {
+		oa = 3; or = 2; og = 1; ob = 0;
+	}
+	byte[] line = new byte[stride];
+	long surfaceData = Cairo.cairo_image_surface_get_data(surface);
+	if (hasAlpha) {
+		for (int y = 0; y < height; y++) {
+			C.memmove (line, surfaceData + (y * stride), stride);
+			for (int x = 0, offset = 0; x < width; x++, offset += 4) {
+				int a = line[offset + oa] & 0xFF;
+				int r = line[offset + or] & 0xFF;
+				int g = line[offset + og] & 0xFF;
+				int b = line[offset + ob] & 0xFF;
+				line[offset + 3] = (byte)a;
+				if (a != 0) {
+					line[offset + 0] = (byte)(((r * 0xFF) + a / 2) / a);
+					line[offset + 1] = (byte)(((g * 0xFF) + a / 2) / a);
+					line[offset + 2] = (byte)(((b * 0xFF) + a / 2) / a);
+				}
+			}
+			C.memmove (pixels + (y * stride), line, stride);
+		}
+	} else {
+		int cairoStride = Cairo.cairo_image_surface_get_stride(surface);
+		byte[] cairoLine = new byte[cairoStride];
+		for (int y = 0; y < height; y++) {
+			C.memmove (cairoLine, surfaceData + (y * cairoStride), cairoStride);
+			for (int x = 0, offset = 0, cairoOffset = 0; x < width; x++, offset += 3, cairoOffset += 4) {
+				byte r = cairoLine[cairoOffset + or];
+				byte g = cairoLine[cairoOffset + og];
+				byte b = cairoLine[cairoOffset + ob];
+				line[offset + 0] = r;
+				line[offset + 1] = g;
+				line[offset + 2] = b;
+			}
+			C.memmove (pixels + (y * stride), line, stride);
+		}
+	}
+	return pixbuf;
+}
+
 public static long createPixbuf(Image image) {
 	long surface = convertSurface(image);
 	int format = Cairo.cairo_image_surface_get_format(surface);
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TableItem.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TableItem.java
index ffa1ea5..bf0e0d5 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TableItem.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TableItem.java
@@ -1209,11 +1209,14 @@
 		ImageList imageList = parent.imageList;
 		if (imageList == null) imageList = parent.imageList = new ImageList();
 		int imageIndex = imageList.indexOf(image);
-		pixbuf = ImageList.createPixbuf(image);
+		// When we create a blank image surface gets created with dimensions 0, 0.
+        // This call recreates the surface with correct dimensions
+		surface = ImageList.convertSurface(image);
 		if (imageIndex == -1) {
 			imageIndex = imageList.add(image);
 		}
 		surface = imageList.getSurface(imageIndex);
+		pixbuf = ImageList.createPixbuf(surface);
 	}
 
 	long parentHandle = parent.handle;
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TreeItem.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TreeItem.java
index 03e6b0f..4e2700c 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TreeItem.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/TreeItem.java
@@ -1516,11 +1516,14 @@
 		ImageList imageList = parent.imageList;
 		if (imageList == null) imageList = parent.imageList = new ImageList();
 		int imageIndex = imageList.indexOf(image);
-		pixbuf = ImageList.createPixbuf(image);
+		// When we create a blank image surface gets created with dimensions 0, 0.
+        // This call recreates the surface with correct dimensions
+		surface = ImageList.convertSurface(image);
 		if (imageIndex == -1) {
 			imageIndex = imageList.add(image);
 		}
 		surface = imageList.getSurface(imageIndex);
+		pixbuf = ImageList.createPixbuf(surface);
 	}
 
 	int modelIndex = parent.columnCount == 0 ? Tree.FIRST_COLUMN : parent.columns [index].modelIndex;