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