This commit is contained in:
TonyChyi 2022-09-28 14:29:54 +08:00
parent 095b52fb5d
commit 5c93e698b6
3 changed files with 12 additions and 9 deletions

View File

@ -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
}
}
}

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=