determineOutputFormat determines the output image format based on the processing options and image properties. It modifies the ProcessingOptions in place to set the output format.
( img *vips.Image, imgdata imagedata.ImageData, po ProcessingOptions, animated bool, )
| 282 | // |
| 283 | // It modifies the ProcessingOptions in place to set the output format. |
| 284 | func (p *Processor) determineOutputFormat( |
| 285 | img *vips.Image, |
| 286 | imgdata imagedata.ImageData, |
| 287 | po ProcessingOptions, |
| 288 | animated bool, |
| 289 | ) (imagetype.Type, error) { |
| 290 | // Check if the image may have transparency |
| 291 | expectTransparency := !po.ShouldFlatten() && |
| 292 | (img.HasAlpha() || po.PaddingEnabled() || po.ExtendEnabled()) |
| 293 | |
| 294 | format := po.Format() |
| 295 | |
| 296 | switch { |
| 297 | case format == imagetype.SVG: |
| 298 | // At this point we can't allow requested format to be SVG as we can't save SVGs |
| 299 | return imagetype.Unknown, newSaveFormatError(format) |
| 300 | case format == imagetype.Unknown: |
| 301 | switch { |
| 302 | case po.PreferJxl() && !animated: |
| 303 | format = imagetype.JXL |
| 304 | case po.PreferAvif() && !animated: |
| 305 | format = imagetype.AVIF |
| 306 | case po.PreferWebP(): |
| 307 | format = imagetype.WEBP |
| 308 | case p.isImageTypePreferred(imgdata.Format()): |
| 309 | format = imgdata.Format() |
| 310 | default: |
| 311 | format = p.findPreferredFormat(animated, expectTransparency) |
| 312 | } |
| 313 | case po.EnforceJxl() && !animated: |
| 314 | format = imagetype.JXL |
| 315 | case po.EnforceAvif() && !animated: |
| 316 | format = imagetype.AVIF |
| 317 | case po.EnforceWebP(): |
| 318 | format = imagetype.WEBP |
| 319 | } |
| 320 | |
| 321 | po.SetFormat(format) |
| 322 | |
| 323 | if !vips.SupportsSave(format) { |
| 324 | return format, newSaveFormatError(format) |
| 325 | } |
| 326 | |
| 327 | return format, nil |
| 328 | } |
| 329 | |
| 330 | // isImageTypePreferred checks if the given image type is in the list of preferred formats. |
| 331 | func (p *Processor) isImageTypePreferred(imgtype imagetype.Type) bool { |
no test coverage detected