Skip to content

Instantly share code, notes, and snippets.

@gsscoder gsscoder/tga.hs
Created Sep 21, 2015

Embed
What would you like to do?
Haskell port of OCaml http://pastebin.com/a66MDZkP
{-|
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
You can’t perform that action at this time.