Commit | Line | Data |
---|---|---|
5fc7bf09 KL |
1 | /* |
2 | * Jexer - Java Text User Interface | |
3 | * | |
4 | * The MIT License (MIT) | |
5 | * | |
6 | * Copyright (C) 2019 Kevin Lamonte | |
7 | * | |
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: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
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. | |
25 | * | |
26 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
27 | * @version 1 | |
28 | */ | |
29 | package jexer.tterminal; | |
30 | ||
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; | |
36 | ||
37 | /** | |
38 | * Sixel parses a buffer of sixel image data into a BufferedImage. | |
39 | */ | |
40 | public class Sixel { | |
41 | ||
42 | // ------------------------------------------------------------------------ | |
43 | // Constants -------------------------------------------------------------- | |
44 | // ------------------------------------------------------------------------ | |
45 | ||
46 | /** | |
47 | * Parser character scan states. | |
48 | */ | |
49 | private enum ScanState { | |
50 | GROUND, | |
8dd530ff | 51 | RASTER, |
69a8c368 KL |
52 | COLOR, |
53 | REPEAT, | |
5fc7bf09 KL |
54 | } |
55 | ||
56 | // ------------------------------------------------------------------------ | |
57 | // Variables -------------------------------------------------------------- | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
60 | /** | |
61 | * If true, enable debug messages. | |
62 | */ | |
69a8c368 | 63 | private static boolean DEBUG = false; |
5fc7bf09 KL |
64 | |
65 | /** | |
66 | * Number of pixels to increment when we need more horizontal room. | |
67 | */ | |
68 | private static int WIDTH_INCREASE = 400; | |
69 | ||
70 | /** | |
71 | * Number of pixels to increment when we need more vertical room. | |
72 | */ | |
73 | private static int HEIGHT_INCREASE = 400; | |
74 | ||
686d4da2 KL |
75 | /** |
76 | * Maximum width in pixels. | |
77 | */ | |
78 | private static int MAX_WIDTH = 1000; | |
79 | ||
80 | /** | |
81 | * Maximum height in pixels. | |
82 | */ | |
83 | private static int MAX_HEIGHT = 1000; | |
84 | ||
5fc7bf09 KL |
85 | /** |
86 | * Current scanning state. | |
87 | */ | |
88 | private ScanState scanState = ScanState.GROUND; | |
89 | ||
90 | /** | |
ab215e38 | 91 | * Parameters being collected. |
5fc7bf09 | 92 | */ |
ab215e38 KL |
93 | private int [] params = new int[5]; |
94 | ||
95 | /** | |
96 | * Current parameter being collected. | |
97 | */ | |
98 | private int paramsI = 0; | |
5fc7bf09 KL |
99 | |
100 | /** | |
101 | * The sixel palette colors specified. | |
102 | */ | |
103 | private HashMap<Integer, Color> palette; | |
104 | ||
105 | /** | |
106 | * The buffer to parse. | |
107 | */ | |
108 | private String buffer; | |
109 | ||
110 | /** | |
111 | * The image being drawn to. | |
112 | */ | |
113 | private BufferedImage image; | |
114 | ||
115 | /** | |
116 | * The real width of image. | |
117 | */ | |
118 | private int width = 0; | |
119 | ||
120 | /** | |
121 | * The real height of image. | |
122 | */ | |
123 | private int height = 0; | |
124 | ||
8dd530ff KL |
125 | /** |
126 | * The width of image provided in the raster attribute. | |
127 | */ | |
128 | private int rasterWidth = 0; | |
129 | ||
130 | /** | |
131 | * The height of image provided in the raster attribute. | |
132 | */ | |
133 | private int rasterHeight = 0; | |
134 | ||
5fc7bf09 KL |
135 | /** |
136 | * The repeat count. | |
137 | */ | |
138 | private int repeatCount = -1; | |
139 | ||
140 | /** | |
141 | * The current drawing x position. | |
142 | */ | |
143 | private int x = 0; | |
144 | ||
74bbd9bc KL |
145 | /** |
146 | * The maximum y drawn to. This will set the final image height. | |
147 | */ | |
148 | private int y = 0; | |
149 | ||
5fc7bf09 KL |
150 | /** |
151 | * The current drawing color. | |
152 | */ | |
153 | private Color color = Color.BLACK; | |
154 | ||
686d4da2 KL |
155 | /** |
156 | * If set, abort processing this image. | |
157 | */ | |
158 | private boolean abort = false; | |
159 | ||
5fc7bf09 KL |
160 | // ------------------------------------------------------------------------ |
161 | // Constructors ----------------------------------------------------------- | |
162 | // ------------------------------------------------------------------------ | |
163 | ||
164 | /** | |
165 | * Public constructor. | |
166 | * | |
167 | * @param buffer the sixel data to parse | |
686d4da2 | 168 | * @param palette palette to use, or null for a private palette |
5fc7bf09 | 169 | */ |
686d4da2 | 170 | public Sixel(final String buffer, final HashMap<Integer, Color> palette) { |
5fc7bf09 | 171 | this.buffer = buffer; |
686d4da2 KL |
172 | if (palette == null) { |
173 | this.palette = new HashMap<Integer, Color>(); | |
174 | } else { | |
175 | this.palette = palette; | |
5fc7bf09 KL |
176 | } |
177 | } | |
178 | ||
179 | // ------------------------------------------------------------------------ | |
180 | // Sixel ------------------------------------------------------------------ | |
181 | // ------------------------------------------------------------------------ | |
182 | ||
183 | /** | |
184 | * Get the image. | |
185 | * | |
186 | * @return the sixel data as an image. | |
187 | */ | |
188 | public BufferedImage getImage() { | |
686d4da2 KL |
189 | if (buffer != null) { |
190 | for (int i = 0; (i < buffer.length()) && (abort == false); i++) { | |
191 | consume(buffer.charAt(i)); | |
192 | } | |
193 | buffer = null; | |
194 | } | |
195 | if (abort == true) { | |
196 | return null; | |
197 | } | |
198 | ||
8dd530ff KL |
199 | if ((width > 0) && (height > 0) && (image != null)) { |
200 | /* | |
201 | System.err.println(String.format("%d %d %d %d", width, y + 1, | |
202 | rasterWidth, rasterHeight)); | |
203 | */ | |
204 | ||
205 | if ((rasterWidth > width) || (rasterHeight > y + 1)) { | |
206 | resizeImage(Math.max(width, rasterWidth), | |
207 | Math.max(y + 1, rasterHeight)); | |
208 | } | |
74bbd9bc | 209 | return image.getSubimage(0, 0, width, y + 1); |
5fc7bf09 KL |
210 | } |
211 | return null; | |
212 | } | |
213 | ||
214 | /** | |
215 | * Resize image to a new size. | |
216 | * | |
217 | * @param newWidth new width of image | |
218 | * @param newHeight new height of image | |
219 | */ | |
220 | private void resizeImage(final int newWidth, final int newHeight) { | |
221 | BufferedImage newImage = new BufferedImage(newWidth, newHeight, | |
222 | BufferedImage.TYPE_INT_ARGB); | |
223 | ||
8dd530ff KL |
224 | if (image == null) { |
225 | image = newImage; | |
226 | return; | |
227 | } | |
228 | ||
03ae544a KL |
229 | if (DEBUG) { |
230 | System.err.println("resizeImage(); old " + image.getWidth() + "x" + | |
231 | image.getHeight() + " new " + newWidth + "x" + newHeight); | |
232 | } | |
233 | ||
5fc7bf09 KL |
234 | Graphics2D gr = newImage.createGraphics(); |
235 | gr.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null); | |
236 | gr.dispose(); | |
237 | image = newImage; | |
238 | } | |
239 | ||
240 | /** | |
241 | * Clear the parameters and flags. | |
242 | */ | |
243 | private void toGround() { | |
ab215e38 KL |
244 | paramsI = 0; |
245 | for (int i = 0; i < params.length; i++) { | |
246 | params[i] = 0; | |
247 | } | |
5fc7bf09 KL |
248 | scanState = ScanState.GROUND; |
249 | repeatCount = -1; | |
250 | } | |
251 | ||
5fc7bf09 KL |
252 | /** |
253 | * Get a color parameter value, with a default. | |
254 | * | |
255 | * @param position parameter index. 0 is the first parameter. | |
256 | * @param defaultValue value to use if colorParams[position] doesn't exist | |
257 | * @return parameter value | |
258 | */ | |
8dd530ff | 259 | private int getParam(final int position, final int defaultValue) { |
ab215e38 | 260 | if (position > paramsI) { |
5fc7bf09 KL |
261 | return defaultValue; |
262 | } | |
ab215e38 | 263 | return params[position]; |
5fc7bf09 KL |
264 | } |
265 | ||
266 | /** | |
267 | * Get a color parameter value, clamped to within min/max. | |
268 | * | |
269 | * @param position parameter index. 0 is the first parameter. | |
270 | * @param defaultValue value to use if colorParams[position] doesn't exist | |
271 | * @param minValue minimum value inclusive | |
272 | * @param maxValue maximum value inclusive | |
273 | * @return parameter value | |
274 | */ | |
8dd530ff | 275 | private int getParam(final int position, final int defaultValue, |
5fc7bf09 KL |
276 | final int minValue, final int maxValue) { |
277 | ||
278 | assert (minValue <= maxValue); | |
8dd530ff | 279 | int value = getParam(position, defaultValue); |
5fc7bf09 KL |
280 | if (value < minValue) { |
281 | value = minValue; | |
282 | } | |
283 | if (value > maxValue) { | |
284 | value = maxValue; | |
285 | } | |
286 | return value; | |
287 | } | |
288 | ||
289 | /** | |
290 | * Add sixel data to the image. | |
291 | * | |
292 | * @param ch the character of sixel data | |
293 | */ | |
294 | private void addSixel(final char ch) { | |
295 | int n = ((int) ch - 63); | |
e6469faa KL |
296 | |
297 | if (DEBUG && (color == null)) { | |
298 | System.err.println("color is null?!"); | |
299 | System.err.println(buffer); | |
300 | } | |
301 | ||
5fc7bf09 KL |
302 | int rgb = color.getRGB(); |
303 | int rep = (repeatCount == -1 ? 1 : repeatCount); | |
304 | ||
305 | if (DEBUG) { | |
306 | System.err.println("addSixel() rep " + rep + " char " + | |
307 | Integer.toHexString(n) + " color " + color); | |
308 | } | |
309 | ||
69a8c368 KL |
310 | assert (n >= 0); |
311 | ||
8dd530ff KL |
312 | if (image == null) { |
313 | // The raster attributes was not provided. | |
314 | resizeImage(WIDTH_INCREASE, HEIGHT_INCREASE); | |
315 | } | |
316 | ||
5fc7bf09 KL |
317 | if (x + rep > image.getWidth()) { |
318 | // Resize the image, give us another max(rep, WIDTH_INCREASE) | |
319 | // pixels of horizontal length. | |
320 | resizeImage(image.getWidth() + Math.max(rep, WIDTH_INCREASE), | |
321 | image.getHeight()); | |
322 | } | |
323 | ||
324 | // If nothing will be drawn, just advance x. | |
325 | if (n == 0) { | |
326 | x += rep; | |
327 | if (x > width) { | |
328 | width = x; | |
329 | } | |
686d4da2 KL |
330 | if (width > MAX_WIDTH) { |
331 | abort = true; | |
332 | } | |
5fc7bf09 KL |
333 | return; |
334 | } | |
335 | ||
ab215e38 | 336 | int dy = 0; |
5fc7bf09 | 337 | for (int i = 0; i < rep; i++) { |
69a8c368 | 338 | if ((n & 0x01) != 0) { |
ab215e38 KL |
339 | dy = 0; |
340 | image.setRGB(x, height + dy, rgb); | |
5fc7bf09 | 341 | } |
69a8c368 | 342 | if ((n & 0x02) != 0) { |
ab215e38 KL |
343 | dy = 1; |
344 | image.setRGB(x, height + dy, rgb); | |
5fc7bf09 | 345 | } |
69a8c368 | 346 | if ((n & 0x04) != 0) { |
ab215e38 KL |
347 | dy = 2; |
348 | image.setRGB(x, height + dy, rgb); | |
5fc7bf09 | 349 | } |
69a8c368 | 350 | if ((n & 0x08) != 0) { |
ab215e38 KL |
351 | dy = 3; |
352 | image.setRGB(x, height + dy, rgb); | |
5fc7bf09 | 353 | } |
69a8c368 | 354 | if ((n & 0x10) != 0) { |
ab215e38 KL |
355 | dy = 4; |
356 | image.setRGB(x, height + dy, rgb); | |
5fc7bf09 | 357 | } |
69a8c368 | 358 | if ((n & 0x20) != 0) { |
ab215e38 KL |
359 | dy = 5; |
360 | image.setRGB(x, height + dy, rgb); | |
5fc7bf09 | 361 | } |
ab215e38 KL |
362 | if (height + dy > y) { |
363 | y = height + dy; | |
5fc7bf09 | 364 | } |
ab215e38 KL |
365 | x++; |
366 | } | |
367 | if (x > width) { | |
368 | width = x; | |
5fc7bf09 | 369 | } |
686d4da2 KL |
370 | if (width > MAX_WIDTH) { |
371 | abort = true; | |
372 | } | |
373 | if (y + 1 > MAX_HEIGHT) { | |
374 | abort = true; | |
375 | } | |
5fc7bf09 KL |
376 | } |
377 | ||
378 | /** | |
379 | * Process a color palette change. | |
380 | */ | |
381 | private void setPalette() { | |
8dd530ff | 382 | int idx = getParam(0, 0); |
5fc7bf09 | 383 | |
ab215e38 | 384 | if (paramsI == 0) { |
5fc7bf09 KL |
385 | Color newColor = palette.get(idx); |
386 | if (newColor != null) { | |
387 | color = newColor; | |
69a8c368 | 388 | } else { |
e6469faa KL |
389 | if (DEBUG) { |
390 | System.err.println("COLOR " + idx + " NOT FOUND"); | |
391 | } | |
392 | color = Color.BLACK; | |
5fc7bf09 KL |
393 | } |
394 | ||
395 | if (DEBUG) { | |
69a8c368 | 396 | System.err.println("set color " + idx + " " + color); |
5fc7bf09 KL |
397 | } |
398 | return; | |
399 | } | |
400 | ||
8dd530ff KL |
401 | int type = getParam(1, 0); |
402 | float red = (float) (getParam(2, 0, 0, 100) / 100.0); | |
403 | float green = (float) (getParam(3, 0, 0, 100) / 100.0); | |
404 | float blue = (float) (getParam(4, 0, 0, 100) / 100.0); | |
5fc7bf09 KL |
405 | |
406 | if (type == 2) { | |
407 | Color newColor = new Color(red, green, blue); | |
408 | palette.put(idx, newColor); | |
409 | if (DEBUG) { | |
410 | System.err.println("Palette color " + idx + " --> " + newColor); | |
411 | } | |
69a8c368 KL |
412 | } else { |
413 | if (DEBUG) { | |
414 | System.err.println("UNKNOWN COLOR TYPE " + type + ": " + type + | |
415 | " " + idx + " R " + red + " G " + green + " B " + blue); | |
416 | } | |
5fc7bf09 KL |
417 | } |
418 | } | |
419 | ||
8dd530ff KL |
420 | /** |
421 | * Parse the raster attributes. | |
422 | */ | |
423 | private void parseRaster() { | |
424 | int pan = getParam(0, 0); // Aspect ratio numerator | |
425 | int pad = getParam(1, 0); // Aspect ratio denominator | |
426 | int pah = getParam(2, 0); // Horizontal width | |
427 | int pav = getParam(3, 0); // Vertical height | |
428 | ||
429 | if ((pan == pad) && (pah > 0) && (pav > 0)) { | |
430 | rasterWidth = pah; | |
431 | rasterHeight = pav; | |
686d4da2 KL |
432 | if ((rasterWidth <= MAX_WIDTH) && (rasterHeight <= MAX_HEIGHT)) { |
433 | resizeImage(rasterWidth, rasterHeight); | |
434 | } else { | |
435 | abort = true; | |
436 | } | |
437 | } else { | |
438 | abort = true; | |
8dd530ff KL |
439 | } |
440 | } | |
441 | ||
5fc7bf09 KL |
442 | /** |
443 | * Run this input character through the sixel state machine. | |
444 | * | |
445 | * @param ch character from the remote side | |
446 | */ | |
447 | private void consume(char ch) { | |
448 | ||
449 | // DEBUG | |
450 | // System.err.printf("Sixel.consume() %c STATE = %s\n", ch, scanState); | |
451 | ||
69a8c368 KL |
452 | // Between decimal 63 (inclusive) and 127 (exclusive) --> pixels |
453 | if ((ch >= 63) && (ch < 127)) { | |
454 | if (scanState == ScanState.COLOR) { | |
455 | setPalette(); | |
5fc7bf09 | 456 | } |
8dd530ff KL |
457 | if (scanState == ScanState.RASTER) { |
458 | parseRaster(); | |
459 | toGround(); | |
460 | } | |
69a8c368 KL |
461 | addSixel(ch); |
462 | toGround(); | |
5fc7bf09 | 463 | return; |
69a8c368 | 464 | } |
5fc7bf09 | 465 | |
69a8c368 KL |
466 | if (ch == '#') { |
467 | // Next color is here, parse what we had before. | |
468 | if (scanState == ScanState.COLOR) { | |
469 | setPalette(); | |
470 | toGround(); | |
5fc7bf09 | 471 | } |
8dd530ff KL |
472 | if (scanState == ScanState.RASTER) { |
473 | parseRaster(); | |
474 | toGround(); | |
475 | } | |
69a8c368 | 476 | scanState = ScanState.COLOR; |
5fc7bf09 | 477 | return; |
69a8c368 | 478 | } |
5fc7bf09 | 479 | |
69a8c368 KL |
480 | if (ch == '!') { |
481 | // Repeat count | |
482 | if (scanState == ScanState.COLOR) { | |
5fc7bf09 KL |
483 | setPalette(); |
484 | toGround(); | |
485 | } | |
8dd530ff KL |
486 | if (scanState == ScanState.RASTER) { |
487 | parseRaster(); | |
488 | toGround(); | |
489 | } | |
69a8c368 KL |
490 | scanState = ScanState.REPEAT; |
491 | repeatCount = 0; | |
492 | return; | |
493 | } | |
5fc7bf09 | 494 | |
69a8c368 KL |
495 | if (ch == '-') { |
496 | if (scanState == ScanState.COLOR) { | |
5fc7bf09 KL |
497 | setPalette(); |
498 | toGround(); | |
69a8c368 | 499 | } |
8dd530ff KL |
500 | if (scanState == ScanState.RASTER) { |
501 | parseRaster(); | |
502 | toGround(); | |
503 | } | |
5fc7bf09 | 504 | |
03ae544a KL |
505 | height += 6; |
506 | x = 0; | |
507 | ||
508 | if (height + 6 > image.getHeight()) { | |
69a8c368 KL |
509 | // Resize the image, give us another HEIGHT_INCREASE |
510 | // pixels of vertical length. | |
511 | resizeImage(image.getWidth(), | |
512 | image.getHeight() + HEIGHT_INCREASE); | |
5fc7bf09 | 513 | } |
69a8c368 KL |
514 | return; |
515 | } | |
516 | ||
517 | if (ch == '$') { | |
518 | if (scanState == ScanState.COLOR) { | |
5fc7bf09 KL |
519 | setPalette(); |
520 | toGround(); | |
5fc7bf09 | 521 | } |
8dd530ff KL |
522 | if (scanState == ScanState.RASTER) { |
523 | parseRaster(); | |
524 | toGround(); | |
525 | } | |
69a8c368 KL |
526 | x = 0; |
527 | return; | |
528 | } | |
5fc7bf09 | 529 | |
69a8c368 KL |
530 | if (ch == '"') { |
531 | if (scanState == ScanState.COLOR) { | |
5fc7bf09 KL |
532 | setPalette(); |
533 | toGround(); | |
5fc7bf09 | 534 | } |
8dd530ff | 535 | scanState = ScanState.RASTER; |
5fc7bf09 | 536 | return; |
69a8c368 | 537 | } |
5fc7bf09 | 538 | |
69a8c368 | 539 | switch (scanState) { |
5fc7bf09 | 540 | |
69a8c368 KL |
541 | case GROUND: |
542 | // Unknown character. | |
543 | if (DEBUG) { | |
544 | System.err.println("UNKNOWN CHAR: " + ch); | |
5fc7bf09 | 545 | } |
69a8c368 KL |
546 | return; |
547 | ||
8dd530ff KL |
548 | case RASTER: |
549 | // 30-39, 3B --> param | |
550 | if ((ch >= '0') && (ch <= '9')) { | |
551 | params[paramsI] *= 10; | |
552 | params[paramsI] += (ch - '0'); | |
553 | } | |
554 | if (ch == ';') { | |
555 | if (paramsI < params.length - 1) { | |
556 | paramsI++; | |
557 | } | |
558 | } | |
69a8c368 | 559 | return; |
5fc7bf09 | 560 | |
69a8c368 KL |
561 | case COLOR: |
562 | // 30-39, 3B --> param | |
5fc7bf09 | 563 | if ((ch >= '0') && (ch <= '9')) { |
ab215e38 KL |
564 | params[paramsI] *= 10; |
565 | params[paramsI] += (ch - '0'); | |
5fc7bf09 KL |
566 | } |
567 | if (ch == ';') { | |
ab215e38 KL |
568 | if (paramsI < params.length - 1) { |
569 | paramsI++; | |
570 | } | |
5fc7bf09 | 571 | } |
5fc7bf09 KL |
572 | return; |
573 | ||
69a8c368 | 574 | case REPEAT: |
5fc7bf09 KL |
575 | if ((ch >= '0') && (ch <= '9')) { |
576 | if (repeatCount == -1) { | |
577 | repeatCount = (int) (ch - '0'); | |
578 | } else { | |
579 | repeatCount *= 10; | |
580 | repeatCount += (int) (ch - '0'); | |
581 | } | |
582 | } | |
5fc7bf09 | 583 | return; |
69a8c368 | 584 | |
5fc7bf09 KL |
585 | } |
586 | ||
587 | } | |
588 | ||
589 | } |