-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressor.java
More file actions
89 lines (65 loc) · 1.63 KB
/
Compressor.java
File metadata and controls
89 lines (65 loc) · 1.63 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
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Compressor {
static class Pixel {
int r, g, b, data;
public Pixel (int r, int g, int b) {
data = (r << 16) | (g << 8) | b;
}
void setPixel (int x, int y, BufferedImage img) {
img.setRGB(x, y, data);
}
}
static int countChars (String s) throws IOException {
int count = 0;
FileReader input = null;
try {
input = new FileReader(s);
while (input.read() != -1) {
count++;
}
} finally {
if (input != null)
input.close();
}
return count;
}
public static void main (String[] args) throws IOException {
FileReader input = null;
BufferedImage img = null;
int length;
try {
input = new FileReader(args[0]);
File output = new File(args[1]);
length = countChars(args[0]);
int n = ((int) Math.sqrt(length/3)) + 1;
img = new BufferedImage(n, n, BufferedImage.TYPE_INT_RGB);
int c1 = 0, c2 = 0, c3 = 0, endx = 0, endy = 0;
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
if ((c1 = input.read()) != -1 && (c2 = input.read()) != -1
&& (c3 = input.read()) != -1) {
(new Pixel(c1,c2,c3)).setPixel(x, y, img);
} else {
endx = x;
endy = y;
break;
}
}
if (endx != 0) break;
}
if (c2 == -1)
(new Pixel(c1,0,0)).setPixel(endx, endy, img);
else if (c3 == -1)
(new Pixel(c1,c2,0)).setPixel(endx, endy, img);
ImageIO.write(img, "PNG", output);
} finally {
if (input != null)
input.close();
}
}
}