zoukankan      html  css  js  c++  java
  • 图像饱和度调整

    package chapter4;

    import java.awt.image.BufferedImage;
    import java.nio.Buffer;

    /**
    * Created by LENOVO on 18-1-29.
    */
    public class SaturationFilter extends AbstractBufferedImageOp{
    public static final double clo60 = 1.0/60.0;
    public static final double clo255 = 1.0/255.0;
    private double ratio =0.25;//饱和度调整系数
    public SaturationFilter(){}
    public double getRatio() {
    return ratio;
    }
    public void setRatio(double ratio) {
    this.ratio = ratio;
    }
    public BufferedImage filter(BufferedImage src,BufferedImage dest){
    int width = src.getWidth();
    int height = src.getHeight();
    if(dest == null){//目标图像为空的话
    dest = creatCompatibleDestImage(src,null);
    }
    double sat =127.0d*ratio;//sat为调整值,计算公式为sat=127*ratio
    int[] inpixels = new int[width*height];
    int[] outpixels = new int[width*height];

    getRGB(src,0,0,width,height,inpixels);
    double min,max,dif,sum;
    double f1,f2;
    int index = 0;
    double h,s,l;//HSL色彩空间的三个分量
    double v1,v2,v3,h1;
    for(int row=0;row<height;row++){
    int ta=0,tr=0,tg=0,tb=0;
    for(int col=0;col<width;col++){
    index = row*width+col;
    ta = (inpixels[index] >> 24 ) & 0xff;
    tr = (inpixels[index] >> 16 ) & 0xff;
    tg = (inpixels[index] >> 8 ) & 0xff;
    tb = (inpixels[index]) & 0xff;

    //RGB 转换为 HSL
    min = tr;
    if(tg<min)
    min = tg;
    if(tb<min)
    min = tb;
    max = tr;
    f1 = 0.0;
    f2 = tg-tb;
    if(tg>max){
    max = tg;
    f1 = 120.0;
    f2 = tb-tr;
    }
    if(tb >max){
    max = tb;
    f1 = 240.0;
    f2 = tr-tg;
    }
    dif = max-min;
    sum = max+min;
    l =0.5*sum;//亮度仅与图像的最多颜色成分和最少的颜色成分的总量有关。亮度越小,图像越趋于黑色。亮度越高图像越趋于明亮的白色。
    if(dif == 0){//最大值与最小值相同,则表示为灰色,那么s定义为0,h未定义通常也写为0
    h = 0.0;
    s = 0.0;
    } else if(l<127.5){//根据亮度l计算饱和度
    s = 255.0*dif/sum;
    } else{
    s = 255.0*dif/(510.0-sum);
    }
    h = (f1 +60.0*f2)/dif;//计算色调h
    if(h<0.0){
    h+=360.0;
    }
    if(h>360.0){
    h-=360.0;
    }

    //饱和度调整
    s = s+sat;
    if(s<0.0){
    s = 0.0;
    }
    if(s>255.0){
    s = 255.0;
    }

    //HSL 转换为 RGB
    if(s == 0){//表示灰色,R,G,B定义为0
    tr = (int)l;
    tg = (int)l;
    tb = (int)l;
    }else{
    if(l<127.5){
    v2 = clo255 *l*(255+s);
    }else{
    v2 = l+s-clo255*s*l;
    }
    v1 = 2*l-v2;
    v3 = v2-v1;
    h1 = h+120.0;
    if(h1>=360.0)
    h1 -=360.0;
    //计算tr
    if(h1<60){
    tr = (int)(v1 +v3 *h1 *clo60);
    }else if(h1<180.0){
    tr = (int)v2;
    }else if(h1<240.0){
    tr = (int)(v1+v3*(4-h1*clo60));
    }else{
    tr = (int)v1;
    }
    //计算tg
    h1 = h;
    if(h1<60.0){
    tg = (int)(v1 +v3 *h1 *clo60);
    }else if(h1 <180.0){
    tg = (int)v2;
    }else if(h1<240.0){
    tg = (int)(v1+v3*(4-h1*clo60));
    }else {
    tg = (int)v1;
    }
    //计算tb
    h1 = h-120.0;
    if(h1 <0.0){
    h1 += 360.0;
    }
    if(h1<60.0){
    tb = (int)(v1 +v3 *h1 *clo60);
    }else if(h1<180.0){
    tb = (int)v2;
    }else if(h1<240.0){
    tb = (int)(v1+v3*(4-h1*clo60));
    }else{
    tb = (int)v1;
    }
    }
    outpixels[index] = (ta <<24) | (clamp(tr) << 16) | (clamp(tg) << 8) | clamp(tb);
    }
    }
    setRGB(dest,0,0,width,height,outpixels);
    return dest;
    }
    public static int clamp(int c){
    return c>255 ? 255:((c<0) ? 0:c);
    }
    }

    测试代码同上面老照片特效!!

  • 相关阅读:
    DEDECMS之五 单页
    DEDECMS之六 网站地图、RSS地图
    DEDECMS之四 栏目调用
    DEDECMS之三 首页、列表页怎么调用文章内容
    DEDECMS之七 如何实现文章推荐排行榜
    centos6下安装dedecms
    C# 自动部署之附加数据库
    产品经理技能之BRD的笔记之菜鸟入门
    产品经理技能之MRD的笔记之一
    产品需求文档(PRD)的写作方法之笔记一
  • 原文地址:https://www.cnblogs.com/bigdream6/p/8378278.html
Copyright © 2011-2022 走看看