-
Notifications
You must be signed in to change notification settings - Fork 0
/
heif_test.go
171 lines (149 loc) · 4.65 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
/*
* Go interface to libheif
*
* Copyright (c) 2018-2024 struktur AG, Joachim Bauch <[email protected]>
*
* 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 libheif
import (
"fmt"
"image"
"os"
"path"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetVersion(t *testing.T) {
require := require.New(t)
version := GetVersion()
require.NotEmpty(version, "Version is missing")
}
type decodeTest struct {
colorspace Colorspace
chroma Chroma
}
func CheckHeifImage(t *testing.T, handle *ImageHandle, thumbnail bool) {
t.Helper()
t.Run("properties", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)
handle.GetWidth()
handle.GetHeight()
handle.HasAlphaChannel()
handle.HasDepthImage()
count := handle.GetNumberOfDepthImages()
ids := handle.GetListOfDepthImageIDs()
assert.Len(ids, count, "Number of depth image ids mismatched")
if !thumbnail {
count = handle.GetNumberOfThumbnails()
ids := handle.GetListOfThumbnailIDs()
assert.Len(ids, count, "Number of thumbnail image ids mismatched")
for _, id := range ids {
id := id
t.Run(fmt.Sprintf("thumb-%d", id), func(t *testing.T) {
t.Parallel()
if thumb, err := handle.GetThumbnail(id); assert.NoError(err) {
CheckHeifImage(t, thumb, true)
}
})
}
}
})
decodeTests := []decodeTest{
{ColorspaceUndefined, ChromaUndefined},
{ColorspaceYCbCr, Chroma420},
{ColorspaceYCbCr, Chroma422},
{ColorspaceYCbCr, Chroma444},
{ColorspaceRGB, Chroma444},
{ColorspaceRGB, ChromaInterleavedRGB},
{ColorspaceRGB, ChromaInterleavedRGBA},
{ColorspaceRGB, ChromaInterleavedRRGGBB_BE},
{ColorspaceRGB, ChromaInterleavedRRGGBBAA_BE},
}
for _, test := range decodeTests {
test := test
t.Run(fmt.Sprintf("%d/%d", test.colorspace, test.chroma), func(t *testing.T) {
t.Parallel()
assert := assert.New(t)
if img, err := handle.DecodeImage(test.colorspace, test.chroma, nil); assert.NoError(err, "Error decoding image") {
img.GetColorspace()
img.GetChromaFormat()
_, err := img.GetImage()
assert.NoError(err)
}
})
}
}
func CheckHeifFile(t *testing.T, ctx *Context) {
t.Helper()
assert := assert.New(t)
assert.Equal(2, ctx.GetNumberOfTopLevelImages(), "Number of top level images mismatched")
assert.Len(ctx.GetListOfTopLevelImageIDs(), 2, "Number of top level image ids mismatched")
_, err := ctx.GetPrimaryImageID()
assert.NoError(err, "Error getting primary image id")
if handle, err := ctx.GetPrimaryImageHandle(); assert.NoError(err, "Error getting primary image handler") {
assert.True(handle.IsPrimaryImage(), "Expected primary image")
t.Run("primary", func(t *testing.T) {
t.Parallel()
CheckHeifImage(t, handle, false)
})
}
}
func TestReadFromFile(t *testing.T) {
t.Parallel()
require := require.New(t)
ctx, err := NewContext()
require.NoError(err, "Can't create context")
filename := path.Join("testdata", "example.heic")
require.NoError(ctx.ReadFromFile(filename))
CheckHeifFile(t, ctx)
}
func TestReadFromMemory(t *testing.T) {
t.Parallel()
require := require.New(t)
ctx, err := NewContext()
require.NoError(err, "Can't create context")
filename := path.Join("testdata", "example.heic")
data, err := os.ReadFile(filename)
require.NoError(err, "Can't read file")
require.NoError(ctx.ReadFromMemory(data))
// Make sure future processing works if "data" is GC'd
data = nil // nolint
runtime.GC()
CheckHeifFile(t, ctx)
}
func TestReadImage(t *testing.T) {
t.Parallel()
assert := assert.New(t)
require := require.New(t)
filename := path.Join("testdata", "example.heic")
fp, err := os.Open(filename)
require.NoError(err)
defer fp.Close()
config, format1, err := image.DecodeConfig(fp)
require.NoError(err)
assert.Equal("heif", format1)
_, err = fp.Seek(0, 0)
require.NoError(err)
img, format2, err := image.Decode(fp)
require.NoError(err)
assert.Equal("heif", 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)
}
}