Search in sources :

Example 66 with ImageData

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

the class ImageUtils method changeImageColor2.

public static ImageData changeImageColor2(Color color, ImageData originalImageData) {
    if (color == null || originalImageData == null || color.getRGB().equals(new RGB(0, 0, 0)))
        return originalImageData;
    ImageData imageData = ImageUtils.convertToGrayscale(originalImageData);
    new Colorizer().doColorize(imageData);
    return imageData;
}
Also used : ImageData(org.eclipse.swt.graphics.ImageData) RGB(org.eclipse.swt.graphics.RGB)

Example 67 with ImageData

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

the class ImageUtils method changeImageColor.

/**
 * Apply color change on an image.
 *
 * @param color
 * @param imageData
 */
public static ImageData changeImageColor(Color color, ImageData originalImageData) {
    if (color == null || originalImageData == null || color.getRGB().equals(new RGB(0, 0, 0)))
        return originalImageData;
    ImageData imageData = ImageUtils.convertToGrayscale(originalImageData);
    int luminance = (int) Math.round((0.299 * color.getRed()) + (0.587 * color.getGreen()) + (0.114 * color.getBlue()));
    // find min/max/average values ignoring white & transparent
    int sum = 0, count = 0, min = 0, max = 0;
    int[] lineData = new int[imageData.width];
    PaletteData palette = imageData.palette;
    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++) {
            int pixelValue = lineData[x];
            // Do not set transparent pixel
            if (lineData[x] != imageData.transparentPixel) {
                // Get pixel color value if not using direct palette
                if (!palette.isDirect) {
                    pixelValue = palette.getPixel(palette.colors[lineData[x]]);
                }
                RGB current = palette.getRGB(pixelValue);
                if (current.blue == current.green && current.blue == current.red && current.blue < 255) {
                    min = Math.min(current.red, min);
                    max = Math.max(current.red, max);
                    sum += current.red;
                    count++;
                }
            }
        }
    }
    if (count == 0)
        return imageData;
    // we need to adjust the gradient depending on the luminance unless
    // bright colors will appear in white
    int gradientWidth = 512, gradientHeight = 10;
    int average = (int) sum / count;
    int start = average - 32;
    if (start < 0)
        start = 0;
    int end = max + luminance;
    if (end > gradientWidth - 1)
        end = gradientWidth - 1;
    // create the color gradient
    java.awt.Color color1 = new java.awt.Color(color.getRed(), color.getGreen(), color.getBlue());
    java.awt.Color color2 = new java.awt.Color(250, 250, 255);
    BufferedImage gradient = new BufferedImage(gradientWidth, gradientHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = gradient.createGraphics();
    g2.setPaint(color1);
    g2.fill(new java.awt.Rectangle(0, 0, start, gradientHeight));
    g2.setPaint(new GradientPaint(start, 0, color1, end, gradientHeight, color2, false));
    g2.fill(new java.awt.Rectangle(start, 0, end - start, gradientHeight));
    g2.setPaint(color2);
    g2.fill(new java.awt.Rectangle(end, 0, gradientWidth - end, gradientHeight));
    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++) {
            int pixelValue = lineData[x];
            // Do not set transparent pixel
            if (lineData[x] != imageData.transparentPixel) {
                // Get pixel color value if not using direct palette
                if (!palette.isDirect) {
                    pixelValue = palette.getPixel(palette.colors[lineData[x]]);
                }
                RGB current = palette.getRGB(pixelValue);
                if (current.blue == current.green && current.blue == current.red && current.blue < 255) {
                    int gradientRGB = gradient.getRGB(current.red, 0);
                    java.awt.Color gradientColor = new java.awt.Color(gradientRGB);
                    RGB degraded = new RGB(gradientColor.getRed(), gradientColor.getGreen(), gradientColor.getBlue());
                    if (palette.isDirect) {
                        int appliedColor = palette.getPixel(degraded);
                        imageData.setPixel(x, y, appliedColor);
                    } else {
                        palette.colors[lineData[x]] = degraded;
                    }
                }
            }
        }
    }
    return imageData;
}
Also used : Color(org.eclipse.swt.graphics.Color) GradientPaint(java.awt.GradientPaint) RGB(org.eclipse.swt.graphics.RGB) GradientPaint(java.awt.GradientPaint) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) PaletteData(org.eclipse.swt.graphics.PaletteData) ImageData(org.eclipse.swt.graphics.ImageData)

Example 68 with ImageData

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

the class AWT2SWTImageConverter method convertToSWT.

static ImageData convertToSWT(BufferedImage bufferedImage) {
    if (bufferedImage.getColorModel() instanceof DirectColorModel) {
        DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
        PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(), colorModel.getBlueMask());
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette);
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                int rgb = bufferedImage.getRGB(x, y);
                int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF));
                data.setPixel(x, y, pixel);
            }
        }
        return data;
    } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
        IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
        int size = colorModel.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        colorModel.getReds(reds);
        colorModel.getGreens(greens);
        colorModel.getBlues(blues);
        RGB[] rgbs = new RGB[size];
        for (int i = 0; i < rgbs.length; i++) {
            rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
        }
        PaletteData palette = new PaletteData(rgbs);
        ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette);
        data.transparentPixel = colorModel.getTransparentPixel();
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                data.setPixel(x, y, pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) ImageData(org.eclipse.swt.graphics.ImageData) WritableRaster(java.awt.image.WritableRaster) DirectColorModel(java.awt.image.DirectColorModel) RGB(org.eclipse.swt.graphics.RGB) IndexColorModel(java.awt.image.IndexColorModel)

Example 69 with ImageData

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

the class IntensityGraphFigure method drawRGBImage.

/**
 * Calculate the image data from source RGB data array [RGBRGBRGB...].
 *
 * @param dataArray
 *            the source data in RGB mode.
 * @param dataWidth
 *            number of columns of dataArray; This will be the width of image data.
 * @param dataHeight
 *            number of rows of dataArray; This will be the height of image data.
 * @param max
 *            the upper limit of the data in dataArray
 * @param min
 *            the lower limit of the data in dataArray
 * @param imageData
 *            the imageData to be filled. null if a new instance should be created.
 * @param shrink
 *            true if area size of image data is smaller than dataWidth*dataHeight. If this is true, it will use the
 *            nearest neighbor iamge scaling algorithm as described at
 *            http://tech-algorithm.com/articles/nearest-neighbor-image-scaling/.
 * @return the image data. null if dataWidth or dataHeight is less than 1 or larger than the data array.
 */
private ImageData drawRGBImage(IPrimaryArrayWrapper dataArray, int dataWidth, int dataHeight, double max, double min, ImageData imageData, boolean shrink) {
    if (dataWidth < 1 || dataHeight < 1 || dataWidth * dataHeight * 3 > dataArray.getSize() || dataWidth * dataHeight < 0)
        return null;
    if (imageData == null)
        imageData = new ImageData(dataWidth, dataHeight, 24, palette);
    if (shrink) {
        int height = imageData.height;
        int width = imageData.width;
        // EDIT: added +1 to account for an early rounding problem
        int x_ratio = (int) ((dataWidth << 16) / width) + 1;
        int y_ratio = (int) ((dataHeight << 16) / height) + 1;
        // int x_ratio = (int)((w1<<16)/w2) ;
        // int y_ratio = (int)((h1<<16)/h2) ;
        int x2, y2;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                x2 = ((j * x_ratio) >> 16);
                y2 = ((i * y_ratio) >> 16);
                int index = y2 * dataWidth * 3 + x2 * 3;
                int pixel = calcRGBPixel(dataArray, max, min, index);
                imageData.setPixel(j, i, pixel);
            }
        }
    } else {
        for (int y = 0; y < dataHeight; y++) {
            for (int x = 0; x < dataWidth; x++) {
                // the index of the value in the color table array
                int index = y * dataWidth * 3 + x * 3;
                int pixel = calcRGBPixel(dataArray, max, min, index);
                imageData.setPixel(x, y, pixel);
            }
        }
    }
    return imageData;
}
Also used : ImageData(org.eclipse.swt.graphics.ImageData) Point(org.eclipse.draw2d.geometry.Point) PrecisionPoint(org.eclipse.draw2d.geometry.PrecisionPoint)

Example 70 with ImageData

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

the class ZoomRenderer method paintBackground.

public void paintBackground(Graphics g) {
    g.pushState();
    try {
        // This is the clip to the figure. We must clip to this the whole time!
        Rectangle mainClip = getBounds();
        g.setClip(mainClip);
        ImageData scaledImgData = image.getImageData().scaledTo(widgetBounds.width, widgetBounds.height);
        Image scaledImage = new Image(Display.getCurrent(), scaledImgData);
        try {
            g.drawImage(scaledImage, widgetBounds.y, widgetBounds.x);
        } finally {
            scaledImage.dispose();
        }
        // TODO: Is this useless, since the graphics state gets popped?
        //       Also, seems redundant to the setClip call above!
        // Reset the clip to the main clip, the entire size of the frame.
        g.setClip(mainClip);
    } finally {
        g.popState();
    }
}
Also used : ImageData(org.eclipse.swt.graphics.ImageData) Rectangle(org.eclipse.draw2d.geometry.Rectangle) Image(org.eclipse.swt.graphics.Image)

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