XPM values and colours output working.
[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      (length sixels))))
115
116 (defun sixel-image-to-xpm-values (sixel-image)
117   (let* ((dims (sixel-image-dims sixel-image))
118          (colour-map (sixel-image-colour-map sixel-image))
119          (n-colours (length colour-map)))
120     (concat "\""
121             (number-to-string (car dims)) " "
122             (number-to-string (cdr dims)) " "
123             (number-to-string n-colours) " 2\"")))
124
125 (defun sixel-image-to-xpm-colours (sixel-image)
126   (let ((colour-map (sixel-image-colour-map sixel-image)))
127     (string-join
128      (mapcar (lambda (colour)
129                (concat
130                 "\""
131                 (elt colour 0) " "
132                 "c #"
133                 (format "%02x%02x%02x"
134                         (elt colour 1)
135                         (elt colour 2)
136                         (elt colour 3))
137                 "\""))
138              colour-map)
139      ",")))
140
141 (defun sixel-to-xpm (string)
142   "Returns an XPM image representation of the SIXEL graphic encoded in STRING."
143   (let* ((param-string (sixel-get-params string))
144          (data-string (sixel-get-data string))
145          (sixel-image (sixel-process-data data-string)))
146     (concat
147      "/* XPM */"
148      "static char * pixmap = {"
149      (sixel-image-to-xpm-values sixel-image) ",\n"
150      (sixel-image-to-xpm-colours sixel-image)
151      ;; (sixel-data-to-XPM-pixels data)
152      "};")))
153
154          
155
156 ;; sixel.el ends here