Last active
August 29, 2015 14:05
-
-
Save 2bbb/4ca75d42c0763690959b to your computer and use it in GitHub Desktop.
PerfumeWorldImageImporter
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
| final String fileName = "penrose.png"; | |
| final String outJsonName = "out.json"; | |
| final int voxelsWidth = 80; | |
| final int voxelsHeight = 60; | |
| class Voxel { | |
| public Voxel(int x_, int y_) { | |
| x = x_; | |
| y = y_; | |
| } | |
| public int x; | |
| public int y; | |
| public int r; | |
| public int g; | |
| public int b; | |
| } | |
| ArrayList<Voxel> voxels = new ArrayList<Voxel>(voxelsWidth * voxelsHeight); | |
| PImage image = loadImage(fileName); | |
| int dstWidth = 0; | |
| int dstHeight = 0; | |
| if(abs(image.width * 3 - image.height * 4) < 10) { | |
| println("p1"); | |
| dstWidth = voxelsWidth; | |
| dstHeight = voxelsHeight; | |
| } else if(image.width * 3 < image.height * 4) { | |
| println("p2"); | |
| dstWidth = (int)((float)voxelsHeight * image.width / image.height); | |
| dstHeight = voxelsHeight; | |
| } else { | |
| println("p3"); | |
| dstWidth = voxelsWidth; | |
| dstHeight = (int)((float)voxelsWidth * image.height / image.width); | |
| } | |
| println(dstWidth, dstHeight); | |
| image.resize(dstWidth, dstHeight); | |
| image.loadPixels(); | |
| int offsetX = (voxelsWidth - dstWidth) / 2; | |
| int offsetY = (voxelsHeight - dstHeight) / 2; | |
| for(int j = 0; j < dstHeight; j++) { | |
| for(int i = 0; i < dstWidth; i++) { | |
| Voxel v = new Voxel(i + offsetX, j + offsetY); | |
| color c = image.pixels[i + j * dstWidth]; | |
| v.r = ((c >> 16) & 255); | |
| v.g = ((c >> 8) & 255); | |
| v.b = (c & 255); | |
| voxels.add(v); | |
| } | |
| } | |
| import java.util.Date; | |
| JSONObject obj = new JSONObject(); | |
| try { | |
| Date d = new Date(); | |
| obj.setLong("updatedAt", d.getTime()); | |
| obj.setInt("version", 1); | |
| JSONArray arr = new JSONArray(); | |
| for(int n = 0; n < voxels.size(); n++) { | |
| JSONObject o = new JSONObject(); | |
| Voxel v = voxels.get(n); | |
| o.setInt("id", n); | |
| o.setInt("color", v.r * 65536 + v.g * 256 + v.b); | |
| o.setInt("x", v.x); | |
| o.setInt("y", 0); | |
| o.setInt("z", v.y); | |
| o.setInt("w", 1); | |
| o.setInt("h", 1); | |
| o.setInt("d", 1); | |
| arr.append(o); | |
| } | |
| obj.setJSONArray("voxels", arr); | |
| saveJSONObject(obj, outJsonName); | |
| } catch(Exception e) { | |
| } | |
| exit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
