forked from slyrus/opticl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageops.lisp
108 lines (95 loc) · 3.61 KB
/
imageops.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
;;; Copyright (c) 2011 Cyrus Harmon, All rights reserved.
;;; See COPYRIGHT file for details.
(in-package :opticl)
(defun sum (array)
(let ((acc 0))
(map-array (lambda (v) (incf acc v)) array)
acc))
(defun sum-range (array vendr vstartr vendc vstartc)
(let ((acc 0))
(loop for i from vstartr below vendr
do (loop for j from vstartc below vendc
do (incf acc (aref array i j))))
acc))
(defmacro make-constrain-fn (min max)
`(lambda (val)
(cond ((<= val ,min) ,min)
((>= val ,max) ,max)
(t (round val)))))
(defun constrain (val min max)
(let ((val (if (< val min) min val)))
(if (> val max)
max
val)))
(defun pixel-in-bounds (img y x)
(with-image-bounds (ymax xmax)
img
(and (>= y 0) (< y ymax)
(>= x 0) (< x xmax))))
(defmacro when-pixel-in-bounds ((img y x) &body body)
(let ((ymax (gensym)) (xmax (gensym)))
`(let ((,ymax (1- (array-dimension ,img 0)))
(,xmax (1- (array-dimension ,img 1))))
(if (and (<= 0 ,y ,ymax)
(<= 0 ,x ,xmax))
,@body))))
(defun transpose-image (img)
(with-image-bounds (ymax xmax channels)
img
(let ((zimg (make-array
(cons xmax (cons ymax (when channels (list channels))))
:element-type (array-element-type img))))
(loop for i below ymax
do (loop for j below xmax
do (setf (pixel zimg j i) (pixel img i j))))
zimg)))
(defun copy-image (img)
(with-image-bounds (ymax xmax channels)
img
(let ((new-image (make-array
(cons ymax (cons xmax (when channels (list channels))))
:element-type (array-element-type img))))
(loop for i below ymax
do (loop for j below xmax
do (setf (pixel new-image i j) (pixel img i j))))
new-image)))
(defun crop-image (img y1 x1 y2 x2)
(with-image-bounds (ymax xmax channels)
img
(declare (ignore ymax xmax))
(let ((new-rows (- y2 y1))
(new-cols (- x2 x1)))
(let ((new-image (make-array
(cons new-rows (cons new-cols (when channels (list channels))))
:element-type (array-element-type img))))
(loop for i-src from y1 below y2
for i-dest below new-rows
do (loop for j-src from x1 below x2
for j-dest below new-cols
do
(setf (pixel new-image i-dest j-dest) (pixel img i-src j-src))))
new-image))))
(defun map-array (fn array &key
(element-type (array-element-type array))
(adjustable (adjustable-array-p array))
(force-simple t))
(let ((len (reduce #'* (array-dimensions array))))
(if force-simple
(let* ((disp (make-array len
:displaced-to array))
(vec (map `(vector ,element-type) fn disp))
(dest (make-array (array-dimensions array)
:adjustable adjustable
:element-type element-type)))
(loop for i below len
do (setf (row-major-aref dest i)
(elt vec i)))
dest)
(let* ((disp (make-array len :displaced-to array)))
(make-array (array-dimensions array)
:element-type element-type
:displaced-to (map `(vector ,element-type) fn disp))))))
(defun trim-image (img y-pixels x-pixels)
(with-image-bounds (height width)
img
(crop-image img y-pixels x-pixels (- height y-pixels) (- width x-pixels))))