Last active
July 3, 2020 14:08
-
-
Save dminuoso/ff8925b1872ee60c107d97f15de2c759 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
chunksOf :: Int -> [a] -> [[a]] | |
chunksOf _ [] = [] | |
chunksOf n l = take n l : chunksOf n (drop n l) | |
type Pixel = [Int] | |
serialize :: NP.PPM -> String | |
serialize (NP.PPM (NP.PPMHeader t w h) img) | |
= unlines ( [show (maximum rgbList) | |
, show h | |
, show w | |
, "P3"] ++ pixelData) | |
where | |
rgbList :: [Int] | |
rgbList = NP.pixelDataToIntList img | |
pixels :: [Pixel] | |
pixels = chunksOf 3 rgbList | |
pixelToAscii :: [Int] -> String | |
pixelToAscii = unwords . fmap show | |
rows :: [[Pixel]] | |
rows = chunksOf 10 pixels | |
serializeRow :: [Pixel] -> String | |
serializeRow = unwords . fmap pixelToAscii | |
pixelData :: [String] | |
pixelData = serializeRow <$> rows |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment