1 package be
.nikiroo
.jvcard
.tui
;
4 import java
.awt
.Graphics
;
6 import java
.awt
.image
.BufferedImage
;
7 import java
.awt
.image
.ImageObserver
;
9 import com
.googlecode
.lanterna
.TerminalSize
;
12 * This class converts an {@link Image} into a textual representation that can
13 * be displayed to the user in a TUI.
18 public class ImageText
{
20 private TerminalSize size
;
22 private boolean ready
;
24 private boolean invert
;
28 * Use 5 different "colours" which are actually Unicode
29 * {@link Character}s representing
31 * <li>space (blank)</li>
32 * <li>low shade (░)</li>
33 * <li>medium shade (▒)</li>
34 * <li>high shade (▓)</li>
35 * <li>full block (█)</li>
40 * Use "block" Unicode {@link Character}s up to quarter blocks, thus in
41 * effect doubling the resolution both in vertical and horizontal space.
42 * Note that since 2 {@link Character}s next to each other are square,
43 * we will use 4 blocks per 2 blocks for w/h resolution.
47 * Use {@link Character}s from both {@link Mode#DOUBLE_RESOLUTION} and
48 * {@link Mode#DITHERING}.
52 * Only use ASCII {@link Character}s.
58 * Create a new {@link ImageText} with the given parameters.
61 * the source {@link Image}
63 * the final text size to target
65 * the mode of conversion
67 public ImageText(Image image
, TerminalSize size
, Mode mode
) {
68 setImage(image
, size
);
73 * Change the source {@link Image}.
76 * the new {@link Image}
78 public void setImage(Image image
) {
79 setImage(image
, size
);
83 * Change the source {@link Image}.
88 public void setImage(TerminalSize size
) {
89 setImage(image
, size
);
93 * Change the source {@link Image}.
96 * the new {@link Image}
100 public void setImage(Image image
, TerminalSize size
) {
110 * Change the image-to-text mode.
113 * the new {@link Mode}
115 public void setMode(Mode mode
) {
122 * Set the colour-invert mode.
125 * TRUE to inverse the colours
127 public void setColorInvert(boolean invert
) {
128 this.invert
= invert
;
134 * Check if the colours are inverted.
136 * @return TRUE if the colours are inverted
138 public boolean isColorInvert() {
143 * Return the textual representation of the included {@link Image}.
145 * @return the {@link String} representation
147 public String
getText() {
153 if (mode
== Mode
.DOUBLE_RESOLUTION
|| mode
== Mode
.DOUBLE_DITHERING
)
156 int w
= size
.getColumns() * mult
;
157 int h
= size
.getRows() * mult
;
159 BufferedImage buff
= new BufferedImage(w
, h
,
160 BufferedImage
.TYPE_INT_ARGB
);
162 Graphics gfx
= buff
.getGraphics();
164 TerminalSize srcSize
= getSize(image
);
165 srcSize
= new TerminalSize(srcSize
.getColumns() * 2,
170 if (srcSize
.getColumns() > srcSize
.getRows()) {
171 double ratio
= (double) size
.getColumns()
172 / (double) size
.getRows();
173 ratio
*= (double) srcSize
.getRows()
174 / (double) srcSize
.getColumns();
176 h
= (int) Math
.round(ratio
* h
);
177 y
= (buff
.getHeight() - h
) / 2;
179 double ratio
= (double) size
.getRows()
180 / (double) size
.getColumns();
181 ratio
*= (double) srcSize
.getColumns()
182 / (double) srcSize
.getRows();
184 w
= (int) Math
.round(ratio
* w
);
185 x
= (buff
.getWidth() - w
) / 2;
188 if (gfx
.drawImage(image
, x
, y
, w
, h
, new ImageObserver() {
190 public boolean imageUpdate(Image img
, int infoflags
, int x
,
191 int y
, int width
, int height
) {
192 ImageText
.this.ready
= true;
202 } catch (InterruptedException e
) {
208 StringBuilder builder
= new StringBuilder();
210 for (int row
= 0; row
< buff
.getHeight(); row
+= mult
) {
212 builder
.append('\n');
214 for (int col
= 0; col
< buff
.getWidth(); col
+= mult
) {
216 if (mode
== Mode
.DITHERING
)
217 builder
.append(getDitheringChar(buff
.getRGB(col
,
221 builder
.append(getAsciiChar(buff
.getRGB(col
, row
)));
222 } else if (mult
== 2) {
223 builder
.append(getBlockChar( //
224 buff
.getRGB(col
, row
),//
225 buff
.getRGB(col
+ 1, row
),//
226 buff
.getRGB(col
, row
+ 1),//
227 buff
.getRGB(col
+ 1, row
+ 1),//
228 mode
== Mode
.DOUBLE_DITHERING
//
234 text
= builder
.toString();
241 public String
toString() {
246 * Return the size of the given {@link Image}.
249 * the image to measure
253 static private TerminalSize
getSize(Image img
) {
254 TerminalSize size
= null;
255 while (size
== null) {
256 int w
= img
.getWidth(null);
257 int h
= img
.getHeight(null);
258 if (w
> -1 && h
> -1) {
259 size
= new TerminalSize(w
, h
);
263 } catch (InterruptedException e
) {
272 * Return the {@link Character} corresponding to this colour in
273 * {@link Mode#ASCII} mode.
278 * @return the {@link Character} to use
280 private char getAsciiChar(int pixel
) {
281 float brigthness
= getBrightness(pixel
);
282 if (brigthness
< 0.20) {
284 } else if (brigthness
< 0.40) {
286 } else if (brigthness
< 0.60) {
288 } else if (brigthness
< 0.80) {
296 * Return the {@link Character} corresponding to this colour in
297 * {@link Mode#DITHERING} mode.
302 * @return the {@link Character} to use
304 private char getDitheringChar(int pixel
) {
305 float brigthness
= getBrightness(pixel
);
306 if (brigthness
< 0.20) {
308 } else if (brigthness
< 0.40) {
310 } else if (brigthness
< 0.60) {
312 } else if (brigthness
< 0.80) {
320 * Return the {@link Character} corresponding to the 4 given colours in
321 * {@link Mode#DOUBLE_RESOLUTION} or {@link Mode#DOUBLE_DITHERING} mode.
324 * the upper left colour
326 * the upper right colour
328 * the lower left colour
330 * the lower right colour
332 * TRUE to use {@link Mode#DOUBLE_DITHERING}, FALSE for
333 * {@link Mode#DOUBLE_RESOLUTION}
335 * @return the {@link Character} to use
337 private char getBlockChar(int upperleft
, int upperright
, int lowerleft
,
338 int lowerright
, boolean dithering
) {
340 if (getBrightness(upperleft
) > 0.5f
)
342 if (getBrightness(upperright
) > 0.5f
)
344 if (getBrightness(lowerleft
) > 0.5f
)
346 if (getBrightness(lowerright
) > 0.5f
)
383 avg
+= getBrightness(upperleft
);
384 avg
+= getBrightness(upperright
);
385 avg
+= getBrightness(lowerleft
);
386 avg
+= getBrightness(lowerright
);
391 } else if (avg
< 0.40) {
393 } else if (avg
< 0.60) {
395 } else if (avg
< 0.80) {
409 * Temporary array used so not to create a lot of new ones.
411 private float[] tmp
= new float[4];
414 * Return the brightness value to use from the given ARGB colour.
419 * @return the brightness to sue for computations
421 private float getBrightness(int argb
) {
423 return 1 - rgb2hsb(argb
, tmp
)[2];
424 return rgb2hsb(argb
, tmp
)[2];
428 * Convert the given ARGB colour in HSL/HSB, either into the supplied array
429 * or into a new one if array is NULL.
432 * ARGB pixels are given in 0xAARRGGBB format, while the returned array will
433 * contain Hue, Saturation, Lightness/Brightness, Alpha, in this order. H,
434 * S, L and A are all ranging from 0 to 1 (indeed, H is in 1/360th).
439 * the ARGB colour pixel to convert
441 * the array to convert into or NULL to create a new one
443 * @return the array containing the HSL/HSB converted colour
445 static float[] rgb2hsb(int argb
, float[] array
) {
447 a
= ((argb
& 0xff000000) >> 24);
448 r
= ((argb
& 0x00ff0000) >> 16);
449 g
= ((argb
& 0x0000ff00) >> 8);
450 b
= ((argb
& 0x000000ff));
453 array
= new float[4];
454 Color
.RGBtoHSB(r
, g
, b
, array
);
460 // // other implementation:
463 // a = ((argb & 0xff000000) >> 24) / 255.0f;
464 // r = ((argb & 0x00ff0000) >> 16) / 255.0f;
465 // g = ((argb & 0x0000ff00) >> 8) / 255.0f;
466 // b = ((argb & 0x000000ff)) / 255.0f;
468 // float rgbMin, rgbMax;
469 // rgbMin = Math.min(r, Math.min(g, b));
470 // rgbMax = Math.max(r, Math.max(g, b));
473 // l = (rgbMin + rgbMax) / 2;
476 // if (rgbMin == rgbMax) {
480 // s = (rgbMax - rgbMin) / (rgbMax + rgbMin);
482 // s = (rgbMax - rgbMin) / (2.0f - rgbMax - rgbMin);
487 // if (r > g && r > b) {
488 // h = (g - b) / (rgbMax - rgbMin);
489 // } else if (g > b) {
490 // h = 2.0f + (b - r) / (rgbMax - rgbMin);
492 // h = 4.0f + (r - g) / (rgbMax - rgbMin);
494 // h /= 6; // from 0 to 1
496 // return new float[] { h, s, l, a };
498 // // // natural mode:
500 // // int aa = (int) Math.round(100 * a);
501 // // int hh = (int) (360 * h);
504 // // int ss = (int) Math.round(100 * s);
505 // // int ll = (int) Math.round(100 * l);
507 // // return new int[] { hh, ss, ll, aa };