now in technicolor!

This commit is contained in:
Nicole Dresselhaus 2015-11-21 20:26:21 +01:00
parent d1a7826472
commit aa26b51693
3 changed files with 29 additions and 21 deletions

View File

@ -5,11 +5,13 @@ import Codec.Picture.Types
import Data.Maybe (fromJust) import Data.Maybe (fromJust)
import Data.Word (Word8) import Data.Word (Word8)
import Data.List (transpose) import Data.List (transpose)
import Text.Printf (printf)
import Control.Arrow ((&&&))
targetWidth :: Int targetWidth :: Int
targetWidth = 80 targetWidth = 80
targetHeight :: Int targetHeight :: Int
targetHeight = 50 targetHeight = 40
main :: IO () main :: IO ()
main = do main = do
@ -23,41 +25,44 @@ main = do
pix <- return $ pixelize s targetWidth targetHeight pix <- return $ pixelize s targetWidth targetHeight
case pix of case pix of
Nothing -> return () Nothing -> return ()
Just c -> do Just (f,b) -> do
savePngImage "test.png" (ImageRGB8 c) savePngImage "test.png" (ImageRGB8 b)
str <- return $ img2ascii conv c str <- return $ img2ascii conv (f,b)
mapM_ putStrLn (transpose $ chunksof targetHeight str) mapM_ (\x -> putStr x >> putStrLn "\x1b[0m") (concat <$> str)
Nothing -> return () Nothing -> return ()
chunksof :: Int -> [a] -> [[a]] chunksof :: Int -> [a] -> [[a]]
chunksof _ [] = [] chunksof _ [] = []
chunksof c xs = take c xs : chunksof c (drop c xs) chunksof c xs = take c xs : chunksof c (drop c xs)
conv :: Word8 -> Char conv :: (PixelRGB8,PixelRGB8) -> String
conv x conv (fp@(PixelRGB8 fr fg fb),PixelRGB8 br bg bb) = printf "\x1b[48;2;%d;%d;%dm\x1b[38;2;%d;%d;%dm%c" br bg bb fr fg fb (lumi.computeLuma $ fp)
| x > 225 = '@' where
| x > 180 = 'O' lumi :: Word8 -> Char
| x > 150 = 'o' lumi x
| x > 50 = '.' | x > 225 = '@'
| otherwise = ' ' | x > 180 = 'O'
| x > 150 = 'X'
| x > 50 = 'o'
| x > 25 = 'x'
| x > 10 = '.'
| otherwise = ' '
img2ascii :: (Word8 -> Char) -> Image PixelRGB8 -> String img2ascii :: ((PixelRGB8,PixelRGB8) -> String) -> (Image PixelRGB8,Image PixelRGB8) -> [[String]]
img2ascii c im@(Image w h _) = do img2ascii c (fg@(Image w h _),bg@(Image w' h' _)) = (fmap.fmap) (c.(uncurry (pixelAt fg) &&& uncurry (pixelAt bg))) [[(x,y) | x <- [0..w-1]] | y <- [0..h-1]]
x <- [0..w-1]
y <- [0..h-1]
return $ c (255-computeLuma (pixelAt im x y))
pixelize :: Image PixelRGB8 -> Int -> Int -> Maybe (Image PixelRGB8) pixelize :: Image PixelRGB8 -> Int -> Int -> Maybe (Image PixelRGB8,Image PixelRGB8)
pixelize im@(Image iw ih id) tw th = pixelize im@(Image iw ih id) tw th =
if windoww == 0 || windowh == 0 then if windoww == 0 || windowh == 0 then
Nothing Nothing
else Just $ snd $ generateFoldImage (folder windoww windowh) im tw th else Just $ (snd $ generateFoldImage (folder filterfun windoww windowh) im tw th,
snd $ generateFoldImage (folder filterfuninv windoww windowh) im tw th)
where where
windoww = iw `div` tw windoww = iw `div` tw
windowh = ih `div` th windowh = ih `div` th
folder :: Int -> Int -> Image PixelRGB8 -> Int -> Int -> (Image PixelRGB8, PixelRGB8) folder :: ((PixelRGB8, Int, Int) -> (PixelRGB8, Int, Int) -> (PixelRGB8, Int, Int)) -> Int -> Int -> Image PixelRGB8 -> Int -> Int -> (Image PixelRGB8, PixelRGB8)
folder ww wh im@(Image iw ih id) x y = (im,(\(a,_,_) -> a) $ foldl1 filterfun folder f ww wh im@(Image iw ih id) x y = (im,(\(a,_,_) -> a) $ foldl1 f
[ (pixelAt im (x'+dx) (y'+dy),dx,dy) [ (pixelAt im (x'+dx) (y'+dy),dx,dy)
| dx <- [-(ww `div` 2)..ww - (ww `div`2)] | dx <- [-(ww `div` 2)..ww - (ww `div`2)]
, dy <- [-(ww `div` 2)..ww - (ww `div`2)] , dy <- [-(ww `div` 2)..ww - (ww `div`2)]
@ -71,6 +76,9 @@ folder ww wh im@(Image iw ih id) x y = (im,(\(a,_,_) -> a) $ foldl1 filterfun
filterfun :: (PixelRGB8,Int,Int) -> (PixelRGB8, Int, Int) -> (PixelRGB8,Int,Int) filterfun :: (PixelRGB8,Int,Int) -> (PixelRGB8, Int, Int) -> (PixelRGB8,Int,Int)
filterfun (x@(PixelRGB8 r g b),_,_) (y@(PixelRGB8 r' g' b'),_,_) = if computeLuma x > computeLuma y then (x,0,0) else (y,0,0) filterfun (x@(PixelRGB8 r g b),_,_) (y@(PixelRGB8 r' g' b'),_,_) = if computeLuma x > computeLuma y then (x,0,0) else (y,0,0)
filterfuninv :: (PixelRGB8,Int,Int) -> (PixelRGB8, Int, Int) -> (PixelRGB8,Int,Int)
filterfuninv (x@(PixelRGB8 r g b),_,_) (y@(PixelRGB8 r' g' b'),_,_) = if computeLuma x < computeLuma y then (x,0,0) else (y,0,0)
extractDynImage :: DynamicImage -> Maybe (Image PixelRGB8) extractDynImage :: DynamicImage -> Maybe (Image PixelRGB8)
extractDynImage image = extractDynImage image =
case image of case image of

BIN
test.jpg

Binary file not shown.

Before

Width:  |  Height:  |  Size: 47 KiB

After

Width:  |  Height:  |  Size: 134 KiB

BIN
test.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.4 KiB

After

Width:  |  Height:  |  Size: 8.8 KiB