-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
heif_test.go
178 lines (160 loc) · 5.01 KB
/
heif_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* GO interface to libheif
* Copyright (c) 2018 struktur AG, Joachim Bauch <bauch@struktur.de>
*
* This file is part of heif, an example application using libheif.
*
* libheif is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* libheif is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libheif. If not, see <http://www.gnu.org/licenses/>.
*/
package heif
import (
"fmt"
"image"
"io/ioutil"
"os"
"path"
"testing"
)
func TestGetVersion(t *testing.T) {
version := GetVersion()
if version == "" {
t.Fatal("Version is missing")
}
}
type decodeTest struct {
colorspace Colorspace
chroma Chroma
}
func CheckHeifImage(t *testing.T, handle *ImageHandle, thumbnail bool) {
handle.GetWidth()
handle.GetHeight()
handle.HasAlphaChannel()
handle.HasDepthImage()
count := handle.GetNumberOfDepthImages()
if ids := handle.GetListOfDepthImageIDs(); len(ids) != count {
t.Errorf("Expected %d depth image ids, got %d", count, len(ids))
}
if !thumbnail {
count = handle.GetNumberOfThumbnails()
ids := handle.GetListOfThumbnailIDs()
if len(ids) != count {
t.Errorf("Expected %d thumbnail image ids, got %d", count, len(ids))
}
for _, id := range ids {
if thumb, err := handle.GetThumbnail(id); err != nil {
t.Errorf("Could not get thumbnail %d: %s", id, err)
} else {
CheckHeifImage(t, thumb, true)
}
}
}
decodeTests := []decodeTest{
decodeTest{ColorspaceUndefined, ChromaUndefined},
decodeTest{ColorspaceYCbCr, Chroma420},
decodeTest{ColorspaceYCbCr, Chroma422},
decodeTest{ColorspaceYCbCr, Chroma444},
decodeTest{ColorspaceRGB, Chroma444},
decodeTest{ColorspaceRGB, ChromaInterleavedRGB},
decodeTest{ColorspaceRGB, ChromaInterleavedRGBA},
decodeTest{ColorspaceRGB, ChromaInterleavedRRGGBB_BE},
decodeTest{ColorspaceRGB, ChromaInterleavedRRGGBBAA_BE},
}
for _, test := range decodeTests {
if img, err := handle.DecodeImage(test.colorspace, test.chroma, nil); err != nil {
t.Errorf("Could not decode image with %v / %v: %s", test.colorspace, test.chroma, err)
} else {
img.GetColorspace()
img.GetChromaFormat()
if _, err := img.GetImage(); err != nil {
t.Errorf("Could not get image with %v /%v: %s", test.colorspace, test.chroma, err)
continue
}
}
}
}
func CheckHeifFile(t *testing.T, ctx *Context) {
if count := ctx.GetNumberOfTopLevelImages(); count != 2 {
t.Errorf("Expected %d top level images, got %d", 2, count)
}
if ids := ctx.GetListOfTopLevelImageIDs(); len(ids) != 2 {
t.Errorf("Expected %d top level image ids, got %+v", 2, ids)
}
if _, err := ctx.GetPrimaryImageID(); err != nil {
t.Errorf("Expected a primary image, got %s", err)
}
if handle, err := ctx.GetPrimaryImageHandle(); err != nil {
t.Errorf("Could not get primary image handle: %s", err)
} else {
if !handle.IsPrimaryImage() {
t.Error("Expected primary image")
}
CheckHeifImage(t, handle, false)
}
}
func TestReadFromFile(t *testing.T) {
ctx, err := NewContext()
if err != nil {
t.Fatalf("Can't create context: %s", err)
}
filename := path.Join("..", "..", "examples", "example.heic")
if err := ctx.ReadFromFile(filename); err != nil {
t.Fatalf("Can't read from %s: %s", filename, err)
}
CheckHeifFile(t, ctx)
}
func TestReadFromMemory(t *testing.T) {
ctx, err := NewContext()
if err != nil {
t.Fatalf("Can't create context: %s", err)
}
filename := path.Join("..", "..", "examples", "example.heic")
data, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatalf("Can't read file %s: %s", filename, err)
}
if err := ctx.ReadFromMemory(data); err != nil {
t.Fatalf("Can't read from memory: %s", err)
}
data = nil // Make sure future processing works if "data" is GC'd
CheckHeifFile(t, ctx)
}
func TestReadImage(t *testing.T) {
filename := path.Join("..", "..", "examples", "example.heic")
fp, err := os.Open(filename)
if err != nil {
t.Fatalf("Could not open %s: %s", filename, err)
}
defer fp.Close()
config, format1, err := image.DecodeConfig(fp)
if err != nil {
t.Fatalf("Could not load image config from %s: %s", filename, err)
}
if format1 != "heif" {
t.Errorf("Expected format heif, got %s", format1)
}
if _, err := fp.Seek(0, 0); err != nil {
t.Fatalf("Could not seek to start of %s: %s", filename, err)
}
img, format2, err := image.Decode(fp)
if err != nil {
t.Fatalf("Could not load image from %s: %s", filename, err)
}
if format2 != "heif" {
t.Errorf("Expected format heif, got %s", format2)
}
r := img.Bounds()
if config.Width != (r.Max.X-r.Min.X) || config.Height != (r.Max.Y-r.Min.Y) {
fmt.Printf("Image size %+v does not match config %+v\n", r, config)
}
}