-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform_test.go
More file actions
231 lines (191 loc) · 7.03 KB
/
transform_test.go
File metadata and controls
231 lines (191 loc) · 7.03 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
package xtx
import (
"image"
"image/color"
"testing"
"golang.org/x/image/draw"
)
func TestResize(t *testing.T) {
src := image.NewRGBA(image.Rect(0, 0, 100, 200))
dst := Resize(src, 50, 100)
if dst.Bounds().Dx() != 50 || dst.Bounds().Dy() != 100 {
t.Errorf("Resize: got %dx%d, want 50x100", dst.Bounds().Dx(), dst.Bounds().Dy())
}
}
func TestResize_Upscale(t *testing.T) {
src := image.NewRGBA(image.Rect(0, 0, 10, 10))
// Fill with red.
for y := 0; y < 10; y++ {
for x := 0; x < 10; x++ {
src.SetRGBA(x, y, color.RGBA{R: 255, A: 255})
}
}
dst := Resize(src, 20, 20)
if dst.Bounds().Dx() != 20 || dst.Bounds().Dy() != 20 {
t.Errorf("Resize upscale: got %dx%d, want 20x20", dst.Bounds().Dx(), dst.Bounds().Dy())
}
// Center pixel should still be reddish.
r, _, _, _ := dst.At(10, 10).RGBA()
if r < 0x8000 {
t.Errorf("upscaled red pixel: R=%d, expected high", r)
}
}
func TestResize_MinClamp(t *testing.T) {
src := image.NewRGBA(image.Rect(0, 0, 10, 10))
dst := Resize(src, 0, -5)
if dst.Bounds().Dx() < 1 || dst.Bounds().Dy() < 1 {
t.Errorf("Resize should clamp to minimum 1x1, got %dx%d",
dst.Bounds().Dx(), dst.Bounds().Dy())
}
}
func TestResizeToWidth(t *testing.T) {
src := image.NewRGBA(image.Rect(0, 0, 100, 200))
dst := ResizeToWidth(src, 50)
if dst.Bounds().Dx() != 50 {
t.Errorf("width = %d, want 50", dst.Bounds().Dx())
}
if dst.Bounds().Dy() != 100 {
t.Errorf("height = %d, want 100 (aspect ratio preserved)", dst.Bounds().Dy())
}
}
func TestResizeToWidth_NoOp(t *testing.T) {
src := image.NewRGBA(image.Rect(0, 0, 480, 800))
dst := ResizeToWidth(src, 480)
if dst.Bounds().Dx() != 480 || dst.Bounds().Dy() != 800 {
t.Errorf("no-op resize: got %dx%d, want 480x800", dst.Bounds().Dx(), dst.Bounds().Dy())
}
}
func TestResizeToHeight(t *testing.T) {
src := image.NewRGBA(image.Rect(0, 0, 100, 200))
dst := ResizeToHeight(src, 100)
if dst.Bounds().Dy() != 100 {
t.Errorf("height = %d, want 100", dst.Bounds().Dy())
}
if dst.Bounds().Dx() != 50 {
t.Errorf("width = %d, want 50 (aspect ratio preserved)", dst.Bounds().Dx())
}
}
func TestRotateCW(t *testing.T) {
// Create a 4x2 image with a known pattern.
// Top-left = red, top-right = green, bottom-left = blue, bottom-right = white.
src := image.NewRGBA(image.Rect(0, 0, 4, 2))
src.Set(0, 0, color.RGBA{R: 255, A: 255}) // top-left = red
src.Set(3, 0, color.RGBA{G: 255, A: 255}) // top-right = green
src.Set(0, 1, color.RGBA{B: 255, A: 255}) // bottom-left = blue
src.Set(3, 1, color.White) // bottom-right = white
dst := RotateCW(src)
// 4x2 → 2x4 after 90° CW rotation.
if dst.Bounds().Dx() != 2 || dst.Bounds().Dy() != 4 {
t.Fatalf("RotateCW: got %dx%d, want 2x4", dst.Bounds().Dx(), dst.Bounds().Dy())
}
// After CW rotation:
// top-left of src (red) → top-right of dst (x=1, y=0)
// top-right of src (green) → bottom-right of dst (x=1, y=3)
// bottom-left of src (blue) → top-left of dst (x=0, y=0)
// bottom-right of src (white) → bottom-left of dst (x=0, y=3)
assertColor(t, dst, 1, 0, "red (rotated top-left)", color.RGBA{R: 255, A: 255})
assertColor(t, dst, 1, 3, "green (rotated top-right)", color.RGBA{G: 255, A: 255})
assertColor(t, dst, 0, 0, "blue (rotated bottom-left)", color.RGBA{B: 255, A: 255})
assertColor(t, dst, 0, 3, "white (rotated bottom-right)", color.RGBA{R: 255, G: 255, B: 255, A: 255})
}
func TestRotateCCW(t *testing.T) {
src := image.NewRGBA(image.Rect(0, 0, 4, 2))
src.Set(0, 0, color.RGBA{R: 255, A: 255}) // top-left = red
src.Set(3, 0, color.RGBA{G: 255, A: 255}) // top-right = green
dst := RotateCCW(src)
// 4x2 → 2x4 after 90° CCW rotation.
if dst.Bounds().Dx() != 2 || dst.Bounds().Dy() != 4 {
t.Fatalf("RotateCCW: got %dx%d, want 2x4", dst.Bounds().Dx(), dst.Bounds().Dy())
}
// After CCW rotation:
// top-left of src (red) → bottom-left of dst (x=0, y=3)
// top-right of src (green) → top-left of dst (x=0, y=0)
assertColor(t, dst, 0, 3, "red (rotated CCW)", color.RGBA{R: 255, A: 255})
assertColor(t, dst, 0, 0, "green (rotated CCW)", color.RGBA{G: 255, A: 255})
}
func TestRotateCW_CCW_RoundTrip(t *testing.T) {
src := image.NewRGBA(image.Rect(0, 0, 8, 4))
src.Set(0, 0, color.RGBA{R: 255, A: 255})
src.Set(7, 3, color.RGBA{G: 255, A: 255})
// CW then CCW should return to original.
result := RotateCCW(RotateCW(src))
if result.Bounds().Dx() != 8 || result.Bounds().Dy() != 4 {
t.Fatalf("round-trip: got %dx%d, want 8x4", result.Bounds().Dx(), result.Bounds().Dy())
}
assertColor(t, result, 0, 0, "top-left round-trip", color.RGBA{R: 255, A: 255})
assertColor(t, result, 7, 3, "bottom-right round-trip", color.RGBA{G: 255, A: 255})
}
func TestNewWhitePage(t *testing.T) {
page := NewWhitePage(480, 800)
if page.Bounds().Dx() != 480 || page.Bounds().Dy() != 800 {
t.Fatalf("size = %dx%d, want 480x800", page.Bounds().Dx(), page.Bounds().Dy())
}
// Check corners are white.
corners := [][2]int{{0, 0}, {479, 0}, {0, 799}, {479, 799}}
for _, c := range corners {
r, g, b, a := page.At(c[0], c[1]).RGBA()
if r != 0xffff || g != 0xffff || b != 0xffff || a != 0xffff {
t.Errorf("pixel (%d,%d) = (%x,%x,%x,%x), want all 0xffff", c[0], c[1], r, g, b, a)
}
}
}
// assertColor checks that the pixel at (x,y) matches the expected RGBA color.
func assertColor(t *testing.T, img *image.RGBA, x, y int, label string, want color.RGBA) {
t.Helper()
got := img.RGBAAt(x, y)
if got != want {
t.Errorf("%s at (%d,%d): got %v, want %v", label, x, y, got, want)
}
}
// --- Scaler comparison benchmarks ---
// makeGradient creates a test image with a horizontal gray gradient.
func makeGradient(w, h int) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, w, h))
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
v := uint8(x * 255 / w)
img.SetRGBA(x, y, color.RGBA{R: v, G: v, B: v, A: 255})
}
}
return img
}
func TestResizeScalers_Quality(t *testing.T) {
src := makeGradient(720, 200)
scalers := []struct {
name string
scaler draw.Interpolator
}{
{"ApproxBiLinear", draw.ApproxBiLinear},
{"BiLinear", draw.BiLinear},
{"CatmullRom", draw.CatmullRom},
}
for _, tc := range scalers {
dst := image.NewRGBA(image.Rect(0, 0, 480, 133))
tc.scaler.Scale(dst, dst.Bounds(), src, src.Bounds(), draw.Over, nil)
// Gradient preserved: left dark, right light.
rLeft, _, _, _ := dst.At(10, 66).RGBA()
rRight, _, _, _ := dst.At(470, 66).RGBA()
if rRight <= rLeft {
t.Errorf("%s: gradient not preserved, left=%d right=%d", tc.name, rLeft, rRight)
}
}
}
// Benchmarks: webtoon page resize (720×4000 → 480×2666).
func BenchmarkResize_ApproxBiLinear(b *testing.B) {
benchmarkScaler(b, draw.ApproxBiLinear)
}
func BenchmarkResize_BiLinear(b *testing.B) {
benchmarkScaler(b, draw.BiLinear)
}
func BenchmarkResize_CatmullRom(b *testing.B) {
benchmarkScaler(b, draw.CatmullRom)
}
func benchmarkScaler(b *testing.B, scaler draw.Interpolator) {
src := makeGradient(720, 4000)
dstW, dstH := 480, 2666
b.ResetTimer()
for i := 0; i < b.N; i++ {
dst := image.NewRGBA(image.Rect(0, 0, dstW, dstH))
scaler.Scale(dst, dst.Bounds(), src, src.Bounds(), draw.Over, nil)
}
}