import com.android.layoutlib.api.LayoutBridge;
import com.android.layoutlib.api.LayoutScene;
import com.android.layoutlib.api.SceneResult;
+import com.android.layoutlib.api.ViewInfo;
import com.android.sdklib.SdkConstants;
import org.eclipse.swt.SWT;
// actually scanning pixels.
/** Width of the rendered preview image (before it is cropped) */
- private static final int RENDER_HEIGHT = 200;
+ private static final int RENDER_HEIGHT = 400;
/** Height of the rendered preview image (before it is cropped) */
- private static final int RENDER_WIDTH = 300;
+ private static final int RENDER_WIDTH = 500;
/** Amount of alpha to multiply into the image (divided by 256) */
private static final int IMG_ALPHA = 192;
BufferedImage image = result.getImage();
if (image != null) {
BufferedImage cropped;
+ Rect initialCrop = null;
+ ViewInfo viewInfo = result.getRootView();
+ if (viewInfo != null) {
+ int x1 = viewInfo.getLeft();
+ int x2 = viewInfo.getRight();
+ int y2 = viewInfo.getBottom();
+ int y1 = viewInfo.getTop();
+ initialCrop = new Rect(x1, y1, x2 - x1, y2 - y1);
+ }
+
if (hasTransparency) {
- cropped = SwtUtils.cropBlank(image);
+ cropped = SwtUtils.cropBlank(image, initialCrop);
} else {
int edgeColor = image.getRGB(image.getWidth() - 1, image.getHeight() - 1);
- cropped = SwtUtils.cropColor(image, edgeColor);
+ cropped = SwtUtils.cropColor(image, edgeColor, initialCrop);
}
if (cropped != null) {
*/
package com.android.ide.eclipse.adt.internal.editors.layout.gle2;
+import com.android.ide.common.api.Rect;
+
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
* this is not the same as pixels that aren't opaque (an alpha value other than 255).
*
* @param image the image to be cropped
+ * @param initialCrop If not null, specifies a rectangle which contains an initial
+ * crop to continue. This can be used to crop an image where you already
+ * know about margins in the image
* @return a cropped version of the source image, or null if the whole image was blank
* and cropping completely removed everything
*/
- public static BufferedImage cropBlank(BufferedImage image) {
- return crop(image, new CropFilter() {
+ public static BufferedImage cropBlank(BufferedImage image, Rect initialCrop) {
+ CropFilter filter = new CropFilter() {
public boolean crop(BufferedImage bufferedImage, int x, int y) {
int rgb = bufferedImage.getRGB(x, y);
return (rgb & 0xFF000000) == 0x00000000;
// TODO: Do a threshold of 80 instead of just 0? Might give better
// visual results -- e.g. check <= 0x80000000
}
- });
+ };
+ return crop(image, filter, initialCrop);
}
/**
* @param image the image to be cropped
* @param blankRgba the color considered to be blank, as a 32 pixel integer with 8
* bits of alpha, red, green and blue
+ * @param initialCrop If not null, specifies a rectangle which contains an initial
+ * crop to continue. This can be used to crop an image where you already
+ * know about margins in the image
* @return a cropped version of the source image, or null if the whole image was blank
* and cropping completely removed everything
*/
- public static BufferedImage cropColor(BufferedImage image, final int blankRgba) {
- return crop(image, new CropFilter() {
+ public static BufferedImage cropColor(BufferedImage image,
+ final int blankRgba, Rect initialCrop) {
+ CropFilter filter = new CropFilter() {
public boolean crop(BufferedImage bufferedImage, int x, int y) {
return blankRgba == bufferedImage.getRGB(x, y);
}
- });
+ };
+ return crop(image, filter, initialCrop);
}
/**
boolean crop(BufferedImage image, int x, int y);
}
- private static BufferedImage crop(BufferedImage image, CropFilter filter) {
+ private static BufferedImage crop(BufferedImage image, CropFilter filter, Rect initialCrop) {
if (image == null) {
return null;
}
// First, determine the dimensions of the real image within the image
- int x1 = 0;
- int y1 = 0;
- int x2 = image.getWidth();
- int y2 = image.getHeight();
+ int x1, y1, x2, y2;
+ if (initialCrop != null) {
+ x1 = initialCrop.x;
+ y1 = initialCrop.y;
+ x2 = initialCrop.x + initialCrop.w;
+ y2 = initialCrop.y + initialCrop.h;
+ } else {
+ x1 = 0;
+ y1 = 0;
+ x2 = image.getWidth();
+ y2 = image.getHeight();
+ }
+
+ // Nothing left to crop
+ if (x1 == x2 || y1 == y2) {
+ return null;
+ }
// This algorithm is a bit dumb -- it just scans along the edges looking for
// a pixel that shouldn't be cropped. I could maybe try to make it smarter by
package com.android.ide.eclipse.adt.internal.editors.layout.gle2;
+import com.android.ide.common.api.Rect;
+
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.dispose();
- BufferedImage crop = SwtUtils.cropBlank(image);
+ BufferedImage crop = SwtUtils.cropBlank(image, null);
+ assertNull(crop);
+ }
+
+ public void testCropBlankPre() throws Exception {
+ BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB_PRE);
+ Graphics g = image.getGraphics();
+ g.setColor(new Color(0, true));
+ g.fillRect(0, 0, image.getWidth(), image.getHeight());
+ g.dispose();
+
+ BufferedImage crop = SwtUtils.cropBlank(image, new Rect(5, 5, 80, 80));
assertNull(crop);
}
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.dispose();
- BufferedImage crop = SwtUtils.cropBlank(image);
+ BufferedImage crop = SwtUtils.cropBlank(image, null);
assertNotNull(crop);
assertEquals(image.getWidth(), crop.getWidth());
assertEquals(image.getHeight(), crop.getHeight());
g.fillRect(25, 25, 50, 50);
g.dispose();
- BufferedImage crop = SwtUtils.cropBlank(image);
+ BufferedImage crop = SwtUtils.cropBlank(image, null);
+ assertNotNull(crop);
+ assertEquals(50, crop.getWidth());
+ assertEquals(50, crop.getHeight());
+ assertEquals(0xFF00FF00, crop.getRGB(0, 0));
+ assertEquals(0xFF00FF00, crop.getRGB(49, 49));
+ }
+
+ public void testCropSomethingPre() throws Exception {
+ BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB_PRE);
+ Graphics g = image.getGraphics();
+ g.setColor(new Color(0, true));
+ g.fillRect(0, 0, image.getWidth(), image.getHeight());
+ g.setColor(new Color(0xFF00FF00, true));
+ g.fillRect(25, 25, 50, 50);
+ g.dispose();
+
+ BufferedImage crop = SwtUtils.cropBlank(image, new Rect(0, 0, 100, 100));
+ assertNotNull(crop);
+ assertEquals(50, crop.getWidth());
+ assertEquals(50, crop.getHeight());
+ assertEquals(0xFF00FF00, crop.getRGB(0, 0));
+ assertEquals(0xFF00FF00, crop.getRGB(49, 49));
+ }
+
+ public void testCropSomethingPre2() throws Exception {
+ BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB_PRE);
+ Graphics g = image.getGraphics();
+ g.setColor(new Color(0, true));
+ g.fillRect(0, 0, image.getWidth(), image.getHeight());
+ g.setColor(new Color(0xFF00FF00, true));
+ g.fillRect(25, 25, 50, 50);
+ g.dispose();
+
+ BufferedImage crop = SwtUtils.cropBlank(image, new Rect(5, 5, 80, 80));
assertNotNull(crop);
assertEquals(50, crop.getWidth());
assertEquals(50, crop.getHeight());
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.dispose();
- BufferedImage crop = SwtUtils.cropColor(image, 0xFF00FF00);
+ BufferedImage crop = SwtUtils.cropColor(image, 0xFF00FF00, null);
assertNull(crop);
}
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.dispose();
- BufferedImage crop = SwtUtils.cropColor(image, 0xFFFF0000);
+ BufferedImage crop = SwtUtils.cropColor(image, 0xFFFF0000, null);
assertNotNull(crop);
assertEquals(image.getWidth(), crop.getWidth());
assertEquals(image.getHeight(), crop.getHeight());
g.fillRect(25, 25, 50, 50);
g.dispose();
- BufferedImage crop = SwtUtils.cropColor(image, 0xFF00FF00);
+ BufferedImage crop = SwtUtils.cropColor(image, 0xFF00FF00, null);
assertEquals(50, crop.getWidth());
assertEquals(50, crop.getHeight());
assertEquals(0xFFFF0000, crop.getRGB(0, 0));
}
public void testNullOk() throws Exception {
- SwtUtils.cropBlank(null);
- SwtUtils.cropColor(null, 0);
+ SwtUtils.cropBlank(null, null);
+ SwtUtils.cropColor(null, 0, null);
}
public void testImageConvertNoAlpha() throws Exception {
}
}
+ public void testNothingTodo() throws Exception {
+ BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB_PRE);
+ Graphics g = image.getGraphics();
+ g.setColor(new Color(0xFF00FF00, true));
+ g.fillRect(0, 0, image.getWidth(), image.getHeight());
+ g.dispose();
+
+ BufferedImage crop = SwtUtils.cropColor(image, 0xFFFF0000, new Rect(40, 40, 0, 0));
+ assertNull(crop);
+ }
+
}