2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 package jexer
.tterminal
;
31 import java
.awt
.Color
;
32 import java
.awt
.Graphics2D
;
33 import java
.awt
.image
.BufferedImage
;
34 import java
.util
.ArrayList
;
35 import java
.util
.HashMap
;
38 * Sixel parses a buffer of sixel image data into a BufferedImage.
42 // ------------------------------------------------------------------------
43 // Constants --------------------------------------------------------------
44 // ------------------------------------------------------------------------
47 * Parser character scan states.
49 private enum ScanState
{
56 // ------------------------------------------------------------------------
57 // Variables --------------------------------------------------------------
58 // ------------------------------------------------------------------------
61 * If true, enable debug messages.
63 private static boolean DEBUG
= false;
66 * Number of pixels to increment when we need more horizontal room.
68 private static int WIDTH_INCREASE
= 400;
71 * Number of pixels to increment when we need more vertical room.
73 private static int HEIGHT_INCREASE
= 400;
76 * Current scanning state.
78 private ScanState scanState
= ScanState
.GROUND
;
81 * Parameter characters being collected.
83 private ArrayList
<Integer
> colorParams
;
86 * The sixel palette colors specified.
88 private HashMap
<Integer
, Color
> palette
;
91 * The buffer to parse.
93 private String buffer
;
96 * The image being drawn to.
98 private BufferedImage image
;
101 * The real width of image.
103 private int width
= 0;
106 * The real height of image.
108 private int height
= 0;
113 private int repeatCount
= -1;
116 * The current drawing x position.
121 * The maximum y drawn to. This will set the final image height.
126 * The current drawing color.
128 private Color color
= Color
.BLACK
;
130 // ------------------------------------------------------------------------
131 // Constructors -----------------------------------------------------------
132 // ------------------------------------------------------------------------
135 * Public constructor.
137 * @param buffer the sixel data to parse
139 public Sixel(final String buffer
) {
140 this.buffer
= buffer
;
141 colorParams
= new ArrayList
<Integer
>();
142 palette
= new HashMap
<Integer
, Color
>();
143 image
= new BufferedImage(200, 100, BufferedImage
.TYPE_INT_ARGB
);
144 for (int i
= 0; i
< buffer
.length(); i
++) {
145 consume(buffer
.charAt(i
));
149 // ------------------------------------------------------------------------
150 // Sixel ------------------------------------------------------------------
151 // ------------------------------------------------------------------------
156 * @return the sixel data as an image.
158 public BufferedImage
getImage() {
159 if ((width
> 0) && (height
> 0)) {
160 return image
.getSubimage(0, 0, width
, y
+ 1);
166 * Resize image to a new size.
168 * @param newWidth new width of image
169 * @param newHeight new height of image
171 private void resizeImage(final int newWidth
, final int newHeight
) {
172 BufferedImage newImage
= new BufferedImage(newWidth
, newHeight
,
173 BufferedImage
.TYPE_INT_ARGB
);
176 System
.err
.println("resizeImage(); old " + image
.getWidth() + "x" +
177 image
.getHeight() + " new " + newWidth
+ "x" + newHeight
);
180 Graphics2D gr
= newImage
.createGraphics();
181 gr
.drawImage(image
, 0, 0, image
.getWidth(), image
.getHeight(), null);
187 * Clear the parameters and flags.
189 private void toGround() {
191 scanState
= ScanState
.GROUND
;
196 * Save a byte into the color parameters buffer.
198 * @param ch byte to save
200 private void param(final byte ch
) {
201 if (colorParams
.size() == 0) {
202 colorParams
.add(Integer
.valueOf(0));
204 Integer n
= colorParams
.get(colorParams
.size() - 1);
205 if ((ch
>= '0') && (ch
<= '9')) {
208 colorParams
.set(colorParams
.size() - 1, n
);
211 if ((ch
== ';') && (colorParams
.size() < 16)) {
212 colorParams
.add(Integer
.valueOf(0));
217 * Get a color parameter value, with a default.
219 * @param position parameter index. 0 is the first parameter.
220 * @param defaultValue value to use if colorParams[position] doesn't exist
221 * @return parameter value
223 private int getColorParam(final int position
, final int defaultValue
) {
224 if (colorParams
.size() < position
+ 1) {
227 return colorParams
.get(position
).intValue();
231 * Get a color parameter value, clamped to within min/max.
233 * @param position parameter index. 0 is the first parameter.
234 * @param defaultValue value to use if colorParams[position] doesn't exist
235 * @param minValue minimum value inclusive
236 * @param maxValue maximum value inclusive
237 * @return parameter value
239 private int getColorParam(final int position
, final int defaultValue
,
240 final int minValue
, final int maxValue
) {
242 assert (minValue
<= maxValue
);
243 int value
= getColorParam(position
, defaultValue
);
244 if (value
< minValue
) {
247 if (value
> maxValue
) {
254 * Add sixel data to the image.
256 * @param ch the character of sixel data
258 private void addSixel(final char ch
) {
259 int n
= ((int) ch
- 63);
261 if (DEBUG
&& (color
== null)) {
262 System
.err
.println("color is null?!");
263 System
.err
.println(buffer
);
266 int rgb
= color
.getRGB();
267 int rep
= (repeatCount
== -1 ?
1 : repeatCount
);
270 System
.err
.println("addSixel() rep " + rep
+ " char " +
271 Integer
.toHexString(n
) + " color " + color
);
276 if (x
+ rep
> image
.getWidth()) {
277 // Resize the image, give us another max(rep, WIDTH_INCREASE)
278 // pixels of horizontal length.
279 resizeImage(image
.getWidth() + Math
.max(rep
, WIDTH_INCREASE
),
283 // If nothing will be drawn, just advance x.
292 for (int i
= 0; i
< rep
; i
++) {
293 if ((n
& 0x01) != 0) {
294 image
.setRGB(x
, height
+ 0, rgb
);
295 y
= Math
.max(y
, height
);
297 if ((n
& 0x02) != 0) {
298 image
.setRGB(x
, height
+ 1, rgb
);
299 y
= Math
.max(y
, height
+ 1);
301 if ((n
& 0x04) != 0) {
302 image
.setRGB(x
, height
+ 2, rgb
);
303 y
= Math
.max(y
, height
+ 2);
305 if ((n
& 0x08) != 0) {
306 image
.setRGB(x
, height
+ 3, rgb
);
307 y
= Math
.max(y
, height
+ 3);
309 if ((n
& 0x10) != 0) {
310 image
.setRGB(x
, height
+ 4, rgb
);
311 y
= Math
.max(y
, height
+ 4);
313 if ((n
& 0x20) != 0) {
314 image
.setRGB(x
, height
+ 5, rgb
);
315 y
= Math
.max(y
, height
+ 5);
326 * Process a color palette change.
328 private void setPalette() {
329 int idx
= getColorParam(0, 0);
331 if (colorParams
.size() == 1) {
332 Color newColor
= palette
.get(idx
);
333 if (newColor
!= null) {
337 System
.err
.println("COLOR " + idx
+ " NOT FOUND");
343 System
.err
.println("set color " + idx
+ " " + color
);
348 int type
= getColorParam(1, 0);
349 float red
= (float) (getColorParam(2, 0, 0, 100) / 100.0);
350 float green
= (float) (getColorParam(3, 0, 0, 100) / 100.0);
351 float blue
= (float) (getColorParam(4, 0, 0, 100) / 100.0);
354 Color newColor
= new Color(red
, green
, blue
);
355 palette
.put(idx
, newColor
);
357 System
.err
.println("Palette color " + idx
+ " --> " + newColor
);
361 System
.err
.println("UNKNOWN COLOR TYPE " + type
+ ": " + type
+
362 " " + idx
+ " R " + red
+ " G " + green
+ " B " + blue
);
368 * Run this input character through the sixel state machine.
370 * @param ch character from the remote side
372 private void consume(char ch
) {
375 // System.err.printf("Sixel.consume() %c STATE = %s\n", ch, scanState);
377 // Between decimal 63 (inclusive) and 127 (exclusive) --> pixels
378 if ((ch
>= 63) && (ch
< 127)) {
379 if (scanState
== ScanState
.COLOR
) {
389 // Next color is here, parse what we had before.
390 if (scanState
== ScanState
.COLOR
) {
394 scanState
= ScanState
.COLOR
;
400 if (scanState
== ScanState
.COLOR
) {
404 scanState
= ScanState
.REPEAT
;
410 if (scanState
== ScanState
.COLOR
) {
418 if (height
+ 6 > image
.getHeight()) {
419 // Resize the image, give us another HEIGHT_INCREASE
420 // pixels of vertical length.
421 resizeImage(image
.getWidth(),
422 image
.getHeight() + HEIGHT_INCREASE
);
428 if (scanState
== ScanState
.COLOR
) {
437 if (scanState
== ScanState
.COLOR
) {
441 scanState
= ScanState
.QUOTE
;
448 // Unknown character.
450 System
.err
.println("UNKNOWN CHAR: " + ch
);
455 // Ignore everything else in the quote header.
459 // 30-39, 3B --> param
460 if ((ch
>= '0') && (ch
<= '9')) {
469 if ((ch
>= '0') && (ch
<= '9')) {
470 if (repeatCount
== -1) {
471 repeatCount
= (int) (ch
- '0');
474 repeatCount
+= (int) (ch
- '0');