forked from discord/lilliput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thumbhash_test.go
82 lines (72 loc) · 3.06 KB
/
thumbhash_test.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package lilliput
import (
"encoding/base64"
"io/ioutil"
"testing"
)
func TestThumbhash(t *testing.T) {
checkImage := func(expectedB64Hash, filePath string, ops *ImageOps, dst []byte) {
inputBuf, err := ioutil.ReadFile(filePath)
if err != nil {
t.Fatalf("failed to read input file %q: %v", filePath, err)
}
decoder, err := NewDecoder(inputBuf)
if err != nil {
t.Fatalf("error decoding image %q: %v", filePath, err)
}
defer decoder.Close()
header, err := decoder.Header()
if err != nil {
t.Fatalf("error reading image header of %q: %v", filePath, err)
}
opts := &ImageOptions{
FileType: ".thumbhash",
Width: header.width,
Height: header.height,
ResizeMethod: ImageOpsNoResize,
NormalizeOrientation: true,
}
hash, err := ops.Transform(decoder, opts, dst)
if err != nil {
t.Fatalf("error transforming image %q: %v", filePath, err)
}
b64Hash := base64.StdEncoding.EncodeToString(hash)
if b64Hash != expectedB64Hash {
t.Errorf("hash of %q is %q but should be %q",
filePath, b64Hash, expectedB64Hash)
}
}
ops := NewImageOps(8192)
defer ops.Close()
dst := make([]byte, 0, 1024*1024)
// These test images came from the demo page at:
// https://evanw.github.io/thumbhash/
//
// The expected thumbhashes in the tests were generated using the reference
// rust implementation there.
//
// Note the thumbhashes for 'field.jpg' and 'opera.png' generated by the
// rust reference code were slightly different than the respective hashes in
// the demo page (presumably generated by the JS reference implementation).
//
// This is not very surprising given the heavy reliance on floating point
// math. The differences were likely rounding errors. The decoded images
// from those hashes were visually identical.
checkImage("1QcSHQRnh493V4dIh4eXh1h4kJUI", "data/sunrise.jpg", ops, dst)
checkImage("3PcNNYSFeXh/d3eld0iHZoZgVwh2", "data/sunset.jpg", ops, dst)
checkImage("3OcRJYB4d3h/iIeHeEh3eIhw+j3A", "data/field.jpg", ops, dst)
checkImage("HBkSHYSIeHiPiHh8eJd4eTN0EEQG", "data/fall.jpg", ops, dst)
checkImage("VggKDYAW6lZvdYd6d2iZh/p4GE/k", "data/street.jpg", ops, dst)
checkImage("2fcZFIB3iId/h3iJh4aIYJ2V8g==", "data/mountain.jpg", ops, dst)
checkImage("IQgSLYZ6iHePh4h1eFeHh4dwgwg3", "data/coast.jpg", ops, dst)
checkImage("YJqGPQw7sFlslqhFafSE+Q6oJ1h2iHB2Rw==", "data/firefox.png", ops, dst)
checkImage("mYqDBQQnxnj0JoLYdN7f8JhpuDeHiHdwZw==", "data/opera.png", ops, dst)
// Test other image formats, bit depths, and color spaces.
checkImage("YJqGPQw7oFlslqhGafOE+Q6oJ1h2iHBlVw==", "data/firefox-16bit.png", ops, dst)
checkImage("YJqGPQw7sFlslqhFafSE+Q6oJ1h2iHB2Rw==", "data/firefox-16bit-alpha.png", ops, dst)
checkImage("FwgOBwAxOWl4l3aQpFiIN5iHBgAAAAAA", "data/firefox-gray.jpg", ops, dst)
checkImage("4AeKBQA7oFl7lqhmaDBp92yJJ1h2iHB2Rw==", "data/firefox-gray-alpha.webp", ops, dst)
checkImage("EwiCBQAnwnjzJpHIZAAAAAAAuDeHiHdwZw==", "data/opera-gray-alpha.png", ops, dst)
// Test downsampling.
checkImage("VvYRNQRod3x3B4iHeHhYiHeAeQUo", "data/large-sunrise.jpg", ops, dst)
}