-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Closes #95 Implementing Focal Point Cropping * #95 Wrap the filterContext into the ImageFilterContext * Adding test for FocalPoint cropping * Address review comments
- Loading branch information
Showing
7 changed files
with
288 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package filters | ||
|
||
import ( | ||
"github.com/zalando-stups/skrop/parse" | ||
"github.com/zalando/skipper/filters" | ||
"gopkg.in/h2non/bimg.v1" | ||
"strconv" | ||
) | ||
|
||
// CropByFocalPointName is the name of the filter | ||
const CropByFocalPointName = "cropByFocalPoint" | ||
|
||
type cropByFocalPoint struct { | ||
targetX float64 | ||
targetY float64 | ||
aspectRatio float64 | ||
} | ||
|
||
// NewCropByFocalPoint creates a new filter of this type | ||
func NewCropByFocalPoint() filters.Spec { | ||
return &cropByFocalPoint{} | ||
} | ||
|
||
func (f *cropByFocalPoint) Name() string { | ||
return CropByFocalPointName | ||
} | ||
|
||
func (f *cropByFocalPoint) CreateOptions(imageContext *ImageFilterContext) (*bimg.Options, error) { | ||
imageSize, err := imageContext.Image.Size() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
focalPointX := imageContext.PathParam("focalPointX") | ||
focalPointY := imageContext.PathParam("focalPointY") | ||
|
||
if focalPointX == "" || focalPointY == "" { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
|
||
sourceX, err := strconv.Atoi(focalPointX) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sourceY, err := strconv.Atoi(focalPointY) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
right := imageSize.Width - sourceX | ||
bottom := imageSize.Height - sourceY | ||
|
||
cropLeftWidth := int(float64(sourceX) / f.targetX) | ||
cropRightWidth := int(float64(right) / (float64(1) - f.targetX)) | ||
|
||
width := cropRightWidth | ||
|
||
if cropLeftWidth < cropRightWidth { | ||
width = cropLeftWidth | ||
} | ||
|
||
cropTopHeight := int(float64(sourceY) / f.targetY) | ||
cropBottomHeight := int(float64(bottom) / (float64(1) - f.targetY)) | ||
|
||
height := cropBottomHeight | ||
|
||
if cropTopHeight < cropBottomHeight { | ||
height = int(float64(sourceY) / f.targetY) | ||
} | ||
|
||
ratio := float64(height) / float64(width) | ||
|
||
if ratio > f.aspectRatio { | ||
height = int(float64(width) * f.aspectRatio) | ||
} else { | ||
width = int(float64(height) / f.aspectRatio) | ||
} | ||
|
||
return &bimg.Options{ | ||
AreaWidth: width, | ||
AreaHeight: height, | ||
Top: sourceY - int(float64(height) * f.targetY), | ||
Left: sourceX - int(float64(width) * f.targetX)}, nil | ||
} | ||
|
||
func (f *cropByFocalPoint) CanBeMerged(other *bimg.Options, self *bimg.Options) bool { | ||
return false | ||
} | ||
|
||
func (f *cropByFocalPoint) Merge(other *bimg.Options, self *bimg.Options) *bimg.Options { | ||
return self | ||
} | ||
|
||
func (f *cropByFocalPoint) CreateFilter(args []interface{}) (filters.Filter, error) { | ||
var err error | ||
|
||
if len(args) < 3 || len(args) > 3 { | ||
return nil, filters.ErrInvalidFilterParameters | ||
} | ||
|
||
c := &cropByFocalPoint{} | ||
|
||
c.targetX, err = parse.EskipFloatArg(args[0]) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c.targetY, err = parse.EskipFloatArg(args[1]) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c.aspectRatio, err = parse.EskipFloatArg(args[2]) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func (f *cropByFocalPoint) Request(ctx filters.FilterContext) {} | ||
|
||
func (f *cropByFocalPoint) Response(ctx filters.FilterContext) { | ||
HandleImageResponse(ctx, f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package filters | ||
|
||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/zalando-stups/skrop/filters/imagefiltertest" | ||
"github.com/zalando/skipper/filters" | ||
) | ||
|
||
func TestNewCropByFocalPoint(t *testing.T) { | ||
name := NewCropByFocalPoint().Name() | ||
assert.Equal(t, "cropByFocalPoint", name) | ||
} | ||
|
||
func TestCropByFocalPoint_Name(t *testing.T) { | ||
c := cropByFocalPoint{} | ||
assert.Equal(t, "cropByFocalPoint", c.Name()) | ||
} | ||
|
||
func TestCropByFocalPoint_CreateOptions(t *testing.T) { | ||
c := cropByFocalPoint{targetX: 0.5, targetY: 0.5, aspectRatio: 1.5} | ||
image := imagefiltertest.LandscapeImage() | ||
fc := createDefaultContext(t, "doesnotmatter.com") | ||
fc.FParams = make(map[string]string) | ||
fc.FParams["focalPointX"] = "500"; | ||
fc.FParams["focalPointY"] = "334"; | ||
|
||
options, _ := c.CreateOptions(buildParameters(fc, image)) | ||
|
||
assert.Equal(t, 445, options.AreaWidth) | ||
assert.Equal(t, 668, options.AreaHeight) | ||
assert.Equal(t, 0, options.Top) | ||
assert.Equal(t, 278, options.Left) | ||
|
||
c = cropByFocalPoint{targetX: 0.5, targetY: 0.25, aspectRatio: 1.5} | ||
image = imagefiltertest.LandscapeImage() | ||
fc = createDefaultContext(t, "doesnotmatter.com") | ||
fc.FParams = make(map[string]string) | ||
fc.FParams["focalPointX"] = "500"; | ||
fc.FParams["focalPointY"] = "334"; | ||
|
||
options, _ = c.CreateOptions(buildParameters(fc, image)) | ||
|
||
assert.Equal(t, 296, options.AreaWidth) | ||
assert.Equal(t, 445, options.AreaHeight) | ||
assert.Equal(t, 223, options.Top) | ||
assert.Equal(t, 352, options.Left) | ||
} | ||
|
||
func TestCropByFocalPoint_CreateOptions_MissingPathParam(t *testing.T) { | ||
c := cropByFocalPoint{targetX: 0.5, targetY: 0.5, aspectRatio: 1.5} | ||
image := imagefiltertest.LandscapeImage() | ||
fc := createDefaultContext(t, "doesnotmatter.com") | ||
fc.FParams = make(map[string]string) | ||
fc.FParams["focalPointY"] = "334"; | ||
|
||
options, err := c.CreateOptions(buildParameters(fc, image)) | ||
|
||
assert.Nil(t, options) | ||
assert.Equal(t, filters.ErrInvalidFilterParameters, err) | ||
|
||
fc = createDefaultContext(t, "doesnotmatter.com") | ||
fc.FParams = make(map[string]string) | ||
fc.FParams["focalPointX"] = "334"; | ||
|
||
options, err = c.CreateOptions(buildParameters(fc, image)) | ||
|
||
assert.Nil(t, options) | ||
assert.Equal(t, filters.ErrInvalidFilterParameters, err) | ||
} | ||
|
||
func TestCropByFocalPoint_CreateOptions_InvalidPathParam(t *testing.T) { | ||
c := cropByFocalPoint{targetX: 0.5, targetY: 0.5, aspectRatio: 1.5} | ||
image := imagefiltertest.LandscapeImage() | ||
fc := createDefaultContext(t, "doesnotmatter.com") | ||
fc.FParams = make(map[string]string) | ||
fc.FParams["focalPointX"] = "xyz"; | ||
fc.FParams["focalPointY"] = "abc"; | ||
|
||
options, err := c.CreateOptions(buildParameters(fc, image)) | ||
|
||
assert.Nil(t, options) | ||
assert.NotNil(t, err) | ||
|
||
fc.FParams["focalPointX"] = "100"; | ||
fc.FParams["focalPointY"] = "abc"; | ||
|
||
options, err = c.CreateOptions(buildParameters(fc, image)) | ||
|
||
assert.Nil(t, options) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestCropByFocalPoint_CanBeMerged(t *testing.T) { | ||
ea := transformFromQueryParams{} | ||
assert.Equal(t, false, ea.CanBeMerged(nil, nil)) | ||
} | ||
|
||
func TestCropByFocalPoint_CreateFilter(t *testing.T) { | ||
imagefiltertest.TestCreate(t, NewCropByFocalPoint, []imagefiltertest.CreateTestItem{{ | ||
Msg: "less than 3 args", | ||
Args: nil, | ||
Err: true, | ||
}, { | ||
Msg: "invalid targetX", | ||
Args: []interface{}{"xyz", 0.5, 1.5}, | ||
Err: true, | ||
}, { | ||
Msg: "invalid targetY", | ||
Args: []interface{}{0.5, "abc", 1.5}, | ||
Err: true, | ||
}, { | ||
Msg: "invalid aspectRatio", | ||
Args: []interface{}{0.5, 0.5, "qwerty"}, | ||
Err: true, | ||
}, { | ||
Msg: "3 args", | ||
Args: []interface{}{0.5, 0.5, 1.5}, | ||
Err: false, | ||
}, { | ||
Msg: "more than 3 args", | ||
Args: []interface{}{0.5, 0.5, 1.5, 1.0}, | ||
Err: true, | ||
}}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters