5945b8272898875bacd7a5390c6eec5de29c16b8
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
;
27 * Th rendering modes supported by this {@link ImageText} to convert
28 * {@link Image}s into text.
35 * Use 5 different "colours" which are actually Unicode
36 * {@link Character}s representing
38 * <li>space (blank)</li>
39 * <li>low shade (░)</li>
40 * <li>medium shade (▒)</li>
41 * <li>high shade (▓)</li>
42 * <li>full block (█)</li>
47 * Use "block" Unicode {@link Character}s up to quarter blocks, thus in
48 * effect doubling the resolution both in vertical and horizontal space.
49 * Note that since 2 {@link Character}s next to each other are square,
50 * we will use 4 blocks per 2 blocks for w/h resolution.
54 * Use {@link Character}s from both {@link Mode#DOUBLE_RESOLUTION} and
55 * {@link Mode#DITHERING}.
59 * Only use ASCII {@link Character}s.
65 * Create a new {@link ImageText} with the given parameters. Defaults to
66 * {@link Mode#DOUBLE_DITHERING} and no colour inversion.
69 * the source {@link Image}
71 * the final text size to target
73 public ImageText(Image image
, TerminalSize size
) {
74 this(image
, size
, Mode
.DOUBLE_DITHERING
, false);
78 * Create a new {@link ImageText} with the given parameters.
81 * the source {@link Image}
83 * the final text size to target
85 * the mode of conversion
87 * TRUE to invert colours rendering
89 public ImageText(Image image
, TerminalSize size
, Mode mode
, boolean invert
) {
93 setColorInvert(invert
);
97 * Change the source {@link Image}.
100 * the new {@link Image}
102 public void setImage(Image image
) {
109 * Change the target size of this {@link ImageText}.
114 public void setSize(TerminalSize size
) {
121 * Change the image-to-text mode.
124 * the new {@link Mode}
126 public void setMode(Mode mode
) {
133 * Set the colour-invert mode.
136 * TRUE to inverse the colours
138 public void setColorInvert(boolean invert
) {
139 this.invert
= invert
;
145 * Check if the colours are inverted.
147 * @return TRUE if the colours are inverted
149 public boolean isColorInvert() {
154 * Return the textual representation of the included {@link Image}.
156 * @return the {@link String} representation
158 public String
getText() {
160 if (image
== null || size
== null || size
.getColumns() == 0
161 || size
.getRows() == 0)
165 if (mode
== Mode
.DOUBLE_RESOLUTION
|| mode
== Mode
.DOUBLE_DITHERING
)
168 int w
= size
.getColumns() * mult
;
169 int h
= size
.getRows() * mult
;
171 BufferedImage buff
= new BufferedImage(w
, h
,
172 BufferedImage
.TYPE_INT_ARGB
);
174 Graphics gfx
= buff
.getGraphics();
176 TerminalSize srcSize
= getSize(image
);
177 srcSize
= new TerminalSize(srcSize
.getColumns() * 2,
182 if (srcSize
.getColumns() < srcSize
.getRows()) {
183 double ratio
= (double) size
.getColumns()
184 / (double) size
.getRows();
185 ratio
*= (double) srcSize
.getRows()
186 / (double) srcSize
.getColumns();
188 h
= (int) Math
.round(ratio
* h
);
189 y
= (buff
.getHeight() - h
) / 2;
191 double ratio
= (double) size
.getRows()
192 / (double) size
.getColumns();
193 ratio
*= (double) srcSize
.getColumns()
194 / (double) srcSize
.getRows();
196 w
= (int) Math
.round(ratio
* w
);
197 x
= (buff
.getWidth() - w
) / 2;
200 if (gfx
.drawImage(image
, x
, y
, w
, h
, new ImageObserver() {
202 public boolean imageUpdate(Image img
, int infoflags
, int x
,
203 int y
, int width
, int height
) {
204 ImageText
.this.ready
= true;
214 } catch (InterruptedException e
) {
220 StringBuilder builder
= new StringBuilder();
222 for (int row
= 0; row
< buff
.getHeight(); row
+= mult
) {
224 builder
.append('\n');
226 for (int col
= 0; col
< buff
.getWidth(); col
+= mult
) {
229 float brightness
= getBrightness(buff
.getRGB(col
, row
));
230 if (mode
== Mode
.DITHERING
)
231 car
= getDitheringChar(brightness
, " ░▒▓█");
232 if (mode
== Mode
.ASCII
)
233 car
= getDitheringChar(brightness
, " .-+=o8#");
236 } else if (mult
== 2) {
237 builder
.append(getBlockChar( //
238 buff
.getRGB(col
, row
),//
239 buff
.getRGB(col
+ 1, row
),//
240 buff
.getRGB(col
, row
+ 1),//
241 buff
.getRGB(col
+ 1, row
+ 1),//
242 mode
== Mode
.DOUBLE_DITHERING
//
248 text
= builder
.toString();
255 public String
toString() {
260 * Return the size of the given {@link Image}.
263 * the image to measure
267 static private TerminalSize
getSize(Image img
) {
268 TerminalSize size
= null;
269 while (size
== null) {
270 int w
= img
.getWidth(null);
271 int h
= img
.getHeight(null);
272 if (w
> -1 && h
> -1) {
273 size
= new TerminalSize(w
, h
);
277 } catch (InterruptedException e
) {
286 * Return the {@link Character} corresponding to the given brightness level
287 * from the evenly-separated given {@link Character}s.
290 * the brightness level
292 * the {@link Character}s to choose from, from less bright to
293 * most bright; <b>MUST</b> contain at least one
296 * @return the {@link Character} to use
298 private char getDitheringChar(float brightness
, String cars
) {
299 int index
= Math
.round(brightness
* (cars
.length() - 1));
300 return cars
.charAt(index
);
304 * Return the {@link Character} corresponding to the 4 given colours in
305 * {@link Mode#DOUBLE_RESOLUTION} or {@link Mode#DOUBLE_DITHERING} mode.
308 * the upper left colour
310 * the upper right colour
312 * the lower left colour
314 * the lower right colour
316 * TRUE to use {@link Mode#DOUBLE_DITHERING}, FALSE for
317 * {@link Mode#DOUBLE_RESOLUTION}
319 * @return the {@link Character} to use
321 private char getBlockChar(int upperleft
, int upperright
, int lowerleft
,
322 int lowerright
, boolean dithering
) {
324 if (getBrightness(upperleft
) > 0.5f
)
326 if (getBrightness(upperright
) > 0.5f
)
328 if (getBrightness(lowerleft
) > 0.5f
)
330 if (getBrightness(lowerright
) > 0.5f
)
367 avg
+= getBrightness(upperleft
);
368 avg
+= getBrightness(upperright
);
369 avg
+= getBrightness(lowerleft
);
370 avg
+= getBrightness(lowerright
);
373 return getDitheringChar(avg
, " ░▒▓█");
383 * Temporary array used so not to create a lot of new ones.
385 private float[] tmp
= new float[4];
388 * Return the brightness value to use from the given ARGB colour.
393 * @return the brightness to sue for computations
395 private float getBrightness(int argb
) {
397 return 1 - rgb2hsb(argb
, tmp
)[2];
398 return rgb2hsb(argb
, tmp
)[2];
402 * Convert the given ARGB colour in HSL/HSB, either into the supplied array
403 * or into a new one if array is NULL.
406 * ARGB pixels are given in 0xAARRGGBB format, while the returned array will
407 * contain Hue, Saturation, Lightness/Brightness, Alpha, in this order. H,
408 * S, L and A are all ranging from 0 to 1 (indeed, H is in 1/360th).
413 * the ARGB colour pixel to convert
415 * the array to convert into or NULL to create a new one
417 * @return the array containing the HSL/HSB converted colour
419 static float[] rgb2hsb(int argb
, float[] array
) {
421 a
= ((argb
& 0xff000000) >> 24);
422 r
= ((argb
& 0x00ff0000) >> 16);
423 g
= ((argb
& 0x0000ff00) >> 8);
424 b
= ((argb
& 0x000000ff));
427 array
= new float[4];
428 Color
.RGBtoHSB(r
, g
, b
, array
);
434 // // other implementation:
437 // a = ((argb & 0xff000000) >> 24) / 255.0f;
438 // r = ((argb & 0x00ff0000) >> 16) / 255.0f;
439 // g = ((argb & 0x0000ff00) >> 8) / 255.0f;
440 // b = ((argb & 0x000000ff)) / 255.0f;
442 // float rgbMin, rgbMax;
443 // rgbMin = Math.min(r, Math.min(g, b));
444 // rgbMax = Math.max(r, Math.max(g, b));
447 // l = (rgbMin + rgbMax) / 2;
450 // if (rgbMin == rgbMax) {
454 // s = (rgbMax - rgbMin) / (rgbMax + rgbMin);
456 // s = (rgbMax - rgbMin) / (2.0f - rgbMax - rgbMin);
461 // if (r > g && r > b) {
462 // h = (g - b) / (rgbMax - rgbMin);
463 // } else if (g > b) {
464 // h = 2.0f + (b - r) / (rgbMax - rgbMin);
466 // h = 4.0f + (r - g) / (rgbMax - rgbMin);
468 // h /= 6; // from 0 to 1
470 // return new float[] { h, s, l, a };
472 // // // natural mode:
474 // // int aa = (int) Math.round(100 * a);
475 // // int hh = (int) (360 * h);
478 // // int ss = (int) Math.round(100 * s);
479 // // int ll = (int) Math.round(100 * l);
481 // // return new int[] { hh, ss, ll, aa };