histogram_test.go 893 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package imaging
  2. import (
  3. "image"
  4. "image/color"
  5. "testing"
  6. )
  7. func TestHistogram(t *testing.T) {
  8. b := image.Rectangle{image.Point{0, 0}, image.Point{2, 2}}
  9. i1 := image.NewRGBA(b)
  10. i1.Set(0, 0, image.Black)
  11. i1.Set(1, 0, image.White)
  12. i1.Set(1, 1, image.White)
  13. i1.Set(0, 1, color.Gray{123})
  14. h := Histogram(i1)
  15. if h[0] != 0.25 || h[123] != 0.25 || h[255] != 0.5 {
  16. t.Errorf("Incorrect histogram for image i1")
  17. }
  18. i2 := image.NewRGBA(b)
  19. i2.Set(0, 0, color.Gray{51})
  20. i2.Set(0, 1, color.Gray{14})
  21. i2.Set(1, 0, color.Gray{14})
  22. h = Histogram(i2)
  23. if h[14] != 0.5 || h[51] != 0.25 || h[0] != 0.25 {
  24. t.Errorf("Incorrect histogram for image i2")
  25. }
  26. b = image.Rectangle{image.Point{0, 0}, image.Point{0, 0}}
  27. i3 := image.NewRGBA(b)
  28. h = Histogram(i3)
  29. for _, val := range h {
  30. if val != 0 {
  31. t.Errorf("Histogram for an empty image should be a zero histogram.")
  32. return
  33. }
  34. }
  35. }