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
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.
*
* 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);
}
/**
* 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;
- }
}
/**
*/
public String getText() {
if (text == null) {
- if (image == null)
+ if (image == null || size == null || size.getColumns() == 0
+ || size.getRows() == 0)
return "";
int mult = 1;
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()
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),//
}
/**
- * 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; <b>MUST</b> 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);
}
/**
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 '█';
}