-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageDisplay.java
135 lines (105 loc) · 4.52 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
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import javax.swing.*;
public class ImageDisplay {
private JFrame frame;
private JLabel lbIm1;
private JLabel lbIm2;
private BufferedImage image;
private InputStream inputStream;
public static int RGB_FACTOR = 3;
private int width = 960;
private int height = 540;
private int width_o = width;
private int height_o = height;
double factor_w = 1;
double factor_h = 1;
public void showIms(String[] args) {
// Input arguments from command line .
factor_w = Double.parseDouble(args[1]);
factor_h = Double.parseDouble(args[2]);
width = (int) (width * factor_w);
height = (int) (height * factor_h);
long fps = Long.parseLong(args[3]);
frame = new JFrame();
GridBagLayout gLayout = new GridBagLayout();
frame.getContentPane().setLayout(gLayout);
JLabel lbText1 = new JLabel("Video height" + height + " width" + width);
lbText1.setHorizontalAlignment(SwingConstants.CENTER);
ArrayList<ImageIcon> images = new ArrayList<ImageIcon>();
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = GridBagConstraints.CENTER;
gridBagConstraints.weightx = 0.5;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
frame.getContentPane().add(lbText1, gridBagConstraints);
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
// Taking input parameters for display.
try {
File file = new File(args[0]);
inputStream = new FileInputStream(file);
long len = file.length();
byte[] bytes = new byte[(int) len];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead = inputStream.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
int count = 0;
while (count == 0) {
count++;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println();
int numberOfFrames = (int) (len / (RGB_FACTOR * width * width));
for (int index = 0; index < (numberOfFrames); index++) {
long lStartTime = System.currentTimeMillis();
int ind = (int) ((RGB_FACTOR * width_o * height_o * index));
for (int yAxis = 0; yAxis < height; yAxis++) {
for (int xAxis = 0; xAxis < width; xAxis++) {
byte red = bytes[ind];
//Shift on ce length to get green array
byte green = bytes[ind + height_o * width_o];
//Shift twice length to get blue array
byte blue = bytes[ind + height_o * width_o * 2];
int pixValue = 0xff000000 | ((red & 0xff) << 16) | ((green & 0xff) << 8) | (blue & 0xff);
image.setRGB(xAxis, yAxis, pixValue);
ind = ind + 2;
}
}
ImageIcon image = new ImageIcon(this.image);
images.add(image);
long lEndTime = System.currentTimeMillis();
displayFrame(image, gridBagConstraints);
try {
long delay = (long) ((1000.0 / fps) - (lEndTime - lStartTime));
Thread.sleep((delay > 0) ? delay : 0);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Display per frame.
private void displayFrame(ImageIcon image, GridBagConstraints c) {
lbIm1 = new JLabel(image);
frame.getContentPane().add(lbIm1, c);
frame.pack();
frame.setVisible(true);
}
// Main function.
public static void main(String[] args) {
ImageDisplay ren = new ImageDisplay();
ren.showIms(args);
}
}