export_color_profile.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. if pctx.iccImported {
  15. if keepProfile {
  16. // We imported ICC profile and want to keep it,
  17. // so we need to export it
  18. if err := img.ExportColourProfile(); err != nil {
  19. return err
  20. }
  21. } else {
  22. // We imported ICC profile but don't want to keep it,
  23. // so we need to export image to sRGB for maximum compatibility
  24. if err := img.ExportColourProfileToSRGB(); err != nil {
  25. return err
  26. }
  27. }
  28. } else if !keepProfile {
  29. // We don't import ICC profile and don't want to keep it,
  30. // so we need to transform it to sRGB for maximum compatibility
  31. if err := img.TransformColourProfile(); err != nil {
  32. return err
  33. }
  34. }
  35. if !keepProfile {
  36. return img.RemoveColourProfile()
  37. }
  38. return nil
  39. }