图像像素基本操作——自然系列滤镜

主要代码如下:
package chapter5;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**

  • Created by LENOVO on 18-1-30.
    */
    public class LvJingFilter extends AbstractBufferedImageOp {
    int[] fogLookUp = new int[257];//雾化风格颜色查找表
    int[] rainbowLookup ;
    public LvJingFilter(){

    }
    public BufferedImage filter(BufferedImage src,BufferedImage dest){
    int width = src.getWidth();
    int height = src.getHeight();
    if(dest == null){
    dest = creatCompatibleDestImage(src,null);
    }
    int inpixels[] = new int[widthheight];
    int outpixels[] = new int[width
    height];
    getRGB(src,0,0,width,height,inpixels);
    int index = 0;
    for(int row=0;row<height;row++){
    int ta = 0,tr = 0,tg = 0,tb = 0;
    for(int col=0;col<width;col++){
    index = rowwidth+col;
    ta = (inpixels[index] >> 24) & 0xff;
    tr = (inpixels[index] >> 16) & 0xff;
    tg = (inpixels[index] >> 8) & 0xff;
    tb = (inpixels[index]);
    /
    空气风格
    tr = (tg+tb)/2;
    tg = (tr+tb)/2;
    tb = (tr+tg)/2;
    /
    /
    燃情风格
    int gray = (tr+tg+tb)/3;
    tr = clamp(gray3);
    tg = gray;
    tb = gray/3;
    /
    /
    冰冻风格
    tr = clamp((int) Math.abs((tr-tg-tb)
    1.5));
    tg = clamp((int) Math.abs((tg-tb-tr)1.5));
    tb = clamp((int) (Math.abs(tb-tr-tg)
    1.5));
    /
    /
    熔岩风格
    int gray = (tr+tg+tb)/3;
    tr = gray;
    tg = Math.abs(tb-128);
    tb = Math.abs(tb-128);
    /
    /
    金属风格
    float r = Math.abs(tr-64);
    float g = Math.abs(r-64);
    float b = Math.abs(g-64);
    float gray = ((222r + 707g + 71b)/1000);
    r = gray +70;
    r = r+(((r-128)
    100)/100f);
    g = gray+65;
    g = g+(((g-128)100)/100f);
    b = gray +75;
    b = b+(((b-128)
    100)/100f);
    tr = clamp((int) r);
    tg = clamp((int) g);
    tb = clamp((int) b);
    /
    /
    海洋风格
    int gray = (tr+tg +tb)/3;
    tr = clamp(gray/3);
    tg = gray;
    tb = clamp(gray*3);
    /
    /
    湖水风格
    int gray = (tr+tg+tb)/3;
    tr = clamp(gray-tg-tb);
    tg = clamp(gray-tr-tb);
    tb = clamp(gray-tr-tg);
    /
    /
    雾化风格
    buildFogLookupTable();
    tr = fogLookUp[clamp(tr)];
    tg = fogLookUp[clamp(tg)];
    tb = fogLookUp[clamp(tb)];
    */
    //基于特定图像给出的颜色作为标准来生成颜色查找表——本例为彩虹
    buildRainBowLookupTable();
    tr = rainbowLookup[clamp(tr)];
    tg = rainbowLookup[clamp(tg)];
    tb = rainbowLookup[clamp(tb)];
    outpixels[index] = (ta <<24 ) | (clamp(tr) <<16) | (clamp(tg) << 8) | clamp(tb);
    }
    }
    setRGB(dest,0,0,width,height,outpixels);

     return dest;
    

    }
    //雾风格颜色查找表//40,41,42,.....127,127,127,....127,128,129,...,255
    public void buildFogLookupTable(){
    //颜色查找表
    int fogLimit = 40;
    for(int i=0; i<fogLookUp.length;i++){
    if(i>127){
    fogLookUp[i] = i-fogLimit;
    if(fogLookUp[i]<127){
    fogLookUp[i] = 127;
    }
    }else{
    fogLookUp[i] = i+fogLimit;
    if(fogLookUp[i] > 127){
    fogLookUp[i] = 127;
    }
    }

     }
    

    }

    //彩虹风格颜色查找表
    public void buildRainBowLookupTable(){
    java.net.URL imageURL = this.getClass().getResource("rainbow.jpg");
    if(imageURL !=null){
    try {
    BufferedImage image = ImageIO.read(imageURL);
    int width = image.getWidth();
    int height = image.getHeight();
    rainbowLookup = new int[width];
    int[] inPixels = new int[width*height];
    getRGB(image,0,0,width,height,inPixels);
    for(int col=0;col<width;col++){
    rainbowLookup[col] = inPixels[col];
    }
    } catch (IOException e) {
    // e.printStackTrace();
    System.out.println("An error occured when loading the image icon...");
    }
    }

    }
    }
    测试代码同上

posted @ 2018-01-31 18:10  爽朗的sunmeng  阅读(498)  评论(0编辑  收藏  举报