-
Notifications
You must be signed in to change notification settings - Fork 35
/
pnm.lisp
446 lines (398 loc) · 16 KB
/
pnm.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
;;; Copyright (c) 2011 Cyrus Harmon, All rights reserved.
;;; See COPYRIGHT file for details.
(in-package :opticl)
(defparameter *whitespace-chars* #(#\# #\Space #\Newline #\Tab #\Linefeed #\Return))
(defparameter *whitespace-bytes* (map 'vector #'char-code *whitespace-chars*))
;;;
;;; various utility functions for reading and writing PNM files
(defun whitespace-byte-p (int)
(find int *whitespace-bytes*))
(defun skip-line (stream)
(loop for c = (read-byte stream)
until (find c (map 'vector #'char-code '(#\Newline #\Linefeed)))))
(defun read-until-whitespace (stream)
(loop for c = (read-byte stream)
until (whitespace-byte-p c)
collect c))
(defun read-byte-skipping-whitespace (stream)
(loop for c = (read-byte stream)
while (whitespace-byte-p c)
do (when (eq c (char-code #\#))
(skip-line stream))
finally (return c)))
(defun read-number (stream)
(let ((first-char (read-byte-skipping-whitespace stream)))
(map 'string #'code-char
(cons first-char (read-until-whitespace stream)))))
(defun read-binary-value (stream)
(let ((byte (read-byte stream)))
(if (whitespace-byte-p byte)
(read-binary-value stream)
(digit-char-p (code-char byte)))))
(defun write-integer (int stream)
(map nil (lambda (x) (write-byte (char-code x) stream)) (format nil "~D" int)))
(defun write-word (word stream)
(write-byte (ash word -8) stream)
(write-byte (logand word #xff) stream))
(defun %emit-byte (char stream)
(if (typep stream 'string-stream)
(write-char char stream)
(write-byte (char-code char) stream)))
(defun %emit-integer (number stream)
(if (typep stream 'string-stream)
(write number :stream stream)
(write-integer number stream)))
;;;
;;; PBM Files
(defun %read-pbm-ascii-stream (stream)
(let ((width (parse-integer (read-number stream)))
(height (parse-integer (read-number stream))))
(let ((img (make-1-bit-gray-image height width)))
(loop for i below height
do
(loop for j below width
do (setf (pixel img i j)
(read-binary-value stream))))
img)))
(defun %read-pbm-binary-stream (stream)
(let ((width (parse-integer (read-number stream)))
(height (parse-integer (read-number stream))))
(let* ((img (make-1-bit-gray-image height width))
(bytes (make-array (1+ (ash (1- (* height width)) -3))
:element-type '(unsigned-byte 8))))
(read-sequence bytes stream)
(loop for i below height
do (loop for j below width
do (setf (pixel img i j)
(ldb (byte 1 (- 7 (mod (+ (* i width) j) 8)))
(aref bytes (ash (+ (* i width) j) -3))))))
img)))
(defun read-pbm-stream (stream)
(let ((magic (make-array 2 :element-type '(unsigned-byte 8))))
(read-sequence magic stream)
(cond ((equalp magic #(80 49)) ;; P1 magic number
(%read-pbm-ascii-stream stream))
((equalp magic #(80 52)) ;; P4 magic number
(%read-pbm-binary-stream stream))
(t (error "Invalid PBM Magic Number")))))
(defun read-pbm-file (pathname)
(with-open-file (stream pathname :direction :input :element-type '(unsigned-byte 8))
(read-pbm-stream stream)))
(defun %write-pbm-ascii-stream (stream image)
(map nil (lambda (x) (%emit-byte x stream)) "P1")
(%emit-byte #\Newline stream)
(with-image-bounds (height width)
image
(%emit-integer width stream)
(%emit-byte #\Space stream)
(%emit-integer height stream)
(%emit-byte #\Newline stream)
(loop for i below height
do (loop for j below width
do
(%emit-byte (digit-char (pixel image i j)) stream)
(if (and (plusp j) (= (mod (+ (* i width) j 1) 35) 0))
(%emit-byte #\Newline stream)
(%emit-byte #\Space stream))))))
(defun %write-pbm-binary-stream (stream image)
(map nil (lambda (x) (write-byte (char-code x) stream)) "P4")
(write-byte (char-code #\Newline) stream)
(with-image-bounds (height width)
image
(write-integer width stream)
(write-byte (char-code #\Space) stream)
(write-integer height stream)
(write-byte (char-code #\Newline) stream)
(let ((bytes (make-array (1+ (ash (1- (* height width)) -3))
:element-type '(unsigned-byte 8))))
(loop for i below height
do (loop for j below width
do
(setf (ldb (byte 1 (- 7 (mod (+ (* i width) j) 8)))
(aref bytes (ash (+ (* i width) j) -3)))
(pixel image i j))))
(write-sequence bytes stream))))
(defun write-pbm-stream (stream image &key (binary t))
(if binary
(%write-pbm-binary-stream stream image)
(%write-pbm-ascii-stream stream image)))
(defun write-pbm-file (pathname image
&key (binary t))
(with-open-file (stream pathname :direction :output
:if-exists :supersede
:element-type '(unsigned-byte 8))
(write-pbm-stream stream image :binary binary)
(truename pathname)))
;;;
;;; PGM files
(defun %read-pgm-ascii-stream (stream)
(let ((width (parse-integer (read-number stream)))
(height (parse-integer (read-number stream)))
(maxgray (parse-integer (read-number stream))))
(if (< maxgray 256)
(let ((img (make-8-bit-gray-image height width)))
(loop for i below height
do
(loop for j below width
do (setf (pixel img i j)
(parse-integer (read-number stream)))))
img)
(let ((img (make-16-bit-gray-image height width)))
(loop for i below height
do
(loop for j below width
do (setf (pixel img i j)
(parse-integer (read-number stream)))))
img))))
(defun %read-pgm-binary-stream (stream)
(let ((width (parse-integer (read-number stream)))
(height (parse-integer (read-number stream)))
(maxgray (parse-integer (read-number stream))))
(if (< maxgray 256)
(let ((img (make-8-bit-gray-image height width)))
(loop for i below height
do
(loop for j below width
do (setf (pixel img i j)
(read-byte stream))))
img)
(let ((img (make-16-bit-gray-image height width)))
(loop for i below height
do
(loop for j below width
do (setf (pixel img i j)
(+ (ash (read-byte stream) 8)
(read-byte stream)))))
img))))
(defun read-pgm-stream (stream)
(let ((magic (make-array 2 :element-type '(unsigned-byte 8))))
(read-sequence magic stream)
(cond ((equalp magic #(80 50)) ;; P2 magic number
(%read-pgm-ascii-stream stream))
((equalp magic #(80 53)) ;; P5 magic number
(%read-pgm-binary-stream stream))
(t (error "Invalid PGM Magic Number")))))
(defun read-pgm-file (pathname)
(with-open-file (stream pathname :direction :input :element-type '(unsigned-byte 8))
(read-pgm-stream stream)))
(defun %write-pgm-ascii-stream (stream image)
(map nil (lambda (x) (%emit-byte x stream)) "P2")
(%emit-byte #\Newline stream)
(with-image-bounds (height width)
image
(%emit-integer width stream)
(%emit-byte #\Space stream)
(%emit-integer height stream)
(%emit-byte #\Newline stream)
(etypecase image
(8-bit-gray-image
(%emit-integer #xff stream))
(16-bit-gray-image
(%emit-integer #xffff stream)))
(%emit-byte #\Newline stream)
(loop for i below height
do (loop for j below width
do
(%emit-integer (pixel image i j) stream)
(unless (= j (1- width))
(%emit-byte #\Space stream)))
(%emit-byte #\Newline stream))))
(defun %write-pgm-binary-stream (stream image)
(map nil (lambda (x) (write-byte (char-code x) stream)) "P5")
(write-byte (char-code #\Newline) stream)
(with-image-bounds (height width)
image
(write-integer width stream)
(write-byte (char-code #\Newline) stream)
(write-integer height stream)
(write-byte (char-code #\Newline) stream)
(etypecase image
(8-bit-gray-image
(locally
(declare (type 8-bit-gray-image image))
(write-integer #xff stream)
(write-byte (char-code #\Newline) stream)
(loop for i below height
do (loop for j below width
do (write-byte (pixel image i j) stream)))))
(16-bit-gray-image
(locally
(declare (type 16-bit-gray-image image))
(write-integer #xffff stream)
(write-byte (char-code #\Newline) stream)
(loop for i below height
do (loop for j below width
do (let ((val (pixel image i j)))
(write-byte (ash val -8) stream)
(write-byte (logand val #xff) stream)))))))))
(defun write-pgm-stream (stream image &key (binary t))
(if binary
(%write-pgm-binary-stream stream image)
(%write-pgm-ascii-stream stream image)))
(defun write-pgm-file (pathname image
&key (binary t))
(with-open-file (stream pathname :direction :output
:if-exists :supersede
:element-type '(unsigned-byte 8))
(write-pgm-stream stream image :binary binary)
(truename pathname)))
;;;
;;; PPM Files
(defun %read-ppm-ascii-stream (stream)
(let ((width (parse-integer (read-number stream)))
(height (parse-integer (read-number stream)))
(maxval (parse-integer (read-number stream))))
(if (< maxval 256)
(let ((img (make-8-bit-rgb-image height width)))
(loop for i below height
do
(loop for j below width
do (setf (pixel img i j)
(values (read-number stream)
(read-number stream)
(read-number stream)))))
img)
(let ((img (make-16-bit-gray-image height width)))
(loop for i below height
do
(loop for j below width
do (setf (pixel img i j)
(values (read-number stream)
(read-number stream)
(read-number stream)))))
img))))
(defun %read-ppm-binary-stream (stream)
(let ((width (parse-integer (read-number stream)))
(height (parse-integer (read-number stream)))
(maxval (parse-integer (read-number stream))))
(if (< maxval 256)
(let ((img (make-8-bit-rgb-image height width)))
(loop for i below height
do
(loop for j below width
do (setf (pixel img i j)
(values (read-byte stream)
(read-byte stream)
(read-byte stream)))))
img)
(let ((img (make-16-bit-gray-image height width)))
(loop for i below height
do
(loop for j below width
do (setf (pixel img i j)
(values (+ (ash (read-byte stream) 8)
(read-byte stream))
(+ (ash (read-byte stream) 8)
(read-byte stream))
(+ (ash (read-byte stream) 8)
(read-byte stream))))))
img))))
(defun read-ppm-stream (stream)
(let ((magic (make-array 2 :element-type '(unsigned-byte 8))))
(read-sequence magic stream)
(cond ((equalp magic #(80 51)) ;; P3 magic number
(%read-ppm-ascii-stream stream))
((equalp magic #(80 54)) ;; P6 magic number
(%read-ppm-binary-stream stream))
(t (error "Invalid PPM Magic Number")))))
(defun read-ppm-file (pathname)
(with-open-file (stream pathname :direction :input :element-type '(unsigned-byte 8))
(read-ppm-stream stream)))
(defun %write-ppm-ascii-stream (stream image)
(map nil (lambda (x) (%emit-byte x stream)) "P3")
(%emit-byte #\Newline stream)
(with-image-bounds (height width)
image
(%emit-integer width stream)
(%emit-byte #\Space stream)
(%emit-integer height stream)
(%emit-byte #\Newline stream)
(etypecase image
(8-bit-rgb-image (%emit-integer #xff stream))
(8-bit-rgba-image (%emit-integer #xff stream))
(16-bit-rgb-image (%emit-integer #xffff stream))
(16-bit-rgba-image (%emit-integer #xffff stream)))
(%emit-byte #\Newline stream)
(loop for i below height do
(loop for j below width do
(multiple-value-bind (r g b)
(pixel image i j)
(%emit-integer r stream)
(%emit-byte #\Space stream)
(%emit-integer g stream)
(%emit-byte #\Space stream)
(%emit-integer b stream)
(%emit-byte #\Space stream))
(unless (= j (1- width))
(%emit-byte #\Space stream)))
(%emit-byte #\Newline stream))))
(defun %write-8-bit-rgb-ppm-binary-data (stream image height width)
(write-integer #xff stream)
(write-byte (char-code #\Newline) stream)
(loop for i below height
do (loop for j below width
do (multiple-value-bind (r g b)
(pixel image i j)
(write-byte r stream)
(write-byte g stream)
(write-byte b stream)))))
(defun %write-16-bit-rgb-ppm-binary-data (stream image height width)
(write-integer #xffff stream)
(write-byte (char-code #\Newline) stream)
(loop for i below height
do (loop for j below width
do (multiple-value-bind (r g b)
(pixel image i j)
(write-word r stream)
(write-word g stream)
(write-word b stream)))))
(defun %write-ppm-binary-stream (stream image)
(map nil (lambda (x) (write-byte (char-code x) stream)) "P6")
(write-byte (char-code #\Newline) stream)
(with-image-bounds (height width)
image
(write-integer width stream)
(write-byte (char-code #\Newline) stream)
(write-integer height stream)
(write-byte (char-code #\Newline) stream)
(etypecase image
(8-bit-rgb-image
(%write-8-bit-rgb-ppm-binary-data stream image height width))
(8-bit-rgba-image
(%write-8-bit-rgb-ppm-binary-data stream image height width))
(16-bit-rgb-image
(%write-16-bit-rgb-ppm-binary-data stream image height width))
(16-bit-rgba-image
(%write-16-bit-rgb-ppm-binary-data stream image height width)))))
(defun write-ppm-stream (stream image &key (binary t))
(if binary
(%write-ppm-binary-stream stream image)
(%write-ppm-ascii-stream stream image)))
(defun write-ppm-file (pathname image
&key (binary t))
(with-open-file (stream pathname :direction :output
:if-exists :supersede
:element-type '(unsigned-byte 8))
(write-ppm-stream stream image :binary binary)
(truename pathname)))
;;;
;;; PNM Reading (based on magic number)
(defun read-pnm-stream (stream)
(let ((magic (make-array 2 :element-type '(unsigned-byte 8))))
(read-sequence magic stream)
(cond ((equalp magic #(80 49)) ;; P1 magic number
(%read-pbm-ascii-stream stream))
((equalp magic #(80 50)) ;; P2 magic number
(%read-pgm-ascii-stream stream))
((equalp magic #(80 51)) ;; P3 magic number
(%read-ppm-ascii-stream stream))
((equalp magic #(80 52)) ;; P4 magic number
(%read-pbm-binary-stream stream))
((equalp magic #(80 53)) ;; P5 magic number
(%read-pgm-binary-stream stream))
((equalp magic #(80 54)) ;; P6 magic number
(%read-ppm-binary-stream stream))
(t (error "Invalid PGM Magic Number")))))
(defun read-pnm-file (pathname)
(with-open-file (stream pathname :direction :input :element-type '(unsigned-byte 8))
(read-pnm-stream stream)))