   (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


(def type-codes {:byte 1, :ascii 2, :short 3, :long 4, :rational 5})

(def type-length {:byte 1, :ascii 1, :short 2, :long 4, :rational 8})

(defn make-tag
  "Create a big-endian tiff image IFD entry"

               [:StripByteCounts :long 1 (* width height)]
               [:XResolution :rational 1 pixel-size-offset]
               [:YResolution :rational 1 pixel-size-offset]
               [:ResolutionUnit :short 1 (if pixel-size-cm 3 1)]
               ])
              ]
    ;  (println "pso:" pixel-size-offset ", bpi:" bytes-per-image ", current: " current-offset "; next: "  (+ image-offset bytes-per-image))
      (concat
        (make-bytes ifd-entry-count 2)
        (list ifd-entries)
        (make-bytes (if final-image? 0 (+ image-offset bytes-per-image)) 4)
        (make-bytes 0 2)
        (rational-value pixel-size-cm)
        ))))


;; Memory-mapped files

(defn open-mmap [file size]
  (let [f (RandomAccessFile. file "rwd")
        rwChannel (.getChannel f)
        dbb (.map rwChannel FileChannel$MapMode/READ_WRITE 0 size)
        dsb (.asShortBuffer dbb)]
    {:file f :rwChannel rwChannel :byte-buf dbb :short-buf dsb}))
        
(defn write-seq [mmap byte-vals]
  (.put (:byte-buf mmap) (byte-array (map byte byte-vals))))  
