refactor, sixel performance
[fanfix.git] / src / jexer / tterminal / Sixel.java
index de3232c6588da3b1e6445a8d031b3804f8ab69eb..63e3c0fadcfa670d308c0f327f89f8e21313b810 100644 (file)
@@ -117,6 +117,11 @@ public class Sixel {
      */
     private int x = 0;
 
+    /**
+     * The maximum y drawn to.  This will set the final image height.
+     */
+    private int y = 0;
+
     /**
      * The current drawing color.
      */
@@ -152,7 +157,7 @@ public class Sixel {
      */
     public BufferedImage getImage() {
         if ((width > 0) && (height > 0)) {
-            return image.getSubimage(0, 0, width, height + 6);
+            return image.getSubimage(0, 0, width, y + 1);
         }
         return null;
     }
@@ -167,6 +172,11 @@ public class Sixel {
         BufferedImage newImage = new BufferedImage(newWidth, newHeight,
             BufferedImage.TYPE_INT_ARGB);
 
+        if (DEBUG) {
+            System.err.println("resizeImage(); old " + image.getWidth() + "x" +
+                image.getHeight() + " new " + newWidth + "x" + newHeight);
+        }
+
         Graphics2D gr = newImage.createGraphics();
         gr.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
         gr.dispose();
@@ -247,6 +257,12 @@ public class Sixel {
      */
     private void addSixel(final char ch) {
         int n = ((int) ch - 63);
+
+        if (DEBUG && (color == null)) {
+            System.err.println("color is null?!");
+            System.err.println(buffer);
+        }
+
         int rgb = color.getRGB();
         int rep = (repeatCount == -1 ? 1 : repeatCount);
 
@@ -276,21 +292,27 @@ public class Sixel {
         for (int i = 0; i < rep; i++) {
             if ((n & 0x01) != 0) {
                 image.setRGB(x, height + 0, rgb);
+                y = Math.max(y, height);
             }
             if ((n & 0x02) != 0) {
                 image.setRGB(x, height + 1, rgb);
+                y = Math.max(y, height + 1);
             }
             if ((n & 0x04) != 0) {
                 image.setRGB(x, height + 2, rgb);
+                y = Math.max(y, height + 2);
             }
             if ((n & 0x08) != 0) {
                 image.setRGB(x, height + 3, rgb);
+                y = Math.max(y, height + 3);
             }
             if ((n & 0x10) != 0) {
                 image.setRGB(x, height + 4, rgb);
+                y = Math.max(y, height + 4);
             }
             if ((n & 0x20) != 0) {
                 image.setRGB(x, height + 5, rgb);
+                y = Math.max(y, height + 5);
             }
             x++;
             if (x > width) {
@@ -311,7 +333,10 @@ public class Sixel {
             if (newColor != null) {
                 color = newColor;
             } else {
-                System.err.println("COLOR " + idx + " NOT FOUND");
+                if (DEBUG) {
+                    System.err.println("COLOR " + idx + " NOT FOUND");
+                }
+                color = Color.BLACK;
             }
 
             if (DEBUG) {
@@ -387,14 +412,15 @@ public class Sixel {
                 toGround();
             }
 
-            if (height + 6 < image.getHeight()) {
+            height += 6;
+            x = 0;
+
+            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;
             return;
         }