| 1 |
package gift |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"image" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
func TestResize(t *testing.T) { |
| 10 |
var img0, img1 *image.Gray |
| 11 |
|
| 12 |
|
| 13 |
w, h := 10, 20 |
| 14 |
img0 = image.NewGray(image.Rect(0, 0, w, h)) |
| 15 |
sz := []struct{ w0, h0, w1, h1 int }{ |
| 16 |
{w, h, w, h}, |
| 17 |
{w * 2, h, w * 2, h}, |
| 18 |
{w, h * 2, w, h * 2}, |
| 19 |
{w * 2, h * 2, w * 2, h * 2}, |
| 20 |
{w / 2, h, w / 2, h}, |
| 21 |
{w, 0, w, h}, |
| 22 |
{0, h, w, h}, |
| 23 |
{w * 2, 0, w * 2, h * 2}, |
| 24 |
{0, h / 2, w / 2, h / 2}, |
| 25 |
{0, 0, 0, 0}, |
| 26 |
{1, -1, 0, 0}, |
| 27 |
{-1, 1, 0, 0}, |
| 28 |
} |
| 29 |
rfilters := []Resampling{ |
| 30 |
NearestNeighborResampling, |
| 31 |
BoxResampling, |
| 32 |
LinearResampling, |
| 33 |
CubicResampling, |
| 34 |
LanczosResampling, |
| 35 |
} |
| 36 |
for _, workers := range []int{5, 1} { |
| 37 |
for _, z := range sz { |
| 38 |
for _, f := range rfilters { |
| 39 |
opts := Options{Workers: workers} |
| 40 |
g := NewWithOptions(opts, Resize(z.w0, z.h0, f)) |
| 41 |
img1 = image.NewGray(g.Bounds(img0.Bounds())) |
| 42 |
g.Draw(img1, img0) |
| 43 |
w2, h2 := img1.Bounds().Dx(), img1.Bounds().Dy() |
| 44 |
if w2 != z.w1 || h2 != z.h1 { |
| 45 |
t.Errorf("resize %s %dx%d: expected %dx%d got %dx%d", f, z.w0, z.h0, z.w1, z.h1, w2, h2) |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
img0 = image.NewGray(image.Rect(-1, -1, 4, 1)) |
| 53 |
img0.Pix = []uint8{ |
| 54 |
1, 2, 3, 4, 5, |
| 55 |
6, 7, 8, 0, 1, |
| 56 |
} |
| 57 |
img1Exp := image.NewGray(image.Rect(0, 0, 2, 2)) |
| 58 |
img1Exp.Pix = []uint8{ |
| 59 |
2, 4, |
| 60 |
7, 0, |
| 61 |
} |
| 62 |
f := Resize(2, 2, NearestNeighborResampling) |
| 63 |
img1 = image.NewGray(f.Bounds(img0.Bounds())) |
| 64 |
f.Draw(img1, img0, nil) |
| 65 |
if img1.Bounds().Size() != img1Exp.Bounds().Size() { |
| 66 |
t.Errorf("expected %v got %v", img1Exp.Bounds().Size(), img1.Bounds().Size()) |
| 67 |
} |
| 68 |
if !bytes.Equal(img1Exp.Pix, img1.Pix) { |
| 69 |
t.Errorf("expected %v got %v", img1Exp.Pix, img1.Pix) |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
img0 = image.NewGray(image.Rect(-1, -1, 3, 1)) |
| 74 |
img0.Pix = []uint8{ |
| 75 |
1, 2, 2, 1, |
| 76 |
4, 5, 8, 9, |
| 77 |
} |
| 78 |
img1Exp = image.NewGray(image.Rect(0, 0, 2, 1)) |
| 79 |
img1Exp.Pix = []uint8{ |
| 80 |
3, 5, |
| 81 |
} |
| 82 |
f = Resize(2, 1, BoxResampling) |
| 83 |
img1 = image.NewGray(f.Bounds(img0.Bounds())) |
| 84 |
f.Draw(img1, img0, nil) |
| 85 |
if img1.Bounds().Size() != img1Exp.Bounds().Size() { |
| 86 |
t.Errorf("expected %v got %v", img1Exp.Bounds().Size(), img1.Bounds().Size()) |
| 87 |
} |
| 88 |
if !bytes.Equal(img1Exp.Pix, img1.Pix) { |
| 89 |
t.Errorf("expected %v got %v", img1Exp.Pix, img1.Pix) |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
img0 = &image.Gray{} |
| 94 |
f = Resize(100, 100, BoxResampling) |
| 95 |
img1 = image.NewGray(f.Bounds(img0.Bounds())) |
| 96 |
f.Draw(img1, img0, nil) |
| 97 |
if img1.Bounds().Dx() != 0 || img1.Bounds().Dy() != 0 { |
| 98 |
t.Errorf("empty image resized is not empty: %dx%d", img1.Bounds().Dx(), img1.Bounds().Dy()) |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
for _, f := range rfilters { |
| 103 |
if f.Kernel(f.Support()+0.000001) != 0 { |
| 104 |
t.Errorf("filter %s value outside support != 0", f) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
if sinc(0) != 1 { |
| 110 |
t.Errorf("sinc(0) != 1") |
| 111 |
} |
| 112 |
if bcspline(-2, 0, 0.5) != 0 { |
| 113 |
t.Errorf("bcspline(-2, ...) != 0") |
| 114 |
} |
| 115 |
|
| 116 |
if (resamp{name: "test"}).String() != "test" { |
| 117 |
t.Error("resamplingStruct String() fail") |
| 118 |
} |
| 119 |
|
| 120 |
testData := []struct { |
| 121 |
desc string |
| 122 |
w, h int |
| 123 |
r Resampling |
| 124 |
srcb, dstb image.Rectangle |
| 125 |
srcPix, dstPix []uint8 |
| 126 |
}{ |
| 127 |
{ |
| 128 |
"resize (1, 2 -> 1, 1; box; non-alpha)", |
| 129 |
1, 1, BoxResampling, |
| 130 |
image.Rect(0, 0, 1, 2), |
| 131 |
image.Rect(0, 0, 1, 1), |
| 132 |
[]uint8{ |
| 133 |
0xff, 0x00, 0x00, 0xff, |
| 134 |
0x00, 0xff, 0x00, 0xff, |
| 135 |
}, |
| 136 |
[]uint8{ |
| 137 |
0x80, 0x80, 0x00, 0xff, |
| 138 |
}, |
| 139 |
}, |
| 140 |
{ |
| 141 |
"resize (1, 2 -> 1, 1; box; alpha)", |
| 142 |
1, 1, BoxResampling, |
| 143 |
image.Rect(0, 0, 1, 2), |
| 144 |
image.Rect(0, 0, 1, 1), |
| 145 |
[]uint8{ |
| 146 |
0xff, 0x00, 0x00, 0xff, |
| 147 |
0x00, 0xff, 0x00, 0x00, |
| 148 |
}, |
| 149 |
[]uint8{ |
| 150 |
0xff, 0x00, 0x00, 0x80, |
| 151 |
}, |
| 152 |
}, |
| 153 |
{ |
| 154 |
"resize (1, 2 -> 1, 3; linear; alpha)", |
| 155 |
1, 3, LinearResampling, |
| 156 |
image.Rect(0, 0, 1, 2), |
| 157 |
image.Rect(0, 0, 1, 3), |
| 158 |
[]uint8{ |
| 159 |
0xff, 0x00, 0x00, 0xff, |
| 160 |
0x00, 0xff, 0x00, 0x00, |
| 161 |
}, |
| 162 |
[]uint8{ |
| 163 |
0xff, 0x00, 0x00, 0xff, |
| 164 |
0xff, 0x00, 0x00, 0x80, |
| 165 |
0x00, 0x00, 0x00, 0x00, |
| 166 |
}, |
| 167 |
}, |
| 168 |
} |
| 169 |
|
| 170 |
for _, d := range testData { |
| 171 |
src := image.NewNRGBA(d.srcb) |
| 172 |
src.Pix = d.srcPix |
| 173 |
|
| 174 |
f := Resize(d.w, d.h, d.r) |
| 175 |
dst := image.NewNRGBA(f.Bounds(src.Bounds())) |
| 176 |
f.Draw(dst, src, nil) |
| 177 |
|
| 178 |
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 179 |
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
func TestResizeToFit(t *testing.T) { |
| 185 |
testData := []struct { |
| 186 |
desc string |
| 187 |
w, h int |
| 188 |
r Resampling |
| 189 |
srcb, dstb image.Rectangle |
| 190 |
srcPix, dstPix []uint8 |
| 191 |
}{ |
| 192 |
{ |
| 193 |
"resize to fit (0, 0, nearest)", |
| 194 |
0, 0, NearestNeighborResampling, |
| 195 |
image.Rect(-1, -1, 4, 4), |
| 196 |
image.Rect(0, 0, 0, 0), |
| 197 |
[]uint8{ |
| 198 |
0x00, 0x01, 0x02, 0x03, 0x04, |
| 199 |
0x05, 0x06, 0x07, 0x08, 0x09, |
| 200 |
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 201 |
0x0f, 0x10, 0x11, 0x12, 0x13, |
| 202 |
0x14, 0x15, 0x16, 0x17, 0x18, |
| 203 |
}, |
| 204 |
[]uint8{}, |
| 205 |
}, |
| 206 |
{ |
| 207 |
"resize to fit (1, 1, nearest)", |
| 208 |
1, 1, NearestNeighborResampling, |
| 209 |
image.Rect(-1, -1, 4, 4), |
| 210 |
image.Rect(0, 0, 1, 1), |
| 211 |
[]uint8{ |
| 212 |
0x00, 0x01, 0x02, 0x03, 0x04, |
| 213 |
0x05, 0x06, 0x07, 0x08, 0x09, |
| 214 |
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 215 |
0x0f, 0x10, 0x11, 0x12, 0x13, |
| 216 |
0x14, 0x15, 0x16, 0x17, 0x18, |
| 217 |
}, |
| 218 |
[]uint8{0x0c}, |
| 219 |
}, |
| 220 |
{ |
| 221 |
"resize to fit (2, 3, box)", |
| 222 |
2, 3, BoxResampling, |
| 223 |
image.Rect(-1, -1, 3, 1), |
| 224 |
image.Rect(0, 0, 2, 1), |
| 225 |
[]uint8{ |
| 226 |
0x00, 0x01, 0x02, 0x03, |
| 227 |
0x05, 0x06, 0x07, 0x08, |
| 228 |
}, |
| 229 |
[]uint8{0x03, 0x05}, |
| 230 |
}, |
| 231 |
{ |
| 232 |
"resize to fit (3, 2, box)", |
| 233 |
3, 2, BoxResampling, |
| 234 |
image.Rect(-1, -1, 1, 3), |
| 235 |
image.Rect(0, 0, 1, 2), |
| 236 |
[]uint8{ |
| 237 |
0x00, 0x01, |
| 238 |
0x05, 0x06, |
| 239 |
0x02, 0x03, |
| 240 |
0x07, 0x08, |
| 241 |
}, |
| 242 |
[]uint8{0x03, 0x05}, |
| 243 |
}, |
| 244 |
{ |
| 245 |
"resize to fit (2, 4, box)", |
| 246 |
2, 4, BoxResampling, |
| 247 |
image.Rect(-1, -1, 1, 3), |
| 248 |
image.Rect(0, 0, 2, 4), |
| 249 |
[]uint8{ |
| 250 |
0x00, 0x01, |
| 251 |
0x05, 0x06, |
| 252 |
0x02, 0x03, |
| 253 |
0x07, 0x08, |
| 254 |
}, |
| 255 |
[]uint8{ |
| 256 |
0x00, 0x01, |
| 257 |
0x05, 0x06, |
| 258 |
0x02, 0x03, |
| 259 |
0x07, 0x08, |
| 260 |
}, |
| 261 |
}, |
| 262 |
{ |
| 263 |
"resize to fit (3, 10, box)", |
| 264 |
3, 10, BoxResampling, |
| 265 |
image.Rect(-1, -1, 1, 3), |
| 266 |
image.Rect(0, 0, 2, 4), |
| 267 |
[]uint8{ |
| 268 |
0x00, 0x01, |
| 269 |
0x05, 0x06, |
| 270 |
0x02, 0x03, |
| 271 |
0x07, 0x08, |
| 272 |
}, |
| 273 |
[]uint8{ |
| 274 |
0x00, 0x01, |
| 275 |
0x05, 0x06, |
| 276 |
0x02, 0x03, |
| 277 |
0x07, 0x08, |
| 278 |
}, |
| 279 |
}, |
| 280 |
} |
| 281 |
|
| 282 |
for _, d := range testData { |
| 283 |
src := image.NewGray(d.srcb) |
| 284 |
src.Pix = d.srcPix |
| 285 |
|
| 286 |
f := ResizeToFit(d.w, d.h, d.r) |
| 287 |
dst := image.NewGray(f.Bounds(src.Bounds())) |
| 288 |
f.Draw(dst, src, nil) |
| 289 |
|
| 290 |
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 291 |
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
func TestResizeToFill(t *testing.T) { |
| 297 |
testData := []struct { |
| 298 |
desc string |
| 299 |
w, h int |
| 300 |
r Resampling |
| 301 |
anchor Anchor |
| 302 |
srcb, dstb image.Rectangle |
| 303 |
srcPix, dstPix []uint8 |
| 304 |
}{ |
| 305 |
{ |
| 306 |
"resize to fill (0, 0, nearest, center)", |
| 307 |
0, 0, NearestNeighborResampling, CenterAnchor, |
| 308 |
image.Rect(-1, -1, 3, 1), |
| 309 |
image.Rect(0, 0, 0, 0), |
| 310 |
[]uint8{ |
| 311 |
0x00, 0x01, 0x02, 0x03, |
| 312 |
0x04, 0x05, 0x06, 0x07, |
| 313 |
}, |
| 314 |
[]uint8{}, |
| 315 |
}, |
| 316 |
{ |
| 317 |
"resize to fill (4, 2, nearest, center)", |
| 318 |
4, 2, NearestNeighborResampling, CenterAnchor, |
| 319 |
image.Rect(-1, -1, 3, 1), |
| 320 |
image.Rect(0, 0, 4, 2), |
| 321 |
[]uint8{ |
| 322 |
0x00, 0x01, 0x02, 0x03, |
| 323 |
0x04, 0x05, 0x06, 0x07, |
| 324 |
}, |
| 325 |
[]uint8{ |
| 326 |
0x00, 0x01, 0x02, 0x03, |
| 327 |
0x04, 0x05, 0x06, 0x07, |
| 328 |
}, |
| 329 |
}, |
| 330 |
{ |
| 331 |
"resize to fill (4, 4, nearest, center)", |
| 332 |
4, 4, NearestNeighborResampling, CenterAnchor, |
| 333 |
image.Rect(-1, -1, 3, 1), |
| 334 |
image.Rect(0, 0, 4, 4), |
| 335 |
[]uint8{ |
| 336 |
0x00, 0x01, 0x02, 0x03, |
| 337 |
0x04, 0x05, 0x06, 0x07, |
| 338 |
}, |
| 339 |
[]uint8{ |
| 340 |
0x01, 0x01, 0x02, 0x02, |
| 341 |
0x01, 0x01, 0x02, 0x02, |
| 342 |
0x05, 0x05, 0x06, 0x06, |
| 343 |
0x05, 0x05, 0x06, 0x06, |
| 344 |
}, |
| 345 |
}, |
| 346 |
{ |
| 347 |
"resize to fill (4, 4, nearest, bottom-right)", |
| 348 |
4, 4, NearestNeighborResampling, BottomRightAnchor, |
| 349 |
image.Rect(-1, -1, 1, 3), |
| 350 |
image.Rect(0, 0, 4, 4), |
| 351 |
[]uint8{ |
| 352 |
0x00, 0x01, |
| 353 |
0x02, 0x03, |
| 354 |
0x04, 0x05, |
| 355 |
0x06, 0x07, |
| 356 |
}, |
| 357 |
[]uint8{ |
| 358 |
0x04, 0x04, 0x05, 0x05, |
| 359 |
0x04, 0x04, 0x05, 0x05, |
| 360 |
0x06, 0x06, 0x07, 0x07, |
| 361 |
0x06, 0x06, 0x07, 0x07, |
| 362 |
}, |
| 363 |
}, |
| 364 |
{ |
| 365 |
"resize to fill (2, 1, nearest, bottom)", |
| 366 |
2, 1, NearestNeighborResampling, BottomAnchor, |
| 367 |
image.Rect(-1, -1, 5, 5), |
| 368 |
image.Rect(0, 0, 2, 1), |
| 369 |
[]uint8{ |
| 370 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, |
| 371 |
0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, |
| 372 |
0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, |
| 373 |
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, |
| 374 |
0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, |
| 375 |
0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, |
| 376 |
}, |
| 377 |
[]uint8{ |
| 378 |
0xa7, 0xaa, |
| 379 |
}, |
| 380 |
}, |
| 381 |
{ |
| 382 |
"resize to fill (2, 1, nearest, top)", |
| 383 |
2, 1, NearestNeighborResampling, TopAnchor, |
| 384 |
image.Rect(-1, -1, 5, 5), |
| 385 |
image.Rect(0, 0, 2, 1), |
| 386 |
[]uint8{ |
| 387 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, |
| 388 |
0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, |
| 389 |
0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, |
| 390 |
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, |
| 391 |
0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, |
| 392 |
0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, |
| 393 |
}, |
| 394 |
[]uint8{ |
| 395 |
0x07, 0x0a, |
| 396 |
}, |
| 397 |
}, |
| 398 |
} |
| 399 |
|
| 400 |
for _, d := range testData { |
| 401 |
src := image.NewGray(d.srcb) |
| 402 |
src.Pix = d.srcPix |
| 403 |
|
| 404 |
f := ResizeToFill(d.w, d.h, d.r, d.anchor) |
| 405 |
dst := image.NewGray(f.Bounds(src.Bounds())) |
| 406 |
f.Draw(dst, src, nil) |
| 407 |
|
| 408 |
if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 409 |
t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 410 |
} |
| 411 |
} |
| 412 |
} |