Fixed colour scaling.
[sixel.git] / sixel.el
index 8147c8f..053739d 100644 (file)
--- a/sixel.el
+++ b/sixel.el
 ;;; Code:
 
 (defvar test-string
-  (concat "q"
+  (concat "\ePq"
           "#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0"
           "#1~~@@vv@@~~@@~~$"
           "#2??}}GG}}??}}??-"
-          "#1!14@"))
+          "#1!14@\e\\"))
 
 (defun sixel-get-params (string)
   "Retrieve the sixel parameters."
-  (car (split-string string "q")))
+  (car (split-string (substring string 2) "q")))
 
 (defun sixel-get-data (string)
   "Retrieve data string."
@@ -57,6 +57,7 @@ Returns a sixel image object."
           finished)
       (while  (not finished)
         (cond
+         ;; Define colour:
          ((looking-at "#\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\)")
           (let ((tag (format "%02x" (string-to-number (match-string 1))))
                 (mode (match-string 2))
@@ -64,19 +65,19 @@ Returns a sixel image object."
                 (g (string-to-number (match-string 4)))
                 (b (string-to-number (match-string 5))))
             (push (list tag r g b) colour-map)))
-
+         ;; Set current colour:
          ((looking-at "#\\([0-9]+\\)")
           (let ((tag (format "%02x" (string-to-number (match-string 1)))))
             (setq current-colour tag)))
-
+         ;; Carriage return:
          ((looking-at "\\$")
           (setq idx-out 0))
-
+         ;; New line:
          ((looking-at "-")
           (push (reverse this-row) rows)
           (setq this-row nil)
           (setq idx-out 0))
-
+         ;; RLE sixel char sequence:
          ((looking-at "!\\([0-9]+\\)\\([?-~]\\)")
           (let ((repeat-count (string-to-number (match-string 1)))
                 (char (elt (match-string 2) 0)))
@@ -84,20 +85,35 @@ Returns a sixel image object."
               (setq this-row
                     (sixel-tag-sixel-in-row this-row idx-out char current-colour))
               (setq idx-out (1+ idx-out)))))
-
-         ((looking-at "\\([?-~]\\)")
+         ;; Sixel char:
+         ((looking-at "\\([?-~]\\)") ; Sixel char
           (let ((char (elt (match-string 1) 0)))
             (setq this-row
                   (sixel-tag-sixel-in-row this-row idx-out char current-colour))
             (setq idx-out (1+ idx-out))))
-
-         (t (setq finished t)))
-
+         ;; Termination sequence:
+         ((looking-at "\e\\\\")
+          (setq finished t))
+         ;; Skip other char:
+         ((looking-at "[[:ascii:]]")))
         (goto-char (match-end 0)))
       (push (reverse this-row) rows)
       (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))
@@ -136,13 +152,13 @@ representation of SIXEL-IMAGE."
                  (elt colour 0) " "
                  "c #"
                  (format "%02x%02x%02x"
-                         (elt colour 1)
-                         (elt colour 2)
-                         (elt colour 3))
+                         (/ (* 255 (elt colour 1)) 100)
+                         (/ (* 255 (elt colour 2)) 100)
+                         (/ (* 255 (elt colour 3)) 100))
                  "\""))
               colour-map)
-      ",")
-     "\",\n"
+      ",\n")
+     ",\n"
      "\"-- c #000000\"")))
 
 (defun sixel-image-to-xpm-pixels (sixel-image)
@@ -171,12 +187,14 @@ 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)))
-    (concat
-     "/* XPM */"
-     "static char * pixmap = {"
-     (sixel-image-to-xpm-values sixel-image) ",\n"
-     (sixel-image-to-xpm-colours sixel-image) ",\n"
-     (sixel-image-to-xpm-pixels sixel-image) "};")))
+         (sixel-image (sixel-pad-rows (sixel-process-data data-string))))
+    (if (string-prefix-p "\eP" string)
+        (concat
+         "/* XPM */"
+         "static char * pixmap[] = {"
+         (sixel-image-to-xpm-values sixel-image) ",\n"
+         (sixel-image-to-xpm-colours sixel-image) ",\n"
+         (sixel-image-to-xpm-pixels sixel-image) "};")
+      (error "Incorrecly formatted sixel string."))))
 
 ;; sixel.el ends here