-
Notifications
You must be signed in to change notification settings - Fork 122
/
ASDFPixelSort.pde
377 lines (310 loc) · 7.32 KB
/
ASDFPixelSort.pde
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/********
ASDF Pixel Sort
Kim Asendorf | 2010 | kimasendorf.com
Sorting modes
0 = white
1 = black
2 = bright
3 = dark
*/
int mode = 0;
// image path is relative to sketch directory
PImage img;
String imgFileName = "mountains";
String fileType = "jpg";
int loops = 1;
// threshold values to determine sorting start and end pixels
// using the absolute rgb value
// r*g*b = 255*255*255 = 16581375
// 0 = white
// -16581375 = black
// sort all pixels whiter than the threshold
int whiteValue = -12345678;
// sort all pixels blacker than the threshold
int blackValue = -3456789;
// using the brightness value
// sort all pixels brighter than the threshold
int brightValue = 127;
// sort all pixels darker than the threshold
int darkValue = 223;
int row = 0;
int column = 0;
boolean saved = false;
void setup() {
img = loadImage(imgFileName + "." + fileType);
// use only numbers (not variables) for the size() command, Processing 3
size(1, 1);
// allow resize and update surface to image dimensions
surface.setResizable(true);
surface.setSize(img.width, img.height);
// load image onto surface - scale to the available width,height for display
image(img, 0, 0, width, height);
}
void draw() {
if (frameCount <= loops) {
// loop through columns
println("Sorting Columns");
while (column < img.width-1) {
img.loadPixels();
sortColumn();
column++;
img.updatePixels();
}
// loop through rows
println("Sorting Rows");
while (row < img.height-1) {
img.loadPixels();
sortRow();
row++;
img.updatePixels();
}
}
// load updated image onto surface and scale to fit the display width and height
image(img, 0, 0, width, height);
if (!saved && frameCount >= loops) {
// save img
img.save(imgFileName + "_" + mode + ".png");
saved = true;
println("Saved frame " + frameCount);
// exiting here can interrupt file save, wait for user to trigger exit
println("Click or press any key to exit...");
}
}
void keyPressed() {
if (saved) {
System.exit(0);
}
}
void mouseClicked() {
if (saved) {
System.exit(0);
}
}
void sortRow() {
// current row
int y = row;
// where to start sorting
int x = 0;
// where to stop sorting
int xEnd = 0;
while (xEnd < img.width-1) {
switch (mode) {
case 0:
x = getFirstNoneWhiteX(x, y);
xEnd = getNextWhiteX(x, y);
break;
case 1:
x = getFirstNoneBlackX(x, y);
xEnd = getNextBlackX(x, y);
break;
case 2:
x = getFirstNoneBrightX(x, y);
xEnd = getNextBrightX(x, y);
break;
case 3:
x = getFirstNoneDarkX(x, y);
xEnd = getNextDarkX(x, y);
break;
default:
break;
}
if (x < 0) break;
int sortingLength = xEnd-x;
color[] unsorted = new color[sortingLength];
color[] sorted = new color[sortingLength];
for (int i = 0; i < sortingLength; i++) {
unsorted[i] = img.pixels[x + i + y * img.width];
}
sorted = sort(unsorted);
for (int i = 0; i < sortingLength; i++) {
img.pixels[x + i + y * img.width] = sorted[i];
}
x = xEnd+1;
}
}
void sortColumn() {
// current column
int x = column;
// where to start sorting
int y = 0;
// where to stop sorting
int yEnd = 0;
while (yEnd < img.height-1) {
switch (mode) {
case 0:
y = getFirstNoneWhiteY(x, y);
yEnd = getNextWhiteY(x, y);
break;
case 1:
y = getFirstNoneBlackY(x, y);
yEnd = getNextBlackY(x, y);
break;
case 2:
y = getFirstNoneBrightY(x, y);
yEnd = getNextBrightY(x, y);
break;
case 3:
y = getFirstNoneDarkY(x, y);
yEnd = getNextDarkY(x, y);
break;
default:
break;
}
if (y < 0) break;
int sortingLength = yEnd-y;
color[] unsorted = new color[sortingLength];
color[] sorted = new color[sortingLength];
for (int i = 0; i < sortingLength; i++) {
unsorted[i] = img.pixels[x + (y+i) * img.width];
}
sorted = sort(unsorted);
for (int i = 0; i < sortingLength; i++) {
img.pixels[x + (y+i) * img.width] = sorted[i];
}
y = yEnd+1;
}
}
// white x
int getFirstNoneWhiteX(int x, int y) {
while (img.pixels[x + y * img.width] < whiteValue) {
x++;
if (x >= img.width) return -1;
}
return x;
}
int getNextWhiteX(int x, int y) {
x++;
while (img.pixels[x + y * img.width] > whiteValue) {
x++;
if (x >= img.width) return img.width-1;
}
return x-1;
}
// black x
int getFirstNoneBlackX(int x, int y) {
while (img.pixels[x + y * img.width] > blackValue) {
x++;
if (x >= img.width) return -1;
}
return x;
}
int getNextBlackX(int x, int y) {
x++;
while (img.pixels[x + y * img.width] < blackValue) {
x++;
if (x >= img.width) return img.width-1;
}
return x-1;
}
// bright x
int getFirstNoneBrightX(int x, int y) {
while (brightness(img.pixels[x + y * img.width]) < brightValue) {
x++;
if (x >= img.width) return -1;
}
return x;
}
int getNextBrightX(int x, int y) {
x++;
while (brightness(img.pixels[x + y * img.width]) > brightValue) {
x++;
if (x >= img.width) return img.width-1;
}
return x-1;
}
// dark x
int getFirstNoneDarkX(int x, int y) {
while (brightness(img.pixels[x + y * img.width]) > darkValue) {
x++;
if (x >= img.width) return -1;
}
return x;
}
int getNextDarkX(int x, int y) {
x++;
while (brightness(img.pixels[x + y * img.width]) < darkValue) {
x++;
if (x >= img.width) return img.width-1;
}
return x-1;
}
// white y
int getFirstNoneWhiteY(int x, int y) {
if (y < img.height) {
while (img.pixels[x + y * img.width] < whiteValue) {
y++;
if (y >= img.height) return -1;
}
}
return y;
}
int getNextWhiteY(int x, int y) {
y++;
if (y < img.height) {
while (img.pixels[x + y * img.width] > whiteValue) {
y++;
if (y >= img.height) return img.height-1;
}
}
return y-1;
}
// black y
int getFirstNoneBlackY(int x, int y) {
if (y < img.height) {
while (img.pixels[x + y * img.width] > blackValue) {
y++;
if (y >= img.height) return -1;
}
}
return y;
}
int getNextBlackY(int x, int y) {
y++;
if (y < img.height) {
while (img.pixels[x + y * img.width] < blackValue) {
y++;
if (y >= img.height) return img.height-1;
}
}
return y-1;
}
// bright y
int getFirstNoneBrightY(int x, int y) {
if (y < img.height) {
while (brightness(img.pixels[x + y * img.width]) < brightValue) {
y++;
if (y >= img.height) return -1;
}
}
return y;
}
int getNextBrightY(int x, int y) {
y++;
if (y < img.height) {
while (brightness(img.pixels[x + y * img.width]) > brightValue) {
y++;
if (y >= img.height) return img.height-1;
}
}
return y-1;
}
// dark y
int getFirstNoneDarkY(int x, int y) {
if (y < img.height) {
while (brightness(img.pixels[x + y * img.width]) > darkValue) {
y++;
if (y >= img.height) return -1;
}
}
return y;
}
int getNextDarkY(int x, int y) {
y++;
if (y < img.height) {
while (brightness(img.pixels[x + y * img.width]) < darkValue) {
y++;
if (y >= img.height) return img.height-1;
}
}
return y-1;
}