import java.util.Collections;
import java.util.HashMap;
import java.util.List;
-import java.util.LinkedList;
import jexer.TImage;
import jexer.bits.Cell;
import jexer.bits.CellAttributes;
import jexer.bits.Color;
+import jexer.event.TCommandEvent;
import jexer.event.TInputEvent;
import jexer.event.TKeypressEvent;
import jexer.event.TMouseEvent;
import jexer.event.TResizeEvent;
+import static jexer.TCommand.*;
import static jexer.TKeypress.*;
/**
MOUSE_SGR,
}
- /**
- * Number of colors in the sixel palette. Xterm 335 defines the max as
- * 1024.
- */
- private static final int MAX_COLOR_REGISTERS = 1024;
- // Black-and-white is possible too.
- // private static final int MAX_COLOR_REGISTERS = 2;
-
// ------------------------------------------------------------------------
// Variables --------------------------------------------------------------
// ------------------------------------------------------------------------
*/
private SixelCache sixelCache = null;
+ /**
+ * Number of colors in the sixel palette. Xterm 335 defines the max as
+ * 1024. Valid values are: 2 (black and white), 256, 512, 1024, and
+ * 2048.
+ */
+ private int sixelPaletteSize = 1024;
+
/**
* If true, then we changed System.in and need to change it back.
*/
/**
* SixelPalette is used to manage the conversion of images between 24-bit
- * RGB color and a palette of MAX_COLOR_REGISTERS colors.
+ * RGB color and a palette of sixelPaletteSize colors.
*/
private class SixelPalette {
* Map of color palette index for sixel output, from the order it was
* generated by makePalette() to rgbColors.
*/
- private int [] rgbSortedIndex = new int[MAX_COLOR_REGISTERS];
+ private int [] rgbSortedIndex = new int[sixelPaletteSize];
/**
* The color palette, organized by hue, saturation, and luminance.
int green = (color >>> 8) & 0xFF;
int blue = color & 0xFF;
- if (MAX_COLOR_REGISTERS == 2) {
+ if (sixelPaletteSize == 2) {
if (((red * red) + (green * green) + (blue * blue)) < 35568) {
// Black
return 0;
((255 - blue) * (255 - blue))) < diff) {
// White is a closer match.
- idx = MAX_COLOR_REGISTERS - 1;
+ idx = sixelPaletteSize - 1;
}
assert (idx != -1);
return idx;
}
/**
- * Dither an image to a MAX_COLOR_REGISTERS palette. The dithered
+ * Dither an image to a sixelPaletteSize palette. The dithered
* image cells will contain indexes into the palette.
*
* @param image the image to dither
imageY) & 0xFFFFFF;
int colorIdx = matchColor(oldPixel);
assert (colorIdx >= 0);
- assert (colorIdx < MAX_COLOR_REGISTERS);
+ assert (colorIdx < sixelPaletteSize);
int newPixel = rgbColors.get(colorIdx);
ditheredImage.setRGB(imageX, imageY, colorIdx);
private void makePalette() {
// Generate the sixel palette. Because we have no idea at this
// layer which image(s) will be shown, we have to use a common
- // palette with MAX_COLOR_REGISTERS colors for everything, and
+ // palette with sixelPaletteSize colors for everything, and
// map the BufferedImage colors to their nearest neighbor in RGB
// space.
- if (MAX_COLOR_REGISTERS == 2) {
+ if (sixelPaletteSize == 2) {
rgbColors.add(0);
rgbColors.add(0xFFFFFF);
rgbSortedIndex[0] = 0;
satBits = 2;
lumBits = 1;
- assert (MAX_COLOR_REGISTERS >= 256);
- assert ((MAX_COLOR_REGISTERS == 256)
- || (MAX_COLOR_REGISTERS == 512)
- || (MAX_COLOR_REGISTERS == 1024)
- || (MAX_COLOR_REGISTERS == 2048));
+ assert (sixelPaletteSize >= 256);
+ assert ((sixelPaletteSize == 256)
+ || (sixelPaletteSize == 512)
+ || (sixelPaletteSize == 1024)
+ || (sixelPaletteSize == 2048));
- switch (MAX_COLOR_REGISTERS) {
+ switch (sixelPaletteSize) {
case 512:
hueBits = 5;
satBits = 2;
}
// System.err.printf("\n</body></html>\n");
- assert (rgbColors.size() == MAX_COLOR_REGISTERS);
+ assert (rgbColors.size() == sixelPaletteSize);
/*
* We need to sort rgbColors, so that toSixel() can know where
Collections.sort(rgbColors);
HashMap<Integer, Integer> rgbColorIndices = null;
rgbColorIndices = new HashMap<Integer, Integer>();
- for (int i = 0; i < MAX_COLOR_REGISTERS; i++) {
+ for (int i = 0; i < sixelPaletteSize; i++) {
rgbColorIndices.put(rgbColors.get(i), i);
}
- for (int i = 0; i < MAX_COLOR_REGISTERS; i++) {
+ for (int i = 0; i < sixelPaletteSize; i++) {
int rawColor = rawRgbList.get(i);
rgbSortedIndex[i] = rgbColorIndices.get(rawColor);
}
if (DEBUG) {
- for (int i = 0; i < MAX_COLOR_REGISTERS; i++) {
+ for (int i = 0; i < sixelPaletteSize; i++) {
assert (rawRgbList != null);
int idx = rgbSortedIndex[i];
int rgbColor = rgbColors.get(idx);
- if ((idx != 0) && (idx != MAX_COLOR_REGISTERS - 1)) {
+ if ((idx != 0) && (idx != sixelPaletteSize - 1)) {
/*
System.err.printf("%d %06x --> %d %06x\n",
i, rawRgbList.get(i), idx, rgbColors.get(idx));
// Set the dimmest color as true black, and the brightest as true
// white.
rgbColors.set(0, 0);
- rgbColors.set(MAX_COLOR_REGISTERS - 1, 0xFFFFFF);
+ rgbColors.set(sixelPaletteSize - 1, 0xFFFFFF);
/*
System.err.printf("<html><body>\n");
public String emitPalette(final StringBuilder sb,
final boolean [] used) {
- for (int i = 0; i < MAX_COLOR_REGISTERS; i++) {
+ for (int i = 0; i < sixelPaletteSize; i++) {
if (((used != null) && (used[i] == true)) || (used == null)) {
int rgbColor = rgbColors.get(i);
sb.append(String.format("#%d;2;%d;%d;%d", i,
// ------------------------------------------------------------------------
/**
- * Constructor sets up state for getEvent().
+ * Constructor sets up state for getEvent(). If either windowWidth or
+ * windowHeight are less than 1, the terminal is not resized.
*
* @param listener the object this backend needs to wake up when new
* input comes in
// Send dtterm/xterm sequences, which will probably not work because
// allowWindowOps is defaulted to false.
- String resizeString = String.format("\033[8;%d;%dt", windowHeight,
- windowWidth);
- this.output.write(resizeString);
- this.output.flush();
+ if ((windowWidth > 0) && (windowHeight > 0)) {
+ String resizeString = String.format("\033[8;%d;%dt", windowHeight,
+ windowWidth);
+ this.output.write(resizeString);
+ this.output.flush();
+ }
}
/**
reloadOptions();
// Spin up the input reader
- eventQueue = new LinkedList<TInputEvent>();
+ eventQueue = new ArrayList<TInputEvent>();
readerThread = new Thread(this);
readerThread.start();
reloadOptions();
// Spin up the input reader
- eventQueue = new LinkedList<TInputEvent>();
+ eventQueue = new ArrayList<TInputEvent>();
readerThread = new Thread(this);
readerThread.start();
} else {
sixel = false;
}
+
+ // Palette size
+ int paletteSize = 1024;
+ try {
+ paletteSize = Integer.parseInt(System.getProperty(
+ "jexer.ECMA48.sixelPaletteSize", "1024"));
+ switch (paletteSize) {
+ case 2:
+ case 256:
+ case 512:
+ case 1024:
+ case 2048:
+ sixelPaletteSize = paletteSize;
+ break;
+ default:
+ // Ignore value
+ break;
+ }
+ } catch (NumberFormatException e) {
+ // SQUASH
+ }
}
// ------------------------------------------------------------------------
// available() will often return > 1, so we need to read in chunks to
// stay caught up.
char [] readBuffer = new char[128];
- List<TInputEvent> events = new LinkedList<TInputEvent>();
+ List<TInputEvent> events = new ArrayList<TInputEvent>();
while (!done && !stopReaderThread) {
try {
}
} // while ((done == false) && (stopReaderThread == false))
- // TODO: pass an event up to TApplication to tell it this Backend is
- // done.
+ // Pass an event up to TApplication to tell it this Backend is done.
+ synchronized (eventQueue) {
+ eventQueue.add(new TCommandEvent(cmBackendDisconnect));
+ }
+ if (listener != null) {
+ synchronized (listener) {
+ listener.notifyAll();
+ }
+ }
- System.err.println("*** run() exiting..."); System.err.flush();
+ // System.err.println("*** run() exiting..."); System.err.flush();
}
// ------------------------------------------------------------------------
// Sixel output support ---------------------------------------------------
// ------------------------------------------------------------------------
+ /**
+ * Get the number of colors in the sixel palette.
+ *
+ * @return the palette size
+ */
+ public int getSixelPaletteSize() {
+ return sixelPaletteSize;
+ }
+
+ /**
+ * Set the number of colors in the sixel palette.
+ *
+ * @param paletteSize the new palette size
+ */
+ public void setSixelPaletteSize(final int paletteSize) {
+ if (paletteSize == sixelPaletteSize) {
+ return;
+ }
+
+ switch (paletteSize) {
+ case 2:
+ case 256:
+ case 512:
+ case 1024:
+ case 2048:
+ break;
+ default:
+ throw new IllegalArgumentException("Unsupported sixel palette " +
+ " size: " + paletteSize);
+ }
+
+ // Don't step on the screen refresh thread.
+ synchronized (this) {
+ sixelPaletteSize = paletteSize;
+ palette = null;
+ sixelCache = null;
+ clearPhysical();
+ }
+ }
+
/**
* Start a sixel string for display one row's worth of bitmap data.
*
int [] rgbArray;
for (int i = 0; i < cells.size() - 1; i++) {
- if (cells.get(i).isInvertedImage()) {
+ int tileWidth = Math.min(cells.get(i).getImage().getWidth(),
+ imageWidth);
+ int tileHeight = Math.min(cells.get(i).getImage().getHeight(),
+ imageHeight);
+ if (false && cells.get(i).isInvertedImage()) {
+ // I used to put an all-white cell over the cursor, don't do
+ // that anymore.
rgbArray = new int[imageWidth * imageHeight];
for (int j = 0; j < rgbArray.length; j++) {
rgbArray[j] = 0xFFFFFF;
}
} else {
- rgbArray = cells.get(i).getImage().getRGB(0, 0,
- imageWidth, imageHeight, null, 0, imageWidth);
+ try {
+ rgbArray = cells.get(i).getImage().getRGB(0, 0,
+ tileWidth, tileHeight, null, 0, tileWidth);
+ } catch (Exception e) {
+ throw new RuntimeException("image " + imageWidth + "x" +
+ imageHeight +
+ "tile " + tileWidth + "x" +
+ tileHeight +
+ " cells.get(i).getImage() " +
+ cells.get(i).getImage() +
+ " i " + i +
+ " fullWidth " + fullWidth +
+ " fullHeight " + fullHeight, e);
+ }
}
/*
fullWidth, fullHeight, cells.size(), getTextWidth());
*/
- image.setRGB(i * imageWidth, 0, imageWidth, imageHeight,
- rgbArray, 0, imageWidth);
- if (imageHeight < fullHeight) {
+ image.setRGB(i * imageWidth, 0, tileWidth, tileHeight,
+ rgbArray, 0, tileWidth);
+ if (tileHeight < fullHeight) {
int backgroundColor = cells.get(i).getBackground().getRGB();
for (int imageX = 0; imageX < image.getWidth(); imageX++) {
for (int imageY = imageHeight; imageY < fullHeight;
}
}
totalWidth -= ((cells.size() - 1) * imageWidth);
- if (cells.get(cells.size() - 1).isInvertedImage()) {
+ if (false && cells.get(cells.size() - 1).isInvertedImage()) {
+ // I used to put an all-white cell over the cursor, don't do that
+ // anymore.
rgbArray = new int[totalWidth * imageHeight];
for (int j = 0; j < rgbArray.length; j++) {
rgbArray[j] = 0xFFFFFF;
}
} else {
- rgbArray = cells.get(cells.size() - 1).getImage().getRGB(0, 0,
- totalWidth, imageHeight, null, 0, totalWidth);
+ try {
+ rgbArray = cells.get(cells.size() - 1).getImage().getRGB(0, 0,
+ totalWidth, imageHeight, null, 0, totalWidth);
+ } catch (Exception e) {
+ throw new RuntimeException("image " + imageWidth + "x" +
+ imageHeight + " cells.get(cells.size() - 1).getImage() " +
+ cells.get(cells.size() - 1).getImage(), e);
+ }
}
image.setRGB((cells.size() - 1) * imageWidth, 0, totalWidth,
imageHeight, rgbArray, 0, totalWidth);
// Emit the palette, but only for the colors actually used by these
// cells.
- boolean [] usedColors = new boolean[MAX_COLOR_REGISTERS];
+ boolean [] usedColors = new boolean[sixelPaletteSize];
for (int imageX = 0; imageX < image.getWidth(); imageX++) {
for (int imageY = 0; imageY < image.getHeight(); imageY++) {
usedColors[image.getRGB(imageX, imageY)] = true;
int colorIdx = image.getRGB(imageX, imageY + currentRow);
assert (colorIdx >= 0);
- assert (colorIdx < MAX_COLOR_REGISTERS);
+ assert (colorIdx < sixelPaletteSize);
sixels[imageX][imageY] = colorIdx;
}
}
- for (int i = 0; i < MAX_COLOR_REGISTERS; i++) {
+ for (int i = 0; i < sixelPaletteSize; i++) {
boolean isUsed = false;
for (int imageX = 0; imageX < image.getWidth(); imageX++) {
for (int j = 0; j < 6; j++) {
// colored pixels, and select the color.
sb.append(String.format("$#%d", i));
+ int oldData = -1;
+ int oldDataCount = 0;
for (int imageX = 0; imageX < image.getWidth(); imageX++) {
// Add up all the pixels that match this color.
}
}
assert (data >= 0);
- assert (data < 127);
+ assert (data < 64);
data += 63;
- sb.append((char) data);
+
+ if (data == oldData) {
+ oldDataCount++;
+ } else {
+ if (oldDataCount == 1) {
+ sb.append((char) oldData);
+ } else if (oldDataCount > 1) {
+ sb.append(String.format("!%d", oldDataCount));
+ sb.append((char) oldData);
+ }
+ oldDataCount = 1;
+ oldData = data;
+ }
+
} // for (int imageX = 0; imageX < image.getWidth(); imageX++)
- } // for (int i = 0; i < MAX_COLOR_REGISTERS; i++)
+
+ // Emit the last sequence.
+ if (oldDataCount == 1) {
+ sb.append((char) oldData);
+ } else if (oldDataCount > 1) {
+ sb.append(String.format("!%d", oldDataCount));
+ sb.append((char) oldData);
+ }
+
+ } // for (int i = 0; i < sixelPaletteSize; i++)
// Advance to the next scan line.
sb.append("-");
return (startSixel(x, y) + sb.toString() + endSixel());
}
+ /**
+ * Get the sixel support flag.
+ *
+ * @return true if this terminal is emitting sixel
+ */
+ public boolean hasSixel() {
+ return sixel;
+ }
+
// ------------------------------------------------------------------------
// End sixel output support -----------------------------------------------
// ------------------------------------------------------------------------