-
Notifications
You must be signed in to change notification settings - Fork 99
/
HSLFiltering.java
304 lines (261 loc) · 9.59 KB
/
HSLFiltering.java
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Catalano Imaging Library
// The Catalano Framework
//
// Copyright © Diego Catalano, 2012-2016
// diego.catalano at live.com
//
// Copyright © Andrew Kirillov, 2007-2008
// andrew.kirillov at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
package Catalano.Imaging.Concurrent.Filters;
import Catalano.Core.FloatRange;
import Catalano.Core.IntRange;
import Catalano.Imaging.Concurrent.Share;
import Catalano.Imaging.FastBitmap;
import Catalano.Imaging.IApplyInPlace;
import Catalano.Imaging.Tools.ColorConverter;
/**
* Color filtering in HSL color space.
*
* <para>The filter operates in <b>HSL</b> color space and filters pixels, which color is inside/outside of the specified HSL range -
* it keeps pixels with colors inside/outside of the specified range and fills the rest with FillColor specified color.</para>
*
* @author Diego Catalano
*/
public class HSLFiltering implements IApplyInPlace{
private IntRange hue = new IntRange(0, 359);
private FloatRange saturation = new FloatRange(0, 1);
private FloatRange luminance = new FloatRange(0, 1);
private int fillH = 0;
private float fillS = 0.0f;
private float fillL = 0.0f;
private boolean fillOutsideRange = true;
private boolean updateH = true;
private boolean updateS = true;
private boolean updateL = true;
/**
* Get Range of hue component, [0, 359].
* @return Hue component.
*/
public IntRange getHue() {
return hue;
}
/**
* Set Range of hue component, [0, 359].
* @param hue Hue component.
*/
public void setHue(IntRange hue) {
this.hue = hue;
}
/**
* Get Range of saturation component, [0, 1].
* @return Saturation component.
*/
public FloatRange getSaturation() {
return saturation;
}
/**
* Set Range of saturation component, [0, 1].
* @param saturation Saturation component.
*/
public void setSaturation(FloatRange saturation) {
this.saturation = saturation;
}
/**
* Get Range of luminance component, [0, 1].
* @return Luminance component.
*/
public FloatRange getLuminance() {
return luminance;
}
/**
* Set Range of luminance component, [0, 1].
* @param luminance Luminance component.
*/
public void setLuminance(FloatRange luminance) {
this.luminance = luminance;
}
/**
* Get fill color used to fill filtered pixels.
* @return Hue color.
*/
public float[] getFillColor(){
return new float[] {fillH, fillS, fillL};
}
/**
* Set fill color used to fill filtered pixels.
* @param hue Hue, [0, 359].
* @param saturation Saturation, [0, 1].
* @param luminance Luminance, [0, 1].
*/
public void setFillColor(int hue, float saturation, float luminance){
this.fillH = hue;
this.fillS = saturation;
this.fillL = luminance;
}
/**
* Verify if pixels should be filled inside or outside specified color range.
* @return True if fill outside the range, otherwise false.
*/
public boolean isFillOutsideRange() {
return fillOutsideRange;
}
/**
* Determines, if pixels should be filled inside or outside specified.
* @param fillOutsideRange True if fill outside the range, otherwise false.
*/
public void setFillOutsideRange(boolean fillOutsideRange) {
this.fillOutsideRange = fillOutsideRange;
}
/**
* Verify if hue value of filtered pixels should be updated.
* @return True if should be updated, otherwise false.
*/
public boolean isUpdatedHue(){
return updateH;
}
/**
* Determines, if hue value of filtered pixels should be updated.
* @param update True if should be updated, otherwise false.
*/
public void setUpdateHue(boolean update){
this.updateH = update;
}
/**
* Verify if saturation value of filtered pixels should be updated.
* @return True if should be updated, otherwise false.
*/
public boolean isUpdatedSaturation(){
return updateS;
}
/**
* Determines, if saturation value of filtered pixels should be updated.
* @param update True if should be updated, otherwise false.
*/
public void setUpdateSaturation(boolean update){
this.updateS = update;
}
/**
* Verify if luminance value of filtered pixels should be updated.
* @return True if should be updated, otherwise false.
*/
public boolean isUpdatedLuminance(){
return updateL;
}
/**
* Determines, if luminance value of filtered pixels should be updated.
* @param update True if should be updated, otherwise false.
*/
public void setUpdateLuminance(boolean update){
this.updateL = update;
}
/**
* Initializes a new instance of the HSLFiltering class.
*/
public HSLFiltering() {}
/**
* Initializes a new instance of the HSLFiltering class.
* @param hue Range of hue component.
* @param saturation Range of saturation component.
* @param luminance Range of luminance component.
*/
public HSLFiltering(IntRange hue, FloatRange saturation, FloatRange luminance){
this.hue = hue;
this.saturation = saturation;
this.luminance = luminance;
}
@Override
public void applyInPlace(FastBitmap fastBitmap) {
if (fastBitmap.isRGB()){
Parallel(fastBitmap);
}
else{
throw new IllegalArgumentException("HSL Filtering only works in RGB space color.");
}
}
private void Parallel(FastBitmap fastBitmap){
int cores = Runtime.getRuntime().availableProcessors();
Thread[] t = new Thread[cores];
int part = fastBitmap.getHeight() / cores;
int startX = 0;
for (int i = 0; i < cores; i++) {
t[i] = new Thread(new Run(new Share(fastBitmap, startX, startX + part)));
t[i].start();
startX += part;
}
try {
for (int i = 0; i < cores; i++) {
t[i].join();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private class Run implements Runnable {
private Share share;
public Run(Share obj) {
this.share = obj;
}
@Override
public void run() {
boolean updated;
for (int i = share.startX; i < share.endHeight; i++) {
for (int j = 0; j < share.endWidth; j++) {
updated = false;
int r = share.fastBitmap.getRed(i, j);
int g = share.fastBitmap.getGreen(i, j);
int b = share.fastBitmap.getBlue(i, j);
double[] hsl = ColorConverter.RGBtoHSL(r, g, b);
// check HSL values
if (
( hsl[1] >= saturation.getMin() ) && ( hsl[1] <= saturation.getMax() ) &&
( hsl[2] >= luminance.getMin() ) && ( hsl[2] <= luminance.getMax() ) &&
(
( ( hue.getMin() < hue.getMax() ) && ( hsl[0] >= hue.getMin() ) && ( hsl[0] <= hue.getMax() ) ) ||
( ( hue.getMin() > hue.getMax() ) && ( ( hsl[0] >= hue.getMin() ) || ( hsl[0] <= hue.getMax() ) ) )
)
)
{
if ( !fillOutsideRange )
{
if ( updateH ) hsl[0] = fillH;
if ( updateS ) hsl[1] = fillS;
if ( updateL ) hsl[2] = fillL;
updated = true;
}
}
else
{
if ( fillOutsideRange )
{
if ( updateH ) hsl[0] = fillH;
if ( updateS ) hsl[1] = fillS;
if ( updateL ) hsl[2] = fillL;
updated = true;
}
}
if ( updated )
{
// convert back to RGB
int[] rgb = ColorConverter.HSLtoRGB(hsl[0], hsl[1], hsl[2]);
share.fastBitmap.setRGB(i, j, rgb);
}
}
}
}
}
}