     (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)
+          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 %)


(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))))  
