1 色彩调节
package demo2.coloradjust;
import demo2.utils.AbstractImageOptionFilter;
import java.awt.image.BufferedImage;
public class ColorAdjustFilter extends AbstractImageOptionFilter {
private float minValue;
private float maxValue;
private float defaultMinValue;
private float defaultMaxValue;
private float bvalue;
private int[] lut;
private boolean minmax;
public ColorAdjustFilter() {
defaultMinValue = 0;
defaultMaxValue = 255;
minValue = 0;
maxValue = 255;
bvalue = 20;
minmax = true;
}
public void setBvalue(float bvalue) {
this.bvalue = bvalue;
}
public void setMinmax(boolean minmax) {
this.minmax = minmax;
}
public void setMinValue(float minValue) {
this.minValue = minValue;
}
public void setMaxValue(float maxValue) {
this.maxValue = maxValue;
}
@Override
public BufferedImage process(BufferedImage src) {
int width = src.getWidth();
int height = src.getHeight();
int[] pixels = new int[width * height];
getRGB(src, 0, 0, width, height, pixels);
int index = 0;
if(minmax) {
setupMinMaxLut();
}
else {
setupBrightnessLut();
}
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
index = row * width + col;
int pixel = pixels[index];
int ta = (pixel >> 24) & 0xff;
int tr = (pixel >> 16) & 0xff;
int tg = (pixel >> 8) & 0xff;
int tb = (pixel) & 0xff;
pixels[index] = (ta << 24 | lut[tr] << 16 | lut[tg] << 8 | lut[tb]);
}
}
setRGB(src, 0, 0, width, height, pixels);
return src;
}
public void setupMinMaxLut() {
float min = defaultMinValue + minValue*(defaultMaxValue-defaultMinValue)/255;
float max = defaultMinValue + maxValue*(defaultMaxValue-defaultMinValue)/255;
System.out.println("min : " + min + ", max : " + max);
lut = new int[256];
for(int i=0; i<256; i++) {
float v1 = (i - min);
float delta = max - min;
int v = (int)((v1 / delta) * 256);
if(v < 0) {
v = 0;
}
if(v > 255) {
v = 255;
}
lut[i] = v;
}
}
public void setupBrightnessLut() {
float center = defaultMinValue + (defaultMaxValue-defaultMinValue)*((256-bvalue)/256);
float min = defaultMinValue + minValue*(defaultMaxValue-defaultMinValue)/255;
float max = defaultMinValue + maxValue*(defaultMaxValue-defaultMinValue)/255;
float delta= max-min;
min = center - delta/2.0f;
max = center + delta/2.0f;
System.out.println("min : " + min + ", max : " + max);
lut = new int[256];
for(int i=0; i<256; i++) {
float v1 = (i - min);
float delta2 = max - min;
int v = (int)((v1 / delta2) * 256);
if(v < 0) {
v = 0;
}
if(v > 255) {
v = 255;
}
lut[i] = v;
}
}
}
package demo2.coloradjust;
import demo2.rgbspace.WhiteImageFilter;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ImagePanel extends JComponent implements ActionListener {
private BufferedImage image;
private JButton processBtn;
public ImagePanel(BufferedImage image) {
this.image = image;
}
public JButton getButton() {
processBtn = new JButton("按钮");
processBtn.addActionListener(this);
return processBtn;
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
if (null != image) {
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
}
}
public void process() {
ColorAdjustFilter tool = new ColorAdjustFilter();
tool.setMinValue(20);
tool.process(image);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == processBtn) {
this.process();
this.repaint();
}
}
public static void main(String[] args) {
File file = new File("resource/color.png");
try {
BufferedImage image = ImageIO.read(file);
ImagePanel imp = new ImagePanel(image);
JFrame frame = new JFrame();
frame.getContentPane().add(imp, BorderLayout.CENTER);
frame.getContentPane().add(imp.getButton(), BorderLayout.SOUTH);
frame.setSize(600, 600);
frame.setTitle("图像显示测试");
frame.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
2 亮度调整