- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageProcessing.java
More file actions
Latest commit
230 lines (198 loc) · 5.91 KB
/
Copy pathImageProcessing.java
File metadata and controls
230 lines (198 loc) · 5.91 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
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
packagemain;
publicfinalclassImageProcessing {
/**
* Returns red component from given packed color.
*
* @param rgb: a 32-bits RGB color. Requirement: none, any integer
* @return an integer, between 0 and 255
* @see #getGreen
* @see #getBlue
* @see #getRGB(int, int, int)
*/
publicstaticintgetRed(intrgb) {
intdecal;
decal = rgb >> 16;
intresultat = decal & 0xff;
if (resultat < 0) {
return0;
} elseif (resultat > 255) {
return255;
}
returnresultat;
}
/**
* Returns green component from given packed color.
*
* @param rgb: a 32-bits RGB color. Requirement: none, any integer
* @return an integer between 0 and 255
* @see #getRed
* @see #getBlue
* @see #getRGB(int, int, int)
*/
publicstaticintgetGreen(intrgb) {
intdecal = rgb >> 8;
intresultat = decal & 0xff;
if (resultat < 0) {
return0;
} elseif (resultat > 255) {
return255;
}
returnresultat;
}
/**
* Returns blue component from given packed color.
*
* @param rgb: a 32-bits RGB color. Requirement: none, any integer
* @return an integer between 0 and 255
* @see #getRed
* @see #getGreen
* @see #getRGB(int, int, int)
*/
publicstaticintgetBlue(intrgb) {
intresultat = rgb & 0xff;
if (resultat < 0) {
return0;
} elseif (resultat > 255) {
return255;
}
returnresultat;
}
/**
* Returns the average of red, green and blue components from given packed
* color.
*
* @param rgb: 32-bits RGB color. Requirement: none, any integer
* @return a double between 0 and 255
* @see #getRed
* @see #getGreen
* @see #getBlue
* @see #getRGB(int)
*/
publicstaticdoublegetGray(intrgb) {
intred = getRed(rgb);
intgreen = getGreen(rgb);
intblue = getBlue(rgb);
doublegray = (red + green + blue) / 3.0;
returngray;
}
/**
* Returns packed RGB components from given red, green and blue components.
*
* @param red: an integer. Requirement: none, any integer
* @param green: an integer. Requirement: none, any integer
* @param blue: an integer. Requirement: none, any integer
* @return a 32-bits RGB color
* @see #getRed
* @see #getGreen
* @see #getBlue
*/
publicstaticintgetRGB(intred, intgreen, intblue) {
if (red < 0) {
red = 0;
} elseif (red > 255) {
red = 255;
}
if (green < 0) {
green = 0;
} elseif (green > 255) {
green = 255;
}
if (blue < 0) {
blue = 0;
} elseif (blue > 255) {
blue = 255;
}
intrgb = (red << 16) | (green << 8) | (blue);
returnrgb;
}
/**
* Returns packed RGB components from given gray-scale value.
*
* @param gray: a double. Requirement: none, any double
* @return a 32-bits RGB color
* @see #getGray
*/
publicstaticintgetRGB(doublegray) {
if (gray < 0) {
gray = 0;
} elseif (gray > 255) {
gray = 255;
}
intred = (int) gray;
intgreen = (int) gray;
intblue = (int) gray;
intrgb = (red << 16) | (green << 8) | (blue);
returnrgb;
}
/**
* Converts packed RGB image to gray-scale image.
*
* @param image: a HxW integer array. Requirement: image has to be an array of greater than 1 by 1
* @return a HxW double array
* @see #encode
* @see #getGray
*/
publicstaticdouble[][] toGray(int[][] image) {
assertimage.length > 0 && image[0].length > 0;
double[][] gray = newdouble[image.length][image[0].length];
for (introw = 0; row < image.length; row++) {
for (intcol = 0; col < image[0].length; col++) {
gray[row][col] = getGray(image[row][col]);
}
}
returngray;
}
/**
* Converts gray-scale image to packed RGB image.
*
* @param channels: a HxW double array. Requirement: gray has to be an array greater than 1 by 1
* @return a HxW integer array
* @see #decode
* @see #getRGB(double)
*/
publicstaticint[][] toRGB(double[][] gray) {
assertgray.length > 0 && gray[0].length > 0;
int[][] imageRgb = newint[gray.length][gray[0].length];
for (introw = 0; row < gray.length; row++) {
for (intcol = 0; col < gray[0].length; col++) {
imageRgb[row][col] = getRGB(gray[row][col]);
}
}
returnimageRgb;
}
/**
* Convert an arbitrary 2D double matrix into a 2D integer matrix which can be
* used as RGB image
*
* @param matrix: the arbitrary 2D double array to convert into integer. Requirement: matrix has to be an array greater than 1 by 1
* @param min: a double, the minimum value the matrix could theoretically contains. Requirement: none, any integer
* @param max: a double, the maximum value the matrix could theoretically contains. Requirement: none, any integer
* @return an 2D integer array, containing a RGB mapping of the matrix
*/
publicstaticint[][] matrixToRGBImage(double[][] matrix, doublemin, doublemax) {
assertmatrix.length > 0 && matrix[0].length > 0;
double[][] copyOfMatrix = copyMatrix(matrix);
for (introw = 0; row < matrix.length; row++) {
for (intcol = 0; col < matrix[0].length; col++) {
copyOfMatrix[row][col] = (matrix[row][col] - min) / (max - min) * 255;
}
}
int[][] imageRgb = toRGB(copyOfMatrix);
returnimageRgb;
}
/**
* Copy the 2D array matrix in an other 2D array of the same size called copyMatrix
* @param matrix : the arbitrary 2D double array to copy in a double array. Requirement: matrix has to be an array greater than 1 by 1
* @return a 2D double array
*/
publicstaticdouble[][] copyMatrix(double[][] matrix){
assertmatrix.length > 0 && matrix[0].length > 0;
double[][] copyMatrix = newdouble [matrix.length][matrix[0].length];
for (introw = 0; row < copyMatrix.length; row++) {
for (intcol = 0; col < copyMatrix[0].length; col++) {
copyMatrix[row][col] = matrix[row][col];
}
}
returncopyMatrix;
}
}