Pad out short sixel rows.
authorTim Vaughan <tgvaughan@gmail.com>
Tue, 21 May 2019 09:26:11 +0000 (11:26 +0200)
committerTim Vaughan <tgvaughan@gmail.com>
Tue, 21 May 2019 09:26:45 +0000 (11:26 +0200)
sixel.el

index 81fa32e..8f00687 100644 (file)
--- a/sixel.el
+++ b/sixel.el
@@ -101,6 +101,19 @@ Returns a sixel image object."
       (cons colour-map
             (reverse rows)))))
 
+(defun sixel-pad-rows (sixel-image)
+  "Pad out contents of rows in SIXEL-IMAGE so that all rows are the same length."
+  (let ((width (car (sixel-image-dims sixel-image)))
+        (rows (cdr sixel-image)))
+    (dotimes (row-idx (length rows))
+      (let* ((row-cdr (nthcdr row-idx rows))
+             (row-width (length (car row-cdr))))
+        (if (< row-width width)
+            (setcar row-cdr (append (car row-cdr)
+                                    (make-list (- width row-width)
+                                               [nil nil nil nil nil nil])))))))
+  sixel-image)
+
 (defun sixel-image-colour-map (sixel-image)
   "Extract colour map from SIXEL-DATA."
   (car sixel-image))
@@ -174,7 +187,7 @@ of SIXEL-IMAGE."
   "Returns an XPM image representation of the SIXEL graphic encoded in STRING."
   (let* ((param-string (sixel-get-params string))
          (data-string (sixel-get-data string))
-         (sixel-image (sixel-process-data data-string)))
+         (sixel-image (sixel-pad-rows (sixel-process-data data-string))))
     (if (string-prefix-p "\eP" string)
         (concat
          "/* XPM */"