diff options
Diffstat (limited to 'propertysheet/src/org/eclipse/wb/internal/core/utils/ui/DrawUtils.java')
-rw-r--r-- | propertysheet/src/org/eclipse/wb/internal/core/utils/ui/DrawUtils.java | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/propertysheet/src/org/eclipse/wb/internal/core/utils/ui/DrawUtils.java b/propertysheet/src/org/eclipse/wb/internal/core/utils/ui/DrawUtils.java index f7cc09d..c22256e 100644 --- a/propertysheet/src/org/eclipse/wb/internal/core/utils/ui/DrawUtils.java +++ b/propertysheet/src/org/eclipse/wb/internal/core/utils/ui/DrawUtils.java @@ -262,6 +262,73 @@ public class DrawUtils { //////////////////////////////////////////////////////////////////////////// // + // Rotated images + // + //////////////////////////////////////////////////////////////////////////// + /** + * Returns a new Image that is the given Image rotated left by 90 degrees. The client is + * responsible for disposing the returned Image. This method MUST be invoked from the + * user-interface (Display) thread. + * + * @param srcImage + * the Image that is to be rotated left + * @return the rotated Image (the client is responsible for disposing it) + */ + public static Image createRotatedImage(Image srcImage) { + // prepare Display + Display display = Display.getCurrent(); + if (display == null) { + SWT.error(SWT.ERROR_THREAD_INVALID_ACCESS); + } + // rotate ImageData + ImageData destData; + { + ImageData srcData = srcImage.getImageData(); + if (srcData.depth < 8) { + destData = rotatePixelByPixel(srcData); + } else { + destData = rotateOptimized(srcData); + } + } + // create new image + return new Image(display, destData); + } + + private static ImageData rotatePixelByPixel(ImageData srcData) { + ImageData destData = + new ImageData(srcData.height, srcData.width, srcData.depth, srcData.palette); + for (int y = 0; y < srcData.height; y++) { + for (int x = 0; x < srcData.width; x++) { + destData.setPixel(y, srcData.width - x - 1, srcData.getPixel(x, y)); + } + } + return destData; + } + + private static ImageData rotateOptimized(ImageData srcData) { + int bytesPerPixel = Math.max(1, srcData.depth / 8); + int destBytesPerLine = + ((srcData.height * bytesPerPixel - 1) / srcData.scanlinePad + 1) * srcData.scanlinePad; + byte[] newData = new byte[destBytesPerLine * srcData.width]; + for (int srcY = 0; srcY < srcData.height; srcY++) { + for (int srcX = 0; srcX < srcData.width; srcX++) { + int destX = srcY; + int destY = srcData.width - srcX - 1; + int destIndex = destY * destBytesPerLine + destX * bytesPerPixel; + int srcIndex = srcY * srcData.bytesPerLine + srcX * bytesPerPixel; + System.arraycopy(srcData.data, srcIndex, newData, destIndex, bytesPerPixel); + } + } + return new ImageData(srcData.height, + srcData.width, + srcData.depth, + srcData.palette, + srcData.scanlinePad, + newData); + } + + //////////////////////////////////////////////////////////////////////////// + // // Colors // //////////////////////////////////////////////////////////////////////////// |