MCPcopy Index your code
hub / github.com/docker/docker-agent / pickSmallestEncoding

Function pickSmallestEncoding

pkg/chat/image.go:259–276  ·  view source on GitHub ↗

pickSmallestEncoding encodes the image as both PNG and JPEG and returns whichever is smaller.

(img image.Image)

Source from the content-addressed store, hash-verified

257
258// pickSmallestEncoding encodes the image as both PNG and JPEG and returns whichever is smaller.
259func pickSmallestEncoding(img image.Image) ([]byte, string, error) {
260 pngData, errPNG := encodePNG(img)
261 jpegData, errJPEG := encodeJPEG(img, jpegQuality)
262 if errPNG != nil && errJPEG != nil {
263 return nil, "", errors.Join(errPNG, errJPEG)
264 }
265 if errPNG != nil {
266 return jpegData, "image/jpeg", nil
267 }
268 if errJPEG != nil {
269 return pngData, "image/png", nil
270 }
271
272 if len(pngData) <= len(jpegData) {
273 return pngData, "image/png", nil
274 }
275 return jpegData, "image/jpeg", nil
276}
277
278func encodePNG(img image.Image) ([]byte, error) {
279 var buf bytes.Buffer

Callers 1

ResizeImageFunction · 0.85

Calls 2

encodePNGFunction · 0.85
encodeJPEGFunction · 0.85

Tested by

no test coverage detected