From: Tim Vaughan Date: Mon, 20 May 2019 20:45:11 +0000 (+0200) Subject: XPM values and colours output working. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?p=sixel.git;a=commitdiff_plain;h=6b9058e99e890e33246c6e4e3cad2d4dc81dd951 XPM values and colours output working. --- diff --git a/sixel.el b/sixel.el index ed46c76..f2532e5 100644 --- a/sixel.el +++ b/sixel.el @@ -46,7 +46,8 @@ with TAG." row) (defun sixel-process-data (string) - "Convert STRING into a list of lists representing individual sixels." + "Convert STRING into a list of lists representing individual sixels. +Returns a sixel image object." (with-temp-buffer (insert string) (goto-char (point-min)) @@ -57,7 +58,7 @@ with TAG." (while (not finished) (cond ((looking-at "#\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\)") - (let ((tag (intern (match-string 1))) + (let ((tag (format "%02x" (string-to-number (match-string 1)))) (mode (match-string 2)) (r (string-to-number (match-string 3))) (g (string-to-number (match-string 4))) @@ -65,7 +66,7 @@ with TAG." (push (list tag r g b) colour-map))) ((looking-at "#\\([0-9]+\\)") - (let ((tag (intern (match-string 1)))) + (let ((tag (format "%02x" (string-to-number (match-string 1))))) (setq current-colour tag))) ((looking-at "\\$") @@ -94,7 +95,62 @@ with TAG." (goto-char (match-end 0))) (push (reverse this-row) rows) - (list color-map + (cons colour-map (reverse rows))))) +(defun sixel-image-colour-map (sixel-image) + "Extract colour map from SIXEL-DATA." + (car sixel-image)) + +(defun sixel-image-sixels (sixel-image) + "Extract sixels from SIXEL-DATA." + (cdr sixel-image)) + +(defun sixel-image-dims (sixel-image) + "Computes image width from SIXEL-DATA. Returns pair (width . height)." + (let ((sixels (sixel-image-sixels sixel-image))) + (cons + (apply #'max (mapcar (lambda (row) (length row)) sixels)) + (length sixels)))) + +(defun sixel-image-to-xpm-values (sixel-image) + (let* ((dims (sixel-image-dims sixel-image)) + (colour-map (sixel-image-colour-map sixel-image)) + (n-colours (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) + (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) + ","))) + +(defun sixel-to-xpm (string) + "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) + ;; (sixel-data-to-XPM-pixels data) + "};"))) + + + ;; sixel.el ends here