• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <math.h>
18 #include "filters.h"
19 
fastevalPoly(double * poly,int n,double x)20 double fastevalPoly(double *poly,int n, double x){
21 
22     double f =x;
23     double sum = poly[0]+poly[1]*f;
24     int i;
25     for (i = 2; i < n; i++) {
26         f*=x;
27         sum += poly[i]*f;
28     }
29     return sum;
30 }
31 
rgb2hsv(unsigned char * rgb,int rgbOff,unsigned short * hsv,int hsvOff)32 void rgb2hsv( unsigned char *rgb,int rgbOff,unsigned short *hsv,int hsvOff)
33 {
34     int iMin,iMax,chroma;
35     int ABITS = 4;
36     int HSCALE = 256;
37 
38     int k1=255 << ABITS;
39     int k2=HSCALE << ABITS;
40 
41     int ri = rgb[rgbOff+0];
42     int gi = rgb[rgbOff+1];
43     int bi = rgb[rgbOff+2];
44     short rv,rs,rh;
45 
46     if (ri > gi) {
47         iMax = MAX (ri, bi);
48         iMin = MIN (gi, bi);
49     } else {
50         iMax = MAX (gi, bi);
51         iMin = MIN (ri, bi);
52     }
53 
54     chroma = iMax - iMin;
55     // set value
56     rv = (short)( iMax << ABITS);
57 
58     // set saturation
59     if (rv == 0)
60         rs = 0;
61     else
62         rs = (short)((k1*chroma)/iMax);
63 
64     // set hue
65     if (rs == 0)
66         rh = 0;
67     else {
68         if ( ri == iMax ) {
69             rh  = (short)( (k2*(6*chroma+gi - bi))/(6*chroma));
70             if (rh >= k2) rh -= k2;
71         } else if (gi  == iMax)
72             rh  = (short)( (k2*(2*chroma+bi - ri ))/(6*chroma));
73         else // (bi == iMax )
74                     rh  = (short)( (k2*(4*chroma+ri - gi ))/(6*chroma));
75     }
76     hsv[hsvOff+0] = rv;
77     hsv[hsvOff+1] = rs;
78     hsv[hsvOff+2] = rh;
79 }
80 
hsv2rgb(unsigned short * hsv,int hsvOff,unsigned char * rgb,int rgbOff)81 void hsv2rgb(unsigned short *hsv,int hsvOff, unsigned char *rgb,int rgbOff)
82 {
83     int ABITS = 4;
84     int HSCALE = 256;
85     int m;
86     int H,X,ih,is,iv;
87     int k1=255<<ABITS;
88     int k2=HSCALE<<ABITS;
89     int k3=1<<(ABITS-1);
90     int rr=0;
91     int rg=0;
92     int rb=0;
93     short cv = hsv[hsvOff+0];
94     short cs = hsv[hsvOff+1];
95     short ch = hsv[hsvOff+2];
96 
97     // set chroma and min component value m
98     //chroma = ( cv * cs )/k1;
99     //m = cv - chroma;
100     m = ((int)cv*(k1 - (int)cs ))/k1;
101 
102     // chroma  == 0 <-> cs == 0 --> m=cv
103     if (cs == 0) {
104         rb = ( rg = ( rr =( cv >> ABITS) ));
105     } else {
106         ih=(int)ch;
107         is=(int)cs;
108         iv=(int)cv;
109 
110         H = (6*ih)/k2;
111         X = ((iv*is)/k2)*(k2- abs(6*ih- 2*(H>>1)*k2 - k2)) ;
112 
113         // removing additional bits --> unit8
114         X=( (X+iv*(k1 - is ))/k1 + k3 ) >> ABITS;
115         m=m >> ABITS;
116 
117         // ( chroma + m ) --> cv ;
118         cv=(short) (cv >> ABITS);
119         switch (H) {
120         case 0:
121             rr = cv;
122             rg = X;
123             rb = m;
124             break;
125         case 1:
126             rr = X;
127             rg = cv;
128             rb = m;
129             break;
130         case 2:
131             rr = m;
132             rg = cv;
133             rb = X;
134             break;
135         case 3:
136             rr = m;
137             rg = X;
138             rb = cv;
139             break;
140         case 4:
141             rr = X;
142             rg = m;
143             rb = cv;
144             break;
145         case 5:
146             rr = cv;
147             rg = m ;
148             rb = X;
149             break;
150         }
151     }
152     rgb[rgbOff+0] =  rr;
153     rgb[rgbOff+1] =  rg;
154     rgb[rgbOff+2] =  rb;
155 }
156 
157