Java BufferedImage Operation highlight(BufferedImage img, Color source, Color dest)

Here you can find the source of highlight(BufferedImage img, Color source, Color dest)

Description

Turn the BufferedImage instance color components from source to destination for highlighting.

License

Open Source License

Parameter

Parameter Description
img a parameter
source a parameter
dest a parameter

Return

a copy of the highlighted img instance

Declaration

public static BufferedImage highlight(BufferedImage img, Color source, Color dest) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.*;
import java.awt.image.*;;

public class Main {
    /**/*from w ww.j  a v  a  2  s  .c  om*/
     * Turn the {@link BufferedImage} instance color components from source to destination for highlighting.
     * Does not modify img.
     * @param img
     * @param source
     * @param dest
     * @return a copy of the highlighted img instance
     */
    public static BufferedImage highlight(BufferedImage img, Color source, Color dest) {
        img = deepCopy(img);
        short[] r = new short[256];
        short[] g = new short[256];
        short[] b = new short[256];
        short[] a = new short[256];
        for (int i = 0; i < 256; i++) {
            r[i] = (short) i;
            g[i] = (short) i;
            b[i] = (short) i;
            a[i] = (short) i;
        }
        r[source.getRed()] = (short) dest.getRed();
        g[source.getGreen()] = (short) dest.getGreen();
        b[source.getBlue()] = (short) dest.getBlue();
        short[][] blueInvert = new short[][] { r, g, b, a };
        BufferedImageOp blueInvertOp = new LookupOp(new ShortLookupTable(0, blueInvert), null);
        Graphics2D gr = img.createGraphics();
        gr.drawImage(img, 0, 0, null);
        gr.dispose();
        blueInvertOp.filter(img, img);
        return img;
    }

    public static BufferedImage deepCopy(BufferedImage bi) {
        ColorModel cm = bi.getColorModel();
        boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
        WritableRaster raster = bi.copyData(null);
        return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
    }
}

Related

  1. generateOutline(BufferedImage source, Color color, boolean alpha)
  2. gradientMask(BufferedImage img, float start, float stop)
  3. greyScale(BufferedImage image)
  4. grid(BufferedImage image, int between)
  5. height(BufferedImage image, Double measurementLatitude, Double measurementLongitude)
  6. hitTest(BufferedImage image, int x, int y)
  7. hueShift(BufferedImage image, int hue)
  8. inColormap(float[][] in, float min, float max, float[][] colormap, BufferedImage b)
  9. indexToDirectColorModel(BufferedImage image)