emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/scanner 8486db9 55/56: correct and clean up image scann


From: Stefan Monnier
Subject: [elpa] externals/scanner 8486db9 55/56: correct and clean up image scanning
Date: Fri, 10 Apr 2020 13:56:07 -0400 (EDT)

branch: externals/scanner
commit 8486db9f26462c5a018deaf9841d28f12f9cab18
Author: Raffael Stocker <address@hidden>
Commit: Raffael Stocker <address@hidden>

    correct and clean up image scanning
    
    * scanner.el:
    remove ‘scanner--determine-image-format’
    (scanner-scan-image): correct image format selection
---
 scanner-test.el | 97 +++++++++++++++++++++------------------------------------
 scanner.el      | 84 ++++++++++++++++++++++++-------------------------
 2 files changed, 77 insertions(+), 104 deletions(-)

diff --git a/scanner-test.el b/scanner-test.el
index fa2b128..4175ce0 100644
--- a/scanner-test.el
+++ b/scanner-test.el
@@ -7,7 +7,7 @@
 ;; Created: 05. Feb 2020
 ;; Version: 0.0
 ;; Package-Requires: ((emacs "25.1") (dash "2.12.0"))
-;; Keywords: hardware multimedia
+;; Keywords: hardware, multimedia
 ;; URL: https://gitlab.com/rstocker/scanner.git
 
 ;; This file is NOT part of GNU Emacs
@@ -50,71 +50,52 @@
 (require 'dash)
 (require 'ert)
 
-(ert-deftest scanner-test-determine-image-format ()
-  "Test format determination from extension."
-  (let ((scanner-image-format '(:image "img-def" :doc "doc-def")))
-    ;; a known image format is returned in standardized form
-    (should (string= "jpeg" (scanner--determine-image-format "jpg")))
-    (should (string= "jpeg" (scanner--determine-image-format "jpeg")))
-    (should (string= "jpeg" (scanner--determine-image-format "JPG")))
-    (should (string= "tiff" (scanner--determine-image-format "tiff")))
-    (should (string= "tiff" (scanner--determine-image-format "tif")))
-    (should (string= "pnm" (scanner--determine-image-format "pnm")))
-    (should (string= "png" (scanner--determine-image-format "png")))
-    ;; unknown image formats are treated as the default format
-    (should (string= "img-def" (scanner--determine-image-format nil)))
-    (should (string= "img-def" (scanner--determine-image-format "")))
-    (should (string= "img-def" (scanner--determine-image-format "gif")))
-    (should (string= "img-def" (scanner--determine-image-format 42)))
-    ;; if the argument is not char-or-string-p, an error is signalled
-    (should-error (scanner--determine-image-format '(42))
-                 :type 'wrong-type-argument)))
-
 (ert-deftest scanner-test-scanimage-args ()
   "Test the argument list construction for scanimage."
   ;; minimum args list (no device-specific options are available)
   (let ((switches nil)
-       (scanner-image-format '(:image "fmt-img" :doc "fmt-doc"))
        (scanner-device-name "devname")
        (scanner-image-size '(200 250))
        (-compare-fn #'string=))
     ;; known values are included with their switches
     (should (-is-infix-p '("-d" "devname") (scanner--scanimage-args "file"
                                                                    :image
-                                                                   switches)))
+                                                                   switches
+                                                                   "jpeg")))
     (should (-is-infix-p '("-o" "file") (scanner--scanimage-args "file"
                                                                 :image
-                                                                switches)))
-    (should (-contains-p (scanner--scanimage-args "file" :image switches)
-                        "--format=fmt-img"))
-    ;; a provided image format overrides the default format
-    (should (-contains-p (scanner--scanimage-args "file" :image switches
-                                                 "arg-fmt")
-                        "--format=arg-fmt"))
+                                                                switches
+                                                                "jpeg")))
+    (should (-contains-p (scanner--scanimage-args "file" :image switches 
"jpeg")
+                        "--format=jpeg"))
     ;; device-specific options are not included in the argument list
-    (should-not (-contains-p (scanner--scanimage-args "file" :image switches)
+    (should-not (-contains-p (scanner--scanimage-args "file" :image switches
+                                                     "jpeg")
                             "--mode"))
-    (should-not (-contains-p (scanner--scanimage-args "file" :image switches)
+    (should-not (-contains-p (scanner--scanimage-args "file" :image switches
+                                                     "jpeg")
                             "--resolution"))
-    (should-not (-contains-p (scanner--scanimage-args "file" :image switches)
+    (should-not (-contains-p (scanner--scanimage-args "file" :image switches
+                                                     "jpeg")
                             "-x"))
-    (should-not (-contains-p (scanner--scanimage-args "file" :image switches)
+    (should-not (-contains-p (scanner--scanimage-args "file" :image switches
+                                                     "jpeg")
                             "-y"))
-    (should-not (-contains-p (scanner--scanimage-args "file" :doc switches)
+    (should-not (-contains-p (scanner--scanimage-args "file" :doc switches 
"jpeg")
                             "-x"))
-    (should-not (-contains-p (scanner--scanimage-args "file" :doc switches)
+    (should-not (-contains-p (scanner--scanimage-args "file" :doc switches 
"jpeg")
                             "-y"))
     ;; without format and device name, these are not in the args list
     (let ((scanner-image-format nil)
          (scanner-device-name nil))
-      (should-not (-contains-p (scanner--scanimage-args "file" :image switches)
+      (should-not (-contains-p (scanner--scanimage-args "file" :image switches
+                                                       "jpeg")
                               "--format="))
       (should-not (-contains-p (scanner--scanimage-args "file" :image
-                                                       switches)
+                                                       switches "jpeg")
                               "-d"))))
   ;; image args list with device-specific args
   (let ((switches '("--resolution" "-x" "-y" "--mode"))
-       (scanner-image-format '(:image "fmt-img" :doc "fmt-doc"))
        (scanner-resolution '(:doc 300 :image 600))
        (scanner-scan-mode '(:image "Color" :doc "Gray"))
        (scanner-doc-papersize :a4)
@@ -122,41 +103,35 @@
        (scanner-image-size '(200 250))
        (-compare-fn #'string=))
     (should (-is-infix-p '("-o" "file") (scanner--scanimage-args "file" :image
-                                                                switches)))
-    (should (-contains-p (scanner--scanimage-args "file" :image switches)
-                        "--format=fmt-img"))
-    (should (-contains-p (scanner--scanimage-args "file" :image switches
-                                                 "arg-fmt")
-                        "--format=arg-fmt"))
-    (should (-contains-p (scanner--scanimage-args "file" :image switches)
+                                                                switches 
"jpeg")))
+    (should (-contains-p (scanner--scanimage-args "file" :image switches 
"jpeg")
+                        "--format=jpeg"))
+    (should (-contains-p (scanner--scanimage-args "file" :image switches 
"jpeg")
                         "--mode=Color"))
-    (should (-contains-p (scanner--scanimage-args "file" :image switches)
+    (should (-contains-p (scanner--scanimage-args "file" :image switches 
"jpeg")
                         "--resolution=600"))
     (should (-is-infix-p '("-x" "210") (scanner--scanimage-args "file" :doc
-                                                               switches)))
+                                                               switches 
"jpeg")))
     (should (-is-infix-p '("-y" "297") (scanner--scanimage-args "file" :doc
-                                                               switches)))
+                                                               switches 
"jpeg")))
     (should (-is-infix-p '("-x" "200") (scanner--scanimage-args "file" :image
-                                                               switches)))
+                                                               switches 
"jpeg")))
     (should (-is-infix-p '("-y" "250") (scanner--scanimage-args "file" :image
-                                                               switches)))
+                                                               switches 
"jpeg")))
     (should (-is-infix-p '("-o" "file") (scanner--scanimage-args "file" :doc
-                                                                switches)))
-    (should (-contains-p (scanner--scanimage-args "file" :doc switches)
-                        "--format=fmt-doc"))
-    (should (-contains-p (scanner--scanimage-args "file" :doc switches
-                                                 "arg-fmt")
-                        "--format=arg-fmt"))
-    (should (-contains-p (scanner--scanimage-args "file" :doc switches)
+                                                                switches 
"jpeg")))
+    (should (-contains-p (scanner--scanimage-args "file" :doc switches "jpeg")
+                        "--format=jpeg"))
+    (should (-contains-p (scanner--scanimage-args "file" :doc switches "jpeg")
                         "--mode=Gray"))
-    (should (-contains-p (scanner--scanimage-args "file" :doc switches)
+    (should (-contains-p (scanner--scanimage-args "file" :doc switches "jpeg")
                         "--resolution=300"))
     (let ((scanner-image-size nil))
       (should-not (-contains-p (scanner--scanimage-args "file" :image
-                                                       switches)
+                                                       switches "jpeg")
                               "-x"))
       (should-not (-contains-p (scanner--scanimage-args "file" :image
-                                                       switches)
+                                                       switches "jpeg")
                               "-y")))))
 
 
diff --git a/scanner.el b/scanner.el
index c6a1f4f..e5e9dbd 100644
--- a/scanner.el
+++ b/scanner.el
@@ -7,7 +7,7 @@
 ;; Created: 05. Feb 2020
 ;; Version: 0.0
 ;; Package-Requires: ((emacs "25.1") (dash "2.12.0"))
-;; Keywords: hardware multimedia
+;; Keywords: hardware, multimedia
 ;; URL: https://gitlab.com/rstocker/scanner.git
 
 ;; This file is NOT part of GNU Emacs
@@ -70,7 +70,7 @@
 (require 'menu-bar)
 
 
-;; customization
+;;;; customization
 (defgroup scanner nil
   "Scan documents and images."
   :group 'multimedia)
@@ -186,9 +186,8 @@ This is usually \"Color\" or \"Gray\", but depends on your
 scanning device."
   :type '(plist :value-type string))
 
-;; FIXME remove these before release
 (defcustom scanner-scanimage-switches
-  '("--brightness" "50" "--contrast" "50")
+  '()
   "Additional options to be passed to scanimage(1)."
   :type '(repeat string))
 
@@ -207,7 +206,7 @@ plugged in.  For these, auto-detection will always be done."
   :type '(number))
 
 
-;; menu
+;;;; menu
 ;;;###autoload
 (defvar scanner-menu
   (let ((map (make-sparse-keymap)))
@@ -257,7 +256,7 @@ plugged in.  For these, auto-detection will always be done."
   (list 'menu-item "Scanner" scanner-menu))
 
 
-;; internal variables and functions
+;;;; internal variables and functions
 (defvar scanner--detected-devices
   nil
   "List of devices detected by SANE.
@@ -287,34 +286,33 @@ name, the device type, and the vendor and model names."
          (--filter (eql 3 (length it))
                    (mapcar (lambda (x) (split-string x "|")) scanners)))))
 
-(defun scanner--scanimage-args (outfile type switches &optional img-fmt)
+(defun scanner--scanimage-args (outfile scan-type switches img-fmt)
   "Construct the argument list for scanimage(1).
-OUTFILE is the output filename, TYPE is either ‘:image’ or
+OUTFILE is the output filename, SCAN-TYPE is either ‘:image’ or
 ‘:doc’, SWITCHES is a list of available device-dependent options
 and IMG-FMT is the output image format.
 
-When scanning documents (type :doc), scanner uses the IMG-FMT
+When scanning documents (scan-type :doc), scanner uses the IMG-FMT
 argument for the intermediate representation before conversion to
 the document format.  If any of the required options from
 ‘scanner--device-specific-switches’ are unavailable, they are
 simply dropped."
-  (let ((size (cond ((eq :doc type)
+  (let ((size (cond ((eq :doc scan-type)
                     (plist-get scanner-paper-sizes scanner-doc-papersize))
-                   ((eq :image type) scanner-image-size)
+                   ((eq :image scan-type) scanner-image-size)
                    (t nil))))
     (-flatten (list (and scanner-device-name
                         (list "-d" scanner-device-name))
-                   (-when-let (fmt (or img-fmt
-                                       (plist-get scanner-image-format type)))
-                     (concat "--format=" fmt))
+                   (concat "--format=" img-fmt)
                    "-o" outfile
                    (--map (pcase it
                             ("--mode" (concat "--mode="
-                                              (plist-get scanner-scan-mode 
type)))
+                                              (plist-get scanner-scan-mode
+                                                         scan-type)))
                             ("--resolution" (concat "--resolution="
                                                     (number-to-string
                                                      (plist-get 
scanner-resolution
-                                                                type))))
+                                                                scan-type))))
                             ((and "-x" (guard size))
                              (list "-x" (number-to-string (car size))))
                             ((and "-y" (guard size))
@@ -334,19 +332,7 @@ extensions depending on the selected output options, see
                  scanner-tesseract-switches
                  scanner-tesseract-outputs)))
 
-(defun scanner--determine-image-format (extension)
-  "Determine image file format from EXTENSION.
-If the extension is unknown, return the default format."
-  (let ((ext (if extension (downcase extension) ""))
-       (known-ext '(("jpeg" . "jpeg")
-                    ("jpg" . "jpeg")
-                    ("png" . "png")
-                    ("pnm" . "pnm")
-                    ("tiff" . "tiff")
-                    ("tif" . "tiff"))))
-    (or (cdr (assoc ext known-ext))
-       (plist-get scanner-image-format :image))))
-
+;; FIXME write log output
 (defun scanner--ensure-init ()
   "Ensure that scanning device is initialized.
 If no scanning device has been configured or the configured
@@ -394,7 +380,7 @@ MSG is a format string, with ARGS passed to ‘format’."
   (get-buffer-create "*Scanner*"))
 
 
-;; commands
+;;;; commands
 (defun scanner-select-papersize (size)
   "Select the papersize SIZE for document scanning."
   (interactive
@@ -554,27 +540,39 @@ If ‘scanner-device-name’ is nil or this device is 
unavailable,
 attempt auto-detection.  If more than one scanning device is
 available, ask for a selection interactively."
   (interactive "P\nFImage file name: ")
-  (let* ((fmt (scanner--determine-image-format filename))
-        (fname-base (file-name-sans-extension filename))
-        (fname-ext (if (file-name-extension filename)
-                       (file-name-extension filename t)
-                     (concat "." fmt)))
-        (num-scans (prefix-numeric-value nscans))
-        (switches (scanner--ensure-init))
-        (page-count 1))
+  (let ((derived-fmt (cdr (assoc (downcase (file-name-extension filename t))
+                                '((".jpeg" . "jpeg")
+                                  (".jpg" . "jpeg")
+                                  (".png" . "png")
+                                  (".pnm" . "pnm")
+                                  (".tiff" . "tiff")
+                                  (".tif" . "tiff")))))
+       (num-scans (prefix-numeric-value nscans))
+       (switches (scanner--ensure-init))
+       (page-count 1))
     (cl-labels ((scanimage
                 (multi-scan)
-                (let* ((img-file (if multi-scan
+                (let* ((img-fmt (or derived-fmt
+                                    (plist-get scanner-image-format :image)))
+                       (img-ext (if derived-fmt
+                                    (file-name-extension filename t)
+                                  (concat "."
+                                          (plist-get scanner-image-format
+                                                     :image))))
+                       (img-base (if derived-fmt
+                                       (file-name-sans-extension filename)
+                                     filename))
+                       (img-file (if multi-scan
                                      (prog1
-                                         (concat fname-base "-"
+                                         (concat img-base "-"
                                                  (number-to-string page-count)
-                                                 fname-ext)
+                                                 img-ext)
                                        (cl-incf page-count))
-                                   (concat fname-base fname-ext)))
+                                   (concat img-base img-ext)))
                        (scanimage-args (scanner--scanimage-args img-file
                                                                 :image
                                                                 switches
-                                                                fmt)))
+                                                                img-fmt)))
                   (scanner--log "Scanning image to file \"%s\"" img-file)
                   (make-process :name "Scanner (scanimage)"
                                 :command `(,scanner-scanimage-program



reply via email to

[Prev in Thread] Current Thread [Next in Thread]