(defvar test-string (concat "q" "#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0" "#1~~@@vv@@~~@@~~$" "#2??GG????-" "#1!14@")) (defun sixel-get-params (string) "Retrieve the sixel parameters." (car (split-string string "q"))) (defun sixel-get-data (string) "Retrieve data string." (substring string (1+ (string-match "q" string)))) (defvar sixel-colour-map nil "Map of two-character names to RGB colour triples.") (defvar sixel-current-colour nil "Current colour.") (defun sixel-compute-row-length (string) (apply 'max (mapcar (lambda (substr) (apply 'max (mapcar (lambda (subsubstr) (length (subsubstr))) (split-string substr "$")))) (split-string string -)))) (defun sixel-tag-bits (sixel n tag) "Set bits of SIXEL corresponding to N with to the value TAG." (dotimes (i 6) (if (= (% n 2) 1) (aset sixel i tag)) (setq n (/ n 2)))) (defun sixel-tag-sixel-in-row (row-variable index char tag) "Tag the bits of the sixel at INDEX in the list identified by the variable ROW-VARIABLE corresponding to input character CHAR with TAG." (while (not (< index (length (symbol-value row-variable)))) (add-to-list row-variable (make-vector 6 nil))) (let ((sixel (elt (symbol-value row-variable) index))) (sixel-tag-bits sixel (- char 63) tag))) (defun sixel-process-row (string) (let ((idx-in 0) (idx-out 0) result) (while (< idx-in (length string)) (let (trunc-string (substring string index-in)) (cond ((string-match "^#\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\)" trunc-string) (let ((tag (intern (string-to (match-string 1 trunc-string)))) (mode (match-string 2 trunc-string)) (r (string-to-number (match-string 3 trunc-string))) (g (string-to-number (match-string 4 trunc-string))) (b (string-to-number (match-string 5 trunc-string)))) (add-to-list 'sixel-colour-map (list (tag r g b))) (setq idx-in (match-end 0)))) ((string-match "^#\\([0-9]+\\)" trunc-string) (let ((tag (intern (match-string 1 trunc-string)))) (setq sixel-current-colour tag) (setq idx-in (match-end 0)))) ((string-match "^$" trunc-string) (setq idx-out 0) (setq idx-in (match-end 0 trunc-string))) ((string-match "^!\\([0-9]+\\)\\([?-~]\\)" trunc-string) (let ((repeat-count (string-to-number (match-string 1 trunc-string))) (char (elt (match-string 2 trunc-string) 0))) (dotimes (i repeat-count) (sixel-tag-sixel-in-row 'result idx-out char sixel-current-colour) (setq idx-out (+1 idx-out))))) ((string-match "^\\([?-~]\\)" trunc-string) (let ((char (elt (match-string 1 trunc-string) 0))) (sixel-tag-sixel-in-row 'result idx-out char sixel-current-colour)) (setq idx-out (+1 idx-out)))))))) (sixel-get-rows test-string) (defun sixel-to-xpm (string) "Converts the given sixel string into an XPM image." (let* ((sp (split-string string "q")) (control-string (car sp)) ())))