-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
256 lines (214 loc) · 8.86 KB
/
Copy pathMain.java
File metadata and controls
256 lines (214 loc) · 8.86 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package com.company;
import com.company.ColorPlacementAlgorithms.*;
import com.company.Sorters.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.FileAlreadyExistsException;
import java.util.*;
public class Main {
// Width and height of the image
/** Common numbers of colors and dimensions:
* 256 : 4096x4096
* 192 : 3456x2048
* 128 : 2048 x 1024
* 64 : 512 x 512
*/
final static int NUM_COLORS = 64;
// final static int NUM_COLORS = 128;
final static int MAX_COLORS = 256;
public final static int WIDTH = 1024;
public final static int HEIGHT = 256;
// public final static int WIDTH = 2048;
// public final static int HEIGHT = 1024;
// Starting coordinates for first pixel
public final static int START_X = WIDTH / 3 - 1;
public final static int START_Y = HEIGHT / 4 - 1;
public final static int SEED = 200;//new Random().nextInt();
private static ColorMetric algoMetric = new
ColorDistanceMetrics.sumSqRGBDist();
private final static String WEIGHTING_NAME = "-LnQd";
private final static int NUM_FRAMES = 50;
private static Comparer SORTER = new ColorComparator("grb");
// private static Comparer SORTER = new HueComparator();
/*
private static NeighborDistanceAlgorithm.neighborMetric ngbhrMetric =
NeighborDistanceAlgorithm.neighborMetric.MIN;
private static AlgorithmFactory.distanceMetric dist =
AlgorithmFactory.distanceMetric.sumRGBDist;
private static Algorithm ALGORITHM = AlgorithmFactory.getAlgorithm(dist, ngbhrMetric, ngbhrPref);
*/
private static NeighborPreference ngbhrPref =
new NeighborPreference(1, NeighborPreference.exponent.N_TWO, NeighborPreference.preferenceKind.MULTIPLICATIVE);
/*** End configuration parameters ***/
public static Pixel[] Image;
static Random rand = new Random(SEED);
private static final int[] NEIGH_X = {-1, 0, 1, -1, 1, -1, 0, 1};
private static final int[] NEIGH_Y = {-1, -1, -1, 0, 0, 1, 1, 1};
private static Algorithm ALGORITHM = new GenericAlgorithm(algoMetric);
// TODO move these functions to FileUtility class or some such class
static BufferedImage exportImg(Pixel[] Image, BufferedImage img){
for (int x = 0; x < WIDTH; x++) {
for (int y = 0; y < HEIGHT; y++) {
Color rgb = Image[y * WIDTH + x].color;
if (rgb == null) {
rgb = Color.black;
}
img.setRGB(x, y, rgb.getRGB());
}
}
return img;
}
public static String getFileBasename(){
return "imgrgb_" + ALGORITHM.getName() +
"_" + SORTER.getName() + "_" +
START_X + "x" + START_Y +
"s" + SEED + WEIGHTING_NAME;
}
private static boolean makeDirectory(String dirPath){
File file = new File(dirPath);
if (file.mkdir()) {
return true;
}
else {
System.err.println("Failed to create directory!");
return false;
}
}
// TODO make this generic
public static String getOutputDirectory(){
String basename = getFileBasename();
return "/home/sky/Desktop/" + basename + "/";
}
public static String getOutputFilename(String suffix){
return getOutputDirectory() + "img-" + suffix + ".png";
}
public static void saveImgToFile(BufferedImage img, String suffix) {
saveImgToFile(img, suffix, false);
}
public static void saveImgToFile(BufferedImage img,
String filename, boolean verbose) {
try {
File outputFile = new File(filename);
ImageIO.write(img, "png", outputFile);
if (verbose) System.out.println("Wrote to <" + outputFile + ">");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws FileAlreadyExistsException {
// TODO cmd-line args
System.out.println("Generating Colors...");
Color colors[] = new Color[WIDTH * HEIGHT];
for (int r = 0; r < NUM_COLORS; r++) {
int R = (r*255 / (NUM_COLORS - 1));
for (int g = 0; g < NUM_COLORS; g++) {
int G = (g*255 / (NUM_COLORS - 1));
for (int b = 0; b < NUM_COLORS; b++) {
int B = (b*255 / (NUM_COLORS - 1));
int ind = r*NUM_COLORS*NUM_COLORS + g*NUM_COLORS + b;
colors[ind] = new Color(R, G, B);
}
}
}
System.out.println("Sorting...");
// TODO Implement different sorting algos, learn java sorting syntax
Arrays.sort(colors, SORTER);
assert(colors.length == WIDTH * HEIGHT);
System.out.println("Randomizing weights...");
Image = new Pixel[WIDTH * HEIGHT];
int dupCount = 0;
HashSet<Integer> weights = new HashSet<>();
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
int weight = -1;
do {
if (weight != -1) dupCount++;
weight = rand.nextInt(Integer.MAX_VALUE);
} while (!weights.add(weight));
Image[y*WIDTH + x] = new Pixel(true, false, weight, -1, x, y);
}
}
System.out.println("Duplicate rns: " + dupCount + " of " + (WIDTH * HEIGHT) + " = " + (double)(dupCount) / ((double)(WIDTH*HEIGHT)));
// Precalculate neighbors of every pixel
System.out.println("Calculating neighbors...");
assert (NEIGH_X.length == NEIGH_Y.length);
ArrayList<Pixel> ng = new ArrayList<Pixel>();
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
ng.clear();
for (int i = 0; i < NEIGH_X.length; i++) {
int y2 = y + NEIGH_Y[i];
if (y2 < 0 || y2 == HEIGHT) {
continue;
}
int x2 = x + NEIGH_X[i];
if (x2 < 0 || x2 == WIDTH) {
continue;
}
ng.add(Image[y2*WIDTH + x2]);
}
Image[y*WIDTH + x].neighbors = ng.toArray(new Pixel[ng.size()]);
}
}
System.out.println("Initializing algorithm...");
ALGORITHM.init(WIDTH, HEIGHT);
BufferedImage img = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
long[] checkpoints = new long[NUM_FRAMES];
int checkpointIndex = 0;
for (int i = 0; i < NUM_FRAMES; i++) {
checkpoints[i] = (((long)(i+1)) * colors.length / NUM_FRAMES);
}
System.out.println("Creating output directory...");
if (!makeDirectory(getOutputDirectory())) {
throw new FileAlreadyExistsException(getOutputDirectory());
}
System.out.println("Assigning colors...");
for (int i = 0; i < colors.length; i++) {
if (i % (32*4096) == 0) {
System.out.println(((double) i * 100.0) / (WIDTH * HEIGHT) + "%...");
}
try {
ALGORITHM.place(colors[i]);
} catch (Exception e) {
e.printStackTrace();
}
// if ((float) i / colors.length >= .95) { break;}
if (i == checkpoints[checkpointIndex]) {
img = exportImg(Image, img);
saveImgToFile(img,
getOutputFilename(
String.format("%05d",
checkpointIndex)));
checkpointIndex++;
}
}
ALGORITHM.done();
System.out.println("Verifying colors used...");
boolean[] used = new boolean[MAX_COLORS*MAX_COLORS*MAX_COLORS];
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
Color pix = Image[y*WIDTH + x].color;
int r = pix.getRed();
int g = pix.getGreen();
int b = pix.getBlue();
try {
if (used[r * 256 * 256 + g * 256 + b]) {
System.out.println("Color " + r + "/" + g + "/" +
b + " is added more than once!");
} else {
used[r * 256 * 256 + g * 256 + b] = true;
}
} catch (Exception e) {
System.out.println(r + "/" + g + "/" + b);
System.out.println(r * 256 * 256 + g * 256 + b + " / " + used.length);
throw e;
}
}
}
img = exportImg(Image, img);
saveImgToFile(img, getOutputFilename("final"), true);
}
}