Skip to content

Instantly share code, notes, and snippets.

@dminuoso
Last active July 3, 2020 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dminuoso/ff8925b1872ee60c107d97f15de2c759 to your computer and use it in GitHub Desktop.
Save dminuoso/ff8925b1872ee60c107d97f15de2c759 to your computer and use it in GitHub Desktop.
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