-
Notifications
You must be signed in to change notification settings - Fork 0
/
papier.java
99 lines (81 loc) · 2.2 KB
/
papier.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
import java.net.*;
import java.awt.Color;
import java.io.*;
import java.lang.System;
import java.awt.Image;
import java.awt.image.*;
import java.util.Random;
import javax.imageio.ImageIO;
class ImageWriter implements Runnable {
private BufferedImage img;
private int width;
private int height;
private int from;
private int to;
private Writer out;
private Random rand;
ImageWriter(String server, int port, BufferedImage img, int from, int to) {
try {
connect(server, port);
} catch (Exception e) {
System.out.println(e);
}
this.img = img;
this.width = img.getWidth();
this.height = img.getHeight();
this.from = from;
this.to = to;
this.out = out;
this.rand = new Random();
}
private final static String rgbToHex(int rgb) throws NullPointerException {
String hexColour = Integer.toHexString(rgb & 0xffffff);
if (hexColour.length() < 6) {
hexColour = "000000".substring(0, 6 - hexColour.length()) + hexColour;
}
return hexColour;
}
private void connect(String server, int port) throws IOException {
Socket s = new Socket(server, port);
OutputStream tmp = s.getOutputStream();
BufferedOutputStream bufout = new BufferedOutputStream(tmp);
out = new PrintWriter(bufout);
}
@Override
public void run() {
int x;
int y;
while (true) {
for (y = from; y < to; y++) {
for (x = 0; x < width; x++) {
try {
out.write("PX " + x + " " + y + " " + rgbToHex(img.getRGB(x, y)) + "\n");
} catch (Exception e) {}
}
}
}
}
}
public class papier {
public static void main(String[] args) throws InterruptedException, IOException {
if (args.length != 4) {
System.out.println("Usage: " + "papier <server> <port> <image> <threads>");
System.exit(1);
}
String server = args[0];
int port = Integer.valueOf(args[1]);
BufferedImage image = ImageIO.read(new File(args[2]));
int height = image.getHeight();
int threads = Integer.valueOf(args[3]);
Thread t[] = new Thread[threads];
int zone = height/threads;
for (int i = 0; i < threads; i++) {
ImageWriter writer = new ImageWriter(server, port, image, i*zone, (i+1)*zone);
t[i] = new Thread(writer);
t[i].start();
}
for (int i = 0; i < threads; i++) {
t[i].join();
}
}
}