Close to working, insert-image broken somehow though.
authorTim Vaughan <tgvaughan@gmail.com>
Mon, 20 May 2019 21:41:16 +0000 (23:41 +0200)
committerTim Vaughan <tgvaughan@gmail.com>
Mon, 20 May 2019 21:41:41 +0000 (23:41 +0200)
sixel.el

index f2532e5..8147c8f 100644 (file)
--- a/sixel.el
+++ b/sixel.el
@@ -17,7 +17,7 @@
   (concat "q"
           "#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0"
           "#1~~@@vv@@~~@@~~$"
-          "#2??GG????-"
+          "#2??}}GG}}??}}??-"
           "#1!14@"))
 
 (defun sixel-get-params (string)
@@ -111,32 +111,61 @@ Returns a sixel image object."
   (let ((sixels (sixel-image-sixels sixel-image)))
     (cons
      (apply #'max (mapcar (lambda (row) (length row)) sixels))
-     (length sixels))))
+     (* 6 (length sixels)))))
 
 (defun sixel-image-to-xpm-values (sixel-image)
+  "Produce string representing parameter values component of XPM
+representation of SIXEL-IMAGE."
   (let* ((dims (sixel-image-dims sixel-image))
          (colour-map (sixel-image-colour-map sixel-image))
-         (n-colours (length colour-map)))
+         (n-colours (1+ (length colour-map))))
     (concat "\""
             (number-to-string (car dims)) " "
             (number-to-string (cdr dims)) " "
             (number-to-string n-colours) " 2\"")))
 
 (defun sixel-image-to-xpm-colours (sixel-image)
+  "Produce string representing colour definitions component of XPM
+representation of SIXEL-IMAGE."
   (let ((colour-map (sixel-image-colour-map sixel-image)))
-    (string-join
-     (mapcar (lambda (colour)
-               (concat
-                "\""
-                (elt colour 0) " "
-                "c #"
-                (format "%02x%02x%02x"
-                        (elt colour 1)
-                        (elt colour 2)
-                        (elt colour 3))
-                "\""))
-             colour-map)
-     ",")))
+    (concat
+     (string-join
+      (mapcar (lambda (colour)
+                (concat
+                 "\""
+                 (elt colour 0) " "
+                 "c #"
+                 (format "%02x%02x%02x"
+                         (elt colour 1)
+                         (elt colour 2)
+                         (elt colour 3))
+                 "\""))
+              colour-map)
+      ",")
+     "\",\n"
+     "\"-- c #000000\"")))
+
+(defun sixel-image-to-xpm-pixels (sixel-image)
+  "Produce string representating pixels component of XPM representation
+of SIXEL-IMAGE."
+  (concat
+   "\""
+   (string-join
+    (mapcar (lambda (sixel-row)
+              (string-join
+               (mapcar (lambda (i)
+                         (string-join
+                          (mapcar (lambda (sixel)
+                                    (let ((pixel (elt sixel i)))
+                                      (if pixel
+                                          pixel
+                                        "--")))
+                                  sixel-row)))
+                       (number-sequence 0 5))
+               "\",\n\""))
+            (sixel-image-sixels sixel-image))
+    "\",\n\"")
+   "\""))
 
 (defun sixel-to-xpm (string)
   "Returns an XPM image representation of the SIXEL graphic encoded in STRING."
@@ -147,10 +176,7 @@ Returns a sixel image object."
      "/* XPM */"
      "static char * pixmap = {"
      (sixel-image-to-xpm-values sixel-image) ",\n"
-     (sixel-image-to-xpm-colours sixel-image)
-     ;; (sixel-data-to-XPM-pixels data)
-     "};")))
-
-         
+     (sixel-image-to-xpm-colours sixel-image) ",\n"
+     (sixel-image-to-xpm-pixels sixel-image) "};")))
 
 ;; sixel.el ends here