1 ;;; sixel.el --- minor mode for processing sixel graphics
3 ;; Copyright (C) 2019 Tim Vaughan
5 ;; Author: Tim Vaughan <tgvaughan@gmail.com>
6 ;; Created: 19 May 2019
9 ;; Homepage: https://github.com/tgvaughan/sixel
10 ;; Package-Requires: ((emacs "25"))
18 "#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0"
23 (defun sixel-get-params (string)
24 "Retrieve the sixel parameters."
25 (car (split-string (substring string 2) "q")))
27 (defun sixel-get-data (string)
28 "Retrieve data string."
29 (substring string (1+ (string-match "q" string))))
31 (defun sixel-tag-bits (sixel n tag)
32 "Set bits of SIXEL corresponding to N with to the value TAG."
38 (defun sixel-tag-sixel-in-row (row index char tag)
39 "Tag the bits of the sixel at INDEX in the list identified by
40 the variable ROW-VARIABLE corresponding to input character CHAR
42 (while (not (< index (length row)))
43 (push (make-vector 6 nil) row))
44 (let ((sixel (elt row (- (length row) 1 index))))
45 (sixel-tag-bits sixel (- char 63) tag))
48 (defun sixel-process-data (string)
49 "Convert STRING into a list of lists representing individual sixels.
50 Returns a sixel image object."
53 (goto-char (point-min))
56 current-colour colour-map
61 ((looking-at "#\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\)")
62 (let ((tag (format "%02x" (string-to-number (match-string 1))))
63 (mode (match-string 2))
64 (r (string-to-number (match-string 3)))
65 (g (string-to-number (match-string 4)))
66 (b (string-to-number (match-string 5))))
67 (push (list tag r g b) colour-map)))
68 ;; Set current colour:
69 ((looking-at "#\\([0-9]+\\)")
70 (let ((tag (format "%02x" (string-to-number (match-string 1)))))
71 (setq current-colour tag)))
77 (push (reverse this-row) rows)
80 ;; RLE sixel char sequence:
81 ((looking-at "!\\([0-9]+\\)\\([?-~]\\)")
82 (let ((repeat-count (string-to-number (match-string 1)))
83 (char (elt (match-string 2) 0)))
84 (dotimes (i repeat-count)
86 (sixel-tag-sixel-in-row this-row idx-out char current-colour))
87 (setq idx-out (1+ idx-out)))))
89 ((looking-at "\\([?-~]\\)") ; Sixel char
90 (let ((char (elt (match-string 1) 0)))
92 (sixel-tag-sixel-in-row this-row idx-out char current-colour))
93 (setq idx-out (1+ idx-out))))
94 ;; Termination sequence:
95 ((looking-at "
\e\\\\")
98 ((looking-at "[[:ascii:]]")))
99 (goto-char (match-end 0)))
100 (push (reverse this-row) rows)
104 (defun sixel-pad-rows (sixel-image)
105 "Pad out contents of rows in SIXEL-IMAGE so that all rows are the same length."
106 (let ((width (car (sixel-image-dims sixel-image)))
107 (rows (cdr sixel-image)))
108 (dotimes (row-idx (length rows))
109 (let* ((row-cdr (nthcdr row-idx rows))
110 (row-width (length (car row-cdr))))
111 (if (< row-width width)
112 (setcar row-cdr (append (car row-cdr)
113 (make-list (- width row-width)
114 [nil nil nil nil nil nil])))))))
117 (defun sixel-image-colour-map (sixel-image)
118 "Extract colour map from SIXEL-DATA."
121 (defun sixel-image-sixels (sixel-image)
122 "Extract sixels from SIXEL-DATA."
125 (defun sixel-image-dims (sixel-image)
126 "Computes image width from SIXEL-DATA. Returns pair (width . height)."
127 (let ((sixels (sixel-image-sixels sixel-image)))
129 (apply #'max (mapcar (lambda (row) (length row)) sixels))
130 (* 6 (length sixels)))))
132 (defun sixel-image-to-xpm-values (sixel-image)
133 "Produce string representing parameter values component of XPM
134 representation of SIXEL-IMAGE."
135 (let* ((dims (sixel-image-dims sixel-image))
136 (colour-map (sixel-image-colour-map sixel-image))
137 (n-colours (1+ (length colour-map))))
139 (number-to-string (car dims)) " "
140 (number-to-string (cdr dims)) " "
141 (number-to-string n-colours) " 2\"")))
143 (defun sixel-image-to-xpm-colours (sixel-image)
144 "Produce string representing colour definitions component of XPM
145 representation of SIXEL-IMAGE."
146 (let ((colour-map (sixel-image-colour-map sixel-image)))
149 (mapcar (lambda (colour)
154 (format "%02x%02x%02x"
162 "\"-- c #000000\"")))
164 (defun sixel-image-to-xpm-pixels (sixel-image)
165 "Produce string representating pixels component of XPM representation
170 (mapcar (lambda (sixel-row)
174 (mapcar (lambda (sixel)
175 (let ((pixel (elt sixel i)))
180 (number-sequence 0 5))
182 (sixel-image-sixels sixel-image))
186 (defun sixel-to-xpm (string)
187 "Returns an XPM image representation of the SIXEL graphic encoded in STRING."
188 (let* ((param-string (sixel-get-params string))
189 (data-string (sixel-get-data string))
190 (sixel-image (sixel-pad-rows (sixel-process-data data-string))))
191 (if (string-prefix-p "
\eP" string)
194 "static char * pixmap[] = {"
195 (sixel-image-to-xpm-values sixel-image) ",\n"
196 (sixel-image-to-xpm-colours sixel-image) ",\n"
197 (sixel-image-to-xpm-pixels sixel-image) "};")
198 (error "Incorrecly formatted sixel string."))))
200 ;; sixel.el ends here