Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- file: ch10/PNM.hs
  2. matchHeader :: L.ByteString -> L.ByteString -> Maybe L.ByteString
  3.  
  4. -- "nat" here is short for "natural number"
  5. getNat :: L.ByteString -> Maybe (Int, L.ByteString)
  6.  
  7. getBytes :: Int -> L.ByteString
  8.          -> Maybe (L.ByteString, L.ByteString)
  9.  
  10. parseP5 s =
  11.   case matchHeader (L8.pack "P5") s of
  12.     Nothing -> Nothing
  13.     Just s1 ->
  14.       case getNat s1 of
  15.         Nothing -> Nothing
  16.         Just (width, s2) ->
  17.           case getNat (L8.dropWhile isSpace s2) of
  18.             Nothing -> Nothing
  19.             Just (height, s3) ->
  20.               case getNat (L8.dropWhile isSpace s3) of
  21.                 Nothing -> Nothing
  22.                 Just (maxGrey, s4)
  23.                   | maxGrey > 255 -> Nothing
  24.                   | otherwise ->
  25.                       case getBytes 1 s4 of
  26.                         Nothing -> Nothing
  27.                         Just (_, s5) ->
  28.                           case getBytes (width * height) s5 of
  29.                             Nothing -> Nothing
  30.                             Just (bitmap, s6) ->
  31.                               Just (Greymap width height maxGrey bitmap, s6)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement