commit ccfbdf3aceb05d52499bd20cb4af1ea454e467a7 parent 6e02ac856defd64a83b2cd32c9e84a46747e553e Author: Tomas Hlavaty <tom@logand.com> Date: Sat, 20 Jun 2020 23:17:47 +0200 framebuffer-screenshot also as pbm or pgm Diffstat:
M | emacs-framebuffer.el | | | 50 | ++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 50 insertions(+), 0 deletions(-)
diff --git a/emacs-framebuffer.el b/emacs-framebuffer.el @@ -53,6 +53,8 @@ (defun framebuffer-file-format (file) (let ((ext (file-name-extension file))) (cond + ((equal ext "pbm") 'pbm) + ((equal ext "pgm") 'pgm) ((equal ext "ppm") 'ppm) ))) @@ -364,6 +366,54 @@ (with-temp-buffer (set-buffer-multibyte nil) (ecase (framebuffer-file-format file) + (pbm + (insert "P4\n") + (insert (format "%d %d\n" w h)) + (ecase bpp + (32 + (dotimes (y h) + (with-current-buffer fb + (goto-char (+ (point-min) (* 4 vw y)))) + (let ((i 0) + (n 0)) + (dotimes (x w) + (let (r g b a) + (with-current-buffer fb + (setq b (char-after) + g (char-after (goto-char (1+ (point)))) + r (char-after (goto-char (1+ (point)))) + a (char-after (goto-char (1+ (point))))) + (goto-char (1+ (point)))) + (setq n (+ (ash n 1) (if (plusp (+ r g b)) 1 0))) + (incf i) + (unless (< i 8) + (insert n) + (setq i 0 + n 0)))) + (when (< i 8) + (insert n)))))) + (let ((coding-system-for-write 'raw-text-unix)) + (write-region (point-min) (point-max) file))) + (pgm + (insert "P5\n") + (insert (format "%d %d\n" w h)) + (insert "255\n") + (ecase bpp + (32 + (dotimes (y h) + (with-current-buffer fb + (goto-char (+ (point-min) (* 4 vw y)))) + (dotimes (x w) + (let (r g b a) + (with-current-buffer fb + (setq b (char-after) + g (char-after (goto-char (1+ (point)))) + r (char-after (goto-char (1+ (point)))) + a (char-after (goto-char (1+ (point))))) + (goto-char (1+ (point)))) + (insert (floor (+ r g b) 3))))))) + (let ((coding-system-for-write 'raw-text-unix)) + (write-region (point-min) (point-max) file))) (ppm (insert "P6\n") (insert (format "%d %d\n" w h))