-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageDisplay.java
489 lines (450 loc) · 21 KB
/
ImageDisplay.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
public class ImageDisplay {
JFrame frame;
JLabel lbIm1;
JLabel lbIm2;
BufferedImage imageOne;
BufferedImage decodedImage;
int width = 512;
int height = 512;
int scaledHeight = height / 8;
int scaledWidth = width / 8;
int[][][] originalimage;
int[][][] dctValues;
int[][][] deQuantizeValues;
double[][][][] cos;
int[][][] finalimage;
double c1 = 0.25;
double c2 = 0.25 * 0.707;
double c3 = 0.125;
int calpow;
public void readImageRGB(int width, int height, String imagePath, BufferedImage image) {
try {
int frameLength = width * height * 3;
originalimage = new int[3][height][width];
File file = new File(imagePath);
RandomAccessFile raf = new RandomAccessFile(file, "r");
raf.seek(0);
long len = frameLength;
byte[] bytes = new byte[(int) len];
raf.read(bytes);
int ind = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
byte r = bytes[ind];
byte g = bytes[ind + height * width];
byte b = bytes[ind + height * width * 2];
originalimage[0][y][x] = r & 0xff;
originalimage[1][y][x] = g & 0xff;
originalimage[2][y][x] = b & 0xff;
int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
image.setRGB(x, y, pix);
ind++;
}
}
raf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void encodeDCT() {
cos = new double[8][8][8][8];
double pi = Math.PI;
dctValues = new int[3][height][width];
for (int x = 0; x < 8; x++) {
int x2 = 2 * x + 1;
for (int y = 0; y < 8; y++) {
for (int a = 0; a < 8; a++) {
int a2 = 2 * a + 1;
for (int b = 0; b < 8; b++) {
cos[y][b][x][a] = Math.cos(pi * x2 * y * 0.0625) * Math.cos(pi * a2 * b * 0.0625);
}
}
}
}
for (int ch = 0; ch < 3; ch++) {
for (int y = 0; y < scaledHeight; y++) {
for (int x = 0; x < scaledWidth; x++) {
double c;
for (int v = 0; v < 8; v++) {
for (int u = 0; u < 8; u++) {
double sumDCT = 0;
if (u != 0 && v != 0)
c = c1;
else if ((u == 0 && v != 0) || (u != 0 && v == 0))
c = c2;
else
c = c3;
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
// DCT
sumDCT += originalimage[ch][j + y * 8][i + x * 8] * cos[v][u][j][i];
}
}
// quantization
dctValues[ch][v + y * 8][u + x * 8] = (int)(sumDCT * c / calpow);
}
}
}
}
}
}
public void iDCTBaselineMode(int latency, BufferedImage image) {
deQuantize();
int flag = 1;
if(latency == 0){
flag = 0;
}
for(int z1=0;z1<scaledHeight;z1++){
for(int z2=0;z2<scaledWidth;z2++){
for (int ch = 0; ch < 3; ch++) {
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
double sum = 0;
for (int v = 0; v < 8; v++) {
for (int u = 0; u < 8; u++) {
double c;
if (u != 0 && v != 0)
c = c1;
else if ((u == 0 && v != 0) || (u != 0 && v == 0))
c = c2;
else
c = c3;
int dequantizedValue = deQuantizeValues[ch][z1*8 + v][z2*8 + u];
// iDCT
sum += dequantizedValue * cos[v][u][j][i] * c;
}
}
finalimage[ch][z1*8 + j][z2*8 + i] = (int) sum;
// Set the pixel in the image
int rgb = image.getRGB(z2*8 + i, z1*8 + j);
if (ch == 0) {
rgb = (rgb & 0xFF00FFFF) | ((finalimage[ch][z1*8 + j][z2*8 + i] << 16) & 0x00FF0000);
} else if (ch == 1) {
rgb = (rgb & 0xFFFF00FF) | ((finalimage[ch][z1*8 + j][z2*8 + i] << 8) & 0x0000FF00);
} else if (ch == 2) {
rgb = (rgb & 0xFFFFFF00) | (finalimage[ch][z1*8 + j][z2*8 + i] & 0x000000FF);
}
image.setRGB(z2*8 + i, z1*8 + j, rgb);
}
}
}
if(flag == 1){
lbIm2.setIcon(new ImageIcon(image)); // Update image
lbIm2.updateUI(); // Refresh UI
try {
Thread.sleep(latency);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
if(flag == 0){
lbIm2.setIcon(new ImageIcon(image)); // Update image
lbIm2.updateUI();
}
}
public void iDCTProgressiveSpectral(int latency, BufferedImage image) {
deQuantize();
for (int k = 0; k < 64; k++) {
for(int z1=0;z1<scaledHeight;z1++){
for(int z2=0;z2<scaledWidth;z2++){
for (int ch = 0; ch < 3; ch++) {
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
double sum = 0;
double c;
int check = 0;
int v = 0, u = 0;
boolean goingUp = true;
while (v < 8 && u < 8) {
if (u != 0 && v != 0)
c = c1;
else if ((u == 0 && v != 0) || (u != 0 && v == 0))
c = c2;
else
c = c3;
int dequantizedValue = deQuantizeValues[ch][z1*8 + v][z2*8 + u];
// iDCT
sum += dequantizedValue * cos[v][u][j][i] * c;
if(++check >= k)
break;
// If going up
if (goingUp) {
// If we are at the first row, move right
if (v == 0) {
u++;
goingUp = false;
}
// If we are at the last column, move down
else if (u == 8 - 1) {
v++;
goingUp = false;
}
// Move diagonally up-left
else {
v--;
u++;
}
} else {
// If we are at the last row, move down
if (v == 8 - 1) {
u++;
goingUp = true;
}
// If we are at the first column, move right
else if (u == 0) {
v++;
goingUp = true;
}
// Move diagonally down-right
else {
v++;
u--;
}
}
}
finalimage[ch][z1*8 + j][z2*8 + i] = (int) (sum);
// Set the pixel in the image
int rgb = image.getRGB(z2*8 + i, z1*8 + j);
if (ch == 0) {
rgb = (rgb & 0xFF00FFFF) | ((finalimage[ch][z1*8 + j][z2*8 + i] << 16) & 0x00FF0000);
} else if (ch == 1) {
rgb = (rgb & 0xFFFF00FF) | ((finalimage[ch][z1*8 + j][z2*8 + i] << 8) & 0x0000FF00);
} else if (ch == 2) {
rgb = (rgb & 0xFFFFFF00) | (finalimage[ch][z1*8 + j][z2*8 + i] & 0x000000FF);
}
image.setRGB(z2*8 + i, z1*8 + j, rgb);
}
}
}
}
}
lbIm2.setIcon(new ImageIcon(image)); // Update image
lbIm2.updateUI(); // Refresh UI
try {
Thread.sleep(latency);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void iDCTProgressiveSuccessiveBit(int latency, BufferedImage image) {
deQuantize();
int maxBitDepth = getMaxBit();
for (int bit = 1; bit <= maxBitDepth; bit++) {
int shiftAmount = maxBitDepth - bit;
for (int z1 = 0; z1 < scaledHeight; z1++) {
for (int z2 = 0; z2 < scaledWidth; z2++) {
for (int ch = 0; ch < 3; ch++) {
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
double sum = 0;
for (int v = 0; v < 8; v++) {
for (int u = 0; u < 8; u++) {
double c;
if (u != 0 && v != 0)
c = c1;
else if ((u == 0 && v != 0) || (u != 0 && v == 0))
c = c2;
else
c = c3;
int coefficient = deQuantizeValues[ch][z1 * 8 + v][z2 * 8 + u];
// long coefficientAsLong = (long) coefficient;
// coefficientAsLong = (coefficientAsLong >> shiftAmount) << shiftAmount;
// coefficient = coefficientAsLong;
if(coefficient < 0){
int val = -coefficient;
val = (val >> shiftAmount) << shiftAmount;
coefficient = -val;
}else{
int val = coefficient;
val = (val >> shiftAmount) << shiftAmount;
coefficient = val;
}
sum += coefficient * cos[v][u][j][i] * c;
}
}
finalimage[ch][z1 * 8 + j][z2 * 8 + i] = (int) (sum);
// Set the pixel in the image
int rgb = image.getRGB(z2 * 8 + i, z1 * 8 + j);
if (ch == 0) {
rgb = (rgb & 0xFF00FFFF) | ((finalimage[ch][z1 * 8 + j][z2 * 8 + i] << 16) & 0x00FF0000);
} else if (ch == 1) {
rgb = (rgb & 0xFFFF00FF) | ((finalimage[ch][z1 * 8 + j][z2 * 8 + i] << 8) & 0x0000FF00);
} else if (ch == 2) {
rgb = (rgb & 0xFFFFFF00) | (finalimage[ch][z1 * 8 + j][z2 * 8 + i] & 0x000000FF);
}
image.setRGB(z2 * 8 + i, z1 * 8 + j, rgb);
}
}
}
}
}
lbIm2.setIcon(new ImageIcon(image));
lbIm2.updateUI();
try {
Thread.sleep(latency);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public int getMaxBit() {
int maxBitDepth = 0;
for (int ch = 0; ch < 3; ch++) {
for (int z1 = 0; z1 < scaledHeight; z1++) {
for (int z2 = 0; z2 < scaledWidth; z2++) {
for (int v = 0; v < 8; v++) {
for (int u = 0; u < 8; u++) {
int coefficient = Math.abs(deQuantizeValues[ch][z1 * 8 + v][z2 * 8 + u]);
int bitDepth = 0;
while (coefficient != 0) {
coefficient >>= 1;
bitDepth++;
}
if (bitDepth > maxBitDepth) {
maxBitDepth = bitDepth;
}
}
}
}
}
}
return maxBitDepth;
}
public void deQuantize() {
deQuantizeValues = new int[3][height][width];
for (int ch = 0; ch < 3; ch++) {
for (int z1 = 0; z1 < scaledHeight; z1++) {
for (int z2 = 0; z2 < scaledWidth; z2++) {
for (int v = 0; v < 8; v++) {
for (int u = 0; u < 8; u++) {
deQuantizeValues[ch][z1 * 8 + v][z2 * 8 + u] = dctValues[ch][z1 * 8 + v][z2 * 8 + u] * calpow;
}
}
}
}
}
}
public void iDCTProgressiveSuccessive32Bit(int latency, BufferedImage image) {
deQuantize();
int maxBitDepth = 32;
for (int bit = 1; bit <= maxBitDepth; bit++) {
int shiftAmount = maxBitDepth - bit;
for (int z1 = 0; z1 < scaledHeight; z1++) {
for (int z2 = 0; z2 < scaledWidth; z2++) {
for (int ch = 0; ch < 3; ch++) {
for (int j = 0; j < 8; j++) {
for (int i = 0; i < 8; i++) {
double sum = 0;
for (int v = 0; v < 8; v++) {
for (int u = 0; u < 8; u++) {
double c;
if (u != 0 && v != 0)
c = c1;
else if ((u == 0 && v != 0) || (u != 0 && v == 0))
c = c2;
else
c = c3;
int coefficient = deQuantizeValues[ch][z1 * 8 + v][z2 * 8 + u];
// long coefficientAsLong = (long) coefficient;
// coefficientAsLong = (coefficientAsLong >> shiftAmount) << shiftAmount;
// coefficient = coefficientAsLong;
if(coefficient < 0){
int val = -coefficient;
val = (val >> shiftAmount) << shiftAmount;
coefficient = -val;
}else{
int val = coefficient;
val = (val >> shiftAmount) << shiftAmount;
coefficient = val;
}
sum += coefficient * cos[v][u][j][i] * c;
}
}
finalimage[ch][z1 * 8 + j][z2 * 8 + i] = (int) (sum);
// Set the pixel in the image
int rgb = image.getRGB(z2 * 8 + i, z1 * 8 + j);
if (ch == 0) {
rgb = (rgb & 0xFF00FFFF) | ((finalimage[ch][z1 * 8 + j][z2 * 8 + i] << 16) & 0x00FF0000);
} else if (ch == 1) {
rgb = (rgb & 0xFFFF00FF) | ((finalimage[ch][z1 * 8 + j][z2 * 8 + i] << 8) & 0x0000FF00);
} else if (ch == 2) {
rgb = (rgb & 0xFFFFFF00) | (finalimage[ch][z1 * 8 + j][z2 * 8 + i] & 0x000000FF);
}
image.setRGB(z2 * 8 + i, z1 * 8 + j, rgb);
}
}
}
}
}
lbIm2.setIcon(new ImageIcon(image));
lbIm2.updateUI();
try {
Thread.sleep(latency);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void showIms(String[] args){
// Read in the specified image
imageOne = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
decodedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
readImageRGB(width, height, args[0], imageOne);
// Initialize frame
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1, 2)); // Set GridLayout with 1 row and 2 columns
// Display first image
JLabel lbIm1 = new JLabel(new ImageIcon(imageOne));
frame.add(createHeaderPanel("Original Image", lbIm1));
// Display second image
lbIm2 = new JLabel(new ImageIcon(decodedImage));
frame.add(createHeaderPanel("Decoded Image", lbIm2));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
finalimage = new int[3][height][width];
int qLevel = Integer.parseInt(args[1]);
int mode = Integer.parseInt(args[2]);
int latency = Integer.parseInt(args[3]);
calpow = (int)Math.pow(2, qLevel);
encodeDCT();
switch (mode) {
case 1:
iDCTBaselineMode(latency, decodedImage);
break;
case 2:
iDCTProgressiveSpectral(latency, decodedImage);
break;
case 3:
iDCTProgressiveSuccessiveBit(latency, decodedImage);
break;
case 4:
iDCTProgressiveSuccessive32Bit(latency, decodedImage);
break;
default:
System.out.println("invalid");
break;
}
}
public static JPanel createHeaderPanel(String headerText, JLabel contentLabel) {
JPanel panel = new JPanel(new BorderLayout());
JLabel headerLabel = new JLabel(headerText);
headerLabel.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(headerLabel, BorderLayout.NORTH);
panel.add(contentLabel, BorderLayout.CENTER);
return panel;
}
public static void main(String[] args) {
ImageDisplay ren = new ImageDisplay();
ren.showIms(args);
}
}