-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcontext_image_test.go
More file actions
1161 lines (963 loc) · 30 KB
/
Copy pathcontext_image_test.go
File metadata and controls
1161 lines (963 loc) · 30 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gg
import (
"image"
"image/color"
"math"
"testing"
)
func TestDrawImage(t *testing.T) {
// Create a context
dc := NewContext(200, 200)
dc.Clear()
// Create a test image
img, err := NewImageBuf(50, 50, FormatRGBA8)
if err != nil {
t.Fatalf("Failed to create image: %v", err)
}
// Fill with red
for y := 0; y < 50; y++ {
for x := 0; x < 50; x++ {
_ = img.SetRGBA(x, y, 255, 0, 0, 255)
}
}
// Draw image
dc.DrawImage(img, 10, 10)
// Verify pixel at (10, 10) is red
result := dc.pixmap.GetPixel(10, 10)
if result.R < 0.9 || result.G > 0.1 || result.B > 0.1 {
t.Errorf("Expected red pixel at (10, 10), got R=%.2f G=%.2f B=%.2f", result.R, result.G, result.B)
}
}
func TestDrawImageEx(t *testing.T) {
tests := []struct {
name string
opts DrawImageOptions
}{
{
name: "basic position",
opts: DrawImageOptions{
X: 20,
Y: 20,
Interpolation: InterpNearest,
Opacity: 1.0,
BlendMode: BlendNormal,
},
},
{
name: "scaled",
opts: DrawImageOptions{
X: 20,
Y: 20,
DstWidth: 100,
DstHeight: 100,
Interpolation: InterpBilinear,
Opacity: 1.0,
BlendMode: BlendNormal,
},
},
{
name: "with opacity",
opts: DrawImageOptions{
X: 20,
Y: 20,
Interpolation: InterpBilinear,
Opacity: 0.5,
BlendMode: BlendNormal,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dc := NewContext(200, 200)
dc.Clear()
// Create test image
img, err := NewImageBuf(50, 50, FormatRGBA8)
if err != nil {
t.Fatalf("Failed to create image: %v", err)
}
// Fill with blue
for y := 0; y < 50; y++ {
for x := 0; x < 50; x++ {
_ = img.SetRGBA(x, y, 0, 0, 255, 255)
}
}
// Draw with options
dc.DrawImageEx(img, tt.opts)
// Verify something was drawn (just check it's not transparent)
x := int(tt.opts.X)
y := int(tt.opts.Y)
if x >= 0 && x < 200 && y >= 0 && y < 200 {
result := dc.pixmap.GetPixel(x, y)
if result.A == 0 {
t.Errorf("Expected non-transparent pixel at (%d, %d)", x, y)
}
}
})
}
}
func TestDrawImageWithTransform(t *testing.T) {
dc := NewContext(200, 200)
dc.Clear()
// Create test image
img, err := NewImageBuf(50, 50, FormatRGBA8)
if err != nil {
t.Fatalf("Failed to create image: %v", err)
}
// Fill with green
for y := 0; y < 50; y++ {
for x := 0; x < 50; x++ {
_ = img.SetRGBA(x, y, 0, 255, 0, 255)
}
}
// Apply transformation
dc.Translate(100, 100)
dc.Scale(0.5, 0.5)
// Draw image
dc.DrawImage(img, 0, 0)
// The image should be drawn at transformed position (100, 100) with 0.5 scale
// Check pixel near the center of the transformed image
result := dc.pixmap.GetPixel(100, 100)
if result.G < 0.9 {
t.Errorf("Expected green pixel at transformed position, got G=%.2f", result.G)
}
}
func TestDrawImageSrcRect(t *testing.T) {
dc := NewContext(200, 200)
dc.Clear()
// Create test image with quadrants
img, err := NewImageBuf(100, 100, FormatRGBA8)
if err != nil {
t.Fatalf("Failed to create image: %v", err)
}
// Top-left: red
for y := 0; y < 50; y++ {
for x := 0; x < 50; x++ {
_ = img.SetRGBA(x, y, 255, 0, 0, 255)
}
}
// Top-right: green
for y := 0; y < 50; y++ {
for x := 50; x < 100; x++ {
_ = img.SetRGBA(x, y, 0, 255, 0, 255)
}
}
// Draw only top-left quadrant
srcRect := image.Rect(0, 0, 50, 50)
dc.DrawImageEx(img, DrawImageOptions{
X: 10,
Y: 10,
SrcRect: &srcRect,
Interpolation: InterpNearest,
Opacity: 1.0,
BlendMode: BlendNormal,
})
// Verify we got red (top-left quadrant)
result := dc.pixmap.GetPixel(10, 10)
if result.R < 0.9 || result.G > 0.1 {
t.Errorf("Expected red from top-left quadrant, got R=%.2f G=%.2f", result.R, result.G)
}
}
func TestCreateImagePattern(t *testing.T) {
dc := NewContext(200, 200)
// Create test image
img, err := NewImageBuf(50, 50, FormatRGBA8)
if err != nil {
t.Fatalf("Failed to create image: %v", err)
}
// Fill with yellow
for y := 0; y < 50; y++ {
for x := 0; x < 50; x++ {
_ = img.SetRGBA(x, y, 255, 255, 0, 255)
}
}
// Create pattern
pattern := dc.CreateImagePattern(img, 0, 0, 50, 50)
if pattern == nil {
t.Fatal("CreateImagePattern returned nil")
}
// Test ColorAt
col := pattern.ColorAt(10, 10)
if col.R < 0.9 || col.G < 0.9 || col.B > 0.1 {
t.Errorf("Expected yellow from pattern, got R=%.2f G=%.2f B=%.2f", col.R, col.G, col.B)
}
}
func TestImagePattern_Tiling(t *testing.T) {
// Create a small 2x2 image
img, err := NewImageBuf(2, 2, FormatRGBA8)
if err != nil {
t.Fatalf("Failed to create image: %v", err)
}
// Set distinct colors
_ = img.SetRGBA(0, 0, 255, 0, 0, 255) // Red
_ = img.SetRGBA(1, 0, 0, 255, 0, 255) // Green
_ = img.SetRGBA(0, 1, 0, 0, 255, 255) // Blue
_ = img.SetRGBA(1, 1, 255, 255, 0, 255) // Yellow
pattern := &ImagePattern{
image: img,
x: 0,
y: 0,
w: 2,
h: 2,
inverse: Identity(),
}
// Test wrapping behavior
tests := []struct {
x, y float64
expected string
}{
{0, 0, "red"},
{1, 0, "green"},
{0, 1, "blue"},
{1, 1, "yellow"},
{2, 0, "red"}, // Wraps to (0, 0)
{0, 2, "red"}, // Wraps to (0, 0)
{3, 3, "yellow"}, // Wraps to (1, 1)
}
for _, tt := range tests {
col := pattern.ColorAt(tt.x, tt.y)
// Just verify we get some color (detailed color checking is complex with wrapping)
if col.A == 0 {
t.Errorf("ColorAt(%.0f, %.0f) returned transparent (expected %s)", tt.x, tt.y, tt.expected)
}
}
}
func TestNewImageBuf(t *testing.T) {
tests := []struct {
name string
width int
height int
format ImageFormat
wantErr bool
}{
{"valid RGBA8", 100, 100, FormatRGBA8, false},
{"valid Gray8", 50, 50, FormatGray8, false},
{"zero width", 0, 100, FormatRGBA8, true},
{"zero height", 100, 0, FormatRGBA8, true},
{"negative width", -10, 100, FormatRGBA8, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
img, err := NewImageBuf(tt.width, tt.height, tt.format)
if (err != nil) != tt.wantErr {
t.Errorf("NewImageBuf() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && img == nil {
t.Error("NewImageBuf() returned nil without error")
}
})
}
}
func TestImageBufFromImage(t *testing.T) {
// Create standard library image
stdImg := image.NewRGBA(image.Rect(0, 0, 10, 10))
stdImg.Set(5, 5, color.RGBA{R: 255, G: 0, B: 0, A: 255})
// Convert to ImageBuf
img := ImageBufFromImage(stdImg)
if img == nil {
t.Fatal("ImageBufFromImage returned nil")
}
// Verify dimensions
w, h := img.Bounds()
if w != 10 || h != 10 {
t.Errorf("Expected 10x10, got %dx%d", w, h)
}
// Verify pixel
r, g, b, a := img.GetRGBA(5, 5)
if r != 255 || g != 0 || b != 0 || a != 255 {
t.Errorf("Expected red pixel, got RGBA(%d, %d, %d, %d)", r, g, b, a)
}
}
func TestSetFillPattern(t *testing.T) {
dc := NewContext(100, 100)
// Create a simple pattern
img, err := NewImageBuf(10, 10, FormatRGBA8)
if err != nil {
t.Fatalf("Failed to create image: %v", err)
}
pattern := dc.CreateImagePattern(img, 0, 0, 10, 10)
// Set as fill pattern
dc.SetFillPattern(pattern)
// Verify pattern is set
if dc.paint.Pattern == nil {
t.Error("Expected paint pattern to be set")
}
}
func TestSetStrokePattern(t *testing.T) {
dc := NewContext(100, 100)
// Create a simple pattern
img, err := NewImageBuf(10, 10, FormatRGBA8)
if err != nil {
t.Fatalf("Failed to create image: %v", err)
}
pattern := dc.CreateImagePattern(img, 0, 0, 10, 10)
// Set as stroke pattern
dc.SetStrokePattern(pattern)
// Verify pattern is set
if dc.paint.Pattern == nil {
t.Error("Expected paint pattern to be set")
}
}
func TestInterpolationModes(t *testing.T) {
modes := []InterpolationMode{
InterpNearest,
InterpBilinear,
InterpBicubic,
}
for _, mode := range modes {
t.Run(mode.String(), func(t *testing.T) {
dc := NewContext(200, 200)
dc.Clear()
// Create small source image
img, err := NewImageBuf(50, 50, FormatRGBA8)
if err != nil {
t.Fatalf("Failed to create image: %v", err)
}
// Fill with magenta
for y := 0; y < 50; y++ {
for x := 0; x < 50; x++ {
_ = img.SetRGBA(x, y, 255, 0, 255, 255)
}
}
// Scale up to test interpolation
dc.DrawImageEx(img, DrawImageOptions{
X: 10,
Y: 10,
DstWidth: 100,
DstHeight: 100,
Interpolation: mode,
Opacity: 1.0,
BlendMode: BlendNormal,
})
// Check pixel at the start of the drawn image (should definitely be there)
result := dc.pixmap.GetPixel(15, 15)
if result.A < 0.9 {
t.Errorf("Expected opaque pixel with %s interpolation, got A=%.2f", mode.String(), result.A)
}
// Verify it's magenta-ish (allowing for some interpolation error)
if result.R < 0.8 || result.B < 0.8 {
t.Errorf("Expected magenta pixel with %s interpolation, got R=%.2f G=%.2f B=%.2f", mode.String(), result.R, result.G, result.B)
}
})
}
}
func TestBlendModes(t *testing.T) {
modes := []struct {
mode BlendMode
name string
}{
{BlendNormal, "Normal"},
{BlendMultiply, "Multiply"},
{BlendScreen, "Screen"},
{BlendOverlay, "Overlay"},
}
for _, m := range modes {
t.Run(m.name, func(t *testing.T) {
dc := NewContext(100, 100)
// Fill background with white
dc.SetRGB(1, 1, 1)
dc.DrawRectangle(0, 0, 100, 100)
dc.Fill()
// Create red image
img, err := NewImageBuf(50, 50, FormatRGBA8)
if err != nil {
t.Fatalf("Failed to create image: %v", err)
}
for y := 0; y < 50; y++ {
for x := 0; x < 50; x++ {
_ = img.SetRGBA(x, y, 255, 0, 0, 255)
}
}
// Draw with blend mode
dc.DrawImageEx(img, DrawImageOptions{
X: 25,
Y: 25,
Interpolation: InterpNearest,
Opacity: 1.0,
BlendMode: m.mode,
})
// Just verify something was drawn
result := dc.pixmap.GetPixel(30, 30)
if result.A == 0 {
t.Errorf("Expected non-transparent pixel with %s blend mode", m.name)
}
})
}
}
// TestDrawImageClipped_RoundedRect verifies that DrawImage respects a
// rounded rectangle clip region.
func TestDrawImageClipped_RoundedRect(t *testing.T) {
dc := NewContext(200, 200)
dc.Clear()
// Create a red 100x100 image.
img := makeTestImage(t, 100, 100, 255, 0, 0, 255)
dc.Push()
// Set a rounded rectangle clip in the center.
dc.DrawRoundedRectangle(50, 50, 100, 100, 15)
dc.Clip()
// Draw image at (50, 50) — exactly covering the clip region.
dc.DrawImage(img, 50, 50)
dc.Pop()
// Pixel inside the clip (center of the rounded rect) should be red.
inside := dc.pixmap.GetPixel(100, 100)
if inside.R < 0.8 {
t.Errorf("Expected red inside clip, got R=%.2f G=%.2f B=%.2f", inside.R, inside.G, inside.B)
}
// Pixel at the corner (50, 50) should be clipped away by the rounded corner.
// The corner radius is 15px, so (50, 50) is in the clipped corner area.
corner := dc.pixmap.GetPixel(51, 51)
if corner.R > 0.5 {
t.Errorf("Expected clipped corner at (51, 51), got R=%.2f (should be low due to rounded clip)", corner.R)
}
// Pixel outside the clip entirely should be transparent/background.
outside := dc.pixmap.GetPixel(10, 10)
if outside.R > 0.1 {
t.Errorf("Expected no image outside clip, got R=%.2f", outside.R)
}
}
// TestDrawImageClipped_Circle verifies that DrawImage respects a circular
// clip region.
func TestDrawImageClipped_Circle(t *testing.T) {
dc := NewContext(200, 200)
dc.Clear()
// Create a green 200x200 image.
img := makeTestImage(t, 200, 200, 0, 255, 0, 255)
dc.Push()
// Clip to a circle centered at (100, 100) with radius 50.
dc.DrawCircle(100, 100, 50)
dc.Clip()
// Draw image at origin — covers entire canvas.
dc.DrawImage(img, 0, 0)
dc.Pop()
// Pixel at the center of the circle should be green.
center := dc.pixmap.GetPixel(100, 100)
if center.G < 0.8 {
t.Errorf("Expected green at circle center, got G=%.2f", center.G)
}
// Pixel well outside the circle should be background (not green).
outside := dc.pixmap.GetPixel(5, 5)
if outside.G > 0.1 {
t.Errorf("Expected no green outside circle clip, got G=%.2f", outside.G)
}
}
// TestDrawImageClipped_NestedClips verifies that DrawImage works with
// nested Push/Pop and multiple clip regions.
func TestDrawImageClipped_NestedClips(t *testing.T) {
dc := NewContext(200, 200)
dc.Clear()
// Create a blue 200x200 image.
img := makeTestImage(t, 200, 200, 0, 0, 255, 255)
dc.Push()
// First clip: large rectangle covering most of the canvas.
dc.ClipRect(20, 20, 160, 160)
dc.Push()
// Second clip: circle in the center (intersects with the rectangle).
dc.DrawCircle(100, 100, 50)
dc.Clip()
// Draw image — should only appear in the intersection of rect and circle.
dc.DrawImage(img, 0, 0)
dc.Pop() // Restore to just the rectangle clip.
dc.Pop() // Restore to no clip.
// Pixel at center (inside both clips) should be blue.
center := dc.pixmap.GetPixel(100, 100)
if center.B < 0.8 {
t.Errorf("Expected blue at center (inside both clips), got B=%.2f", center.B)
}
// Pixel inside the rectangle but outside the circle should NOT be blue.
rectOnly := dc.pixmap.GetPixel(25, 25)
if rectOnly.B > 0.1 {
t.Errorf("Expected no blue at (25,25) — inside rect but outside circle, got B=%.2f", rectOnly.B)
}
// Pixel completely outside both clips should be background.
outside := dc.pixmap.GetPixel(5, 5)
if outside.B > 0.1 {
t.Errorf("Expected no blue at (5,5), got B=%.2f", outside.B)
}
}
// TestDrawImageClipped_NoClip is a regression test ensuring that DrawImage
// without any clip produces the same result as before the refactoring.
func TestDrawImageClipped_NoClip(t *testing.T) {
dc := NewContext(200, 200)
dc.Clear()
// Create red image.
img := makeTestImage(t, 50, 50, 255, 0, 0, 255)
// Draw without any clip.
dc.DrawImage(img, 10, 10)
// Verify pixel at (10, 10) is red.
result := dc.pixmap.GetPixel(10, 10)
if result.R < 0.9 || result.G > 0.1 || result.B > 0.1 {
t.Errorf("Expected red at (10, 10), got R=%.2f G=%.2f B=%.2f", result.R, result.G, result.B)
}
// Verify pixel just inside at (59, 59) is red (10+50-1=59).
inside := dc.pixmap.GetPixel(59, 59)
if inside.R < 0.9 {
t.Errorf("Expected red at (59, 59), got R=%.2f", inside.R)
}
// Verify pixel just outside at (60, 60) is NOT red.
outsideR := dc.pixmap.GetPixel(60, 10)
if outsideR.R > 0.1 {
t.Errorf("Expected no red at (60, 10), got R=%.2f", outsideR.R)
}
}
// TestImagePattern_Anchor verifies that an image pattern with a non-zero
// anchor renders at the correct position.
func TestImagePattern_Anchor(t *testing.T) {
// Create 10x10 red image.
img := makeTestImage(t, 10, 10, 255, 0, 0, 255)
pattern := &ImagePattern{
image: img,
w: 10,
h: 10,
inverse: Identity(),
clamp: true,
}
pattern.SetAnchor(50, 50)
// At the anchor position, should return red.
col := pattern.ColorAt(50, 50)
if col.R < 0.9 || col.A < 0.9 {
t.Errorf("Expected red at anchor (50, 50), got R=%.2f A=%.2f", col.R, col.A)
}
// Outside the image region, should return transparent (clamp mode).
col = pattern.ColorAt(0, 0)
if col.A > 0.01 {
t.Errorf("Expected transparent outside anchor region, got A=%.2f", col.A)
}
// Just past the image region, should also be transparent.
col = pattern.ColorAt(60, 60)
if col.A > 0.01 {
t.Errorf("Expected transparent past anchor region at (60, 60), got A=%.2f", col.A)
}
}
// TestImagePattern_Scale verifies that scaling works on the image pattern.
func TestImagePattern_Scale(t *testing.T) {
// Create 10x10 red image.
img := makeTestImage(t, 10, 10, 255, 0, 0, 255)
pattern := &ImagePattern{
image: img,
w: 10,
h: 10,
inverse: Identity(),
clamp: true,
}
pattern.SetScale(2.0, 2.0)
// At (0, 0), maps to source (0, 0) — should be red.
col := pattern.ColorAt(0, 0)
if col.R < 0.9 {
t.Errorf("Expected red at (0, 0) with 2x scale, got R=%.2f", col.R)
}
// At (19, 19), maps to source (9, 9) — still inside, should be red.
col = pattern.ColorAt(19, 19)
if col.R < 0.9 {
t.Errorf("Expected red at (19, 19) with 2x scale, got R=%.2f", col.R)
}
// At (20, 0), maps to source (10, 0) — outside, should be transparent.
col = pattern.ColorAt(20, 0)
if col.A > 0.01 {
t.Errorf("Expected transparent at (20, 0) with 2x scale, got A=%.2f", col.A)
}
}
// TestImagePattern_Opacity verifies that pattern opacity is applied.
func TestImagePattern_Opacity(t *testing.T) {
img := makeTestImage(t, 10, 10, 255, 0, 0, 255)
pattern := &ImagePattern{
image: img,
w: 10,
h: 10,
opacity: 0.5,
inverse: Identity(),
}
col := pattern.ColorAt(0, 0)
if col.R < 0.9 {
t.Errorf("Expected red channel unchanged, got R=%.2f", col.R)
}
// Alpha should be halved.
if col.A < 0.45 || col.A > 0.55 {
t.Errorf("Expected alpha ~0.5, got A=%.2f", col.A)
}
}
// TestDrawImageClipped_WithTransform verifies that DrawImage respects
// clipping even when a transform is applied.
func TestDrawImageClipped_WithTransform(t *testing.T) {
dc := NewContext(200, 200)
dc.Clear()
// Create red image.
img := makeTestImage(t, 100, 100, 255, 0, 0, 255)
dc.Push()
// Clip to center rectangle.
dc.ClipRect(50, 50, 100, 100)
// Apply translation so image starts at (0, 0) but is shifted to (50, 50).
dc.Translate(50, 50)
// Draw image at origin in transformed space.
dc.DrawImage(img, 0, 0)
dc.Pop()
// Center of the clip should have red pixels.
center := dc.pixmap.GetPixel(100, 100)
if center.R < 0.8 {
t.Errorf("Expected red at center with transform+clip, got R=%.2f", center.R)
}
// Outside the clip should be background.
outside := dc.pixmap.GetPixel(10, 10)
if outside.R > 0.1 {
t.Errorf("Expected no red outside clip with transform, got R=%.2f", outside.R)
}
}
// TestDrawImageRounded verifies the convenience method.
func TestDrawImageRounded(t *testing.T) {
dc := NewContext(200, 200)
dc.Clear()
img := makeTestImage(t, 80, 80, 255, 0, 0, 255)
dc.DrawImageRounded(img, 60, 60, 10)
// Center of the image should be red.
center := dc.pixmap.GetPixel(100, 100)
if center.R < 0.8 {
t.Errorf("Expected red at center of rounded image, got R=%.2f", center.R)
}
// Corner should be clipped (radius=10, so the pixel at (60, 60) is in the rounded corner).
corner := dc.pixmap.GetPixel(61, 61)
if corner.R > 0.5 {
t.Errorf("Expected clipped corner at (61, 61), got R=%.2f", corner.R)
}
}
// TestDrawImageCircular verifies the convenience method.
func TestDrawImageCircular(t *testing.T) {
dc := NewContext(200, 200)
dc.Clear()
img := makeTestImage(t, 100, 100, 0, 0, 255, 255)
dc.DrawImageCircular(img, 100, 100, 40)
// Center should be blue (inside the circle).
center := dc.pixmap.GetPixel(100, 100)
if center.B < 0.8 {
t.Errorf("Expected blue at center of circular image, got B=%.2f", center.B)
}
// Pixel at the edge of the bounding box but outside the circle.
// At 45 degrees from center at distance 40 = (100+28, 100+28) roughly.
// At the actual corner (60, 60) which is outside the circle.
corner := dc.pixmap.GetPixel(62, 62)
if corner.B > 0.3 {
t.Errorf("Expected no blue at corner outside circle, got B=%.2f", corner.B)
}
}
// TestFillClippedSolid verifies that regular solid-color Fill() also
// respects the clip stack after this refactoring.
func TestFillClippedSolid(t *testing.T) {
dc := NewContext(200, 200)
dc.Clear()
dc.Push()
// Clip to a circle.
dc.DrawCircle(100, 100, 50)
dc.Clip()
// Fill the entire canvas with red.
dc.SetRGB(1, 0, 0)
dc.DrawRectangle(0, 0, 200, 200)
dc.Fill()
dc.Pop()
// Center of the circle should be red.
center := dc.pixmap.GetPixel(100, 100)
if center.R < 0.8 {
t.Errorf("Expected red at circle center, got R=%.2f", center.R)
}
// Outside the circle should be background (black from Clear).
outside := dc.pixmap.GetPixel(5, 5)
if outside.R > 0.1 {
t.Errorf("Expected no red outside circle clip, got R=%.2f", outside.R)
}
}
// TestStrokeClipped verifies that Stroke() respects the clip stack.
func TestStrokeClipped(t *testing.T) {
dc := NewContext(200, 200)
dc.Clear()
dc.Push()
// Clip to a small rectangle in the center.
dc.ClipRect(80, 80, 40, 40)
// Stroke a horizontal line across the entire canvas.
dc.SetRGB(1, 0, 0)
dc.SetLineWidth(4)
dc.DrawLine(0, 100, 200, 100)
dc.Stroke()
dc.Pop()
// Inside the clip, the line should be visible.
inside := dc.pixmap.GetPixel(100, 100)
if inside.R < 0.5 {
t.Errorf("Expected red stroke inside clip, got R=%.2f", inside.R)
}
// Outside the clip, the line should NOT be visible.
outside := dc.pixmap.GetPixel(10, 100)
if outside.R > 0.1 {
t.Errorf("Expected no stroke outside clip, got R=%.2f", outside.R)
}
}
// --- ImagePattern setter methods ---
func TestImagePatternSetters(t *testing.T) {
img := makeTestImage(t, 10, 10, 255, 0, 0, 255)
p := &ImagePattern{image: img, w: 10, h: 10, inverse: Identity()}
// SetAnchor
p.SetAnchor(100, 200)
if p.anchorX != 100 || p.anchorY != 200 {
t.Errorf("SetAnchor: got (%f,%f), want (100,200)", p.anchorX, p.anchorY)
}
// SetOpacity
p.SetOpacity(0.75)
if p.opacity != 0.75 {
t.Errorf("SetOpacity: got %f, want 0.75", p.opacity)
}
// SetClamp
p.SetClamp(true)
if !p.clamp {
t.Error("SetClamp(true): expected true")
}
p.SetClamp(false)
if p.clamp {
t.Error("SetClamp(false): expected false")
}
// SetScale
p.SetScale(3.0, 4.0)
if p.scaleX != 3.0 || p.scaleY != 4.0 {
t.Errorf("SetScale: got (%f,%f), want (3,4)", p.scaleX, p.scaleY)
}
}
// --- Rotation / Transform tests (IMG-002, issue #224) ---
// TestDrawImage_Rotation90 reproduces the bug from issue #224:
// DrawImage with 90-degree rotation should fill the rotated rectangle,
// not produce a 1px sliver.
func TestDrawImage_Rotation90(t *testing.T) {
// Create a tall narrow red image (100x400).
srcImg := makeTestImage(t, 100, 400, 255, 0, 0, 255)
// Create a wide short canvas (400x100).
dc := NewContext(400, 100)
dc.ClearWithColor(White)
// Rotate 90 degrees: the 100x400 image should fill the 400x100 canvas.
dc.Translate(400, 0)
dc.Rotate(math.Pi / 2)
dc.DrawImage(srcImg, 0, 0)
result := dc.Image()
// Center pixel should be red.
r, _, _, a := result.At(200, 50).RGBA()
if a == 0 || r == 0 {
t.Errorf("expected red pixel at center (200,50) after 90-degree rotation, got RGBA=(%d,%d,%d,%d)",
r>>8, 0, 0, a>>8)
}
// Near top-left corner should also be red (inside the rotated image).
r2, _, _, a2 := result.At(10, 10).RGBA()
if a2 == 0 || r2 == 0 {
t.Errorf("expected red pixel at (10,10) after 90-degree rotation, got RGBA=(%d,%d,%d,%d)",
r2>>8, 0, 0, a2>>8)
}
}
// TestDrawImage_Rotation45 verifies drawing with a 45-degree rotation.
func TestDrawImage_Rotation45(t *testing.T) {
srcImg := makeTestImage(t, 50, 50, 0, 0, 255, 255)
dc := NewContext(200, 200)
dc.ClearWithColor(White)
// Center the image and rotate 45 degrees.
dc.Translate(100, 100)
dc.Rotate(math.Pi / 4)
dc.DrawImage(srcImg, -25, -25) // center the 50x50 image
result := dc.Image()
// Center pixel (100, 100) should be blue.
_, _, b, a := result.At(100, 100).RGBA()
if a == 0 || b == 0 {
t.Errorf("expected blue pixel at center (100,100) after 45-degree rotation, got B=%d A=%d",
b>>8, a>>8)
}
}
// TestDrawImage_NoTransformRegression verifies that DrawImage without any
// transform continues to work correctly after the affine refactoring.
func TestDrawImage_NoTransformRegression(t *testing.T) {
srcImg := makeTestImage(t, 50, 50, 255, 0, 0, 255)
dc := NewContext(200, 200)
dc.Clear()
dc.DrawImage(srcImg, 10, 10)
// Pixel at (10, 10) should be red.
pixel := dc.pixmap.GetPixel(10, 10)
if pixel.R < 0.9 {
t.Errorf("expected red at (10,10), got R=%.2f", pixel.R)
}
// Pixel at (59, 59) should be red (10+50-1).
pixel2 := dc.pixmap.GetPixel(59, 59)
if pixel2.R < 0.9 {
t.Errorf("expected red at (59,59), got R=%.2f", pixel2.R)
}
// Pixel outside at (60, 10) should NOT be red.
pixel3 := dc.pixmap.GetPixel(60, 10)
if pixel3.R > 0.1 {
t.Errorf("expected no red at (60,10), got R=%.2f", pixel3.R)
}
}
// TestDrawImage_Scale2x verifies DrawImage with Scale(2,2) transform.
func TestDrawImage_Scale2x(t *testing.T) {
srcImg := makeTestImage(t, 50, 50, 0, 255, 0, 255)
dc := NewContext(200, 200)
dc.ClearWithColor(White)
dc.Scale(2, 2)
dc.DrawImage(srcImg, 10, 10)
result := dc.Image()
// In device space, the image at user (10,10) with 2x scale is at device (20,20).
// The 50x50 image scaled 2x covers device pixels (20,20) to (119,119).
r, g, _, a := result.At(20, 20).RGBA()
if a == 0 || g == 0 {
t.Errorf("expected green at device (20,20) with 2x scale, got G=%d A=%d", g>>8, a>>8)
}
_ = r
// Center of scaled image: device (70, 70).
_, g2, _, a2 := result.At(70, 70).RGBA()
if a2 == 0 || g2 == 0 {
t.Errorf("expected green at device (70,70) with 2x scale, got G=%d A=%d", g2>>8, a2>>8)
}
}
// TestDrawImage_TranslateRotate verifies combined translate + rotate.
func TestDrawImage_TranslateRotate(t *testing.T) {
srcImg := makeTestImage(t, 40, 40, 255, 255, 0, 255)
dc := NewContext(200, 200)
dc.ClearWithColor(White)
dc.Translate(100, 100)
dc.Rotate(math.Pi / 6) // 30 degrees
dc.DrawImage(srcImg, -20, -20)
result := dc.Image()
// Center of rotation is at (100, 100). The image is centered there.
// After rotation, (100, 100) should be yellow (the image center).
r, g, _, a := result.At(100, 100).RGBA()
if a == 0 || r == 0 || g == 0 {
t.Errorf("expected yellow at (100,100) with translate+rotate, got R=%d G=%d A=%d",
r>>8, g>>8, a>>8)
}
}
// TestDrawImage_DeviceScale verifies DrawImage with WithDeviceScale(2.0).
func TestDrawImage_DeviceScale(t *testing.T) {