Add PPM image exporting

This commit is contained in:
Kiana Sheibani 2022-11-30 12:56:52 -05:00
parent 8660bde83f
commit ef52a8f702
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
5 changed files with 49 additions and 11 deletions

6
.gitignore vendored
View file

@ -5,3 +5,9 @@ build/
*.*~ *.*~
**/.DS_Store **/.DS_Store
# Ignore output images
*.ppm
*.png
*.jpg
*.gif

View file

@ -3,15 +3,11 @@ module Render.Object
import Data.Vect import Data.Vect
import Render.Camera import Render.Camera
import Render.Color import Render.Color
import public Render.Object.Interface
import public Render.Object.Point
%default total %default total
public export
interface Object' obj where
draw : obj -> Camera -> List (Integer, Integer, ColorAlpha)
public export public export
data Object : Type where data Object : Type where
MkObject : Object' obj => obj -> Object MkObject : IsObject obj => obj -> Object

View file

@ -0,0 +1,11 @@
module Render.Object.Interface
import Data.Vect
import Render.Color
import Render.Camera
%default total
public export
interface IsObject obj where
draw : obj -> Camera -> List (Integer, Integer, ColorAlpha)

View file

@ -4,12 +4,11 @@ import Data.Vect
import Render.Color import Render.Color
import Render.Camera import Render.Camera
import Render.Picture import Render.Picture
import Render.Object import Render.Object.Interface
%default total %default total
public export public export
record Point where record Point where
constructor MkPoint constructor MkPoint
@ -18,7 +17,7 @@ record Point where
export export
Object' Point where IsObject Point where
draw (MkPoint pos col) cam = draw (MkPoint pos col) cam =
let (px,py) = pointToPix cam pos let (px,py) = pointToPix cam pos
in [(px,py,col)] in [(px,py,col)]

View file

@ -1,6 +1,9 @@
module Render.Scene module Render.Scene
import Data.DPair
import Data.Vect import Data.Vect
import Data.Buffer
import System.File
import Render.Color import Render.Color
import Render.Camera import Render.Camera
import Render.Object import Render.Object
@ -15,7 +18,7 @@ record Scene where
bgcolor : Color bgcolor : Color
public export export
render : (cam : Camera) -> Scene -> PictureType cam render : (cam : Camera) -> Scene -> PictureType cam
render cam sc = render cam sc =
let blank : PictureType cam = replicate _ (replicate _ sc.bgcolor) let blank : PictureType cam = replicate _ (replicate _ sc.bgcolor)
@ -31,3 +34,26 @@ render cam sc =
drawObject pic (MkObject obj) = drawObject pic (MkObject obj) =
let pixs = draw obj cam let pixs = draw obj cam
in foldr drawPixel pic pixs in foldr drawPixel pic pixs
export
renderToPPM : HasIO io => (dest : String) -> Camera -> Scene -> io (Either FileError ())
renderToPPM dest cam sc = do
let bufsize = cast cam.pixw * cast cam.pixh * 3
Just buf <- newBuffer bufsize
| Nothing => pure $ Right ()
let pic = render cam sc
for_ (zip (tabulate fst) (concat pic)) $ \(i,[r,g,b]) => do
setByte buf (cast i * 3) (cast $ r * 255)
setByte buf (cast i * 3 + 1) (cast $ g * 255)
setByte buf (cast i * 3 + 2) (cast $ b * 255)
_ <- if !(exists dest) then removeFile {io} dest else pure $ Right ()
Right h <- openFile dest Append
| Left err => pure $ Left err
Right () <- fPutStrLn h "P6\n\{show cam.pixw} \{show cam.pixh}\n255"
| Left err => pure $ Left err
Right () <- writeBufferData h buf 0 bufsize
| Left (err,_) => pure $ Left err
pure $ Right ()