| 1 | package gift |
| 2 | |
| 3 | import ( |
| 4 | "image" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestPixelate(t *testing.T) { |
| 9 | testData := []struct { |
| 10 | desc string |
| 11 | size int |
| 12 | srcb, dstb image.Rectangle |
| 13 | srcPix, dstPix []uint8 |
| 14 | }{ |
| 15 | { |
| 16 | "pixelate (0)", |
| 17 | 0, |
| 18 | image.Rect(-1, -1, 4, 2), |
| 19 | image.Rect(0, 0, 5, 3), |
| 20 | []uint8{ |
| 21 | 0x00, 0x40, 0x00, 0x40, 0x00, |
| 22 | 0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 23 | 0x00, 0x80, 0x00, 0x80, 0x00, |
| 24 | }, |
| 25 | []uint8{ |
| 26 | 0x00, 0x40, 0x00, 0x40, 0x00, |
| 27 | 0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 28 | 0x00, 0x80, 0x00, 0x80, 0x00, |
| 29 | }, |
| 30 | }, |
| 31 | { |
| 32 | "pixelate (1)", |
| 33 | 1, |
| 34 | image.Rect(-1, -1, 4, 2), |
| 35 | image.Rect(0, 0, 5, 3), |
| 36 | []uint8{ |
| 37 | 0x00, 0x40, 0x00, 0x40, 0x00, |
| 38 | 0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 39 | 0x00, 0x80, 0x00, 0x80, 0x00, |
| 40 | }, |
| 41 | []uint8{ |
| 42 | 0x00, 0x40, 0x00, 0x40, 0x00, |
| 43 | 0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 44 | 0x00, 0x80, 0x00, 0x80, 0x00, |
| 45 | }, |
| 46 | }, |
| 47 | { |
| 48 | "pixelate (2)", |
| 49 | 2, |
| 50 | image.Rect(-1, -1, 4, 2), |
| 51 | image.Rect(0, 0, 5, 3), |
| 52 | []uint8{ |
| 53 | 0x00, 0x40, 0x00, 0x40, 0x00, |
| 54 | 0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 55 | 0x00, 0x80, 0x00, 0x80, 0x00, |
| 56 | }, |
| 57 | []uint8{ |
| 58 | 0x54, 0x54, 0x64, 0x64, 0x30, |
| 59 | 0x54, 0x54, 0x64, 0x64, 0x30, |
| 60 | 0x40, 0x40, 0x40, 0x40, 0x00, |
| 61 | }, |
| 62 | }, |
| 63 | { |
| 64 | "pixelate (3)", |
| 65 | 3, |
| 66 | image.Rect(-1, -1, 4, 2), |
| 67 | image.Rect(0, 0, 5, 3), |
| 68 | []uint8{ |
| 69 | 0x00, 0x40, 0x00, 0x40, 0x00, |
| 70 | 0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 71 | 0x00, 0x80, 0x00, 0x80, 0x00, |
| 72 | }, |
| 73 | []uint8{ |
| 74 | 0x45, 0x45, 0x45, 0x4d, 0x4d, |
| 75 | 0x45, 0x45, 0x45, 0x4d, 0x4d, |
| 76 | 0x45, 0x45, 0x45, 0x4d, 0x4d, |
| 77 | }, |
| 78 | }, |
| 79 | { |
| 80 | "pixelate (10)", |
| 81 | 10, |
| 82 | image.Rect(-1, -1, 4, 2), |
| 83 | image.Rect(0, 0, 5, 3), |
| 84 | []uint8{ |
| 85 | 0x00, 0x40, 0x00, 0x40, 0x00, |
| 86 | 0x60, 0xB0, 0xA0, 0xB0, 0x60, |
| 87 | 0x00, 0x80, 0x00, 0x80, 0x00, |
| 88 | }, |
| 89 | []uint8{ |
| 90 | 0x49, 0x49, 0x49, 0x49, 0x49, |
| 91 | 0x49, 0x49, 0x49, 0x49, 0x49, |
| 92 | 0x49, 0x49, 0x49, 0x49, 0x49, |
| 93 | }, |
| 94 | }, |
| 95 | { |
| 96 | "pixelate 0x0", |
| 97 | 3, |
| 98 | image.Rect(-1, -1, -1, -1), |
| 99 | image.Rect(0, 0, 0, 0), |
| 100 | []uint8{}, |
| 101 | []uint8{}, |
| 102 | }, |
| 103 | } |
| 104 | |
| 105 | for _, d := range testData { |
| 106 | src := image.NewGray(d.srcb) |
| 107 | src.Pix = d.srcPix |
| 108 | |
| 109 | f := Pixelate(d.size) |
| 110 | dst := image.NewGray(f.Bounds(src.Bounds())) |
| 111 | f.Draw(dst, src, nil) |
| 112 | |
| 113 | if !checkBoundsAndPix(dst.Bounds(), d.dstb, dst.Pix, d.dstPix) { |
| 114 | t.Errorf("test [%s] failed: %#v, %#v", d.desc, dst.Bounds(), dst.Pix) |
| 115 | } |
| 116 | } |
| 117 | } |