avatar-generator (empty) → 0.1.0.0
raw patch · 6 files changed
+159/−0 lines, 6 filesdep +JuicyPixelsdep +basedep +randomsetup-changed
Dependencies added: JuicyPixels, base, random
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- avatar-generator.cabal +34/−0
- src/AvatarGenerator.hs +30/−0
- src/ImageManipulation.hs +43/−0
- src/Main.hs +20/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Keera Studios Ltd++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 Ivan Perez nor the names of other+ 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+OWNER 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ avatar-generator.cabal view
@@ -0,0 +1,34 @@+-- Initial avatar-generator.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/++name: avatar-generator+version: 0.1.0.0+synopsis: A simple 5x5 random avatar icon generator+description: A straightforward avatar image generator that produces 512x512+ random images.+homepage: http://github.com/keera-studios/avatar-generator+license: BSD3+license-file: LICENSE+author: Ivan Perez+maintainer: ivan.perez@keera.co.uk+copyright: Keera Studios Ltd+category: Graphics+build-type: Simple+-- extra-source-files: +cabal-version: >=1.10++executable avatar-generator+ main-is: Main.hs+ other-modules: AvatarGenerator ImageManipulation+ build-depends: base >=4.6 && <4.7, random >=1.1 && <1.2, JuicyPixels >=3.2 && <3.3+ hs-source-dirs: src+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/keera-studios/avatar-generator++source-repository this+ type: git+ location: https://github.com/keera-studios/avatar-generator+ tag: v0.1.0.0
+ src/AvatarGenerator.hs view
@@ -0,0 +1,30 @@+module AvatarGenerator where++-- JuicyPixel+import Data.Word+import Codec.Picture++-- To transform images+import ImageManipulation++avatarGenerator :: [Bool] -> (Word8, Word8, Word8) -> String -> IO ()+avatarGenerator vals (r,g,b) path = writePng path $+ generateImage bigImage desiredWidth desiredHeight++ where bigImage :: Img PixelRGBA8+ bigImage = scale desiredWidth desiredHeight width height smallImage emptyRGBA8++ smallImage :: Img PixelRGBA8+ smallImage = imageFilter color $ crop width height False avatar+ where color True = PixelRGBA8 r g b 255+ color False = emptyRGBA8++ avatar :: Img Bool+ avatar = hMirror width lhsAvatar+ where lhsAvatar x y = vals !! ((y * width) + x) ++ width = 5+ height = 5++ desiredWidth = 512+ desiredHeight = 512
+ src/ImageManipulation.hs view
@@ -0,0 +1,43 @@+module ImageManipulation where++-- To manipulate images using JuicyPixel+import Data.Word+import Codec.Picture++type Img a = Int -> Int -> a++emptyRGBA8 :: PixelRGBA8+emptyRGBA8 = PixelRGBA8 0 0 0 0++imageFilter :: (a -> b) -> Img a -> Img b+imageFilter filter img = \x y -> filter (img x y)++crop :: Int -> Int -> a -> Img a -> Img a+crop w h d f = \x y -> if x >= w || y >= h then d else f x y++scale :: Int -> Int -> Int -> Int -> Img a -> a -> Img a+scale dw dh w h img bg = move (marginLeft, marginTop) (zoom zhf zvf img) bg+ where marginLeft = (dw - (zhf * w)) `div` 2+ marginTop = (dh - (zvf * h)) `div` 2+ zhf = dw `div` w+ zvf = dh `div` h++move :: (Int, Int) -> Img a -> a -> Img a+move (dx,dy) img bg x y+ | x < dx || y < dy = bg+ | otherwise = img (x - dx) (y - dy)++zoom :: Int -> Int -> Img a -> Img a+zoom zh zv f = \x y -> f (x `div` zh) (y `div` zv)++hflip :: Int -> Img a -> Img a+hflip w f = \x y -> f (w - 1 - x) y++hNext :: Int -> Img a -> Img a -> Img a+hNext w f g = \x y -> if x < w then f x y else g (x - w) y++hMirror :: Int -> Img a -> Img a+hMirror w orig = hNext (midColumn w) orig flipped+ where flipped = hflip (halfWidth w) orig+ midColumn w = if odd w then (w `div` 2) + 1 else (w `div` 2)+ halfWidth w = w `div` 2
+ src/Main.hs view
@@ -0,0 +1,20 @@+-- To create a program that cna be invoked from CLI+import System.Environment+import System.Random+import System.IO+import System.Exit++-- Avatar generator+import AvatarGenerator++main :: IO ()+main = do+ args <- getArgs+ case args of+ [f] -> do gen <- getStdGen+ let list = randomRs (False, True) gen+ (r:g:b:_) = randomRs (0, 255) gen+ avatarGenerator list (r,g,b) f++ _ -> do hPutStrLn stderr "usage: generate-avatar <output>"+ exitWith (ExitFailure 1)