package com.cp.atheater; import java.awt.image.BufferedImage; public class Converter { private static final char[] characterTransformTable = { '#', 'W', 'M', 'B', 'R', 'X', 'V', 'Y', 'I', 't', 'i', '+', '=', ';', ':', ',', '.' }; public ASCIIFrame convert(BufferedImage image) { return convert(image, ASCIIType.ASCII_TELNET); } public ASCIIFrame convert(BufferedImage image, int resolution) { return convert(image, ASCIIType.ASCII_TELNET, resolution); } public ASCIIFrame convert(BufferedImage image, ASCIIType type) { return convert(image, type, 3); } public ASCIIFrame convert(BufferedImage image, ASCIIType type, int resolution) { int width = image.getWidth(), height = image.getHeight(); char[] letters = new char[(((width * height) / resolution) + height) + 1]; int[] colors = new int[letters.length]; int lastHeight = 0; for (int index = 0; index < width * height; index += resolution) { if (index % width != lastHeight) { lastHeight = index % width; continue; } letters[index / resolution] = renderSingleCharacter(image.getRGB(index % width, index / width)); colors[index / resolution] = image.getRGB(index % width, index / width); //output += '\n'; } return new ASCIIFrame(letters, colors); } private char renderSingleCharacter(int rgb) { int green = (rgb >> 8) & 0xff; int blue = (rgb >> 16) & 0xff; int brightness = green * 2 + blue; return characterTransformTable[Math.round(brightness / 50) + 1]; } }