Search in sources :

Example 61 with ImageData

use of org.eclipse.swt.graphics.ImageData in project yamcs-studio by yamcs.

the class GIFSymbolImage method loadAnimatedImage.

private void loadAnimatedImage(IJobErrorHandler errorHandler) {
    AbstractInputStreamRunnable uiTask = new AbstractInputStreamRunnable() {

        @Override
        public void runWithInputStream(InputStream stream) {
            synchronized (GIFSymbolImage.this) {
                ImageData[] dataArray = loader.load(stream);
                if (dataArray == null || dataArray.length < 1)
                    return;
                originalImageDataArray = dataArray;
                originalImageData = originalImageDataArray[0];
                animated = originalImageDataArray.length > 1;
                loadingImage = false;
                resetData();
                if (animated)
                    startAnimation();
                Display.getDefault().syncExec(new Runnable() {

                    @Override
                    public void run() {
                        fireSymbolImageLoaded();
                    }
                });
            }
        }
    };
    ResourceUtil.pathToInputStreamInJob(imagePath, uiTask, "Loading GIF Image...", errorHandler);
}
Also used : AbstractInputStreamRunnable(org.csstudio.swt.widgets.util.AbstractInputStreamRunnable) InputStream(java.io.InputStream) ImageData(org.eclipse.swt.graphics.ImageData) AbstractInputStreamRunnable(org.csstudio.swt.widgets.util.AbstractInputStreamRunnable)

Example 62 with ImageData

use of org.eclipse.swt.graphics.ImageData in project yamcs-studio by yamcs.

the class PNGSymbolImage method syncLoadImage.

// ************************************************************
// Image loading
// ************************************************************
public void syncLoadImage() {
    if (imagePath == null)
        return;
    InputStream stream = null;
    Image tempImage = null;
    try {
        stream = ResourceUtil.pathToInputStream(imagePath.toPortableString());
        tempImage = new Image(Display.getDefault(), stream);
        ImageData imgData = tempImage.getImageData();
        setOriginalImageData(imgData);
    } catch (Exception e) {
        Activator.getLogger().log(Level.WARNING, "ERROR in loading PNG image " + imagePath, e);
    } finally {
        try {
            if (stream != null)
                stream.close();
            if (tempImage != null && !tempImage.isDisposed())
                tempImage.dispose();
        } catch (IOException e) {
            Activator.getLogger().log(Level.WARNING, "ERROR in closing PNG image stream ", e);
        }
    }
}
Also used : InputStream(java.io.InputStream) ImageData(org.eclipse.swt.graphics.ImageData) IOException(java.io.IOException) Image(org.eclipse.swt.graphics.Image) IOException(java.io.IOException)

Example 63 with ImageData

use of org.eclipse.swt.graphics.ImageData in project yamcs-studio by yamcs.

the class PNGSymbolImage method loadImage.

private void loadImage(IJobErrorHandler errorHandler) {
    AbstractInputStreamRunnable uiTask = new AbstractInputStreamRunnable() {

        @Override
        public void runWithInputStream(InputStream stream) {
            synchronized (PNGSymbolImage.this) {
                Image tempImage = null;
                try {
                    tempImage = new Image(Display.getDefault(), stream);
                    ImageData imgData = tempImage.getImageData();
                    setOriginalImageData(imgData);
                } finally {
                    try {
                        stream.close();
                        if (tempImage != null && !tempImage.isDisposed()) {
                            tempImage.dispose();
                        }
                    } catch (IOException e) {
                        Activator.getLogger().log(Level.WARNING, "ERROR in closing PNG image stream ", e);
                    }
                }
                loadingImage = false;
                Display.getDefault().syncExec(new Runnable() {

                    public void run() {
                        fireSymbolImageLoaded();
                    }
                });
            }
        }
    };
    ResourceUtil.pathToInputStreamInJob(imagePath, uiTask, "Loading PNG Image...", errorHandler);
}
Also used : AbstractInputStreamRunnable(org.csstudio.swt.widgets.util.AbstractInputStreamRunnable) InputStream(java.io.InputStream) ImageData(org.eclipse.swt.graphics.ImageData) AbstractInputStreamRunnable(org.csstudio.swt.widgets.util.AbstractInputStreamRunnable) IOException(java.io.IOException) Image(org.eclipse.swt.graphics.Image)

Example 64 with ImageData

use of org.eclipse.swt.graphics.ImageData in project yamcs-studio by yamcs.

the class ImageUtils method convertToGrayscale.

/**
 * Convert a colored image to grayscale image using average method.
 */
public static ImageData convertToGrayscale(ImageData originalImageData) {
    ImageData imageData = (ImageData) originalImageData.clone();
    PaletteData palette = imageData.palette;
    if (palette.isDirect) {
        int[] lineData = new int[imageData.width];
        for (int y = 0; y < imageData.height; y++) {
            imageData.getPixels(0, y, imageData.width, lineData, 0);
            // Analyze each pixel value in the line
            for (int x = 0; x < lineData.length; x++) {
                RGB rgb = palette.getRGB(lineData[x]);
                // int gray = (int) Math.round((rgb.red + rgb.green + rgb.blue) / 3);
                int gray = (int) Math.round((0.21 * rgb.red) + (0.72 * rgb.green) + (0.07 * rgb.blue));
                int newColor = palette.getPixel(new RGB(gray, gray, gray));
                imageData.setPixel(x, y, newColor);
            }
        }
        if (imageData.transparentPixel != -1) {
            RGB rgb = palette.getRGB(imageData.transparentPixel);
            // int gray = (int) Math.round((rgb.red + rgb.green + rgb.blue) / 3);
            int gray = (int) Math.round((0.21 * rgb.red) + (0.72 * rgb.green) + (0.07 * rgb.blue));
            int newColor = palette.getPixel(new RGB(gray, gray, gray));
            imageData.transparentPixel = newColor;
        }
    } else {
        for (int i = 0; i < palette.colors.length; i++) {
            RGB rgb = palette.colors[i];
            // int gray = (int) Math.round((rgb.red + rgb.green + rgb.blue) / 3);
            int gray = (int) Math.round((0.21 * rgb.red) + (0.72 * rgb.green) + (0.07 * rgb.blue));
            palette.colors[i] = new RGB(gray, gray, gray);
        }
    }
    return imageData;
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) ImageData(org.eclipse.swt.graphics.ImageData) RGB(org.eclipse.swt.graphics.RGB) GradientPaint(java.awt.GradientPaint)

Example 65 with ImageData

use of org.eclipse.swt.graphics.ImageData in project yamcs-studio by yamcs.

the class ImageUtils method oldChangeImageColor.

// ************************************************************
// Old method to change image color
// ************************************************************
public static void oldChangeImageColor(Color color, ImageData originalImageData) {
    if (color == null || originalImageData == null || color.getRGB().equals(new RGB(0, 0, 0)))
        return;
    ImageData imageData = ImageUtils.convertToGrayscale(originalImageData);
    int newColor = 0;
    int[] lineData = new int[imageData.width];
    // Calculate pixel value (integer)
    if (imageData.palette.isDirect) {
        RGB rgb = color.getRGB();
        int redMask = imageData.palette.redMask;
        int blueMask = imageData.palette.blueMask;
        int greenMask = imageData.palette.greenMask;
        int redShift = imageData.palette.redShift;
        int greenShift = imageData.palette.greenShift;
        int blueShift = imageData.palette.blueShift;
        newColor |= (redShift < 0 ? rgb.red << -redShift : rgb.red >>> redShift) & redMask;
        newColor |= (greenShift < 0 ? rgb.green << -greenShift : rgb.green >>> greenShift) & greenMask;
        newColor |= (blueShift < 0 ? rgb.blue << -blueShift : rgb.blue >>> blueShift) & blueMask;
    } else {
        // Add new color in PaletteData colors
        int paletteLength = imageData.palette.colors.length;
        newColor = (imageData.transparentPixel + 1) % paletteLength;
        imageData.palette.colors[newColor] = color.getRGB();
    }
    for (int y = 0; y < imageData.height; y++) {
        imageData.getPixels(0, y, imageData.width, lineData, 0);
        // Analyze each pixel value in the line
        for (int x = 0; x < lineData.length; x++) {
            // Do not set transparent pixel && change only black pixel
            int pixelValue = lineData[x];
            if (!imageData.palette.isDirect) {
                pixelValue = imageData.palette.getPixel(imageData.palette.colors[lineData[x]]);
            }
            if (lineData[x] != imageData.transparentPixel && isShadeOfGray(pixelValue, imageData.palette)) {
                int appliedColor = applyShade(pixelValue, newColor, imageData.palette);
                if (imageData.alphaData == null) {
                // appliedColor = applyShade(pixelValue, newColor, imageData.palette);
                }
                imageData.setPixel(x, y, appliedColor);
            }
        }
    }
}
Also used : ImageData(org.eclipse.swt.graphics.ImageData) RGB(org.eclipse.swt.graphics.RGB) GradientPaint(java.awt.GradientPaint)

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