@@ -351,14 +351,15 @@ func TestPixMap_Pointer(t *testing.T) {
351
351
func TestPixMap_Foreach (t * testing.T ) {
352
352
t .Run ("should not do anything when update function is nil" , func (t * testing.T ) {
353
353
pixMap := pi .NewPixMap (2 , 2 )
354
- pixMap .Foreach (0 , 0 , 2 , 2 , nil )
354
+ pixMap .Foreach (nil )
355
355
})
356
356
357
357
t .Run ("should run update line number of times" , func (t * testing.T ) {
358
358
t .Run ("inside clipping range" , func (t * testing.T ) {
359
359
pixMap := pi .NewPixMap (1 , 3 )
360
360
timesRun := 0
361
- pixMap .Foreach (0 , 0 , 1 , 2 , func (x , y int , dst []byte ) {
361
+ src := pixMap .WithClip (0 , 0 , 1 , 2 )
362
+ src .Foreach (func (x , y int , dst []byte ) {
362
363
timesRun ++
363
364
})
364
365
assert .Equal (t , 2 , timesRun )
@@ -367,7 +368,8 @@ func TestPixMap_Foreach(t *testing.T) {
367
368
t .Run ("outside clipping range" , func (t * testing.T ) {
368
369
pixMap := pi .NewPixMap (1 , 3 )
369
370
timesRun := 0
370
- pixMap .Foreach (0 , 0 , 1 , 4 , func (x , y int , dst []byte ) {
371
+ src := pixMap .WithClip (0 , 0 , 1 , 4 )
372
+ src .Foreach (func (x , y int , dst []byte ) {
371
373
timesRun ++
372
374
})
373
375
assert .Equal (t , 3 , timesRun )
@@ -380,15 +382,17 @@ func TestPixMap_Foreach(t *testing.T) {
380
382
381
383
t .Run ("x=0" , func (t * testing.T ) {
382
384
var lines [][]byte
383
- pixMap .Foreach (0 , 0 , 2 , 3 , func (x , y int , dst []byte ) {
385
+ src := pixMap .WithClip (0 , 0 , 2 , 3 )
386
+ src .Foreach (func (x , y int , dst []byte ) {
384
387
lines = append (lines , dst )
385
388
})
386
389
assert .Equal (t , [][]byte {{0 , 1 }, {3 , 4 }, {6 , 7 }}, lines )
387
390
})
388
391
389
392
t .Run ("x=1" , func (t * testing.T ) {
390
393
var lines [][]byte
391
- pixMap .Foreach (1 , 0 , 2 , 3 , func (x , y int , dst []byte ) {
394
+ src := pixMap .WithClip (1 , 0 , 2 , 3 )
395
+ src .Foreach (func (x , y int , dst []byte ) {
392
396
lines = append (lines , dst )
393
397
})
394
398
assert .Equal (t , [][]byte {{1 , 2 }, {4 , 5 }, {7 , 8 }}, lines )
@@ -401,15 +405,17 @@ func TestPixMap_Foreach(t *testing.T) {
401
405
402
406
t .Run ("inside clipping range" , func (t * testing.T ) {
403
407
var coords []pi.Position
404
- pixMap .Foreach (1 , 1 , 2 , 2 , func (x , y int , dst []byte ) {
408
+ src := pixMap .WithClip (1 , 1 , 2 , 2 )
409
+ src .Foreach (func (x , y int , dst []byte ) {
405
410
coords = append (coords , pi.Position {X : x , Y : y })
406
411
})
407
412
assert .Equal (t , []pi.Position {{X : 1 , Y : 1 }, {X : 1 , Y : 2 }}, coords )
408
413
})
409
414
410
415
t .Run ("outside clipping range" , func (t * testing.T ) {
411
416
var coords []pi.Position
412
- pixMap .Foreach (- 1 , - 1 , 3 , 3 , func (x , y int , dst []byte ) {
417
+ src := pixMap .WithClip (- 1 , - 1 , 3 , 3 )
418
+ src .Foreach (func (x , y int , dst []byte ) {
413
419
coords = append (coords , pi.Position {X : x , Y : y })
414
420
})
415
421
assert .Equal (t , []pi.Position {{X : 0 , Y : 0 }, {X : 0 , Y : 1 }}, coords )
@@ -422,14 +428,16 @@ func TestPixMap_Foreach(t *testing.T) {
422
428
update := func (x , y int , dst []byte ) {
423
429
executed = true
424
430
}
425
- pixMap .Foreach (0 , 0 , 0 , 1 , update ) // width = 0
431
+ src := pixMap .WithClip (0 , 0 , 0 , 1 ) // width = 0
432
+ src .Foreach (update )
426
433
assert .False (t , executed )
427
434
})
428
435
429
436
t .Run ("should update pixels" , func (t * testing.T ) {
430
437
pixMap := pi .NewPixMap (2 , 3 )
431
438
i := byte (1 )
432
- pixMap .Foreach (0 , 0 , 2 , 3 , func (x , y int , dst []byte ) {
439
+ src := pixMap .WithClip (0 , 0 , 2 , 3 )
440
+ src .Foreach (func (x , y int , dst []byte ) {
433
441
dst [0 ] = i
434
442
dst [1 ] = i + 1
435
443
i += 2
@@ -442,7 +450,7 @@ func TestPixMap_Copy(t *testing.T) {
442
450
testPixMapCopy (t , pi .PixMap .Copy )
443
451
}
444
452
445
- func testPixMapCopy (t * testing.T , merge func (pi.PixMap , int , int , int , int , pi.PixMap , int , int )) {
453
+ func testPixMapCopy (t * testing.T , merge func (pi.PixMap , pi.PixMap , int , int )) {
446
454
t .Run ("src bigger than dst" , func (t * testing.T ) {
447
455
src := pi .NewPixMapWithPix ([]byte {
448
456
1 , 2 , 3 ,
@@ -488,7 +496,8 @@ func testPixMapCopy(t *testing.T, merge func(pi.PixMap, int, int, int, int, pi.P
488
496
for name , test := range tests {
489
497
t .Run (name , func (t * testing.T ) {
490
498
dst := pi .NewPixMap (dstWidth , dstHeight )
491
- merge (src , test .x , test .y , test .w , test .h , dst , test .dstX , test .dstY )
499
+ source := src .WithClip (test .x , test .y , test .w , test .h )
500
+ merge (source , dst , test .dstX , test .dstY )
492
501
assert .Equal (t , test .expected , dst .Pix ())
493
502
})
494
503
}
@@ -517,16 +526,17 @@ func testPixMapCopy(t *testing.T, merge func(pi.PixMap, int, int, int, int, pi.P
517
526
for name , test := range tests {
518
527
t .Run (name , func (t * testing.T ) {
519
528
dst := pi .NewPixMap (dstWidth , dstHeight )
520
- merge (src , test .x , test .y , test .w , test .h , dst , test .dstX , test .dstY )
529
+ source := src .WithClip (test .x , test .y , test .w , test .h )
530
+ merge (source , dst , test .dstX , test .dstY )
521
531
assert .Equal (t , test .expected , dst .Pix ())
522
532
})
523
533
}
524
534
})
525
535
}
526
536
527
537
func TestPixMap_Merge (t * testing.T ) {
528
- testPixMapCopy (t , func (src pi.PixMap , x int , y int , w int , h int , dst pi.PixMap , dstX int , dstY int ) {
529
- src .Merge (x , y , w , h , dst , dstX , dstY , func (dst , src []byte ) {
538
+ testPixMapCopy (t , func (src pi.PixMap , dst pi.PixMap , dstX int , dstY int ) {
539
+ src .Merge (dst , dstX , dstY , func (dst , src []byte ) {
530
540
copy (dst , src )
531
541
})
532
542
})
0 commit comments