allow crop options to be floats and negative

values between 0 and 1 have the same behavior as the size option - it is
treated as a percentage of the original image size.  Negative values for
cx and cy are calculated from the bottom and right edges of the image.
This commit is contained in:
Will Norris 2017-08-31 07:27:35 +00:00
parent 430baac0b0
commit 4533f0c68a
4 changed files with 71 additions and 50 deletions

View file

@ -81,13 +81,15 @@ func TestCropParams(t *testing.T) {
x0, y0, x1, y1 int
crop bool
}{
{Options{CropHeight: 0, CropWidth: 10}, 0, 0, 0, 0, false},
{Options{CropHeight: 10, CropWidth: 0}, 0, 0, 0, 0, false},
{Options{CropHeight: -1, CropWidth: -1}, 0, 0, 0, 0, false},
{Options{CropWidth: 10, CropHeight: 0}, 0, 0, 10, 128, true},
{Options{CropWidth: 0, CropHeight: 10}, 0, 0, 64, 10, true},
{Options{CropWidth: -1, CropHeight: -1}, 0, 0, 0, 0, false},
{Options{CropWidth: 50, CropHeight: 100}, 0, 0, 50, 100, true},
{Options{CropWidth: 100, CropHeight: 100}, 0, 0, 64, 100, true},
{Options{CropX: 50, CropY: 100, CropWidth: 50, CropHeight: 100}, 14, 28, 64, 128, true},
{Options{CropX: 50, CropY: 100, CropWidth: 100, CropHeight: 150}, 0, 0, 64, 128, true},
{Options{CropX: 50, CropY: 100}, 50, 100, 64, 128, true},
{Options{CropX: 50, CropY: 100, CropWidth: 100, CropHeight: 150}, 50, 100, 64, 128, true},
{Options{CropX: -50, CropY: -50}, 14, 78, 64, 128, true},
{Options{CropY: 0.5, CropWidth: 0.5}, 0, 64, 32, 128, true},
}
for _, tt := range tests {
x0, y0, x1, y1, crop := cropParams(src, tt.opt)