1 ;;; sixel.el --- minor mode for processing sixel graphics
3 ;; Copyright (C) 2019 Tim Vaughan
5 ;; Author: Tim Vaughan <tgvaughan@gmail.com>
6 ;; Created: 19 May 2019
9 ;; Homepage: https://github.com/tgvaughan/sixel
10 ;; Package-Requires: ((emacs "25"))
18 "#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0"
23 (defun sixel-get-params (string)
24 "Retrieve the sixel parameters."
25 (car (split-string string "q")))
27 (defun sixel-get-data (string)
28 "Retrieve data string."
29 (substring string (1+ (string-match "q" string))))
31 (defun sixel-tag-bits (sixel n tag)
32 "Set bits of SIXEL corresponding to N with to the value TAG."
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
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))
48 (defun sixel-process-data (string)
49 "Convert STRING into a list of lists representing individual sixels."
52 (goto-char (point-min))
55 current-colour colour-map
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)))
67 ((looking-at "#\\([0-9]+\\)")
68 (let ((tag (intern (match-string 1))))
69 (setq current-colour tag)))
75 (push (reverse this-row) rows)
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)
84 (sixel-tag-sixel-in-row this-row idx-out char current-colour))
85 (setq idx-out (1+ idx-out)))))
87 ((looking-at "\\([?-~]\\)")
88 (let ((char (elt (match-string 1) 0)))
90 (sixel-tag-sixel-in-row this-row idx-out char current-colour))
91 (setq idx-out (1+ idx-out))))
93 (t (setq finished t)))
95 (goto-char (match-end 0)))
96 (push (reverse this-row) rows)
100 ;; sixel.el ends here