Android项目上处理图像的代码(注释全部去掉)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
void RGB2HSV( uint16_t * h, uint16_t * s, uint16_t * v, uint8_t r, uint8_t g, uint8_t b)
{
    double rr, gg, bb;
    double hh, ss, vv;
    double cmax, cmin, cdes;
 
    rr = r;
    gg = g;
    bb = b;
 
    cmax = (rr > gg) ? rr : gg;
    if (bb > cmax) {
        cmax = bb;
    }
 
    cmin = (rr < gg) ? rr : gg;
    if (bb < cmin) {
        cmin = bb;
    }
 
    cdes = cmax - cmin;
    vv = cmax;
    if (cdes != 0) {
        ss = cdes * SCALE / cmax;
        if (cmax == rr) {
            hh = (gg - bb) * SCALE / cdes;
        }else if (cmax == gg) {
            hh = (bb - rr) * SCALE / cdes + 2 * H_SCALE;
        }else {
            hh = (rr - gg) * SCALE / cdes + 4 * H_SCALE;
        }
    }else if (cmax != 0) {
        ss = cdes * SCALE / cmax;
        hh = 0;
    }else {
        ss = 0;
        hh = 0;
    }
    if (hh < 0) {
        hh += 6 * H_SCALE;
    }
 
    *h = hh * H_GETA;
    *s = ss * H_GETA;
    *v = vv * H_GETA;
}
 
void HSV2RGB( uint8_t *r, uint8_t *g, uint8_t *b, uint16_t h, uint16_t s, uint16_t v)
{
    double rr = 0, gg = 0, bb = 0;
    double hh, ss, vv;
 
    if (h == 6 * H_GETA * H_SCALE) {
        h = 0;
    }
    hh = (double)h / H_GETA;
    ss = (double)s / GETA;
    vv = (double)v / GETA;
 
    switch((int)(hh / H_SCALE)) {
        case 0:
            rr = SCALE;
            gg = hh;
            bb = 0;
            break;
        case 1:
            rr = 2 * H_SCALE - hh;
            gg = SCALE;
            bb = 0;
            break;
        case 2:
            rr = 0;
            gg = SCALE;
            bb = hh - 2 * H_SCALE;
            break;
        case 3:
            rr = 0;
            gg = 4 * H_SCALE - hh;
            bb = SCALE;
            break;
        case 4:
            rr = hh - 4 * H_SCALE;
            gg = 0;
            bb = SCALE;
            break;
        case 5:
            rr = SCALE;
            gg = 0;
            bb = 6 * H_SCALE - hh;
            break;
    }
 
    rr = (rr + (SCALE - rr) * (SCALE - ss) / SCALE) * vv / SCALE;
    gg = (gg + (SCALE - gg) * (SCALE - ss) / SCALE) * vv / SCALE;
    bb = (bb + (SCALE - bb) * (SCALE - ss) / SCALE) * vv / SCALE;
 
    *r = rr;
    *g = gg;
    *b = bb;
    if (*r > 255)*r = 255;
    if (*g > 255)*g = 255;
    if (*b > 255)*b = 255;
}
 
void RGB2HLS( double *h, double *l, double *s, uint8_t r, uint8_t g, uint8_t b)
{
    double dr = (double)r/255;
    double dg = (double)g/255;
    double db = (double)b/255;
    double cmax = MAX(dr, MAX(dg, db));
    double cmin = MIN(dr, MIN(dg, db));
    double cdes = cmax - cmin;
    double hh, ll, ss;
 
    ll = (cmax+cmin)/2;
    if(cdes){
        if(ll <= 0.5)
            ss = (cmax-cmin)/(cmax+cmin);
        else
            ss = (cmax-cmin)/(2-cmax-cmin);
 
        if(cmax == dr)
            hh = (0+(dg-db)/cdes)*60;
        else if(cmax == dg)
            hh = (2+(db-dr)/cdes)*60;
        else// if(cmax == b)
            hh = (4+(dr-dg)/cdes)*60;
        if(hh<0)
            hh+=360;
    }else
        hh = ss = 0;
 
    *h = hh;
    *l = ll;
    *s = ss;
}
 
void HLS2RGB( uint8_t *r, uint8_t *g, uint8_t *b, double h, double l, double s)
{
    double cmax,cmin;
 
    if(l <= 0.5)
        cmax = l*(1+s);
    else
        cmax = l*(1-s)+s;
    cmin = 2*l-cmax;
 
    if(s == 0){
        *r = *g = *b = l*255;
    }else{
        *r = HLS2RGBvalue(cmin,cmax,h+120)*255;
        *g = HLS2RGBvalue(cmin,cmax,h)*255;
        *b = HLS2RGBvalue(cmin,cmax,h-120)*255;
    }
}
 
double HLS2RGBvalue(double n1,double n2, double hue)
{
    if(hue > 360)
        hue -= 360;
    else if(hue < 0)
        hue += 360;
    if(hue < 60)
        return n1+(n2-n1)*hue/60;
    else if(hue < 180)
        return n2;
    else if(hue < 240)
        return n1+(n2-n1)*(240-hue)/60;
    else
        return n1;
}

RGB HSV HLS三种色彩模式转换(C语言实现)的更多相关文章

  1. 执行Go程序的三种方式及Go语言关键字

    执行 Go 程序的三种方式及 Go 语言关键字 执行 Go 程序的三种方式 一.使用 go run 命令 二.使用 go build 命令 Step1. 对 go 源码源文件执行 go build 命 ...

  2. Java程序员的现代RPC指南(Windows版预编译好的Protoc支持C++,Java,Python三种最常用的语言,Thrift则支持几乎主流的各种语言)

    Java程序员的现代RPC指南 1.前言 1.1 RPC框架简介 最早接触RPC还是初学Java时,直接用Socket API传东西好麻烦.于是发现了JDK直接支持的RMI,然后就用得不亦乐乎,各种大 ...

  3. PHP,JavaScript,CSS三种HTML内嵌语言的语法,变量,循环,函数记录

    PHP PHP简介: PHP 是服务器端脚本语言. PHP(全称:PHP:Hypertext Preprocessor,即"PHP:超文本预处理器")是一种通用开源脚本语言. PH ...

  4. hibernate 增改查后对象的三种状态转换

    this.getSession().update(obj); this.getSession().merge(obj); this.getSession().saveOrUpdate(obj);1. ...

  5. 低功耗蓝牙UUID三种格式转换

    熟悉BLE技术同学应该对UUID不陌生,服务.特征值.描述都是有UUID格式定义. 蓝牙广播中对服务UUID格式定义都有三种16 bit UUID.32 bit UUID.128 bit UUID. ...

  6. 计算2的n次方的三种方法(C语言实现)

    C代码如下: #include <stdio.h> int func1(int n) { <<n; } int func2(int n) { ) { ; } )*; } int ...

  7. ASP.NET MVC:多语言的三种技术处理策略

    ASP.NET MVC:多语言的三种技术处理策略 背景 本文介绍了多语言的三种技术处理策略,每种策略对应一种场景,这三种场景是: 多语言资源信息只被.NET使用. 多语言资源信息只被Javascrip ...

  8. 三种方式解决你的js加载乱码

    第一种方式——编码统一 我们以前觉得出现乱码的原因是因为编码不统一,就是因为我们设置编码统一之后,就解决了问题,所以,让html和js的编码统一,是最简单的一个乱码解决方式,原因是什么,是因为,如果你 ...

  9. hibernate 三种状态的转换

    一.遇到的神奇的事情 使用jpa操作数据库,当我使用findAll()方法查处一个List的对象后,给对这个list的实体进行了一些操作,并没有调用update 或者 saveOrUpdate方法,更 ...

随机推荐

  1. What’s New in Python 2.7 — Python 3.4.0b2 documentation

    What's New in Python 2.7 - Python 3.4.0b2 documentation What's New in Python 2.7¶

  2. Java字符串排序中文+数字

    编写日期: 2013年9月15日 另一中解法:点击查看 解决思路: 在Java中,排序需要复写的是 equals 方法 和 Comparable<T> 接口 的public int com ...

  3. 建立Go工作环境

    最近在折腾Go语言,找了个开源项目nsq研究源代码.不过前两天不小心把系统搞挂了,这次又要重做一遍,记录一下,备忘. 准备: 1. vim+golang插件+ctags(新版本支持Go) 2. Go1 ...

  4. C语言宏定义技巧

    出处:http://blog.chinaunix.net/uid-14022540-id-2849095.html 1.宏中"#"和"##"的用法 一.一般用法 ...

  5. python 函数形参四种格式

    1:f(a,b) 2:f(a,b=value)有默认值的参数必须在后面 3:f(*a)多个参数直接传进一个元组 4:f(**a)以keys=values 形式给参数,传入转换为字典 def test( ...

  6. @(报错)could not find the main class, Program will exit(已解决)

    原文 @(报错)could not find the main class, Program will exit(已解决)      (很抱歉,如果你希望能更加清楚地看清图片或是图上的文字的话,你可以 ...

  7. haproxy 配置日志

    jrhppt01:/root# vim /etc/haproxy/haproxy.cfg # this config needs haproxy-1.1.28 or haproxy-1.2.1 glo ...

  8. xend调用xenstore的出错揭秘

    近期发现几例问题,均是xend里面报了同一个错误 File "/usr/lib64/python2.4/site-packages/xen/xend/xenstore/xstransact. ...

  9. BZOJ 1264: [AHOI2006]基因匹配Match( LCS )

    序列最大长度2w * 5 = 10w, O(n²)的LCS会T.. LCS 只有当a[i] == b[j]时, 才能更新答案, 我们可以记录n个数在第一个序列中出现的5个位置, 然后从左往右扫第二个序 ...

  10. log4net学习目录

    log4net是用来记录日志的,日志是用来帮助我们排除错误和异常的.这是我们编写软件必须要用到的东西,前面总结了一些有关日志和log4net的文章,在这整理个目录东大家参考. C#日志工具汇总 log ...