|
| 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 | +} |
0 commit comments