Skip to content

Commit

Permalink
remove avg grayscale code as well
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-m committed Dec 17, 2023
1 parent 8afc520 commit 18cea79
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 132 deletions.
35 changes: 0 additions & 35 deletions pkg/avg/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,6 @@ import (
"image"
)

// Gray computes the pixel average, and pixel mean deviation from average,
// on a (grayscale) image.
// Scaled to [0, 1].
// Slightly optimized implementation.
func Gray(img *image.Gray) (avg, avgDev float64) {
var sum int64

m, n := img.Bounds().Dx(), img.Bounds().Dy()
s := img.Stride

for y := 0; y < n; y++ {
ys := y * s
for x := 0; x < m; x++ {
ix := ys + x
px := img.Pix[ix]
sum += int64(px)
}
}

cnt := int64(m * n)
avgPx := sum / cnt

sum = 0
for y := 0; y < n; y++ {
ys := y * s
for x := 0; x < m; x++ {
ix := ys + x
px := img.Pix[ix]
sum += iabs(int64(px) - avgPx)
}
}

return float64(avgPx) / 255, float64(sum) / float64(cnt) / 255
}

const four = 4

// RGBA computes the pixel average, and pixel mean deviation from average,
Expand Down
35 changes: 0 additions & 35 deletions pkg/avg/opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,6 @@ import (
"github.com/stretchr/testify/require"
)

func Test_Gray(t *testing.T) {
high, err := imutil.Load("testdata/high.jpg")
require.NoError(t, err)
highG := imutil.ToGray(high)
avg, avgDev := Gray(highG)
assert.InDelta(t, 0.41, avg, 0.01)
assert.InDelta(t, 0.22, avgDev, 0.01)

mid, err := imutil.Load("testdata/mid.jpg")
require.NoError(t, err)
midG := imutil.ToGray(mid)
avg, avgDev = Gray(midG)
assert.InDelta(t, 0.019, avg, 0.001)
assert.InDelta(t, 0.018, avgDev, 0.001)

low, err := imutil.Load("testdata/low.jpg")
require.NoError(t, err)
lowG := imutil.ToGray(low)
avg, avgDev = Gray(lowG)
assert.InDelta(t, 0., avg, 0.0001)
assert.InDelta(t, 0.00324, avgDev, 0.0001)
}

func Benchmark_Gray(b *testing.B) {
high, err := imutil.Load("testdata/high.jpg")
if err != nil {
b.Error(err)
}
highG := imutil.ToGray(high)

for i := 0; i < b.N; i++ {
Gray(highG)
}
}

func Test_RGBA(t *testing.T) {
high, err := imutil.Load("testdata/high.jpg")
require.NoError(t, err)
Expand Down
27 changes: 0 additions & 27 deletions pkg/avg/slow.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,6 @@ func iabs(a int64) int64 {
return a
}

// GraySlow computes the pixel average, and pixel mean deviation from average,
// on a (grayscale) image.
// Scaled to [0, 1].
// This a completely un-optimized and thus rather slow implementation.
func GraySlow(img *image.Gray) (avg, avgDev float64) {
var sum int64
for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
px := img.GrayAt(x, y).Y
sum += int64(px)
}
}

cnt := int64(img.Bounds().Size().X * img.Bounds().Size().Y)
avgPx := sum / cnt

sum = 0
for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
px := img.GrayAt(x, y).Y
sum += iabs(int64(px) - avgPx)
}
}

return float64(avgPx) / 255, float64(sum) / float64(cnt) / 255
}

// RGBASlow computes the pixel average, and pixel mean deviation from average,
// on an RGBA image, per channel.
// Note that the alpha channel is ignored.
Expand Down
35 changes: 0 additions & 35 deletions pkg/avg/slow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,6 @@ import (
"github.com/stretchr/testify/require"
)

func Test_GraySlow(t *testing.T) {
high, err := imutil.Load("testdata/high.jpg")
require.NoError(t, err)
highG := imutil.ToGray(high)
avg, avgDev := GraySlow(highG)
assert.InDelta(t, 0.41, avg, 0.01)
assert.InDelta(t, 0.22, avgDev, 0.01)

mid, err := imutil.Load("testdata/mid.jpg")
require.NoError(t, err)
midG := imutil.ToGray(mid)
avg, avgDev = GraySlow(midG)
assert.InDelta(t, 0.019, avg, 0.001)
assert.InDelta(t, 0.018, avgDev, 0.001)

low, err := imutil.Load("testdata/low.jpg")
require.NoError(t, err)
lowG := imutil.ToGray(low)
avg, avgDev = GraySlow(lowG)
assert.InDelta(t, 0., avg, 0.0001)
assert.InDelta(t, 0.00324, avgDev, 0.0001)
}

func Benchmark_GraySlow(b *testing.B) {
high, err := imutil.Load("testdata/high.jpg")
if err != nil {
b.Error(err)
}
highG := imutil.ToGray(high)

for i := 0; i < b.N; i++ {
GraySlow(highG)
}
}

func Test_RGBASlow(t *testing.T) {
high, err := imutil.Load("testdata/high.jpg")
require.NoError(t, err)
Expand Down

0 comments on commit 18cea79

Please sign in to comment.