Mercurial > dylan
diff clojure_magick/magick.clj @ 2:b4de894a1e2e
initial import
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 28 Oct 2011 00:03:05 -0700 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/clojure_magick/magick.clj Fri Oct 28 00:03:05 2011 -0700 1.3 @@ -0,0 +1,151 @@ 1.4 +(ns clojure-magick.magick) 1.5 +(import [org.im4java.core ConvertCmd IMOperation Stream2BufferedImage] ) 1.6 + 1.7 +(defn read-image "Read the image in the specified file; returns a BufferedImage." [filename] 1.8 + (let [op (doto (IMOperation.) 1.9 + (.addImage (into-array String [filename])) 1.10 + (.addImage (into-array String ["-"]))) 1.11 + s2b (Stream2BufferedImage.)] 1.12 + (doto (ConvertCmd.) 1.13 + (.setOutputConsumer s2b) 1.14 + (.run op (into-array Object []))) 1.15 + (.getImage s2b)) 1.16 +) 1.17 + 1.18 +(defn write-image "Write the image to the specified file." [img filename] 1.19 + (let [op (doto (IMOperation.) 1.20 + (.addImage) 1.21 + (.addImage))] 1.22 + (.run (ConvertCmd.) op (into-array Object [img filename]))) 1.23 +) 1.24 + 1.25 + 1.26 +(defn glue-vertical "Concatenate images in a sequence from top to bottom, producing a new image." [image & imgs] 1.27 + (let [imgs (cons image imgs) 1.28 + op (doto (IMOperation.) 1.29 + (.addImage (count imgs)) 1.30 + (.append) 1.31 + (.addImage (into-array String ["-"]))) 1.32 + s2b (Stream2BufferedImage.)] 1.33 + (doto (ConvertCmd.) 1.34 + (.setOutputConsumer s2b) 1.35 + (.run op (into-array Object (vec imgs)))) 1.36 + (.getImage s2b) 1.37 +)) 1.38 + 1.39 +(defn glue-horizontal "Concatenate images in a sequence from left to right, producing a new image." [image & imgs] 1.40 + (let [imgs (cons image imgs) 1.41 + op (doto (IMOperation.) 1.42 + (.addImage (count imgs)) 1.43 + (.p_append) 1.44 + (.addImage (into-array String ["-"]))) 1.45 + s2b (Stream2BufferedImage.)] 1.46 + (doto (ConvertCmd.) 1.47 + (.setOutputConsumer s2b) 1.48 + (.run op (into-array Object (vec imgs)))) 1.49 + (.getImage s2b) 1.50 + )) 1.51 + 1.52 + 1.53 + 1.54 +(defn resize "Resize the image, keeping the same aspect ratio." 1.55 + ([img width height] 1.56 + (let [op (doto (IMOperation.) 1.57 + (.addImage) 1.58 + (.resize width height) 1.59 + (.addImage (into-array String ["-"]))) 1.60 + s2b (Stream2BufferedImage.)] 1.61 + (doto (ConvertCmd.) 1.62 + (.setOutputConsumer s2b) 1.63 + (.run op (into-array Object [img]))) 1.64 + (.getImage s2b) 1.65 + )) 1.66 + ( 1.67 + [img width] 1.68 + (resize img width nil) 1.69 + ) 1.70 +) 1.71 + 1.72 + 1.73 +(defn upside-down "Flip the image upside down." 1.74 + [img] 1.75 + (let [op (doto (IMOperation.) 1.76 + (.addImage) 1.77 + (.flip) 1.78 + (.addImage (into-array String ["-"]))) 1.79 + s2b (Stream2BufferedImage.)] 1.80 + (doto (ConvertCmd.) 1.81 + (.setOutputConsumer s2b) 1.82 + (.run op (into-array Object [img]))) 1.83 + (.getImage s2b) 1.84 + ) 1.85 +) 1.86 + 1.87 + 1.88 +(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." 1.89 + ([img axis-x axis-y scale-x scale-y degrees translate-x translate-y] 1.90 + (let [arg-str (apply str (list axis-x "," axis-y " " scale-x "," scale-y " " degrees " " translate-x "," translate-y)) 1.91 + op (doto (IMOperation.) 1.92 + (.addImage) 1.93 + (.distort "ScaleRotateTranslate" arg-str) 1.94 + (.addImage (into-array String ["-"]))) 1.95 + s2b (Stream2BufferedImage.)] 1.96 + (doto (ConvertCmd.) 1.97 + (.setOutputConsumer s2b) 1.98 + (.run op (into-array Object [img]))) 1.99 + (.getImage s2b))) 1.100 + ([img axis-x axis-y scale degrees translate-x translate-y] 1.101 + (recur axis-x axis-y scale scale degrees translate-x translate-y)) 1.102 +) 1.103 + 1.104 + 1.105 +(defn rotate "Rotate the image clockwise by the given amount." [img degrees] 1.106 + (let [op (doto (IMOperation.) 1.107 + (.addImage) 1.108 + (.distort "ScaleRotateTranslate" (str degrees)) 1.109 + (.addImage (into-array String ["-"]))) 1.110 + s2b (Stream2BufferedImage.)] 1.111 + (doto (ConvertCmd.) 1.112 + (.setOutputConsumer s2b) 1.113 + (.run op (into-array Object [img]))) 1.114 + (.getImage s2b)) 1.115 +) 1.116 + 1.117 + 1.118 + 1.119 + 1.120 + 1.121 + 1.122 + 1.123 + 1.124 + 1.125 + 1.126 + 1.127 + 1.128 + 1.129 + 1.130 + 1.131 + 1.132 + 1.133 + 1.134 + 1.135 + 1.136 + 1.137 + 1.138 + 1.139 + 1.140 + 1.141 + 1.142 +(defn resize[] 1.143 + (let [op (doto (IMOperation.) 1.144 + (.addImage (into-array String ["/home/r/jiggly.gif"])) 1.145 + (.resize 800 600) 1.146 + (.addImage (into-array String ["/home/r/jiggly_sm.gif"])) 1.147 + )] 1.148 + (.run (ConvertCmd.) op (into-array Object [])))) 1.149 + 1.150 + 1.151 +(defn run-test[] 1.152 + (let [puff (read-image "/home/r/jiggly.gif")] 1.153 + (write-image (glue-horizontal puff puff puff) "/home/r/multijiggly.gif") 1.154 + ))