if (image == null)
return image;
// create custom image
final int SIZE= 16; // square images
ImageData data= image.getImageData();
Image copy;
if (data.height > SIZE || data.width > SIZE) {
// scale down to icon size
copy= new Image(Display.getCurrent(), data.scaledTo(SIZE, SIZE));
} else {
// don't scale up, but rather copy into the middle and mark everything else transparent
ImageData mask= data.getTransparencyMask();
ImageData resized= new ImageData(SIZE, SIZE, data.depth, data.palette);
ImageData resizedMask= new ImageData(SIZE, SIZE, mask.depth, mask.palette);
int xo= Math.max(0, (SIZE - data.width) / 2);
int yo= Math.max(0, (SIZE - data.height) / 2);
for (int y= 0; y < SIZE; y++) {
for (int x= 0; x < SIZE; x++) {
if (y >= yo && x >= xo && y < yo + data.height && x < xo + data.width) {
resized.setPixel(x, y, data.getPixel(x - xo, y - yo));
resizedMask.setPixel(x, y, mask.getPixel(x - xo, y - yo));
}
}
}
copy= new Image(Display.getCurrent(), resized, resizedMask);