-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel.lsp
223 lines (157 loc) · 4.55 KB
/
parallel.lsp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"Parallel Image Darkening"
"PARALLEL"
(ql:quickload :lparallel)
;; Create the worker threads
(setf lparallel:*kernel* (lparallel:make-kernel 90))
(defvar type 0)
(defvar width 0)
(defvar height 0)
(defvar data ())
;; set the data values (type, width, height, and data variables) for the image
(defun loadImage (filename)
(setq fileList ())
(setq fs (open filename :element-type 'unsigned-byte))
(setq keepGoing t)
(loop while keepGoing do
;; load the fileList
(setq b (read-byte fs nil 'eof))
(cond
((eql b 'eof) (setq keepGoing nil))
(t (setq fileList (append fileList (cons b nil))))
)
)
(close fs)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; set the image data based on the fileList
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; set image type (color or gray)
(cond
((= (nth 1 fileList) 50) (setq type 0))
((= (nth 1 fileList) 53) (setq type 0))
(t (setq type 1))
)
;; skip comments
(setq index 3)
(loop while (= (nth index fileList) 35) do
(loop while (/= (nth index fileList) 10) do
(setq index (+ index 1))
)
(setq index (+ index 1))
)
;; get width and height numbers
(setq widthVector ())
(loop while (/= (nth index fileList) 32) do
(setq widthVector (append widthVector (cons (- (nth index fileList) 48) nil)))
(setq index (+ index 1))
)
(setq index (+ index 1))
(setq heightVector ())
(loop while (/= (nth index fileList) 10) do
(setq heightVector (append heightVector (cons (- (nth index fileList) 48) nil)))
(setq index (+ index 1))
)
(setq index (+ index 1))
;; convert vectors to int
(setq width 0)
(setq i (- (list-length widthVector) 1))
(loop while (>= i 0) do
(setq width (+ width (* (nth i widthVector) (expt 10 (- (- (list-length widthVector) 1) i)))))
(setq i (- i 1))
)
(setq height 0)
(setq i (- (list-length heightVector) 1))
(loop while (>= i 0) do
(setq height (+ height (* (nth i heightVector) (expt 10 (- (- (list-length heightVector) 1) i)))))
(setq i (- i 1))
)
;; skip past max value
(loop while (/= (nth index fileList) 10) do
(setq index (+ index 1))
)
(setq index (+ index 1))
;; copy pixel data to data list
(loop while (< index (list-length fileList)) do
(setq data (append data (cons (nth index fileList) nil)))
(setq index (+ index 1))
)
)
;; given a filename to save to (ending in .pnm), save the image data from the width, height, type, and data variables
(defun saveImage (filename)
(setq fs (open filename :direction :output :if-exists :supersede :element-type 'unsigned-byte))
;; magic number
(write-byte 80 fs)
(cond
((= type 0) (write-byte 53 fs))
((= type 1) (write-byte 54 fs))
)
(write-byte 10 fs)
;; split digits of width and height and convert to ascii
(setq widthVector ())
(setq tWidth width)
(loop while (> tWidth 0) do
(setq num (mod tWidth 10))
(setq num (+ num 48))
(setq widthVector (append widthVector (cons num nil)))
(setq tWidth (floor tWidth 10))
)
(setq heightVector ())
(setq tHeight height)
(loop while (> tHeight 0) do
(setq num (mod tHeight 10))
(setq num (+ num 48))
(setq heightVector (append heightVector (cons num nil)))
(setq tHeight (floor tHeight 10))
)
;; write the width and height (stored in the vectors backwards)
(setq i (- (list-length widthVector) 1))
(loop while (>= i 0) do
(write-byte (nth i widthVector) fs)
(setq i (- i 1))
)
(write-byte 32 fs)
(setq i (- (list-length heightVector) 1))
(loop while (>= i 0) do
(write-byte (nth i heightVector) fs)
(setq i (- i 1))
)
(write-byte 10 fs)
;; max size of pixel value
(write-byte 50 fs)
(write-byte 53 fs)
(write-byte 53 fs)
(write-byte 10 fs)
;; write pixel data to file
(setq size (list-length data))
(setq i 0)
(loop while (< i size) do
(write-byte (nth i data) fs)
(setq i (+ i 1))
)
(close fs)
)
;; given an individual pixel value, darken it
(defun darken (pixel)
(cond
((< (- pixel 50) 0) 0)
(t (- pixel 50))
)
)
;; given a list of pixel data, return a new list that has darker values than the original
(defun darkenImage (L)
(setq newL ())
(setq size (list-length L))
(setq i 0)
(loop while (< i size) do
(setq newL (append newL (cons (darken (nth i L)) nil)))
(setq i (+ i 1))
)
newL
)
;; run the functions to darken auto.pnm and save it as copy.pnm
(loadImage "auto.pnm")
(time
(lparallel:pdotimes (i (list-length data))
(setf (nth i data) (darken (nth i data)))
)
)
(saveImage "copy.pnm")