Example usage for java.awt.image ColorConvertOp ColorConvertOp

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

Introduction

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

Prototype

public ColorConvertOp(ICC_Profile[] profiles, RenderingHints hints) 

Source Link

Document

Constructs a new ColorConvertOp from an array of ICC_Profiles.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    ColorConvertOp op = new ColorConvertOp(cs, null);

    BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_BYTE_INDEXED);
    bufferedImage = op.filter(bufferedImage, null);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage master = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));
    BufferedImage gray = new BufferedImage(master.getWidth(), master.getHeight(), BufferedImage.TYPE_INT_ARGB);

    ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
    op.filter(master, gray);//  ww w . jav  a  2s . c o m

    // new JLabel(new ImageIcon(master));
    // new JLabel(new ImageIcon(gray));
    ImageIO.write(master, "png", new File("c:/Java_Dev/master.png"));
    ImageIO.write(gray, "png", new File("c:/Java_Dev/gray.png"));
}

From source file:ImageUtil.java

public static BufferedImage desaturate(BufferedImage source) {
    ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
    colorConvert.filter(source, source);

    return source;
}

From source file:ColorConvertDemo.java

public void grayOut() {
    ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
    colorConvert.filter(bi, bi);
}

From source file:Sampler.java

private void createColorOps() {
    mOps.put("Grayscale", new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null));
}

From source file:com.hygenics.imaging.ImageCleanup.java

public byte[] setBlackandWhite(byte[] ibytes) {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    ColorConvertOp op = new ColorConvertOp(cs, null);
    BufferedImage image = op.filter(BufferImage(ibytes), null);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    // convert to jpeg for compression
    try {//from  w ww  .  j a va 2s  . com
        // use apache to read to a buffered image with certain metadata and
        // then convert to a jpg
        ImageIO.write(image, "jpg", bos);
        return bos.toByteArray();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return ibytes;
}

From source file:com.chiorichan.factory.event.PostImageProcessor.java

@EventHandler()
public void onEvent(PostEvalEvent event) {
    try {/* www .  ja va2  s  .c o m*/
        if (event.context().contentType() == null
                || !event.context().contentType().toLowerCase().startsWith("image"))
            return;

        float x = -1;
        float y = -1;

        boolean cacheEnabled = AppConfig.get().getBoolean("advanced.processors.imageProcessorCache", true);
        boolean grayscale = false;

        ScriptingContext context = event.context();
        HttpRequestWrapper request = context.request();
        Map<String, String> rewrite = request.getRewriteMap();

        if (rewrite != null) {
            if (rewrite.get("serverSideOptions") != null) {
                String[] params = rewrite.get("serverSideOptions").trim().split("_");

                for (String p : params)
                    if (p.toLowerCase().startsWith("width") && x < 0)
                        x = Integer.parseInt(p.substring(5));
                    else if ((p.toLowerCase().startsWith("x") || p.toLowerCase().startsWith("w"))
                            && p.length() > 1 && x < 0)
                        x = Integer.parseInt(p.substring(1));
                    else if (p.toLowerCase().startsWith("height") && y < 0)
                        y = Integer.parseInt(p.substring(6));
                    else if ((p.toLowerCase().startsWith("y") || p.toLowerCase().startsWith("h"))
                            && p.length() > 1 && y < 0)
                        y = Integer.parseInt(p.substring(1));
                    else if (p.toLowerCase().equals("thumb")) {
                        x = 150;
                        y = 0;
                        break;
                    } else if (p.toLowerCase().equals("bw") || p.toLowerCase().equals("grayscale"))
                        grayscale = true;
            }

            if (request.getArgument("width") != null && request.getArgument("width").length() > 0)
                x = request.getArgumentInt("width");

            if (request.getArgument("height") != null && request.getArgument("height").length() > 0)
                y = request.getArgumentInt("height");

            if (request.getArgument("w") != null && request.getArgument("w").length() > 0)
                x = request.getArgumentInt("w");

            if (request.getArgument("h") != null && request.getArgument("h").length() > 0)
                y = request.getArgumentInt("h");

            if (request.getArgument("thumb") != null) {
                x = 150;
                y = 0;
            }

            if (request.hasArgument("bw") || request.hasArgument("grayscale"))
                grayscale = true;
        }

        // Tests if our Post Processor can process the current image.
        List<String> readerFormats = Arrays.asList(ImageIO.getReaderFormatNames());
        List<String> writerFormats = Arrays.asList(ImageIO.getWriterFormatNames());
        if (context.contentType() != null
                && !readerFormats.contains(context.contentType().split("/")[1].toLowerCase()))
            return;

        int inx = event.context().buffer().readerIndex();
        BufferedImage img = ImageIO.read(new ByteBufInputStream(event.context().buffer()));
        event.context().buffer().readerIndex(inx);

        if (img == null)
            return;

        float w = img.getWidth();
        float h = img.getHeight();
        float w1 = w;
        float h1 = h;

        if (x < 1 && y < 1) {
            x = w;
            y = h;
        } else if (x > 0 && y < 1) {
            w1 = x;
            h1 = x * (h / w);
        } else if (y > 0 && x < 1) {
            w1 = y * (w / h);
            h1 = y;
        } else if (x > 0 && y > 0) {
            w1 = x;
            h1 = y;
        }

        boolean resize = w1 > 0 && h1 > 0 && w1 != w && h1 != h;
        boolean argb = request.hasArgument("argb") && request.getArgument("argb").length() == 8;

        if (!resize && !argb && !grayscale)
            return;

        // Produce a unique encapsulated id based on this image processing request
        String encapId = SecureFunc.md5(context.filename() + w1 + h1 + request.getArgument("argb") + grayscale);
        File tmp = context.site() == null ? AppConfig.get().getDirectoryCache()
                : context.site().directoryTemp();
        File file = new File(tmp, encapId + "_" + new File(context.filename()).getName());

        if (cacheEnabled && file.exists()) {
            event.context().resetAndWrite(FileUtils.readFileToByteArray(file));
            return;
        }

        Image image = resize ? img.getScaledInstance(Math.round(w1), Math.round(h1),
                AppConfig.get().getBoolean("advanced.processors.useFastGraphics", true) ? Image.SCALE_FAST
                        : Image.SCALE_SMOOTH)
                : img;

        // TODO Report malformed parameters to user

        if (argb) {
            FilteredImageSource filteredSrc = new FilteredImageSource(image.getSource(),
                    new RGBColorFilter((int) Long.parseLong(request.getArgument("argb"), 16)));
            image = Toolkit.getDefaultToolkit().createImage(filteredSrc);
        }

        BufferedImage rtn = new BufferedImage(Math.round(w1), Math.round(h1), img.getType());
        Graphics2D graphics = rtn.createGraphics();
        graphics.drawImage(image, 0, 0, null);
        graphics.dispose();

        if (grayscale) {
            ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
            op.filter(rtn, rtn);
        }

        if (resize)
            Log.get().info(EnumColor.GRAY + "Resized image from " + Math.round(w) + "px by " + Math.round(h)
                    + "px to " + Math.round(w1) + "px by " + Math.round(h1) + "px");

        if (rtn != null) {
            ByteArrayOutputStream bs = new ByteArrayOutputStream();

            if (context.contentType() != null
                    && writerFormats.contains(context.contentType().split("/")[1].toLowerCase()))
                ImageIO.write(rtn, context.contentType().split("/")[1].toLowerCase(), bs);
            else
                ImageIO.write(rtn, "png", bs);

            if (cacheEnabled && !file.exists())
                FileUtils.writeByteArrayToFile(file, bs.toByteArray());

            event.context().resetAndWrite(bs.toByteArray());
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }

    return;
}

From source file:edu.ku.brc.ui.IconManager.java

/**
 * Creates a Black and White image from the color
 * @param img the image to be converted/*from  ww  w  . ja  v a  2s.  c o  m*/
 * @return new B&W image
 */
public static ImageIcon createBWImage(final ImageIcon img) {
    BufferedImage bi = new BufferedImage(img.getIconWidth(), img.getIconHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.createGraphics();
    g.drawImage(img.getImage(), 0, 0, null);
    ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
    colorConvert.filter(bi, bi);
    ImageIcon icon = new ImageIcon(bi);
    g.dispose();
    return icon;
}

From source file:net.cloudkit.relaxation.VerifyImage.java

private BufferedImage grayImage(BufferedImage image) {
    ColorConvertOp filterObj = new ColorConvertOp(ColorSpace.getInstance(1003), (RenderingHints) null);
    filterObj.filter(image, image);/*from   w  ww .j  a v a  2  s  . co m*/
    return image;
}

From source file:org.apache.cocoon.reading.ImageReader.java

public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par)
        throws ProcessingException, SAXException, IOException {

    char lastChar;
    String tmpWidth = par.getParameter("width", "0");
    String tmpHeight = par.getParameter("height", "0");

    this.scaleColor[0] = par.getParameterAsFloat("scaleRed", -1.0f);
    this.scaleColor[1] = par.getParameterAsFloat("scaleGreen", -1.0f);
    this.scaleColor[2] = par.getParameterAsFloat("scaleBlue", -1.0f);
    this.offsetColor[0] = par.getParameterAsFloat("offsetRed", 0.0f);
    this.offsetColor[1] = par.getParameterAsFloat("offsetGreen", 0.0f);
    this.offsetColor[2] = par.getParameterAsFloat("offsetBlue", 0.0f);
    this.quality[0] = par.getParameterAsFloat("quality", 0.9f);

    boolean filterColor = false;
    for (int i = 0; i < 3; ++i) {
        if (this.scaleColor[i] != -1.0f) {
            filterColor = true;/*from   w  w w .j ava 2 s .c o m*/
        } else {
            this.scaleColor[i] = 1.0f;
        }
        if (this.offsetColor[i] != 0.0f) {
            filterColor = true;
        }
    }

    if (filterColor) {
        this.colorFilter = new RescaleOp(scaleColor, offsetColor, null);
    }

    usePercent = false;
    lastChar = tmpWidth.charAt(tmpWidth.length() - 1);
    if (lastChar == '%') {
        usePercent = true;
        width = Integer.parseInt(tmpWidth.substring(0, tmpWidth.length() - 1));
    } else {
        width = Integer.parseInt(tmpWidth);
    }

    lastChar = tmpHeight.charAt(tmpHeight.length() - 1);
    if (lastChar == '%') {
        usePercent = true;
        height = Integer.parseInt(tmpHeight.substring(0, tmpHeight.length() - 1));
    } else {
        height = Integer.parseInt(tmpHeight);
    }

    if (par.getParameterAsBoolean("grayscale", GRAYSCALE_DEFAULT)) {
        this.grayscaleFilter = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
    }

    this.enlarge = par.getParameterAsBoolean("allow-enlarging", ENLARGE_DEFAULT);
    this.fitUniform = par.getParameterAsBoolean("fit-uniform", FIT_DEFAULT);

    super.setup(resolver, objectModel, src, par);
}