         dsb (.asShortBuffer dbb)]
     {:rwChannel rwChannel :byte-buf dbb :short-buf dsb}))
         
-(defn write-header [mmap]
-  (.put (:byte-buf mmap) (create-header)))
+(defn write-seq [mmap byte-vals]
+  (.put (:byte-buf mmap) (byte-array (map byte byte-vals))))  
 
-;(defn write-image [pix w h bits-per-pixel pixel-size-um final-image?]
-  
+(defn write-bytes [mmap bytes]
+  (.put (:byte-buf mmap) bytes))
+
+(defn get-pos [mmap]
+  (.position (:byte-buf mmap)))
+
+(defn close-mmap [mmap]
+  (let [chan (:rwChannel mmap)]
+    (doto chan
+      (.truncate (get-pos mmap))
+      .close)))
 
 ; (.position dbb) <-- gets cursor pos
 ; (.position dbb 10) <-- sets cursor pos
-; (.put dbb (byte-array (map byte (range 10)))) 
-; (def q (byte-array 10))
 ; (.get dbb q) <-- read array
-; (.position dsb (bit-shift-right (.position dbb) 1))
-
 
 ;; testing
-
 
 ;;;; image generation
 


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

(defn make-tag
  "Create a big-endian tiff image IFD entry"
  [tag type count value]
  (let [tag-code (tag tag-codes)
        type-code (type type-codes)]
    (concat
      (make-bytes tag-code 2)
      (make-bytes type-code 2)
      (make-bytes count 4)
      (make-bytes value 4))))

(defn rational-value [x]
  (apply concat

  "Create a big-endian tiff image IFD."
  [current-offset width height bits-per-pixel
   pixel-size-um final-image?]
  (flatten
    (let [ifd-entry-count 11
          pixel-size-cm (* 1e-4 pixel-size-um)
          pixels-per-image (* width height)
          bytes-per-image (* width height (bit-shift-right bits-per-pixel 1))
          pixel-size-offset (+ 2 (* 12 ifd-entry-count) current-offset)
          image-offset (+ pixel-size-offset 8)
          ifd-entries
            (map #(apply make-tag %)
              [[:ImageWidth :long 1 width]
               [:ImageLength :long 1 height]
               [:BitsPerSample :short 1 bits-per-pixel]
               [:Compression :short 1 1] ; No compression
               [:PhotometricInterpretation :short 1 1] ; (Black=0;White=Max)
               [:StripOffsets :long 1 image-offset]
               [:RowsPerStrip :long 1 height]
               [: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)]])
              ]
      (concat
        (make-bytes ifd-entry-count 2)
        (list ifd-entries)
        (make-bytes (if final-image? 0 (+ image-offset bytes-per-image)) 4)
        (rational-value pixel-size-cm)))))


;; Memory-mapped files
