Advertisement
jckuri

ReadImageFile.hs

Jun 16th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ReadImageFile.hs
  2.  
  3. import qualified Codec.Picture as Picture
  4. import qualified Data.Vector.Storable as Vector
  5.  
  6. intToDouble i = fromIntegral i::Double
  7.  
  8. printImageInfo imageType image =
  9.  putStrLn ("Length: " ++ (show imgLength)) >>
  10.  putStrLn ("Numbers per pixel: " ++ (show numbersPerPixel)) >>
  11.  putStrLn ("Minimum value: " ++ (show $ Vector.minimum imgData)) >>
  12.  putStrLn ("Maximum value: " ++ (show $ Vector.maximum imgData)) >>
  13.  putStrLn ("Image type: " ++ imageType) >>
  14.  putStrLn ("Dimensions: (" ++ (show width) ++ "," ++ (show height) ++ ")")
  15.  where
  16.   imgData = Picture.imageData image
  17.   imgLength = Vector.length imgData
  18.   width = Picture.imageWidth image
  19.   height = Picture.imageHeight image
  20.   numbersPerPixel = (intToDouble imgLength) / (intToDouble width) / (intToDouble height)
  21.  
  22. processImage img =
  23.  case img of
  24.   Picture.ImageY8 i -> printImageInfo "ImageY8" i
  25.   Picture.ImageYF i -> printImageInfo "ImageYF" i
  26.   Picture.ImageYA8 i -> printImageInfo "ImageYA8" i
  27.   Picture.ImageRGB8 i -> printImageInfo "ImageRGB8" i
  28.   Picture.ImageRGBF i -> printImageInfo "ImageRGBF" i
  29.   Picture.ImageRGBA8 i -> printImageInfo "ImageRGBA8" i
  30.   Picture.ImageYCbCr8 i -> printImageInfo "ImageYCbCr8" i
  31.  
  32. readImageFile imageFile =
  33.  putStrLn ("\nReading image: " ++ imageFile) >>
  34.  Picture.readImage imageFile >>= \result ->
  35.  case result of
  36.   Left error -> putStrLn ("ERROR: " ++ show error)
  37.   Right image -> processImage image
  38.  
  39. main =
  40.  readImageFile "Halflife-Haskell.jpg" >>
  41.  readImageFile "development-haskell.png"
  42.  
  43. -- Download images from:
  44. -- http://images.wikia.com/uncyclopedia/images/f/fb/Halflife-Haskell.jpg
  45. -- http://www.iconattitude.com/icons/open_icon_library/apps/png/256/development-haskell.png
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement