   (let [chan (:rwChannel mmap)]
     (doto chan
       (.truncate (get-pos mmap))
-      .close)))
+      .close)
+    (.close (:file mmap))))
 
 ; (.position dbb) <-- gets cursor pos
 ; (.position dbb 10) <-- sets cursor pos

          (map #(bit-and 0xFF %)
           (take n (iterate #(bit-shift-right % 8) val))))]
    (for [char chars]
      (if (> char 0x7F) (- char 0xFF) char))))

(def tag-codes
  {


;; write minimal one-page tiff
   
(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)) 
    (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))))

