Example usage for java.awt.image RGBImageFilter RGBImageFilter

List of usage examples for java.awt.image RGBImageFilter RGBImageFilter

Introduction

In this page you can find the example usage for java.awt.image RGBImageFilter RGBImageFilter.

Prototype

RGBImageFilter

Source Link

Usage

From source file:Transparency.java

public static Image makeColorTransparent(Image im, final Color color) {
    ImageFilter filter = new RGBImageFilter() {
        // the color we are looking for... Alpha bits are set to opaque
        public int markerRGB = color.getRGB() | 0xFF000000;

        @Override// w  ww  .j  a v a2s  .  c o m
        public final int filterRGB(int x, int y, int rgb) {
            if ((rgb | 0xFF000000) == markerRGB) {
                // Mark the alpha bits as zero - transparent
                return 0x00FFFFFF & rgb;
            } else {
                // nothing to do
                return rgb;
            }
        }
    };

    ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}

From source file:ch.fork.AdHocRailway.ui.utils.ImageTools.java

public static Image TransformGrayToTransparency(BufferedImage image) {
    ImageFilter filter = new RGBImageFilter() {
        public final int filterRGB(int x, int y, int rgb) {
            return (rgb << 8) & 0xFF000000;
        }//w w w.ja  v a  2 s  .  com
    };

    ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}

From source file:ch.fork.AdHocRailway.ui.utils.ImageTools.java

public static Image TransformColorToTransparency(BufferedImage image, Color c1, Color c2) {
    // Primitive test, just an example
    final int r1 = c1.getRed();
    final int g1 = c1.getGreen();
    final int b1 = c1.getBlue();
    final int r2 = c2.getRed();
    final int g2 = c2.getGreen();
    final int b2 = c2.getBlue();
    ImageFilter filter = new RGBImageFilter() {
        public final int filterRGB(int x, int y, int rgb) {
            int r = (rgb & 0xFF0000) >> 16;
            int g = (rgb & 0xFF00) >> 8;
            int b = rgb & 0xFF;
            if (r >= r1 && r <= r2 && g >= g1 && g <= g2 && b >= b1 && b <= b2) {
                // Set fully transparent but keep color
                return rgb & 0xFFFFFF;
            }//from w  ww. j av a2 s  .c o  m
            return rgb;
        }
    };

    ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}

From source file:edu.kit.dama.ui.components.TextImage.java

/**
 * Get the bytes of the final image./* ww  w . j a  v a2s . c  o m*/
 *
 * @return The byte array containing the bytes of the resulting image.
 *
 * @throws IOException if creating the image fails.
 */
public byte[] getBytes() throws IOException {
    Image transparentImage = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(
            new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB).getSource(), new RGBImageFilter() {
                @Override
                public final int filterRGB(int x, int y, int rgb) {
                    return (rgb << 8) & 0xFF000000;
                }
            }));

    //create the actual image and overlay it by the transparent background
    BufferedImage outputImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = outputImage.createGraphics();
    g2d.drawImage(transparentImage, 0, 0, null);
    //draw the remaining stuff
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2d.setColor(color);
    g2d.fillRoundRect(0, 0, size, size, 20, 20);
    g2d.setColor(new Color(Math.round((float) color.getRed() * .9f), Math.round((float) color.getGreen() * .9f),
            Math.round((float) color.getBlue() * .9f)));
    g2d.drawRoundRect(0, 0, size - 1, size - 1, 20, 20);

    Font font = new Font("Dialog", Font.BOLD, size - 4);
    g2d.setFont(font);
    g2d.setColor(Color.WHITE);

    String s = text.toUpperCase().substring(0, 1);
    FontMetrics fm = g2d.getFontMetrics();
    float x = ((float) size - (float) fm.stringWidth(s)) / 2f;
    float y = ((float) fm.getAscent()
            + (float) ((float) size - ((float) fm.getAscent() + (float) fm.getDescent())) / 2f) - 1f;
    g2d.drawString(s, x, y);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ImageIO.write(outputImage, "png", bout);
    g2d.dispose();
    return bout.toByteArray();
}

From source file:edu.stanford.epad.epadws.handlers.dicom.DSOUtil.java

private static Image makeAnyColorWhite(BufferedImage im) {
    nonBlank.set(false);//from ww  w. j a  v a2s . c  o  m
    ImageFilter filter = new RGBImageFilter() {

        @Override
        public final int filterRGB(int x, int y, int rgb) {
            if ((rgb & 0x00FFFFFF) != 0) {
                nonBlank.set(true);
                return 0xFFFFFFFF;
            } else {
                return rgb;
            }
        }
    };

    ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}

From source file:edu.stanford.epad.epadws.handlers.dicom.DSOUtil.java

private static Image makeColorOpaque(BufferedImage im, final Color color) {
    nonBlank.set(false);/*from   ww w  . java 2  s .c om*/
    ImageFilter filter = new RGBImageFilter() {
        public int markerRGB = color.getRGB() | 0xFF000000;

        @Override
        public final int filterRGB(int x, int y, int rgb) {
            if ((rgb & 0x00FFFFFF) != 0) {
                nonBlank.set(true);
            }
            if ((rgb | 0xFF000000) == markerRGB) {
                //               nonBlank.set(true);
                return 0xFF000000 | rgb;
            } else {
                return rgb;
            }
        }
    };

    ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}

From source file:edu.stanford.epad.epadws.handlers.dicom.DSOUtil.java

private static Image makeColorTransparent(BufferedImage im, final Color color) {
    ImageFilter filter = new RGBImageFilter() {
        public int markerRGB = color.getRGB() | 0xFF000000;

        @Override/*from   w  w w  . j  ava 2 s. c o m*/
        public final int filterRGB(int x, int y, int rgb) {
            if ((rgb | 0xFF000000) == markerRGB) {
                return 0x00FFFFFF & rgb;
            } else {
                return rgb;
            }
        }
    };

    ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
    return Toolkit.getDefaultToolkit().createImage(ip);
}

From source file:org.polymap.core.data.image.ImageTransparencyProcessor.java

public static BufferedImage transparency(Image image, final Color markerColor) throws IOException {
    long start = System.currentTimeMillis();

    RGBImageFilter filter = new RGBImageFilter() {
        // the color we are looking for... Alpha bits are set to opaque
        public int markerRGB = markerColor.getRGB() | 0xFF000000;

        byte threshold = 25;

        double range = ((double) 0xFF) / (3 * threshold);

        public final int filterRGB(int x, int y, int rgb) {
            Color probe = new Color(rgb);
            //log.info( "probe=" + probe + ", marker=" + markerColor );

            // delta values
            int dRed = markerColor.getRed() - probe.getRed();
            int dGreen = markerColor.getGreen() - probe.getGreen();
            int dBlue = markerColor.getBlue() - probe.getBlue();
            //log.info( "    dRed=" + dRed + ", dGreen=" + dGreen );

            if (dRed >= 0 && dRed < threshold && dGreen >= 0 && dGreen < threshold && dBlue >= 0
                    && dBlue < threshold) {
                int alpha = (int) Math.round(range * (dRed + dGreen + dBlue));
                //log.info( "    -> alpha=" + alpha );

                return ((alpha << 24) | 0x00FFFFFF) & rgb;
            } else {
                // nothing to do
                return rgb;
            }/*from w w  w . ja v a 2 s .c om*/
        }
    };

    //        BufferedImage bimage = null;
    //        if (image instanceof BufferedImage) {
    //            bimage = (BufferedImage)image;
    //        }
    //        else {
    //            bimage = new BufferedImage(
    //                    image.getHeight( null ), image.getWidth( null ), BufferedImage.TYPE_INT_ARGB );
    //            Graphics g = bimage.getGraphics();
    //            g.drawImage( image, 0, 0, null );
    //            g.dispose();
    //        }

    ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
    Image result = Toolkit.getDefaultToolkit().createImage(ip);

    BufferedImage bresult = new BufferedImage(image.getHeight(null), image.getWidth(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics g = bresult.getGraphics();
    g.drawImage(result, 0, 0, null);
    g.dispose();

    //        // XXX this can surely be done any more clever
    //        int width = bimage.getWidth();
    //        int height = bimage.getHeight();
    //        for (int x=bimage.getMinX(); x<width; x++) {
    //            for (int y=bimage.getMinY(); y<height; y++) {
    //                int filtered = filter.filterRGB( x, y, bimage.getRGB( x, y ) );
    //                result.setRGB( x, y, filtered );
    //            }
    //        }

    log.debug("Transparency done. (" + (System.currentTimeMillis() - start) + "ms)");
    return bresult;
}

From source file:org.polymap.rhei.batik.engine.svg.Svg2Png.java

public static BufferedImage makeColorTransparent(BufferedImage im, final Color color) {
    ImageFilter filter = new RGBImageFilter() {

        private int shift = 0xFF000000;

        public int rgbToMakeTransparent = color.getRGB() | shift;

        public final int filterRGB(int x, int y, int rgb) {
            if ((rgb | shift) == rgbToMakeTransparent) {
                return 0x00FFFFFF & rgb;
            }/*w  ww  .j a v a  2s  .co m*/
            return rgb;
        }
    };
    Image newImage = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(im.getSource(), filter));
    BufferedImage bufferedImage = new BufferedImage(newImage.getWidth(null), newImage.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bufferedImage.createGraphics();
    g2.drawImage(newImage, 0, 0, null);
    g2.dispose();
    return bufferedImage;
}

From source file:util.ui.PictureAreaIcon.java

public void paintIcon(final Component c, Graphics g, int x, int y) {
    if (mScaledIcon == null) {
        return;/*from   w w w  . jav a2  s .  c  om*/
    }

    y += 2;

    Color color = g.getColor();

    if (!colorsInEqualRange(c.getBackground(), c.getForeground()) && !mProgram.isExpired()) {
        g.setColor(c.getBackground());
        g.fillRect(x, y, getIconWidth(), getIconHeight() - 2);
    }

    g.setColor(color);
    g.drawRect(x, y, getIconWidth() - 1, getIconHeight() - 3);

    y += 3;
    x += 3;

    if (mIsGrayFilter && !mIsExpired && mProgram.isExpired()) {
        ImageFilter filter = new GrayFilter(true, 60);
        mScaledIcon
                .setImage(c.createImage(new FilteredImageSource(mScaledIcon.getImage().getSource(), filter)));
        mIsExpired = true;
    }

    if (c.getForeground().getAlpha() != 255) {
        ImageFilter filter = new RGBImageFilter() {
            public int filterRGB(int x, int y, int rgb) {
                if ((rgb & 0xff000000) != 0) {
                    return (rgb & 0xffffff) | (c.getForeground().getAlpha() << 24);
                }
                return rgb;
            }
        };

        mScaledIcon
                .setImage(c.createImage(new FilteredImageSource(mScaledIcon.getImage().getSource(), filter)));
    }

    mScaledIcon.paintIcon(c, g, x, y);

    /*
    if(!mProgram.isExpired()) {
      g.setColor(color);
    } else {
      g.setColor(color);
    }
    */
    mCopyrightText.paintIcon(null, g, x, y + mScaledIcon.getIconHeight());
    if (mDescriptionLines > 0 && mDescriptionText != null) {
        mDescriptionText.paintIcon(null, g, x,
                y + mScaledIcon.getIconHeight() + mCopyrightText.getIconHeight() + 1);
    }
    g.setColor(color);
}