-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sample.java
executable file
·148 lines (120 loc) · 5.04 KB
/
Sample.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
import java.awt.*;
import java.awt.image.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
public class Sample {
JFrame frame;
JLabel lbIm1;
int[][][] imgOne;
int width = 512;
int height = 512;
double zoomFactor = 1.0;
double rotationAngle = 0.0;
public void readImageRGB(int width, int height, String imgPath) {
try {
int frameLength = width * height * 3;
File file = new File(imgPath);
RandomAccessFile raf = new RandomAccessFile(file, "r");
raf.seek(0);
long len = frameLength;
byte[] bytes = new byte[(int) len];
raf.read(bytes);
int ind = 0;
imgOne = new int[width][height][3];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
imgOne[x][y][0] = bytes[ind] & 0xFF; // Red
imgOne[x][y][1] = bytes[ind + height * width] & 0xFF; // Green
imgOne[x][y][2] = bytes[ind + height * width * 2] & 0xFF; // Blue
ind++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void showIms(String[] args) {
readImageRGB(width, height, args[0]);
double zzoomm = Double.parseDouble(args[1]);
double rotf = Double.parseDouble(args[2]);
int fps = 1000/Integer.parseInt(args[3]);
frame = new JFrame();
GridBagLayout gLayout = new GridBagLayout();
frame.getContentPane().setLayout(gLayout);
lbIm1 = new JLabel(new ImageIcon(getBufferedImage()));
Timer timer = new Timer(fps, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
zoomFactor *= zzoomm;
rotationAngle += rotf;
lbIm1.setIcon(new ImageIcon(getTransformedImage()));
frame.revalidate();
frame.repaint();
}
});
timer.start();
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
frame.getContentPane().add(lbIm1, c);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public BufferedImage getBufferedImage() {
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int r = imgOne[x][y][0];
int g = imgOne[x][y][1];
int b = imgOne[x][y][2];
int pixelValue = (255 << 24) | (r << 16) | (g << 8) | b;
bufferedImage.setRGB(x, y, pixelValue);
}
}
return bufferedImage;
}
public BufferedImage getTransformedImage() {
int[][][] transformedImage = new int[width][height][3];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// Apply zoom and rotation transformations
int originalX = (int) ((x - width / 2) / zoomFactor + width / 2);
int originalY = (int) ((y - height / 2) / zoomFactor + height / 2);
double radians = Math.toRadians(rotationAngle);
int rotatedX = (int) (Math.cos(radians) * (originalX - width / 2) - Math.sin(radians) * (originalY - height / 2) + width / 2);
int rotatedY = (int) (Math.sin(radians) * (originalX - width / 2) + Math.cos(radians) * (originalY - height / 2) + height / 2);
if (rotatedX >= 0 && rotatedX < width && rotatedY >= 0 && rotatedY < height) {
transformedImage[x][y][0] = imgOne[rotatedX][rotatedY][0];
transformedImage[x][y][1] = imgOne[rotatedX][rotatedY][1];
transformedImage[x][y][2] = imgOne[rotatedX][rotatedY][2];
}
}
}
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int r = transformedImage[x][y][0];
int g = transformedImage[x][y][1];
int b = transformedImage[x][y][2];
int pixelValue = (255 << 24) | (r << 16) | (g << 8) | b;
bufferedImage.setRGB(x, y, pixelValue);
}
}
return bufferedImage;
}
public static void main(String[] args) {
Sample ren = new Sample();
ren.showIms(args);
}
}