aboutsummaryrefslogtreecommitdiffstats
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com
diff options
context:
space:
mode:
authorRaphael Moll <ralf@android.com>2010-11-19 16:14:06 -0800
committerRaphael Moll <ralf@android.com>2010-11-19 16:42:00 -0800
commit204277066e3daa72ab9b95c9c89ea11ff0ec3529 (patch)
tree427404b8100bdb395632133b28268f1914120648 /eclipse/plugins/com.android.ide.eclipse.adt/src/com
parentd59c1f5ad1dc6219fd901940a8c40f320dad5685 (diff)
downloadsdk-204277066e3daa72ab9b95c9c89ea11ff0ec3529.zip
sdk-204277066e3daa72ab9b95c9c89ea11ff0ec3529.tar.gz
sdk-204277066e3daa72ab9b95c9c89ea11ff0ec3529.tar.bz2
Fix SwtUtils conversion for no-alpha case.
Change-Id: I275b526670ca6e60cfc4f8749631dff2ef240b96
Diffstat (limited to 'eclipse/plugins/com.android.ide.eclipse.adt/src/com')
-rw-r--r--eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/SwtUtils.java78
1 files changed, 66 insertions, 12 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/SwtUtils.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/SwtUtils.java
index c2c46f1..88f91a0 100644
--- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/SwtUtils.java
+++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/SwtUtils.java
@@ -31,13 +31,41 @@ import java.awt.image.Raster;
* Various generic SWT utilities such as image conversion.
*/
public class SwtUtils {
+
private SwtUtils() {
}
/**
+ * Returns the {@link PaletteData} describing the ARGB ordering expected from
+ * integers representing pixels for AWT {@link BufferedImage}.
+ *
+ * @return A new {@link PaletteData} suitable for AWT images.
+ */
+ public static PaletteData getAwtPaletteData(int imageType) {
+ switch (imageType) {
+ case BufferedImage.TYPE_INT_RGB:
+ case BufferedImage.TYPE_INT_ARGB:
+ case BufferedImage.TYPE_INT_ARGB_PRE:
+ return new PaletteData(0x00FF0000, 0x0000FF00, 0x000000FF);
+
+ case BufferedImage.TYPE_3BYTE_BGR:
+ case BufferedImage.TYPE_4BYTE_ABGR:
+ case BufferedImage.TYPE_4BYTE_ABGR_PRE:
+ return new PaletteData(0x000000FF, 0x0000FF00, 0x00FF0000);
+
+ default:
+ throw new UnsupportedOperationException("RGB type not supported yet.");
+ }
+ }
+
+ /**
* Converts an AWT {@link BufferedImage} into an equivalent SWT {@link Image}. Whether
* the transparency data is transferred is optional, and this method can also apply an
* alpha adjustment during the conversion.
+ * <p/>
+ * Implementation details: on Windows, the returned {@link Image} will have an ordering
+ * matching the Windows DIB (e.g. RGBA, not ARGB). Callers must make sure to use
+ * <code>Image.getImageData().paletteData</code> to get the right pixels out of the image.
*
* @param display The display where the SWT image will be shown
* @param awtImage The AWT {@link BufferedImage}
@@ -55,8 +83,8 @@ public class SwtUtils {
Raster raster = awtImage.getData(new java.awt.Rectangle(width, height));
int[] imageDataBuffer = ((DataBufferInt) raster.getDataBuffer()).getData();
- ImageData imageData = new ImageData(width, height, 32, new PaletteData(0x00FF0000,
- 0x0000FF00, 0x000000FF));
+ ImageData imageData =
+ new ImageData(width, height, 32, getAwtPaletteData(awtImage.getType()));
imageData.setPixels(0, 0, imageDataBuffer.length, imageDataBuffer, 0);
@@ -83,8 +111,7 @@ public class SwtUtils {
imageData.alpha = globalAlpha;
}
- Image image = new Image(display, imageData);
- return image;
+ return new Image(display, imageData);
}
/**
@@ -96,14 +123,41 @@ public class SwtUtils {
* @return an AWT image representing the source SWT image
*/
public static BufferedImage convertToAwt(Image swtImage) {
- ImageData data = swtImage.getImageData();
- BufferedImage awtImage = new BufferedImage(data.width, data.height, BufferedImage.TYPE_INT_ARGB);
- PaletteData palette = data.palette;
- if (palette.isDirect) {
- for (int y = 0; y < data.height; y++) {
- for (int x = 0; x < data.width; x++) {
- int pixel = data.getPixel(x, y);
- awtImage.setRGB(x, y, 0xFF000000 | pixel);
+ ImageData swtData = swtImage.getImageData();
+ BufferedImage awtImage =
+ new BufferedImage(swtData.width, swtData.height, BufferedImage.TYPE_INT_ARGB);
+ PaletteData swtPalette = swtData.palette;
+ if (swtPalette.isDirect) {
+ PaletteData awtPalette = getAwtPaletteData(awtImage.getType());
+
+ if (swtPalette.equals(awtPalette)) {
+ // No color conversion needed.
+ for (int y = 0; y < swtData.height; y++) {
+ for (int x = 0; x < swtData.width; x++) {
+ int pixel = swtData.getPixel(x, y);
+ awtImage.setRGB(x, y, 0xFF000000 | pixel);
+ }
+ }
+ } else {
+ // We need to remap the colors
+ int sr = -awtPalette.redShift + swtPalette.redShift;
+ int sg = -awtPalette.greenShift + swtPalette.greenShift;
+ int sb = -awtPalette.blueShift + swtPalette.blueShift;
+
+ for (int y = 0; y < swtData.height; y++) {
+ for (int x = 0; x < swtData.width; x++) {
+ int pixel = swtData.getPixel(x, y);
+
+ int r = pixel & swtPalette.redMask;
+ int g = pixel & swtPalette.greenMask;
+ int b = pixel & swtPalette.blueMask;
+ r = (sr < 0) ? r >>> -sr : r << sr;
+ g = (sg < 0) ? g >>> -sg : g << sg;
+ b = (sb < 0) ? b >>> -sb : b << sb;
+
+ pixel = 0xFF000000 | r | g | b;
+ awtImage.setRGB(x, y, pixel);
+ }
}
}
} else {