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

     1  package imaging
     2  
     3  import (
     4  	"image"
     5  	"testing"
     6  )
     7  
     8  func TestBlur(t *testing.T) {
     9  	testCases := []struct {
    10  		name  string
    11  		src   image.Image
    12  		sigma float64
    13  		want  *image.NRGBA
    14  	}{
    15  		{
    16  			"Blur 3x3 0",
    17  			&image.NRGBA{
    18  				Rect:   image.Rect(-1, -1, 2, 2),
    19  				Stride: 3 * 4,
    20  				Pix: []uint8{
    21  					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    22  					0x00, 0x00, 0x00, 0x00, 0x66, 0xaa, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
    23  					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    24  				},
    25  			},
    26  			0.0,
    27  			&image.NRGBA{
    28  				Rect:   image.Rect(0, 0, 3, 3),
    29  				Stride: 3 * 4,
    30  				Pix: []uint8{
    31  					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    32  					0x00, 0x00, 0x00, 0x00, 0x66, 0xaa, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
    33  					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    34  				},
    35  			},
    36  		},
    37  		{
    38  			"Blur 3x3 0.5",
    39  			&image.NRGBA{
    40  				Rect:   image.Rect(-1, -1, 2, 2),
    41  				Stride: 3 * 4,
    42  				Pix: []uint8{
    43  					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    44  					0x00, 0x00, 0x00, 0x00, 0x66, 0xaa, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
    45  					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    46  				},
    47  			},
    48  			0.5,
    49  			&image.NRGBA{
    50  				Rect:   image.Rect(0, 0, 3, 3),
    51  				Stride: 3 * 4,
    52  				Pix: []uint8{
    53  					0x66, 0xaa, 0xff, 0x04, 0x66, 0xaa, 0xff, 0x18, 0x66, 0xaa, 0xff, 0x04,
    54  					0x66, 0xaa, 0xff, 0x18, 0x66, 0xaa, 0xff, 0x9e, 0x66, 0xaa, 0xff, 0x18,
    55  					0x66, 0xaa, 0xff, 0x04, 0x66, 0xaa, 0xff, 0x18, 0x66, 0xaa, 0xff, 0x04,
    56  				},
    57  			},
    58  		},
    59  		{
    60  			"Blur 3x3 10",
    61  			&image.NRGBA{
    62  				Rect:   image.Rect(-1, -1, 2, 2),
    63  				Stride: 3 * 4,
    64  				Pix: []uint8{
    65  					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    66  					0x00, 0x00, 0x00, 0x00, 0x66, 0xaa, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
    67  					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    68  				},
    69  			},
    70  			10,
    71  			&image.NRGBA{
    72  				Rect:   image.Rect(0, 0, 3, 3),
    73  				Stride: 3 * 4,
    74  				Pix: []uint8{
    75  					0x66, 0xaa, 0xff, 0x1c, 0x66, 0xaa, 0xff, 0x1c, 0x66, 0xaa, 0xff, 0x1c,
    76  					0x66, 0xaa, 0xff, 0x1c, 0x66, 0xaa, 0xff, 0x1c, 0x66, 0xaa, 0xff, 0x1c,
    77  					0x66, 0xaa, 0xff, 0x1c, 0x66, 0xaa, 0xff, 0x1c, 0x66, 0xaa, 0xff, 0x1c,
    78  				},
    79  			},
    80  		},
    81  	}
    82  	for _, tc := range testCases {
    83  		t.Run(tc.name, func(t *testing.T) {
    84  			got := Blur(tc.src, tc.sigma)
    85  			if !compareNRGBA(got, tc.want, 0) {
    86  				t.Fatalf("got result %#v want %#v", got, tc.want)
    87  			}
    88  		})
    89  	}
    90  }
    91  
    92  func TestBlurGolden(t *testing.T) {
    93  	for name, sigma := range map[string]float64{
    94  		"out_blur_0.5.png": 0.5,
    95  		"out_blur_1.5.png": 1.5,
    96  	} {
    97  		got := Blur(testdataFlowersSmallPNG, sigma)
    98  		want, err := Open("testdata/" + name)
    99  		if err != nil {
   100  			t.Fatalf("failed to open image: %v", err)
   101  		}
   102  		if !compareNRGBAGolden(got, toNRGBA(want)) {
   103  			t.Fatalf("resulting image differs from golden: %s", name)
   104  		}
   105  	}
   106  }
   107  
   108  func BenchmarkBlur(b *testing.B) {
   109  	b.ReportAllocs()
   110  	for i := 0; i < b.N; i++ {
   111  		Blur(testdataBranchesJPG, 3)
   112  	}
   113  }
   114  
   115  func TestSharpen(t *testing.T) {
   116  	testCases := []struct {
   117  		name  string
   118  		src   image.Image
   119  		sigma float64
   120  		want  *image.NRGBA
   121  	}{
   122  		{
   123  			"Sharpen 3x3 0",
   124  			&image.NRGBA{
   125  				Rect:   image.Rect(-1, -1, 2, 2),
   126  				Stride: 3 * 4,
   127  				Pix: []uint8{
   128  					0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
   129  					0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
   130  					0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
   131  				},
   132  			},
   133  			0,
   134  			&image.NRGBA{
   135  				Rect:   image.Rect(0, 0, 3, 3),
   136  				Stride: 3 * 4,
   137  				Pix: []uint8{
   138  					0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
   139  					0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
   140  					0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
   141  				},
   142  			},
   143  		},
   144  		{
   145  			"Sharpen 3x3 0.5",
   146  			&image.NRGBA{
   147  				Rect:   image.Rect(-1, -1, 2, 2),
   148  				Stride: 3 * 4,
   149  				Pix: []uint8{
   150  					0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
   151  					0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
   152  					0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
   153  				},
   154  			},
   155  			0.5,
   156  			&image.NRGBA{
   157  				Rect:   image.Rect(0, 0, 3, 3),
   158  				Stride: 3 * 4,
   159  				Pix: []uint8{
   160  					0x66, 0x66, 0x66, 0x66, 0x64, 0x64, 0x64, 0x64, 0x66, 0x66, 0x66, 0x66,
   161  					0x64, 0x64, 0x64, 0x64, 0x7d, 0x7d, 0x7d, 0x7e, 0x64, 0x64, 0x64, 0x64,
   162  					0x66, 0x66, 0x66, 0x66, 0x64, 0x64, 0x64, 0x64, 0x66, 0x66, 0x66, 0x66,
   163  				},
   164  			},
   165  		},
   166  		{
   167  			"Sharpen 3x3 100",
   168  			&image.NRGBA{
   169  				Rect:   image.Rect(-1, -1, 2, 2),
   170  				Stride: 3 * 4,
   171  				Pix: []uint8{
   172  					0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
   173  					0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
   174  					0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
   175  				},
   176  			},
   177  			100,
   178  			&image.NRGBA{
   179  				Rect:   image.Rect(0, 0, 3, 3),
   180  				Stride: 3 * 4,
   181  				Pix: []uint8{
   182  					0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64,
   183  					0x64, 0x64, 0x64, 0x64, 0x86, 0x86, 0x86, 0x86, 0x64, 0x64, 0x64, 0x64,
   184  					0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64,
   185  				},
   186  			},
   187  		},
   188  		{
   189  			"Sharpen 3x1 10",
   190  			&image.NRGBA{
   191  				Rect:   image.Rect(-1, -1, 2, 0),
   192  				Stride: 3 * 4,
   193  				Pix: []uint8{
   194  					0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
   195  				},
   196  			},
   197  			10,
   198  			&image.NRGBA{
   199  				Rect:   image.Rect(0, 0, 3, 1),
   200  				Stride: 3 * 4,
   201  				Pix: []uint8{
   202  					0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
   203  				},
   204  			},
   205  		},
   206  	}
   207  	for _, tc := range testCases {
   208  		t.Run(tc.name, func(t *testing.T) {
   209  			got := Sharpen(tc.src, tc.sigma)
   210  			if !compareNRGBA(got, tc.want, 0) {
   211  				t.Fatalf("got result %#v want %#v", got, tc.want)
   212  			}
   213  		})
   214  	}
   215  }
   216  
   217  func TestSharpenGolden(t *testing.T) {
   218  	for name, sigma := range map[string]float64{
   219  		"out_sharpen_0.5.png": 0.5,
   220  		"out_sharpen_1.5.png": 1.5,
   221  	} {
   222  		got := Sharpen(testdataFlowersSmallPNG, sigma)
   223  		want, err := Open("testdata/" + name)
   224  		if err != nil {
   225  			t.Fatalf("failed to open image: %v", err)
   226  		}
   227  		if !compareNRGBAGolden(got, toNRGBA(want)) {
   228  			t.Fatalf("resulting image differs from golden: %s", name)
   229  		}
   230  	}
   231  }
   232  
   233  func BenchmarkSharpen(b *testing.B) {
   234  	b.ReportAllocs()
   235  	for i := 0; i < b.N; i++ {
   236  		Sharpen(testdataBranchesJPG, 3)
   237  	}
   238  }