forked from slyrus/opticl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coerce.lisp
171 lines (155 loc) · 5.88 KB
/
coerce.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
(in-package :opticl)
(defun mean (&rest numbers)
(/ (apply #'+ numbers) (length numbers)))
(defgeneric coerce-image (image type &key &allow-other-keys)
(:documentation "attempts to coerce a given image into the specified type."))
(defmethod coerce-image (image (type (eql '8-bit-gray-image)) &rest args)
(declare (ignore args))
(etypecase image
(8-bit-gray-image image)
(1-bit-gray-image
(with-image-bounds (y x) image
(let* ((gray-image (make-8-bit-gray-image y x)))
(do-pixels (i j) image
(setf (pixel gray-image i j)
(if (plusp (pixel image i j)) 255 0)))
gray-image)))
(32-bit-gray-image
(with-image-bounds (y x) image
(let ((gray-image (make-8-bit-gray-image y x)))
(do-pixels (i j)
image
(setf (pixel gray-image i j) (pixel image i j)))
gray-image)))
(fixnum-gray-image
(with-image-bounds (y x) image
(let ((gray-image (make-8-bit-gray-image y x)))
(do-pixels (i j)
image
(setf (pixel gray-image i j) (pixel image i j)))
gray-image)))
((or rgb-image rgba-image)
(with-image-bounds (y x channels) image
(let* ((type (array-element-type image))
(gray-image (make-8-bit-gray-image y x)))
(if (subtypep type 'integer)
(do-pixels (i j)
image
(multiple-value-bind (r g b)
(pixel image i j)
(setf (pixel gray-image i j)
(round (mean r g b)))))
(do-pixels (i j)
image
(multiple-value-bind (r g b)
(pixel image i j)
(setf (pixel gray-image i j)
(coerce (round (mean r g b)) type)))))
gray-image)))))
(defmethod coerce-image (image (type (eql 'gray-image)) &key preserve-luminance &allow-other-keys)
(etypecase image
(gray-image image)
((or rgb-image rgba-image)
(with-image-bounds (y x channels)
image
(let* ((type (array-element-type image))
(gray-image (make-array (list y x) :element-type type)))
(if preserve-luminance
(if (subtypep type 'integer)
(do-pixels (i j)
image
(multiple-value-bind (r g b)
(pixel image i j)
(setf (pixel gray-image i j)
(round
(+ (* r 0.2989)
(* g 0.5870)
(* b 0.1140))))))
(do-pixels (i j)
image
(multiple-value-bind (r g b)
(pixel image i j)
(setf (pixel gray-image i j)
(coerce (round
(+ (* r 0.2989)
(* g 0.5870)
(* b 0.1140))) type)))))
(if (subtypep type 'integer)
(do-pixels (i j)
image
(multiple-value-bind (r g b)
(pixel image i j)
(setf (pixel gray-image i j)
(round (mean r g b)))))
(do-pixels (i j)
image
(multiple-value-bind (r g b)
(pixel image i j)
(setf (pixel gray-image i j)
(coerce (round (mean r g b)) type))))))
gray-image)))))
(defmethod coerce-image (image (type (eql 'rgb-image)) &rest args)
(declare (ignore args))
(etypecase image
(gray-image
(with-image-bounds (y x channels)
image
(let* ((type (array-element-type image))
(rgb-image (make-array (list y x 3) :element-type type)))
(do-pixels (i j)
image
(let ((val (pixel image i j)))
(setf (pixel rgb-image i j)
(values val val val))))
rgb-image)))
(rgb-image image)
(rgba-image
(with-image-bounds (y x channels)
image
(let* ((type (array-element-type image))
(rgb-image (make-array (list y x 3) :element-type type)))
(do-pixels (i j)
image
(setf (pixel rgb-image i j)
(pixel image i j)))
rgb-image)))))
(defmethod coerce-image (image (type (eql '8-bit-rgba-image)) &rest args)
(declare (ignore args))
(etypecase image
(gray-image
(with-image-bounds (y x channels)
image
(let* ((type (array-element-type image))
(rgba-image (make-array (list y x 4) :element-type type)))
(do-pixels (i j)
image
(let ((val (pixel image i j)))
(setf (pixel rgba-image i j)
(values val val val 255))))
rgba-image)))
(rgb-image
(with-image-bounds (y x channels)
image
(let* ((type (array-element-type image))
(rgba-image (make-array (list y x 4) :element-type type)))
(do-pixels (i j)
image
(setf (pixel* rgba-image i j)
(append (multiple-value-list (pixel image i j))
(list 255))))
rgba-image)))
(rgba-image image)))
(defmethod coerce-image (image (type (eql 'rgba-image)) &rest args)
(apply #'coerce-image image '8-bit-rgba-image args))
;;;
;;; deprecated convert functions
(defun convert-image-to-8-bit-grayscale (image)
(coerce image '8-bit-gray-image))
(defun convert-image-to-grayscale (image)
(coerce-image image 'gray-image))
(defun convert-image-to-grayscale-luminance (image)
(coerce-image image 'gray-image :preserve-luminance t))
(defun convert-image-to-rgb (image)
(coerce-image image 'rgb-image))
(defun convert-image-to-rgba (image)
(coerce-image image 'rgba-image))