Copy Java swt image fragments with alpha

Im using Java swt Images with full 32-bit color and alpha channels (org.eclipse.swt.graphics.Image). I need to extract a rectangle of the image as a new image, including full alpha channel information.

For example, I may have a 100 × 100 image, and I need to create a new image with only a 50x pixel in the upper left corner, just like in a 100 × 100 image. (Im chopping, not rescaling.)

My initial attempt was to create an empty image of the required size (50 × 50 in this example), then get the GC for the new image, and then draw the correct part of the old image on the new image. The problem was that transparency was not preserved. If my new image starts opaque, the result after the draw will be completely opaque. If my new image becomes transparent, the result after drawing will be completely transparent (all RGB colors will be correct, but invisible, because alpha will be zero for all pixels).

My current solution is awkward: I use GC to draw an image, and then manually copy the alpha channel line by line.

/**
 * Returns a sub-rectangle from the image, preserving alpha transparency.
 * @param source The image from which to copy a portion
 * @param sourceData The ImageData of the source image
 * @param xInSource The x-coordinate in the source image from which to start copying
 * @param yInSource The y-coordinate in the source image from which to start copying
 * @param widthInSource The width (in pixels) of the copied image portion
 * @param heightInSource The height (in pixels) of the copied image portion
 * @param disp The display to use for the new image, or null for the current display
 * @return New image copied from a portion of the source.
 */
private static Image getRectHelper(final Image source, final ImageData sourceData,
        final int xInSource, final int yInSource, final int widthInSource, final int heightInSource, final Display disp)
{
    // In cases like extracting multiple frames at once, we need both the Image and its ImageData without
    // having to extract the ImageData over and over. That’s why this helper takes both parameters.

    // New up an image of the appropriate size
    final Display d = disp==null ? Display.getDefault() : disp;
    final Image dest = new Image(d, widthInSource, heightInSource);

    // This draws the colors from the original image, but does not set the destination alphas
    // (For the  Image class, alpha transparency doesn’t work well – it seems to have been tacked on as an afterthought.)
    final GC g = new GC(dest);
    g.drawImage(source, xInSource, yInSource, widthInSource, heightInSource, 0, 0, widthInSource, heightInSource);
    g.dispose();

    // Get a copy of the dest image data, then sets the destination alphas from the source image
    final ImageData destData = dest.getImageData();
    copyAlpha(sourceData, destData, xInSource, yInSource, widthInSource, heightInSource, 0, 0);

    // Construct a new image with the modified image data
    final Image ret = new Image(d, destData);
    dest.dispose();

    return ret;
}

/**
 * Copies a block of alpha channel information from the sourceData to the destaData. 
 * 
 * @param sourceData The source image data from which to copy alpha information
 * @param destData The destination image data to modify with new alpha information
 * @param xInSource The x-coordinate from which the alpha information should be copied
 * @param yInSource The y-coordinate from which the alpha information should be copied
 * @param width The width (in pixels) of the alpha information to copy
 * @param height The height (in pixels) of the alpha information to copy
 * @param xInDest The x-coordinate to which the alpha information should be copied 
 * @param yInDest The y-coordinate to which the alpha information should be copied
 */
private static void copyAlpha(final ImageData sourceData, final ImageData destData,
        final int xInSource, final int yInSource, final int width, final int height,
        final int xInDest, final int yInDest)
{
    final byte[] alphas = new byte[width];
    for (int ySrc = yInSource, yDst = yInDest; ySrc < yInSource+height; ++ySrc, ++yDst) {
        sourceData.getAlphas(xInSource, ySrc, width, alphas, 0);
        destData.setAlphas(xInDest, yDst, width, alphas, 0);
    }
}

? Java, - . - swt? , . Im ImageData GC.

+5
1

, , , , , . http://help.eclipse.org/kepler/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/graphics/ImageData.html#getTransparencyType()

, SWT, ImageData. , ImageData, :

  • SWT.TRANSPARENCY_ALPHA, , , .
  • SWT.TRANSPARENCY_PIXEL, transparentDixel ImageData ImageData.
  • SWT.TRANSPARENCY_MASK, maskData maskPad ImageData ImageData.

ImageData ( ) GC.

, .

0

All Articles