File: nfm.sc

package info (click to toggle)
stalin 0.11-6
  • links: PTS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 110,396 kB
  • ctags: 163,122
  • sloc: ansic: 1,757,574; lisp: 88,332; sh: 1,514; makefile: 229; sed: 100; csh: 30
file content (409 lines) | stat: -rw-r--r-- 14,782 bytes parent folder | download | duplicates (6)
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
;;; 1. Added INEXACT->EXACT wrappers around READ.
;;; 2. Changed FOR-EACH-N to DO.
;;; 3. Eliminated SQR, MATRIX-REF, MATRIX-SET!, MATRIX-ROWS, MATRIX-COLUMNS,
;;;    MAKE-MATRIX, WHEN, and UNLESS.

;;; begin Stalin
(define make-pbm (primitive-procedure make-structure pbm 2))
(define pbm? (primitive-procedure structure? pbm))
(define pbm-raw? (primitive-procedure structure-ref pbm 0))
(define pbm-bitmap (primitive-procedure structure-ref pbm 1))
(define make-pgm (primitive-procedure make-structure pgm 3))
(define pgm? (primitive-procedure structure? pgm))
(define pgm-raw? (primitive-procedure structure-ref pgm 0))
(define pgm-maxval (primitive-procedure structure-ref pgm 1))
(define pgm-grey (primitive-procedure structure-ref pgm 2))
(define make-ppm (primitive-procedure make-structure ppm 5))
(define ppm? (primitive-procedure structure? ppm))
(define ppm-raw? (primitive-procedure structure-ref ppm 0))
(define ppm-maxval (primitive-procedure structure-ref ppm 1))
(define ppm-red (primitive-procedure structure-ref ppm 2))
(define ppm-green (primitive-procedure structure-ref ppm 3))
(define ppm-blue (primitive-procedure structure-ref ppm 4))
;;; end Stalin
;;; begin Scheme->C
(define (make-pbm raw? bitmap) (vector 'pbm raw? bitmap))
(define (pbm? pnm) (eq? (vector-ref pnm 0) 'pbm))
(define (pbm-raw? pnm) (vector-ref pnm 1))
(define (pbm-bitmap pnm) (vector-ref pnm 2))
(define (make-pgm raw? maxval grey) (vector 'pgm raw? maxval grey))
(define (pgm? pnm) (eq? (vector-ref pnm 0) 'pgm))
(define (pgm-raw? pnm) (vector-ref pnm 1))
(define (pgm-maxval pnm) (vector-ref pnm 2))
(define (pgm-grey pnm) (vector-ref pnm 3))
(define (make-ppm raw? maxval r g b) (vector 'ppm raw? maxval r g b))
(define (ppm? pnm) (eq? (vector-ref pnm 0) 'ppm))
(define (ppm-raw? pnm) (vector-ref pnm 1))
(define (ppm-maxval pnm) (vector-ref pnm 2))
(define (ppm-red pnm) (vector-ref pnm 3))
(define (ppm-green pnm) (vector-ref pnm 4))
(define (ppm-blue pnm) (vector-ref pnm 5))
(define (panic s) (error 'panic s))
;;; end Scheme->C
;;; begin Gambit-C
(define-structure pbm raw? bitmap)
(define-structure pgm raw? maxval grey)
(define-structure ppm raw? maxval red green blue)
(define (panic s) (error s))
;;; end Gambit-C
;;; begin Bigloo
(define (make-pbm raw? bitmap) (vector 'pbm raw? bitmap))
(define (pbm? pnm) (eq? (vector-ref pnm 0) 'pbm))
(define (pbm-raw? pnm) (vector-ref pnm 1))
(define (pbm-bitmap pnm) (vector-ref pnm 2))
(define (make-pgm raw? maxval grey) (vector 'pgm raw? maxval grey))
(define (pgm? pnm) (eq? (vector-ref pnm 0) 'pgm))
(define (pgm-raw? pnm) (vector-ref pnm 1))
(define (pgm-maxval pnm) (vector-ref pnm 2))
(define (pgm-grey pnm) (vector-ref pnm 3))
(define (make-ppm raw? maxval r g b) (vector 'ppm raw? maxval r g b))
(define (ppm? pnm) (eq? (vector-ref pnm 0) 'ppm))
(define (ppm-raw? pnm) (vector-ref pnm 1))
(define (ppm-maxval pnm) (vector-ref pnm 2))
(define (ppm-red pnm) (vector-ref pnm 3))
(define (ppm-green pnm) (vector-ref pnm 4))
(define (ppm-blue pnm) (vector-ref pnm 5))
(define (panic s) (error s 'panic 'panic))
;;; end Bigloo
;;; begin Chez
(define (make-pbm raw? bitmap) (vector 'pbm raw? bitmap))
(define (pbm? pnm) (eq? (vector-ref pnm 0) 'pbm))
(define (pbm-raw? pnm) (vector-ref pnm 1))
(define (pbm-bitmap pnm) (vector-ref pnm 2))
(define (make-pgm raw? maxval grey) (vector 'pgm raw? maxval grey))
(define (pgm? pnm) (eq? (vector-ref pnm 0) 'pgm))
(define (pgm-raw? pnm) (vector-ref pnm 1))
(define (pgm-maxval pnm) (vector-ref pnm 2))
(define (pgm-grey pnm) (vector-ref pnm 3))
(define (make-ppm raw? maxval r g b) (vector 'ppm raw? maxval r g b))
(define (ppm? pnm) (eq? (vector-ref pnm 0) 'ppm))
(define (ppm-raw? pnm) (vector-ref pnm 1))
(define (ppm-maxval pnm) (vector-ref pnm 2))
(define (ppm-red pnm) (vector-ref pnm 3))
(define (ppm-green pnm) (vector-ref pnm 4))
(define (ppm-blue pnm) (vector-ref pnm 5))
(define (panic s) (error 'panic s))
;;; end Chez
;;; begin Chicken
(define-record-type pbm raw? bitmap)
(define-record-type pgm raw? maxval grey)
(define-record-type ppm raw? maxval red green blue)
(define (panic s) (error s))
;;; end Chicken

(define (fuck-up) (panic "This shouldn't happen"))

(define first car)
(define second cadr)
(define third caddr)
(define rest cdr)

(define *max-grey* 255)

(define (pnm-width pnm)
 (vector-length (vector-ref (cond ((pbm? pnm) (pbm-bitmap pnm))
				  ((pgm? pnm) (pgm-grey pnm))
				  ((ppm? pnm) (ppm-red pnm))
				  (else (panic "Argument not PNM")))
			    0)))

(define (pnm-height pnm)
 (vector-length (cond ((pbm? pnm) (pbm-bitmap pnm))
		      ((pgm? pnm) (pgm-grey pnm))
		      ((ppm? pnm) (ppm-red pnm))
		      (else (panic "Argument not PNM")))))

(define (read-pnm pathname)
 (define (read-pnm port)
  (define (read-pbm raw?)
   (let* ((width (inexact->exact (read port)))
	  (height (inexact->exact (read port)))
	  (bitmap (let ((v (make-vector height)))
		   (let loop ((i 0))
		    (if (< i height)
			(begin (vector-set! v i (make-vector width))
			       (loop (+ i 1)))))
		   v)))
    (call-with-current-continuation
     (lambda (return)
      (cond
       (raw? (panic "Cannot (yet) read a raw pbm image"))
       (else
	(do ((y 0 (+ y 1))) ((= y height))
	 (do ((x 0 (+ x 1))) ((= x width))
	  (let ((v (read port)))
	   (if (eof-object? v) (return #f))
	   (vector-set! (vector-ref bitmap y) x (not (zero? v))))))))))
    (make-pbm raw? bitmap)))
  (define (read-pgm raw?)
   (let* ((width (inexact->exact (read port)))
	  (height (inexact->exact (read port)))
	  (maxval (inexact->exact (read port)))
	  (size (* width height))
	  (grey (let ((v (make-vector height)))
		 (let loop ((i 0))
		  (if (< i height)
		      (begin (vector-set! v i (make-vector width))
			     (loop (+ i 1)))))
		 v)))
    (call-with-current-continuation
     (lambda (return)
      (cond
       (raw? (read-char port)
	     (do ((y 0 (+ y 1))) ((= y height))
	      (do ((x 0 (+ x 1))) ((= x width))
	       (let ((c (read-char port)))
		(if (eof-object? c) (return #f))
		(vector-set! (vector-ref grey y) x (char->integer c))))))
       (else (do ((y 0 (+ y 1))) ((= y height))
	      (do ((x 0 (+ x 1))) ((= x width))
	       (let ((v (read port)))
		(if (eof-object? v) (return #f))
		(vector-set! (vector-ref grey y) x (inexact->exact v)))))))))
    (make-pgm raw? maxval grey)))
  (define (read-ppm raw?)
   (let* ((width (inexact->exact (read port)))
	  (height (inexact->exact (read port)))
	  (maxval (inexact->exact (read port)))
	  (size (* width height))
	  (red (let ((v (make-vector height)))
		(let loop ((i 0))
		 (if (< i height)
		     (begin (vector-set! v i (make-vector width))
			    (loop (+ i 1)))))
		v))
	  (green (let ((v (make-vector height)))
		  (let loop ((i 0))
		   (if (< i height)
		       (begin (vector-set! v i (make-vector width))
			      (loop (+ i 1)))))
		  v))
	  (blue (let ((v (make-vector height)))
		 (let loop ((i 0))
		  (if (< i height)
		      (begin (vector-set! v i (make-vector width))
			     (loop (+ i 1)))))
		 v)))
    (call-with-current-continuation
     (lambda (return)
      (cond
       (raw? (read-char port)
	     (do ((y 0 (+ y 1))) ((= y height))
	      (do ((x 0 (+ x 1))) ((= x width))
	       (let* ((c1 (read-char port))
		      (c2 (read-char port))
		      (c3 (read-char port)))
		(if (eof-object? c1) (return #f))
		(vector-set! (vector-ref red y) x (char->integer c1))
		(vector-set! (vector-ref green y) x (char->integer c2))
		(vector-set! (vector-ref blue y) x (char->integer c3))))))
       (else (do ((y 0 (+ y 1))) ((= y height))
	      (do ((x 0 (+ x 1))) ((= x width))
	       (let* ((v1 (read port))
		      (v2 (read port))
		      (v3 (read port)))
		(if (eof-object? v1) (return #f))
		(vector-set! (vector-ref red y) x (inexact->exact v1))
		(vector-set! (vector-ref green y) x (inexact->exact v2))
		(vector-set! (vector-ref blue y) x (inexact->exact v3)))))))))
    (make-ppm raw? maxval red green blue)))
  (let ((format (read port)))
   (case format
    ((P1) (read-pbm #f))
    ((P2) (read-pgm #f))
    ((P3) (read-ppm #f))
    ((P4) (read-pbm #t))
    ((P5) (read-pgm #t))
    ((P6) (read-ppm #t))
    (else (panic "Incorrect format for a pnm image")))))
 (if (string=? pathname "-")
     (read-pnm (current-input-port))
     (call-with-input-file pathname read-pnm)))

(define (write-pnm pnm pathname)
 (define (write-pnm port)
  (define (write-pbm pbm)
   (let ((width (pnm-width pbm))
	 (height (pnm-height pbm))
	 (bitmap (pbm-bitmap pbm)))
    (write (if (pbm-raw? pbm) 'P4 'P1) port)
    (newline port)
    (write width port)
    (write-char #\space port)
    (write height port)
    (newline port)
    (if (pbm-raw? pbm)
	(panic "Cannot (yet) write a raw pbm image")
	(do ((y 0 (+ y 1))) ((= y height))
	 (do ((x 0 (+ x 1))) ((= x width))
	  (write (if (vector-ref (vector-ref bitmap y) x) 1 0) port)
	  (newline port))))))
  (define (write-pgm pgm)
   (let ((width (pnm-width pgm))
	 (height (pnm-height pgm))
	 (grey (pgm-grey pgm)))
    (if (pgm-raw? pgm)
	(do ((y 0 (+ y 1))) ((= y height))
	 (do ((x 0 (+ x 1))) ((= x width))
	  (if (> (vector-ref (vector-ref grey y) x) 255)
	      (panic "Grey value too large for raw pgm file format")))))
    (write (if (pgm-raw? pgm) 'P5 'P2) port)
    (newline port)
    (write width port)
    (write-char #\space port)
    (write height port)
    (newline port)
    (write (pgm-maxval pgm) port)
    (newline port)
    (if (pgm-raw? pgm)
	(do ((y 0 (+ y 1))) ((= y height))
	 (do ((x 0 (+ x 1))) ((= x width))
	  (write-char
	   (integer->char (vector-ref (vector-ref grey y) x)) port)))
	(do ((y 0 (+ y 1))) ((= y height))
	 (do ((x 0 (+ x 1))) ((= x width))
	  (write (vector-ref (vector-ref grey y) x) port)
	  (newline port))))))
  (define (write-ppm ppm)
   (let ((width (pnm-width ppm))
	 (height (pnm-height ppm))
	 (red (ppm-red ppm))
	 (green (ppm-green ppm))
	 (blue (ppm-blue ppm)))
    (if (ppm-raw? ppm)
	(do ((y 0 (+ y 1))) ((= y height))
	 (do ((x 0 (+ x 1))) ((= x width))
	  (if (or (> (vector-ref (vector-ref red y) x) 255)
		  (> (vector-ref (vector-ref green y) x) 255)
		  (> (vector-ref (vector-ref blue y) x) 255))
	      (panic "Color value too large for raw ppm file format")))))
    (write (if (ppm-raw? ppm) 'P6 'P3) port)
    (newline port)
    (write width port)
    (write-char #\space port)
    (write height port)
    (newline port)
    (write (ppm-maxval ppm) port)
    (newline port)
    (if (ppm-raw? ppm)
	(do ((y 0 (+ y 1))) ((= y height))
	 (do ((x 0 (+ x 1))) ((= x width))
	  (write-char (integer->char (vector-ref (vector-ref red y) x)) port)
	  (write-char (integer->char (vector-ref (vector-ref green y) x)) port)
	  (write-char
	   (integer->char (vector-ref (vector-ref blue y) x)) port)))
	(do ((y 0 (+ y 1))) ((= y height))
	 (do ((x 0 (+ x 1))) ((= x width))
	  (write (vector-ref (vector-ref red y) x) port)
	  (newline port)
	  (write (vector-ref (vector-ref green y) x) port)
	  (newline port)
	  (write (vector-ref (vector-ref blue y) x) port)
	  (newline port))))))
  (cond ((pbm? pnm) (write-pbm pnm))
	((pgm? pnm) (write-pgm pnm))
	((ppm? pnm) (write-ppm pnm))
	(else (panic "Non-PNM argument to WRITE-PNM"))))
 (if (string=? pathname "-")
     (write-pnm (current-output-port))
     (call-with-output-file (string-append pathname
					   (cond ((pbm? pnm) ".pbm")
						 ((pgm? pnm) ".pgm")
						 ((ppm? pnm) ".ppm")
						 (else (fuck-up))))
      write-pnm
;;; begin Chez
      'replace
;;; end Chez
      )))

(define (pgm-smooth pgm sigma)
 (if (not (pgm? pgm)) (panic "Argument to PGM-SMOOTH is not a PGM"))
 (let* ((height (pnm-height pgm))
	(width (pnm-width pgm))
	(grey1 (pgm-grey pgm))
	(grey2 (let ((v (make-vector height)))
		(let loop ((i 0))
		 (if (< i height)
		     (begin (vector-set! v i (make-vector width 0))
			    (loop (+ i 1)))))
		v)))
  (do ((y sigma (+ y 1))) ((= y (- height sigma)))
   (do ((x sigma (+ x 1))) ((= x (- width sigma)))
    (do ((i (- y sigma) (+ i 1))) ((= i (+ y sigma 1)))
     (do ((j (- x sigma) (+ j 1))) ((= j (+ x sigma 1)))
      (vector-set!
       (vector-ref grey2 y) x (+ (vector-ref (vector-ref grey2 y) x)
				 (vector-ref (vector-ref grey1 i) j)))))
    (vector-set! (vector-ref grey2 y) x
		 (inexact->exact
		  (floor (/ (vector-ref (vector-ref grey2 y) x)
			    (* (+ sigma sigma 1) (+ sigma sigma 1))))))))
  (make-pgm (pgm-raw? pgm) *max-grey* grey2)))

(define (normal-flow-magnitude pgm1 pgm2 epsilon sigma sensitivity)
 (if (not (and (pgm? pgm1)
	       (pgm? pgm2)
	       (= (pgm-maxval pgm1) (pgm-maxval pgm2))
	       (eq? (pgm-raw? pgm1) (pgm-raw? pgm2))
	       (= (pnm-width pgm1) (pnm-width pgm2))
	       (= (pnm-height pgm1) (pnm-height pgm2))))
     (panic "Arguments to NORMAL-FLOW-MAGNITUDE are not matching PGMs"))
 (let* ((width (pnm-width pgm1))
	(height (pnm-height pgm1))
	(e1 (pgm-grey (pgm-smooth pgm1 sigma)))
	(e2 (pgm-grey (pgm-smooth pgm2 sigma)))
	(m (let ((v (make-vector height)))
	    (let loop ((i 0))
	     (if (< i height)
		 (begin (vector-set! v i (make-vector width 0))
			(loop (+ i 1)))))
	    v)))
  (do ((i 0 (+ i 1))) ((= i (- height 1)))
   (do ((j 0 (+ j 1))) ((= j (- width 1)))
    (let* ((ex (/ (- (+ (vector-ref (vector-ref e1 (+ i 1)) j)
			(vector-ref (vector-ref e1 (+ i 1)) (+ j 1))
			(vector-ref (vector-ref e2 (+ i 1)) j)
			(vector-ref (vector-ref e2 (+ i 1)) (+ j 1)))
		     (+ (vector-ref (vector-ref e1 i) j)
			(vector-ref (vector-ref e1 i) (+ j 1))
			(vector-ref (vector-ref e2 i) j)
			(vector-ref (vector-ref e2 i) (+ j 1))))
		  4.0))
	   (ey (/ (- (+ (vector-ref (vector-ref e1 i) (+ j 1))
			(vector-ref (vector-ref e1 (+ i 1)) (+ j 1))
			(vector-ref (vector-ref e2 i) (+ j 1))
			(vector-ref (vector-ref e2 (+ i 1)) (+ j 1)))
		     (+ (vector-ref (vector-ref e1 i) j)
			(vector-ref (vector-ref e1 (+ i 1)) j)
			(vector-ref (vector-ref e2 i) j)
			(vector-ref (vector-ref e2 (+ i 1)) j)))
		  4.0))
	   (et (/ (- (+ (vector-ref (vector-ref e2 i) j)
			(vector-ref (vector-ref e2 i) (+ j 1))
			(vector-ref (vector-ref e2 (+ i 1)) j)
			(vector-ref (vector-ref e2 (+ i 1)) (+ j 1)))
		     (+ (vector-ref (vector-ref e1 i) j)
			(vector-ref (vector-ref e1 i) (+ j 1))
			(vector-ref (vector-ref e1 (+ i 1)) j)
			(vector-ref (vector-ref e1 (+ i 1)) (+ j 1))))
		  4.0))
	   (l (sqrt (+ (* ex ex) (* ey ey)))))
     (vector-set!
      (vector-ref m i) j
      (min *max-grey*
	   (inexact->exact
	    (floor
	     (* *max-grey*
		(/ (if (< l epsilon) 0.0 (/ (abs et) l)) sensitivity)))))))))
  (pgm-smooth (make-pgm (pgm-raw? pgm1) *max-grey* m) sigma)))

(define (test pgm1 pgm2)
 (write-pnm (normal-flow-magnitude pgm1 pgm2 1.25 1 20.0) "pick-up-flow")
 #f)

(let ((pgm1 (read-pnm "pick-up00-0.pgm"))
      (pgm2 (read-pnm "pick-up00-1.pgm")))
 (do ((i 0 (+ i 1))) ((= i 10))
  (test pgm1 pgm2)
  (test pgm1 pgm2)))