Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for Scalr options #37

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[net.mikera/randomz "0.3.0"]
[com.jhlabs/filters "2.0.235-1"]]

:repositories {"clojars.org" "http://clojars.org/repo"}
:repositories {"clojars.org" "https://clojars.org/repo"}
:parent [net.mikera/clojure-pom "0.4.0"]
:source-paths ["src/main/clojure"]
:test-paths ["src/test/clojure" "src/test/java"]
Expand Down
39 changes: 29 additions & 10 deletions src/main/clojure/mikera/image/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,33 @@
nil)
dst)))

;; Used to define different scalling hints
;; https://github.com/rkalla/imgscalr/blob/921c26ca273e2d04f36d09e7300e7f21aa165b4b/src/main/java/org/imgscalr/Scalr.java#L356
(def scale-methods
{:automatic org.imgscalr.Scalr$Method/AUTOMATIC
:speed org.imgscalr.Scalr$Method/SPEED
:balanced org.imgscalr.Scalr$Method/BALANCED
:quality org.imgscalr.Scalr$Method/QUALITY
:ultra-quality org.imgscalr.Scalr$Method/ULTRA_QUALITY})

;; Used to define different modes of resizing
;; https://github.com/rkalla/imgscalr/blob/921c26ca273e2d04f36d09e7300e7f21aa165b4b/src/main/java/org/imgscalr/Scalr.java#L418
(def scale-modes
{:automatic org.imgscalr.Scalr$Mode/AUTOMATIC
:fit-exact org.imgscalr.Scalr$Mode/FIT_EXACT
:fit-to-width org.imgscalr.Scalr$Mode/FIT_TO_WIDTH
:fit-to-height org.imgscalr.Scalr$Mode/FIT_TO_HEIGHT})

(defn resize
"Resizes an image to the specified width and height. If height is omitted,
maintains the aspect ratio."
(^java.awt.image.BufferedImage [^BufferedImage image new-width new-height]
(^java.awt.image.BufferedImage [^BufferedImage image new-width new-height {:keys [method mode]}]
(Scalr/resize image
org.imgscalr.Scalr$Method/BALANCED
org.imgscalr.Scalr$Mode/FIT_EXACT
(get scale-methods method org.imgscalr.Scalr$Method/BALANCED)
(get scale-modes mode org.imgscalr.Scalr$Mode/FIT_EXACT)
(int new-width) (int new-height) nil))
(^java.awt.image.BufferedImage [^BufferedImage image new-width]
(resize image new-width (/ (* (long new-width) (.getHeight image)) (.getWidth image)))))
(^java.awt.image.BufferedImage [^BufferedImage image new-width options]
(resize image new-width (/ (* (long new-width) (.getHeight image)) (.getWidth image)) options)))

(defn scale-image
"DEPRECATED: use 'resize' instead"
Expand All @@ -87,10 +104,10 @@

(defn scale
"Scales an image by a given factor or ratio."
(^java.awt.image.BufferedImage [^BufferedImage image factor]
(resize image (* (.getWidth image) (double factor)) (* (.getHeight image) (double factor))))
(^java.awt.image.BufferedImage [^BufferedImage image width-factor height-factor]
(resize image (* (.getWidth image) (double width-factor)) (* (.getHeight image) (double height-factor)))))
(^java.awt.image.BufferedImage [^BufferedImage image factor options]
(resize image (* (.getWidth image) (double factor)) (* (.getHeight image) (double factor)) options))
(^java.awt.image.BufferedImage [^BufferedImage image width-factor height-factor options]
(resize image (* (.getWidth image) (double width-factor)) (* (.getHeight image) (double height-factor)) options)))

(defn ensure-default-image-type
"If the provided image is does not have the default image type
Expand Down Expand Up @@ -122,8 +139,10 @@

(defn zoom
"Zooms into (scales) an image with a given scale factor."
(^java.awt.image.BufferedImage [^BufferedImage image factor options]
(scale image factor options))
(^java.awt.image.BufferedImage [^BufferedImage image factor]
(scale image factor)))
(scale image factor {})))

(defn flip
"Flips an image in the specified direction :horizontal or :vertical"
Expand Down
7 changes: 5 additions & 2 deletions src/test/clojure/mikera/image/demo.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@
;; demo of visualising a colour gradient
(show (gradient-image wheel))

(save ant "/path/to/new-ant.png" :quality 0.9 :progressive true)
#_(save ant "/path/to/new-ant.png" :quality 0.9 :progressive true)

;; change pixels on an image, using colour components
(let [temp (copy ant)]
(dotimes [x 256]
(with-components [[r g b] (get-pixel ant x 20)]
(set-pixel temp x 20 (rgb-from-components b x r))))
(show temp))
(show temp))

;; resize an image with options
(show (resize ant 250 {:method :ultra-quality}))
)
6 changes: 3 additions & 3 deletions src/test/clojure/mikera/image/test_core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
(let [img (copy TEST-IMAGE 2.0)]
(is (= 4 (width img)))
(is (= 4 (height img)))
(is (= col/green) (get-pixel img 2 2))
(is (= col/clear) (get-pixel img 1 1))))
(is (= col/green (get-pixel img 2 2)))
(is (= col/clear (get-pixel img 1 1)))))

(deftest test-scale-image
(let [^BufferedImage bi (new-image 10 10)
Expand All @@ -57,7 +57,7 @@

(deftest test-scale
(let [^BufferedImage bi (new-image 10 10)
bi (scale bi 2.0 3.0)]
bi (scale bi 2.0 3.0 {})]
(is (instance? BufferedImage bi))
(is (== 20 (.getWidth bi)))
(is (== 30 (.getHeight bi)))))
Expand Down