Skip to content

Instantly share code, notes, and snippets.

@dasuxullebt dasuxullebt/ToBrl.java
Last active Sep 25, 2016

Embed
What would you like to do?
/*
* Copyright 2016 (C) Christoph-Simon Senjak
*
* Mail: System.out.println(new StringBuilder("ed" + "." + "luxu" + "@" + "ssc").reverse().toString());
*/
import java.io.IOException;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.image.BufferedImage;
public class ToBrl {
public static int imgBool (int y, int x, boolean[][] image) {
try {
return image[y][x] ? 1 : 0;
} catch (ArrayIndexOutOfBoundsException e) {
return 0; // default to background
} catch (NullPointerException e) {
return 0; // default to background
}
}
public static char brailleAt (int x, int y, boolean[][] image) {
return (char)
(0x2800 |
( imgBool(y+0, x+0, image) << 0 ) | // 1
( imgBool(y+1, x+0, image) << 1 ) | // 2
( imgBool(y+2, x+0, image) << 2 ) | // 3
( imgBool(y+0, x+1, image) << 3 ) | // 4
( imgBool(y+1, x+1, image) << 4 ) | // 5
( imgBool(y+2, x+1, image) << 5 ) | // 6
( imgBool(y+3, x+0, image) << 6 ) | // 7
( imgBool(y+3, x+1, image) << 7 ) ); // 8
}
public static void main (String[] args) {
if (args.length != 1) {
System.err.println("Exactly one argument expected.");
System.exit(-1);
} else {
try {
BufferedImage img = ImageIO.read(new File(args[0]));
int width = img.getWidth();
int height = img.getHeight();
boolean[][] pixeldata = new boolean[height][];
for (int y = 0; y < img.getHeight(); ++y) {
pixeldata[y] = new boolean[width];
for (int x = 0; x < img.getWidth(); ++x) {
int rgb = img.getRGB(x, y);
int z = ((rgb & 0xFF) + ((rgb >> 8) & 0xFF) + ((rgb >> 16) & 0xFF)) / 3;
int a = (rgb >> 24) & 0xFF;
pixeldata[y][x] = (a > 0x7F) && (z < 0x7F);
}
}
StringBuffer buf = new StringBuffer("");
for (int y = 0; y < height; y += 4) {
for (int x = 0; x < width; x += 2) {
buf.append(brailleAt(x, y, pixeldata));
}
buf.append('\n');
}
System.out.print(buf);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.