Generated XPMs are now finally valid.
[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 "q"
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@"))
22
23 (defun sixel-get-params (string)
24   "Retrieve the sixel parameters."
25   (car (split-string string "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          ((looking-at "#\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\)")
61           (let ((tag (format "%02x" (string-to-number (match-string 1))))
62                 (mode (match-string 2))
63                 (r (string-to-number (match-string 3)))
64                 (g (string-to-number (match-string 4)))
65                 (b (string-to-number (match-string 5))))
66             (push (list tag r g b) colour-map)))
67
68          ((looking-at "#\\([0-9]+\\)")
69           (let ((tag (format "%02x" (string-to-number (match-string 1)))))
70             (setq current-colour tag)))
71
72          ((looking-at "\\$")
73           (setq idx-out 0))
74
75          ((looking-at "-")
76           (push (reverse this-row) rows)
77           (setq this-row nil)
78           (setq idx-out 0))
79
80          ((looking-at "!\\([0-9]+\\)\\([?-~]\\)")
81           (let ((repeat-count (string-to-number (match-string 1)))
82                 (char (elt (match-string 2) 0)))
83             (dotimes (i repeat-count)
84               (setq this-row
85                     (sixel-tag-sixel-in-row this-row idx-out char current-colour))
86               (setq idx-out (1+ idx-out)))))
87
88          ((looking-at "\\([?-~]\\)")
89           (let ((char (elt (match-string 1) 0)))
90             (setq this-row
91                   (sixel-tag-sixel-in-row this-row idx-out char current-colour))
92             (setq idx-out (1+ idx-out))))
93
94          (t (setq finished t)))
95
96         (goto-char (match-end 0)))
97       (push (reverse this-row) rows)
98       (cons colour-map
99             (reverse rows)))))
100
101 (defun sixel-image-colour-map (sixel-image)
102   "Extract colour map from SIXEL-DATA."
103   (car sixel-image))
104
105 (defun sixel-image-sixels (sixel-image)
106   "Extract sixels from SIXEL-DATA."
107   (cdr sixel-image))
108
109 (defun sixel-image-dims (sixel-image)
110   "Computes image width from SIXEL-DATA. Returns pair (width . height)."
111   (let ((sixels (sixel-image-sixels sixel-image)))
112     (cons
113      (apply #'max (mapcar (lambda (row) (length row)) sixels))
114      (* 6 (length sixels)))))
115
116 (defun sixel-image-to-xpm-values (sixel-image)
117   "Produce string representing parameter values component of XPM
118 representation of SIXEL-IMAGE."
119   (let* ((dims (sixel-image-dims sixel-image))
120          (colour-map (sixel-image-colour-map sixel-image))
121          (n-colours (1+ (length colour-map))))
122     (concat "\""
123             (number-to-string (car dims)) " "
124             (number-to-string (cdr dims)) " "
125             (number-to-string n-colours) " 2\"")))
126
127 (defun sixel-image-to-xpm-colours (sixel-image)
128   "Produce string representing colour definitions component of XPM
129 representation of SIXEL-IMAGE."
130   (let ((colour-map (sixel-image-colour-map sixel-image)))
131     (concat
132      (string-join
133       (mapcar (lambda (colour)
134                 (concat
135                  "\""
136                  (elt colour 0) " "
137                  "c #"
138                  (format "%02x%02x%02x"
139                          (elt colour 1)
140                          (elt colour 2)
141                          (elt colour 3))
142                  "\""))
143               colour-map)
144       ",\n")
145      ",\n"
146      "\"-- c #000000\"")))
147
148 (defun sixel-image-to-xpm-pixels (sixel-image)
149   "Produce string representating pixels component of XPM representation
150 of SIXEL-IMAGE."
151   (concat
152    "\""
153    (string-join
154     (mapcar (lambda (sixel-row)
155               (string-join
156                (mapcar (lambda (i)
157                          (string-join
158                           (mapcar (lambda (sixel)
159                                     (let ((pixel (elt sixel i)))
160                                       (if pixel
161                                           pixel
162                                         "--")))
163                                   sixel-row)))
164                        (number-sequence 0 5))
165                "\",\n\""))
166             (sixel-image-sixels sixel-image))
167     "\",\n\"")
168    "\""))
169
170 (defun sixel-to-xpm (string)
171   "Returns an XPM image representation of the SIXEL graphic encoded in STRING."
172   (let* ((param-string (sixel-get-params string))
173          (data-string (sixel-get-data string))
174          (sixel-image (sixel-process-data data-string)))
175     (concat
176      "/* XPM */"
177      "static char * pixmap[] = {"
178      (sixel-image-to-xpm-values sixel-image) ",\n"
179      (sixel-image-to-xpm-colours sixel-image) ",\n"
180      (sixel-image-to-xpm-pixels sixel-image) "};")))
181
182 ;; sixel.el ends here