export_color_profile.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package processing
  2. import (
  3. "github.com/imgproxy/imgproxy/v3/imagedata"
  4. "github.com/imgproxy/imgproxy/v3/options"
  5. "github.com/imgproxy/imgproxy/v3/vips"
  6. )
  7. func exportColorProfile(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  8. keepProfile := !po.StripColorProfile && po.Format.SupportsColourProfile()
  9. if img.IsLinear() {
  10. if err := img.RgbColourspace(); err != nil {
  11. return err
  12. }
  13. }
  14. // vips 8.15+ tends to lose the colour profile during some color conversions.
  15. // We probably have a backup of the colour profile, so we need to restore it.
  16. img.RestoreColourProfile()
  17. if img.ColourProfileImported() {
  18. if keepProfile {
  19. // We imported ICC profile and want to keep it,
  20. // so we need to export it
  21. if err := img.ExportColourProfile(); err != nil {
  22. return err
  23. }
  24. } else {
  25. // We imported ICC profile but don't want to keep it,
  26. // so we need to export image to sRGB for maximum compatibility
  27. if err := img.ExportColourProfileToSRGB(); err != nil {
  28. return err
  29. }
  30. }
  31. } else if !keepProfile {
  32. // We don't import ICC profile and don't want to keep it,
  33. // so we need to transform it to sRGB for maximum compatibility
  34. if err := img.TransformColourProfile(); err != nil {
  35. return err
  36. }
  37. }
  38. if !keepProfile {
  39. return img.RemoveColourProfile()
  40. }
  41. return nil
  42. }