import_color_profile.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package processing
  2. import (
  3. "github.com/imgproxy/imgproxy/v3/config"
  4. "github.com/imgproxy/imgproxy/v3/imagedata"
  5. "github.com/imgproxy/imgproxy/v3/options"
  6. "github.com/imgproxy/imgproxy/v3/vips"
  7. )
  8. func importColorProfile(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  9. if img.ColourProfileImported() {
  10. return nil
  11. }
  12. if err := img.Rad2Float(); err != nil {
  13. return err
  14. }
  15. convertToLinear := config.UseLinearColorspace && (pctx.wscale != 1 || pctx.hscale != 1)
  16. if img.IsLinear() {
  17. // The image is linear. If we keep its ICC, we'll get wrong colors after
  18. // converting it to sRGB
  19. img.RemoveColourProfile()
  20. } else {
  21. // vips 8.15+ tends to lose the colour profile during some color conversions.
  22. // We need to backup the colour profile before the conversion and restore it later.
  23. img.BackupColourProfile()
  24. if convertToLinear || !img.IsRGB() {
  25. if err := img.ImportColourProfile(); err != nil {
  26. return err
  27. }
  28. }
  29. }
  30. if convertToLinear {
  31. return img.LinearColourspace()
  32. }
  33. return img.RgbColourspace()
  34. }