histogram_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package imaging
  2. import (
  3. "image"
  4. "testing"
  5. )
  6. func TestHistogram(t *testing.T) {
  7. testCases := []struct {
  8. name string
  9. img image.Image
  10. want [256]float64
  11. }{
  12. {
  13. name: "grayscale",
  14. img: &image.RGBA{
  15. Rect: image.Rect(-1, -1, 1, 1),
  16. Stride: 2 * 4,
  17. Pix: []uint8{
  18. 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  19. 0xff, 0xff, 0xff, 0xff, 0x80, 0x80, 0x80, 0xff,
  20. },
  21. },
  22. want: [256]float64{0x00: 0.25, 0x80: 0.25, 0xff: 0.5},
  23. },
  24. {
  25. name: "colorful",
  26. img: &image.RGBA{
  27. Rect: image.Rect(-1, -1, 1, 1),
  28. Stride: 2 * 4,
  29. Pix: []uint8{
  30. 0x00, 0x00, 0x00, 0xff, 0x33, 0x44, 0x55, 0xff,
  31. 0x55, 0x44, 0x33, 0xff, 0x77, 0x66, 0x55, 0xff,
  32. },
  33. },
  34. want: [256]float64{0x00: 0.25, 0x41: 0.25, 0x47: 0.25, 0x69: 0.25},
  35. },
  36. {
  37. name: "zero",
  38. img: &image.RGBA{},
  39. want: [256]float64{},
  40. },
  41. }
  42. for _, tc := range testCases {
  43. t.Run(tc.name, func(t *testing.T) {
  44. got := Histogram(tc.img)
  45. if got != tc.want {
  46. t.Fatalf("got histogram %#v want %#v", got, tc.want)
  47. }
  48. })
  49. }
  50. }
  51. func BenchmarkHistogram(b *testing.B) {
  52. b.ReportAllocs()
  53. for i := 0; i < b.N; i++ {
  54. Histogram(testdataBranchesJPG)
  55. }
  56. }