 (defn create-header
   "Creates a big-endian tiff image file header."
   []
-  (byte-array (map byte [77 77 0 42 0 0 0 8])))
+  [77 77 0 42 0 0 0 8])
 
 (defn make-bytes [val n]
-  (reverse
-    (map #(bit-and 0xFF %)
-         (take n (iterate #(bit-shift-right % 8) val)))))
+  (let [chars
+        (reverse
+          (map #(bit-and 0xFF %)
+           (take n (iterate #(bit-shift-right % 8) val))))]
+    (for [char chars]
+      (if (> char 0x7F) (- char 0xFF) char))))
 
 (def tag-codes
   {

  "Create a big-endian tiff image IFD."
  [current-offset width height bits-per-pixel
   pixel-size-um final-image?]
  (let [pixel-size-cm (* 1e-4 pixel-size-um)
        pixel-size-offset (+ 2 (* 12 11) current-offset)
        image-offset (+ pixel-size-offset 8)
        pixels-per-image (* width height)
        bytes-per-image (* width height (bit-shift-right bits-per-pixel 1))]
    (concat
      (make-bytes 1 2)
      (apply concat
        (map #(apply make-tag %)
          [[:ImageWidth :long 1 width]
           [:ImageWidth :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)]]))
       (make-bytes (if final-image? 0 (+ image-offset bytes-per-image)) 4)
       (rational-value pixel-size-cm))))


;; Memory-mapped files


(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"
  [tag type count value]
  (let [tag-code (tag tag-codes)
        type-code (type type-codes)
        type-length (type type-length)]
    (concat
      (make-bytes tag-code 2)
      (make-bytes type-code 2)
      (make-bytes count 4)
      (make-bytes value type-length)
      (make-bytes 0 (- 4 type-length)))))

(defn rational-value [x]
  (apply concat
