From 5c93e698b627711e51b51d3b366732e6b2dbccba Mon Sep 17 00:00:00 2001 From: TonyChyi Date: Wed, 28 Sep 2022 14:29:54 +0800 Subject: [PATCH] resize! --- drivers/video/ppm.go | 18 +++++++++--------- go.mod | 1 + go.sum | 2 ++ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/drivers/video/ppm.go b/drivers/video/ppm.go index 3be6027..6459500 100644 --- a/drivers/video/ppm.go +++ b/drivers/video/ppm.go @@ -6,6 +6,7 @@ import ( "image/color" "io" + "github.com/nfnt/resize" "github.com/pion/mediadevices/pkg/frame" "github.com/pion/mediadevices/pkg/io/video" "github.com/pion/mediadevices/pkg/prop" @@ -71,17 +72,16 @@ func (v *PPMStreamDriver) VideoRecord(p prop.Media) (video.Reader, error) { return nil, func() {}, err } - // draw input image in middle of canvas - offsetX := (canvas.Bounds().Dx() - img.Bounds().Dx()) / 2 - offsetY := (canvas.Bounds().Dy() - img.Bounds().Dy()) / 2 - for x := 0; x < img.Bounds().Dx(); x++ { - for y := 0; y < img.Bounds().Dy(); y++ { - r, g, b, _ := img.At(x, y).RGBA() + // resize image and draw it to canvas + resized := resize.Resize(uint(p.Width), uint(p.Height), img, resize.Lanczos3) + for x := 0; x < resized.Bounds().Dx(); x++ { + for y := 0; y < resized.Bounds().Dy(); y++ { + r, g, b, _ := resized.At(x, y).RGBA() Y, Cb, Cr := color.RGBToYCbCr(uint8(r), uint8(g), uint8(b)) - canvas.Y[canvas.YOffset(offsetX+x, offsetY+y)] = Y - canvas.Cb[canvas.COffset(offsetX+x, offsetY+y)] = Cb - canvas.Cr[canvas.COffset(offsetX+x, offsetY+y)] = Cr + canvas.Y[canvas.YOffset(x, y)] = Y + canvas.Cb[canvas.COffset(x, y)] = Cb + canvas.Cr[canvas.COffset(x, y)] = Cr } } } diff --git a/go.mod b/go.mod index 79ecbd3..b0c5431 100644 --- a/go.mod +++ b/go.mod @@ -52,6 +52,7 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/digitalocean/go-qemu v0.0.0-20220804221245-2002801203aa github.com/gin-gonic/gin v1.8.1 + github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/pion/webrtc/v3 v3.1.43 github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/sirupsen/logrus v1.9.0 diff --git a/go.sum b/go.sum index 24fdaf1..1b82706 100644 --- a/go.sum +++ b/go.sum @@ -71,6 +71,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=