This repository was archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTUCSDevCompExample2012.java
More file actions
175 lines (161 loc) · 3.96 KB
/
TUCSDevCompExample2012.java
File metadata and controls
175 lines (161 loc) · 3.96 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
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
import java.awt.Color;
import java.util.Vector;
import java.awt.Rectangle;
import java.awt.Point;
import java.io.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
public class TUCSDevCompExample2012
{
public static final Color CREEPER_COLOUR = new Color(73, 156, 64);
public static void main(String[] args)
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
boolean output;
if (args.length == 2 && args[0].equals("--output"))
{
TUCSDevCompExample2012.processInput(input, null, args[1]);
}
else if (args.length == 6 && args[0].equals("--output"))
{
Rectangle bounds;
Point p1, p2;
p1 = new Point(Integer.parseInt(args[2]), Integer.parseInt(args[3]));
p2 = new Point(Integer.parseInt(args[4]), Integer.parseInt(args[5]));
bounds = new Rectangle(p1);
bounds.add(p2);
TUCSDevCompExample2012.processInput(input, bounds, args[1]);
}
else
{
TUCSDevCompExample2012.processInput(input, null, null);
}
}
public static void processInput(BufferedReader input, Rectangle bounds, String output)
{
int size[] = new int[2];
Vector<Color> colours;
Point point;
colours = TUCSDevCompExample2012.readImage(input, size);
if (bounds != null)
{
if (output != null)
{
processOutput(output, colours, size[0], size[1], bounds);
}
}
else
{
for (int i=0; i<colours.size(); ++i)
{
point = new Point(i%size[0], i/size[0]);
if (compareColours(colours.get(i), TUCSDevCompExample2012.CREEPER_COLOUR, 0.02f))
{
if (bounds == null) bounds = new Rectangle(point); else bounds.add(point);
}
}
if (bounds != null)
{
if (output != null)
{
processOutput(output, colours, size[0], size[1], bounds);
}
System.out.print("" + (int)(bounds.getX()));
System.out.print(" " + (int)(bounds.getY()));
System.out.print(" " + (int)(bounds.getX()+bounds.getWidth()));
System.out.print(" " + (int)(bounds.getY()+bounds.getHeight()));
System.out.print("\n");
}
}
}
public static boolean compareColours(Color a, Color b, float threshold)
{
float ca[], cb[], dr, dg, db, aa, ab;
ca = a.getRGBColorComponents(null); cb = b.getRGBColorComponents(null);
aa = (ca[0] + ca[1] + ca[2]) / 3.0f;
ab = (cb[0] + cb[1] + cb[2]) / 3.0f;
for (int i=0; i<3; ++i) {ca[i] = (ca[i]-aa)/aa; cb[i] = (cb[i]-ab)/ab;}
return ((dr=ca[0]-cb[0])*dr + (dg=ca[1]-cb[1])*dg + (db=ca[2]-cb[2])*db) < threshold;
}
public static Vector<Color> readImage(BufferedReader input, int size[])
{
Vector<Color> colours = new Vector<Color>();
char hex_rgb[] = new char[6];
int x=0, y=0, c;
do
{
try
{
input.read(hex_rgb, 0, 6);
if (x == 0 && hex_rgb[0] == '\n')
{
size[1] = y;
break;
}
colours.add(new Color(Integer.parseInt(new String(hex_rgb), 16)));
c = input.read();
}
catch (java.io.IOException e)
{
System.err.println("Failed to read colour: " + e);
break;
}
if (c == ' ')
{
++x;
}
else if (c == '\n')
{
size[0] = x+1;
x = 0;
++y;
}
else
{
System.err.println("Unexpected input: '" + c + " at ("+x+","+y+")'!");
break;
}
}
while (true);
return colours;
}
public static void processOutput(String output, Vector<Color> colours,
int width, int height, Rectangle bounds)
{
int x, y, i, rgb, r, g, b;
BufferedImage image;
Point p;
Color c;
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (i=0,y=0; y<height; ++y)
{
for (x=0; x<width; ++x,++i)
{
p = new Point(x, y);
c = colours.get(i);
r = c.getRed();
g = c.getGreen();
b = c.getBlue();
if (!bounds.contains(p))
{
r /= 2;
g /= 2;
b /= 2;
}
rgb = (r<<16)|(g<< 8)|(b<< 0);
image.setRGB(x, y, rgb);
}
}
try
{
if (!ImageIO.write(image, "png", new File(output)))
{
System.err.println("unable to write png!");
}
}
catch (java.io.IOException e)
{
System.err.println("failed! ("+e+")");
}
}
};