 
 ;; write minimal one-page tiff
    
-(defn write-one-page-tiff [pix w h]
-  (let [mmap (open-mmap "test1.tif" 1000000000)]
+(defn write-one-page-tiff [name pix w h]
+  (let [mmap (open-mmap name 1000000000)]
     (write-seq mmap
       (create-header))
     (write-seq mmap
       (create-IFD (get-pos mmap) w h 8 1.0 true)) 
-    (write-bytes mmap pix)
+    (dorun (repeatedly 500 #(write-bytes mmap pix)))
+    (close-mmap mmap)))
+
+(defn write-multi-page-tiff [name pix w h n]
+  (if (not= (* w h) (count pix))
+    (throw (Exception. "Pixel array doesn't match requested dimensions.")))
+  (let [mmap (open-mmap name 1200000000)]
+    (write-seq mmap
+      (create-header))
+    (doseq [i (range n)]
+      (let [last-image (= i (dec n))
+            pos (get-pos mmap)]
+        ;(println i)
+        (write-seq mmap
+          (create-IFD pos w h 8 1.0 last-image))
+        (write-bytes mmap pix)
+       ; (println "pos:" (get-pos mmap))
+        ))
     (close-mmap mmap)))
    
-   +(defn big-test []
+  (let [img (rand-byte-image 2560 4320 1)]
+    (time (write-multi-page-tiff "test.tif" img 2560 4320 100))))
+

  (let [chan (:rwChannel mmap)]
    (doto chan
      (.truncate (get-pos mmap))
      .close)))

; (.position dbb) <-- gets cursor pos
; (.position dbb 10) <-- sets cursor pos

    (let [ifd-entry-count 11
          pixel-size-cm (* 1e-4 pixel-size-um)
          pixels-per-image (* width height)
          bytes-per-image (* width height (/ bits-per-pixel 8))
          pixel-size-offset (+ current-offset 2 (* 12 ifd-entry-count) 4 2)
          image-offset (+ pixel-size-offset 8)
          ifd-entries
            (map #(apply make-tag %)
