2021-04-26 13:52:50 +02:00
|
|
|
package processing
|
|
|
|
|
|
|
|
import (
|
2021-09-30 16:23:30 +02:00
|
|
|
"github.com/imgproxy/imgproxy/v3/imagedata"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/imath"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/options"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/vips"
|
2021-04-26 13:52:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func applyFilters(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
|
2021-05-17 15:42:31 +02:00
|
|
|
if po.Blur == 0 && po.Sharpen == 0 && po.Pixelate <= 1 {
|
2021-04-26 13:52:50 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-23 13:23:04 +02:00
|
|
|
if err := img.CopyMemory(); err != nil {
|
2021-04-26 13:52:50 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := img.RgbColourspace(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// When image has alpha, we need to premultiply it to get rid of black edges
|
|
|
|
if err := img.Premultiply(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if po.Blur > 0 {
|
|
|
|
if err := img.Blur(po.Blur); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if po.Sharpen > 0 {
|
|
|
|
if err := img.Sharpen(po.Sharpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-17 14:52:40 +02:00
|
|
|
if po.Pixelate > 1 {
|
|
|
|
pixels := imath.Min(po.Pixelate, imath.Min(img.Width(), img.Height()))
|
|
|
|
if err := img.Pixelate(pixels); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 13:52:50 +02:00
|
|
|
if err := img.Unpremultiply(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := img.CastUchar(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-23 13:23:04 +02:00
|
|
|
return img.CopyMemory()
|
2021-04-26 13:52:50 +02:00
|
|
|
}
|