Search in sources :

Example 31 with ImageData

use of org.eclipse.swt.graphics.ImageData in project knime-core by knime.

the class KNIMECTabFolderRenderer method blur.

public ImageData blur(final Image src, final int radius, final int sigma) {
    float[] kernel = create1DKernel(radius, sigma);
    ImageData imgPixels = src.getImageData();
    int width = imgPixels.width;
    int height = imgPixels.height;
    int[] inPixels = new int[width * height];
    int[] outPixels = new int[width * height];
    int offset = 0;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            RGB rgb = imgPixels.palette.getRGB(imgPixels.getPixel(x, y));
            if (rgb.red == 255 && rgb.green == 255 && rgb.blue == 255) {
                inPixels[offset] = (rgb.red << 16) | (rgb.green << 8) | rgb.blue;
            } else {
                inPixels[offset] = (imgPixels.getAlpha(x, y) << 24) | (rgb.red << 16) | (rgb.green << 8) | rgb.blue;
            }
            offset++;
        }
    }
    convolve(kernel, inPixels, outPixels, width, height, true);
    convolve(kernel, outPixels, inPixels, height, width, true);
    ImageData dst = new ImageData(imgPixels.width, imgPixels.height, 24, new PaletteData(0xff0000, 0xff00, 0xff));
    dst.setPixels(0, 0, inPixels.length, inPixels, 0);
    offset = 0;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            if (inPixels[offset] == -1) {
                dst.setAlpha(x, y, 0);
            } else {
                int a = (inPixels[offset] >> 24) & 0xff;
                // if (a < 150) a = 0;
                dst.setAlpha(x, y, a);
            }
            offset++;
        }
    }
    return dst;
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) ImageData(org.eclipse.swt.graphics.ImageData) RGB(org.eclipse.swt.graphics.RGB) Point(org.eclipse.swt.graphics.Point)

Example 32 with ImageData

use of org.eclipse.swt.graphics.ImageData in project eclipse-pmd by acanda.

the class SWTResourceManager method getImage.

/**
 * Returns an {@link Image} encoded by the specified {@link InputStream}.
 *
 * @param stream
 *            the {@link InputStream} encoding the image data
 * @return the {@link Image} encoded by the specified input stream
 */
protected static Image getImage(final InputStream stream) throws IOException {
    try {
        final Display display = Display.getCurrent();
        final ImageData data = new ImageData(stream);
        if (data.transparentPixel > 0) {
            return new Image(display, data, data.getTransparencyMask());
        }
        return new Image(display, data);
    } finally {
        stream.close();
    }
}
Also used : ImageData(org.eclipse.swt.graphics.ImageData) Image(org.eclipse.swt.graphics.Image) Display(org.eclipse.swt.widgets.Display)

Example 33 with ImageData

use of org.eclipse.swt.graphics.ImageData in project eclipse.platform.swt by eclipse.

the class HoverHelp method open.

/**
 * Opens the main program.
 */
public Shell open(Display display) {
    // Load the images
    Class<HoverHelp> clazz = HoverHelp.class;
    try {
        if (images == null) {
            images = new Image[imageLocations.length];
            for (int i = 0; i < imageLocations.length; ++i) {
                try (InputStream stream = clazz.getResourceAsStream(imageLocations[i])) {
                    ImageData source = new ImageData(stream);
                    ImageData mask = source.getTransparencyMask();
                    images[i] = new Image(display, source, mask);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    } catch (Exception ex) {
        System.err.println(getResourceString("error.CouldNotLoadResources", ex.getMessage()));
        return null;
    }
    // Create the window
    Shell shell = new Shell();
    createPartControl(shell);
    shell.addDisposeListener(e -> {
        /* Free resources */
        if (images != null) {
            for (final Image image : images) {
                if (image != null)
                    image.dispose();
            }
            images = null;
        }
    });
    shell.pack();
    shell.open();
    return shell;
}
Also used : Shell(org.eclipse.swt.widgets.Shell) InputStream(java.io.InputStream) ImageData(org.eclipse.swt.graphics.ImageData) IOException(java.io.IOException) Image(org.eclipse.swt.graphics.Image) Point(org.eclipse.swt.graphics.Point) MissingResourceException(java.util.MissingResourceException) IOException(java.io.IOException)

Example 34 with ImageData

use of org.eclipse.swt.graphics.ImageData in project eclipse.platform.swt by eclipse.

the class ImageAnalyzer method menuSaveMaskAs.

void menuSaveMaskAs() {
    if (image == null || !showMask)
        return;
    if (imageData.getTransparencyType() == SWT.TRANSPARENCY_NONE)
        return;
    // stop any animation in progress
    animate = false;
    // Get the user to choose a file name and type to save.
    FileDialog fileChooser = new FileDialog(shell, SWT.SAVE);
    fileChooser.setFilterPath(lastPath);
    if (fileName != null)
        fileChooser.setFileName(fileName);
    fileChooser.setFilterExtensions(SAVE_FILTER_EXTENSIONS);
    fileChooser.setFilterNames(SAVE_FILTER_NAMES);
    String filename = fileChooser.open();
    lastPath = fileChooser.getFilterPath();
    if (filename == null)
        return;
    // Figure out what file type the user wants saved.
    int filetype = fileChooser.getFilterIndex();
    if (filetype == -1) {
        /* The platform file dialog does not support user-selectable file filters.
			 * Determine the desired type by looking at the file extension.
			 */
        filetype = determineFileType(filename);
        if (filetype == SWT.IMAGE_UNDEFINED) {
            MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
            box.setMessage(createMsg(bundle.getString("Unknown_extension"), filename.substring(filename.lastIndexOf('.') + 1)));
            box.open();
            return;
        }
    }
    if (new java.io.File(filename).exists()) {
        MessageBox box = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);
        box.setMessage(createMsg(bundle.getString("Overwrite"), filename));
        if (box.open() == SWT.CANCEL)
            return;
    }
    Cursor waitCursor = display.getSystemCursor(SWT.CURSOR_WAIT);
    shell.setCursor(waitCursor);
    imageCanvas.setCursor(waitCursor);
    try {
        // Save the mask of the current image to the specified file.
        ImageData maskImageData = imageData.getTransparencyMask();
        loader.data = new ImageData[] { maskImageData };
        loader.save(filename, filetype);
    } catch (SWTException e) {
        showErrorDialog(bundle.getString("Saving_lc"), filename, e);
    } catch (SWTError e) {
        showErrorDialog(bundle.getString("Saving_lc"), filename, e);
    } finally {
        shell.setCursor(null);
        imageCanvas.setCursor(crossCursor);
    }
}
Also used : SWTError(org.eclipse.swt.SWTError) SWTException(org.eclipse.swt.SWTException) ImageData(org.eclipse.swt.graphics.ImageData) Cursor(org.eclipse.swt.graphics.Cursor) FileDialog(org.eclipse.swt.widgets.FileDialog) Point(org.eclipse.swt.graphics.Point) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 35 with ImageData

use of org.eclipse.swt.graphics.ImageData in project eclipse.platform.swt by eclipse.

the class ImageAnalyzer method paintImage.

void paintImage(PaintEvent event) {
    GC gc = event.gc;
    Image paintImage = image;
    /* If the user wants to see the transparent pixel in its actual color,
		 * then temporarily turn off transparency.
		 */
    int transparentPixel = imageData.transparentPixel;
    if (transparentPixel != -1 && !transparent) {
        imageData.transparentPixel = -1;
        paintImage = new Image(display, imageData);
    }
    /* Scale the image when drawing, using the user's selected scaling factor. */
    int w = Math.round(imageData.width * xscale);
    int h = Math.round(imageData.height * yscale);
    /* If any of the background is visible, fill it with the background color. */
    Rectangle bounds = imageCanvas.getBounds();
    if (imageData.getTransparencyType() != SWT.TRANSPARENCY_NONE) {
        /* If there is any transparency at all, fill the whole background. */
        gc.fillRectangle(0, 0, bounds.width, bounds.height);
    } else {
        /* Otherwise, just fill in the backwards L. */
        if (ix + w < bounds.width)
            gc.fillRectangle(ix + w, 0, bounds.width - (ix + w), bounds.height);
        if (iy + h < bounds.height)
            gc.fillRectangle(0, iy + h, ix + w, bounds.height - (iy + h));
    }
    /* Draw the image */
    gc.drawImage(paintImage, 0, 0, imageData.width, imageData.height, ix + imageData.x, iy + imageData.y, w, h);
    /* If there is a mask and the user wants to see it, draw it. */
    if (showMask && (imageData.getTransparencyType() != SWT.TRANSPARENCY_NONE)) {
        ImageData maskImageData = imageData.getTransparencyMask();
        Image maskImage = new Image(display, maskImageData);
        gc.drawImage(maskImage, 0, 0, imageData.width, imageData.height, w + 10 + ix + imageData.x, iy + imageData.y, w, h);
        maskImage.dispose();
    }
    /* If transparency was temporarily disabled, restore it. */
    if (transparentPixel != -1 && !transparent) {
        imageData.transparentPixel = transparentPixel;
        paintImage.dispose();
    }
}
Also used : ImageData(org.eclipse.swt.graphics.ImageData) Rectangle(org.eclipse.swt.graphics.Rectangle) GC(org.eclipse.swt.graphics.GC) Image(org.eclipse.swt.graphics.Image) Point(org.eclipse.swt.graphics.Point)

Aggregations

ImageData (org.eclipse.swt.graphics.ImageData)132 Image (org.eclipse.swt.graphics.Image)78 PaletteData (org.eclipse.swt.graphics.PaletteData)38 RGB (org.eclipse.swt.graphics.RGB)33 Point (org.eclipse.swt.graphics.Point)32 Test (org.junit.Test)26 InputStream (java.io.InputStream)24 GC (org.eclipse.swt.graphics.GC)19 IOException (java.io.IOException)18 SWTException (org.eclipse.swt.SWTException)14 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)13 Rectangle (org.eclipse.swt.graphics.Rectangle)12 Color (org.eclipse.swt.graphics.Color)11 ImageLoader (org.eclipse.swt.graphics.ImageLoader)11 Display (org.eclipse.swt.widgets.Display)11 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Cursor (org.eclipse.swt.graphics.Cursor)7 Composite (org.eclipse.swt.widgets.Composite)7 GradientPaint (java.awt.GradientPaint)5 URL (java.net.URL)5