| 1 | package gift |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "image" |
| 6 | "image/color" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestRotate90(t *testing.T) { |
| 11 | img0 := image.NewGray(image.Rect(-1, -1, 3, 1)) |
| 12 | img0.Pix = []uint8{ |
| 13 | 1, 2, 3, 4, |
| 14 | 5, 6, 7, 8, |
| 15 | } |
| 16 | img1Exp := image.NewGray(image.Rect(0, 0, 2, 4)) |
| 17 | img1Exp.Pix = []uint8{ |
| 18 | 4, 8, |
| 19 | 3, 7, |
| 20 | 2, 6, |
| 21 | 1, 5, |
| 22 | } |
| 23 | |
| 24 | f := Rotate90() |
| 25 | img1 := image.NewGray(f.Bounds(img0.Bounds())) |
| 26 | f.Draw(img1, img0, nil) |
| 27 | |
| 28 | if img1.Bounds().Size() != img1Exp.Bounds().Size() { |
| 29 | t.Errorf("expected %v got %v", img1Exp.Bounds().Size(), img1.Bounds().Size()) |
| 30 | } |
| 31 | if !bytes.Equal(img1Exp.Pix, img1.Pix) { |
| 32 | t.Errorf("expected %v got %v", img1Exp.Pix, img1.Pix) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestRotate180(t *testing.T) { |
| 37 | img0 := image.NewGray(image.Rect(-1, -1, 3, 1)) |
| 38 | img0.Pix = []uint8{ |
| 39 | 1, 2, 3, 4, |
| 40 | 5, 6, 7, 8, |
| 41 | } |
| 42 | img1Exp := image.NewGray(image.Rect(0, 0, 4, 2)) |
| 43 | img1Exp.Pix = []uint8{ |
| 44 | 8, 7, 6, 5, |
| 45 | 4, 3, 2, 1, |
| 46 | } |
| 47 | |
| 48 | f := Rotate180() |
| 49 | img1 := image.NewGray(f.Bounds(img0.Bounds())) |
| 50 | f.Draw(img1, img0, nil) |
| 51 | |
| 52 | if img1.Bounds().Size() != img1Exp.Bounds().Size() { |
| 53 | t.Errorf("expected %v got %v", img1Exp.Bounds().Size(), img1.Bounds().Size()) |
| 54 | } |
| 55 | if !bytes.Equal(img1Exp.Pix, img1.Pix) { |
| 56 | t.Errorf("expected %v got %v", img1Exp.Pix, img1.Pix) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestRotate270(t *testing.T) { |
| 61 | img0 := image.NewGray(image.Rect(-1, -1, 3, 1)) |
| 62 | img0.Pix = []uint8{ |
| 63 | 1, 2, 3, 4, |
| 64 | 5, 6, 7, 8, |
| 65 | } |
| 66 | img1Exp := image.NewGray(image.Rect(0, 0, 2, 4)) |
| 67 | img1Exp.Pix = []uint8{ |
| 68 | 5, 1, |
| 69 | 6, 2, |
| 70 | 7, 3, |
| 71 | 8, 4, |
| 72 | } |
| 73 | |
| 74 | f := Rotate270() |
| 75 | img1 := image.NewGray(f.Bounds(img0.Bounds())) |
| 76 | f.Draw(img1, img0, nil) |
| 77 | |
| 78 | if img1.Bounds().Size() != img1Exp.Bounds().Size() { |
| 79 | t.Errorf("expected %v got %v", img1Exp.Bounds().Size(), img1.Bounds().Size()) |
| 80 | } |
| 81 | if !bytes.Equal(img1Exp.Pix, img1.Pix) { |
| 82 | t.Errorf("expected %v got %v", img1Exp.Pix, img1.Pix) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func TestFlipHorizontal(t *testing.T) { |
| 87 | img0 := image.NewGray(image.Rect(-1, -1, 3, 1)) |
| 88 | img0.Pix = []uint8{ |
| 89 | 1, 2, 3, 4, |
| 90 | 5, 6, 7, 8, |
| 91 | } |
| 92 | img1Exp := image.NewGray(image.Rect(0, 0, 4, 2)) |
| 93 | img1Exp.Pix = []uint8{ |
| 94 | 4, 3, 2, 1, |
| 95 | 8, 7, 6, 5, |
| 96 | } |
| 97 | |
| 98 | f := FlipHorizontal() |
| 99 | img1 := image.NewGray(f.Bounds(img0.Bounds())) |
| 100 | f.Draw(img1, img0, nil) |
| 101 | |
| 102 | if img1.Bounds().Size() != img1Exp.Bounds().Size() { |
| 103 | t.Errorf("expected %v got %v", img1Exp.Bounds().Size(), img1.Bounds().Size()) |
| 104 | } |
| 105 | if !bytes.Equal(img1Exp.Pix, img1.Pix) { |
| 106 | t.Errorf("expected %v got %v", img1Exp.Pix, img1.Pix) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestFlipVertical(t *testing.T) { |
| 111 | img0 := image.NewGray(image.Rect(-1, -1, 3, 1)) |
| 112 | img0.Pix = []uint8{ |
| 113 | 1, 2, 3, 4, |
| 114 | 5, 6, 7, 8, |
| 115 | } |
| 116 | img1Exp := image.NewGray(image.Rect(0, 0, 4, 2)) |
| 117 | img1Exp.Pix = []uint8{ |
| 118 | 5, 6, 7, 8, |
| 119 | 1, 2, 3, 4, |
| 120 | } |
| 121 | |
| 122 | f := FlipVertical() |
| 123 | img1 := image.NewGray(f.Bounds(img0.Bounds())) |
| 124 | f.Draw(img1, img0, nil) |
| 125 | |
| 126 | if img1.Bounds().Size() != img1Exp.Bounds().Size() { |
| 127 | t.Errorf("expected %v got %v", img1Exp.Bounds().Size(), img1.Bounds().Size()) |
| 128 | } |
| 129 | if !bytes.Equal(img1Exp.Pix, img1.Pix) { |
| 130 | t.Errorf("expected %v got %v", img1Exp.Pix, img1.Pix) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func TestTranspose(t *testing.T) { |
| 135 | img0 := image.NewGray(image.Rect(-1, -1, 3, 1)) |
| 136 | img0.Pix = []uint8{ |
| 137 | 1, 2, 3, 4, |
| 138 | 5, 6, 7, 8, |
| 139 | } |
| 140 | img1Exp := image.NewGray(image.Rect(0, 0, 2, 4)) |
| 141 | img1Exp.Pix = []uint8{ |
| 142 | 1, 5, |
| 143 | 2, 6, |
| 144 | 3, 7, |
| 145 | 4, 8, |
| 146 | } |
| 147 | |
| 148 | f := Transpose() |
| 149 | img1 := image.NewGray(f.Bounds(img0.Bounds())) |
| 150 | f.Draw(img1, img0, nil) |
| 151 | |
| 152 | if img1.Bounds().Size() != img1Exp.Bounds().Size() { |
| 153 | t.Errorf("expected %v got %v", img1Exp.Bounds().Size(), img1.Bounds().Size()) |
| 154 | } |
| 155 | if !bytes.Equal(img1Exp.Pix, img1.Pix) { |
| 156 | t.Errorf("expected %v got %v", img1Exp.Pix, img1.Pix) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | func TestTransverse(t *testing.T) { |
| 161 | img0 := image.NewGray(image.Rect(-1, -1, 3, 1)) |
| 162 | img0.Pix = []uint8{ |
| 163 | 1, 2, 3, 4, |
| 164 | 5, 6, 7, 8, |
| 165 | } |
| 166 | img1Exp := image.NewGray(image.Rect(0, 0, 2, 4)) |
| 167 | img1Exp.Pix = []uint8{ |
| 168 | 8, 4, |
| 169 | 7, 3, |
| 170 | 6, 2, |
| 171 | 5, 1, |
| 172 | } |
| 173 | |
| 174 | f := Transverse() |
| 175 | img1 := image.NewGray(f.Bounds(img0.Bounds())) |
| 176 | f.Draw(img1, img0, nil) |
| 177 | |
| 178 | if img1.Bounds().Size() != img1Exp.Bounds().Size() { |
| 179 | t.Errorf("expected %v got %v", img1Exp.Bounds().Size(), img1.Bounds().Size()) |
| 180 | } |
| 181 | if !bytes.Equal(img1Exp.Pix, img1.Pix) { |
| 182 | t.Errorf("expected %v got %v", img1Exp.Pix, img1.Pix) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | func TestCrop(t *testing.T) { |
| 187 | testData := []struct { |
| 188 | desc string |
| 189 | r image.Rectangle |
| 190 | srcb, dstb image.Rectangle |
| 191 | srcPix, dstPix []uint8 |
| 192 | }{ |
| 193 | { |
| 194 | "crop (0, 0, 0, 0)", |
| 195 | image.Rect(0, 0, 0, 0), |
| 196 | image.Rect(-1, -1, 4, 2), |
| 197 | image.Rect(0, 0, 0, 0), |
| 198 | []uint8{ |
| 199 | 0x00, 0x40, 0x00, 0x40, 0x00, |
| 200 | 0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 201 | 0x00, 0x80, 0x00, 0x80, 0x00, |
| 202 | }, |
| 203 | []uint8{}, |
| 204 | }, |
| 205 | { |
| 206 | "crop (1, 1, -1, -1)", |
| 207 | image.Rectangle{image.Pt(1, 1), image.Pt(-1, -1)}, |
| 208 | image.Rect(-1, -1, 4, 2), |
| 209 | image.Rect(0, 0, 0, 0), |
| 210 | []uint8{ |
| 211 | 0x00, 0x40, 0x00, 0x40, 0x00, |
| 212 | 0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 213 | 0x00, 0x80, 0x00, 0x80, 0x00, |
| 214 | }, |
| 215 | []uint8{}, |
| 216 | }, |
| 217 | { |
| 218 | "crop (-1, 0, 3, 2)", |
| 219 | image.Rect(-1, 0, 3, 2), |
| 220 | image.Rect(-1, -1, 4, 2), |
| 221 | image.Rect(0, 0, 4, 2), |
| 222 | []uint8{ |
| 223 | 0x00, 0x40, 0x00, 0x40, 0x00, |
| 224 | 0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 225 | 0x00, 0x80, 0x00, 0x80, 0x00, |
| 226 | }, |
| 227 | []uint8{ |
| 228 | 0x60, 0xB0, 0xA0, 0xB0, |
| 229 | 0x00, 0x80, 0x00, 0x80, |
| 230 | }, |
| 231 | }, |
| 232 | { |
| 233 | "crop (-100, -100, 2, 2)", |
| 234 | image.Rect(-100, -100, 2, 2), |
| 235 | image.Rect(-1, -1, 4, 2), |
| 236 | image.Rect(0, 0, 3, 3), |
| 237 | []uint8{ |
| 238 | 0x00, 0x40, 0x00, 0x40, 0x00, |
| 239 | 0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 240 | 0x00, 0x80, 0x00, 0x80, 0x00, |
| 241 | }, |
| 242 | []uint8{ |
| 243 | 0x00, 0x40, 0x00, |
| 244 | 0x60, 0xB0, 0xA0, |
| 245 | 0x00, 0x80, 0x00, |
| 246 | }, |
| 247 | }, |
| 248 | { |
| 249 | "crop (-100, -100, 100, 100)", |
| 250 | image.Rect(-100, -100, 100, 100), |
| 251 | image.Rect(-1, -1, 4, 2), |
| 252 | image.Rect(0, 0, 5, 3), |
| 253 | []uint8{ |
| 254 | 0x00, 0x40, 0x00, 0x40, 0x00, |
| 255 | 0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 256 | 0x00, 0x80, 0x00, 0x80, 0x00, |
| 257 | }, |
| 258 | []uint8{ |
| 259 | 0x00, 0x40, 0x00, 0x40, 0x00, |
| 260 | 0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 261 | 0x00, 0x80, 0x00, 0x80, 0x00, |
| 262 | }, |
| 263 | }, |
| 264 | } |
| 265 | |
| 266 | for _, d := range testData { |
| 267 | src := image.NewGray(d.srcb) |
| 268 | src.Pix = d.srcPix |
| 269 | |
| 270 | f := Crop(d.r) |
| 271 | dst := image.NewGray(f.Bounds(src.Bounds())) |
| 272 | f.Draw(dst, src, nil) |
| 273 | |
| 274 | if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 275 | t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | func TestCropToSize(t *testing.T) { |
| 281 | testData := []struct { |
| 282 | desc string |
| 283 | w, h int |
| 284 | anchor Anchor |
| 285 | srcb, dstb image.Rectangle |
| 286 | srcPix, dstPix []uint8 |
| 287 | }{ |
| 288 | { |
| 289 | "crop to size (0, 0, center)", |
| 290 | 0, 0, CenterAnchor, |
| 291 | image.Rect(-1, -1, 4, 4), |
| 292 | image.Rect(0, 0, 0, 0), |
| 293 | []uint8{ |
| 294 | 0x00, 0x01, 0x02, 0x03, 0x04, |
| 295 | 0x05, 0x06, 0x07, 0x08, 0x09, |
| 296 | 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 297 | 0x0f, 0x10, 0x11, 0x12, 0x13, |
| 298 | 0x14, 0x15, 0x16, 0x17, 0x18, |
| 299 | }, |
| 300 | []uint8{}, |
| 301 | }, |
| 302 | { |
| 303 | "crop to size (3, 3, center)", |
| 304 | 3, 3, CenterAnchor, |
| 305 | image.Rect(-1, -1, 4, 4), |
| 306 | image.Rect(0, 0, 3, 3), |
| 307 | []uint8{ |
| 308 | 0x00, 0x01, 0x02, 0x03, 0x04, |
| 309 | 0x05, 0x06, 0x07, 0x08, 0x09, |
| 310 | 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 311 | 0x0f, 0x10, 0x11, 0x12, 0x13, |
| 312 | 0x14, 0x15, 0x16, 0x17, 0x18, |
| 313 | }, |
| 314 | []uint8{ |
| 315 | 0x06, 0x07, 0x08, |
| 316 | 0x0b, 0x0c, 0x0d, |
| 317 | 0x10, 0x11, 0x12, |
| 318 | }, |
| 319 | }, |
| 320 | { |
| 321 | "crop to size (3, 3, top-left)", |
| 322 | 3, 3, TopLeftAnchor, |
| 323 | image.Rect(-1, -1, 4, 4), |
| 324 | image.Rect(0, 0, 3, 3), |
| 325 | []uint8{ |
| 326 | 0x00, 0x01, 0x02, 0x03, 0x04, |
| 327 | 0x05, 0x06, 0x07, 0x08, 0x09, |
| 328 | 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 329 | 0x0f, 0x10, 0x11, 0x12, 0x13, |
| 330 | 0x14, 0x15, 0x16, 0x17, 0x18, |
| 331 | }, |
| 332 | []uint8{ |
| 333 | 0x00, 0x01, 0x02, |
| 334 | 0x05, 0x06, 0x07, |
| 335 | 0x0a, 0x0b, 0x0c, |
| 336 | }, |
| 337 | }, |
| 338 | { |
| 339 | "crop to size (3, 3, top)", |
| 340 | 3, 3, TopAnchor, |
| 341 | image.Rect(-1, -1, 4, 4), |
| 342 | image.Rect(0, 0, 3, 3), |
| 343 | []uint8{ |
| 344 | 0x00, 0x01, 0x02, 0x03, 0x04, |
| 345 | 0x05, 0x06, 0x07, 0x08, 0x09, |
| 346 | 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 347 | 0x0f, 0x10, 0x11, 0x12, 0x13, |
| 348 | 0x14, 0x15, 0x16, 0x17, 0x18, |
| 349 | }, |
| 350 | []uint8{ |
| 351 | 0x01, 0x02, 0x03, |
| 352 | 0x06, 0x07, 0x08, |
| 353 | 0x0b, 0x0c, 0x0d, |
| 354 | }, |
| 355 | }, |
| 356 | { |
| 357 | "crop to size (3, 3,, top-right)", |
| 358 | 3, 3, TopRightAnchor, |
| 359 | image.Rect(-1, -1, 4, 4), |
| 360 | image.Rect(0, 0, 3, 3), |
| 361 | []uint8{ |
| 362 | 0x00, 0x01, 0x02, 0x03, 0x04, |
| 363 | 0x05, 0x06, 0x07, 0x08, 0x09, |
| 364 | 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 365 | 0x0f, 0x10, 0x11, 0x12, 0x13, |
| 366 | 0x14, 0x15, 0x16, 0x17, 0x18, |
| 367 | }, |
| 368 | []uint8{ |
| 369 | 0x02, 0x03, 0x04, |
| 370 | 0x07, 0x08, 0x09, |
| 371 | 0x0c, 0x0d, 0x0e, |
| 372 | }, |
| 373 | }, |
| 374 | { |
| 375 | "crop to size (3, 3, left)", |
| 376 | 3, 3, LeftAnchor, |
| 377 | image.Rect(-1, -1, 4, 4), |
| 378 | image.Rect(0, 0, 3, 3), |
| 379 | []uint8{ |
| 380 | 0x00, 0x01, 0x02, 0x03, 0x04, |
| 381 | 0x05, 0x06, 0x07, 0x08, 0x09, |
| 382 | 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 383 | 0x0f, 0x10, 0x11, 0x12, 0x13, |
| 384 | 0x14, 0x15, 0x16, 0x17, 0x18, |
| 385 | }, |
| 386 | []uint8{ |
| 387 | 0x05, 0x06, 0x07, |
| 388 | 0x0a, 0x0b, 0x0c, |
| 389 | 0x0f, 0x10, 0x11, |
| 390 | }, |
| 391 | }, |
| 392 | { |
| 393 | "crop to size (3, 3, right)", |
| 394 | 3, 3, RightAnchor, |
| 395 | image.Rect(-1, -1, 4, 4), |
| 396 | image.Rect(0, 0, 3, 3), |
| 397 | []uint8{ |
| 398 | 0x00, 0x01, 0x02, 0x03, 0x04, |
| 399 | 0x05, 0x06, 0x07, 0x08, 0x09, |
| 400 | 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 401 | 0x0f, 0x10, 0x11, 0x12, 0x13, |
| 402 | 0x14, 0x15, 0x16, 0x17, 0x18, |
| 403 | }, |
| 404 | []uint8{ |
| 405 | 0x07, 0x08, 0x09, |
| 406 | 0x0c, 0x0d, 0x0e, |
| 407 | 0x11, 0x12, 0x13, |
| 408 | }, |
| 409 | }, |
| 410 | { |
| 411 | "crop to size (3, 3, bottom-left)", |
| 412 | 3, 3, BottomLeftAnchor, |
| 413 | image.Rect(-1, -1, 4, 4), |
| 414 | image.Rect(0, 0, 3, 3), |
| 415 | []uint8{ |
| 416 | 0x00, 0x01, 0x02, 0x03, 0x04, |
| 417 | 0x05, 0x06, 0x07, 0x08, 0x09, |
| 418 | 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 419 | 0x0f, 0x10, 0x11, 0x12, 0x13, |
| 420 | 0x14, 0x15, 0x16, 0x17, 0x18, |
| 421 | }, |
| 422 | []uint8{ |
| 423 | 0x0a, 0x0b, 0x0c, |
| 424 | 0x0f, 0x10, 0x11, |
| 425 | 0x14, 0x15, 0x16, |
| 426 | }, |
| 427 | }, |
| 428 | { |
| 429 | "crop to size (3, 3, bottom)", |
| 430 | 3, 3, BottomAnchor, |
| 431 | image.Rect(-1, -1, 4, 4), |
| 432 | image.Rect(0, 0, 3, 3), |
| 433 | []uint8{ |
| 434 | 0x00, 0x01, 0x02, 0x03, 0x04, |
| 435 | 0x05, 0x06, 0x07, 0x08, 0x09, |
| 436 | 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 437 | 0x0f, 0x10, 0x11, 0x12, 0x13, |
| 438 | 0x14, 0x15, 0x16, 0x17, 0x18, |
| 439 | }, |
| 440 | []uint8{ |
| 441 | 0x0b, 0x0c, 0x0d, |
| 442 | 0x10, 0x11, 0x12, |
| 443 | 0x15, 0x16, 0x17, |
| 444 | }, |
| 445 | }, |
| 446 | { |
| 447 | "crop to size (3, 3, bottom-right)", |
| 448 | 3, 3, BottomRightAnchor, |
| 449 | image.Rect(-1, -1, 4, 4), |
| 450 | image.Rect(0, 0, 3, 3), |
| 451 | []uint8{ |
| 452 | 0x00, 0x01, 0x02, 0x03, 0x04, |
| 453 | 0x05, 0x06, 0x07, 0x08, 0x09, |
| 454 | 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 455 | 0x0f, 0x10, 0x11, 0x12, 0x13, |
| 456 | 0x14, 0x15, 0x16, 0x17, 0x18, |
| 457 | }, |
| 458 | []uint8{ |
| 459 | 0x0c, 0x0d, 0x0e, |
| 460 | 0x11, 0x12, 0x13, |
| 461 | 0x16, 0x17, 0x18, |
| 462 | }, |
| 463 | }, |
| 464 | { |
| 465 | "crop to size (100, 100, center)", |
| 466 | 100, 100, CenterAnchor, |
| 467 | image.Rect(-1, -1, 4, 4), |
| 468 | image.Rect(0, 0, 5, 5), |
| 469 | []uint8{ |
| 470 | 0x00, 0x01, 0x02, 0x03, 0x04, |
| 471 | 0x05, 0x06, 0x07, 0x08, 0x09, |
| 472 | 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 473 | 0x0f, 0x10, 0x11, 0x12, 0x13, |
| 474 | 0x14, 0x15, 0x16, 0x17, 0x18, |
| 475 | }, |
| 476 | []uint8{ |
| 477 | 0x00, 0x01, 0x02, 0x03, 0x04, |
| 478 | 0x05, 0x06, 0x07, 0x08, 0x09, |
| 479 | 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, |
| 480 | 0x0f, 0x10, 0x11, 0x12, 0x13, |
| 481 | 0x14, 0x15, 0x16, 0x17, 0x18, |
| 482 | }, |
| 483 | }, |
| 484 | } |
| 485 | |
| 486 | for _, d := range testData { |
| 487 | src := image.NewGray(d.srcb) |
| 488 | src.Pix = d.srcPix |
| 489 | |
| 490 | f := CropToSize(d.w, d.h, d.anchor) |
| 491 | dst := image.NewGray(f.Bounds(src.Bounds())) |
| 492 | f.Draw(dst, src, nil) |
| 493 | |
| 494 | if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 495 | t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | func TestRotate(t *testing.T) { |
| 501 | testData := []struct { |
| 502 | desc string |
| 503 | a float32 |
| 504 | bg color.Color |
| 505 | interp Interpolation |
| 506 | srcb, dstb image.Rectangle |
| 507 | srcPix, dstPix []uint8 |
| 508 | }{ |
| 509 | { |
| 510 | "rotate 0x0 90 white nearest", |
| 511 | 90, color.White, NearestNeighborInterpolation, |
| 512 | image.Rect(0, 0, 0, 0), |
| 513 | image.Rect(0, 0, 0, 0), |
| 514 | []uint8{}, |
| 515 | []uint8{}, |
| 516 | }, |
| 517 | { |
| 518 | "rotate 1x1 90 white nearest", |
| 519 | 90, color.White, NearestNeighborInterpolation, |
| 520 | image.Rect(-1, -1, 0, 0), |
| 521 | image.Rect(0, 0, 1, 1), |
| 522 | []uint8{0x80}, |
| 523 | []uint8{0x80}, |
| 524 | }, |
| 525 | { |
| 526 | "rotate 3x3 -90 white nearest", |
| 527 | -90, color.White, NearestNeighborInterpolation, |
| 528 | image.Rect(-1, -1, 2, 2), |
| 529 | image.Rect(0, 0, 3, 3), |
| 530 | []uint8{ |
| 531 | 0x10, 0x20, 0x30, |
| 532 | 0x40, 0x50, 0x60, |
| 533 | 0x70, 0x80, 0x90, |
| 534 | }, |
| 535 | []uint8{ |
| 536 | 0x70, 0x40, 0x10, |
| 537 | 0x80, 0x50, 0x20, |
| 538 | 0x90, 0x60, 0x30, |
| 539 | }, |
| 540 | }, |
| 541 | { |
| 542 | "rotate 3x3 -90 white linear", |
| 543 | -90, color.White, LinearInterpolation, |
| 544 | image.Rect(-1, -1, 2, 2), |
| 545 | image.Rect(0, 0, 3, 3), |
| 546 | []uint8{ |
| 547 | 0x10, 0x20, 0x30, |
| 548 | 0x40, 0x50, 0x60, |
| 549 | 0x70, 0x80, 0x90, |
| 550 | }, |
| 551 | []uint8{ |
| 552 | 0x70, 0x40, 0x10, |
| 553 | 0x80, 0x50, 0x20, |
| 554 | 0x90, 0x60, 0x30, |
| 555 | }, |
| 556 | }, |
| 557 | { |
| 558 | "rotate 3x3 45 black nearest", |
| 559 | 45, color.Black, NearestNeighborInterpolation, |
| 560 | image.Rect(-1, -1, 2, 2), |
| 561 | image.Rect(0, 0, 5, 5), |
| 562 | []uint8{ |
| 563 | 0x10, 0x20, 0x30, |
| 564 | 0x40, 0x50, 0x60, |
| 565 | 0x70, 0x80, 0x90, |
| 566 | }, |
| 567 | []uint8{ |
| 568 | 0x00, 0x00, 0x30, 0x00, 0x00, |
| 569 | 0x00, 0x20, 0x30, 0x60, 0x00, |
| 570 | 0x10, 0x10, 0x50, 0x90, 0x90, |
| 571 | 0x00, 0x40, 0x70, 0x80, 0x00, |
| 572 | 0x00, 0x00, 0x70, 0x00, 0x00, |
| 573 | }, |
| 574 | }, |
| 575 | { |
| 576 | "rotate 5x5 45 black linear", |
| 577 | 45, color.Black, LinearInterpolation, |
| 578 | image.Rect(-1, -1, 4, 4), |
| 579 | image.Rect(0, 0, 8, 8), |
| 580 | []uint8{ |
| 581 | 0xff, 0xff, 0xff, 0xff, 0xff, |
| 582 | 0xff, 0xff, 0xff, 0xff, 0xff, |
| 583 | 0xff, 0xff, 0xff, 0xff, 0xff, |
| 584 | 0xff, 0xff, 0xff, 0xff, 0xff, |
| 585 | 0xff, 0xff, 0xff, 0xff, 0xff, |
| 586 | }, |
| 587 | []uint8{ |
| 588 | 0x00, 0x00, 0x00, 0x26, 0x26, 0x00, 0x00, 0x00, |
| 589 | 0x00, 0x00, 0x2c, 0xe0, 0xe0, 0x2c, 0x00, 0x00, |
| 590 | 0x00, 0x2c, 0xe0, 0xff, 0xff, 0xe0, 0x2c, 0x00, |
| 591 | 0x26, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x26, |
| 592 | 0x26, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x26, |
| 593 | 0x00, 0x2c, 0xe0, 0xff, 0xff, 0xe0, 0x2c, 0x00, |
| 594 | 0x00, 0x00, 0x2c, 0xe0, 0xe0, 0x2c, 0x00, 0x00, |
| 595 | 0x00, 0x00, 0x00, 0x26, 0x26, 0x00, 0x00, 0x00, |
| 596 | }, |
| 597 | }, |
| 598 | { |
| 599 | "rotate 5x5 45 black cubic", |
| 600 | 45, color.Black, CubicInterpolation, |
| 601 | image.Rect(-1, -1, 4, 4), |
| 602 | image.Rect(0, 0, 8, 8), |
| 603 | []uint8{ |
| 604 | 0xff, 0xff, 0xff, 0xff, 0xff, |
| 605 | 0xff, 0xff, 0xff, 0xff, 0xff, |
| 606 | 0xff, 0xff, 0xff, 0xff, 0xff, |
| 607 | 0xff, 0xff, 0xff, 0xff, 0xff, |
| 608 | 0xff, 0xff, 0xff, 0xff, 0xff, |
| 609 | }, |
| 610 | []uint8{ |
| 611 | 0x00, 0x00, 0x00, 0x23, 0x23, 0x00, 0x00, 0x00, |
| 612 | 0x00, 0x00, 0x28, 0xf1, 0xf1, 0x28, 0x00, 0x00, |
| 613 | 0x00, 0x28, 0xe3, 0xff, 0xff, 0xe3, 0x28, 0x00, |
| 614 | 0x23, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xf1, 0x23, |
| 615 | 0x23, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xf1, 0x23, |
| 616 | 0x00, 0x28, 0xe3, 0xff, 0xff, 0xe3, 0x28, 0x00, |
| 617 | 0x00, 0x00, 0x28, 0xf1, 0xf1, 0x28, 0x00, 0x00, |
| 618 | 0x00, 0x00, 0x00, 0x23, 0x23, 0x00, 0x00, 0x00, |
| 619 | }, |
| 620 | }, |
| 621 | } |
| 622 | |
| 623 | for _, d := range testData { |
| 624 | src := image.NewGray(d.srcb) |
| 625 | src.Pix = d.srcPix |
| 626 | |
| 627 | f := Rotate(d.a, d.bg, d.interp) |
| 628 | dst := image.NewGray(f.Bounds(src.Bounds())) |
| 629 | f.Draw(dst, src, nil) |
| 630 | |
| 631 | if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 632 | t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 633 | } |
| 634 | } |
| 635 | } |