-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompression_test.go
More file actions
67 lines (56 loc) · 1.97 KB
/
compression_test.go
File metadata and controls
67 lines (56 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package codec_test
import (
"bytes"
_ "embed"
"image"
"image/draw"
"testing"
codec "github.com/alacrity-engine/resource-codec"
"github.com/stretchr/testify/assert"
)
var (
//go:embed test.jpg
TestImage []byte
)
func TestCompressLZWOrderLSBLitWidth8(t *testing.T) {
img, _, err := image.Decode(bytes.NewReader(TestImage))
assert.Nil(t, err)
imgRGBA := image.NewRGBA(image.Rect(0, 0,
img.Bounds().Dx(), img.Bounds().Dy()))
draw.Draw(imgRGBA, imgRGBA.Bounds(),
img.Bounds(), img.Bounds().Min, draw.Src)
compressedPix, err := codec.CompressLZWOrderLSBLitWidth8(imgRGBA.Pix)
assert.Nil(t, err)
assert.Equal(t, 7155, len(compressedPix))
}
func TestDecompressLZWOrderLSBLitWidth8(t *testing.T) {
img, _, err := image.Decode(bytes.NewReader(TestImage))
assert.Nil(t, err)
imgRGBA := image.NewRGBA(image.Rect(0, 0,
img.Bounds().Dx(), img.Bounds().Dy()))
draw.Draw(imgRGBA, imgRGBA.Bounds(),
img.Bounds(), img.Bounds().Min, draw.Src)
compressedPix, err := codec.CompressLZWOrderLSBLitWidth8(imgRGBA.Pix)
assert.Nil(t, err)
assert.Equal(t, 7155, len(compressedPix))
decompressedPix, err := codec.DecompressLZWOrderLSBLitWidth8(compressedPix,
imgRGBA.Rect.Dx()*imgRGBA.Rect.Dy()*4)
assert.Nil(t, err)
assert.Equal(t, imgRGBA.Rect.Dx()*imgRGBA.Rect.Dy()*4, len(decompressedPix))
hashOriginal, err := codec.Hash(imgRGBA.Pix)
assert.Nil(t, err)
hashDecompressed, err := codec.Hash(decompressedPix)
assert.Nil(t, err)
assert.ElementsMatch(t, hashOriginal, hashDecompressed)
}
func TestCompressPicture(t *testing.T) {
img, _, err := image.Decode(bytes.NewReader(TestImage))
assert.Nil(t, err)
picture, err := codec.NewPictureFromImage(img)
assert.Nil(t, err)
compressedPicture, err := picture.Compress()
assert.Nil(t, err)
assert.Equal(t, 7155, len(compressedPicture.CompressedPix))
assert.Equal(t, codec.HashAlgorithmKeccak256, compressedPicture.OriginalHashAlgorithm)
assert.Equal(t, codec.CompressionAlgorithmLZWOrderLSBLitWidth8, compressedPicture.CompressionAlgorithm)
}