From 09126c915e0f410fddfee14239589fad250ea712 Mon Sep 17 00:00:00 2001 From: Martin Lehoux Date: Sun, 5 Apr 2020 23:05:34 -0700 Subject: feat - Use PNG encoder --- image.go | 22 ++++++++++++---------- main.go | 2 +- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/image.go b/image.go index df9039a..08d32a7 100644 --- a/image.go +++ b/image.go @@ -1,7 +1,9 @@ package main import ( - "fmt" + "image" + "image/color" + "image/png" "os" ) @@ -15,15 +17,15 @@ type Frame [Height][Width]Pixel // Save saves frame to PPM file func (frame Frame) Save(filename string) { - f, err := os.Create(filename) - if err != nil { - panic(nil) - } - defer f.Close() - f.WriteString(fmt.Sprintf("P3\n%d %d\n255\n", Width, Height)) - for _, row := range frame { - for _, pixel := range row { - f.WriteString(fmt.Sprintf("%d %d %d ", pixel.r, pixel.g, pixel.b)) + upLeft, bottomRight := image.Point{0, 0}, image.Point{Width, Height} + image := image.NewRGBA(image.Rectangle{upLeft, bottomRight}) + for x, row := range frame { + for y, pixel := range row { + color := color.RGBA{uint8(pixel.r), uint8(pixel.g), uint8(pixel.b), 0xff} + image.Set(y, x, color) } } + f, _ := os.Create(filename) + defer f.Close() + png.Encode(f, image) } diff --git a/main.go b/main.go index 7211f8e..7042d21 100644 --- a/main.go +++ b/main.go @@ -68,7 +68,7 @@ func main() { } bar.Add(1) } - frame.Save("img.ppm") + frame.Save("img.png") // frame.Save(fmt.Sprintf("frames/frame_%06d.ppm", k)) } -- cgit v1.3