/benchmarks/nfm.sc

https://github.com/barak/stalin · Scala · 409 lines · 397 code · 12 blank · 0 comment · 0 complexity · 0195614bf0b1d9773ec45b6b98c93da4 MD5 · raw file

  1. ;;; 1. Added INEXACT->EXACT wrappers around READ.
  2. ;;; 2. Changed FOR-EACH-N to DO.
  3. ;;; 3. Eliminated SQR, MATRIX-REF, MATRIX-SET!, MATRIX-ROWS, MATRIX-COLUMNS,
  4. ;;; MAKE-MATRIX, WHEN, and UNLESS.
  5. ;;; begin Stalin
  6. (define make-pbm (primitive-procedure make-structure pbm 2))
  7. (define pbm? (primitive-procedure structure? pbm))
  8. (define pbm-raw? (primitive-procedure structure-ref pbm 0))
  9. (define pbm-bitmap (primitive-procedure structure-ref pbm 1))
  10. (define make-pgm (primitive-procedure make-structure pgm 3))
  11. (define pgm? (primitive-procedure structure? pgm))
  12. (define pgm-raw? (primitive-procedure structure-ref pgm 0))
  13. (define pgm-maxval (primitive-procedure structure-ref pgm 1))
  14. (define pgm-grey (primitive-procedure structure-ref pgm 2))
  15. (define make-ppm (primitive-procedure make-structure ppm 5))
  16. (define ppm? (primitive-procedure structure? ppm))
  17. (define ppm-raw? (primitive-procedure structure-ref ppm 0))
  18. (define ppm-maxval (primitive-procedure structure-ref ppm 1))
  19. (define ppm-red (primitive-procedure structure-ref ppm 2))
  20. (define ppm-green (primitive-procedure structure-ref ppm 3))
  21. (define ppm-blue (primitive-procedure structure-ref ppm 4))
  22. ;;; end Stalin
  23. ;;; begin Scheme->C
  24. (define (make-pbm raw? bitmap) (vector 'pbm raw? bitmap))
  25. (define (pbm? pnm) (eq? (vector-ref pnm 0) 'pbm))
  26. (define (pbm-raw? pnm) (vector-ref pnm 1))
  27. (define (pbm-bitmap pnm) (vector-ref pnm 2))
  28. (define (make-pgm raw? maxval grey) (vector 'pgm raw? maxval grey))
  29. (define (pgm? pnm) (eq? (vector-ref pnm 0) 'pgm))
  30. (define (pgm-raw? pnm) (vector-ref pnm 1))
  31. (define (pgm-maxval pnm) (vector-ref pnm 2))
  32. (define (pgm-grey pnm) (vector-ref pnm 3))
  33. (define (make-ppm raw? maxval r g b) (vector 'ppm raw? maxval r g b))
  34. (define (ppm? pnm) (eq? (vector-ref pnm 0) 'ppm))
  35. (define (ppm-raw? pnm) (vector-ref pnm 1))
  36. (define (ppm-maxval pnm) (vector-ref pnm 2))
  37. (define (ppm-red pnm) (vector-ref pnm 3))
  38. (define (ppm-green pnm) (vector-ref pnm 4))
  39. (define (ppm-blue pnm) (vector-ref pnm 5))
  40. (define (panic s) (error 'panic s))
  41. ;;; end Scheme->C
  42. ;;; begin Gambit-C
  43. (define-structure pbm raw? bitmap)
  44. (define-structure pgm raw? maxval grey)
  45. (define-structure ppm raw? maxval red green blue)
  46. (define (panic s) (error s))
  47. ;;; end Gambit-C
  48. ;;; begin Bigloo
  49. (define (make-pbm raw? bitmap) (vector 'pbm raw? bitmap))
  50. (define (pbm? pnm) (eq? (vector-ref pnm 0) 'pbm))
  51. (define (pbm-raw? pnm) (vector-ref pnm 1))
  52. (define (pbm-bitmap pnm) (vector-ref pnm 2))
  53. (define (make-pgm raw? maxval grey) (vector 'pgm raw? maxval grey))
  54. (define (pgm? pnm) (eq? (vector-ref pnm 0) 'pgm))
  55. (define (pgm-raw? pnm) (vector-ref pnm 1))
  56. (define (pgm-maxval pnm) (vector-ref pnm 2))
  57. (define (pgm-grey pnm) (vector-ref pnm 3))
  58. (define (make-ppm raw? maxval r g b) (vector 'ppm raw? maxval r g b))
  59. (define (ppm? pnm) (eq? (vector-ref pnm 0) 'ppm))
  60. (define (ppm-raw? pnm) (vector-ref pnm 1))
  61. (define (ppm-maxval pnm) (vector-ref pnm 2))
  62. (define (ppm-red pnm) (vector-ref pnm 3))
  63. (define (ppm-green pnm) (vector-ref pnm 4))
  64. (define (ppm-blue pnm) (vector-ref pnm 5))
  65. (define (panic s) (error s 'panic 'panic))
  66. ;;; end Bigloo
  67. ;;; begin Chez
  68. (define (make-pbm raw? bitmap) (vector 'pbm raw? bitmap))
  69. (define (pbm? pnm) (eq? (vector-ref pnm 0) 'pbm))
  70. (define (pbm-raw? pnm) (vector-ref pnm 1))
  71. (define (pbm-bitmap pnm) (vector-ref pnm 2))
  72. (define (make-pgm raw? maxval grey) (vector 'pgm raw? maxval grey))
  73. (define (pgm? pnm) (eq? (vector-ref pnm 0) 'pgm))
  74. (define (pgm-raw? pnm) (vector-ref pnm 1))
  75. (define (pgm-maxval pnm) (vector-ref pnm 2))
  76. (define (pgm-grey pnm) (vector-ref pnm 3))
  77. (define (make-ppm raw? maxval r g b) (vector 'ppm raw? maxval r g b))
  78. (define (ppm? pnm) (eq? (vector-ref pnm 0) 'ppm))
  79. (define (ppm-raw? pnm) (vector-ref pnm 1))
  80. (define (ppm-maxval pnm) (vector-ref pnm 2))
  81. (define (ppm-red pnm) (vector-ref pnm 3))
  82. (define (ppm-green pnm) (vector-ref pnm 4))
  83. (define (ppm-blue pnm) (vector-ref pnm 5))
  84. (define (panic s) (error 'panic s))
  85. ;;; end Chez
  86. ;;; begin Chicken
  87. (define-record-type pbm raw? bitmap)
  88. (define-record-type pgm raw? maxval grey)
  89. (define-record-type ppm raw? maxval red green blue)
  90. (define (panic s) (error s))
  91. ;;; end Chicken
  92. (define (fuck-up) (panic "This shouldn't happen"))
  93. (define first car)
  94. (define second cadr)
  95. (define third caddr)
  96. (define rest cdr)
  97. (define *max-grey* 255)
  98. (define (pnm-width pnm)
  99. (vector-length (vector-ref (cond ((pbm? pnm) (pbm-bitmap pnm))
  100. ((pgm? pnm) (pgm-grey pnm))
  101. ((ppm? pnm) (ppm-red pnm))
  102. (else (panic "Argument not PNM")))
  103. 0)))
  104. (define (pnm-height pnm)
  105. (vector-length (cond ((pbm? pnm) (pbm-bitmap pnm))
  106. ((pgm? pnm) (pgm-grey pnm))
  107. ((ppm? pnm) (ppm-red pnm))
  108. (else (panic "Argument not PNM")))))
  109. (define (read-pnm pathname)
  110. (define (read-pnm port)
  111. (define (read-pbm raw?)
  112. (let* ((width (inexact->exact (read port)))
  113. (height (inexact->exact (read port)))
  114. (bitmap (let ((v (make-vector height)))
  115. (let loop ((i 0))
  116. (if (< i height)
  117. (begin (vector-set! v i (make-vector width))
  118. (loop (+ i 1)))))
  119. v)))
  120. (call-with-current-continuation
  121. (lambda (return)
  122. (cond
  123. (raw? (panic "Cannot (yet) read a raw pbm image"))
  124. (else
  125. (do ((y 0 (+ y 1))) ((= y height))
  126. (do ((x 0 (+ x 1))) ((= x width))
  127. (let ((v (read port)))
  128. (if (eof-object? v) (return #f))
  129. (vector-set! (vector-ref bitmap y) x (not (zero? v))))))))))
  130. (make-pbm raw? bitmap)))
  131. (define (read-pgm raw?)
  132. (let* ((width (inexact->exact (read port)))
  133. (height (inexact->exact (read port)))
  134. (maxval (inexact->exact (read port)))
  135. (size (* width height))
  136. (grey (let ((v (make-vector height)))
  137. (let loop ((i 0))
  138. (if (< i height)
  139. (begin (vector-set! v i (make-vector width))
  140. (loop (+ i 1)))))
  141. v)))
  142. (call-with-current-continuation
  143. (lambda (return)
  144. (cond
  145. (raw? (read-char port)
  146. (do ((y 0 (+ y 1))) ((= y height))
  147. (do ((x 0 (+ x 1))) ((= x width))
  148. (let ((c (read-char port)))
  149. (if (eof-object? c) (return #f))
  150. (vector-set! (vector-ref grey y) x (char->integer c))))))
  151. (else (do ((y 0 (+ y 1))) ((= y height))
  152. (do ((x 0 (+ x 1))) ((= x width))
  153. (let ((v (read port)))
  154. (if (eof-object? v) (return #f))
  155. (vector-set! (vector-ref grey y) x (inexact->exact v)))))))))
  156. (make-pgm raw? maxval grey)))
  157. (define (read-ppm raw?)
  158. (let* ((width (inexact->exact (read port)))
  159. (height (inexact->exact (read port)))
  160. (maxval (inexact->exact (read port)))
  161. (size (* width height))
  162. (red (let ((v (make-vector height)))
  163. (let loop ((i 0))
  164. (if (< i height)
  165. (begin (vector-set! v i (make-vector width))
  166. (loop (+ i 1)))))
  167. v))
  168. (green (let ((v (make-vector height)))
  169. (let loop ((i 0))
  170. (if (< i height)
  171. (begin (vector-set! v i (make-vector width))
  172. (loop (+ i 1)))))
  173. v))
  174. (blue (let ((v (make-vector height)))
  175. (let loop ((i 0))
  176. (if (< i height)
  177. (begin (vector-set! v i (make-vector width))
  178. (loop (+ i 1)))))
  179. v)))
  180. (call-with-current-continuation
  181. (lambda (return)
  182. (cond
  183. (raw? (read-char port)
  184. (do ((y 0 (+ y 1))) ((= y height))
  185. (do ((x 0 (+ x 1))) ((= x width))
  186. (let* ((c1 (read-char port))
  187. (c2 (read-char port))
  188. (c3 (read-char port)))
  189. (if (eof-object? c1) (return #f))
  190. (vector-set! (vector-ref red y) x (char->integer c1))
  191. (vector-set! (vector-ref green y) x (char->integer c2))
  192. (vector-set! (vector-ref blue y) x (char->integer c3))))))
  193. (else (do ((y 0 (+ y 1))) ((= y height))
  194. (do ((x 0 (+ x 1))) ((= x width))
  195. (let* ((v1 (read port))
  196. (v2 (read port))
  197. (v3 (read port)))
  198. (if (eof-object? v1) (return #f))
  199. (vector-set! (vector-ref red y) x (inexact->exact v1))
  200. (vector-set! (vector-ref green y) x (inexact->exact v2))
  201. (vector-set! (vector-ref blue y) x (inexact->exact v3)))))))))
  202. (make-ppm raw? maxval red green blue)))
  203. (let ((format (read port)))
  204. (case format
  205. ((P1) (read-pbm #f))
  206. ((P2) (read-pgm #f))
  207. ((P3) (read-ppm #f))
  208. ((P4) (read-pbm #t))
  209. ((P5) (read-pgm #t))
  210. ((P6) (read-ppm #t))
  211. (else (panic "Incorrect format for a pnm image")))))
  212. (if (string=? pathname "-")
  213. (read-pnm (current-input-port))
  214. (call-with-input-file pathname read-pnm)))
  215. (define (write-pnm pnm pathname)
  216. (define (write-pnm port)
  217. (define (write-pbm pbm)
  218. (let ((width (pnm-width pbm))
  219. (height (pnm-height pbm))
  220. (bitmap (pbm-bitmap pbm)))
  221. (write (if (pbm-raw? pbm) 'P4 'P1) port)
  222. (newline port)
  223. (write width port)
  224. (write-char #\space port)
  225. (write height port)
  226. (newline port)
  227. (if (pbm-raw? pbm)
  228. (panic "Cannot (yet) write a raw pbm image")
  229. (do ((y 0 (+ y 1))) ((= y height))
  230. (do ((x 0 (+ x 1))) ((= x width))
  231. (write (if (vector-ref (vector-ref bitmap y) x) 1 0) port)
  232. (newline port))))))
  233. (define (write-pgm pgm)
  234. (let ((width (pnm-width pgm))
  235. (height (pnm-height pgm))
  236. (grey (pgm-grey pgm)))
  237. (if (pgm-raw? pgm)
  238. (do ((y 0 (+ y 1))) ((= y height))
  239. (do ((x 0 (+ x 1))) ((= x width))
  240. (if (> (vector-ref (vector-ref grey y) x) 255)
  241. (panic "Grey value too large for raw pgm file format")))))
  242. (write (if (pgm-raw? pgm) 'P5 'P2) port)
  243. (newline port)
  244. (write width port)
  245. (write-char #\space port)
  246. (write height port)
  247. (newline port)
  248. (write (pgm-maxval pgm) port)
  249. (newline port)
  250. (if (pgm-raw? pgm)
  251. (do ((y 0 (+ y 1))) ((= y height))
  252. (do ((x 0 (+ x 1))) ((= x width))
  253. (write-char
  254. (integer->char (vector-ref (vector-ref grey y) x)) port)))
  255. (do ((y 0 (+ y 1))) ((= y height))
  256. (do ((x 0 (+ x 1))) ((= x width))
  257. (write (vector-ref (vector-ref grey y) x) port)
  258. (newline port))))))
  259. (define (write-ppm ppm)
  260. (let ((width (pnm-width ppm))
  261. (height (pnm-height ppm))
  262. (red (ppm-red ppm))
  263. (green (ppm-green ppm))
  264. (blue (ppm-blue ppm)))
  265. (if (ppm-raw? ppm)
  266. (do ((y 0 (+ y 1))) ((= y height))
  267. (do ((x 0 (+ x 1))) ((= x width))
  268. (if (or (> (vector-ref (vector-ref red y) x) 255)
  269. (> (vector-ref (vector-ref green y) x) 255)
  270. (> (vector-ref (vector-ref blue y) x) 255))
  271. (panic "Color value too large for raw ppm file format")))))
  272. (write (if (ppm-raw? ppm) 'P6 'P3) port)
  273. (newline port)
  274. (write width port)
  275. (write-char #\space port)
  276. (write height port)
  277. (newline port)
  278. (write (ppm-maxval ppm) port)
  279. (newline port)
  280. (if (ppm-raw? ppm)
  281. (do ((y 0 (+ y 1))) ((= y height))
  282. (do ((x 0 (+ x 1))) ((= x width))
  283. (write-char (integer->char (vector-ref (vector-ref red y) x)) port)
  284. (write-char (integer->char (vector-ref (vector-ref green y) x)) port)
  285. (write-char
  286. (integer->char (vector-ref (vector-ref blue y) x)) port)))
  287. (do ((y 0 (+ y 1))) ((= y height))
  288. (do ((x 0 (+ x 1))) ((= x width))
  289. (write (vector-ref (vector-ref red y) x) port)
  290. (newline port)
  291. (write (vector-ref (vector-ref green y) x) port)
  292. (newline port)
  293. (write (vector-ref (vector-ref blue y) x) port)
  294. (newline port))))))
  295. (cond ((pbm? pnm) (write-pbm pnm))
  296. ((pgm? pnm) (write-pgm pnm))
  297. ((ppm? pnm) (write-ppm pnm))
  298. (else (panic "Non-PNM argument to WRITE-PNM"))))
  299. (if (string=? pathname "-")
  300. (write-pnm (current-output-port))
  301. (call-with-output-file (string-append pathname
  302. (cond ((pbm? pnm) ".pbm")
  303. ((pgm? pnm) ".pgm")
  304. ((ppm? pnm) ".ppm")
  305. (else (fuck-up))))
  306. write-pnm
  307. ;;; begin Chez
  308. 'replace
  309. ;;; end Chez
  310. )))
  311. (define (pgm-smooth pgm sigma)
  312. (if (not (pgm? pgm)) (panic "Argument to PGM-SMOOTH is not a PGM"))
  313. (let* ((height (pnm-height pgm))
  314. (width (pnm-width pgm))
  315. (grey1 (pgm-grey pgm))
  316. (grey2 (let ((v (make-vector height)))
  317. (let loop ((i 0))
  318. (if (< i height)
  319. (begin (vector-set! v i (make-vector width 0))
  320. (loop (+ i 1)))))
  321. v)))
  322. (do ((y sigma (+ y 1))) ((= y (- height sigma)))
  323. (do ((x sigma (+ x 1))) ((= x (- width sigma)))
  324. (do ((i (- y sigma) (+ i 1))) ((= i (+ y sigma 1)))
  325. (do ((j (- x sigma) (+ j 1))) ((= j (+ x sigma 1)))
  326. (vector-set!
  327. (vector-ref grey2 y) x (+ (vector-ref (vector-ref grey2 y) x)
  328. (vector-ref (vector-ref grey1 i) j)))))
  329. (vector-set! (vector-ref grey2 y) x
  330. (inexact->exact
  331. (floor (/ (vector-ref (vector-ref grey2 y) x)
  332. (* (+ sigma sigma 1) (+ sigma sigma 1))))))))
  333. (make-pgm (pgm-raw? pgm) *max-grey* grey2)))
  334. (define (normal-flow-magnitude pgm1 pgm2 epsilon sigma sensitivity)
  335. (if (not (and (pgm? pgm1)
  336. (pgm? pgm2)
  337. (= (pgm-maxval pgm1) (pgm-maxval pgm2))
  338. (eq? (pgm-raw? pgm1) (pgm-raw? pgm2))
  339. (= (pnm-width pgm1) (pnm-width pgm2))
  340. (= (pnm-height pgm1) (pnm-height pgm2))))
  341. (panic "Arguments to NORMAL-FLOW-MAGNITUDE are not matching PGMs"))
  342. (let* ((width (pnm-width pgm1))
  343. (height (pnm-height pgm1))
  344. (e1 (pgm-grey (pgm-smooth pgm1 sigma)))
  345. (e2 (pgm-grey (pgm-smooth pgm2 sigma)))
  346. (m (let ((v (make-vector height)))
  347. (let loop ((i 0))
  348. (if (< i height)
  349. (begin (vector-set! v i (make-vector width 0))
  350. (loop (+ i 1)))))
  351. v)))
  352. (do ((i 0 (+ i 1))) ((= i (- height 1)))
  353. (do ((j 0 (+ j 1))) ((= j (- width 1)))
  354. (let* ((ex (/ (- (+ (vector-ref (vector-ref e1 (+ i 1)) j)
  355. (vector-ref (vector-ref e1 (+ i 1)) (+ j 1))
  356. (vector-ref (vector-ref e2 (+ i 1)) j)
  357. (vector-ref (vector-ref e2 (+ i 1)) (+ j 1)))
  358. (+ (vector-ref (vector-ref e1 i) j)
  359. (vector-ref (vector-ref e1 i) (+ j 1))
  360. (vector-ref (vector-ref e2 i) j)
  361. (vector-ref (vector-ref e2 i) (+ j 1))))
  362. 4.0))
  363. (ey (/ (- (+ (vector-ref (vector-ref e1 i) (+ j 1))
  364. (vector-ref (vector-ref e1 (+ i 1)) (+ j 1))
  365. (vector-ref (vector-ref e2 i) (+ j 1))
  366. (vector-ref (vector-ref e2 (+ i 1)) (+ j 1)))
  367. (+ (vector-ref (vector-ref e1 i) j)
  368. (vector-ref (vector-ref e1 (+ i 1)) j)
  369. (vector-ref (vector-ref e2 i) j)
  370. (vector-ref (vector-ref e2 (+ i 1)) j)))
  371. 4.0))
  372. (et (/ (- (+ (vector-ref (vector-ref e2 i) j)
  373. (vector-ref (vector-ref e2 i) (+ j 1))
  374. (vector-ref (vector-ref e2 (+ i 1)) j)
  375. (vector-ref (vector-ref e2 (+ i 1)) (+ j 1)))
  376. (+ (vector-ref (vector-ref e1 i) j)
  377. (vector-ref (vector-ref e1 i) (+ j 1))
  378. (vector-ref (vector-ref e1 (+ i 1)) j)
  379. (vector-ref (vector-ref e1 (+ i 1)) (+ j 1))))
  380. 4.0))
  381. (l (sqrt (+ (* ex ex) (* ey ey)))))
  382. (vector-set!
  383. (vector-ref m i) j
  384. (min *max-grey*
  385. (inexact->exact
  386. (floor
  387. (* *max-grey*
  388. (/ (if (< l epsilon) 0.0 (/ (abs et) l)) sensitivity)))))))))
  389. (pgm-smooth (make-pgm (pgm-raw? pgm1) *max-grey* m) sigma)))
  390. (define (test pgm1 pgm2)
  391. (write-pnm (normal-flow-magnitude pgm1 pgm2 1.25 1 20.0) "pick-up-flow")
  392. #f)
  393. (let ((pgm1 (read-pnm "pick-up00-0.pgm"))
  394. (pgm2 (read-pnm "pick-up00-1.pgm")))
  395. (do ((i 0 (+ i 1))) ((= i 10))
  396. (test pgm1 pgm2)
  397. (test pgm1 pgm2)))