Created
February 8, 2020 01:23
-
-
Save duskvirkus/4d77c5d1f65862af6a780edbd4c4f794 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int order = 10; | |
| int N = int(pow(2, order)); | |
| int total = N * N; | |
| PVector[] path = new PVector[total]; | |
| PImage img; | |
| void setup() { | |
| size(1024, 1024); | |
| background(0); | |
| for (int i = 0; i < total; i++) { | |
| path[i] = hilbert(i); | |
| float len = width / N; | |
| path[i].mult(len); | |
| path[i].add(len/2, len/2); | |
| } | |
| img = loadImage("cabana_square.jpg"); | |
| img.loadPixels(); | |
| assert(img.pixels.length == path.length); | |
| int error = 0; | |
| for (int i = 0; i < path.length; i++) { | |
| int index = int(path[i].x) + int(path[i].y) * width; | |
| int bOriginal = int(brightness(img.pixels[index])); | |
| int b = bOriginal; | |
| b += error; | |
| b /= 255; | |
| if (b == 1) { | |
| error = error - (255 - bOriginal); | |
| } else { | |
| error = error + bOriginal; | |
| } | |
| //b = 1 - b; | |
| b *= 255; | |
| img.pixels[index] = color(b, 255); | |
| } | |
| img.updatePixels(); | |
| image(img, 0, 0); | |
| img.save("out.jpg"); | |
| println("done"); | |
| noLoop(); | |
| } | |
| PVector hilbert(int i) { | |
| PVector[] points = { | |
| new PVector(0, 0), | |
| new PVector(0, 1), | |
| new PVector(1, 1), | |
| new PVector(1, 0) | |
| }; | |
| int index = i & 3; | |
| PVector v = points[index]; | |
| for (int j = 1; j < order; j++) { | |
| i = i >>> 2; | |
| index = i & 3; | |
| float len = pow(2, j); | |
| if (index == 0) { | |
| float temp = v.x; | |
| v.x = v.y; | |
| v.y = temp; | |
| } else if (index == 1) { | |
| v.y += len; | |
| } else if (index == 2) { | |
| v.x += len; | |
| v.y += len; | |
| } else if (index == 3) { | |
| float temp = len - 1 - v.x; | |
| v.x = len - 1 - v.y; | |
| v.y = temp; | |
| v.x += len; | |
| } | |
| } | |
| return v; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int order = 10; | |
| int N = int(pow(2, order)); | |
| int total = N * N; | |
| PVector[] path = new PVector[total]; | |
| PImage img; | |
| void setup() { | |
| size(1024, 1024); | |
| background(0); | |
| for (int i = 0; i < total; i++) { | |
| path[i] = hilbert(i); | |
| float len = width / N; | |
| path[i].mult(len); | |
| path[i].add(len/2, len/2); | |
| } | |
| img = loadImage("cabana_square.jpg"); | |
| img.loadPixels(); | |
| assert(img.pixels.length == path.length); | |
| int error = 0; | |
| for (int i = 0; i < path.length; i++) { | |
| int index = int(path[i].x) + int(path[i].y) * width; | |
| int bOriginal = int(red(img.pixels[index])); | |
| int b = bOriginal; | |
| b += error; | |
| b /= 255; | |
| if (b == 1) { | |
| error = error - (255 - bOriginal); | |
| } else { | |
| error = error + bOriginal; | |
| } | |
| b *= 255; | |
| img.pixels[index] = color(b, green(img.pixels[index]), blue(img.pixels[index]), 255); | |
| } | |
| img.updatePixels(); | |
| error = 0; | |
| for (int i = 0; i < path.length; i++) { | |
| int index = int(path[i].x) + int(path[i].y) * width; | |
| int bOriginal = int(green(img.pixels[index])); | |
| int b = bOriginal; | |
| b += error; | |
| b /= 255; | |
| if (b == 1) { | |
| error = error - (255 - bOriginal); | |
| } else { | |
| error = error + bOriginal; | |
| } | |
| b *= 255; | |
| img.pixels[index] = color(red(img.pixels[index]), b, blue(img.pixels[index]), 255); | |
| } | |
| img.updatePixels(); | |
| error = 0; | |
| for (int i = 0; i < path.length; i++) { | |
| int index = int(path[i].x) + int(path[i].y) * width; | |
| int bOriginal = int(blue(img.pixels[index])); | |
| int b = bOriginal; | |
| b += error; | |
| b /= 255; | |
| if (b == 1) { | |
| error = error - (255 - bOriginal); | |
| } else { | |
| error = error + bOriginal; | |
| } | |
| b *= 255; | |
| img.pixels[index] = color(red(img.pixels[index]), green(img.pixels[index]), b, 255); | |
| } | |
| img.updatePixels(); | |
| image(img, 0, 0); | |
| img.save("out.jpg"); | |
| println("done"); | |
| noLoop(); | |
| } | |
| PVector hilbert(int i) { | |
| PVector[] points = { | |
| new PVector(0, 0), | |
| new PVector(0, 1), | |
| new PVector(1, 1), | |
| new PVector(1, 0) | |
| }; | |
| int index = i & 3; | |
| PVector v = points[index]; | |
| for (int j = 1; j < order; j++) { | |
| i = i >>> 2; | |
| index = i & 3; | |
| float len = pow(2, j); | |
| if (index == 0) { | |
| float temp = v.x; | |
| v.x = v.y; | |
| v.y = temp; | |
| } else if (index == 1) { | |
| v.y += len; | |
| } else if (index == 2) { | |
| v.x += len; | |
| v.y += len; | |
| } else if (index == 3) { | |
| float temp = len - 1 - v.x; | |
| v.x = len - 1 - v.y; | |
| v.y = temp; | |
| v.x += len; | |
| } | |
| } | |
| return v; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment