Add start and stop chars to sixel strings, skip unknown chars.
[sixel.git] / sixel.el
1 ;;; sixel.el --- minor mode for processing sixel graphics
2
3 ;; Copyright (C) 2019 Tim Vaughan
4
5 ;; Author: Tim Vaughan <tgvaughan@gmail.com>
6 ;; Created: 19 May 2019
7 ;; Version: 1.0.0
8 ;; Keywords: 
9 ;; Homepage: https://github.com/tgvaughan/sixel
10 ;; Package-Requires: ((emacs "25"))
11
12 ;;; Commentary:
13
14 ;;; Code:
15
16 (defvar test-string
17   (concat "\ePq"
18           "#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0"
19           "#1~~@@vv@@~~@@~~$"
20           "#2??}}GG}}??}}??-"
21           "#1!14@\e\\"))
22
23 (defun sixel-get-params (string)
24   "Retrieve the sixel parameters."
25   (car (split-string (substring string 2) "q")))
26
27 (defun sixel-get-data (string)
28   "Retrieve data string."
29   (substring string (1+ (string-match "q" string))))
30
31 (defun sixel-tag-bits (sixel n tag)
32   "Set bits of SIXEL corresponding to N with to the value TAG."
33   (dotimes (i 6)
34     (if (= (% n 2) 1)
35         (aset sixel i tag))
36     (setq n (/ n 2))))
37
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
41 with TAG."
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))
46   row)
47
48 (defun sixel-process-data (string)
49   "Convert STRING into a list of lists representing individual sixels.
50 Returns a sixel image object."
51   (with-temp-buffer
52     (insert string)
53     (goto-char (point-min))
54     (let ((idx-out 0)
55           this-row rows
56           current-colour colour-map
57           finished)
58       (while  (not finished)
59         (cond
60          ;; Define colour:
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)))
72          ;; Carriage return:
73          ((looking-at "\\$")
74           (setq idx-out 0))
75          ;; New line:
76          ((looking-at "-")
77           (push (reverse this-row) rows)
78           (setq this-row nil)
79           (setq idx-out 0))
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)
85               (setq this-row
86                     (sixel-tag-sixel-in-row this-row idx-out char current-colour))
87               (setq idx-out (1+ idx-out)))))
88          ;; Sixel char:
89          ((looking-at "\\([?-~]\\)") ; Sixel char
90           (let ((char (elt (match-string 1) 0)))
91             (setq this-row
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\\\\")
96           (setq finished t))
97          ;; Skip other char:
98          ((looking-at "[[:ascii:]]")))
99         (goto-char (match-end 0)))
100       (push (reverse this-row) rows)
101       (cons colour-map
102             (reverse rows)))))
103
104 (defun sixel-image-colour-map (sixel-image)
105   "Extract colour map from SIXEL-DATA."
106   (car sixel-image))
107
108 (defun sixel-image-sixels (sixel-image)
109   "Extract sixels from SIXEL-DATA."
110   (cdr sixel-image))
111
112 (defun sixel-image-dims (sixel-image)
113   "Computes image width from SIXEL-DATA. Returns pair (width . height)."
114   (let ((sixels (sixel-image-sixels sixel-image)))
115     (cons
116      (apply #'max (mapcar (lambda (row) (length row)) sixels))
117      (* 6 (length sixels)))))
118
119 (defun sixel-image-to-xpm-values (sixel-image)
120   "Produce string representing parameter values component of XPM
121 representation of SIXEL-IMAGE."
122   (let* ((dims (sixel-image-dims sixel-image))
123          (colour-map (sixel-image-colour-map sixel-image))
124          (n-colours (1+ (length colour-map))))
125     (concat "\""
126             (number-to-string (car dims)) " "
127             (number-to-string (cdr dims)) " "
128             (number-to-string n-colours) " 2\"")))
129
130 (defun sixel-image-to-xpm-colours (sixel-image)
131   "Produce string representing colour definitions component of XPM
132 representation of SIXEL-IMAGE."
133   (let ((colour-map (sixel-image-colour-map sixel-image)))
134     (concat
135      (string-join
136       (mapcar (lambda (colour)
137                 (concat
138                  "\""
139                  (elt colour 0) " "
140                  "c #"
141                  (format "%02x%02x%02x"
142                          (elt colour 1)
143                          (elt colour 2)
144                          (elt colour 3))
145                  "\""))
146               colour-map)
147       ",\n")
148      ",\n"
149      "\"-- c #000000\"")))
150
151 (defun sixel-image-to-xpm-pixels (sixel-image)
152   "Produce string representating pixels component of XPM representation
153 of SIXEL-IMAGE."
154   (concat
155    "\""
156    (string-join
157     (mapcar (lambda (sixel-row)
158               (string-join
159                (mapcar (lambda (i)
160                          (string-join
161                           (mapcar (lambda (sixel)
162                                     (let ((pixel (elt sixel i)))
163                                       (if pixel
164                                           pixel
165                                         "--")))
166                                   sixel-row)))
167                        (number-sequence 0 5))
168                "\",\n\""))
169             (sixel-image-sixels sixel-image))
170     "\",\n\"")
171    "\""))
172
173 (defun sixel-to-xpm (string)
174   "Returns an XPM image representation of the SIXEL graphic encoded in STRING."
175   (let* ((param-string (sixel-get-params string))
176          (data-string (sixel-get-data string))
177          (sixel-image (sixel-process-data data-string)))
178     (if (string-prefix-p "\eP" string)
179         (concat
180          "/* XPM */"
181          "static char * pixmap[] = {"
182          (sixel-image-to-xpm-values sixel-image) ",\n"
183          (sixel-image-to-xpm-colours sixel-image) ",\n"
184          (sixel-image-to-xpm-pixels sixel-image) "};")
185       (error "Incorrecly formatted sixel string."))))
186
187 ;; sixel.el ends here