apply_filters.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package processing
  2. import (
  3. "github.com/imgproxy/imgproxy/v3/imagedata"
  4. "github.com/imgproxy/imgproxy/v3/imath"
  5. "github.com/imgproxy/imgproxy/v3/options"
  6. "github.com/imgproxy/imgproxy/v3/vips"
  7. )
  8. func applyFilters(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  9. if po.Blur == 0 && po.Sharpen == 0 && po.Pixelate <= 1 {
  10. return nil
  11. }
  12. if err := img.CopyMemory(); err != nil {
  13. return err
  14. }
  15. if err := img.RgbColourspace(); err != nil {
  16. return err
  17. }
  18. // When image has alpha, we need to premultiply it to get rid of black edges
  19. if err := img.Premultiply(); err != nil {
  20. return err
  21. }
  22. if po.Blur > 0 {
  23. if err := img.Blur(po.Blur); err != nil {
  24. return err
  25. }
  26. }
  27. if po.Sharpen > 0 {
  28. if err := img.Sharpen(po.Sharpen); err != nil {
  29. return err
  30. }
  31. }
  32. if po.Pixelate > 1 {
  33. pixels := imath.Min(po.Pixelate, imath.Min(img.Width(), img.Height()))
  34. if err := img.Pixelate(pixels); err != nil {
  35. return err
  36. }
  37. }
  38. if err := img.Unpremultiply(); err != nil {
  39. return err
  40. }
  41. if err := img.CastUchar(); err != nil {
  42. return err
  43. }
  44. return img.CopyMemory()
  45. }