-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIconMaker.java
More file actions
218 lines (175 loc) · 8.03 KB
/
IconMaker.java
File metadata and controls
218 lines (175 loc) · 8.03 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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 IconMaker {
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(IconMaker.getFileExtension(filetmp), "config")) {
files.add(filetmp.substring(0, filetmp.length() - 7));
} else if (Objects.equals(IconMaker.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);
File theDir = new File(dir);
theDir.mkdirs();
count++;
System.out.println("line " + count + " read");
List<String> colorcodes = new ArrayList<String>();
List<String> colornames = new ArrayList<String>();
// get colors
while ((line = config.readLine()) != null) {
if ((line.length() < 4) || (line.charAt(0) == '*')) {
count++;
System.out.println("line " + line +" skiped");
} else if (line.charAt(0) == '#') {
count++;
System.out.println("line " + line + " read");
// split line
String[] tmpline = line.split("=");
colorcodes.add(tmpline[0]);
colornames.add(tmpline[1]);
// unstiching
} else if ((line.length() < 4) || (line.charAt(0) == '*')) {
count++;
System.out.println("line " + line +" skiped");
} else {
count++;
System.out.println("line " + line + " 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, colorcodes, colornames);
}
}
count = 0;
}
System.out.println(change[0] + " change(s)");
System.out.println(newfile[0] + " new file(s)");
}
// output function
public static void output(String basedir, int[] rc, String[] basepath, BufferedImage img, int spriteh, int spritew, int[] change, int[] newfile, List<String> ccodes, List<String> cnames) throws IOException {
// for all colors
for (int x = 0; x < cnames.size(); x++) {
String[] path = Arrays.copyOf(basepath, basepath.length);
String dir = basedir;
// replace <#>
for (int i = 0; i < path.length; i++) {
path[i] = path[i].replace("<#>", cnames.get(x));
System.out.println(cnames.get(x));
System.out.println(Arrays.toString(path));
}
// output folder(s)
for (int i = 0; i < (path.length - 1); i++) {
dir = dir + "/" + path[i];
System.out.println(dir);
}
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);
tmpimg = multiplyImageColors(tmpimg, Color.decode(ccodes.get(x)));
// 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]++;
}
}
}
// Multiply image by color
public static BufferedImage multiplyImageColors(BufferedImage image, Color colorMultiplier) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// Get color multiplier components
float rMult = colorMultiplier.getRed() / 255f;
float gMult = colorMultiplier.getGreen() / 255f;
float bMult = colorMultiplier.getBlue() / 255f;
// Process each pixel
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pixel = image.getRGB(x, y);
Color originalColor = new Color(pixel, true);
// Multiply RGB values
int newRed = Math.min(255, (int) (originalColor.getRed() * rMult));
int newGreen = Math.min(255, (int) (originalColor.getGreen() * gMult));
int newBlue = Math.min(255, (int) (originalColor.getBlue() * bMult));
// Preserve alpha
int newAlpha = originalColor.getAlpha();
// Set new pixel color
Color newColor = new Color(newRed, newGreen, newBlue, newAlpha);
newImage.setRGB(x, y, newColor.getRGB());
}
}
return newImage;
}
// 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;
}
}