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