Switched to using temp buffer for processing string.
[sixel.git] / sixel.el
1 (defvar test-string
2   (concat "q"
3           "#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0"
4           "#1~~@@vv@@~~@@~~$"
5           "#2??GG????-"
6           "#1!14@"))
7
8
9 (defun sixel-get-params (string)
10   "Retrieve the sixel parameters."
11   (car (split-string string "q")))
12
13 (defun sixel-get-data (string)
14   "Retrieve data string."
15   (substring string (1+ (string-match "q" string))))
16
17 (defun sixel-compute-row-length (string)
18   (apply 'max
19          (mapcar
20           (lambda (substr)
21             (apply 'max (mapcar
22                          (lambda (subsubstr)
23                            (length (subsubstr)))
24                          (split-string substr "$"))))
25           (split-string string -))))
26
27 (defun sixel-tag-bits (sixel n tag)
28   "Set bits of SIXEL corresponding to N with to the value TAG."
29   (dotimes (i 6)
30     (if (= (% n 2) 1)
31         (aset sixel i tag))
32     (setq n (/ n 2))))
33
34 (defun sixel-tag-sixel-in-row (row-variable index char tag)
35   "Tag the bits of the sixel at INDEX in the list identified by
36 the variable ROW-VARIABLE corresponding to input character CHAR
37 with TAG."
38   (while (not (< index (length (symbol-value row-variable))))
39     (add-to-list row-variable (make-vector 6 nil)))
40   (let ((sixel (elt (symbol-value row-variable) index)))
41     (sixel-tag-bits sixel (- char 63) tag)))
42
43 (defun sixel-process-data (string)
44   "Convert sixel string into a list of lists representing individual sixels."
45   (with-temp-buffer
46     (insert string)
47     (goto-char (point-min))
48     (let ((idx-out 0)
49           this-row rows
50           current-colour colour-map
51           finished)
52       (while  (not finished)
53         (cond
54          ((looking-at "#\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\)")
55           (let ((tag (intern (match-string 1)))
56                 (mode (match-string 2))
57                 (r (string-to-number (match-string 3)))
58                 (g (string-to-number (match-string 4)))
59                 (b (string-to-number (match-string 5))))
60             (push (list tag r g b) colour-map)))
61
62          ((looking-at "#\\([0-9]+\\)")
63           (let ((tag (intern (match-string 1 trunc-string))))
64             (setq current-colour tag)))
65
66          ((looking-at "$")
67           (setq idx-out 0))
68
69          ((looking-at "^-")
70           (push this-row 'rows)
71           (setq this-row nil)
72           (setq idx-out 0))
73
74          ((looking-at "^!\\([0-9]+\\)\\([?-~]\\)")
75           (let ((repeat-count (string-to-number (match-string 1)))
76                 (char (elt (match-string 2 trunc-string) 0)))
77             (dotimes (i repeat-count)
78               (sixel-tag-sixel-in-row 'result idx-out char current-colour))))
79
80          ((looking-at "^\\([?-~]\\)")
81           (let ((char (elt (match-string 1 trunc-string) 0)))
82             (sixel-tag-sixel-in-row 'result idx-out char current-colour)))
83
84          ((= (point) (point-max))
85           (setq finished t))
86
87          (t (error "Invalid characters found in input string.")))
88
89         (goto-char (match-end 0)))
90       (push this-row 'rows))))