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


(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

  (byte (- (rand-int 256) 128)))

(defn rand-bytes [n]
  (let [array (byte-array n)]
    (doseq [i (range n)]
      (aset-byte array i (rand-byte)))
    array))

(defn rand-byte-image [w h nchannels]
  (rand-bytes (* w h nchannels )))

;; write minimal one-page tiff
   
(defn write-one-page-tiff [pix w h]
  (let [mmap (open-mmap "test1.tif" 1000000000)]
    (write-seq mmap
      (create-header))
    (write-seq mmap
      (create-IFD (get-pos mmap) w h 8 1.0 true)) 
    (write-bytes mmap pix)
    (close-mmap mmap)))
   
   