limage - xcf/compressed_test.go
1 package xcf
2
3 import (
4 "fmt"
5 "image"
6 "image/color"
7 "image/draw"
8 "image/png"
9 "math/rand"
10 "os"
11 "reflect"
12 "testing"
13
14 "vimagination.zapto.org/limage"
15 "vimagination.zapto.org/memio"
16 )
17
18 func imageRandom(r image.Rectangle) image.Image {
19 i := image.NewNRGBA(r)
20 for y := r.Min.Y; y < r.Max.Y; y++ {
21 for x := r.Min.X; x < r.Max.X; x++ {
22 ci := rand.Uint32()
23 i.Set(x, y, color.NRGBA{
24 R: uint8(ci >> 24),
25 G: uint8(ci >> 16),
26 B: uint8(ci >> 8),
27 A: uint8(ci),
28 })
29 }
30 }
31 return i
32 }
33
34 func TestCompressedImages(t *testing.T) {
35 for n, test := range [...]limage.Image{
36 {
37 limage.Layer{
38 Name: "Layer 1",
39 LayerBounds: image.Rect(0, 0, 64, 64),
40 Image: image.NewNRGBA(image.Rect(0, 0, 64, 64)),
41 },
42 },
43 {
44 limage.Layer{
45 Name: "Layer 1",
46 LayerBounds: image.Rect(0, 0, 100, 100),
47 Image: image.NewNRGBA(image.Rect(0, 0, 100, 100)),
48 },
49 },
50 {
51 limage.Layer{
52 Name: "Layer 1",
53 LayerBounds: image.Rect(0, 0, 10, 10),
54 Image: imageRandom(image.Rect(0, 0, 10, 10)),
55 },
56 },
57 {
58 limage.Layer{
59 Name: "Layer 1",
60 LayerBounds: image.Rect(0, 0, 64, 64),
61 Image: imageRandom(image.Rect(0, 0, 64, 64)),
62 },
63 },
64 {
65 limage.Layer{
66 Name: "Layer 1",
67 LayerBounds: image.Rect(0, 0, 100, 100),
68 Image: imageRandom(image.Rect(0, 0, 100, 100)),
69 },
70 },
71 } {
72 g := image.NewNRGBA(test.Bounds())
73 draw.Draw(g, g.Rect, test, image.Point{}, draw.Over)
74 f, _ := os.Create(fmt.Sprintf("%d-o.png", n))
75 png.Encode(f, g)
76 f.Close()
77 var buf []byte
78 Encode(memio.Create(&buf), &test)
79 tl, err := DecodeCompressed(memio.Open(buf))
80 if err != nil {
81 t.Errorf("test %d: unexpected error: %s", n+1, err)
82 continue
83 }
84 gt := image.NewNRGBA(tl.Bounds())
85 draw.Draw(gt, g.Rect, tl, image.Point{}, draw.Over)
86 f, _ = os.Create(fmt.Sprintf("%d-p.png", n))
87 png.Encode(f, gt)
88 f.Close()
89 if !reflect.DeepEqual(g, gt) {
90 t.Errorf("test %d: output does not match test", n+1)
91 }
92 }
93 }
94