git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/imaging/histogram_test.go (about)

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