* chore(deps): bump github.com/kovidgoyal/imaging from 1.8.7 to 1.8.19 Bumps [github.com/kovidgoyal/imaging](https://github.com/kovidgoyal/imaging) from 1.8.7 to 1.8.19. - [Release notes](https://github.com/kovidgoyal/imaging/releases) - [Commits](https://github.com/kovidgoyal/imaging/compare/v1.8.7...v1.8.19) --- updated-dependencies: - dependency-name: github.com/kovidgoyal/imaging dependency-version: 1.8.19 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps): bump golang.org/x/net from 0.48.0 to 0.49.0 Bumps [golang.org/x/net](https://github.com/golang/net) from 0.48.0 to 0.49.0. - [Commits](https://github.com/golang/net/compare/v0.48.0...v0.49.0) --- updated-dependencies: - dependency-name: golang.org/x/net dependency-version: 0.49.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> # Conflicts: # go.mod * chore(deps-dev): bump @babel/runtime in /services/idp Bumps [@babel/runtime](https://github.com/babel/babel/tree/HEAD/packages/babel-runtime) from 7.28.2 to 7.28.6. - [Release notes](https://github.com/babel/babel/releases) - [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md) - [Commits](https://github.com/babel/babel/commits/v7.28.6/packages/babel-runtime) --- updated-dependencies: - dependency-name: "@babel/runtime" dependency-version: 7.28.6 dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package imaging
|
|
|
|
import (
|
|
"image"
|
|
"sync"
|
|
|
|
"github.com/kovidgoyal/imaging/nrgba"
|
|
)
|
|
|
|
// Histogram returns a normalized histogram of an image.
|
|
//
|
|
// Resulting histogram is represented as an array of 256 floats, where
|
|
// histogram[i] is a probability of a pixel being of a particular luminance i.
|
|
func Histogram(img image.Image) [256]float64 {
|
|
var mu sync.Mutex
|
|
var histogram [256]float64
|
|
var total float64
|
|
|
|
w, h := img.Bounds().Dx(), img.Bounds().Dy()
|
|
if w == 0 || h == 0 {
|
|
return histogram
|
|
}
|
|
src := nrgba.NewNRGBAScanner(img)
|
|
|
|
if err := run_in_parallel_over_range(0, func(start, limit int) {
|
|
var tmpHistogram [256]float64
|
|
var tmpTotal float64
|
|
scanLine := make([]uint8, w*4)
|
|
for y := start; y < limit; y++ {
|
|
src.Scan(0, y, w, y+1, scanLine)
|
|
i := 0
|
|
for range w {
|
|
s := scanLine[i : i+3 : i+3]
|
|
r := s[0]
|
|
g := s[1]
|
|
b := s[2]
|
|
y := 0.299*float32(r) + 0.587*float32(g) + 0.114*float32(b)
|
|
tmpHistogram[int(y+0.5)]++
|
|
tmpTotal++
|
|
i += 4
|
|
}
|
|
}
|
|
mu.Lock()
|
|
for i := range 256 {
|
|
histogram[i] += tmpHistogram[i]
|
|
}
|
|
total += tmpTotal
|
|
mu.Unlock()
|
|
}, 0, h); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
for i := range 256 {
|
|
histogram[i] = histogram[i] / total
|
|
}
|
|
return histogram
|
|
}
|