Skip to content

Instantly share code, notes, and snippets.

@bryant
Last active December 27, 2015 09:39
Show Gist options
  • Save bryant/7305681 to your computer and use it in GitHub Desktop.
Save bryant/7305681 to your computer and use it in GitHub Desktop.
converts raw grayscale values into pgm format
import Data.List (intercalate)
import System.Environment (getArgs)
main = do
raw <- readFile =<< head `fmap` getArgs
putStrLn $ "P2 160 120 256\n" ++ rawToPGM raw
where
chunk width [] = []
chunk width xs = a : chunk width b
where (a, b) = splitAt width xs
rawToPGM = unlines . joinCols . (chunk 160) . decimaled
decimaled = map (show . (read :: String -> Int)) . lines
joinCols = map $ intercalate " "
@glguy
Copy link

glguy commented Nov 4, 2013

import Data.List (intercalate)
import System.Environment (getArgs)

main = do
    raw <- readFile . head =<< getArgs
    putStr ("P2 160 120 256\n" ++ rawToString raw)
    where
        rawToString = joinedRows . joinedCols . pixarray . decimaled
        decimaled   = hexToDec . lines
        pixarray    = chunk 160
        joinedCols  = map (intercalate " ")
        joinedRows  = unlines

hexToDec = map (show . (read :: String -> Int))

chunk width [] = []
chunk width xs = a : chunk width b
  where
  (a,b) = splitAt width xs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment