This repository has been archived by the owner on Dec 13, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImgBuffProcess.java
111 lines (88 loc) · 3.73 KB
/
ImgBuffProcess.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
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.List;
public class ImgBuffProcess {
/**Average the color value of a list of ImgBuff and produce a new ImgBuff
* @param list the list of ImgBuff to be averaged
* @return a new ImgBuff
*/
public static ImgBuff averager (List<ImgBuff> list) {
int maxWidth = 0;
int maxHeight = 0;
for (int i=0; i<list.size(); i++){ //set the max width, height in the list of ImgBuff
maxWidth = (maxWidth > list.get(i).getWidth()) ? maxWidth : list.get(i).getWidth();
maxHeight = (maxHeight > list.get(i).getHeight()) ? maxHeight : list.get(i).getHeight();
}
int[] resultPixelsR = new int[maxWidth*maxHeight];
int[] resultPixelsG = new int[maxWidth*maxHeight];
int[] resultPixelsB = new int[maxWidth*maxHeight];
for (int x=0; x<list.size(); x++){ //summing value
int[] data1 = ((DataBufferInt)list.get(x).getImage().getRaster().getDataBuffer()).getData();
for (int j=0; j<data1.length; j++) {
resultPixelsR[j]+= (data1[j] >> 16 & 0xFF);
resultPixelsG[j]+= (data1[j] >> 8 & 0xFF);
resultPixelsB[j]+= (data1[j] & 0xFF);
}
}
for (int i=0; i<resultPixelsR.length; i++){ //averaging value
resultPixelsR[i]/=list.size();
resultPixelsG[i]/=list.size();
resultPixelsB[i]/=list.size();
}
BufferedImage img = new BufferedImage(maxWidth, maxHeight, BufferedImage.TYPE_INT_RGB);
for (int i=0; i<maxWidth; i++){
for (int j=0; j<maxHeight; j++) {
int resultPixel = (resultPixelsR[i + maxWidth*j] << 16)|
(resultPixelsG[i + maxWidth*j] << 8)|
(resultPixelsB[i + maxWidth*j]);
img.setRGB(i, j, resultPixel);
}
}
return (new ImgBuff(img));
}
/**Average the color value of a list of ImgBuff and produce a new ImgBuff
* averagerV2 used the approach where resultColor = sqrt( sum(Color^2) / n )
* instead of resultColor = sum(Color)/n.
*
* See reference: https://www.youtube.com/watch?v=LKnqECcg6Gw
* @param list the list of ImgBuff to be averaged
* @return a new ImgBuff
*/
public static ImgBuff averagerV2 (List<ImgBuff> list) {
int maxWidth = 0;
int maxHeight = 0;
for (int i=0; i<list.size(); i++){ //set the max width, height in the list of ImgBuff
maxWidth = (maxWidth > list.get(i).getWidth()) ? maxWidth : list.get(i).getWidth();
maxHeight = (maxHeight > list.get(i).getHeight()) ? maxHeight : list.get(i).getHeight();
}
int[] resultPixelsR2 = new int[maxWidth*maxHeight];
int[] resultPixelsG2 = new int[maxWidth*maxHeight];
int[] resultPixelsB2 = new int[maxWidth*maxHeight];
for (int x=0; x<list.size(); x++){ //summing value
int[] data1 = ((DataBufferInt)list.get(x).getImage().getRaster().getDataBuffer()).getData();
for (int j=0; j<data1.length; j++) {
resultPixelsR2[j]+= ((data1[j] >> 16 & 0xFF)*(data1[j] >> 16 & 0xFF));
resultPixelsG2[j]+= ((data1[j] >> 8 & 0xFF)*(data1[j] >> 8 & 0xFF));
resultPixelsB2[j]+= ((data1[j] & 0xFF)*(data1[j] & 0xFF));
}
}
for (int i=0; i<resultPixelsR2.length; i++){ //averaging value
resultPixelsR2[i]/=list.size();
resultPixelsG2[i]/=list.size();
resultPixelsB2[i]/=list.size();
resultPixelsR2[i]=(int) Math.sqrt(resultPixelsR2[i]);
resultPixelsG2[i]=(int) Math.sqrt(resultPixelsG2[i]);
resultPixelsB2[i]=(int) Math.sqrt(resultPixelsB2[i]);
}
BufferedImage img = new BufferedImage(maxWidth, maxHeight, BufferedImage.TYPE_INT_RGB);
for (int i=0; i<maxWidth; i++){
for (int j=0; j<maxHeight; j++) {
int resultPixel = (resultPixelsR2[i + maxWidth*j] << 16)|
(resultPixelsG2[i + maxWidth*j] << 8)|
(resultPixelsB2[i + maxWidth*j]);
img.setRGB(i, j, resultPixel);
}
}
return (new ImgBuff(img));
}
}