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 current drawing color.
123 private Color color
= Color
.BLACK
;
125 // ------------------------------------------------------------------------
126 // Constructors -----------------------------------------------------------
127 // ------------------------------------------------------------------------
130 * Public constructor.
132 * @param buffer the sixel data to parse
134 public Sixel(final String buffer
) {
135 this.buffer
= buffer
;
136 colorParams
= new ArrayList
<Integer
>();
137 palette
= new HashMap
<Integer
, Color
>();
138 image
= new BufferedImage(200, 100, BufferedImage
.TYPE_INT_ARGB
);
139 for (int i
= 0; i
< buffer
.length(); i
++) {
140 consume(buffer
.charAt(i
));
144 // ------------------------------------------------------------------------
145 // Sixel ------------------------------------------------------------------
146 // ------------------------------------------------------------------------
151 * @return the sixel data as an image.
153 public BufferedImage
getImage() {
154 if ((width
> 0) && (height
> 0)) {
155 return image
.getSubimage(0, 0, width
, height
+ 6);
161 * Resize image to a new size.
163 * @param newWidth new width of image
164 * @param newHeight new height of image
166 private void resizeImage(final int newWidth
, final int newHeight
) {
167 BufferedImage newImage
= new BufferedImage(newWidth
, newHeight
,
168 BufferedImage
.TYPE_INT_ARGB
);
170 Graphics2D gr
= newImage
.createGraphics();
171 gr
.drawImage(image
, 0, 0, image
.getWidth(), image
.getHeight(), null);
177 * Clear the parameters and flags.
179 private void toGround() {
181 scanState
= ScanState
.GROUND
;
186 * Save a byte into the color parameters buffer.
188 * @param ch byte to save
190 private void param(final byte ch
) {
191 if (colorParams
.size() == 0) {
192 colorParams
.add(Integer
.valueOf(0));
194 Integer n
= colorParams
.get(colorParams
.size() - 1);
195 if ((ch
>= '0') && (ch
<= '9')) {
198 colorParams
.set(colorParams
.size() - 1, n
);
201 if ((ch
== ';') && (colorParams
.size() < 16)) {
202 colorParams
.add(Integer
.valueOf(0));
207 * Get a color parameter value, with a default.
209 * @param position parameter index. 0 is the first parameter.
210 * @param defaultValue value to use if colorParams[position] doesn't exist
211 * @return parameter value
213 private int getColorParam(final int position
, final int defaultValue
) {
214 if (colorParams
.size() < position
+ 1) {
217 return colorParams
.get(position
).intValue();
221 * Get a color parameter value, clamped to within min/max.
223 * @param position parameter index. 0 is the first parameter.
224 * @param defaultValue value to use if colorParams[position] doesn't exist
225 * @param minValue minimum value inclusive
226 * @param maxValue maximum value inclusive
227 * @return parameter value
229 private int getColorParam(final int position
, final int defaultValue
,
230 final int minValue
, final int maxValue
) {
232 assert (minValue
<= maxValue
);
233 int value
= getColorParam(position
, defaultValue
);
234 if (value
< minValue
) {
237 if (value
> maxValue
) {
244 * Add sixel data to the image.
246 * @param ch the character of sixel data
248 private void addSixel(final char ch
) {
249 int n
= ((int) ch
- 63);
250 int rgb
= color
.getRGB();
251 int rep
= (repeatCount
== -1 ?
1 : repeatCount
);
254 System
.err
.println("addSixel() rep " + rep
+ " char " +
255 Integer
.toHexString(n
) + " color " + color
);
260 if (x
+ rep
> image
.getWidth()) {
261 // Resize the image, give us another max(rep, WIDTH_INCREASE)
262 // pixels of horizontal length.
263 resizeImage(image
.getWidth() + Math
.max(rep
, WIDTH_INCREASE
),
267 // If nothing will be drawn, just advance x.
276 for (int i
= 0; i
< rep
; i
++) {
277 if ((n
& 0x01) != 0) {
278 image
.setRGB(x
, height
+ 0, rgb
);
280 if ((n
& 0x02) != 0) {
281 image
.setRGB(x
, height
+ 1, rgb
);
283 if ((n
& 0x04) != 0) {
284 image
.setRGB(x
, height
+ 2, rgb
);
286 if ((n
& 0x08) != 0) {
287 image
.setRGB(x
, height
+ 3, rgb
);
289 if ((n
& 0x10) != 0) {
290 image
.setRGB(x
, height
+ 4, rgb
);
292 if ((n
& 0x20) != 0) {
293 image
.setRGB(x
, height
+ 5, rgb
);
304 * Process a color palette change.
306 private void setPalette() {
307 int idx
= getColorParam(0, 0);
309 if (colorParams
.size() == 1) {
310 Color newColor
= palette
.get(idx
);
311 if (newColor
!= null) {
314 System
.err
.println("COLOR " + idx
+ " NOT FOUND");
318 System
.err
.println("set color " + idx
+ " " + color
);
323 int type
= getColorParam(1, 0);
324 float red
= (float) (getColorParam(2, 0, 0, 100) / 100.0);
325 float green
= (float) (getColorParam(3, 0, 0, 100) / 100.0);
326 float blue
= (float) (getColorParam(4, 0, 0, 100) / 100.0);
329 Color newColor
= new Color(red
, green
, blue
);
330 palette
.put(idx
, newColor
);
332 System
.err
.println("Palette color " + idx
+ " --> " + newColor
);
336 System
.err
.println("UNKNOWN COLOR TYPE " + type
+ ": " + type
+
337 " " + idx
+ " R " + red
+ " G " + green
+ " B " + blue
);
343 * Run this input character through the sixel state machine.
345 * @param ch character from the remote side
347 private void consume(char ch
) {
350 // System.err.printf("Sixel.consume() %c STATE = %s\n", ch, scanState);
352 // Between decimal 63 (inclusive) and 127 (exclusive) --> pixels
353 if ((ch
>= 63) && (ch
< 127)) {
354 if (scanState
== ScanState
.COLOR
) {
364 // Next color is here, parse what we had before.
365 if (scanState
== ScanState
.COLOR
) {
369 scanState
= ScanState
.COLOR
;
375 if (scanState
== ScanState
.COLOR
) {
379 scanState
= ScanState
.REPEAT
;
385 if (scanState
== ScanState
.COLOR
) {
390 if (height
+ 6 < image
.getHeight()) {
391 // Resize the image, give us another HEIGHT_INCREASE
392 // pixels of vertical length.
393 resizeImage(image
.getWidth(),
394 image
.getHeight() + HEIGHT_INCREASE
);
402 if (scanState
== ScanState
.COLOR
) {
411 if (scanState
== ScanState
.COLOR
) {
415 scanState
= ScanState
.QUOTE
;
422 // Unknown character.
424 System
.err
.println("UNKNOWN CHAR: " + ch
);
429 // Ignore everything else in the quote header.
433 // 30-39, 3B --> param
434 if ((ch
>= '0') && (ch
<= '9')) {
443 if ((ch
>= '0') && (ch
<= '9')) {
444 if (repeatCount
== -1) {
445 repeatCount
= (int) (ch
- '0');
448 repeatCount
+= (int) (ch
- '0');