Skip to content

Commit be82bd7

Browse files
author
daizhenjun
committed
ADD FILTER
1 parent bd5fe57 commit be82bd7

20 files changed

+1821
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* HaoRan ImageFilter Classes v0.3
3+
* Copyright (C) 2012 Zhenjun Dai
4+
*
5+
* This library is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU Lesser General Public License as published by the
7+
* Free Software Foundation; either version 2.1 of the License, or (at your
8+
* option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13+
* for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this library; if not, write to the Free Software Foundation.
17+
*/
18+
19+
package HaoRan.ImageFilter;
20+
21+
/**
22+
* 积木效果,(用于处理人像)
23+
* @author daizhj
24+
*
25+
*/
26+
public class BlindFilter implements IImageFilter{
27+
28+
boolean _direct;//horizontal: true, vertical: false
29+
int _color;
30+
int _opacity;
31+
int _width;
32+
33+
34+
public BlindFilter(boolean direct, int width, int opacity, int blindColor)
35+
{
36+
_direct = direct ;
37+
_width = (width >= 2) ? width : 2;
38+
_opacity = Function.FClamp (opacity, 1, 100);
39+
_color = blindColor;
40+
};
41+
42+
//@Override
43+
public Image process(Image imageIn) {
44+
int r, g, b, a;
45+
for(int x = 0 ; x < (imageIn.getWidth() - 1) ; x++){
46+
for(int y = 0 ; y < (imageIn.getHeight() - 1) ; y++){
47+
r = imageIn.getRComponent(x, y);
48+
g = imageIn.getGComponent(x, y);
49+
b = imageIn.getBComponent(x, y);
50+
int nMod = 0 ;
51+
if (_direct) // horizontal direction
52+
nMod = y % _width ;
53+
else if (_direct == false) // vertical direction
54+
nMod = x % _width ;
55+
56+
double fDelta = 255.0 * (_opacity/100.0) / (_width-1.0);
57+
a = Function.FClamp0255(nMod * fDelta) ;
58+
int colorR = _color & 0xFF0000 >> 16;
59+
int colorG = _color & 0x00FF00 >> 8;
60+
int colorB = _color & 0x0000FF;
61+
if (_color == 0xFF)
62+
{
63+
imageIn.setPixelColor(x, y, colorR, colorG, colorB);
64+
continue ;
65+
}
66+
if (a == 0)
67+
continue ;
68+
69+
int t = 0xFF - a ;
70+
imageIn.setPixelColor(x, y, (colorR * a + r * t) / 0xFF, (colorG * a + g * t) / 0xFF, (colorB * a + b * t) / 0xFF);
71+
}
72+
}
73+
return imageIn;
74+
}
75+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
* HaoRan ImageFilter Classes v0.3
3+
* Copyright (C) 2012 Zhenjun Dai
4+
*
5+
* This library is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU Lesser General Public License as published by the
7+
* Free Software Foundation; either version 2.1 of the License, or (at your
8+
* option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13+
* for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this library; if not, write to the Free Software Foundation.
17+
*/
18+
19+
package HaoRan.ImageFilter;
20+
21+
import java.nio.Buffer;
22+
import java.nio.ByteBuffer;
23+
import java.nio.IntBuffer;
24+
import java.text.SimpleDateFormat;
25+
import java.util.Date;
26+
27+
import android.content.res.ColorStateList;
28+
import android.graphics.Color;
29+
import android.util.Log;
30+
31+
/**
32+
* ·´É«Ð§¹û
33+
* @author daizhj
34+
*
35+
*/
36+
public class ColorToneFilter implements IImageFilter{
37+
double _hue ;
38+
double _saturation ;
39+
double[] _lum_tab = new double[256];
40+
41+
42+
/// @name RGB <--> HLS (Hue, Lightness, Saturation).
43+
//@{
44+
/**
45+
RGB --> HLS \n
46+
prgb - address of 24bpp or 32bpp pixel.
47+
*/
48+
static double[] RGBtoHLS (int rgb, double H, double L, double S)
49+
{
50+
int colorR = (rgb & 0x00FF0000) >> 16;
51+
int colorG = (rgb & 0x0000FF00) >> 8;
52+
int colorB = rgb & 0x000000FF;
53+
//Color color = Color.rgb(colorR, colorG, bluecolorB);
54+
int n_cmax = Math.max(colorR, Math.max(colorG, colorB));
55+
int n_cmin = Math.min(colorR, Math.min(colorG, colorB));
56+
57+
L = (n_cmax + n_cmin) / 2.0 / 255.0 ;
58+
if (n_cmax == n_cmin)
59+
{
60+
S = 0.0;
61+
H = 0.0 ;
62+
return new double[] {H, L ,S};
63+
}
64+
65+
double r = colorR / 255.0,
66+
g = colorG / 255.0,
67+
b = colorB / 255.0,
68+
cmax = n_cmax / 255.0,
69+
cmin = n_cmin / 255.0,
70+
delta = cmax - cmin ;
71+
72+
if (L < 0.5)
73+
S = delta / (cmax + cmin) ;
74+
else
75+
S = delta / (2.0 - cmax - cmin) ;
76+
77+
if (colorR == n_cmax)
78+
H = (g-b) / delta ;
79+
else if (colorG == n_cmax)
80+
H = 2.0 + (b-r) / delta ;
81+
else
82+
H = 4.0 + (r-g) / delta ;
83+
84+
H /= 6.0 ;
85+
86+
if (H < 0.0)
87+
H += 1.0 ;
88+
89+
return new double[] {H, L ,S};
90+
}
91+
92+
static int DoubleRGB_to_RGB (double r, double g, double b)
93+
{
94+
return Color.rgb(Image.SAFECOLOR((int)(r*255)), Image.SAFECOLOR((int)(g*255)), Image.SAFECOLOR((int)(b*255))) ;
95+
}
96+
97+
static double HLS_Value (double n1, double n2, double h)
98+
{
99+
if (h > 6.0)
100+
h -= 6.0 ;
101+
else if (h < 0.0)
102+
h += 6.0 ;
103+
104+
if (h < 1.0)
105+
return n1 + (n2 - n1) * h ;
106+
else if (h < 3.0)
107+
return n2 ;
108+
else if (h < 4.0)
109+
return n1 + (n2 - n1) * (4.0 - h) ;
110+
return n1 ;
111+
}
112+
113+
/// HLS --> RGB.
114+
static int HLStoRGB (double H, double L, double S)
115+
{
116+
if ((!(S > 0)) && (!(S < 0))) // == 0
117+
return DoubleRGB_to_RGB (L, L, L) ;
118+
119+
double m1, m2 ;
120+
if (L > 0.5)
121+
m2 = L + S - L*S ;
122+
else
123+
m2 = L * (1.0 + S) ;
124+
m1 = 2.0*L - m2 ;
125+
126+
double r = HLS_Value (m1, m2, H*6.0 + 2.0) ;
127+
double g = HLS_Value (m1, m2, H*6.0) ;
128+
double b = HLS_Value (m1, m2, H*6.0 - 2.0) ;
129+
return DoubleRGB_to_RGB (r,g,b) ;
130+
}
131+
132+
133+
/**
134+
Calculate grayscale value of pixel \n
135+
prgb - address of 24bpp or 32bpp pixel.
136+
*/
137+
static int GetGrayscale (int r, int g , int b)
138+
{
139+
return (int)((30*r + 59*g + 11*g) / 100) ;
140+
}
141+
142+
public ColorToneFilter(int tone, int saturation)
143+
{
144+
double l = 0.0f ;
145+
double[] result = RGBtoHLS (tone, _hue, l, _saturation) ;
146+
_hue = result[0];
147+
l = result[1];
148+
_saturation = result[2];
149+
_saturation = _saturation * (saturation/255.0) * (saturation/255.0) ;
150+
_saturation = ((_saturation < 1) ? _saturation : 1) ;
151+
152+
for (int i=0 ; i < 256 ; i++)
153+
{
154+
int cr = Color.rgb(i,i,i) ;
155+
double h = 0.0f, ll = 0.0f, s = 0.0f ;
156+
result = RGBtoHLS (cr, h, ll, s) ;
157+
h = result[0];
158+
ll = result[1];
159+
s = result[2];
160+
ll = ll * (1 + (128- Math.abs(saturation-128)) / 128.0 / 9.0) ;
161+
_lum_tab[i] = ((ll < 1) ? ll : 1) ;
162+
}
163+
};
164+
165+
//@Override
166+
public Image process(Image imageIn) {
167+
int r, g, b;
168+
for(int x = 0 ; x < imageIn.getWidth() ; x++){
169+
for(int y = 0 ; y < imageIn.getHeight() ; y++){
170+
r = imageIn.getRComponent(x, y);
171+
g = imageIn.getGComponent(x, y);
172+
b = imageIn.getBComponent(x, y);
173+
174+
double l = _lum_tab[GetGrayscale(r, g, b)] ;
175+
int cr = HLStoRGB (_hue, l, _saturation) ;
176+
imageIn.setPixelColor(x, y, cr);
177+
}
178+
}
179+
return imageIn;
180+
}
181+
182+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* HaoRan ImageFilter Classes v0.3
3+
* Copyright (C) 2012 Zhenjun Dai
4+
*
5+
* This library is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU Lesser General Public License as published by the
7+
* Free Software Foundation; either version 2.1 of the License, or (at your
8+
* option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13+
* for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this library; if not, write to the Free Software Foundation.
17+
*/
18+
19+
package HaoRan.ImageFilter.Distort;
20+
21+
22+
import android.graphics.Color;
23+
import HaoRan.ImageFilter.*;
24+
/**
25+
* ÔëµãÌØÐ§
26+
* @author daizhj
27+
*
28+
*/
29+
public class BilinearDistort implements IImageFilter{
30+
31+
Image clone;
32+
33+
BilinearDistort(){};
34+
35+
double[] calc_undistorted_coord (int x, int y, double un_x, double un_y){
36+
return new double[]{un_x, un_y};
37+
}
38+
39+
boolean IsInside (int width , int height, int x, int y) {
40+
return (x>=0) && (x<width) && (y>=0) && (y<height);
41+
}
42+
43+
static int GetBilinear (double x, double y, int[] colors)
44+
{
45+
46+
int[] px0 = new int[3];
47+
px0[0] = (colors[0] & 0xFF0000)>>16;
48+
px0[1] = (colors[0] & 0x00FF00)>>8;
49+
px0[2] = colors[0] & 0x0000FF;
50+
51+
int[] px1 = new int[3];
52+
px1[0] = (colors[1] & 0xFF0000)>>16;
53+
px1[1] = (colors[1] & 0x00FF00)>>8;
54+
px1[2] = colors[1] & 0x0000FF;
55+
56+
int[] px2 = new int[3];
57+
px2[0] = (colors[2] & 0xFF0000)>>16;
58+
px2[1] = (colors[2] & 0x00FF00)>>8;
59+
px2[2] = colors[2] & 0x0000FF;
60+
61+
int[] px3 = new int[3];
62+
px3[0] = (colors[3] & 0xFF0000)>>16;
63+
px3[1] = (colors[3] & 0x00FF00)>>8;
64+
px3[2] = colors[3] & 0x0000FF;
65+
66+
int[] crRet = new int[3];
67+
for (int i=0 ; i < 3 ; i++)
68+
{
69+
double m0 = px0[i] + x * (px1[i] - px0[i]);
70+
double m1 = px2[i] + x * (px3[i] - px2[i]);
71+
double my = m0 + y * (m1 - m0) ;
72+
crRet[i] = Image.SAFECOLOR((int)my) ;
73+
}
74+
75+
return Color.rgb(crRet[0], crRet[1], crRet[2]);
76+
}
77+
78+
//@Override
79+
public Image process(Image imageIn) {
80+
clone = imageIn.clone();
81+
int width = imageIn.getWidth();
82+
int height = imageIn.getHeight();
83+
for(int x = 0 ; x < width; x++){
84+
for(int y = 0 ; y < height ; y++){
85+
double un_x = 0, un_y = 0 ;
86+
double[] result = calc_undistorted_coord (x, y, un_x, un_y) ;
87+
un_x = result[0];un_y = result[1];
88+
int crNull = Color.WHITE;
89+
int cr = crNull ;
90+
if ( (un_x > -1) && (un_x < width) && (un_y > -1) && (un_y < height) )
91+
{
92+
// only this range is valid
93+
int nSrcX = ((un_x < 0) ? -1 : (int)un_x);
94+
int nSrcY = ((un_y < 0) ? -1 : (int)un_y);
95+
int nSrcX_1 = nSrcX + 1;
96+
int nSrcY_1 = nSrcY + 1;
97+
98+
int[] color = new int[4];
99+
color[0] = IsInside(width, height, nSrcX, nSrcY) ? clone.getPixelColor(nSrcX,nSrcY) : crNull;
100+
color[1] = IsInside(width, height, nSrcX_1, nSrcY) ? clone.getPixelColor(nSrcX_1,nSrcY) : crNull;
101+
color[2] = IsInside(width, height, nSrcX, nSrcY_1) ? clone.getPixelColor(nSrcX,nSrcY_1) : crNull;
102+
color[3] = IsInside(width, height, nSrcX_1, nSrcY_1) ? clone.getPixelColor(nSrcX_1,nSrcY_1) : crNull;
103+
cr = GetBilinear(un_x-nSrcX, un_y-nSrcY, color);
104+
}
105+
imageIn.setPixelColor(x, y, cr);
106+
}
107+
}
108+
return imageIn;
109+
}
110+
}

0 commit comments

Comments
 (0)