colorspace_to_processing.go 862 B

1234567891011121314151617181920212223242526272829303132333435
  1. package processing
  2. func (p *Processor) colorspaceToProcessing(c *Context) error {
  3. if c.Img.ColourProfileImported() {
  4. return nil
  5. }
  6. if err := c.Img.Rad2Float(); err != nil {
  7. return err
  8. }
  9. convertToLinear := p.config.UseLinearColorspace && (c.WScale != 1 || c.HScale != 1)
  10. if c.Img.IsLinear() {
  11. // The image is linear. If we keep its ICC, we'll get wrong colors after
  12. // converting it to sRGB
  13. c.Img.RemoveColourProfile()
  14. } else {
  15. // vips 8.15+ tends to lose the colour profile during some color conversions.
  16. // We need to backup the colour profile before the conversion and restore it later.
  17. c.Img.BackupColourProfile()
  18. if convertToLinear || !c.Img.IsRGB() {
  19. if err := c.Img.ImportColourProfile(); err != nil {
  20. return err
  21. }
  22. }
  23. }
  24. if convertToLinear {
  25. return c.Img.LinearColourspace()
  26. }
  27. return c.Img.RgbColourspace()
  28. }