Created
September 21, 2015 16:45
-
-
Save gsscoder/41df52d9b2f9c29f29f7 to your computer and use it in GitHub Desktop.
Haskell port of OCaml http://pastebin.com/a66MDZkP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-| | |
| Haskell port of OCaml http://pastebin.com/a66MDZkP | |
| -} | |
| import qualified Data.ByteString.Lazy as B | |
| import Data.Word8 | |
| type Point = (Double, Double) | |
| type Rgb = (Double, Double, Double) | |
| tga :: (Point -> Rgb) -> Int -> Int -> String -> IO () | |
| tga f width height path = do | |
| B.writeFile path (B.pack contents') | |
| where | |
| pxs = [((adjust x width),(adjust y height)) | y <- [0..height-1], x <- [0..width-1]] | |
| pxs' = fmap f pxs | |
| adjust p dim = ((toDouble p) - (toDouble dim) / 2) / (toDouble dim) | |
| header = | |
| [0,0,2,0,0,0,0,0,0,0,0,0, | |
| width `mod` 256, truncate (toDouble width / 256), | |
| height `mod` 256, truncate (toDouble height / 256), | |
| 24,0] | |
| contents = header ++ (concat $ fmap (\(r,g,b) -> asByte b : asByte g : [asByte r]) pxs') | |
| contents' = fmap toWord8 contents | |
| asByte x = min 255 (max 0 (truncate (128.0 * (x + 1.0)))) | |
| toWord8 x = (fromIntegral x) :: Word8 | |
| toDouble x = (fromIntegral x) :: Double | |
| main :: IO () | |
| main = do | |
| tga (\(x,y) -> (sin x, sin y, x * y)) 640 480 "/Users/giacomo/temp/x.tga" | |
| putStrLn "done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment