1921f44e5c83716ba0c75e2634e30617e3582c20
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
);
175 Graphics2D gr
= newImage
.createGraphics();
176 gr
.drawImage(image
, 0, 0, image
.getWidth(), image
.getHeight(), null);
182 * Clear the parameters and flags.
184 private void toGround() {
186 scanState
= ScanState
.GROUND
;
191 * Save a byte into the color parameters buffer.
193 * @param ch byte to save
195 private void param(final byte ch
) {
196 if (colorParams
.size() == 0) {
197 colorParams
.add(Integer
.valueOf(0));
199 Integer n
= colorParams
.get(colorParams
.size() - 1);
200 if ((ch
>= '0') && (ch
<= '9')) {
203 colorParams
.set(colorParams
.size() - 1, n
);
206 if ((ch
== ';') && (colorParams
.size() < 16)) {
207 colorParams
.add(Integer
.valueOf(0));
212 * Get a color parameter value, with a default.
214 * @param position parameter index. 0 is the first parameter.
215 * @param defaultValue value to use if colorParams[position] doesn't exist
216 * @return parameter value
218 private int getColorParam(final int position
, final int defaultValue
) {
219 if (colorParams
.size() < position
+ 1) {
222 return colorParams
.get(position
).intValue();
226 * Get a color parameter value, clamped to within min/max.
228 * @param position parameter index. 0 is the first parameter.
229 * @param defaultValue value to use if colorParams[position] doesn't exist
230 * @param minValue minimum value inclusive
231 * @param maxValue maximum value inclusive
232 * @return parameter value
234 private int getColorParam(final int position
, final int defaultValue
,
235 final int minValue
, final int maxValue
) {
237 assert (minValue
<= maxValue
);
238 int value
= getColorParam(position
, defaultValue
);
239 if (value
< minValue
) {
242 if (value
> maxValue
) {
249 * Add sixel data to the image.
251 * @param ch the character of sixel data
253 private void addSixel(final char ch
) {
254 int n
= ((int) ch
- 63);
256 if (DEBUG
&& (color
== null)) {
257 System
.err
.println("color is null?!");
258 System
.err
.println(buffer
);
261 int rgb
= color
.getRGB();
262 int rep
= (repeatCount
== -1 ?
1 : repeatCount
);
265 System
.err
.println("addSixel() rep " + rep
+ " char " +
266 Integer
.toHexString(n
) + " color " + color
);
271 if (x
+ rep
> image
.getWidth()) {
272 // Resize the image, give us another max(rep, WIDTH_INCREASE)
273 // pixels of horizontal length.
274 resizeImage(image
.getWidth() + Math
.max(rep
, WIDTH_INCREASE
),
278 // If nothing will be drawn, just advance x.
287 for (int i
= 0; i
< rep
; i
++) {
288 if ((n
& 0x01) != 0) {
289 image
.setRGB(x
, height
+ 0, rgb
);
290 y
= Math
.max(y
, height
);
292 if ((n
& 0x02) != 0) {
293 image
.setRGB(x
, height
+ 1, rgb
);
294 y
= Math
.max(y
, height
+ 1);
296 if ((n
& 0x04) != 0) {
297 image
.setRGB(x
, height
+ 2, rgb
);
298 y
= Math
.max(y
, height
+ 2);
300 if ((n
& 0x08) != 0) {
301 image
.setRGB(x
, height
+ 3, rgb
);
302 y
= Math
.max(y
, height
+ 3);
304 if ((n
& 0x10) != 0) {
305 image
.setRGB(x
, height
+ 4, rgb
);
306 y
= Math
.max(y
, height
+ 4);
308 if ((n
& 0x20) != 0) {
309 image
.setRGB(x
, height
+ 5, rgb
);
310 y
= Math
.max(y
, height
+ 5);
321 * Process a color palette change.
323 private void setPalette() {
324 int idx
= getColorParam(0, 0);
326 if (colorParams
.size() == 1) {
327 Color newColor
= palette
.get(idx
);
328 if (newColor
!= null) {
332 System
.err
.println("COLOR " + idx
+ " NOT FOUND");
338 System
.err
.println("set color " + idx
+ " " + color
);
343 int type
= getColorParam(1, 0);
344 float red
= (float) (getColorParam(2, 0, 0, 100) / 100.0);
345 float green
= (float) (getColorParam(3, 0, 0, 100) / 100.0);
346 float blue
= (float) (getColorParam(4, 0, 0, 100) / 100.0);
349 Color newColor
= new Color(red
, green
, blue
);
350 palette
.put(idx
, newColor
);
352 System
.err
.println("Palette color " + idx
+ " --> " + newColor
);
356 System
.err
.println("UNKNOWN COLOR TYPE " + type
+ ": " + type
+
357 " " + idx
+ " R " + red
+ " G " + green
+ " B " + blue
);
363 * Run this input character through the sixel state machine.
365 * @param ch character from the remote side
367 private void consume(char ch
) {
370 // System.err.printf("Sixel.consume() %c STATE = %s\n", ch, scanState);
372 // Between decimal 63 (inclusive) and 127 (exclusive) --> pixels
373 if ((ch
>= 63) && (ch
< 127)) {
374 if (scanState
== ScanState
.COLOR
) {
384 // Next color is here, parse what we had before.
385 if (scanState
== ScanState
.COLOR
) {
389 scanState
= ScanState
.COLOR
;
395 if (scanState
== ScanState
.COLOR
) {
399 scanState
= ScanState
.REPEAT
;
405 if (scanState
== ScanState
.COLOR
) {
410 if (height
+ 6 < image
.getHeight()) {
411 // Resize the image, give us another HEIGHT_INCREASE
412 // pixels of vertical length.
413 resizeImage(image
.getWidth(),
414 image
.getHeight() + HEIGHT_INCREASE
);
422 if (scanState
== ScanState
.COLOR
) {
431 if (scanState
== ScanState
.COLOR
) {
435 scanState
= ScanState
.QUOTE
;
442 // Unknown character.
444 System
.err
.println("UNKNOWN CHAR: " + ch
);
449 // Ignore everything else in the quote header.
453 // 30-39, 3B --> param
454 if ((ch
>= '0') && (ch
<= '9')) {
463 if ((ch
>= '0') && (ch
<= '9')) {
464 if (repeatCount
== -1) {
465 repeatCount
= (int) (ch
- '0');
468 repeatCount
+= (int) (ch
- '0');