Example usage for java.awt.image LookupOp LookupOp

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

Introduction

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

Prototype

public LookupOp(LookupTable lookup, RenderingHints hints) 

Source Link

Document

Constructs a LookupOp object given the lookup table and a RenderingHints object, which might be null .

Usage

From source file:MainClass.java

public void paint(Graphics g) {
    short[] invert = new short[256];

    for (int i = 0; i < invert.length; i++)
        invert[i] = (short) (255 - i);

    BufferedImageOp invertOp = new LookupOp(new ShortLookupTable(0, invert), null);

    BufferedImage clone = invertOp.filter(createImage(), null);

    g.drawImage(clone, 20, 20, this);
}

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

public void convBW(BufferedImage image) {
    byte[] threshold = new byte[256];

    for (int thresholdOp = 0; thresholdOp < 256; ++thresholdOp) {
        threshold[thresholdOp] = (byte) (thresholdOp < 128 ? 0 : -1);
    }//w  ww . j  a  va  2s  . c  o  m

    LookupOp var4 = new LookupOp(new ByteLookupTable(0, threshold), (RenderingHints) null);
    var4.filter(image, image);
}

From source file:Sampler.java

private void createLookups() {
    short[] brighten = new short[256];
    short[] betterBrighten = new short[256];
    short[] posterize = new short[256];
    short[] invert = new short[256];
    short[] straight = new short[256];
    short[] zero = new short[256];
    for (int i = 0; i < 256; i++) {
        brighten[i] = (short) (128 + i / 2);
        betterBrighten[i] = (short) (Math.sqrt((double) i / 255.0) * 255.0);
        posterize[i] = (short) (i - (i % 32));
        invert[i] = (short) (255 - i);
        straight[i] = (short) i;
        zero[i] = (short) 0;
    }//from   w  ww.  j  ava  2  s .com
    mOps.put("Brighten", new LookupOp(new ShortLookupTable(0, brighten), null));
    mOps.put("Better Brighten", new LookupOp(new ShortLookupTable(0, betterBrighten), null));
    mOps.put("Posterize", new LookupOp(new ShortLookupTable(0, posterize), null));
    mOps.put("Invert", new LookupOp(new ShortLookupTable(0, invert), null));

    short[][] redOnly = { invert, straight, straight };
    short[][] greenOnly = { straight, invert, straight };
    short[][] blueOnly = { straight, straight, invert };
    mOps.put("Red invert", new LookupOp(new ShortLookupTable(0, redOnly), null));
    mOps.put("Green invert", new LookupOp(new ShortLookupTable(0, greenOnly), null));
    mOps.put("Blue invert", new LookupOp(new ShortLookupTable(0, blueOnly), null));

    short[][] redRemove = { zero, straight, straight };
    short[][] greenRemove = { straight, zero, straight };
    short[][] blueRemove = { straight, straight, zero };
    mOps.put("Red remove", new LookupOp(new ShortLookupTable(0, redRemove), null));
    mOps.put("Green remove", new LookupOp(new ShortLookupTable(0, greenRemove), null));
    mOps.put("Blue remove", new LookupOp(new ShortLookupTable(0, blueRemove), null));
}

From source file:SaveImage.java

public void filterImage() {
    BufferedImageOp op = null;//from  w w w.ja va2 s . c  om

    if (opIndex == lastOp) {
        return;
    }
    lastOp = opIndex;
    switch (opIndex) {

    case 0:
        biFiltered = bi; /* original */
        return;
    case 1: /* low pass filter */
    case 2: /* sharpen */
        float[] data = (opIndex == 1) ? BLUR3x3 : SHARPEN3x3;
        op = new ConvolveOp(new Kernel(3, 3, data), ConvolveOp.EDGE_NO_OP, null);

        break;

    case 3: /* lookup */
        byte lut[] = new byte[256];
        for (int j = 0; j < 256; j++) {
            lut[j] = (byte) (256 - j);
        }
        ByteLookupTable blut = new ByteLookupTable(0, lut);
        op = new LookupOp(blut, null);
        break;
    }

    /*
     * Rather than directly drawing the filtered image to the destination,
     * filter it into a new image first, then that filtered image is ready for
     * writing out or painting.
     */
    biFiltered = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    op.filter(bi, biFiltered);
}

From source file:ImageOps.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    int w = getSize().width;
    int h = getSize().height;

    g2.setColor(Color.black);/* w  ww . j  av a  2 s. co m*/
    float[][] data = { { 0.1f, 0.1f, 0.1f, // low-pass filter
            0.1f, 0.2f, 0.1f, 0.1f, 0.1f, 0.1f }, SHARPEN3x3_3 };

    String theDesc[] = { "Convolve LowPass", "Convolve Sharpen", "LookupOp", "RescaleOp" };
    for (int i = 0; i < bi.length; i++) {
        int iw = bi[i].getWidth(this);
        int ih = bi[i].getHeight(this);
        int x = 0, y = 0;

        AffineTransform at = new AffineTransform();
        at.scale((w - 14) / 2.0 / iw, (h - 34) / 2.0 / ih);

        BufferedImageOp biop = null;
        BufferedImage bimg = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);

        switch (i) {
        case 0:
        case 1:
            x = i == 0 ? 5 : w / 2 + 3;
            y = 15;
            Kernel kernel = new Kernel(3, 3, data[i]);
            ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
            cop.filter(bi[i], bimg);
            biop = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
            break;
        case 2:
            x = 5;
            y = h / 2 + 15;
            byte chlut[] = new byte[256];
            for (int j = 0; j < 200; j++)
                chlut[j] = (byte) (256 - j);
            ByteLookupTable blut = new ByteLookupTable(0, chlut);
            LookupOp lop = new LookupOp(blut, null);
            lop.filter(bi[i], bimg);
            biop = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
            break;
        case 3:
            x = w / 2 + 3;
            y = h / 2 + 15;
            RescaleOp rop = new RescaleOp(1.1f, 20.0f, null);
            rop.filter(bi[i], bimg);
            biop = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        }
        g2.drawImage(bimg, biop, x, y);
        TextLayout tl = new TextLayout(theDesc[i], g2.getFont(), g2.getFontRenderContext());
        tl.draw(g2, (float) x, (float) y - 4);
    }
}

From source file:ImageProcessingTest.java

public ImageProcessingFrame() {
    setTitle("ImageProcessingTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    add(new JComponent() {
        public void paintComponent(Graphics g) {
            if (image != null)
                g.drawImage(image, 0, 0, null);
        }//  w w w  . j av  a2  s .  c om
    });

    JMenu fileMenu = new JMenu("File");
    JMenuItem openItem = new JMenuItem("Open");
    openItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            openFile();
        }
    });
    fileMenu.add(openItem);

    JMenuItem exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });
    fileMenu.add(exitItem);

    JMenu editMenu = new JMenu("Edit");
    JMenuItem blurItem = new JMenuItem("Blur");
    blurItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            float weight = 1.0f / 9.0f;
            float[] elements = new float[9];
            for (int i = 0; i < 9; i++)
                elements[i] = weight;
            convolve(elements);
        }
    });
    editMenu.add(blurItem);

    JMenuItem sharpenItem = new JMenuItem("Sharpen");
    sharpenItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            float[] elements = { 0.0f, -1.0f, 0.0f, -1.0f, 5.f, -1.0f, 0.0f, -1.0f, 0.0f };
            convolve(elements);
        }
    });
    editMenu.add(sharpenItem);

    JMenuItem brightenItem = new JMenuItem("Brighten");
    brightenItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            float a = 1.1f;
            // float b = 20.0f;
            float b = 0;
            RescaleOp op = new RescaleOp(a, b, null);
            filter(op);
        }
    });
    editMenu.add(brightenItem);

    JMenuItem edgeDetectItem = new JMenuItem("Edge detect");
    edgeDetectItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            float[] elements = { 0.0f, -1.0f, 0.0f, -1.0f, 4.f, -1.0f, 0.0f, -1.0f, 0.0f };
            convolve(elements);
        }
    });
    editMenu.add(edgeDetectItem);

    JMenuItem negativeItem = new JMenuItem("Negative");
    negativeItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            short[] negative = new short[256 * 1];
            for (int i = 0; i < 256; i++)
                negative[i] = (short) (255 - i);
            ShortLookupTable table = new ShortLookupTable(0, negative);
            LookupOp op = new LookupOp(table, null);
            filter(op);
        }
    });
    editMenu.add(negativeItem);

    JMenuItem rotateItem = new JMenuItem("Rotate");
    rotateItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            if (image == null)
                return;
            AffineTransform transform = AffineTransform.getRotateInstance(Math.toRadians(5),
                    image.getWidth() / 2, image.getHeight() / 2);
            AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
            filter(op);
        }
    });
    editMenu.add(rotateItem);

    JMenuBar menuBar = new JMenuBar();
    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    setJMenuBar(menuBar);
}

From source file:Java2DExample.java

public BufferedImage processImage(BufferedImage image) {
    byte[] invertArray = new byte[256];

    for (int counter = 0; counter < 256; counter++)
        invertArray[counter] = (byte) (255 - counter);

    BufferedImageOp invertFilter = new LookupOp(new ByteLookupTable(0, invertArray), null);
    return invertFilter.filter(image, null);

}

From source file:ImageDrawingComponent.java

public void paint(Graphics g) {

    Graphics2D g2 = (Graphics2D) g;

    switch (opIndex) {
    case 0: /* copy */
        g.drawImage(bi, 0, 0, null);/* w ww.j ava2  s .co  m*/
        break;

    case 1: /* scale up using coordinates */
        g.drawImage(bi, 0, 0, w, h, /* dst rectangle */
                0, 0, w / 2, h / 2, /* src area of image */
                null);
        break;

    case 2: /* scale down using transform */
        g2.drawImage(bi, AffineTransform.getScaleInstance(0.7, 0.7), null);
        break;

    case 3: /* scale up using transform Op and BICUBIC interpolation */
        AffineTransform at = AffineTransform.getScaleInstance(1.5, 1.5);
        AffineTransformOp aop = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
        g2.drawImage(bi, aop, 0, 0);
        break;

    case 4: /* low pass filter */
    case 5: /* sharpen */
        float[] data = (opIndex == 4) ? BLUR3x3 : SHARPEN3x3;
        ConvolveOp cop = new ConvolveOp(new Kernel(3, 3, data), ConvolveOp.EDGE_NO_OP, null);
        g2.drawImage(bi, cop, 0, 0);
        break;

    case 6: /* rescale */
        RescaleOp rop = new RescaleOp(1.1f, 20.0f, null);
        g2.drawImage(bi, rop, 0, 0);
        break;

    case 7: /* lookup */
        byte lut[] = new byte[256];
        for (int j = 0; j < 256; j++) {
            lut[j] = (byte) (256 - j);
        }
        ByteLookupTable blut = new ByteLookupTable(0, lut);
        LookupOp lop = new LookupOp(blut, null);
        g2.drawImage(bi, lop, 0, 0);
        break;

    default:
    }
}

From source file:ColorApp.java

public void applyFilter() {
    LookupOp lop = new LookupOp(lookupTable, null);
    lop.filter(bi, bi);
}

From source file:org.apache.fop.render.pcl.PCLGenerator.java

private RenderedImage getMask(RenderedImage img, Dimension targetDim) {
    ColorModel cm = img.getColorModel();
    if (cm.hasAlpha()) {
        BufferedImage alpha = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        Raster raster = img.getData();
        GraphicsUtil.copyBand(raster, cm.getNumColorComponents(), alpha.getRaster(), 0);

        BufferedImageOp op1 = new LookupOp(new ByteLookupTable(0, THRESHOLD_TABLE), null);
        BufferedImage alphat = op1.filter(alpha, null);

        BufferedImage mask;//from   w ww.j av a  2 s. com
        if (true) {
            mask = new BufferedImage(targetDim.width, targetDim.height, BufferedImage.TYPE_BYTE_BINARY);
        } else {
            byte[] arr = { (byte) 0, (byte) 0xff };
            ColorModel colorModel = new IndexColorModel(1, 2, arr, arr, arr);
            WritableRaster wraster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, targetDim.width,
                    targetDim.height, 1, 1, null);
            mask = new BufferedImage(colorModel, wraster, false, null);
        }

        Graphics2D g2d = mask.createGraphics();
        try {
            AffineTransform at = new AffineTransform();
            double sx = targetDim.getWidth() / img.getWidth();
            double sy = targetDim.getHeight() / img.getHeight();
            at.scale(sx, sy);
            g2d.drawRenderedImage(alphat, at);
        } finally {
            g2d.dispose();
        }
        /*
        try {
        BatchDiffer.saveAsPNG(alpha, new java.io.File("D:/out-alpha.png"));
        BatchDiffer.saveAsPNG(mask, new java.io.File("D:/out-mask.png"));
        } catch (IOException e) {
        e.printStackTrace();
        }*/
        return mask;
    } else {
        return null;
    }
}