Initial commit.
[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 (defvar sixel-colour-map nil
18   "Map of two-character names to RGB colour triples.")
19
20 (defvar sixel-current-colour nil
21   "Current colour.")
22
23 (defun sixel-compute-row-length (string)
24   (apply 'max
25          (mapcar
26           (lambda (substr)
27             (apply 'max (mapcar
28                          (lambda (subsubstr)
29                            (length (subsubstr)))
30                          (split-string substr "$"))))
31           (split-string string -))))
32
33 (defun sixel-tag-bits (n tag bit-count)
34   "Create a list of length BIT-COUNT with the elements corresponding to the
35 true bits of N set to TAG."
36   (if (= bit-count 0)
37       nil
38     (cons (if (= (% n 2) 1)
39               tag
40             nil)
41           (sixel-tag-bits (/ n 2) tag (1- bit-count)))))
42
43 (defun sixel-make-sixel-from-char (char tag)
44   "Retrieve sequence of bits to be set."
45   (sixel-tag-bits (- char 63) tag 6))
46
47 (defun sixel-union (sixel1 sixel2)
48   "Returns a union of the two sixels, SIXEL1 and SIXEL2.
49 When both sixels specify a tag for the same pixel, the tag
50 from SIXEL2 wins."
51   (seq-mapn (lambda (tag1 tag2)
52               (or tag2 tag1))
53             sixel1 sixel2))
54
55 (defun sixel-process-row (string)
56   (let ((idx-in 0)
57         (idx-out 0)
58         result)
59     (while (< idx-in (length string))
60       (let (trunc-string (substring string index-in))
61         (cond
62          ((string-match "^#\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\)"
63                         trunc-string)
64           (let ((tag (intern (string-to (match-string 1 trunc-string))))
65                 (mode (match-string 2 trunc-string))
66                 (r (string-to-number (match-string 3 trunc-string)))
67                 (g (string-to-number (match-string 4 trunc-string)))
68                 (b (string-to-number (match-string 5 trunc-string))))
69             (add-to-list 'sixel-colour-map (list (tag r g b)))
70             (setq idx-in (match-end 0))))
71          ((string-match "^#\\([0-9]+\\)" trunc-string)
72           (let ((tag (intern (match-string 1 trunc-string))))
73             (setq current-colour tag)
74             (setq idx-in (match-end 0))))
75          ((string-match "^!\\([0-9]+\\)\\([?-~]\\)" trunc-string)
76           (let ((repeat-count (string-to-number (match-string 1 trunc-string)))
77                 (char (elt (match-string 2 trunc-string) 0)))
78             (dotimes (i repeat-count)
79               (unless (< idx-out (length result))
80                 (add-to-list result (make-vector 6 nil)))
81               (let ((sixel (elt result idx-out))))))))))))
82                 
83               
84
85 (sixel-get-rows test-string)
86
87 (defun sixel-to-xpm (string)
88   "Converts the given sixel string into an XPM image."
89
90   (let* ((sp (split-string string "q"))
91          (control-string (car sp))
92          ())))