git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/imaging/transform_test.go (about) 1 package imaging 2 3 import ( 4 "image" 5 "image/color" 6 "testing" 7 ) 8 9 func TestFlipH(t *testing.T) { 10 testCases := []struct { 11 name string 12 src image.Image 13 want *image.NRGBA 14 }{ 15 { 16 "FlipH 2x3", 17 &image.NRGBA{ 18 Rect: image.Rect(-1, -1, 1, 2), 19 Stride: 2 * 4, 20 Pix: []uint8{ 21 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, 22 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 23 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 24 }, 25 }, 26 &image.NRGBA{ 27 Rect: image.Rect(0, 0, 2, 3), 28 Stride: 2 * 4, 29 Pix: []uint8{ 30 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 31 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 32 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 33 }, 34 }, 35 }, 36 } 37 for _, tc := range testCases { 38 t.Run(tc.name, func(t *testing.T) { 39 got := FlipH(tc.src) 40 if !compareNRGBA(got, tc.want, 0) { 41 t.Fatalf("got result %#v want %#v", got, tc.want) 42 } 43 }) 44 } 45 } 46 47 func BenchmarkFlipH(b *testing.B) { 48 b.ReportAllocs() 49 for i := 0; i < b.N; i++ { 50 FlipH(testdataBranchesJPG) 51 } 52 } 53 54 func TestFlipV(t *testing.T) { 55 testCases := []struct { 56 name string 57 src image.Image 58 want *image.NRGBA 59 }{ 60 { 61 "FlipV 2x3", 62 &image.NRGBA{ 63 Rect: image.Rect(-1, -1, 1, 2), 64 Stride: 2 * 4, 65 Pix: []uint8{ 66 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, 67 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 68 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 69 }, 70 }, 71 &image.NRGBA{ 72 Rect: image.Rect(0, 0, 2, 3), 73 Stride: 2 * 4, 74 Pix: []uint8{ 75 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 76 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 77 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, 78 }, 79 }, 80 }, 81 } 82 for _, tc := range testCases { 83 t.Run(tc.name, func(t *testing.T) { 84 got := FlipV(tc.src) 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 BenchmarkFlipV(b *testing.B) { 93 b.ReportAllocs() 94 for i := 0; i < b.N; i++ { 95 FlipV(testdataBranchesJPG) 96 } 97 } 98 99 func TestTranspose(t *testing.T) { 100 testCases := []struct { 101 name string 102 src image.Image 103 want *image.NRGBA 104 }{ 105 { 106 "Transpose 2x3", 107 &image.NRGBA{ 108 Rect: image.Rect(-1, -1, 1, 2), 109 Stride: 2 * 4, 110 Pix: []uint8{ 111 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, 112 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 113 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 114 }, 115 }, 116 &image.NRGBA{ 117 Rect: image.Rect(0, 0, 3, 2), 118 Stride: 3 * 4, 119 Pix: []uint8{ 120 0x00, 0x11, 0x22, 0x33, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 121 0xcc, 0xdd, 0xee, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 122 }, 123 }, 124 }, 125 } 126 for _, tc := range testCases { 127 t.Run(tc.name, func(t *testing.T) { 128 got := Transpose(tc.src) 129 if !compareNRGBA(got, tc.want, 0) { 130 t.Fatalf("got result %#v want %#v", got, tc.want) 131 } 132 }) 133 } 134 } 135 136 func BenchmarkTranspose(b *testing.B) { 137 b.ReportAllocs() 138 for i := 0; i < b.N; i++ { 139 Transpose(testdataBranchesJPG) 140 } 141 } 142 143 func TestTransverse(t *testing.T) { 144 testCases := []struct { 145 name string 146 src image.Image 147 want *image.NRGBA 148 }{ 149 { 150 "Transverse 2x3", 151 &image.NRGBA{ 152 Rect: image.Rect(-1, -1, 1, 2), 153 Stride: 2 * 4, 154 Pix: []uint8{ 155 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, 156 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 157 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 158 }, 159 }, 160 &image.NRGBA{ 161 Rect: image.Rect(0, 0, 3, 2), 162 Stride: 3 * 4, 163 Pix: []uint8{ 164 0x00, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xcc, 0xdd, 0xee, 0xff, 165 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x11, 0x22, 0x33, 166 }, 167 }, 168 }, 169 } 170 for _, tc := range testCases { 171 t.Run(tc.name, func(t *testing.T) { 172 got := Transverse(tc.src) 173 if !compareNRGBA(got, tc.want, 0) { 174 t.Fatalf("got result %#v want %#v", got, tc.want) 175 } 176 }) 177 } 178 } 179 180 func BenchmarkTransverse(b *testing.B) { 181 b.ReportAllocs() 182 for i := 0; i < b.N; i++ { 183 Transverse(testdataBranchesJPG) 184 } 185 } 186 187 func TestRotate90(t *testing.T) { 188 testCases := []struct { 189 name string 190 src image.Image 191 want *image.NRGBA 192 }{ 193 { 194 "Rotate90 2x3", 195 &image.NRGBA{ 196 Rect: image.Rect(-1, -1, 1, 2), 197 Stride: 2 * 4, 198 Pix: []uint8{ 199 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, 200 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 201 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 202 }, 203 }, 204 &image.NRGBA{ 205 Rect: image.Rect(0, 0, 3, 2), 206 Stride: 3 * 4, 207 Pix: []uint8{ 208 0xcc, 0xdd, 0xee, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 209 0x00, 0x11, 0x22, 0x33, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 210 }, 211 }, 212 }, 213 } 214 for _, tc := range testCases { 215 t.Run(tc.name, func(t *testing.T) { 216 got := Rotate90(tc.src) 217 if !compareNRGBA(got, tc.want, 0) { 218 t.Fatalf("got result %#v want %#v", got, tc.want) 219 } 220 }) 221 } 222 } 223 224 func BenchmarkRotate90(b *testing.B) { 225 b.ReportAllocs() 226 for i := 0; i < b.N; i++ { 227 Rotate90(testdataBranchesJPG) 228 } 229 } 230 231 func TestRotate180(t *testing.T) { 232 testCases := []struct { 233 name string 234 src image.Image 235 want *image.NRGBA 236 }{ 237 { 238 "Rotate180 2x3", 239 &image.NRGBA{ 240 Rect: image.Rect(-1, -1, 1, 2), 241 Stride: 2 * 4, 242 Pix: []uint8{ 243 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, 244 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 245 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 246 }, 247 }, 248 &image.NRGBA{ 249 Rect: image.Rect(0, 0, 2, 3), 250 Stride: 2 * 4, 251 Pix: []uint8{ 252 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 253 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 254 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 255 }, 256 }, 257 }, 258 } 259 for _, tc := range testCases { 260 t.Run(tc.name, func(t *testing.T) { 261 got := Rotate180(tc.src) 262 if !compareNRGBA(got, tc.want, 0) { 263 t.Fatalf("got result %#v want %#v", got, tc.want) 264 } 265 }) 266 } 267 } 268 269 func BenchmarkRotate180(b *testing.B) { 270 b.ReportAllocs() 271 for i := 0; i < b.N; i++ { 272 Rotate180(testdataBranchesJPG) 273 } 274 } 275 276 func TestRotate270(t *testing.T) { 277 testCases := []struct { 278 name string 279 src image.Image 280 want *image.NRGBA 281 }{ 282 { 283 "Rotate270 2x3", 284 &image.NRGBA{ 285 Rect: image.Rect(-1, -1, 1, 2), 286 Stride: 2 * 4, 287 Pix: []uint8{ 288 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff, 289 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 290 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 291 }, 292 }, 293 &image.NRGBA{ 294 Rect: image.Rect(0, 0, 3, 2), 295 Stride: 3 * 4, 296 Pix: []uint8{ 297 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x11, 0x22, 0x33, 298 0x00, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xcc, 0xdd, 0xee, 0xff, 299 }, 300 }, 301 }, 302 } 303 for _, tc := range testCases { 304 t.Run(tc.name, func(t *testing.T) { 305 got := Rotate270(tc.src) 306 if !compareNRGBA(got, tc.want, 0) { 307 t.Fatalf("got result %#v want %#v", got, tc.want) 308 } 309 }) 310 } 311 } 312 313 func BenchmarkRotate270(b *testing.B) { 314 b.ReportAllocs() 315 for i := 0; i < b.N; i++ { 316 Rotate270(testdataBranchesJPG) 317 } 318 } 319 320 func TestRotate(t *testing.T) { 321 testCases := []struct { 322 name string 323 src image.Image 324 angle float64 325 bg color.Color 326 want *image.NRGBA 327 }{ 328 { 329 "Rotate 0", 330 &image.NRGBA{ 331 Rect: image.Rect(-1, -1, 3, 3), 332 Stride: 4 * 4, 333 Pix: []uint8{ 334 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 335 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 336 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 337 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 338 }, 339 }, 340 0, 341 color.Black, 342 &image.NRGBA{ 343 Rect: image.Rect(0, 0, 4, 4), 344 Stride: 4 * 4, 345 Pix: []uint8{ 346 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 347 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 348 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 349 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 350 }, 351 }, 352 }, 353 { 354 "Rotate 90", 355 &image.NRGBA{ 356 Rect: image.Rect(-1, -1, 3, 3), 357 Stride: 4 * 4, 358 Pix: []uint8{ 359 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 360 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 361 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 362 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 363 }, 364 }, 365 90, 366 color.Black, 367 &image.NRGBA{ 368 Rect: image.Rect(0, 0, 4, 4), 369 Stride: 4 * 4, 370 Pix: []uint8{ 371 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 372 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 373 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 374 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 375 }, 376 }, 377 }, 378 { 379 "Rotate 180", 380 &image.NRGBA{ 381 Rect: image.Rect(-1, -1, 3, 3), 382 Stride: 4 * 4, 383 Pix: []uint8{ 384 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 385 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 386 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 387 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 388 }, 389 }, 390 180, 391 color.Black, 392 &image.NRGBA{ 393 Rect: image.Rect(0, 0, 4, 4), 394 Stride: 4 * 4, 395 Pix: []uint8{ 396 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 397 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 398 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 399 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 400 }, 401 }, 402 }, 403 { 404 "Rotate 45", 405 &image.NRGBA{ 406 Rect: image.Rect(-1, -1, 3, 3), 407 Stride: 4 * 4, 408 Pix: []uint8{ 409 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 410 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 411 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 412 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 413 }, 414 }, 415 45, 416 color.Black, 417 &image.NRGBA{ 418 Rect: image.Rect(0, 0, 6, 6), 419 Stride: 6 * 4, 420 Pix: []uint8{ 421 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x61, 0x00, 0x00, 0xff, 0x58, 0x08, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 422 0x00, 0x00, 0x00, 0xff, 0x61, 0x00, 0x00, 0xff, 0xe9, 0x16, 0x00, 0xff, 0x35, 0xca, 0x00, 0xff, 0x00, 0x30, 0x30, 0xff, 0x00, 0x00, 0x00, 0xff, 423 0x61, 0x00, 0x00, 0xff, 0xe9, 0x16, 0x00, 0xff, 0x35, 0xca, 0x00, 0xff, 0x00, 0x80, 0x80, 0xff, 0x35, 0x35, 0xff, 0xff, 0x58, 0x58, 0x61, 0xff, 424 0x58, 0x08, 0x00, 0xff, 0x35, 0xca, 0x00, 0xff, 0x00, 0x80, 0x80, 0xff, 0x35, 0x35, 0xff, 0xff, 0xe9, 0xe9, 0xff, 0xff, 0x61, 0x61, 0x61, 0xff, 425 0x00, 0x00, 0x00, 0xff, 0x00, 0x30, 0x30, 0xff, 0x35, 0x35, 0xff, 0xff, 0xe9, 0xe9, 0xff, 0xff, 0x61, 0x61, 0x61, 0xff, 0x00, 0x00, 0x00, 0xff, 426 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0x58, 0x58, 0x61, 0xff, 0x61, 0x61, 0x61, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 427 }, 428 }, 429 }, 430 { 431 "Rotate 0x0", 432 &image.NRGBA{ 433 Rect: image.Rect(0, 0, 0, 0), 434 Stride: 0, 435 Pix: []uint8{}, 436 }, 437 123, 438 color.Black, 439 &image.NRGBA{ 440 Rect: image.Rect(0, 0, 0, 0), 441 Stride: 0, 442 Pix: []uint8{}, 443 }, 444 }, 445 { 446 "Rotate -90", 447 &image.NRGBA{ 448 Rect: image.Rect(-1, -1, 0, 1), 449 Stride: 1 * 4, 450 Pix: []uint8{ 451 0xff, 0x00, 0x00, 0xff, 452 0x00, 0xff, 0x00, 0xff, 453 }, 454 }, 455 -90, 456 color.Black, 457 &image.NRGBA{ 458 Rect: image.Rect(0, 0, 2, 1), 459 Stride: 2 * 4, 460 Pix: []uint8{ 461 0x00, 0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 462 }, 463 }, 464 }, 465 { 466 "Rotate -360*10", 467 &image.NRGBA{ 468 Rect: image.Rect(-1, -1, 0, 1), 469 Stride: 1 * 4, 470 Pix: []uint8{ 471 0x00, 0xff, 0x00, 0xff, 472 0xff, 0x00, 0x00, 0xff, 473 }, 474 }, 475 -360 * 10, 476 color.Black, 477 &image.NRGBA{ 478 Rect: image.Rect(0, 0, 1, 2), 479 Stride: 1 * 4, 480 Pix: []uint8{ 481 0x00, 0xff, 0x00, 0xff, 482 0xff, 0x00, 0x00, 0xff, 483 }, 484 }, 485 }, 486 { 487 "Rotate -360*10 + 90", 488 &image.NRGBA{ 489 Rect: image.Rect(-1, -1, 0, 1), 490 Stride: 1 * 4, 491 Pix: []uint8{ 492 0xff, 0x00, 0x00, 0xff, 493 0x00, 0xff, 0x00, 0xff, 494 }, 495 }, 496 -360*10 + 90, 497 color.Black, 498 &image.NRGBA{ 499 Rect: image.Rect(0, 0, 2, 1), 500 Stride: 2 * 4, 501 Pix: []uint8{ 502 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 503 }, 504 }, 505 }, 506 { 507 "Rotate -360*10 + 180", 508 &image.NRGBA{ 509 Rect: image.Rect(-1, -1, 0, 1), 510 Stride: 1 * 4, 511 Pix: []uint8{ 512 0xff, 0x00, 0x00, 0xff, 513 0x00, 0xff, 0x00, 0xff, 514 }, 515 }, 516 -360*10 + 180, 517 color.Black, 518 &image.NRGBA{ 519 Rect: image.Rect(0, 0, 1, 2), 520 Stride: 1 * 4, 521 Pix: []uint8{ 522 0x00, 0xff, 0x00, 0xff, 523 0xff, 0x00, 0x00, 0xff, 524 }, 525 }, 526 }, 527 { 528 "Rotate -360*10 + 270", 529 &image.NRGBA{ 530 Rect: image.Rect(-1, -1, 0, 1), 531 Stride: 1 * 4, 532 Pix: []uint8{ 533 0xff, 0x00, 0x00, 0xff, 534 0x00, 0xff, 0x00, 0xff, 535 }, 536 }, 537 -360*10 + 270, 538 color.Black, 539 &image.NRGBA{ 540 Rect: image.Rect(0, 0, 2, 1), 541 Stride: 2 * 4, 542 Pix: []uint8{ 543 0x00, 0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 544 }, 545 }, 546 }, 547 { 548 "Rotate 360*10", 549 &image.NRGBA{ 550 Rect: image.Rect(-1, -1, 0, 1), 551 Stride: 1 * 4, 552 Pix: []uint8{ 553 0x00, 0xff, 0x00, 0xff, 554 0xff, 0x00, 0x00, 0xff, 555 }, 556 }, 557 360 * 10, 558 color.Black, 559 &image.NRGBA{ 560 Rect: image.Rect(0, 0, 1, 2), 561 Stride: 1 * 4, 562 Pix: []uint8{ 563 0x00, 0xff, 0x00, 0xff, 564 0xff, 0x00, 0x00, 0xff, 565 }, 566 }, 567 }, 568 { 569 "Rotate 360*10 + 90", 570 &image.NRGBA{ 571 Rect: image.Rect(-1, -1, 0, 1), 572 Stride: 1 * 4, 573 Pix: []uint8{ 574 0xff, 0x00, 0x00, 0xff, 575 0x00, 0xff, 0x00, 0xff, 576 }, 577 }, 578 360*10 + 90, 579 color.Black, 580 &image.NRGBA{ 581 Rect: image.Rect(0, 0, 2, 1), 582 Stride: 2 * 4, 583 Pix: []uint8{ 584 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 585 }, 586 }, 587 }, 588 { 589 "Rotate 360*10 + 180", 590 &image.NRGBA{ 591 Rect: image.Rect(-1, -1, 0, 1), 592 Stride: 1 * 4, 593 Pix: []uint8{ 594 0xff, 0x00, 0x00, 0xff, 595 0x00, 0xff, 0x00, 0xff, 596 }, 597 }, 598 360*10 + 180, 599 color.Black, 600 &image.NRGBA{ 601 Rect: image.Rect(0, 0, 1, 2), 602 Stride: 1 * 4, 603 Pix: []uint8{ 604 0x00, 0xff, 0x00, 0xff, 605 0xff, 0x00, 0x00, 0xff, 606 }, 607 }, 608 }, 609 { 610 "Rotate 360*10 + 270", 611 &image.NRGBA{ 612 Rect: image.Rect(-1, -1, 0, 1), 613 Stride: 1 * 4, 614 Pix: []uint8{ 615 0xff, 0x00, 0x00, 0xff, 616 0x00, 0xff, 0x00, 0xff, 617 }, 618 }, 619 360*10 + 270, 620 color.Black, 621 &image.NRGBA{ 622 Rect: image.Rect(0, 0, 2, 1), 623 Stride: 2 * 4, 624 Pix: []uint8{ 625 0x00, 0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 626 }, 627 }, 628 }, 629 } 630 for _, tc := range testCases { 631 t.Run(tc.name, func(t *testing.T) { 632 got := Rotate(tc.src, tc.angle, tc.bg) 633 if !compareNRGBA(got, tc.want, 0) { 634 t.Fatalf("got result %#v want %#v", got, tc.want) 635 } 636 }) 637 } 638 } 639 640 func BenchmarkRotate(b *testing.B) { 641 b.ReportAllocs() 642 for i := 0; i < b.N; i++ { 643 Rotate(testdataBranchesJPG, 30, color.Transparent) 644 } 645 }