image = imageDescriptor.createImage();
Rectangle bound = image.getBounds();
if (bound.width == 16 && bound.height == 16) {
// perfect! it did what was expected!
//
final ImageData data = (ImageData) image.getImageData().clone();
return new ImageDescriptor(){
public ImageData getImageData() {
return (ImageData) data.clone();
}
};
}
if (bound.height < 16 || bound.width < 16) {
// the image is smaller than what we asked for
// perhaps we should display nothing?
// in stead we will try scaling it up
if (WmsPlugin.getDefault().isDebugging()) {
System.out.println("Icon scaled up to requested size"); //$NON-NLS-1$
}
final ImageData data = image.getImageData().scaledTo(16, 16);
return new ImageDescriptor(){
public ImageData getImageData() {
return (ImageData) data.clone();
}
};
}
// the image is larger than the size we asked for
// (so this WMS is not being nice!)
// we will try and decide what to do here ...
// let us select the image we want ...
swatch = new Image(null, 16, 16);
GC gc = new GC(swatch);
int sy = 0; // (bound.height / 2 ) - 8;
int sx = 0;
int sw = 0;
int sh = 0;
ImageData contents = image.getImageData();
if (contents == null) {
return CatalogUIPlugin.getDefault().getImageDescriptor(
ISharedImages.GRID_MISSING);
}
if (contents.maskData != null) {
// ((width + 7) / 8 + (maskPad - 1)) / maskPad * maskPad
int maskPad = contents.maskPad;
int scanLine = ((contents.width + 7) / 8 + (maskPad - 1)) / maskPad * maskPad;
// skip leading mask ...
SKIPY: for( int y = 0; y < contents.height / 2; y++ ) {
sy = y;
for( int x = 0; x < contents.width / 2; x++ ) {
int mask = contents.maskData[y * scanLine + x];
if (mask != 0)
break SKIPY;
}
}
SKIPX: for( int x = 0; x < contents.width / 2; x++ ) {
sx = x;
for( int y = sy; y < contents.height / 2; y++ ) {
int mask = contents.maskData[y * scanLine + x];
if (mask != 0)
break SKIPX;
}
}
sh = Math.min(contents.height - sy, 16);
sw = Math.min(contents.width - sx, 16);
if (WmsPlugin.getDefault().isDebugging())
System.out.println("Mask offset to " + sx + "x" + sy); //$NON-NLS-1$ //$NON-NLS-2$
} else if (contents.alphaData != null) {
SKIPY: for( int y = 0; y < contents.height / 2; y++ ) {
sy = y;
for( int x = 0; x < contents.width / 2; x++ ) {
int alpha = contents.alphaData[y * contents.width + x];
if (alpha != 0)
break SKIPY;
}
}
SKIPX: for( int x = 0; x < contents.width / 2; x++ ) {
sx = x;
for( int y = sy; y < contents.height / 2; y++ ) {
int alpha = contents.alphaData[y * contents.width + x];
if (alpha != 0)
break SKIPX;
}
}
sh = Math.min(contents.height - sy, 16);
sw = Math.min(contents.width - sx, 16);
if (WmsPlugin.getDefault().isDebugging())
System.out.println("Alpha offset to " + sx + "x" + sy); //$NON-NLS-1$ //$NON-NLS-2$
} else {
// try ignoring "white"
int depth = contents.depth;
int scanLine = contents.bytesPerLine;
SKIPY: for( int y = 0; y < contents.height / 2; y++ ) {
sy = y;
for( int x = 0; x < contents.width / 2; x++ ) {
int datum = contents.data[y * scanLine + x * depth];
if (datum != 0)
break SKIPY;
}
}
SKIPX: for( int x = 0; x < contents.width / 2; x++ ) {
sx = x;
for( int y = sy; y < contents.height / 2; y++ ) {
int datum = contents.data[y * scanLine + x * depth];
if (datum != 0)
break SKIPX;
}
}
sh = Math.min(contents.height - sy, 16);
sw = Math.min(contents.width - sx, 16);
if (WmsPlugin.getDefault().isDebugging())
System.out.println("Alpha offset to " + sx + "x" + sy); //$NON-NLS-1$ //$NON-NLS-2$
}
// else {
// sh = Math.min( bound.height, bound.width );
// sw = Math.min( bound.height, bound.width );
// }
if (WmsPlugin.getDefault().isDebugging())
System.out.println("Image resized to " + sh + "x" + sw); //$NON-NLS-1$ //$NON-NLS-2$
// gc.drawImage(image, sx, sy, sw, sh, 0, 0, 16, 16);
// chances are this has a label or category view or something
// grab the gply from the bottom left corner and we are good to
// (based on mapserver example)
//
gc.drawImage(image, 0, bound.height - 16, 16, 16, 0, 0, 16, 16);
final ImageData data = (ImageData) swatch.getImageData().clone();
return new ImageDescriptor(){
public ImageData getImageData() {
return (ImageData) data.clone();
}
};
} finally {
if (image != null) {
image.dispose();