7597b07e511af433dcbf0e07fe2e1c27f8790b2d
[chise/concord-images.git] / concord-images.el
1 (concord-assign-genre 'image-resource "/usr/local/var/photo/db")
2
3 (defun concord-images-encode-url-as-id (url &optional base)
4   (let (ret)
5     (cond
6      ((and base
7            (string-match (concat "^" (regexp-quote base)) url))
8       (setq ret (substring url (match-end 0)))
9       (if (string-match "\\.[a-zA-Z0-9]+$" ret)
10           (substring ret 0 (match-beginning 0))
11         ret)
12       )
13      ((string-match "^http://\\(hng\\|image\\)\\.\\(chise\\.org\\|kanji\\.zinbun\\.kyoto-u\\.ac\\.jp\\)/images/iiif/" url)
14       (setq ret (substring url (match-end 0)))
15       (if (string-match "\\.[a-zA-Z0-9]+$" ret)
16           (substring ret 0 (match-beginning 0))
17         ret)
18       )
19      ((string-match "^http://\\(hng\\|image\\)\\.\\(chise\\.org\\|kanji\\.zinbun\\.kyoto-u\\.ac\\.jp\\)/images/" url)
20       (setq ret (substring url (match-end 0)))
21       (if (string-match "\\.[a-zA-Z0-9]+$" ret)
22           (substring ret 0 (match-beginning 0))
23         ret)
24       )
25      ((string-match "^http://" url)
26       (substring url (match-end 0))
27       )
28      (t
29       url))))
30
31 (defun concord-images-add-url (url &optional iiif iip base)
32   (let (img-id img-cobj)
33     (unless (setq img-cobj (concord-decode-object '=location url
34                                                   'image-resource))
35       (setq img-id (intern (concord-images-encode-url-as-id url base)))
36       (setq img-cobj (concord-make-object 'image-resource img-id))
37       (concord-object-put img-cobj '=location url)
38       (when iiif
39         (concord-object-put img-cobj '=location@iiif iiif)
40         (with-temp-buffer
41           (call-process
42            "curl" nil (current-buffer) nil
43            "--silent"
44            (concat iiif "/info.json"))
45           (goto-char (point-min))
46           (when (re-search-forward
47                  "^[ \t]*\"width\"[ \t]*:[ \t]*\\([0-9]+\\)" nil t)
48             (concord-object-put img-cobj 'image-width
49                                 (string-to-int (match-string 1))))
50           (when (re-search-forward
51                  "^[ \t]*\"height\"[ \t]*:[ \t]*\\([0-9]+\\)" nil t)
52             (concord-object-put img-cobj 'image-height
53                                 (string-to-int (match-string 1))))
54           ))
55       (when iip
56         (concord-object-put img-cobj '=location@iip iip))
57       )
58     img-cobj))
59
60 (defun concord-images-add-iiif (url &optional base)
61   (let (img-id img-cobj)
62     (unless (setq img-cobj (concord-decode-object '=location url
63                                                   'image-resource))
64       (setq img-id (intern (concord-images-encode-url-as-id url base)))
65       (setq img-cobj (concord-make-object 'image-resource img-id))
66       (concord-object-put img-cobj '=location@iiif url))
67     img-cobj))
68
69 (defun concord-images-add-segments (base-cobj x y width height)
70   (let ((base-id (concord-object-get base-cobj '=id))
71         base-width base-width-f base-height-f
72         base-iiif-url
73         seg-id seg-iiif-url seg-cobj
74         base-iip-url
75         rel-x rel-y rel-w rel-h)
76     (setq seg-id (intern (format "%s/xywh=%d,%d,%d,%d"
77                                  base-id
78                                  x y width height)))
79     (unless (setq seg-cobj (concord-decode-object '=id seg-id
80                                                   'image-resource))
81       (setq seg-cobj (concord-make-object 'image-resource seg-id))
82       (setq base-iiif-url (concord-object-get base-cobj '=location@iiif))
83       (concord-object-put seg-cobj '=location
84                           (format "%s#xywh=%d,%d,%d,%d"
85                                   (concord-object-get base-cobj '=location)
86                                   x y width height))
87       (concord-object-put seg-cobj 'image-offset-x x)
88       (concord-object-put seg-cobj 'image-offset-y y)
89       (concord-object-put seg-cobj 'image-width width)
90       (concord-object-put seg-cobj 'image-height height)
91       (setq seg-iiif-url (format "%s/%d,%d,%d,%d/full/0/default.jpg"
92                                  base-iiif-url x y width height))
93       (concord-object-put seg-cobj '=location@iiif seg-iiif-url)
94       (when (setq base-iip-url (concord-object-get base-cobj '=location@iip))
95         (setq base-width (concord-object-get base-cobj 'image-width)
96               base-width-f (float base-width)
97               base-height-f (float
98                              (concord-object-get base-cobj 'image-height)))
99         (setq rel-x (/ x base-width-f)
100               rel-y (/ y base-height-f)
101               rel-w (/ width base-width-f)
102               rel-h (/ height base-height-f))
103         (if (string-match "&CVT=jpeg" base-iip-url)
104             (setq base-iip-url (substring base-iip-url 0 (match-beginning 0))))
105         (concord-object-put
106          seg-cobj '=location@iip
107          (format "%s&RGN=%f,%f,%f,%f&WID=%d&CVT=jpeg"
108                  base-iip-url rel-x rel-y rel-w rel-h width)))
109       (concord-object-adjoin* seg-cobj '<-image-segment base-cobj)
110       )
111     seg-cobj))
112
113 (provide 'concord-images)