-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.java
More file actions
184 lines (164 loc) · 4 KB
/
Util.java
File metadata and controls
184 lines (164 loc) · 4 KB
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
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Util {
final static int checkPixelBounds(int value) {
if (value > 255)
return 255;
if (value < 0)
return 0;
return value;
}
// get red channel from colorspace (4 bytes)
final static int getR(int rgb) {
return checkPixelBounds((rgb & 0x00ff0000) >>> 16);
}
// get green channel from colorspace (4 bytes)
final static int getG(int rgb) {
return checkPixelBounds((rgb & 0x0000ff00) >>> 8);
}
// get blue channel from colorspace (4 bytes)
final static int getB(int rgb) {
return checkPixelBounds(rgb & 0x000000ff);
}
final static int makeColor(int r, int g, int b) {
return (255 << 24 | r << 16 | g << 8 | b);
}
final static int covertToGray(int r, int g, int b) {
return checkPixelBounds((int) (0.2126 * r + 0.7152 * g + 0.0722 * b));
}
final static double[] affine(double[][] a, double[] b) {
int aRow = a.length;
int bRow = b.length;
double[] result = new double[aRow];
for (int i = 0; i < aRow; i++) {
for (int j = 0; j < bRow; j++) {
result[i] += a[i][j] * b[j];
}
}
return result;
}
final static int bilinear(int leftTop, int rightTop, int leftBottom, int rightBottom, double alpha, double beta) {
double left = linear(leftTop, leftBottom, alpha);
double right = linear(rightTop, rightBottom, alpha);
double value = linear(left, right, beta);
return checkPixelBounds((int) value);
}
final static double linear(double v1, double v2, double weight) {
return v1 + (v2 - v1) * weight; // 10 + (20-10) * 0.2
}
final static int checkImageBounds(int value, int length) {
if (value > length - 1)
return length - 1;
else if (value < 0)
return 0;
else
return value;
}
final static int[][] DELTA(int[][] a, int[][] b) {
int DELTA[][] = new int[a.length][a[0].length];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
DELTA[i][j] = a[i][j] - b[i][j];
}
}
return DELTA;
}
static HSL RGB2HSL(double r, double g, double b) {
double h = 0;
double s;
double l;
r = r / 255.0;
g = g / 255.0;
b = b / 255.0;
double min = Math.min(r, Math.min(g, b));
double max = Math.max(r, Math.max(g, b));
l = 0.5 * (min + max);
s = getS(min, max, l);
if (s == 0) {
h = 0.0;
} else if (max == r) {
h = ((g - b) / (max - min)) % 6;
} else if (max == g) {
h = 2.0 + (b - r) / (max - min);
} else if (max == b) {
h = 4.0 + (r - g) / (max - min);
}
h *= 60;
if (h < 0.0) {
h += 360;
}
if (h > 360) {
h -= 360;
}
return new HSL(h, s, l);
}
static double getS(double min, double max, double l) {
double ans = 0.0;
if (min == max) {
return ans;
}
if (l < 0.5) {
ans = (max - min) / (max + min);
}
if (l > 0.5) {
ans = (max - min) / (max + min);
}
return ans;
}
static int[] paint(double hue, double sat, double lum) {
int [] a = new int[3] ;
if (hue < 0) {
hue += 360;
}
if (hue > 360) {
hue -= 360;
}
if (sat < 0.0) {
sat = 0.0;
}
if (sat > 1.0) {
sat = 1.0;
}
if (lum < 0.0) {
lum = 0.0;
}
if (lum > 1.0) {
lum = 1.0;
}
double r =0, g=0 , b=0 ;
double c = (1.0 - Math.abs(2 * lum - 1.0)) * sat;
double x = c * (1 - Math.abs(((hue / 60) % 2) - 1));
double m = lum - (c /2.0);
if (hue > 0 && hue < 60) {
r = c;
g = x;
b = 0;
} else if (hue >= 60 && hue < 120) {
r = x;
g = c;
b = 0;
} else if (hue >= 120 && hue < 180) {
r = 0;
g = c;
b = x;
} else if (hue >= 180 && hue < 240) {
r = 0;
g = x;
b = c;
} else if (hue >= 240 && hue < 300) {
r = c;
g = 0;
b = x;
}
r += m;
g += m;
b += m;
a[0] = checkPixelBounds((int) (255 * r));
a[1] = checkPixelBounds((int) (255 * g));
a[2] = checkPixelBounds((int) (255 * b));
return a;
}
}