-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnstitcher.java
More file actions
174 lines (138 loc) · 6.42 KB
/
Unstitcher.java
File metadata and controls
174 lines (138 loc) · 6.42 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
import java.io.*;
import java.util.*;
import java.util.List;
import java.awt.*;
import java.awt.image.DataBuffer;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Unstitcher {
public static void main(String[] args) throws IOException {
List<String> files = new ArrayList<String>();
List<String> filesname = new ArrayList<String>();
// find all files in folder and find duplicate names based on files type
File folder = new File("./");
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
} else {
String filetmp = fileEntry.getName();
if (Objects.equals(Unstitcher.getFileExtension(filetmp), "config")) {
files.add(filetmp.substring(0, filetmp.length() - 7));
} else if (Objects.equals(Unstitcher.getFileExtension(filetmp), "png")) {
if (files.contains(filetmp.substring(0, filetmp.length() - 4))) {
filesname.add(filetmp.substring(0, filetmp.length() - 4));
}
}
}
}
unstich(filesname);
}
// unstiching loop per file set
public static void unstich(List<String> names) throws IOException {
int[] change = {0};
int[] newfile = {0};
int count = 0;
for (int x = 0; x < names.size(); x++) {
String line = null;
BufferedImage img = ImageIO.read(new File(names.get(x) + ".png"));
BufferedReader config = new BufferedReader(new FileReader(names.get(x) + ".config"));
System.out.println(names.get(x) + ".config being read");
int spritesize = img.getHeight()/16;
// firstline directory create
String[] firstline = config.readLine().split("=");
String dir = "." + firstline[1].substring(0, firstline[1].length() - 1);
String dirhold = dir;
File theDir = new File(dir);
theDir.mkdirs();
count++;
System.out.println("line " + count + " read");
// unstiching
while ((line = config.readLine()) != null) {
if ((line.length() < 4) || (line.charAt(0) == '*')) {
count++;
System.out.println("line " + count +" skiped");
}
else if ((line.charAt(0) == '-')) {
count++;
System.out.println("line " + count + " read");
// split line
String[] tmpline = line.split("=");
String[] tmppath = tmpline[1].split("/");
String[] rcstring = tmpline[0].split(",");
String[] rcstringstart = rcstring[0].split("\\.");
String[] rcstringend = rcstring[1].split("\\.");
// setup locations
int[] rc = {((Integer.parseInt(rcstringstart[0].substring(1, rcstringstart[0].length())) - 1) * spritesize),
((Integer.parseInt(rcstringstart[1]) - 1) * spritesize)};
int[] hw = {(Integer.parseInt(rcstringend[0]) * spritesize), (Integer.parseInt(rcstringend[1]) * spritesize)};
output(dir, rc, tmppath, img, (hw[1]-rc[1]), (hw[0]-rc[0]), change, newfile);
} else {
count++;
System.out.println("line " + count + " read");
// split line
String[] tmpline = line.split("=");
String[] tmppath = tmpline[1].split("/");
String[] rcstring = tmpline[0].split("\\.");
// setup locations
int[] rc = {((Integer.parseInt(rcstring[0]) - 1) * spritesize), ((Integer.parseInt(rcstring[1]) - 1) * spritesize)};
output(dir, rc, tmppath, img, spritesize, spritesize, change, newfile);
}
}
count = 0;
}
System.out.println(change[0] + " change(s)");
System.out.println(newfile[0] + " new file(s)");
}
// output function
public static void output(String dir, int[] rc, String[] path, BufferedImage img, int spriteh, int spritew, int[] change, int[] newfile) throws IOException {
// output folder(s)
for (int i = 0; i < (path.length - 1); i++) {
dir = dir + "/" + path[i];
}
File theSubDir = new File(dir);
theSubDir.mkdirs();
// setup output image for transparency
BufferedImage tmpimg = new BufferedImage(spriteh, spritew, Transparency.BITMASK);
// split and output images
Graphics2D sprite = tmpimg.createGraphics();
sprite.drawImage(img.getSubimage(rc[1], rc[0], spriteh, spritew),0,0, null);
// does exsist
boolean check = new File(dir, path[path.length - 1] + ".png").exists();
if(check) {
BufferedImage isthere = ImageIO.read(new File(dir + "/" + path[path.length - 1] + ".png"));
if (bufferedImagesEqual(isthere, tmpimg)) {
// no change
System.out.println("no change");
} else {
// changed
ImageIO.write(tmpimg, "png", new File(dir + "/" + path[path.length - 1] + ".png"));
System.out.println("change");
change[0]++;
}
} else {
// new file
ImageIO.write(tmpimg, "png", new File(dir + "/" + path[path.length - 1] + ".png"));
System.out.println("new file");
newfile[0]++;
}
}
// get file type
public static String getFileExtension(String fullName) {
String fileName = new File(fullName).getName();
int dotIndex = fileName.lastIndexOf('.');
return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1);
}
// is buffered image same
public static boolean bufferedImagesEqual(BufferedImage img1, BufferedImage img2) {
if (img1.getWidth() == img2.getWidth() && img1.getHeight() == img2.getHeight()) {
for (int x = 0; x < img1.getWidth(); x++) {
for (int y = 0; y < img1.getHeight(); y++) {
if (img1.getRGB(x, y) != img2.getRGB(x, y))
return false;
}
}
} else {
return false;
}
return true;
}
}