How to use Image Filter?
Here, filteredImage, originalImage
is object of BufferedImage please check use of BuffereImage.
public void saturation(float p_saturation) {
ImageProducer produce = new FilteredImageSource(filteredImage
.getSource(), new SaturationFilter(p_saturation / 100));
filteredImage = createBufferedImage(createImage(produce));
// repaint the filteredImage;
}
public void colorTemprature(int p_colorTemprature) {
ImageProducer produce = new FilteredImageSource(originalImage
.getSource(), new ColorFilter(p_colorTemprature));
filteredImage = createBufferedImage(createImage(produce));
// repaint the filteredImage;
}
public void brightness(int p_brightness) {
ImageProducer produce = new FilteredImageSource(originalImage
.getSource(), new BrightnessFilter(p_brightness));
filteredImage = createBufferedImage(createImage(produce));
// repaint the filteredImage;
}
Example of image Filters:
BrightnessFilter.java
package com.joshi.image.filter;
// Brightness Filter Class
// BrightnessFilter.java
import java.awt.image.*;
public class BrightnessFilter extends RGBImageFilter {
int brightness;
public BrightnessFilter(int b) {
brightness = b;
canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb) {
// Get the individual colors
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = (rgb >> 0) & 0xff;
// Calculate the brightness
r += (brightness * r) / 100;
g += (brightness * g) / 100;
b += (brightness * b) / 100;
// Check the boundaries
r = Math.min(Math.max(0, r), 255);
g = Math.min(Math.max(0, g), 255);
b = Math.min(Math.max(0, b), 255);
// Return the result
return (rgb & 0xff000000) | (r << 16) | (g << 8) | (b << 0);
}
}
ColorFilter.java
package com.joshi.image.filter;
/**
* @author SJoshi
*/
// Color Filter Class
// ColorFilter.java
import java.awt.image.*;
public class ColorFilter extends RGBImageFilter {
int red, green, blue;
int colorTemprature;
public ColorFilter(int r, int g, int b) {
red = r;
green = g;
blue = b;
canFilterIndexColorModel = true;
}
public ColorFilter(int p_colorTemprature) {
colorTemprature = p_colorTemprature;
red = p_colorTemprature;
green = p_colorTemprature;
blue = p_colorTemprature;
canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb) {
if (colorTemprature == 0) {
return rgb;
}
// Filter the colors
int r = ((rgb >> 16) & 0xff);
int g = ((rgb >> 8) & 0xff);
int b = ((rgb >> 0) & 0xff);
r = r + colorTemprature;
g = g + colorTemprature;
b = b + colorTemprature;
if (r > 255) {
r = 255 - r;
}
if (g > 255) {
g = 255 - g;
}
if (b > 255) {
b = 255 - b;
}
// Return the result
/*
* if(red==0) { return rgb; }
*/
return (rgb & 0xff000000) | (r << 16) | (g << 8) | (b << 0);
}
}
SaturationFilter.java
package com.joshi.image.filter;
import java.awt.Color;
import java.awt.image.RGBImageFilter;
/**
*
* @author SJoshi
*
*/
public class SaturationFilter extends RGBImageFilter {
/*A private variable used to hold hue/saturation/brightness
values returned from the static conversion methods in Color.
*/
private float hsbvals[] = new float[3];
// the Hue of the indicated new foreground color.
float fgHue;
//the Saturation of the indicated new foreground color.
float fgSaturation;
//the Brightness of the indicated new foreground color.
float fgBrightness;
/*
Construct a HueFilter object which performs color modifications
to warp existing image colors to have a new primary hue.
*/
public SaturationFilter(Color fg) {
Color.RGBtoHSB(fg.getRed(), fg.getGreen(), fg.getBlue(), hsbvals);
fgHue = hsbvals[0];
fgSaturation = hsbvals[1];
fgBrightness = hsbvals[2];
canFilterIndexColorModel = true;
}
public SaturationFilter(float p_saturation) {
fgSaturation = p_saturation;
canFilterIndexColorModel = true;
}
/*
* Filter an individual pixel in the image by modifying its
* hue, saturation, and brightness components to be similar
* to the indicated new foreground color.
*/
@Override
public int filterRGB(int x, int y, int rgb) {
if (fgSaturation == 0) {
return rgb;
}
int alpha = (rgb >> 24) & 0xff;
int red = (rgb >> 16) & 0xff;
int green = (rgb >> 8) & 0xff;
int blue = (rgb) & 0xff;
Color.RGBtoHSB(red, green, blue, hsbvals);
// float newHue = fgHue;
float newHue = hsbvals[0];
float newSaturation = fgSaturation;
float newBrightness = hsbvals[2];// *
// (hsbvals[1] * fgBrightness + (1 - hsbvals[1]));
rgb = Color.HSBtoRGB(newHue, newSaturation, newBrightness);
return (rgb & 0x00ffffff) | (alpha << 24);
}
}