Skip to content

Commit d09c984

Browse files
committed
Updates crop transform and scalar API
1 parent 20da25c commit d09c984

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed

regression_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ func TestBottomRightCrop(t *testing.T) {
5353
func TestOffsetCrop(t *testing.T) {
5454
goldenTest(t, "fixtures/tomatoes.png", func(tx *vips.Transform) {
5555
tx.Resize(500, 720).
56-
CropOffset(120, 0).
56+
CropOffsetX(120).
5757
ResizeStrategy(vips.ResizeStrategyCrop)
5858
})
5959
}
6060

6161
func TestRelativeOffsetCrop(t *testing.T) {
6262
goldenTest(t, "fixtures/tomatoes.png", func(tx *vips.Transform) {
6363
tx.Resize(500, 720).
64-
CropOffsetPercent(0.1066, 0).
64+
CropRelativeOffsetX(0.1066).
6565
ResizeStrategy(vips.ResizeStrategyCrop)
6666
})
6767
}

transform.go

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,27 @@ func (t *Transform) Anchor(anchor Anchor) *Transform {
109109
return t
110110
}
111111

112-
// CropOffset sets the target offset from the crop position
113-
func (t *Transform) CropOffset(x, y int) *Transform {
112+
// CropOffsetX sets the target offset from the crop position
113+
func (t *Transform) CropOffsetX(x int) *Transform {
114114
t.tx.CropOffsetX.SetInt(x)
115+
return t
116+
}
117+
118+
// CropOffsetY sets the target offset from the crop position
119+
func (t *Transform) CropOffsetY(y int) *Transform {
115120
t.tx.CropOffsetY.SetInt(y)
116121
return t
117122
}
118123

119-
// CropOffsetPercent sets the target offset from the crop position
120-
func (t *Transform) CropOffsetPercent(x, y float64) *Transform {
121-
t.tx.CropOffsetX.SetRelative(x)
122-
t.tx.CropOffsetY.SetRelative(y)
124+
// CropRelativeOffsetX sets the target offset from the crop position
125+
func (t *Transform) CropRelativeOffsetX(x float64) *Transform {
126+
t.tx.CropOffsetX.SetScale(x)
127+
return t
128+
}
129+
130+
// CropRelativeOffsetY sets the target offset from the crop position
131+
func (t *Transform) CropRelativeOffsetY(y float64) *Transform {
132+
t.tx.CropOffsetY.SetScale(y)
123133
return t
124134
}
125135

@@ -186,20 +196,20 @@ func (t *Transform) Stretch() *Transform {
186196

187197
// ScaleWidth scales the image by its width proportionally
188198
func (t *Transform) ScaleWidth(scale float64) *Transform {
189-
t.tx.Width.SetRelative(scale)
199+
t.tx.Width.SetScale(scale)
190200
return t
191201
}
192202

193203
// ScaleHeight scales the height of the image proportionally
194204
func (t *Transform) ScaleHeight(scale float64) *Transform {
195-
t.tx.Height.SetRelative(scale)
205+
t.tx.Height.SetScale(scale)
196206
return t
197207
}
198208

199209
// Scale the image
200210
func (t *Transform) Scale(scale float64) *Transform {
201-
t.tx.Width.SetRelative(scale)
202-
t.tx.Height.SetRelative(scale)
211+
t.tx.Width.SetScale(scale)
212+
t.tx.Height.SetScale(scale)
203213
return t
204214
}
205215

@@ -307,7 +317,7 @@ func NewBlackboard(image *ImageRef, p *TransformParams) *Blackboard {
307317
bb.cropOffsetX = p.CropOffsetX.GetRounded(imageWidth)
308318
bb.cropOffsetY = p.CropOffsetY.GetRounded(imageHeight)
309319

310-
if p.Width.Value() == 0 && p.Height.Value() == 0 {
320+
if p.Width.Value == 0 && p.Height.Value == 0 {
311321
return bb
312322
}
313323

@@ -323,8 +333,8 @@ func NewBlackboard(image *ImageRef, p *TransformParams) *Blackboard {
323333
bb.targetWidth = roundFloat(ratio(bb.targetHeight, imageHeight) * float64(imageWidth))
324334
}
325335

326-
if p.Width.IsRelative() && p.Height.IsRelative() {
327-
sx, sy := p.Width.Value(), p.Height.Value()
336+
if p.Width.Relative && p.Height.Relative {
337+
sx, sy := p.Width.Value, p.Height.Value
328338
if sx == 0 {
329339
sx = sy
330340
} else if sy == 0 {
@@ -610,37 +620,29 @@ func (r *LazyFile) Write(p []byte) (n int, err error) {
610620
}
611621

612622
type Scalar struct {
613-
value float64
614-
relative bool
623+
Value float64
624+
Relative bool
615625
}
616626

617627
func (s *Scalar) SetInt(value int) {
618628
s.Set(float64(value))
619629
}
620630

621631
func (s *Scalar) Set(value float64) {
622-
s.value = value
623-
s.relative = false
624-
}
625-
626-
func (s *Scalar) SetRelative(f float64) {
627-
s.value = f
628-
s.relative = true
629-
}
630-
631-
func (s *Scalar) IsRelative() bool {
632-
return s.relative
632+
s.Value = value
633+
s.Relative = false
633634
}
634635

635-
func (s *Scalar) Value() float64 {
636-
return s.value
636+
func (s *Scalar) SetScale(f float64) {
637+
s.Value = f
638+
s.Relative = true
637639
}
638640

639641
func (s *Scalar) Get(base int) float64 {
640-
if s.relative {
641-
return s.value * float64(base)
642+
if s.Relative {
643+
return s.Value * float64(base)
642644
}
643-
return s.value
645+
return s.Value
644646
}
645647

646648
func (s *Scalar) GetRounded(base int) int {

0 commit comments

Comments
 (0)