Image Color Operation: dark, bright, contrast, negative : Image Filter « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » Image FilterScreenshots 
Image Color Operation: dark, bright, contrast, negative
Image Color Operation: dark, bright, contrast, negative
  
/*
 * Apollo - Motion capture and animation system
 * Copyright (c) 2005 Apollo
 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @author Giovane.Kuhn - brain@netuno.com.br
 *
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ByteLookupTable;
import java.awt.image.LookupOp;
import java.awt.image.LookupTable;
import java.awt.image.ShortLookupTable;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

public class ColorApp extends JFrame {
    DisplayPanel displayPanel;

    JButton brightenButton, darkenButton, contrastIncButton, contrastDecButton, reverseButton, resetButton;

    public ColorApp() {
        super();
        Container container = getContentPane();

        displayPanel = new DisplayPanel();
        container.add(displayPanel);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(32));
        panel.setBorder(new TitledBorder("Click a Button to Perform the Associated Operation and Reset..."));

        brightenButton = new JButton("Brightness >>");
        brightenButton.addActionListener(new ButtonListener());
        darkenButton = new JButton("Darkness >>");
        darkenButton.addActionListener(new ButtonListener());
        contrastIncButton = new JButton("Contrast >>");
        contrastIncButton.addActionListener(new ButtonListener());
        contrastDecButton = new JButton("Contrast <<");
        contrastDecButton.addActionListener(new ButtonListener());
        reverseButton = new JButton("Negative");
        reverseButton.addActionListener(new ButtonListener());
        resetButton = new JButton("Reset");
        resetButton.addActionListener(new ButtonListener());

        panel.add(brightenButton);
        panel.add(darkenButton);
        panel.add(contrastIncButton);
        panel.add(contrastDecButton);
        panel.add(reverseButton);
        panel.add(resetButton);

        container.add(BorderLayout.SOUTH, panel);

        addWindowListener(new WindowEventHandler());
        setSize(displayPanel.getWidth(), displayPanel.getHeight() 25);
        show();
    }

    class WindowEventHandler extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    }

    public static void main(String arg[]) {
        new ColorApp();
    }

    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JButton button = (JButtone.getSource();

            if (button.equals(brightenButton)) {
                displayPanel.brightenLUT();
                displayPanel.applyFilter();
                displayPanel.repaint();
            else if (button.equals(darkenButton)) {
                displayPanel.darkenLUT();
                displayPanel.applyFilter();
                displayPanel.repaint();
            else if (button.equals(contrastIncButton)) {
                displayPanel.contrastIncLUT();
                displayPanel.applyFilter();
                displayPanel.repaint();
            else if (button.equals(contrastDecButton)) {
                displayPanel.contrastDecLUT();
                displayPanel.applyFilter();
                displayPanel.repaint();
            else if (button.equals(reverseButton)) {
                displayPanel.reverseLUT();
                displayPanel.applyFilter();
                displayPanel.repaint();
            else if (button.equals(resetButton)) {
                displayPanel.reset();
                displayPanel.repaint();
            }
        }
    }
}

class DisplayPanel extends JPanel {
    Image displayImage;

    BufferedImage bi;

    Graphics2D big;

    LookupTable lookupTable;

    DisplayPanel() {
        setBackground(Color.black)// panel background color
        loadImage();
        setSize(displayImage.getWidth(this), displayImage.getWidth(this))// panel
        createBufferedImage();
    }

    public void loadImage() {
        displayImage = Toolkit.getDefaultToolkit().getImage("a.jpg");
        MediaTracker mt = new MediaTracker(this);
        mt.addImage(displayImage, 1);
        try {
            mt.waitForAll();
        catch (Exception e) {
            System.out.println("Exception while loading.");
        }

        if (displayImage.getWidth(this== -1) {
            System.out.println("No jpg file");
            System.exit(0);
        }
    }

    public void createBufferedImage() {
        bi = new BufferedImage(displayImage.getWidth(this), displayImage.getHeight(this), BufferedImage.TYPE_INT_ARGB);

        big = bi.createGraphics();
        big.drawImage(displayImage, 00this);
    }

    public void brightenLUT() {
        short brighten[] new short[256];
        for (int i = 0; i < 256; i++) {
            short pixelValue = (short) (i + 10);
            if (pixelValue > 255)
                pixelValue = 255;
            else if (pixelValue < 0)
                pixelValue = 0;
            brighten[i= pixelValue;
        }
        lookupTable = new ShortLookupTable(0, brighten);
    }

    public void darkenLUT() {
        short brighten[] new short[256];
        for (int i = 0; i < 256; i++) {
            short pixelValue = (short) (i - 10);
            if (pixelValue > 255)
                pixelValue = 255;
            else if (pixelValue < 0)
                pixelValue = 0;
            brighten[i= pixelValue;
        }
        lookupTable = new ShortLookupTable(0, brighten);
    }

    public void contrastIncLUT() {
        short brighten[] new short[256];
        for (int i = 0; i < 256; i++) {
            short pixelValue = (short) (i * 1.2);
            if (pixelValue > 255)
                pixelValue = 255;
            else if (pixelValue < 0)
                pixelValue = 0;
            brighten[i= pixelValue;
        }
        lookupTable = new ShortLookupTable(0, brighten);
    }

    public void contrastDecLUT() {
        short brighten[] new short[256];
        for (int i = 0; i < 256; i++) {
            short pixelValue = (short) (i / 1.2);
            if (pixelValue > 255)
                pixelValue = 255;
            else if (pixelValue < 0)
                pixelValue = 0;
            brighten[i= pixelValue;
        }
        lookupTable = new ShortLookupTable(0, brighten);
    }

    public void reverseLUT() {
        byte reverse[] new byte[256];
        for (int i = 0; i < 256; i++) {
            reverse[i(byte) (255 - i);
        }
        lookupTable = new ByteLookupTable(0, reverse);
    }

    public void reset() {
        big.setColor(Color.black);
        big.clearRect(00, bi.getWidth(this), bi.getHeight(this));
        big.drawImage(displayImage, 00this);
    }

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

    public void update(Graphics g) {
        g.clearRect(00, getWidth(), getHeight());
        paintComponent(g);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2D = (Graphics2Dg;
        g2D.drawImage(bi, 00this);
    }
}

   
    
  
Related examples in the same category
1.Image Filter
2.ImageComparator compares a byte[] for equality by creating 2 hashes for the bytearray and comparing thoose hashes.
3.Gray scale image operationGray scale image operation
4.Dithering a 24-bit RGB image to a monochrome (1-bit or bilevel) image
5.If the image has transparent pixels
6.Copy Raster
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.