private enum ScanState {
GROUND,
QUOTE,
- COLOR_ENTRY,
- COLOR_PARAM,
- COLOR_PIXELS,
- SIXEL_REPEAT,
+ COLOR,
+ REPEAT,
}
// ------------------------------------------------------------------------
/**
* If true, enable debug messages.
*/
- private static boolean DEBUG = true;
+ private static boolean DEBUG = false;
/**
* Number of pixels to increment when we need more horizontal room.
*/
public BufferedImage getImage() {
if ((width > 0) && (height > 0)) {
- return image.getSubimage(0, 0, width, height);
+ return image.getSubimage(0, 0, width, height + 6);
}
return null;
}
Integer.toHexString(n) + " color " + color);
}
+ assert (n >= 0);
+
if (x + rep > image.getWidth()) {
// Resize the image, give us another max(rep, WIDTH_INCREASE)
// pixels of horizontal length.
}
for (int i = 0; i < rep; i++) {
- if ((n & 0x01) == 0x01) {
- image.setRGB(x, height, rgb);
+ if ((n & 0x01) != 0) {
+ image.setRGB(x, height + 0, rgb);
}
- if ((n & 0x02) == 0x02) {
+ if ((n & 0x02) != 0) {
image.setRGB(x, height + 1, rgb);
}
- if ((n & 0x04) == 0x04) {
+ if ((n & 0x04) != 0) {
image.setRGB(x, height + 2, rgb);
}
- if ((n & 0x08) == 0x08) {
+ if ((n & 0x08) != 0) {
image.setRGB(x, height + 3, rgb);
}
- if ((n & 0x10) == 0x10) {
+ if ((n & 0x10) != 0) {
image.setRGB(x, height + 4, rgb);
}
- if ((n & 0x20) == 0x20) {
+ if ((n & 0x20) != 0) {
image.setRGB(x, height + 5, rgb);
}
x++;
Color newColor = palette.get(idx);
if (newColor != null) {
color = newColor;
+ } else {
+ System.err.println("COLOR " + idx + " NOT FOUND");
}
if (DEBUG) {
- System.err.println("set color: " + color);
+ System.err.println("set color " + idx + " " + color);
}
return;
}
if (DEBUG) {
System.err.println("Palette color " + idx + " --> " + newColor);
}
+ } else {
+ if (DEBUG) {
+ System.err.println("UNKNOWN COLOR TYPE " + type + ": " + type +
+ " " + idx + " R " + red + " G " + green + " B " + blue);
+ }
}
}
// DEBUG
// System.err.printf("Sixel.consume() %c STATE = %s\n", ch, scanState);
- switch (scanState) {
-
- case GROUND:
- switch (ch) {
- case '#':
- scanState = ScanState.COLOR_ENTRY;
- return;
- case '\"':
- scanState = ScanState.QUOTE;
- return;
- default:
- break;
- }
-
- if (ch == '!') {
- // Repeat count
- scanState = ScanState.SIXEL_REPEAT;
- }
- if (ch == '-') {
- if (height + 6 < image.getHeight()) {
- // Resize the image, give us another HEIGHT_INCREASE
- // pixels of vertical length.
- resizeImage(image.getWidth(),
- image.getHeight() + HEIGHT_INCREASE);
- }
- height += 6;
- x = 0;
- }
-
- if (ch == '$') {
- x = 0;
+ // Between decimal 63 (inclusive) and 127 (exclusive) --> pixels
+ if ((ch >= 63) && (ch < 127)) {
+ if (scanState == ScanState.COLOR) {
+ setPalette();
+ toGround();
}
+ addSixel(ch);
+ toGround();
return;
+ }
- case QUOTE:
- switch (ch) {
- case '#':
- scanState = ScanState.COLOR_ENTRY;
- return;
- default:
- break;
+ if (ch == '#') {
+ // Next color is here, parse what we had before.
+ if (scanState == ScanState.COLOR) {
+ setPalette();
+ toGround();
}
-
- // Ignore everything else in the quote header.
+ scanState = ScanState.COLOR;
return;
+ }
- case COLOR_ENTRY:
- // Between decimal 63 (inclusive) and 189 (exclusive) --> pixels
- if ((ch >= 63) && (ch < 189)) {
- addSixel(ch);
- return;
- }
-
- // 30-39, 3B --> param, then switch to COLOR_PARAM
- if ((ch >= '0') && (ch <= '9')) {
- param((byte) ch);
- scanState = ScanState.COLOR_PARAM;
- }
- if (ch == ';') {
- param((byte) ch);
- scanState = ScanState.COLOR_PARAM;
- }
-
- if (ch == '#') {
- // Next color is here, parse what we had before.
+ if (ch == '!') {
+ // Repeat count
+ if (scanState == ScanState.COLOR) {
setPalette();
toGround();
}
+ scanState = ScanState.REPEAT;
+ repeatCount = 0;
+ return;
+ }
- if (ch == '!') {
+ if (ch == '-') {
+ if (scanState == ScanState.COLOR) {
setPalette();
toGround();
+ }
- // Repeat count
- scanState = ScanState.SIXEL_REPEAT;
+ if (height + 6 < image.getHeight()) {
+ // Resize the image, give us another HEIGHT_INCREASE
+ // pixels of vertical length.
+ resizeImage(image.getWidth(),
+ image.getHeight() + HEIGHT_INCREASE);
}
- if (ch == '-') {
+ height += 6;
+ x = 0;
+ return;
+ }
+
+ if (ch == '$') {
+ if (scanState == ScanState.COLOR) {
setPalette();
toGround();
-
- if (height + 6 < image.getHeight()) {
- // Resize the image, give us another HEIGHT_INCREASE
- // pixels of vertical length.
- resizeImage(image.getWidth(),
- image.getHeight() + HEIGHT_INCREASE);
- }
- height += 6;
- x = 0;
}
+ x = 0;
+ return;
+ }
- if (ch == '$') {
+ if (ch == '"') {
+ if (scanState == ScanState.COLOR) {
setPalette();
toGround();
-
- x = 0;
}
+ scanState = ScanState.QUOTE;
return;
+ }
- case COLOR_PARAM:
+ switch (scanState) {
- // Between decimal 63 (inclusive) and 189 (exclusive) --> pixels
- if ((ch >= 63) && (ch < 189)) {
- addSixel(ch);
- return;
+ case GROUND:
+ // Unknown character.
+ if (DEBUG) {
+ System.err.println("UNKNOWN CHAR: " + ch);
}
+ return;
+
+ case QUOTE:
+ // Ignore everything else in the quote header.
+ return;
- // 30-39, 3B --> param, then switch to COLOR_PARAM
+ case COLOR:
+ // 30-39, 3B --> param
if ((ch >= '0') && (ch <= '9')) {
param((byte) ch);
}
if (ch == ';') {
param((byte) ch);
}
-
- if (ch == '#') {
- // Next color is here, parse what we had before.
- setPalette();
- toGround();
- scanState = ScanState.COLOR_ENTRY;
- }
-
- if (ch == '!') {
- setPalette();
- toGround();
-
- // Repeat count
- scanState = ScanState.SIXEL_REPEAT;
- }
- if (ch == '-') {
- setPalette();
- toGround();
-
- if (height + 6 < image.getHeight()) {
- // Resize the image, give us another HEIGHT_INCREASE
- // pixels of vertical length.
- resizeImage(image.getWidth(),
- image.getHeight() + HEIGHT_INCREASE);
- }
- height += 6;
- x = 0;
- }
-
- if (ch == '$') {
- setPalette();
- toGround();
-
- x = 0;
- }
return;
- case SIXEL_REPEAT:
-
- // Between decimal 63 (inclusive) and 189 (exclusive) --> pixels
- if ((ch >= 63) && (ch < 189)) {
- addSixel(ch);
- toGround();
- }
-
+ case REPEAT:
if ((ch >= '0') && (ch <= '9')) {
if (repeatCount == -1) {
repeatCount = (int) (ch - '0');
repeatCount += (int) (ch - '0');
}
}
-
- if (ch == '#') {
- // Next color.
- toGround();
- scanState = ScanState.COLOR_ENTRY;
- }
-
return;
+
}
}