//////////////////// filter.java ////////////////////
import java.awt.event.*;
import java.awt.*;
import java.awt.image.FilteredImageSource;
import java.awt.image.RGBImageFilter;
class filter
{
public static void main(String args[])
{
new MainWindow(args[0],Double.parseDouble(args[1]));
}
}
class MainWindow extends Frame
{
public MainWindow(String image_name,double rate)
{
// Windowリスナの登録
WindowListener listener = new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
addWindowListener(listener);
setLayout(new BorderLayout());
// イメージの取得
Toolkit toolkit = getToolkit();
Image src = toolkit.getImage(image_name);
// イメージのフィルタリング処理
RGBImageFilter filter;
if(rate >= 0)
filter = new BrighterFilter(Math.abs(rate));
else
filter = new DarkerFilter(Math.abs(rate));
Image image = createImage(new FilteredImageSource(src.getSource(),filter));
// コンポーネントの追加
PaintCanvas canvas = new PaintCanvas(image);
add(canvas,BorderLayout.CENTER);
Label lbl = new Label(image_name,Label.CENTER);
add(lbl,BorderLayout.SOUTH);
// フレームの初期設定
setSize(150,150);
setTitle("Image Filter");
setVisible(true);
}
}
// イメージ表示用キャンバス
class PaintCanvas extends Canvas
{
Image image;
public PaintCanvas(Image image)
{
this.image = image;
}
public void paint(Graphics g)
{
g.drawImage(image,0,0,this);
}
}
////////////////////////////////////////////////////////////
//////////////////// BrighterFilter.java ////////////////////
import java.awt.image.RGBImageFilter;
// 明化処理フィルタ
class BrighterFilter extends RGBImageFilter
{
final int RGB_MAX = 255;
// 明化の度合(大きいほど明るくなる)
double rate;
int hosei;
public BrighterFilter(double rate)
{
// rateのノーマライズ
if(rate > 1.0)
rate = 1.0;
if(rate < 0.0)
rate = 0.0;
this.rate = rate;
hosei = (int)(RGB_MAX * rate);
canFilterIndexColorModel = true;
}
public int filterRGB(int x,int y,int rgb)
{
// 各RGB値の取得
int alpha_value = (rgb & 0xff000000);
int red_value = (rgb & 0x00ff0000) >> 16;
int green_value = (rgb & 0x0000ff00) >> 8;
int blue_value = (rgb & 0x000000ff);
// 明化処理
red_value = (int)(red_value * (1 - rate)) + hosei;
green_value = (int)(green_value * (1 - rate)) + hosei;
blue_value = (int)(blue_value * (1 - rate)) + hosei;
return(alpha_value | (red_value << 16) | (green_value << 8) | blue_value);
}
}
////////////////////////////////////////////////////////////
//////////////////// DarkerFilter.java ////////////////////
import java.awt.image.RGBImageFilter;
// 暗化処理フィルタ
class DarkerFilter extends RGBImageFilter
{
// 暗化の度合(大きいほど暗くなる)
double rate;
public DarkerFilter(double rate)
{
// rateのノーマライズ
if(rate > 1.0)
rate = 1.0;
if(rate < 0.0)
rate = 0.0;
this.rate = rate;
canFilterIndexColorModel = true;
}
public int filterRGB(int x,int y,int rgb)
{
// 各RGB値の取得
int alpha_value = (rgb & 0xff000000);
int red_value = (rgb & 0x00ff0000) >> 16;
int green_value = (rgb & 0x0000ff00) >> 8;
int blue_value = (rgb & 0x000000ff);
// 暗化処理
red_value = (int)(red_value * (1 - rate));
green_value = (int)(green_value * (1 - rate));
blue_value = (int)(blue_value * (1 - rate));
return(alpha_value | (red_value << 16) | (green_value << 8) | blue_value);
}
}
////////////////////////////////////////////////////////////
Source is here. (ZIP Format,21762Byte,Shift-JIS)