aboutsummaryrefslogtreecommitdiff
path: root/ennum-image.el
blob: 924ec7e3c47c47c606f683f38abb0e1b73d48229 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
;; -*- lexical-binding: t -*-

(require 'image)
(require 'seq)

(defcustom ennum-image-convert-path
  "convert"
  "Path to imagemagick convert executable."
  :type 'string
  :group 'ennum)

(defcustom ennum-image-identify-path
  "identify"
  "Path to imagemagick identify executable."
  :type 'string
  :group 'ennum)

(defcustom ennum-image-jpegtran-path
  "jpegtran"
  "Path to jpegtran executable."
  :type 'string
  :group 'ennum)

(defcustom ennum-image-optipng-path
  "optipng"
  "Path to optipng executable."
  :type 'string
  :group 'ennum)

;; Check if all necessary image types are supported
(seq-do (lambda (image-type)
          (unless (image-type-available-p image-type)
            (lwarn '(ennum) :error "`%s' image type not supported" image-type)))
        '(jpeg png svg))

;; Check for existence of external image processing utilities
(seq-do (lambda (external-program)
          (unless (executable-find external-program)
            (lwarn '(ennum) :error "`%s' not found" external-program)))
        (list ennum-image-convert-path
              ennum-image-identify-path
              ennum-image-jpegtran-path
              ennum-image-optipng-path))

(defun ennum-image-resize-image (infile-path outfile-path width)
  "A simple shell wrapper around ImageMagick's convert"
  (ennum-image--assert-file-exists infile-path)
  (cl-case (image-type infile-path)
    (svg
     (copy-file infile-path outfile-path t))
    (otherwise
     (call-process ennum-image-convert-path nil nil nil
                   infile-path "-resize" (format "%d>" width) outfile-path)))
  outfile-path)

(defun ennum-image-optimize-image (image-path)
  "A simple shell wrapper around jpegtran and optipng"
  (ennum-image--assert-file-exists image-path)
  (cl-case (image-type image-path)
    (jpeg
     (call-process ennum-image-jpegtran-path
                   nil nil nil "-optimize"
                   "-progressive" "-copy" "none"
                   "-outfile" image-path image-path))
    (png
     (call-process ennum-image-optipng-path
                   nil nil nil image-path)))
  image-path)

(defun ennum-image-get-width (image-path)
  (ennum-image--assert-file-exists image-path)
  (cl-case (image-type image-path)
    (svg 1e+INF)
    (otherwise
     (with-temp-buffer
       (call-process ennum-image-identify-path nil t nil
                     "-format" "%w" image-path)
       (string-to-number (buffer-string))))))

(defun ennum-image--assert-file-exists (path)
  (unless (file-exists-p path)
    (error "File %s does not exist" path)))

(provide 'ennum-image)