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 * Parameters being collected.
83 private int [] params
= new int[5];
86 * Current parameter being collected.
88 private int paramsI
= 0;
91 * The sixel palette colors specified.
93 private HashMap
<Integer
, Color
> palette
;
96 * The buffer to parse.
98 private String buffer
;
101 * The image being drawn to.
103 private BufferedImage image
;
106 * The real width of image.
108 private int width
= 0;
111 * The real height of image.
113 private int height
= 0;
116 * The width of image provided in the raster attribute.
118 private int rasterWidth
= 0;
121 * The height of image provided in the raster attribute.
123 private int rasterHeight
= 0;
128 private int repeatCount
= -1;
131 * The current drawing x position.
136 * The maximum y drawn to. This will set the final image height.
141 * The current drawing color.
143 private Color color
= Color
.BLACK
;
145 // ------------------------------------------------------------------------
146 // Constructors -----------------------------------------------------------
147 // ------------------------------------------------------------------------
150 * Public constructor.
152 * @param buffer the sixel data to parse
154 public Sixel(final String buffer
) {
155 this.buffer
= buffer
;
156 palette
= new HashMap
<Integer
, Color
>();
157 for (int i
= 0; i
< buffer
.length(); i
++) {
158 consume(buffer
.charAt(i
));
162 // ------------------------------------------------------------------------
163 // Sixel ------------------------------------------------------------------
164 // ------------------------------------------------------------------------
169 * @return the sixel data as an image.
171 public BufferedImage
getImage() {
172 if ((width
> 0) && (height
> 0) && (image
!= null)) {
174 System.err.println(String.format("%d %d %d %d", width, y + 1,
175 rasterWidth, rasterHeight));
178 if ((rasterWidth
> width
) || (rasterHeight
> y
+ 1)) {
179 resizeImage(Math
.max(width
, rasterWidth
),
180 Math
.max(y
+ 1, rasterHeight
));
182 return image
.getSubimage(0, 0, width
, y
+ 1);
188 * Resize image to a new size.
190 * @param newWidth new width of image
191 * @param newHeight new height of image
193 private void resizeImage(final int newWidth
, final int newHeight
) {
194 BufferedImage newImage
= new BufferedImage(newWidth
, newHeight
,
195 BufferedImage
.TYPE_INT_ARGB
);
203 System
.err
.println("resizeImage(); old " + image
.getWidth() + "x" +
204 image
.getHeight() + " new " + newWidth
+ "x" + newHeight
);
207 Graphics2D gr
= newImage
.createGraphics();
208 gr
.drawImage(image
, 0, 0, image
.getWidth(), image
.getHeight(), null);
214 * Clear the parameters and flags.
216 private void toGround() {
218 for (int i
= 0; i
< params
.length
; i
++) {
221 scanState
= ScanState
.GROUND
;
226 * Get a color parameter value, with a default.
228 * @param position parameter index. 0 is the first parameter.
229 * @param defaultValue value to use if colorParams[position] doesn't exist
230 * @return parameter value
232 private int getParam(final int position
, final int defaultValue
) {
233 if (position
> paramsI
) {
236 return params
[position
];
240 * Get a color parameter value, clamped to within min/max.
242 * @param position parameter index. 0 is the first parameter.
243 * @param defaultValue value to use if colorParams[position] doesn't exist
244 * @param minValue minimum value inclusive
245 * @param maxValue maximum value inclusive
246 * @return parameter value
248 private int getParam(final int position
, final int defaultValue
,
249 final int minValue
, final int maxValue
) {
251 assert (minValue
<= maxValue
);
252 int value
= getParam(position
, defaultValue
);
253 if (value
< minValue
) {
256 if (value
> maxValue
) {
263 * Add sixel data to the image.
265 * @param ch the character of sixel data
267 private void addSixel(final char ch
) {
268 int n
= ((int) ch
- 63);
270 if (DEBUG
&& (color
== null)) {
271 System
.err
.println("color is null?!");
272 System
.err
.println(buffer
);
275 int rgb
= color
.getRGB();
276 int rep
= (repeatCount
== -1 ?
1 : repeatCount
);
279 System
.err
.println("addSixel() rep " + rep
+ " char " +
280 Integer
.toHexString(n
) + " color " + color
);
286 // The raster attributes was not provided.
287 resizeImage(WIDTH_INCREASE
, HEIGHT_INCREASE
);
290 if (x
+ rep
> image
.getWidth()) {
291 // Resize the image, give us another max(rep, WIDTH_INCREASE)
292 // pixels of horizontal length.
293 resizeImage(image
.getWidth() + Math
.max(rep
, WIDTH_INCREASE
),
297 // If nothing will be drawn, just advance x.
307 for (int i
= 0; i
< rep
; i
++) {
308 if ((n
& 0x01) != 0) {
310 image
.setRGB(x
, height
+ dy
, rgb
);
312 if ((n
& 0x02) != 0) {
314 image
.setRGB(x
, height
+ dy
, rgb
);
316 if ((n
& 0x04) != 0) {
318 image
.setRGB(x
, height
+ dy
, rgb
);
320 if ((n
& 0x08) != 0) {
322 image
.setRGB(x
, height
+ dy
, rgb
);
324 if ((n
& 0x10) != 0) {
326 image
.setRGB(x
, height
+ dy
, rgb
);
328 if ((n
& 0x20) != 0) {
330 image
.setRGB(x
, height
+ dy
, rgb
);
332 if (height
+ dy
> y
) {
343 * Process a color palette change.
345 private void setPalette() {
346 int idx
= getParam(0, 0);
349 Color newColor
= palette
.get(idx
);
350 if (newColor
!= null) {
354 System
.err
.println("COLOR " + idx
+ " NOT FOUND");
360 System
.err
.println("set color " + idx
+ " " + color
);
365 int type
= getParam(1, 0);
366 float red
= (float) (getParam(2, 0, 0, 100) / 100.0);
367 float green
= (float) (getParam(3, 0, 0, 100) / 100.0);
368 float blue
= (float) (getParam(4, 0, 0, 100) / 100.0);
371 Color newColor
= new Color(red
, green
, blue
);
372 palette
.put(idx
, newColor
);
374 System
.err
.println("Palette color " + idx
+ " --> " + newColor
);
378 System
.err
.println("UNKNOWN COLOR TYPE " + type
+ ": " + type
+
379 " " + idx
+ " R " + red
+ " G " + green
+ " B " + blue
);
385 * Parse the raster attributes.
387 private void parseRaster() {
388 int pan
= getParam(0, 0); // Aspect ratio numerator
389 int pad
= getParam(1, 0); // Aspect ratio denominator
390 int pah
= getParam(2, 0); // Horizontal width
391 int pav
= getParam(3, 0); // Vertical height
393 if ((pan
== pad
) && (pah
> 0) && (pav
> 0)) {
396 resizeImage(rasterWidth
, rasterHeight
);
401 * Run this input character through the sixel state machine.
403 * @param ch character from the remote side
405 private void consume(char ch
) {
408 // System.err.printf("Sixel.consume() %c STATE = %s\n", ch, scanState);
410 // Between decimal 63 (inclusive) and 127 (exclusive) --> pixels
411 if ((ch
>= 63) && (ch
< 127)) {
412 if (scanState
== ScanState
.COLOR
) {
415 if (scanState
== ScanState
.RASTER
) {
425 // Next color is here, parse what we had before.
426 if (scanState
== ScanState
.COLOR
) {
430 if (scanState
== ScanState
.RASTER
) {
434 scanState
= ScanState
.COLOR
;
440 if (scanState
== ScanState
.COLOR
) {
444 if (scanState
== ScanState
.RASTER
) {
448 scanState
= ScanState
.REPEAT
;
454 if (scanState
== ScanState
.COLOR
) {
458 if (scanState
== ScanState
.RASTER
) {
466 if (height
+ 6 > image
.getHeight()) {
467 // Resize the image, give us another HEIGHT_INCREASE
468 // pixels of vertical length.
469 resizeImage(image
.getWidth(),
470 image
.getHeight() + HEIGHT_INCREASE
);
476 if (scanState
== ScanState
.COLOR
) {
480 if (scanState
== ScanState
.RASTER
) {
489 if (scanState
== ScanState
.COLOR
) {
493 scanState
= ScanState
.RASTER
;
500 // Unknown character.
502 System
.err
.println("UNKNOWN CHAR: " + ch
);
507 // 30-39, 3B --> param
508 if ((ch
>= '0') && (ch
<= '9')) {
509 params
[paramsI
] *= 10;
510 params
[paramsI
] += (ch
- '0');
513 if (paramsI
< params
.length
- 1) {
520 // 30-39, 3B --> param
521 if ((ch
>= '0') && (ch
<= '9')) {
522 params
[paramsI
] *= 10;
523 params
[paramsI
] += (ch
- '0');
526 if (paramsI
< params
.length
- 1) {
533 if ((ch
>= '0') && (ch
<= '9')) {
534 if (repeatCount
== -1) {
535 repeatCount
= (int) (ch
- '0');
538 repeatCount
+= (int) (ch
- '0');