Files
opencloud/vendor/github.com/davidbyttow/govips/v2/vips/math.go
dependabot[bot] 8f26149743 build(deps): bump github.com/davidbyttow/govips/v2 from 2.17.0 to 2.18.0
Bumps [github.com/davidbyttow/govips/v2](https://github.com/davidbyttow/govips) from 2.17.0 to 2.18.0.
- [Release notes](https://github.com/davidbyttow/govips/releases)
- [Commits](https://github.com/davidbyttow/govips/compare/v2.17.0...v2.18.0)

---
updated-dependencies:
- dependency-name: github.com/davidbyttow/govips/v2
  dependency-version: 2.18.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-04-23 08:57:32 +02:00

58 lines
1.4 KiB
Go

package vips
import "math"
// Scalar is the basic scalar measurement of an image's height, width or offset coordinate.
type Scalar struct {
Value float64
Relative bool
}
// ValueOf takes a floating point value and returns a corresponding Scalar struct
func ValueOf(value float64) Scalar {
return Scalar{value, false}
}
// IsZero checks whether the associated Scalar's value is zero.
func (s *Scalar) IsZero() bool {
return s.Value == 0 && !s.Relative
}
// SetInt sets an integer value for the associated Scalar.
func (s *Scalar) SetInt(value int) {
s.Set(float64(value))
}
// Set sets a float value for the associated Scalar.
func (s *Scalar) Set(value float64) {
s.Value = value
s.Relative = false
}
// SetScale sets a float value for the associated Scalar and makes it relative.
func (s *Scalar) SetScale(f float64) {
s.Value = f
s.Relative = true
}
// Get returns the value of the scalar. Either absolute, or if relative, multiplied by the base given as parameter.
func (s *Scalar) Get(base int) float64 {
if s.Relative {
return s.Value * float64(base)
}
return s.Value
}
// GetRounded returns the value of the associated Scalar, rounded to the nearest integer, if absolute.
// If the Scalar is relative, it will be multiplied by the supplied base parameter.
func (s *Scalar) GetRounded(base int) int {
return roundFloat(s.Get(base))
}
func roundFloat(f float64) int {
if f < 0 {
return int(math.Ceil(f - 0.5))
}
return int(math.Floor(f + 0.5))
}