ed46c7623541b2f37b5a17eef8bf88e4f4d5a00f
[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   (with-temp-buffer
51     (insert string)
52     (goto-char (point-min))
53     (let ((idx-out 0)
54           this-row rows
55           current-colour colour-map
56           finished)
57       (while  (not finished)
58         (cond
59          ((looking-at "#\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\);\\([0-9]+\\)")
60           (let ((tag (intern (match-string 1)))
61                 (mode (match-string 2))
62                 (r (string-to-number (match-string 3)))
63                 (g (string-to-number (match-string 4)))
64                 (b (string-to-number (match-string 5))))
65             (push (list tag r g b) colour-map)))
66
67          ((looking-at "#\\([0-9]+\\)")
68           (let ((tag (intern (match-string 1))))
69             (setq current-colour tag)))
70
71          ((looking-at "\\$")
72           (setq idx-out 0))
73
74          ((looking-at "-")
75           (push (reverse this-row) rows)
76           (setq this-row nil)
77           (setq idx-out 0))
78
79          ((looking-at "!\\([0-9]+\\)\\([?-~]\\)")
80           (let ((repeat-count (string-to-number (match-string 1)))
81                 (char (elt (match-string 2) 0)))
82             (dotimes (i repeat-count)
83               (setq this-row
84                     (sixel-tag-sixel-in-row this-row idx-out char current-colour))
85               (setq idx-out (1+ idx-out)))))
86
87          ((looking-at "\\([?-~]\\)")
88           (let ((char (elt (match-string 1) 0)))
89             (setq this-row
90                   (sixel-tag-sixel-in-row this-row idx-out char current-colour))
91             (setq idx-out (1+ idx-out))))
92
93          (t (setq finished t)))
94
95         (goto-char (match-end 0)))
96       (push (reverse this-row) rows)
97       (list color-map
98             (reverse rows)))))
99
100 ;; sixel.el ends here