-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageHandler.java
More file actions
166 lines (117 loc) · 3.24 KB
/
ImageHandler.java
File metadata and controls
166 lines (117 loc) · 3.24 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
public class ImageHandler {
private MImage one;
public ImageHandler(MImage one) {
this.one = one;
}
// task1
public void grayScale() {
int[] rgb = new int[3];
int gray = 0;
for (int i = 0; i < one.getW(); i++) {
for (int j = 0; j < one.getH(); j++) {
one.getPixel(i, j, rgb);
gray = (int) Math.round((0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]));
rgb[0] = gray;
rgb[1] = gray;
rgb[2] = gray;
one.setPixel(i, j, rgb);
}
}
}
// task2
public void UCQ() {
int[] rgb = new int[3];
for (int i = 0; i < one.getW(); i++) {
for (int j = 0; j < one.getH(); j++) {
one.getPixel(i, j, rgb);
rgb[0] = (rgb[0] / 32) * 32 + 16;
rgb[1] = (rgb[1] / 32) * 32 + 16;
rgb[2] = (rgb[2] / 64) * 64 + 32;
one.setPixel(i, j, rgb);
}
}
}
public void indexValue() {
int[] rgb = new int[3];
int gray = 0;
for (int i = 0; i < one.getW(); i++) {
for (int j = 0; j < one.getH(); j++) {
one.getPixel(i, j, rgb);
/*
gray = (int) Math.round((0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]));
rgb[0] = (gray /32) *32 +16;
rgb[1] = gray/32*32 +16;
rgb[2] = (gray/64)*64 +32;*/
/*
rgb[0] = (rgb[0] / 32) * 32 + 16;
rgb[1] = (rgb[1] / 32) * 32 + 16;
rgb[2] = (rgb[2] / 64) * 64 + 32;
gray = (int) Math.round((0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]));
rgb[0] = gray;
rgb[1] = gray;
rgb[2] = gray;*/
one.setPixel(i, j, rgb);
}
}
}
public void generateLookUpTable() {
System.out.println("LUT by UCQ");
System.out.println("Index " + " R " + " G " + " B");
System.out.println("____________________________");
int rgb[] = new int[3];
int bin[] = new int[3];
int r = 0, b = 0, g = 0;
int red = 0;
int green = 0;
int blue = 0;
r = rgb[0] / 32;
b = rgb[1] / 32;
g = rgb[2] / 64;
for (int i = 0; i <= 255; i++) {
bin = rgbValue8Bit(i);
red = 32 * bin[0] + 16;
green = 32 * bin[1] + 16;
blue = 64 * bin[2] + 32;
System.out.println(i + " " + red + " " + green + " " + blue);
}
}
public int[] rgbValue8Bit(int index) {
int bin[] = new int[3];
// RED
if (index / (int) Math.pow(2, 7) > 0) {
bin[0] += (int) Math.pow(2, 2);
index -= (int) Math.pow(2, 7);
}
if (index / (int) Math.pow(2, 6) > 0) {
bin[0] += (int) Math.pow(2, 1);
index -= (int) Math.pow(2, 6);
}
if (index / (int) Math.pow(2, 5) > 0) {
bin[0] += (int) Math.pow(2, 0);
index -= (int) Math.pow(2, 5);
}
// Green
if (index / (int) Math.pow(2, 4) > 0) {
bin[1] += (int) Math.pow(2, 2);
index -= (int) Math.pow(2, 4);
}
if (index / (int) Math.pow(2, 3) > 0) {
bin[1] += (int) Math.pow(2, 1);
index -= (int) Math.pow(2, 3);
}
if (index / (int) Math.pow(2, 2) > 0) {
bin[1] += (int) Math.pow(2, 0);
index -= (int) Math.pow(2, 2);
}
// BLUE
if (index / (int) Math.pow(2, 1) > 0) {
bin[2] += (int) Math.pow(2, 1);
index -= (int) Math.pow(2, 1);
}
if (index == 1) {
bin[2] += (int) Math.pow(2, 0);
index -= (int) Math.pow(2, 0);
}
return bin;
}
}