1
0

colorspace_to_result.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package processing
  2. func (p *Processor) colorspaceToResult(c *Context) error {
  3. keepProfile := !c.PO.StripColorProfile() && c.PO.Format().SupportsColourProfile()
  4. if c.Img.IsLinear() {
  5. if err := c.Img.RgbColourspace(); err != nil {
  6. return err
  7. }
  8. }
  9. // vips 8.15+ tends to lose the colour profile during some color conversions.
  10. // We probably have a backup of the colour profile, so we need to restore it.
  11. c.Img.RestoreColourProfile()
  12. if c.Img.ColourProfileImported() {
  13. if keepProfile {
  14. // We imported ICC profile and want to keep it,
  15. // so we need to export it
  16. if err := c.Img.ExportColourProfile(); err != nil {
  17. return err
  18. }
  19. } else {
  20. // We imported ICC profile but don't want to keep it,
  21. // so we need to export image to sRGB for maximum compatibility
  22. if err := c.Img.ExportColourProfileToSRGB(); err != nil {
  23. return err
  24. }
  25. }
  26. } else if !keepProfile {
  27. // We don't import ICC profile and don't want to keep it,
  28. // so we need to transform it to sRGB for maximum compatibility
  29. if err := c.Img.TransformColourProfileToSRGB(); err != nil {
  30. return err
  31. }
  32. }
  33. if !keepProfile {
  34. return c.Img.RemoveColourProfile()
  35. }
  36. return nil
  37. }