open Prelude
let niter = 255
let limit = 4.0
let mandelbrot x y w h =
let cr = 2.0 *. x /. w . 1.5 in
let ci = 2.0 *. y /. h . 1.0 in
let rec aux i zr zi = if i >= niter then 0 else
let nzr = zr *. zr . zi *. zi +. cr
and nzi = 2.0 *. zr *. zi +. ci in
if nzr *. nzr +. nzi *. nzi > limit then 255i
else aux (i+1) nzr nzi in
aux 0 0.0 0.0
let draw_fractal xoff yoff w h =
pbinit (fun i >
let y, x = quot_rem i w in
mandelbrot (float (xoff+x)) (float (yoff+y)) (float w) (float h)
) (w*h)
let blend a b w h =
let c = pbinit (fun i >
let y, x = quot_rem i w in
let r = ord (suget b i) / 32 in
let x1 = max (xr) 0
and x2 = min (x+r) (w1) in
saverage (ssub (y*w+x1) (x2x1+1) a)
) (slen a) in
pbinit (fun i >
let y, x = quot_rem i w in
let r = ord (suget b i) / 32 in
let y1 = max (yr) 0
and y2 = min (y+r) (h1) in
saverage (ssubStride w (y1*w+x) (y2y1+1) c)
) (slen a)
let square_fractal d =
let frac1 = draw_fractal 0 0 d d in
let frac2 = draw_fractal (d/4) (d/4) d d in
let header = sprintf "P5 %i %i 255\n" d d in
header ^ (blend frac1 frac2 d d)
let () =
let img = square_fractal (parseInt Sys.argv.(1)) in
output_string stdout img
art with code
20080901
Adaptive blur filter Mandelbrot
To finish off today's Mandelbrot madness, here's a version that does a ghetto 2D box filter with kernel size controlled by the second image (i.e. blur more where second image is white, don't blur where it's black.) Using a second core gives an 80% speed boost.
Subscribe to:
Post Comments (Atom)
Blog Archive

▼
2008
(107)

▼
September
(17)
 I/O in programming languages: open and read
 Basics of I/O
 Building an OCaml array library from basic operati...
 A month and a half of iPhone 3G
 Gitbug  Inrepo bug tracker for git
 prelude.ml: now on GitHub
 Slowmotion Missile Fleet
 prelude.ml: further modularization
 prelude.ml: more combinatorial wanking
 prelude.ml: range iterators
 "Shared Memory" Parallelism
 Noncopying forked workers using Bigarrays
 Constantspace parallel combinators in OCaml
 Haskell on parallel hardware
 Almost Burning Ship
 Adaptive blur filter Mandelbrot
 Prelude.ml  more multicore mandelbrot

▼
September
(17)
About Me

Ilmari Heikkinen
 Built art installations, web sites, graphics libraries, web browsers, mobile apps, desktop apps, media player themes, many nutty prototypes