X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Ftterminal%2FSixel.java;h=a4c00fc67f1da1ca38847b9bb5dad63daac5e006;hb=12b90437b5f22c2ae6e9b9b14c3b62b60f6143e5;hp=f19087770f82e37d1a7474b0d2db42ac2098d0c7;hpb=ab215e38e98a76ad189ba537e37c420ae515e4c0;p=nikiroo-utils.git diff --git a/src/jexer/tterminal/Sixel.java b/src/jexer/tterminal/Sixel.java index f190877..a4c00fc 100644 --- a/src/jexer/tterminal/Sixel.java +++ b/src/jexer/tterminal/Sixel.java @@ -48,7 +48,7 @@ public class Sixel { */ private enum ScanState { GROUND, - QUOTE, + RASTER, COLOR, REPEAT, } @@ -72,6 +72,16 @@ public class Sixel { */ private static int HEIGHT_INCREASE = 400; + /** + * Maximum width in pixels. + */ + private static int MAX_WIDTH = 1000; + + /** + * Maximum height in pixels. + */ + private static int MAX_HEIGHT = 1000; + /** * Current scanning state. */ @@ -112,6 +122,16 @@ public class Sixel { */ private int height = 0; + /** + * The width of image provided in the raster attribute. + */ + private int rasterWidth = 0; + + /** + * The height of image provided in the raster attribute. + */ + private int rasterHeight = 0; + /** * The repeat count. */ @@ -132,6 +152,11 @@ public class Sixel { */ private Color color = Color.BLACK; + /** + * If set, abort processing this image. + */ + private boolean abort = false; + // ------------------------------------------------------------------------ // Constructors ----------------------------------------------------------- // ------------------------------------------------------------------------ @@ -140,13 +165,14 @@ public class Sixel { * Public constructor. * * @param buffer the sixel data to parse + * @param palette palette to use, or null for a private palette */ - public Sixel(final String buffer) { + public Sixel(final String buffer, final HashMap palette) { this.buffer = buffer; - palette = new HashMap(); - image = new BufferedImage(200, 100, BufferedImage.TYPE_INT_ARGB); - for (int i = 0; i < buffer.length(); i++) { - consume(buffer.charAt(i)); + if (palette == null) { + this.palette = new HashMap(); + } else { + this.palette = palette; } } @@ -160,7 +186,26 @@ public class Sixel { * @return the sixel data as an image. */ public BufferedImage getImage() { - if ((width > 0) && (height > 0)) { + if (buffer != null) { + for (int i = 0; (i < buffer.length()) && (abort == false); i++) { + consume(buffer.charAt(i)); + } + buffer = null; + } + if (abort == true) { + return null; + } + + if ((width > 0) && (height > 0) && (image != null)) { + /* + System.err.println(String.format("%d %d %d %d", width, y + 1, + rasterWidth, rasterHeight)); + */ + + if ((rasterWidth > width) || (rasterHeight > y + 1)) { + resizeImage(Math.max(width, rasterWidth), + Math.max(y + 1, rasterHeight)); + } return image.getSubimage(0, 0, width, y + 1); } return null; @@ -176,6 +221,11 @@ public class Sixel { BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB); + if (image == null) { + image = newImage; + return; + } + if (DEBUG) { System.err.println("resizeImage(); old " + image.getWidth() + "x" + image.getHeight() + " new " + newWidth + "x" + newHeight); @@ -206,7 +256,7 @@ public class Sixel { * @param defaultValue value to use if colorParams[position] doesn't exist * @return parameter value */ - private int getColorParam(final int position, final int defaultValue) { + private int getParam(final int position, final int defaultValue) { if (position > paramsI) { return defaultValue; } @@ -222,11 +272,11 @@ public class Sixel { * @param maxValue maximum value inclusive * @return parameter value */ - private int getColorParam(final int position, final int defaultValue, + private int getParam(final int position, final int defaultValue, final int minValue, final int maxValue) { assert (minValue <= maxValue); - int value = getColorParam(position, defaultValue); + int value = getParam(position, defaultValue); if (value < minValue) { value = minValue; } @@ -259,6 +309,11 @@ public class Sixel { assert (n >= 0); + if (image == null) { + // The raster attributes was not provided. + resizeImage(WIDTH_INCREASE, HEIGHT_INCREASE); + } + if (x + rep > image.getWidth()) { // Resize the image, give us another max(rep, WIDTH_INCREASE) // pixels of horizontal length. @@ -272,6 +327,9 @@ public class Sixel { if (x > width) { width = x; } + if (width > MAX_WIDTH) { + abort = true; + } return; } @@ -309,13 +367,19 @@ public class Sixel { if (x > width) { width = x; } + if (width > MAX_WIDTH) { + abort = true; + } + if (y + 1 > MAX_HEIGHT) { + abort = true; + } } /** * Process a color palette change. */ private void setPalette() { - int idx = getColorParam(0, 0); + int idx = getParam(0, 0); if (paramsI == 0) { Color newColor = palette.get(idx); @@ -334,10 +398,10 @@ public class Sixel { return; } - int type = getColorParam(1, 0); - float red = (float) (getColorParam(2, 0, 0, 100) / 100.0); - float green = (float) (getColorParam(3, 0, 0, 100) / 100.0); - float blue = (float) (getColorParam(4, 0, 0, 100) / 100.0); + int type = getParam(1, 0); + float red = (float) (getParam(2, 0, 0, 100) / 100.0); + float green = (float) (getParam(3, 0, 0, 100) / 100.0); + float blue = (float) (getParam(4, 0, 0, 100) / 100.0); if (type == 2) { Color newColor = new Color(red, green, blue); @@ -353,6 +417,28 @@ public class Sixel { } } + /** + * Parse the raster attributes. + */ + private void parseRaster() { + int pan = getParam(0, 0); // Aspect ratio numerator + int pad = getParam(1, 0); // Aspect ratio denominator + int pah = getParam(2, 0); // Horizontal width + int pav = getParam(3, 0); // Vertical height + + if ((pan == pad) && (pah > 0) && (pav > 0)) { + rasterWidth = pah; + rasterHeight = pav; + if ((rasterWidth <= MAX_WIDTH) && (rasterHeight <= MAX_HEIGHT)) { + resizeImage(rasterWidth, rasterHeight); + } else { + abort = true; + } + } else { + abort = true; + } + } + /** * Run this input character through the sixel state machine. * @@ -368,6 +454,10 @@ public class Sixel { if (scanState == ScanState.COLOR) { setPalette(); } + if (scanState == ScanState.RASTER) { + parseRaster(); + toGround(); + } addSixel(ch); toGround(); return; @@ -379,6 +469,10 @@ public class Sixel { setPalette(); toGround(); } + if (scanState == ScanState.RASTER) { + parseRaster(); + toGround(); + } scanState = ScanState.COLOR; return; } @@ -389,6 +483,10 @@ public class Sixel { setPalette(); toGround(); } + if (scanState == ScanState.RASTER) { + parseRaster(); + toGround(); + } scanState = ScanState.REPEAT; repeatCount = 0; return; @@ -399,6 +497,10 @@ public class Sixel { setPalette(); toGround(); } + if (scanState == ScanState.RASTER) { + parseRaster(); + toGround(); + } height += 6; x = 0; @@ -417,6 +519,10 @@ public class Sixel { setPalette(); toGround(); } + if (scanState == ScanState.RASTER) { + parseRaster(); + toGround(); + } x = 0; return; } @@ -426,7 +532,7 @@ public class Sixel { setPalette(); toGround(); } - scanState = ScanState.QUOTE; + scanState = ScanState.RASTER; return; } @@ -439,8 +545,17 @@ public class Sixel { } return; - case QUOTE: - // Ignore everything else in the quote header. + case RASTER: + // 30-39, 3B --> param + if ((ch >= '0') && (ch <= '9')) { + params[paramsI] *= 10; + params[paramsI] += (ch - '0'); + } + if (ch == ';') { + if (paramsI < params.length - 1) { + paramsI++; + } + } return; case COLOR: