annotate clojure_magick/magick.clj @ 11:1f112b4f9e8f tip

Fixed what was baroque.
author Dylan Holmes <ocsenave@gmail.com>
date Tue, 01 Nov 2011 02:30:49 -0500
parents b4de894a1e2e
children
rev   line source
rlm@2 1 (ns clojure-magick.magick)
rlm@2 2 (import [org.im4java.core ConvertCmd IMOperation Stream2BufferedImage] )
rlm@2 3
rlm@2 4 (defn read-image "Read the image in the specified file; returns a BufferedImage." [filename]
rlm@2 5 (let [op (doto (IMOperation.)
rlm@2 6 (.addImage (into-array String [filename]))
rlm@2 7 (.addImage (into-array String ["-"])))
rlm@2 8 s2b (Stream2BufferedImage.)]
rlm@2 9 (doto (ConvertCmd.)
rlm@2 10 (.setOutputConsumer s2b)
rlm@2 11 (.run op (into-array Object [])))
rlm@2 12 (.getImage s2b))
rlm@2 13 )
rlm@2 14
rlm@2 15 (defn write-image "Write the image to the specified file." [img filename]
rlm@2 16 (let [op (doto (IMOperation.)
rlm@2 17 (.addImage)
rlm@2 18 (.addImage))]
rlm@2 19 (.run (ConvertCmd.) op (into-array Object [img filename])))
rlm@2 20 )
rlm@2 21
rlm@2 22
rlm@2 23 (defn glue-vertical "Concatenate images in a sequence from top to bottom, producing a new image." [image & imgs]
rlm@2 24 (let [imgs (cons image imgs)
rlm@2 25 op (doto (IMOperation.)
rlm@2 26 (.addImage (count imgs))
rlm@2 27 (.append)
rlm@2 28 (.addImage (into-array String ["-"])))
rlm@2 29 s2b (Stream2BufferedImage.)]
rlm@2 30 (doto (ConvertCmd.)
rlm@2 31 (.setOutputConsumer s2b)
rlm@2 32 (.run op (into-array Object (vec imgs))))
rlm@2 33 (.getImage s2b)
rlm@2 34 ))
rlm@2 35
rlm@2 36 (defn glue-horizontal "Concatenate images in a sequence from left to right, producing a new image." [image & imgs]
rlm@2 37 (let [imgs (cons image imgs)
rlm@2 38 op (doto (IMOperation.)
rlm@2 39 (.addImage (count imgs))
rlm@2 40 (.p_append)
rlm@2 41 (.addImage (into-array String ["-"])))
rlm@2 42 s2b (Stream2BufferedImage.)]
rlm@2 43 (doto (ConvertCmd.)
rlm@2 44 (.setOutputConsumer s2b)
rlm@2 45 (.run op (into-array Object (vec imgs))))
rlm@2 46 (.getImage s2b)
rlm@2 47 ))
rlm@2 48
rlm@2 49
rlm@2 50
rlm@2 51 (defn resize "Resize the image, keeping the same aspect ratio."
rlm@2 52 ([img width height]
rlm@2 53 (let [op (doto (IMOperation.)
rlm@2 54 (.addImage)
rlm@2 55 (.resize width height)
rlm@2 56 (.addImage (into-array String ["-"])))
rlm@2 57 s2b (Stream2BufferedImage.)]
rlm@2 58 (doto (ConvertCmd.)
rlm@2 59 (.setOutputConsumer s2b)
rlm@2 60 (.run op (into-array Object [img])))
rlm@2 61 (.getImage s2b)
rlm@2 62 ))
rlm@2 63 (
rlm@2 64 [img width]
rlm@2 65 (resize img width nil)
rlm@2 66 )
rlm@2 67 )
rlm@2 68
rlm@2 69
rlm@2 70 (defn upside-down "Flip the image upside down."
rlm@2 71 [img]
rlm@2 72 (let [op (doto (IMOperation.)
rlm@2 73 (.addImage)
rlm@2 74 (.flip)
rlm@2 75 (.addImage (into-array String ["-"])))
rlm@2 76 s2b (Stream2BufferedImage.)]
rlm@2 77 (doto (ConvertCmd.)
rlm@2 78 (.setOutputConsumer s2b)
rlm@2 79 (.run op (into-array Object [img])))
rlm@2 80 (.getImage s2b)
rlm@2 81 )
rlm@2 82 )
rlm@2 83
rlm@2 84
rlm@2 85 (defn scale-rotate-translate "Scale the image, then rotate the image about the axis coordinates, then translate the image by the given amount. Does not change the dimensions of the picture."
rlm@2 86 ([img axis-x axis-y scale-x scale-y degrees translate-x translate-y]
rlm@2 87 (let [arg-str (apply str (list axis-x "," axis-y " " scale-x "," scale-y " " degrees " " translate-x "," translate-y))
rlm@2 88 op (doto (IMOperation.)
rlm@2 89 (.addImage)
rlm@2 90 (.distort "ScaleRotateTranslate" arg-str)
rlm@2 91 (.addImage (into-array String ["-"])))
rlm@2 92 s2b (Stream2BufferedImage.)]
rlm@2 93 (doto (ConvertCmd.)
rlm@2 94 (.setOutputConsumer s2b)
rlm@2 95 (.run op (into-array Object [img])))
rlm@2 96 (.getImage s2b)))
rlm@2 97 ([img axis-x axis-y scale degrees translate-x translate-y]
rlm@2 98 (recur axis-x axis-y scale scale degrees translate-x translate-y))
rlm@2 99 )
rlm@2 100
rlm@2 101
rlm@2 102 (defn rotate "Rotate the image clockwise by the given amount." [img degrees]
rlm@2 103 (let [op (doto (IMOperation.)
rlm@2 104 (.addImage)
rlm@2 105 (.distort "ScaleRotateTranslate" (str degrees))
rlm@2 106 (.addImage (into-array String ["-"])))
rlm@2 107 s2b (Stream2BufferedImage.)]
rlm@2 108 (doto (ConvertCmd.)
rlm@2 109 (.setOutputConsumer s2b)
rlm@2 110 (.run op (into-array Object [img])))
rlm@2 111 (.getImage s2b))
rlm@2 112 )
rlm@2 113
rlm@2 114
rlm@2 115
rlm@2 116
rlm@2 117
rlm@2 118
rlm@2 119
rlm@2 120
rlm@2 121
rlm@2 122
rlm@2 123
rlm@2 124
rlm@2 125
rlm@2 126
rlm@2 127
rlm@2 128
rlm@2 129
rlm@2 130
rlm@2 131
rlm@2 132
rlm@2 133
rlm@2 134
rlm@2 135
rlm@2 136
rlm@2 137
rlm@2 138
rlm@2 139 (defn resize[]
rlm@2 140 (let [op (doto (IMOperation.)
rlm@2 141 (.addImage (into-array String ["/home/r/jiggly.gif"]))
rlm@2 142 (.resize 800 600)
rlm@2 143 (.addImage (into-array String ["/home/r/jiggly_sm.gif"]))
rlm@2 144 )]
rlm@2 145 (.run (ConvertCmd.) op (into-array Object []))))
rlm@2 146
rlm@2 147
rlm@2 148 (defn run-test[]
rlm@2 149 (let [puff (read-image "/home/r/jiggly.gif")]
rlm@2 150 (write-image (glue-horizontal puff puff puff) "/home/r/multijiggly.gif")
rlm@2 151 ))