-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageInfo.java
304 lines (270 loc) · 15.4 KB
/
ImageInfo.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
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class ImageInfo {
private int height;
private int width;
private int colorModel;
private int length;
private byte[] data;
private BufferedImage bufferedImage;
public ImageInfo(String filename, int width, int height, int colorModel) {
this.colorModel = colorModel;
this.height = height;
this.width = width;
this.bufferedImage = new BufferedImage(this.width, this.height, this.colorModel);
this.data = new byte[this.width * this.height * 3];
setBufferedImage(filename);
}
public void setBufferedImage(String filename) {
getDataFromFile(filename);
setBufferedImage(this.data);
}
public void setBufferedImage(byte[] data) {
this.data = data;
int idx = 0;
for (int ydx = 0; ydx < this.height; ydx++) {
for (int xdx = 0; xdx < this.width; xdx++) {
this.bufferedImage.setRGB(xdx, ydx, 0xff000000 | ((this.data[idx] & 0xff) << 16) | ((this.data[idx + this.height * this.width] & 0xff) << 8) | (this.data[idx + this.height * (this.width << 1)] & 0xff));
idx++;
}
}
}
public ImageInfo(int width, int height, int colorModel) {
this.colorModel = colorModel;
this.width = width;
this.height = height;
this.data = new byte[this.width * this.height * 3];
this.bufferedImage = new BufferedImage(this.width, this.height, this.colorModel);
}
public static void resize(ImageInfo imageInfo, ImageInfo expectedImageInfo, float scaleWidth, float scaleHeight, int antiAliasing, int analysis) {
float FAC_TWO = 2.0f;
if (analysis == 0) {
for (int ydx = 0; ydx < expectedImageInfo.getHeight(); ydx++) {
for (int xdx = 0; xdx < expectedImageInfo.width(); xdx++) {
int[] rgbArray = antiAliasing == 0 ? imageInfo.getRGB(xdx, ydx) : imageInfo.getAverageRGBPixel((int) Math.floor(xdx / scaleWidth), (int) Math.floor(ydx / scaleHeight));
expectedImageInfo.setRGB(xdx, ydx, 0xff000000 | ((rgbArray[0] & 0xff) << 16) | ((rgbArray[1] & 0xff) << 8) | (rgbArray[2] & 0xff));
}
}
} else if (analysis == 1) {
if (antiAliasing == 0) {
int resizeType = -1;
int squareSide = 0;
int side1 = 0;
int side2 = 0;
if (expectedImageInfo.getHeight() > expectedImageInfo.width()) {
resizeType = 0;
int squareSideSrcImage = imageInfo.width();
squareSide = expectedImageInfo.width();
float ratio = (float) squareSide / squareSideSrcImage;
side1 = (int) Math.floor((expectedImageInfo.getHeight() - squareSide) / FAC_TWO);
side2 = (int) Math.ceil((expectedImageInfo.getHeight() - squareSide) / FAC_TWO);
int side1src = (int) Math.floor((imageInfo.getHeight() - squareSideSrcImage) / FAC_TWO);
int side2src = (int) Math.ceil((imageInfo.getHeight() - squareSideSrcImage) / FAC_TWO);
for (int y = 0; y < expectedImageInfo.getHeight(); y++) {
for (int x = 0; x < expectedImageInfo.width(); x++) {
if (y < side1) {
int[] rgb = imageInfo.getRGB((int) Math.floor(x / scaleWidth), (int) Math.floor(y / ((float) side1 / side1src)));
expectedImageInfo.setRGB(x, y, 0xff000000 | ((rgb[0] & 0xff) << 16) | ((rgb[1] & 0xff) << 8) | (rgb[2] & 0xff));
} else if (y > (side1 + squareSide)) {
float scale3 = (float) side2 / side2src;
int[] rgb = imageInfo.getRGB((int) Math.floor(x / scaleWidth), (int) Math.floor((y - side1 - squareSide) / scale3 + side1src + squareSideSrcImage));
expectedImageInfo.setRGB(x, y, 0xff000000 | ((rgb[0] & 0xff) << 16) | ((rgb[1] & 0xff) << 8) | (rgb[2] & 0xff));
} else {
int[] rgb = imageInfo.getRGB((int) Math.floor(x / ratio), (int) Math.floor((y - side1 + side1src) / ratio));
expectedImageInfo.setRGB(x, y, 0xff000000 | ((rgb[0] & 0xff) << 16) | ((rgb[1] & 0xff) << 8) | (rgb[2] & 0xff));
}
}
}
} else {
resizeType = 1;
int squareSideSrc = imageInfo.getHeight();
squareSide = expectedImageInfo.getHeight();
float ratio = (float) squareSide / squareSideSrc;
side1 = (int) Math.floor((expectedImageInfo.width() - squareSide) / FAC_TWO);
side2 = (int) Math.ceil((expectedImageInfo.width() - squareSide) / FAC_TWO);
int side1src = (int) Math.floor((imageInfo.width() - squareSideSrc) / FAC_TWO);
int side2src = (int) Math.ceil((imageInfo.width() - squareSideSrc) / FAC_TWO);
for (int ydx = 0; ydx < expectedImageInfo.getHeight(); ydx++) {
for (int xdx = 0; xdx < expectedImageInfo.width(); xdx++) {
if (xdx < side1) {
int[] rgbArray = imageInfo.getRGB((int) Math.floor(xdx / ((float) side1 / side1src)), (int) Math.floor(ydx / scaleHeight));
expectedImageInfo.setRGB(xdx, ydx, 0xff000000 | ((rgbArray[0] & 0xff) << 16) | ((rgbArray[1] & 0xff) << 8) | (rgbArray[2] & 0xff));
} else if (xdx > (side1 + squareSide)) {
float scale3 = (float) side2 / side2src;
int[] rgbArray = imageInfo.getRGB((int) Math.floor((xdx - side1 - squareSide) / scale3 + side1src + squareSideSrc), (int) Math.floor(ydx / scaleHeight));
expectedImageInfo.setRGB(xdx, ydx, 0xff000000 | ((rgbArray[0] & 0xff) << 16) | ((rgbArray[1] & 0xff) << 8) | (rgbArray[2] & 0xff));
} else {
int[] rgbArray = imageInfo.getRGB((int) Math.floor((xdx - side1 + side1src) / ratio), (int) Math.floor(ydx / ratio));
expectedImageInfo.setRGB(xdx, ydx, 0xff000000 | ((rgbArray[0] & 0xff) << 16) | ((rgbArray[1] & 0xff) << 8) | (rgbArray[2] & 0xff));
}
}
}
}
} else {
int resizeType = -1;
int squareSide = 0;
int side1 = 0;
int side2 = 0;
if (expectedImageInfo.getHeight() > expectedImageInfo.width()) {
resizeType = 0; // Vertical
int squareSideSrc = imageInfo.width();
squareSide = expectedImageInfo.width();
float ratio = (float) squareSide / squareSideSrc;
side1 = (int) Math.floor((expectedImageInfo.getHeight() - squareSide) / FAC_TWO);
side2 = (int) Math.ceil((expectedImageInfo.getHeight() - squareSide) / FAC_TWO);
int side1src = (int) Math.floor((imageInfo.getHeight() - squareSideSrc) / FAC_TWO);
int side2src = (int) Math.ceil((imageInfo.getHeight() - squareSideSrc) / FAC_TWO);
for (int y = 0; y < expectedImageInfo.getHeight(); y++) {
for (int x = 0; x < expectedImageInfo.width(); x++) {
if (y < side1) {
int[] rgb = imageInfo.getAverageRGBPixel((int) Math.floor(x / scaleWidth), (int) Math.floor(y / ((float) side1 / side1src)));
expectedImageInfo.setRGB(x, y, 0xff000000 | ((rgb[0] & 0xff) << 16) | ((rgb[1] & 0xff) << 8) | (rgb[2] & 0xff));
} else if (y > (side1 + squareSide)) {
float scale3 = (float) side2 / side2src;
int[] rgb = imageInfo.getAverageRGBPixel((int) Math.floor(x / scaleWidth), (int) Math.floor((y - side1 - squareSide) / scale3 + side1src + squareSideSrc));
expectedImageInfo.setRGB(x, y, 0xff000000 | ((rgb[0] & 0xff) << 16) | ((rgb[1] & 0xff) << 8) | (rgb[2] & 0xff));
} else {
int[] rgb = imageInfo.getAverageRGBPixel((int) Math.floor(x / ratio), (int) Math.floor((y - side1 + side1src) / ratio));
expectedImageInfo.setRGB(x, y, 0xff000000 | ((rgb[0] & 0xff) << 16) | ((rgb[1] & 0xff) << 8) | (rgb[2] & 0xff));
}
}
}
} else {
resizeType = 1;
int squareSideSrc = imageInfo.getHeight();
squareSide = expectedImageInfo.getHeight();
float ratio = (float) squareSide / squareSideSrc;
side1 = (int) Math.floor((expectedImageInfo.width() - squareSide) / FAC_TWO);
side2 = (int) Math.ceil((expectedImageInfo.width() - squareSide) / FAC_TWO);
int side1src = (int) Math.floor((imageInfo.width() - squareSideSrc) / FAC_TWO);
int side2src = (int) Math.ceil((imageInfo.width() - squareSideSrc) / FAC_TWO);
for (int ydx = 0; ydx < expectedImageInfo.getHeight(); ydx++) {
for (int xdx = 0; xdx < expectedImageInfo.width(); xdx++) {
if (xdx < side1) {
int[] rgb = imageInfo.getAverageRGBPixel((int) Math.floor(xdx / ((float) side1 / side1src)), (int) Math.floor(ydx / scaleHeight));
expectedImageInfo.setRGB(xdx, ydx, 0xff000000 | ((rgb[0] & 0xff) << 16) | ((rgb[1] & 0xff) << 8) | (rgb[2] & 0xff));
} else if (xdx > (side1 + squareSide)) {
float scale3 = (float) side2 / side2src;
int[] rgb = imageInfo.getAverageRGBPixel((int) Math.floor((xdx - side1 - squareSide) / scale3 + side1src + squareSideSrc), (int) Math.floor(ydx / scaleHeight));
expectedImageInfo.setRGB(xdx, ydx, 0xff000000 | ((rgb[0] & 0xff) << 16) | ((rgb[1] & 0xff) << 8) | (rgb[2] & 0xff));
} else {
int[] rgb = imageInfo.getAverageRGBPixel((int) Math.floor((xdx - side1 + side1src) / ratio), (int) Math.floor(ydx / ratio));
expectedImageInfo.setRGB(xdx, ydx, 0xff000000 | ((rgb[0] & 0xff) << 16) | ((rgb[1] & 0xff) << 8) | (rgb[2] & 0xff));
}
}
}
}
}
}
}
private int[] getAverageRGBPixel(int x, int y) {
{
float[] rgbArr = new float[3];
for (int i = 0; i < 3; i++) {
rgbArr[i] = 0;
}
int[] rgbIntArr = new int[3];
float FAC_9 = 9.0f;
float FAC_6 = 6.0f;
float FAC_4 = 4.0f;
boolean cond1 = x > 0 && y > 0 && x < this.width - 1 && y < this.height - 1;
boolean cond2 = x == 0 && y == 0;
boolean cond3 = x == 0 && y == this.height - 1;
boolean cond4 = x == this.width - 1 && y == 0;
boolean cond5 = x == this.width - 1 && y == this.height - 1;
boolean cond8 = x == this.width - 1;
boolean cond9 = y == this.height - 1;
if (cond1) {
for (int idx = x - 1; idx <= x + 1; idx++) {
for (int jdx = y - 1; jdx <= y + 1; jdx++) {
int[] rgbArray = this.getRGB(idx, jdx);
for (int cdx = 0; cdx < 3; cdx++) {
rgbArr[cdx] += rgbArray[cdx] / FAC_9;
}
}
}
} else if (cond2 || cond3) {
for (int idx = 0; idx <= 1; idx++) {
for (int jdx = cond3 ? y - 1 : 0; jdx <= (cond3 ? y : 1); jdx++) {
int[] rgbArray = this.getRGB(idx, jdx);
for (int cdx = 0; cdx < 3; cdx++) {
rgbArr[cdx] += rgbArray[cdx] / FAC_4;
}
}
}
} else if (cond4 || cond5) {
for (int idx = x - 1; idx <= x; idx++) {
for (int jdx = cond5 ? y - 1 : 0; jdx <= (cond5 ? y : 1); jdx++) {
int[] rgbArray = this.getRGB(idx, jdx);
for (int cdx = 0; cdx < 3; cdx++) {
rgbArr[cdx] += rgbArray[cdx] / FAC_4;
}
}
}
} else if (x == 0 || y == 0) {
for (int idx = y == 0 ? x - 1 : 0; idx <= (y == 0 ? x + 1 : 1); idx++) {
for (int jdx = y == 0 ? 0 : y - 1; jdx <= (y == 0 ? 1 : y + 1); jdx++) {
int[] rgbArray = this.getRGB(idx, jdx);
for (int cdx = 0; cdx < 3; cdx++) {
rgbArr[cdx] += rgbArray[cdx] / FAC_6;
}
}
}
} else if (cond8 || cond9) {
for (int idx = x - 1; idx <= (cond9 ? x + 1 : x); idx++) {
for (int jdx = y - 1; jdx <= (cond9 ? y : y + 1); jdx++) {
int[] rgbArray = this.getRGB(idx, jdx);
for (int cdx = 0; cdx < 3; cdx++) {
rgbArr[cdx] += rgbArray[cdx] / FAC_6;
}
}
}
}
for (int idx = 0; idx < 3; idx++) {
rgbIntArr[idx] = (int) Math.floor(rgbArr[idx]);
}
return rgbIntArr;
}
}
public BufferedImage copyData() {
return new BufferedImage(this.bufferedImage.getColorModel(), this.bufferedImage.copyData(null), false, null);
}
private void getDataFromFile(String filename) {
try {
File file = new File(filename);
InputStream inputStream = new FileInputStream(file);
this.length = (int) file.length();
int offset = 0, numRead = 0;
while (offset < this.length && (numRead = inputStream.read(this.data, offset, this.length - offset)) >= 0) {
offset += numRead;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public BufferedImage getImg() {
return this.bufferedImage;
}
public int getHeight() {
return this.height;
}
public int width() {
return this.width;
}
public void setRGB(int x, int y, int pix) {
this.bufferedImage.setRGB(x, y, pix);
}
public int[] getRGB(int x, int y) {
int xTemp = x, yTemp = y;
if (x >= this.width) xTemp--;
if (y >= this.height) yTemp--;
int pixValue = this.bufferedImage.getRGB(xTemp, yTemp);
int[] rgbArray = new int[3];
rgbArray[0] = (pixValue >> 16) & 0xff;
rgbArray[1] = (pixValue >> 8) & 0xff;
rgbArray[2] = (pixValue & 0xff);
return rgbArray;
}
}