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 pathCreepEncodeImage.java
More file actions
99 lines (90 loc) · 2.01 KB
/
CreepEncodeImage.java
File metadata and controls
99 lines (90 loc) · 2.01 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
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.*;
public class CreepEncodeImage
{
static final String COMMAND_USAGE = "Usage: java CreepEncodeImage [filename ...]";
static final String INPUT_INSTRUCTIONS = "Enter file names or \"q\" to exit.";
public static void main(String[] args)
{
if (args.length > 0)
{
for (int i=0; i<args.length; ++i)
{
processFile(args[i]);
}
}
else
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String filename;
System.out.println(CreepEncodeImage.COMMAND_USAGE);
System.out.println(CreepEncodeImage.INPUT_INSTRUCTIONS);
do
{
try
{
filename = input.readLine();
if (filename.equals("q"))
break;
}
catch (java.io.IOException e)
{
System.err.println("Failed to read line: " + e);
break;
}
if (!CreepEncodeImage.processFile(filename))
{
System.out.println(CreepEncodeImage.INPUT_INSTRUCTIONS);
}
}
while (true);
}
}
public static boolean processFile(String filename)
{
BufferedImage image;
PrintWriter output;
int w, h, x, y, i;
int pixels[];
String hex;
try
{
image = ImageIO.read(new File(filename));
}
catch (java.io.IOException e)
{
System.err.println("Failed to open '" + filename + "': " + e);
return false;
}
w = image.getWidth();
h = image.getHeight();
System.out.print("Processing " + filename + "...");
pixels = image.getRGB(0,0,w,h,null,0,w);
try
{
output = new PrintWriter(new FileWriter(filename+".creep"));
for (i=0,y=0; y<h; ++y)
{
for (x=0; x<w; ++x, ++i)
{
hex = Integer.toHexString(pixels[i] & 0xFFFFFF);
while (hex.length()<6) hex = "0" + hex;
output.print("" + hex);
output.print((x+1<w)?" ":"\n");
}
}
output.print("\n");
output.flush();
output.close();
output = null;
System.out.println("done");
}
catch (java.io.IOException e)
{
System.err.println("failed! ("+e+")");
return false;
}
return true;
}
};