X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Ftui%2FImageText.java;h=5945b8272898875bacd7a5390c6eec5de29c16b8;hb=6e4b1db46676e929b4ea2d3c0cc052c2df9d5b93;hp=b0232f7b798d023bba7fe0ef8f18f0dd00da7225;hpb=20ce79bb39f98eacdf796f600dd175868ee31347;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/tui/ImageText.java b/src/be/nikiroo/jvcard/tui/ImageText.java index b0232f7..5945b82 100644 --- a/src/be/nikiroo/jvcard/tui/ImageText.java +++ b/src/be/nikiroo/jvcard/tui/ImageText.java @@ -23,6 +23,13 @@ public class ImageText { private Mode mode; private boolean invert; + /** + * Th rendering modes supported by this {@link ImageText} to convert + * {@link Image}s into text. + * + * @author niki + * + */ public enum Mode { /** * Use 5 different "colours" which are actually Unicode @@ -54,6 +61,19 @@ public class ImageText { ASCII, } + /** + * Create a new {@link ImageText} with the given parameters. Defaults to + * {@link Mode#DOUBLE_DITHERING} and no colour inversion. + * + * @param image + * the source {@link Image} + * @param size + * the final text size to target + */ + public ImageText(Image image, TerminalSize size) { + this(image, size, Mode.DOUBLE_DITHERING, false); + } + /** * Create a new {@link ImageText} with the given parameters. * @@ -63,10 +83,14 @@ public class ImageText { * the final text size to target * @param mode * the mode of conversion + * @param invert + * TRUE to invert colours rendering */ - public ImageText(Image image, TerminalSize size, Mode mode) { - setImage(image, size); + public ImageText(Image image, TerminalSize size, Mode mode, boolean invert) { + setImage(image); + setSize(size); setMode(mode); + setColorInvert(invert); } /** @@ -76,34 +100,21 @@ public class ImageText { * the new {@link Image} */ public void setImage(Image image) { - setImage(image, size); - } - - /** - * Change the source {@link Image}. - * - * @param size - * the size to use - */ - public void setImage(TerminalSize size) { - setImage(image, size); + this.text = null; + this.ready = false; + this.image = image; } /** - * Change the source {@link Image}. + * Change the target size of this {@link ImageText}. * - * @param image - * the new {@link Image} * @param size - * the size to use + * the new size */ - public void setImage(Image image, TerminalSize size) { + public void setSize(TerminalSize size) { this.text = null; this.ready = false; this.size = size; - if (image != null) { - this.image = image; - } } /** @@ -146,7 +157,8 @@ public class ImageText { */ public String getText() { if (text == null) { - if (image == null) + if (image == null || size == null || size.getColumns() == 0 + || size.getRows() == 0) return ""; int mult = 1; @@ -167,7 +179,7 @@ public class ImageText { int x = 0; int y = 0; - if (srcSize.getColumns() > srcSize.getRows()) { + if (srcSize.getColumns() < srcSize.getRows()) { double ratio = (double) size.getColumns() / (double) size.getRows(); ratio *= (double) srcSize.getRows() @@ -213,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),// @@ -269,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); } /** @@ -386,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 '█'; }