Categories: Uncategorized

[Haskell]Input PGM file part1

Target

I could output PGM file with Haskell.
So I want to make a program which read PGM file.

Program

At first only input header and output it.
import System.IO
import System.Environment (getArgs)

main = do
    args <- getArgs
    if length args == 0 then do
        print "usage : ./pgm-input test.pgm"
    else do
        infile <- openFile (head args) ReadMode

        -- Read PGM Header
        pgm_type <- hGetLine infile
        pgm_comment <- hGetLine infile
        pgm_max_brightness <- hGetLine infile
        pgm_size <- hGetLine infile
        hClose infile

        -- Output PGM File
        putStrLn pgm_type
        putStrLn pgm_comment
        putStrLn pgm_max_brightness
        putStrLn pgm_size
Run.
$ ./pgm-input test.pgm
P5
# PGM type grayscale image
255 255
255
zuqqhi2