1 package be
.nikiroo
.utils
;
4 import java
.awt
.Dimension
;
5 import java
.awt
.Graphics
;
7 import java
.awt
.image
.BufferedImage
;
8 import java
.awt
.image
.ImageObserver
;
11 * This class converts an {@link Image} into a textual representation that can
12 * be displayed to the user in a TUI.
16 public class ImageText
{
18 private Dimension size
;
20 private boolean ready
;
22 private boolean invert
;
25 * Th rendering modes supported by this {@link ImageText} to convert
26 * {@link Image}s into text.
33 * Use 5 different "colours" which are actually Unicode
34 * {@link Character}s representing
36 * <li>space (blank)</li>
37 * <li>low shade (░)</li>
38 * <li>medium shade (▒)</li>
39 * <li>high shade (▓)</li>
40 * <li>full block (█)</li>
45 * Use "block" Unicode {@link Character}s up to quarter blocks, thus in
46 * effect doubling the resolution both in vertical and horizontal space.
47 * Note that since 2 {@link Character}s next to each other are square,
48 * we will use 4 blocks per 2 blocks for w/h resolution.
52 * Use {@link Character}s from both {@link Mode#DOUBLE_RESOLUTION} and
53 * {@link Mode#DITHERING}.
57 * Only use ASCII {@link Character}s.
63 * Create a new {@link ImageText} with the given parameters. Defaults to
64 * {@link Mode#DOUBLE_DITHERING} and no colour inversion.
67 * the source {@link Image}
69 * the final text size to target
71 public ImageText(Image image
, Dimension size
) {
72 this(image
, size
, Mode
.DOUBLE_DITHERING
, false);
76 * Create a new {@link ImageText} with the given parameters.
79 * the source {@link Image}
81 * the final text size to target
83 * the mode of conversion
85 * TRUE to invert colours rendering
87 public ImageText(Image image
, Dimension size
, Mode mode
, boolean invert
) {
91 setColorInvert(invert
);
95 * Change the source {@link Image}.
98 * the new {@link Image}
100 public void setImage(Image image
) {
107 * Change the target size of this {@link ImageText}.
112 public void setSize(Dimension size
) {
119 * Change the image-to-text mode.
122 * the new {@link Mode}
124 public void setMode(Mode mode
) {
131 * Set the colour-invert mode.
134 * TRUE to inverse the colours
136 public void setColorInvert(boolean invert
) {
137 this.invert
= invert
;
143 * Check if the colours are inverted.
145 * @return TRUE if the colours are inverted
147 public boolean isColorInvert() {
152 * Return the textual representation of the included {@link Image}.
154 * @return the {@link String} representation
156 public String
getText() {
158 if (image
== null || size
== null || size
.width
== 0
159 || size
.height
== 0) {
164 if (mode
== Mode
.DOUBLE_RESOLUTION
|| mode
== Mode
.DOUBLE_DITHERING
) {
168 int w
= size
.width
* mult
;
169 int h
= size
.height
* mult
;
171 BufferedImage buff
= new BufferedImage(w
, h
,
172 BufferedImage
.TYPE_INT_ARGB
);
174 Graphics gfx
= buff
.getGraphics();
176 Dimension srcSize
= getSize(image
);
177 srcSize
= new Dimension(srcSize
.width
* 2, srcSize
.height
);
181 if (srcSize
.width
< srcSize
.height
) {
182 double ratio
= (double) size
.width
/ (double) size
.height
;
183 ratio
*= (double) srcSize
.height
/ (double) srcSize
.width
;
185 h
= (int) Math
.round(ratio
* h
);
186 y
= (buff
.getHeight() - h
) / 2;
188 double ratio
= (double) size
.height
/ (double) size
.width
;
189 ratio
*= (double) srcSize
.width
/ (double) srcSize
.height
;
191 w
= (int) Math
.round(ratio
* w
);
192 x
= (buff
.getWidth() - w
) / 2;
195 if (gfx
.drawImage(image
, x
, y
, w
, h
, new ImageObserver() {
197 public boolean imageUpdate(Image img
, int infoflags
, int x
,
198 int y
, int width
, int height
) {
199 ImageText
.this.ready
= true;
209 } catch (InterruptedException e
) {
215 StringBuilder builder
= new StringBuilder();
217 for (int row
= 0; row
< buff
.getHeight(); row
+= mult
) {
219 builder
.append('\n');
222 for (int col
= 0; col
< buff
.getWidth(); col
+= mult
) {
225 float brightness
= getBrightness(buff
.getRGB(col
, row
));
226 if (mode
== Mode
.DITHERING
)
227 car
= getDitheringChar(brightness
, " ░▒▓█");
228 if (mode
== Mode
.ASCII
)
229 car
= getDitheringChar(brightness
, " .-+=o8#");
232 } else if (mult
== 2) {
233 builder
.append(getBlockChar( //
234 buff
.getRGB(col
, row
),//
235 buff
.getRGB(col
+ 1, row
),//
236 buff
.getRGB(col
, row
+ 1),//
237 buff
.getRGB(col
+ 1, row
+ 1),//
238 mode
== Mode
.DOUBLE_DITHERING
//
244 text
= builder
.toString();
251 public String
toString() {
256 * Return the size of the given {@link Image}.
259 * the image to measure
263 static private Dimension
getSize(Image img
) {
264 Dimension size
= null;
265 while (size
== null) {
266 int w
= img
.getWidth(null);
267 int h
= img
.getHeight(null);
268 if (w
> -1 && h
> -1) {
269 size
= new Dimension(w
, h
);
273 } catch (InterruptedException e
) {
282 * Return the {@link Character} corresponding to the given brightness level
283 * from the evenly-separated given {@link Character}s.
286 * the brightness level
288 * the {@link Character}s to choose from, from less bright to
289 * most bright; <b>MUST</b> contain at least one
292 * @return the {@link Character} to use
294 private char getDitheringChar(float brightness
, String cars
) {
295 int index
= Math
.round(brightness
* (cars
.length() - 1));
296 return cars
.charAt(index
);
300 * Return the {@link Character} corresponding to the 4 given colours in
301 * {@link Mode#DOUBLE_RESOLUTION} or {@link Mode#DOUBLE_DITHERING} mode.
304 * the upper left colour
306 * the upper right colour
308 * the lower left colour
310 * the lower right colour
312 * TRUE to use {@link Mode#DOUBLE_DITHERING}, FALSE for
313 * {@link Mode#DOUBLE_RESOLUTION}
315 * @return the {@link Character} to use
317 private char getBlockChar(int upperleft
, int upperright
, int lowerleft
,
318 int lowerright
, boolean dithering
) {
321 if (getBrightness(upperleft
) > 0.5f
) {
324 if (getBrightness(upperright
) > 0.5f
) {
327 if (getBrightness(lowerleft
) > 0.5f
) {
330 if (getBrightness(lowerright
) > 0.5f
) {
368 avg
+= getBrightness(upperleft
);
369 avg
+= getBrightness(upperright
);
370 avg
+= getBrightness(lowerleft
);
371 avg
+= getBrightness(lowerright
);
374 return getDitheringChar(avg
, " ░▒▓█");
384 * Temporary array used so not to create a lot of new ones.
386 private float[] tmp
= new float[4];
389 * Return the brightness value to use from the given ARGB colour.
394 * @return the brightness to sue for computations
396 private float getBrightness(int argb
) {
398 return 1 - rgb2hsb(argb
, tmp
)[2];
401 return rgb2hsb(argb
, tmp
)[2];
405 * Convert the given ARGB colour in HSL/HSB, either into the supplied array
406 * or into a new one if array is NULL.
409 * ARGB pixels are given in 0xAARRGGBB format, while the returned array will
410 * contain Hue, Saturation, Lightness/Brightness, Alpha, in this order. H,
411 * S, L and A are all ranging from 0 to 1 (indeed, H is in 1/360th).
416 * the ARGB colour pixel to convert
418 * the array to convert into or NULL to create a new one
420 * @return the array containing the HSL/HSB converted colour
422 static float[] rgb2hsb(int argb
, float[] array
) {
424 a
= ((argb
& 0xff000000) >> 24);
425 r
= ((argb
& 0x00ff0000) >> 16);
426 g
= ((argb
& 0x0000ff00) >> 8);
427 b
= ((argb
& 0x000000ff));
430 array
= new float[4];
433 Color
.RGBtoHSB(r
, g
, b
, array
);
439 // // other implementation:
442 // a = ((argb & 0xff000000) >> 24) / 255.0f;
443 // r = ((argb & 0x00ff0000) >> 16) / 255.0f;
444 // g = ((argb & 0x0000ff00) >> 8) / 255.0f;
445 // b = ((argb & 0x000000ff)) / 255.0f;
447 // float rgbMin, rgbMax;
448 // rgbMin = Math.min(r, Math.min(g, b));
449 // rgbMax = Math.max(r, Math.max(g, b));
452 // l = (rgbMin + rgbMax) / 2;
455 // if (rgbMin == rgbMax) {
459 // s = (rgbMax - rgbMin) / (rgbMax + rgbMin);
461 // s = (rgbMax - rgbMin) / (2.0f - rgbMax - rgbMin);
466 // if (r > g && r > b) {
467 // h = (g - b) / (rgbMax - rgbMin);
468 // } else if (g > b) {
469 // h = 2.0f + (b - r) / (rgbMax - rgbMin);
471 // h = 4.0f + (r - g) / (rgbMax - rgbMin);
473 // h /= 6; // from 0 to 1
475 // return new float[] { h, s, l, a };
477 // // // natural mode:
479 // // int aa = (int) Math.round(100 * a);
480 // // int hh = (int) (360 * h);
483 // // int ss = (int) Math.round(100 * s);
484 // // int ll = (int) Math.round(100 * l);
486 // // return new int[] { hh, ss, ll, aa };