From 6e4b1db46676e929b4ea2d3c0cc052c2df9d5b93 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Mon, 4 Apr 2016 19:22:41 +0200 Subject: [PATCH] ASCII images: slight change in characters used and in code --- src/be/nikiroo/jvcard/tui/ImageText.java | 76 ++++++------------------ 1 file changed, 19 insertions(+), 57 deletions(-) diff --git a/src/be/nikiroo/jvcard/tui/ImageText.java b/src/be/nikiroo/jvcard/tui/ImageText.java index 073315d..5945b82 100644 --- a/src/be/nikiroo/jvcard/tui/ImageText.java +++ b/src/be/nikiroo/jvcard/tui/ImageText.java @@ -225,12 +225,14 @@ public class ImageText { for (int col = 0; col < buff.getWidth(); col += mult) { if (mult == 1) { + char car = ' '; + float brightness = getBrightness(buff.getRGB(col, row)); if (mode == Mode.DITHERING) - builder.append(getDitheringChar(buff.getRGB(col, - row))); - else - // Mode.ASCII - builder.append(getAsciiChar(buff.getRGB(col, row))); + car = getDitheringChar(brightness, " ░▒▓█"); + if (mode == Mode.ASCII) + car = getDitheringChar(brightness, " .-+=o8#"); + + builder.append(car); } else if (mult == 2) { builder.append(getBlockChar( // buff.getRGB(col, row),// @@ -281,51 +283,21 @@ public class ImageText { } /** - * Return the {@link Character} corresponding to this colour in - * {@link Mode#ASCII} mode. + * Return the {@link Character} corresponding to the given brightness level + * from the evenly-separated given {@link Character}s. * - * @param pixel - * the colour + * @param brightness + * the brightness level + * @param cars + * the {@link Character}s to choose from, from less bright to + * most bright; MUST contain at least one + * {@link Character} * * @return the {@link Character} to use */ - private char getAsciiChar(int pixel) { - float brigthness = getBrightness(pixel); - if (brigthness < 0.20) { - return ' '; - } else if (brigthness < 0.40) { - return '.'; - } else if (brigthness < 0.60) { - return '+'; - } else if (brigthness < 0.80) { - return '*'; - } else { - return '#'; - } - } - - /** - * Return the {@link Character} corresponding to this colour in - * {@link Mode#DITHERING} mode. - * - * @param pixel - * the colour - * - * @return the {@link Character} to use - */ - private char getDitheringChar(int pixel) { - float brigthness = getBrightness(pixel); - if (brigthness < 0.20) { - return ' '; - } else if (brigthness < 0.40) { - return '░'; - } else if (brigthness < 0.60) { - return '▒'; - } else if (brigthness < 0.80) { - return '▓'; - } else { - return '█'; - } + private char getDitheringChar(float brightness, String cars) { + int index = Math.round(brightness * (cars.length() - 1)); + return cars.charAt(index); } /** @@ -398,17 +370,7 @@ public class ImageText { avg += getBrightness(lowerright); avg /= 4; - if (avg < 0.20) { - return ' '; - } else if (avg < 0.40) { - return '░'; - } else if (avg < 0.60) { - return '▒'; - } else if (avg < 0.80) { - return '▓'; - } else { - return '█'; - } + return getDitheringChar(avg, " ░▒▓█"); } else { return '█'; } -- 2.27.0