Files
overlord/Overlord-Client/cmd/agent/capture/jpeg_std.go
T

44 lines
909 B
Go
Raw Normal View History

2026-07-30 14:16:34 -05:00
//go:build !turbojpeg
2026-03-05 00:04:26 -06:00
package capture
import (
"bytes"
"image"
"image/jpeg"
"sync"
2026-03-05 00:04:26 -06:00
)
var jpegBufPool = sync.Pool{
New: func() interface{} {
b := bytes.NewBuffer(make([]byte, 0, 128*1024))
return b
},
}
2026-03-05 00:04:26 -06:00
func encodeJPEG(img image.Image, quality int) ([]byte, error) {
buf := jpegBufPool.Get().(*bytes.Buffer)
buf.Reset()
2026-07-30 14:16:34 -05:00
err := jpeg.Encode(buf, img, &jpeg.Options{Quality: jpegEncoderQuality(quality)})
2026-03-05 00:04:26 -06:00
if err != nil {
jpegBufPool.Put(buf)
2026-03-05 00:04:26 -06:00
return nil, err
}
out := make([]byte, buf.Len())
copy(out, buf.Bytes())
jpegBufPool.Put(buf)
return out, nil
2026-03-05 00:04:26 -06:00
}
2026-05-21 23:28:06 -05:00
func encodeJPEGToBuf(dst *bytes.Buffer, img image.Image, quality int) error {
buf := jpegBufPool.Get().(*bytes.Buffer)
buf.Reset()
2026-07-30 14:16:34 -05:00
if err := jpeg.Encode(buf, img, &jpeg.Options{Quality: jpegEncoderQuality(quality)}); err != nil {
2026-05-21 23:28:06 -05:00
jpegBufPool.Put(buf)
return err
}
dst.Write(buf.Bytes())
jpegBufPool.Put(buf)
return nil
}