-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprism_test.go
More file actions
317 lines (273 loc) · 7.73 KB
/
Copy pathprism_test.go
File metadata and controls
317 lines (273 loc) · 7.73 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
package prism
import (
"bytes"
"image"
"image/color"
"image/jpeg"
"image/png"
"testing"
)
func TestNew(t *testing.T) {
img := New(100, 50, color.NRGBA{R: 255, G: 0, B: 0, A: 255})
if img.Bounds().Dx() != 100 || img.Bounds().Dy() != 50 {
t.Fatalf("size: got %dx%d, want 100x50", img.Bounds().Dx(), img.Bounds().Dy())
}
c := img.NRGBAAt(0, 0)
if c.R != 255 || c.G != 0 || c.B != 0 || c.A != 255 {
t.Fatalf("color: got %v, want red", c)
}
}
func TestNew_Zero(t *testing.T) {
img := New(0, 0, color.Black)
if img.Bounds().Dx() != 0 || img.Bounds().Dy() != 0 {
t.Fatalf("expected empty image for zero size")
}
}
func TestClone(t *testing.T) {
src := New(10, 10, color.NRGBA{R: 128, G: 64, B: 32, A: 200})
dst := Clone(src)
if dst.Bounds().Dx() != 10 || dst.Bounds().Dy() != 10 {
t.Fatalf("size mismatch")
}
c := dst.NRGBAAt(5, 5)
if c.R != 128 || c.G != 64 || c.B != 32 || c.A != 200 {
t.Fatalf("color: got %v, want {128,64,32,200}", c)
}
}
func TestClone_Nil(t *testing.T) {
dst := Clone(nil)
if dst == nil {
t.Fatal("expected non-nil empty image")
}
}
func TestFormatFromExtension(t *testing.T) {
tests := []struct {
ext string
format Format
ok bool
}{
{".jpg", JPEG, true},
{".jpeg", JPEG, true},
{".JPG", JPEG, true},
{".png", PNG, true},
{".gif", GIF, true},
{".tiff", TIFF, true},
{".tif", TIFF, true},
{".bmp", BMP, true},
{".xyz", 0, false},
}
for _, tt := range tests {
f, err := FormatFromExtension(tt.ext)
if tt.ok {
if err != nil {
t.Errorf("ext=%s: unexpected error: %v", tt.ext, err)
}
if f != tt.format {
t.Errorf("ext=%s: got %d, want %d", tt.ext, f, tt.format)
}
} else {
if err == nil {
t.Errorf("ext=%s: expected error", tt.ext)
}
}
}
}
func TestFormatFromFilename(t *testing.T) {
f, err := FormatFromFilename("photo.png")
if err != nil || f != PNG {
t.Fatalf("got %d/%v, want PNG", f, err)
}
}
func TestEncodeDecodePNG(t *testing.T) {
src := New(8, 8, color.NRGBA{R: 100, G: 200, B: 50, A: 255})
var buf bytes.Buffer
if err := Encode(&buf, src, PNG); err != nil {
t.Fatalf("encode: %v", err)
}
img, err := Decode(&buf)
if err != nil {
t.Fatalf("decode: %v", err)
}
if img.Bounds().Dx() != 8 || img.Bounds().Dy() != 8 {
t.Fatalf("size mismatch after roundtrip")
}
}
func TestEncodeDecodeJPEG(t *testing.T) {
src := New(8, 8, color.NRGBA{R: 100, G: 200, B: 50, A: 255})
var buf bytes.Buffer
if err := Encode(&buf, src, JPEG, JPEGQuality(90)); err != nil {
t.Fatalf("encode: %v", err)
}
img, err := Decode(&buf)
if err != nil {
t.Fatalf("decode: %v", err)
}
if img.Bounds().Dx() != 8 || img.Bounds().Dy() != 8 {
t.Fatalf("size mismatch after roundtrip")
}
}
func TestEncodeDecodeGIF(t *testing.T) {
src := New(8, 8, color.NRGBA{R: 255, G: 0, B: 0, A: 255})
var buf bytes.Buffer
if err := Encode(&buf, src, GIF, GIFNumColors(16)); err != nil {
t.Fatalf("encode: %v", err)
}
img, err := Decode(&buf)
if err != nil {
t.Fatalf("decode: %v", err)
}
if img.Bounds().Dx() != 8 || img.Bounds().Dy() != 8 {
t.Fatalf("size mismatch after roundtrip")
}
}
func TestMaxImageSize(t *testing.T) {
// Create a small PNG in memory.
src := New(100, 100, color.White)
var buf bytes.Buffer
png.Encode(&buf, src)
// Should succeed with large limit.
_, err := Decode(bytes.NewReader(buf.Bytes()), MaxImageSize(100000))
if err != nil {
t.Fatalf("expected success with large limit: %v", err)
}
// Should fail with small limit and return ErrImageTooLarge.
_, err = Decode(bytes.NewReader(buf.Bytes()), MaxImageSize(100))
if err == nil {
t.Fatal("expected error with small limit")
}
if err != ErrImageTooLarge {
t.Fatalf("expected ErrImageTooLarge, got: %v", err)
}
}
func TestScannerBoundsCheck(t *testing.T) {
// Create an NRGBA image with known bounds.
img := image.NewNRGBA(image.Rect(10, 10, 20, 20))
for i := range img.Pix {
img.Pix[i] = 128
}
s := newScanner(img)
// Scan within bounds — should not panic.
dst := make([]byte, 10*10*4)
s.scan(10, 10, 20, 20, dst)
// Scan with coordinates exceeding bounds — should not panic.
dst2 := make([]byte, 20*20*4)
s.scan(0, 0, 30, 30, dst2) // way outside bounds — must not panic
}
func TestResampleFilterKernels(t *testing.T) {
// Verify that all filter kernels return 0 outside their support.
filters := []struct {
name string
filter ResampleFilter
}{
{"Box", Box},
{"Linear", Linear},
{"Hermite", Hermite},
{"MitchellNetravali", MitchellNetravali},
{"CatmullRom", CatmullRom},
{"BSpline", BSpline},
{"Gaussian", Gaussian},
{"Bartlett", Bartlett},
{"Lanczos", Lanczos},
{"Hann", Hann},
{"Hamming", Hamming},
{"Blackman", Blackman},
{"Welch", Welch},
{"Cosine", Cosine},
}
for _, f := range filters {
// Should be non-zero near 0.
v := f.filter.Kernel(0)
if v == 0 {
t.Errorf("%s: kernel(0) = 0, want non-zero", f.name)
}
// Should be zero far outside support.
v = f.filter.Kernel(f.filter.Support + 1)
if v != 0 {
t.Errorf("%s: kernel(%f) = %f, want 0", f.name, f.filter.Support+1, v)
}
}
}
func TestEncodeDecodeBMP(t *testing.T) {
src := New(8, 8, color.NRGBA{R: 100, G: 200, B: 50, A: 255})
var buf bytes.Buffer
if err := Encode(&buf, src, BMP); err != nil {
t.Fatalf("encode: %v", err)
}
img, err := Decode(&buf)
if err != nil {
t.Fatalf("decode: %v", err)
}
if img.Bounds().Dx() != 8 || img.Bounds().Dy() != 8 {
t.Fatalf("size mismatch after roundtrip")
}
}
func TestEncodeDecodeTIFF(t *testing.T) {
src := New(8, 8, color.NRGBA{R: 100, G: 200, B: 50, A: 255})
var buf bytes.Buffer
if err := Encode(&buf, src, TIFF); err != nil {
t.Fatalf("encode: %v", err)
}
img, err := Decode(&buf)
if err != nil {
t.Fatalf("decode: %v", err)
}
if img.Bounds().Dx() != 8 || img.Bounds().Dy() != 8 {
t.Fatalf("size mismatch after roundtrip")
}
}
func TestEncodeWEBP_Unsupported(t *testing.T) {
src := New(8, 8, color.White)
var buf bytes.Buffer
err := Encode(&buf, src, WEBP)
if err != ErrUnsupportedFormat {
t.Fatalf("expected ErrUnsupportedFormat, got: %v", err)
}
}
func TestExifOrientation6_Rotate90(t *testing.T) {
// Create a 4-wide x 2-tall image
src := New(4, 2, color.NRGBA{R: 100, G: 100, B: 100, A: 255})
// Orientation 6 per EXIF spec = 90 CW rotation, so 4x2 becomes 2x4
result := applyOrientation(src, 6)
if result.Bounds().Dx() != 2 || result.Bounds().Dy() != 4 {
t.Fatalf("orientation 6 should rotate 90 CW (4x2->2x4), got %dx%d",
result.Bounds().Dx(), result.Bounds().Dy())
}
}
func TestExifOrientation8_Rotate270(t *testing.T) {
// Create a 4-wide x 2-tall image
src := New(4, 2, color.NRGBA{R: 100, G: 100, B: 100, A: 255})
// Orientation 8 per EXIF spec = 270 CW rotation, so 4x2 becomes 2x4
result := applyOrientation(src, 8)
if result.Bounds().Dx() != 2 || result.Bounds().Dy() != 4 {
t.Fatalf("orientation 8 should rotate 270 CW (4x2->2x4), got %dx%d",
result.Bounds().Dx(), result.Bounds().Dy())
}
}
func TestNewDimensionOverflow(t *testing.T) {
// Extremely large dimensions should return empty image, not OOM
img := New(1_000_000, 1_000_000, color.White)
if img.Bounds().Dx() != 0 {
t.Fatal("expected empty image for oversized dimensions")
}
}
func TestResizeDimensionOverflow(t *testing.T) {
src := New(4, 4, color.White)
// Extremely large target dimensions should return empty image
dst := Resize(src, 1_000_000, 1_000_000, Lanczos)
if dst.Bounds().Dx() != 0 {
t.Fatal("expected empty image for oversized resize dimensions")
}
}
// Verify decode of various formats by encoding and decoding.
func TestDecodeJPEG(t *testing.T) {
src := New(4, 4, color.NRGBA{R: 200, G: 100, B: 50, A: 255})
var buf bytes.Buffer
jpeg.Encode(&buf, src, nil)
img, err := Decode(&buf)
if err != nil {
t.Fatalf("decode JPEG: %v", err)
}
if img.Bounds().Dx() != 4 {
t.Fatalf("wrong width")
}
}