You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
1.9 KiB
102 lines
1.9 KiB
package pipelines
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"image"
|
|
|
|
"github.com/disintegration/imaging"
|
|
)
|
|
|
|
type IExecutableStep interface {
|
|
Execute(src image.Image) (image.Image, error)
|
|
}
|
|
|
|
// Resize image
|
|
|
|
type ResizeImageStep struct {
|
|
Step
|
|
Options struct {
|
|
Width int `json:"width"`
|
|
Height int `json:"height"`
|
|
Upscale bool `json:"upscale"`
|
|
} `json:"options"`
|
|
}
|
|
|
|
func (s ResizeImageStep) Execute(src image.Image) (image.Image, error) {
|
|
src = imaging.Resize(src, s.Options.Width, s.Options.Height, imaging.Linear)
|
|
return src, nil
|
|
}
|
|
|
|
// Rotate image
|
|
|
|
type RotateImageStep struct {
|
|
Step
|
|
Options struct {
|
|
Angle float64 `json:"angle"`
|
|
} `json:"options"`
|
|
}
|
|
|
|
func (s RotateImageStep) Execute(src image.Image) (image.Image, error) {
|
|
src = imaging.Rotate(src, s.Options.Angle, image.Black)
|
|
return src, nil
|
|
}
|
|
|
|
// Flip image
|
|
|
|
type FlipImageStep struct {
|
|
Step
|
|
Options struct {
|
|
Direction string `json:"direction"`
|
|
} `json:"options"`
|
|
}
|
|
|
|
func (s FlipImageStep) Execute(src image.Image) (image.Image, error) {
|
|
switch s.Options.Direction {
|
|
case "h":
|
|
src = imaging.FlipH(src)
|
|
case "v":
|
|
src = imaging.FlipH(src)
|
|
default:
|
|
return src, errors.New(fmt.Sprintf("invalid flip direction: %s", s.Options.Direction))
|
|
}
|
|
|
|
return src, nil
|
|
}
|
|
|
|
// Grayscale image
|
|
|
|
type GrayscaleImageStep struct {
|
|
Step
|
|
}
|
|
|
|
func (s GrayscaleImageStep) Execute(src image.Image) (image.Image, error) {
|
|
src = imaging.Grayscale(src)
|
|
return src, nil
|
|
}
|
|
|
|
// Fit image
|
|
|
|
type FitImageStep struct {
|
|
Step
|
|
Options struct {
|
|
Height int `json:"height"`
|
|
Width int `json:"width"`
|
|
} `json:"options"`
|
|
}
|
|
|
|
func (s FitImageStep) Execute(src image.Image) (image.Image, error) {
|
|
src = imaging.Fit(src, s.Options.Width, s.Options.Height, imaging.Lanczos)
|
|
return src, nil
|
|
}
|
|
|
|
// Invert image
|
|
|
|
type InvertImageStep struct {
|
|
Step
|
|
}
|
|
|
|
func (s InvertImageStep) Execute(src image.Image) (image.Image, error) {
|
|
src = imaging.Invert(src)
|
|
return src, nil
|
|
}
|