forked from willnorris/imageproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform_test.go
More file actions
542 lines (485 loc) · 18.7 KB
/
transform_test.go
File metadata and controls
542 lines (485 loc) · 18.7 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
// Copyright 2013 Google LLC. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package imageproxy
import (
"bufio"
"bytes"
"encoding/base64"
"fmt"
"image"
"image/color"
"image/draw"
"image/gif"
"image/jpeg"
"image/png"
"io"
"os"
"reflect"
"testing"
"golang.org/x/image/bmp"
"github.com/disintegration/imaging"
)
var (
red = color.NRGBA{255, 0, 0, 255}
green = color.NRGBA{0, 255, 0, 255}
blue = color.NRGBA{0, 0, 255, 255}
yellow = color.NRGBA{255, 255, 0, 255}
)
// newImage creates a new NRGBA image with the specified dimensions and pixel
// color data. If the length of pixels is 1, the entire image is filled with
// that color.
func newImage(w, h int, pixels ...color.Color) image.Image {
m := image.NewNRGBA(image.Rect(0, 0, w, h))
if len(pixels) == 1 {
draw.Draw(m, m.Bounds(), &image.Uniform{pixels[0]}, image.ZP, draw.Src)
} else {
for i, p := range pixels {
m.Set(i%w, i/w, p)
}
}
return m
}
func TestResizeParams(t *testing.T) {
src := image.NewNRGBA(image.Rect(0, 0, 64, 128))
tests := []struct {
opt Options
w, h int
resize bool
}{
{Options{Width: 0.5}, 32, 0, true},
{Options{Height: 0.5}, 0, 64, true},
{Options{Width: 0.5, Height: 0.5}, 32, 64, true},
{Options{Width: 100, Height: 200}, 0, 0, false},
{Options{Width: 100, Height: 200, ScaleUp: true}, 100, 200, true},
{Options{Width: 64}, 0, 0, false},
{Options{Height: 128}, 0, 0, false},
}
for _, tt := range tests {
w, h, resize := resizeParams(src, tt.opt)
if w != tt.w || h != tt.h || resize != tt.resize {
t.Errorf("resizeParams(%v) returned (%d,%d,%t), want (%d,%d,%t)", tt.opt, w, h, resize, tt.w, tt.h, tt.resize)
}
}
}
func TestCropParams(t *testing.T) {
src := image.NewNRGBA(image.Rect(0, 0, 64, 128))
tests := []struct {
opt Options
x0, y0, x1, y1 int
}{
{Options{CropWidth: 10, CropHeight: 0}, 0, 0, 10, 128},
{Options{CropWidth: 0, CropHeight: 10}, 0, 0, 64, 10},
{Options{CropWidth: -1, CropHeight: -1}, 0, 0, 64, 128},
{Options{CropWidth: 50, CropHeight: 100}, 0, 0, 50, 100},
{Options{CropWidth: 100, CropHeight: 100}, 0, 0, 64, 100},
{Options{CropX: 50, CropY: 100}, 50, 100, 64, 128},
{Options{CropX: 50, CropY: 100, CropWidth: 100, CropHeight: 150}, 50, 100, 64, 128},
{Options{CropX: -50, CropY: -50}, 14, 78, 64, 128},
{Options{CropY: 0.5, CropWidth: 0.5}, 0, 64, 32, 128},
{Options{Width: 10, Height: 10, SmartCrop: true}, 0, 0, 64, 64},
}
for _, tt := range tests {
want := image.Rect(tt.x0, tt.y0, tt.x1, tt.y1)
got := cropParams(src, tt.opt)
if !got.Eq(want) {
t.Errorf("cropParams(%v) returned %v, want %v", tt.opt, got, want)
}
}
}
func TestTransform(t *testing.T) {
src := newImage(2, 2, red, green, blue, yellow)
buf := new(bytes.Buffer)
png.Encode(buf, src)
tests := []struct {
name string
encode func(io.Writer, image.Image)
exactOutput bool // whether input and output should match exactly
}{
{"bmp", func(w io.Writer, m image.Image) { bmp.Encode(w, m) }, true},
{"gif", func(w io.Writer, m image.Image) { gif.Encode(w, m, nil) }, true},
{"jpeg", func(w io.Writer, m image.Image) { jpeg.Encode(w, m, nil) }, false},
{"png", func(w io.Writer, m image.Image) { png.Encode(w, m) }, false},
}
for _, tt := range tests {
buf := new(bytes.Buffer)
tt.encode(buf, src)
in := buf.Bytes()
out, err := Transform(in, emptyOptions)
if err != nil {
t.Errorf("Transform with encoder %s returned unexpected error: %v", tt.name, err)
}
if tt.exactOutput && !reflect.DeepEqual(in, out) {
t.Errorf("Transform with with encoder %s with empty options returned modified result", tt.name)
}
out, err = Transform(in, Options{Width: -1, Height: -1})
if err != nil {
t.Errorf("Transform with encoder %s returned unexpected error: %v", tt.name, err)
}
if len(out) == 0 {
t.Errorf("Transform with encoder %s returned empty bytes", tt.name)
}
if tt.exactOutput && !reflect.DeepEqual(in, out) {
t.Errorf("Transform with encoder %s with noop Options returned modified result", tt.name)
}
}
if _, err := Transform([]byte{}, Options{Width: 1}); err == nil {
t.Errorf("Transform with invalid image input did not return expected err")
}
}
func TestTransform_InvalidFormat(t *testing.T) {
src := newImage(2, 2, red, green, blue, yellow)
buf := new(bytes.Buffer)
png.Encode(buf, src)
_, err := Transform(buf.Bytes(), Options{Format: "invalid"})
if err == nil {
t.Errorf("Transform with invalid format did not return expected error")
}
}
// Test that each of the eight EXIF orientations is applied to the transformed
// image appropriately.
func TestTransform_EXIF(t *testing.T) {
ref := newImage(2, 2, red, green, blue, yellow)
// reference image encoded as TIF, with each of the 8 EXIF orientations
// applied in reverse and the EXIF tag set. When orientation is
// applied, each should display as the ref image.
tests := []string{
"SUkqAAgAAAAOAAABAwABAAAAAgAAAAEBAwABAAAAAgAAAAIBAwAEAAAAtgAAAAMBAwABAAAACAAAAAYBAwABAAAAAgAAABEBBAABAAAAzgAAABIBAwABAAAAAQAAABUBAwABAAAABAAAABYBAwABAAAAAgAAABcBBAABAAAAGQAAABoBBQABAAAAvgAAABsBBQABAAAAxgAAACgBAwABAAAAAgAAAFIBAwABAAAAAgAAAAAAAAAIAAgACAAIAEgAAAABAAAASAAAAAEAAAB4nPrPwPAfDBn+////n+E/IAAA//9DzAj4AA==", // Orientation=1
"SUkqAAgAAAAOAAABAwABAAAAAgAAAAEBAwABAAAAAgAAAAIBAwAEAAAAtgAAAAMBAwABAAAACAAAAAYBAwABAAAAAgAAABEBBAABAAAAzgAAABIBAwABAAAAAgAAABUBAwABAAAABAAAABYBAwABAAAAAgAAABcBBAABAAAAGQAAABoBBQABAAAAvgAAABsBBQABAAAAxgAAACgBAwABAAAAAgAAAFIBAwABAAAAAgAAAAAAAAAIAAgACAAIAEgAAAABAAAASAAAAAEAAAB4nGL4z/D/PwPD////GcAUIAAA//9HyAj4AA==", // Orientation=2
"SUkqAAgAAAAOAAABAwABAAAAAgAAAAEBAwABAAAAAgAAAAIBAwAEAAAAtgAAAAMBAwABAAAACAAAAAYBAwABAAAAAgAAABEBBAABAAAAzgAAABIBAwABAAAAAwAAABUBAwABAAAABAAAABYBAwABAAAAAgAAABcBBAABAAAAFwAAABoBBQABAAAAvgAAABsBBQABAAAAxgAAACgBAwABAAAAAgAAAFIBAwABAAAAAgAAAAAAAAAIAAgACAAIAEgAAAABAAAASAAAAAEAAAB4nPr/n+E/AwOY/A9iAAIAAP//T8AI+AA=", // Orientation=3
"SUkqAAgAAAAOAAABAwABAAAAAgAAAAEBAwABAAAAAgAAAAIBAwAEAAAAtgAAAAMBAwABAAAACAAAAAYBAwABAAAAAgAAABEBBAABAAAAzgAAABIBAwABAAAABAAAABUBAwABAAAABAAAABYBAwABAAAAAgAAABcBBAABAAAAGgAAABoBBQABAAAAvgAAABsBBQABAAAAxgAAACgBAwABAAAAAgAAAFIBAwABAAAAAgAAAAAAAAAIAAgACAAIAEgAAAABAAAASAAAAAEAAAB4nGJg+P///3+G//8ZGP6DICAAAP//S8QI+A==", // Orientation=4
"SUkqAAgAAAAOAAABAwABAAAAAgAAAAEBAwABAAAAAgAAAAIBAwAEAAAAtgAAAAMBAwABAAAACAAAAAYBAwABAAAAAgAAABEBBAABAAAAzgAAABIBAwABAAAABQAAABUBAwABAAAABAAAABYBAwABAAAAAgAAABcBBAABAAAAGAAAABoBBQABAAAAvgAAABsBBQABAAAAxgAAACgBAwABAAAAAgAAAFIBAwABAAAAAgAAAAAAAAAIAAgACAAIAEgAAAABAAAASAAAAAEAAAB4nPrPwABC/xn+M/wHkYAAAAD//0PMCPg=", // Orientation=5
"SUkqAAgAAAAOAAABAwABAAAAAgAAAAEBAwABAAAAAgAAAAIBAwAEAAAAtgAAAAMBAwABAAAACAAAAAYBAwABAAAAAgAAABEBBAABAAAAzgAAABIBAwABAAAABgAAABUBAwABAAAABAAAABYBAwABAAAAAgAAABcBBAABAAAAGAAAABoBBQABAAAAvgAAABsBBQABAAAAxgAAACgBAwABAAAAAgAAAFIBAwABAAAAAgAAAAAAAAAIAAgACAAIAEgAAAABAAAASAAAAAEAAAB4nGL4z/D/PwgzMIDQf0AAAAD//0vECPg=", // Orientation=6
"SUkqAAgAAAAOAAABAwABAAAAAgAAAAEBAwABAAAAAgAAAAIBAwAEAAAAtgAAAAMBAwABAAAACAAAAAYBAwABAAAAAgAAABEBBAABAAAAzgAAABIBAwABAAAABwAAABUBAwABAAAABAAAABYBAwABAAAAAgAAABcBBAABAAAAFgAAABoBBQABAAAAvgAAABsBBQABAAAAxgAAACgBAwABAAAAAgAAAFIBAwABAAAAAgAAAAAAAAAIAAgACAAIAEgAAAABAAAASAAAAAEAAAB4nPr/nwECGf7/BxGAAAAA//9PwAj4", // Orientation=7
"SUkqAAgAAAAOAAABAwABAAAAAgAAAAEBAwABAAAAAgAAAAIBAwAEAAAAtgAAAAMBAwABAAAACAAAAAYBAwABAAAAAgAAABEBBAABAAAAzgAAABIBAwABAAAACAAAABUBAwABAAAABAAAABYBAwABAAAAAgAAABcBBAABAAAAFQAAABoBBQABAAAAvgAAABsBBQABAAAAxgAAACgBAwABAAAAAgAAAFIBAwABAAAAAgAAAAAAAAAIAAgACAAIAEgAAAABAAAASAAAAAEAAAB4nGJg+P//P4QAQ0AAAAD//0fICPgA", // Orientation=8
}
for _, src := range tests {
in, err := base64.StdEncoding.DecodeString(src)
if err != nil {
t.Errorf("error decoding source: %v", err)
}
out, err := Transform(in, Options{Height: -1, Width: -1, Format: "tiff"})
if err != nil {
t.Errorf("Transform(%q) returned error: %v", src, err)
}
d, _, err := image.Decode(bytes.NewReader(out))
if err != nil {
t.Errorf("error decoding transformed image: %v", err)
}
// construct new image with same colors as decoded image for easy comparison
got := newImage(2, 2, d.At(0, 0), d.At(1, 0), d.At(0, 1), d.At(1, 1))
if want := ref; !reflect.DeepEqual(got, want) {
t.Errorf("Transform(%v) returned image %#v, want %#v", src, got, want)
}
}
}
// Test that EXIF orientation and any additional transforms don't conflict.
// This is tested with orientation=7, which involves both a rotation and a
// flip, combined with an additional rotation transform.
func TestTransform_EXIF_Rotate(t *testing.T) {
// base64-encoded TIF image (2x2 yellow green blue red) with EXIF
// orientation=7. When orientation applied, displays as (2x2 red green
// blue yellow).
src := "SUkqAAgAAAAOAAABAwABAAAAAgAAAAEBAwABAAAAAgAAAAIBAwAEAAAAtgAAAAMBAwABAAAACAAAAAYBAwABAAAAAgAAABEBBAABAAAAzgAAABIBAwABAAAABwAAABUBAwABAAAABAAAABYBAwABAAAAAgAAABcBBAABAAAAFgAAABoBBQABAAAAvgAAABsBBQABAAAAxgAAACgBAwABAAAAAgAAAFIBAwABAAAAAgAAAAAAAAAIAAgACAAIAEgAAAABAAAASAAAAAEAAAB4nPr/nwECGf7/BxGAAAAA//9PwAj4"
in, err := base64.StdEncoding.DecodeString(src)
if err != nil {
t.Errorf("error decoding source: %v", err)
}
out, err := Transform(in, Options{Rotate: 90, Format: "tiff"})
if err != nil {
t.Errorf("Transform(%q) returned error: %v", src, err)
}
d, _, err := image.Decode(bytes.NewReader(out))
if err != nil {
t.Errorf("error decoding transformed image: %v", err)
}
// construct new image with same colors as decoded image for easy comparison
got := newImage(2, 2, d.At(0, 0), d.At(1, 0), d.At(0, 1), d.At(1, 1))
want := newImage(2, 2, green, yellow, red, blue)
if !reflect.DeepEqual(got, want) {
t.Errorf("Transform(%v) returned image %#v, want %#v", src, got, want)
}
}
func TestTransformImage(t *testing.T) {
// ref is a 2x2 reference image containing four colors
ref := newImage(2, 2, red, green, blue, yellow)
// cropRef is a 4x4 image with four colors, each in 2x2 quarter
cropRef := newImage(4, 4, red, red, green, green, red, red, green, green, blue, blue, yellow, yellow, blue, blue, yellow, yellow)
// use simpler filter while testing that won't skew colors
resampleFilter = imaging.Box
tests := []struct {
src image.Image // source image to transform
opt Options // options to apply during transform
want image.Image // expected transformed image
}{
// no transformation
{ref, emptyOptions, ref},
// rotations
{ref, Options{Rotate: 45}, ref}, // invalid rotation is a noop
{ref, Options{Rotate: 360}, ref},
{ref, Options{Rotate: 90}, newImage(2, 2, green, yellow, red, blue)},
{ref, Options{Rotate: 180}, newImage(2, 2, yellow, blue, green, red)},
{ref, Options{Rotate: 270}, newImage(2, 2, blue, red, yellow, green)},
{ref, Options{Rotate: 630}, newImage(2, 2, blue, red, yellow, green)},
{ref, Options{Rotate: -90}, newImage(2, 2, blue, red, yellow, green)},
// flips
{
ref,
Options{FlipHorizontal: true},
newImage(2, 2, green, red, yellow, blue),
},
{
ref,
Options{FlipVertical: true},
newImage(2, 2, blue, yellow, red, green),
},
{
ref,
Options{FlipHorizontal: true, FlipVertical: true},
newImage(2, 2, yellow, blue, green, red),
},
{
ref,
Options{Rotate: 90, FlipHorizontal: true},
newImage(2, 2, yellow, green, blue, red),
},
// resizing
{ // can't resize larger than original image
ref,
Options{Width: 100, Height: 100},
ref,
},
{ // can resize larger than original image
ref,
Options{Width: 4, Height: 4, ScaleUp: true},
newImage(4, 4, red, red, green, green, red, red, green, green, blue, blue, yellow, yellow, blue, blue, yellow, yellow),
},
{ // invalid values
ref,
Options{Width: -1, Height: -1},
ref,
},
{ // absolute values
newImage(100, 100, red),
Options{Width: 1, Height: 1},
newImage(1, 1, red),
},
{ // percentage values
newImage(100, 100, red),
Options{Width: 0.50, Height: 0.25},
newImage(50, 25, red),
},
{ // only width specified, proportional height
newImage(100, 50, red),
Options{Width: 50},
newImage(50, 25, red),
},
{ // only height specified, proportional width
newImage(100, 50, red),
Options{Height: 25},
newImage(50, 25, red),
},
{ // resize in one dimenstion, with cropping
newImage(4, 2, red, red, blue, blue, red, red, blue, blue),
Options{Width: 4, Height: 1},
newImage(4, 1, red, red, blue, blue),
},
{ // resize in two dimensions, with cropping
newImage(4, 2, red, red, blue, blue, red, red, blue, blue),
Options{Width: 2, Height: 2},
newImage(2, 2, red, blue, red, blue),
},
{ // resize in two dimensions, fit option prevents cropping
newImage(4, 2, red, red, blue, blue, red, red, blue, blue),
Options{Width: 2, Height: 2, Fit: true},
newImage(2, 1, red, blue),
},
{ // scale image explicitly
newImage(4, 2, red, red, blue, blue, red, red, blue, blue),
Options{Width: 2, Height: 1},
newImage(2, 1, red, blue),
},
// combinations of options
{
newImage(4, 2, red, red, blue, blue, red, red, blue, blue),
Options{Width: 2, Height: 1, Fit: true, FlipHorizontal: true, Rotate: 90},
newImage(1, 2, blue, red),
},
// crop
{ // quarter ((0, 0), (2, 2)) -> red
cropRef,
Options{CropHeight: 2, CropWidth: 2},
newImage(2, 2, red, red, red, red),
},
{ // quarter ((2, 0), (4, 2)) -> green
cropRef,
Options{CropHeight: 2, CropWidth: 2, CropX: 2},
newImage(2, 2, green, green, green, green),
},
{ // quarter ((0, 2), (2, 4)) -> blue
cropRef,
Options{CropHeight: 2, CropWidth: 2, CropX: 0, CropY: 2},
newImage(2, 2, blue, blue, blue, blue),
},
{ // quarter ((2, 2), (4, 4)) -> yellow
cropRef,
Options{CropHeight: 2, CropWidth: 2, CropX: 2, CropY: 2},
newImage(2, 2, yellow, yellow, yellow, yellow),
},
// percentage-based resize in addition to rectangular crop
{
newImage(12, 12, red),
Options{Width: 0.5, Height: 0.5, CropWidth: 8, CropHeight: 8},
newImage(6, 6, red),
},
}
for _, tt := range tests {
if got := transformImage(tt.src, tt.opt); !reflect.DeepEqual(got, tt.want) {
t.Errorf("transformImage(%v, %v) returned image %#v, want %#v", tt.src, tt.opt, got, tt.want)
}
}
}
// Test the specific case for rectangular canvas with width specified
func TestTransformImage_RectangleWithWidth(t *testing.T) {
// Create a tall image (height > width) to test the specific case
tallImage := newImage(100, 200, red)
// Test with width=250, Rectangle=true
// Should create a 250x350 canvas (5:7 ratio) and fit the image within it
result := transformImage(tallImage, Options{Width: 250, Rectangle: true})
// Check that the result has the correct 5:7 ratio dimensions
expectedWidth := 250
expectedHeight := int(float64(250) * 7.0 / 5.0) // 350
if result.Bounds().Dx() != expectedWidth {
t.Errorf("Rectangle transformation with width=250: got width %d, want %d",
result.Bounds().Dx(), expectedWidth)
}
if result.Bounds().Dy() != expectedHeight {
t.Errorf("Rectangle transformation with width=250: got height %d, want %d",
result.Bounds().Dy(), expectedHeight)
}
// Test with a wide image (width > height)
wideImage := newImage(300, 100, blue)
result2 := transformImage(wideImage, Options{Width: 250, Rectangle: true})
// Should still create a 250x350 canvas
if result2.Bounds().Dx() != expectedWidth {
t.Errorf("Rectangle transformation with width=250 (wide image): got width %d, want %d",
result2.Bounds().Dx(), expectedWidth)
}
if result2.Bounds().Dy() != expectedHeight {
t.Errorf("Rectangle transformation with width=250 (wide image): got height %d, want %d",
result2.Bounds().Dy(), expectedHeight)
}
}
// Test comprehensive rectangle canvas functionality
func TestRectangleCanvas(t *testing.T) {
tests := []struct {
name string
image image.Image
options Options
wantW int
wantH int
}{
{
name: "Tall image with width specified",
image: newImage(100, 200, red),
options: Options{Width: 250, Rectangle: true},
wantW: 250,
wantH: 350, // 250 * 7/5
},
{
name: "Wide image with width specified",
image: newImage(300, 100, blue),
options: Options{Width: 250, Rectangle: true},
wantW: 250,
wantH: 350,
},
{
name: "Image with height specified",
image: newImage(100, 300, green),
options: Options{Height: 350, Rectangle: true},
wantW: 250, // 350 * 5/7
wantH: 350,
},
{
name: "Rectangle without dimensions",
image: newImage(100, 200, red),
options: Options{Rectangle: true},
wantW: 142, // int(200 * 5/7)
wantH: 200,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := transformImage(tt.image, tt.options)
if result.Bounds().Dx() != tt.wantW {
t.Errorf("%s: got width %d, want %d", tt.name, result.Bounds().Dx(), tt.wantW)
}
if result.Bounds().Dy() != tt.wantH {
t.Errorf("%s: got height %d, want %d", tt.name, result.Bounds().Dy(), tt.wantH)
}
})
}
}
func TestTWMChanges(t *testing.T) {
src, err := getTwmTestImage("test-images/unanime.png")
if err != nil {
t.Errorf("test failed getting image: %v", err)
t.Fail()
}
tests := []struct {
name string
matchOutFilename string
options Options
}{
{"jpeg 400x", "test-images/unanime-400x.png", Options{Width: 400}},
{"jpeg 400x,200ml", "test-images/unanime-400x,200ml.png", Options{Width: 400, IndicatorSize: optIndicatorSize200ml, Square: true}},
}
for _, tt := range tests {
shouldOut, err := getTwmTestImage(tt.matchOutFilename)
if err != nil {
t.Errorf("test failed getting out image %s: %v", tt.matchOutFilename, err)
continue
}
out, err := Transform(src, tt.options)
if err != nil {
t.Errorf("Transform with encoder %s returned unexpected error: %v", tt.name, err)
}
if !reflect.DeepEqual(shouldOut, out) {
TEMPORARYWriteTestImage("test-images/tempout.png", out)
t.Errorf("Transform with with encoder %s with empty options returned modified result", tt.name)
}
fmt.Println("done", tt.name)
}
}
func getTwmTestImage(name string) ([]byte, error) {
src, err := os.Open(name)
if err != nil {
return nil, err
}
stat, err := src.Stat()
if err != nil {
return nil, err
}
bs := make([]byte, stat.Size())
bufio.NewReader(src).Read(bs)
return bs, nil
}
func TEMPORARYWriteTestImage(name string, bs []byte) {
f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
fmt.Println(err)
return
}
n, err := f.Write(bs)
fmt.Println(n, err)
}