;;Copyright (c) 2009, Jason Lee Hurt ;;All rights reserved. ;; ;;Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: ;; ;; * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. ;; * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. ;; * Neither the name of the jlhdevelopment.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. ;; ;;THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. (import '(javax.swing JFrame JPanel JButton JFileChooser GrayFilter) '(javax.swing.filechooser FileFilter) '(java.awt.event ActionListener) '(java.awt.image BufferedImage ImageObserver FilteredImageSource) '(java.io File) '(javax.imageio ImageIO) '(java.awt Graphics)) (def m_image (ref nil)) (def imageFrame (new JFrame)) (def imagePanel (proxy [JPanel] [] (paintComponent [g] (proxy-super paintComponent g) (. g (drawImage (deref m_image) 0 0 nil))))) (defn openImage [imageFile] (let [bufferedImage (. ImageIO (read imageFile))] (dosync (ref-set m_image bufferedImage)) (doto imageFrame (. setVisible false) (. setSize (. bufferedImage (getWidth)) (. bufferedImage (getHeight))) (. add imagePanel) (. setVisible true)))) (defn grayscaleImage [bufferedImage] (let [grayFilter (new GrayFilter false 30) filteredImageSource (new FilteredImageSource (. bufferedImage (getSource)) grayFilter)] (. imageFrame (createImage filteredImageSource)))) (def imageFileFilter (proxy [FileFilter] [] (accept [f] (if (and (not (nil? f)) (or (. f (isDirectory)) (or (.. f (getName) (endsWith "gif")) (.. f (getName) (endsWith "png"))))) true false)) (getDescription [] "Image Files"))) (def openButton (doto (new JButton "Open Image") (.addActionListener (let [fileChooser (doto (new JFileChooser) (.setFileFilter imageFileFilter))] (proxy [ActionListener] [] (actionPerformed [e] (if (= (JFileChooser/APPROVE_OPTION) (. fileChooser (showOpenDialog openButton))) (openImage (. fileChooser (getSelectedFile)))))))))) (def grayscaleButton (doto (new JButton "Grayscale") (.addActionListener (proxy [ActionListener] [] (actionPerformed [e] (dosync (ref-set m_image (grayscaleImage (deref m_image)))) (. imageFrame (repaint)) (. imageFrame (requestFocus))))))) (def buttonPanel (doto (new JPanel) (.add openButton) (.add grayscaleButton))) (def mainFrame (doto (new JFrame "Grayscale Converter") (.setDefaultCloseOperation (JFrame/EXIT_ON_CLOSE)) (.add buttonPanel) (.setSize 250 70) (.setVisible true)))