-
Notifications
You must be signed in to change notification settings - Fork 35
/
io.lisp
107 lines (94 loc) · 3.43 KB
/
io.lisp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
(in-package :opticl)
(defparameter *image-stream-reader-hash-table* (make-hash-table))
(map nil (lambda (z)
(destructuring-bind (x y) z
(setf (gethash x *image-stream-reader-hash-table*) y)))
'((:tiff read-tiff-stream)
(:tif read-tiff-stream)
(:jpeg read-jpeg-stream)
(:jpg read-jpeg-stream)
(:png read-png-stream)
(:pbm read-pbm-stream)
(:pgm read-pgm-stream)
(:ppm read-ppm-stream)
(:gif read-gif-stream)))
(defparameter *image-stream-writer-hash-table* (make-hash-table))
(map nil (lambda (z)
(destructuring-bind (x y) z
(setf (gethash x *image-stream-writer-hash-table*) y)))
'((:tiff write-tiff-stream)
(:tif write-tiff-stream)
(:jpeg write-jpeg-stream)
(:jpg write-jpeg-stream)
(:png write-png-stream)
(:pbm write-pbm-stream)
(:pgm write-pgm-stream)
(:ppm write-ppm-stream)
(:gif write-gif-stream)))
(defparameter *image-file-reader-hash-table* (make-hash-table))
(map nil (lambda (z)
(destructuring-bind (x y) z
(setf (gethash x *image-file-reader-hash-table*) y)))
'((:tiff read-tiff-file)
(:tif read-tiff-file)
(:jpeg read-jpeg-file)
(:jpg read-jpeg-file)
(:png read-png-file)
(:pbm read-pbm-file)
(:pgm read-pgm-file)
(:ppm read-ppm-file)
(:gif read-gif-file)
(:tga read-tga-file)))
(defparameter *image-file-writer-hash-table* (make-hash-table))
(map nil (lambda (z)
(destructuring-bind (x y) z
(setf (gethash x *image-file-writer-hash-table*) y)))
'((:tiff write-tiff-file)
(:tif write-tiff-file)
(:jpeg write-jpeg-file)
(:jpg write-jpeg-file)
(:png write-png-file)
(:pbm write-pbm-file)
(:pgm write-pgm-file)
(:ppm write-ppm-file)
(:gif write-gif-file)))
(defun get-image-stream-reader (type)
(let* ((key (intern (string-upcase type) :keyword)))
(gethash key *image-stream-reader-hash-table*)))
(defun get-image-stream-writer (type)
(let* ((key (intern (string-upcase type) :keyword)))
(gethash key *image-stream-writer-hash-table*)))
(defun read-image-stream (stream type)
(let ((fn (get-image-stream-reader type)))
(if fn
(funcall fn stream)
(error "Cannot read image stream ~S of type ~S" stream type))))
(defun write-image-stream (stream type image)
(let ((fn (get-image-stream-writer type)))
(if fn
(funcall fn stream image)
(error "Cannot write image stream ~S of type ~S" stream type))))
(defun get-image-file-reader (file)
(typecase file
(string (get-image-file-reader (pathname file)))
(pathname
(let* ((type (pathname-type file))
(key (intern (string-upcase type) :keyword)))
(gethash key *image-file-reader-hash-table*)))))
(defun get-image-file-writer (file)
(typecase file
(string (get-image-file-writer (pathname file)))
(pathname
(let* ((type (pathname-type file))
(key (intern (string-upcase type) :keyword)))
(gethash key *image-file-writer-hash-table*)))))
(defun read-image-file (file)
(let ((fn (get-image-file-reader file)))
(if fn
(funcall fn file)
(error "Cannot read image file: ~S" file))))
(defun write-image-file (file image)
(let ((fn (get-image-file-writer file)))
(if fn
(funcall fn file image)
(error "Cannot write image file: ~S" file))))