limage - ora/encode_test.go
1 package ora
2
3 import (
4 "archive/zip"
5 "image"
6 "image/color"
7 "testing"
8
9 "vimagination.zapto.org/limage"
10 "vimagination.zapto.org/memio"
11 )
12
13 /*
14 type colourSquare struct {
15 image.Rectangle
16 color.Color
17 }
18
19 func (c colourSquare) ColorModel() color.Model {
20 return color.ModelFunc(func(color.Color) color.Color {
21 return c.Color
22 })
23 }
24
25 func (c colourSquare) Bounds() image.Rectangle {
26 return c.Rectangle
27 }
28
29 func (c colourSquare) At(int, int) color.Color {
30 return c.Color
31 }
32 */
33
34 func TestEncode(t *testing.T) {
35 tests := []limage.Image{
36 {
37 limage.Layer{
38 Name: "Layer Group",
39 Image: limage.Image{
40 limage.Layer{
41 Name: "Blue",
42 Image: singleColourImage{
43 Colour: color.RGBA64{B: 65535, A: 65535},
44 Width: 30,
45 Height: 30,
46 },
47 LayerBounds: image.Rect(0, 0, 30, 30),
48 },
49 limage.Layer{
50 Name: "Red",
51 Image: singleColourImage{Colour: color.RGBA64{R: 65535, A: 65535},
52 Width: 30,
53 Height: 30,
54 },
55 LayerBounds: image.Rect(20, 20, 50, 50),
56 },
57 },
58 LayerBounds: image.Rect(0, 0, 50, 50),
59 },
60 limage.Layer{
61 Name: "Background",
62 Image: singleColourImage{
63 Colour: color.RGBA64{A: 65535},
64 Width: 50,
65 Height: 50,
66 },
67 LayerBounds: image.Rect(0, 0, 50, 50),
68 },
69 },
70 {
71 limage.Layer{
72 Name: "Layer",
73 Image: singleColourImage{
74 Colour: color.RGBA64{R: 65535, A: 65535},
75 Width: 30,
76 Height: 30,
77 },
78 LayerBounds: image.Rect(10, 10, 40, 40),
79 },
80 limage.Layer{
81 Name: "Background",
82 Image: singleColourImage{
83 Colour: color.RGBA64{A: 65535},
84 Width: 50,
85 Height: 50,
86 },
87 LayerBounds: image.Rect(0, 0, 50, 50),
88 },
89 },
90 {
91 limage.Layer{
92 Name: "Background",
93 Image: singleColourImage{
94 Colour: color.RGBA64{A: 65535},
95 Width: 50,
96 Height: 50,
97 },
98 LayerBounds: image.Rect(0, 0, 50, 50),
99 },
100 },
101 {
102 limage.Layer{
103 Name: "Background",
104 Image: singleColourImage{
105 Colour: color.RGBA64{R: 65535, A: 65535},
106 Width: 50,
107 Height: 50,
108 },
109 LayerBounds: image.Rect(0, 0, 50, 50),
110 },
111 },
112 {
113 limage.Layer{
114 Name: "Background",
115 Image: singleColourImage{
116 Colour: color.RGBA64{R: 65535, G: 65535, B: 65535, A: 65535},
117 Width: 50,
118 Height: 50,
119 },
120 LayerBounds: image.Rect(0, 0, 50, 50),
121 },
122 },
123 }
124
125 buf := make([]byte, 683)
126
127 for n, test := range tests {
128 buf = buf[:0]
129 if err := Encode(memio.Create(&buf), test); err != nil {
130 t.Errorf("test %d: unexpected error: %s", n+1, err)
131 continue
132 }
133 f, err := zip.NewReader(memio.Open(buf), int64(len(buf)))
134 if err != nil {
135 t.Errorf("test %d: unexpected error: %s", n+1, err)
136 continue
137 }
138 l, err := Decode(f)
139 if err != nil {
140 t.Errorf("test %d: unexpected error: %s", n+1, err)
141 continue
142 }
143 if err := compareLayers(l, test); err != nil {
144 t.Errorf("test %d: %s", n+1, err)
145 }
146 }
147 }
148