Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Transparent, scalable figures
Transparent, scalable figures [message #153378] Fri, 08 October 2004 20:29
Eclipse UserFriend
Originally posted by: kvdijken.tiscali.nl

This is my current code for scalable transparent figures (polygons, code for
linestrings is similar). For a history and notes on how to use it, see
thread 'setLineWidth in pixel units instead of model units ?'. Note that
this code does not win a price for clean programming, as a lot of
subclassing of standard classes has to be done to use this code, but I see
no other way to get scalable, transparent figures (yet).

The alphaData array needs a scanline padding of 1. By feeding the Image an
ImageData with padding 1, my hope was to be able to skip the convertPad
method call. Unfortunately, the Image class refuses to use the padding of 1
(on win32), and getImageData() returns an ImageData with padding 4, so the
convertPad call is still necessary.

Also the call to getImageData() is an expensive one, I'd like to get rid of
it.

I hope other people can improve on this code (especially speed is
important).

Koen van Dijken


RGB rgbBlack = new RGB(0, 0, 0);

RGB rgbWhite = new RGB(255, 255, 255);

RGB[] rgb = new RGB[256];
{
for (int i = 0; i < 256; i++)
rgb[i] = rgbWhite;
}

byte[] convertPad(byte[] data, int width, int height, int depth, int
pad,
int newPad) {
if (pad == newPad)
return data;
int stride = (width * depth + 7) / 8;
int bpl = (stride + (pad - 1)) / pad * pad;
int newBpl = (stride + (newPad - 1)) / newPad * newPad;
byte[] newData = new byte[height * newBpl];
int srcIndex = 0, destIndex = 0;
for (int y = 0; y < height; y++) {
System.arraycopy(data, srcIndex, newData, destIndex, stride);
srcIndex += bpl;
destIndex += newBpl;
}
return newData;
}

public void fillTransparentPolygon(PointList pointList, int alpha,
boolean border) {
PointList points = pointList.getCopy();

Rectangle size = points.getBounds().getCopy();

size.x *= zoom;
size.y *= zoom;
size.width *= zoom;
size.height *= zoom;

// Clone the original figure. We have to translate it
// because we need a (0,0) oriented figure in our GC.
points.translate(-points.getBounds().x, -points.getBounds().y);
int[] pointArray = points.toIntArray();

for (int i = 0; i < pointArray.length; i++)
pointArray[i] *= zoom;

byte[] alphaData = new byte[size.width * size.height];

rgb[alpha] = rgbBlack;
try {
PaletteData paletteTemp = new PaletteData(rgb);

// Unfortunately scanline padding != 4 is not supported (on
win32?).
// The call to convertPad is still necessary to convert it to
// a padding of 1, as required by the alpha mask.
final int DEPTH = 8;
int bytesPerLine = (size.width * DEPTH + 7) / 8;
byte[] imageData = new byte[bytesPerLine * size.height];
ImageData dataTemp = new ImageData(size.width, size.height,
DEPTH,
paletteTemp, 1, imageData);

Image imageTemp = new Image(Display.getCurrent(), dataTemp);
try {
GC gcTemp = new GC(imageTemp);
try {
gcTemp.setBackground(ColorConstants.black);
gcTemp.fillPolygon(pointArray);
if (border) {
gcTemp.setForeground(this.getForegroundColor());
gcTemp.setLineStyle(this.getLineStyle());
gcTemp.setLineWidth(this.getLineWidth());
gcTemp.drawPolygon(pointArray);
}

ImageData data = imageTemp.getImageData();
alphaData = convertPad(data.data, data.width,
data.height,
data.depth, data.scanlinePad, 1);
} finally {
gcTemp.dispose();
}
} finally {
imageTemp.dispose();
}
} finally {
rgb[alpha] = rgbWhite;
}

// Now draw the polygon in full color with the
// alpha mask calculated in the previous step.
PaletteData paletteFinal = new PaletteData(0xFF, 0xFF00, 0xFF0000);
ImageData dataFinal = new ImageData(size.width, size.height, 24,
paletteFinal);
dataFinal.alphaData = alphaData;
Image imageFinal = new Image(Display.getCurrent(), dataFinal);
try {
GC gc = new GC(imageFinal);
try {
gc.setBackground(this.getBackgroundColor());
gc.fillPolygon(pointArray);
if (border) {
gc.setForeground(this.getForegroundColor());
gc.setLineStyle(this.getLineStyle());
gc.setLineWidth(this.getLineWidth());
gc.drawPolygon(pointArray);
}
graphics.drawImage(imageFinal, size.x, size.y);
} finally {
gc.dispose();
}
} finally {
imageFinal.dispose();
}
}
Previous Topic:is it possible to create SWT controls offscreen and scrape their graphics?
Next Topic:Where should I dispose the Image used by Label?
Goto Forum:
  


Current Time: Fri Apr 26 22:06:16 GMT 2024

Powered by FUDForum. Page generated in 0.03061 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top