1 package be
.nikiroo
.jvcard
.tui
;
3 import java
.awt
.Graphics
;
5 import java
.awt
.image
.BufferedImage
;
6 import java
.awt
.image
.ImageObserver
;
8 import com
.googlecode
.lanterna
.TerminalSize
;
10 public class ImageText
{
12 private TerminalSize size
;
14 private boolean ready
;
16 public ImageText(Image image
, TerminalSize size
) {
17 setImage(image
, size
);
20 public void setImage(Image image
) {
21 setImage(image
, size
);
24 public void setImage(TerminalSize size
) {
25 setImage(image
, size
);
28 public void setImage(Image image
, TerminalSize size
) {
37 public String
getText() {
42 int w
= size
.getColumns() * 2;
43 int h
= size
.getRows() * 2;
44 BufferedImage buff
= new BufferedImage(w
, h
,
45 BufferedImage
.TYPE_INT_ARGB
);
46 Graphics gfx
= buff
.getGraphics();
48 TerminalSize srcSize
= getSize(image
);
51 if (srcSize
.getColumns() > srcSize
.getRows()) {
52 double ratio
= (double) srcSize
.getRows()
53 / (double) srcSize
.getColumns();
54 h
= (int) Math
.round(ratio
* h
);
55 y
= (buff
.getHeight() - h
) / 2;
57 double ratio
= (double) srcSize
.getColumns()
58 / (double) srcSize
.getRows();
59 w
= (int) Math
.round(ratio
* w
);
60 x
= (buff
.getWidth() - w
) / 2;
64 if (gfx
.drawImage(image
, x
, y
, w
, h
, new ImageObserver() {
66 public boolean imageUpdate(Image img
, int infoflags
, int x
,
67 int y
, int width
, int height
) {
68 ImageText
.this.ready
= true;
78 } catch (InterruptedException e
) {
84 int[][] square
= new int[2][2];
85 StringBuilder builder
= new StringBuilder();
86 for (int row
= 0; row
< buff
.getHeight(); row
+= 2) {
90 for (int col
= 0; col
< buff
.getWidth(); col
+= 2) {
91 square
[0][0] = buff
.getRGB(col
, row
);
92 square
[0][1] = buff
.getRGB(col
, row
+ 1);
93 square
[1][0] = buff
.getRGB(col
+ 1, row
);
94 square
[1][1] = buff
.getRGB(col
+ 1, row
+ 1);
95 builder
.append(getChar(square
));
99 text
= builder
.toString();
106 public String
toString() {
110 static private TerminalSize
getSize(Image img
) {
111 TerminalSize size
= null;
112 while (size
== null) {
113 int w
= img
.getWidth(null);
114 int h
= img
.getHeight(null);
115 if (w
> -1 && h
> -1) {
116 size
= new TerminalSize(w
, h
);
120 } catch (InterruptedException e
) {
128 static private char getChar(int[][] square
) {
130 if (rgb2hsl(square
[0][0])[3] > 50)
132 if (rgb2hsl(square
[0][1])[3] > 50)
134 if (rgb2hsl(square
[1][0])[3] > 50)
136 if (rgb2hsl(square
[1][1])[3] > 50)
177 // return [a, h, s, l]; a/s/l: 0 to 100%, h = 0 to 359°
178 static int[] rgb2hsl(int argb
) {
180 a
= ((argb
& 0xff000000) >> 24) / 255.0;
181 r
= ((argb
& 0x00ff0000) >> 16) / 255.0;
182 g
= ((argb
& 0x0000ff00) >> 8) / 255.0;
183 b
= ((argb
& 0x000000ff)) / 255.0;
185 double rgbMin
, rgbMax
;
186 rgbMin
= Math
.min(r
, Math
.min(g
, b
));
187 rgbMax
= Math
.max(r
, Math
.max(g
, b
));
190 l
= (rgbMin
+ rgbMax
) / 2;
193 if (rgbMin
== rgbMax
) {
197 s
= (rgbMax
- rgbMin
) / (rgbMax
+ rgbMin
);
199 s
= (rgbMax
- rgbMin
) / (2.0 - rgbMax
- rgbMin
);
204 if (r
> g
&& r
> b
) {
205 h
= (g
- b
) / (rgbMax
- rgbMin
);
207 h
= 2.0 + (b
- r
) / (rgbMax
- rgbMin
);
209 h
= 4.0 + (r
- g
) / (rgbMax
- rgbMin
);
212 int aa
= (int) Math
.round(100 * a
);
213 int hh
= (int) (60 * h
);
216 int ss
= (int) Math
.round(100 * s
);
217 int ll
= (int) Math
.round(100 * l
);
219 return new int[] { aa
, hh
, ss
, ll
};