commit 07ff446574552a2fac29d344766178712b8e3aa9 parent f34d745890917c9e2184a67f16c20c497a8dc151 Author: Tomas Hlavaty <tom@logand.com> Date: Sun, 24 May 2020 07:03:43 +0200 improve framebuffer-image-size Diffstat:
M | emacs-framebuffer.el | | | 118 | ++++++++++++++++++++++++++++++------------------------------------------------- |
1 file changed, 45 insertions(+), 73 deletions(-)
diff --git a/emacs-framebuffer.el b/emacs-framebuffer.el @@ -84,83 +84,55 @@ (+ (framebuffer-next-u16le brook) (* 65536 (framebuffer-next-u16le brook)))) -(defun framebuffer-png-size (file) - (with-temp-buffer - (set-buffer-multibyte nil) - (insert-file-contents-literally file) - (let ((brook (framebuffer-buffer-brook))) - (when (and (= 137 (funcall brook)) - (= ?P (funcall brook)) - (= ?N (funcall brook)) - (= ?G (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-jpeg-size (file) - (with-temp-buffer - (set-buffer-multibyte nil) - (insert-file-contents-literally file) - (let ((brook (framebuffer-buffer-brook))) - (when (and (= #xff (framebuffer-next-u8 brook)) - (= #xd8 (framebuffer-next-u8 brook))) - (let ((n 0) - (m 0)) - (while (not (member m '(#xc0 #xc1 #xc2 #xc3 #xc5 #xc6 #xc7 #xc8 - #xc9 #xca #xcb #xcd #xc0e #xcf))) - (goto-char (+ (point) n)) - (while (= #xff (setq m (framebuffer-next-u8 brook)))) - (setq n (- (framebuffer-next-u16 brook) 2))) - (framebuffer-next-u8 brook) - (let ((h (framebuffer-next-u16 brook))) - (cons (framebuffer-next-u16 brook) - h))))))) - -(defun framebuffer-bmp-size (file) - (with-temp-buffer - (set-buffer-multibyte nil) - (insert-file-contents-literally file) - (let ((brook (framebuffer-buffer-brook))) - (when (and (= ?B (framebuffer-next-u8 brook)) - (= ?M (framebuffer-next-u8 brook))) - (dotimes (i 16) - (framebuffer-next-u8 brook)) - (cons (framebuffer-next-u32le brook) - (framebuffer-next-u32le brook)))))) - -(defun framebuffer-gif-size (file) +(defun framebuffer-image-size (file) (with-temp-buffer (set-buffer-multibyte nil) (insert-file-contents-literally file) (let ((brook (framebuffer-buffer-brook))) - (when (and (= ?G (framebuffer-next-u8 brook)) - (= ?I (framebuffer-next-u8 brook)) - (= ?F (framebuffer-next-u8 brook)) - (= ?8 (framebuffer-next-u8 brook)) - (member (framebuffer-next-u8 brook) '(?7 ?9)) - (= ?a (framebuffer-next-u8 brook))) - (cons (framebuffer-next-u16le brook) - (framebuffer-next-u16le brook)))))) - -(defun framebuffer-image-size (file) - (let ((e (file-name-extension file))) - (cond - ((string-match "png\\|PNG" e) - (framebuffer-png-size file)) - ((string-match "jpe?g\\|JPE?G" e) - (framebuffer-jpeg-size file)) - ((string-match "bmp\\|BMP" e) - (framebuffer-bmp-size file)) - ((string-match "gif\\|GIF" e) - (framebuffer-gif-size file))))) + (case (funcall brook) + (137 ;; png + (when (and (= ?P (funcall brook)) + (= ?N (funcall brook)) + (= ?G (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)))) + (#xff ;; jpeg + (when (= #xd8 (framebuffer-next-u8 brook)) + (let ((n 0) + (m 0)) + (while (not (member m '(#xc0 #xc1 #xc2 #xc3 #xc5 #xc6 #xc7 #xc8 + #xc9 #xca #xcb #xcd #xc0e #xcf))) + (goto-char (+ (point) n)) + (while (= #xff (setq m (framebuffer-next-u8 brook)))) + (setq n (- (framebuffer-next-u16 brook) 2)))) + (framebuffer-next-u8 brook) + (let ((h (framebuffer-next-u16 brook))) + (cons (framebuffer-next-u16 brook) + h)))) + (?B ;; bmp + (when (= ?M (framebuffer-next-u8 brook)) + (dotimes (i 16) + (framebuffer-next-u8 brook)) + (cons (framebuffer-next-u32le brook) + (framebuffer-next-u32le brook)))) + (?G ;; gif + (when (and (= ?I (framebuffer-next-u8 brook)) + (= ?F (framebuffer-next-u8 brook)) + (= ?8 (framebuffer-next-u8 brook)) + (member (framebuffer-next-u8 brook) '(?7 ?9)) + (= ?a (framebuffer-next-u8 brook))) + (cons (framebuffer-next-u16le brook) + (framebuffer-next-u16le brook)))) + )))) (defun framebuffer-image-file (file) (interactive "fFile: ")