commit b96b08d54202a1e017de90b1166d06753d65d601
parent bfde63ffe01ad3b40b29b46fbec2f299ed8d9f5b
Author: Tomas Hlavaty <tom@logand.com>
Date: Sun, 24 May 2020 03:55:58 +0200
use framebuffer-png-size instead of file program
Diffstat:
1 file changed, 51 insertions(+), 4 deletions(-)
diff --git a/emacs-framebuffer.el b/emacs-framebuffer.el
@@ -36,11 +36,58 @@
(insert (format "0;1;%d;%d;%d;%d;;;;;%s\n" x y w h (expand-file-name file)))
(call-process-region (point-min) (point-max) "w3mimgdisplay")))
-(defun framebuffer-image-size (file)
+(defun framebuffer-buffer-brook ()
+ (lambda ()
+ (let ((z (char-after)))
+ (when z
+ (goto-char (1+ (point)))
+ z))))
+
+(defun framebuffer-next-u8 (brook)
+ (let ((z (funcall brook)))
+ (when z
+ (if (<= 0 z 255)
+ z
+ (error "expected octet, got %s" z)))))
+
+(defun framebuffer-next-u16 (brook)
+ (+ (* 256 (framebuffer-next-u8 brook))
+ (framebuffer-next-u8 brook)))
+
+(defun framebuffer-next-u32 (brook)
+ (+ (* 65536 (framebuffer-next-u16 brook))
+ (framebuffer-next-u16 brook)))
+
+(defun framebuffer-png-size (file)
(with-temp-buffer
- (call-process "file" nil t nil (expand-file-name file))
- (search-backward-regexp ", \\([0-9]+\\)+x\\([0-9]+\\),")
- (cons (read (match-string 1)) (read (match-string 2)))))
+ (set-buffer-multibyte nil)
+ (insert-file-contents-literally file)
+ (let ((brook (framebuffer-buffer-brook)))
+ (when (and (= 137 (funcall brook))
+ (= 80 (funcall brook))
+ (= 78 (funcall brook))
+ (= 71 (funcall brook))
+ (= 13 (funcall brook))
+ (= 10 (funcall brook))
+ (= 26 (funcall brook))
+ (= 10 (funcall brook))
+ (<= 12 (framebuffer-next-u32 brook))
+ (= ?I (funcall brook))
+ (= ?H (funcall brook))
+ (= ?D (funcall brook))
+ (= ?R (funcall brook)))
+ (cons (framebuffer-next-u32 brook)
+ (framebuffer-next-u32 brook))))))
+
+(defun framebuffer-image-size (file)
+ (cond
+ ((string-match "png\\|PNG" (file-name-extension file))
+ (framebuffer-png-size file))
+ (t
+ (with-temp-buffer
+ (call-process "file" nil t nil (expand-file-name file))
+ (search-backward-regexp ", \\([0-9]+\\)+x\\([0-9]+\\),")
+ (cons (read (match-string 1)) (read (match-string 2)))))))
(defun framebuffer-image-file (file)
(interactive "fFile: ")