Debian Codegraph @

go/heif/heif_test.go

raw
1 /*
2 * Test for GO interface to libheif
3 *
4 * MIT License
5 *
6 * Copyright (c) 2018 Joachim Bauch <[email protected]>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 package heif
28
29 import (
30 "fmt"
31 "image"
32 "io/ioutil"
33 "os"
34 "path"
35 "testing"
36 )
37
38 func TestGetVersion(t *testing.T) {
39 version := GetVersion()
40 if version == "" {
41 t.Fatal("Version is missing")
42 }
43 }
44
45 type decodeTest struct {
46 colorspace Colorspace
47 chroma Chroma
48 }
49
50 func CheckHeifImage(t *testing.T, handle *ImageHandle, thumbnail bool) {
51 handle.GetWidth()
52 handle.GetHeight()
53 handle.HasAlphaChannel()
54 handle.HasDepthImage()
55 count := handle.GetNumberOfDepthImages()
56 if ids := handle.GetListOfDepthImageIDs(); len(ids) != count {
57 t.Errorf("Expected %d depth image ids, got %d", count, len(ids))
58 }
59 if !thumbnail {
60 count = handle.GetNumberOfThumbnails()
61 ids := handle.GetListOfThumbnailIDs()
62 if len(ids) != count {
63 t.Errorf("Expected %d thumbnail image ids, got %d", count, len(ids))
64 }
65 for _, id := range ids {
66 if thumb, err := handle.GetThumbnail(id); err != nil {
67 t.Errorf("Could not get thumbnail %d: %s", id, err)
68 } else {
69 CheckHeifImage(t, thumb, true)
70 }
71 }
72 }
73
74 decodeTests := []decodeTest{
75 decodeTest{ColorspaceUndefined, ChromaUndefined},
76 decodeTest{ColorspaceYCbCr, Chroma420},
77 decodeTest{ColorspaceYCbCr, Chroma422},
78 decodeTest{ColorspaceYCbCr, Chroma444},
79 decodeTest{ColorspaceRGB, Chroma444},
80 decodeTest{ColorspaceRGB, ChromaInterleavedRGB},
81 decodeTest{ColorspaceRGB, ChromaInterleavedRGBA},
82 decodeTest{ColorspaceRGB, ChromaInterleavedRRGGBB_BE},
83 decodeTest{ColorspaceRGB, ChromaInterleavedRRGGBBAA_BE},
84 }
85 for _, test := range decodeTests {
86 if img, err := handle.DecodeImage(test.colorspace, test.chroma, nil); err != nil {
87 t.Errorf("Could not decode image with %v / %v: %s", test.colorspace, test.chroma, err)
88 } else {
89 img.GetColorspace()
90 img.GetChromaFormat()
91
92 if _, err := img.GetImage(); err != nil {
93 t.Errorf("Could not get image with %v /%v: %s", test.colorspace, test.chroma, err)
94 continue
95 }
96 }
97 }
98 }
99
100 func CheckHeifFile(t *testing.T, ctx *Context) {
101 if count := ctx.GetNumberOfTopLevelImages(); count != 2 {
102 t.Errorf("Expected %d top level images, got %d", 2, count)
103 }
104 if ids := ctx.GetListOfTopLevelImageIDs(); len(ids) != 2 {
105 t.Errorf("Expected %d top level image ids, got %+v", 2, ids)
106 }
107 if _, err := ctx.GetPrimaryImageID(); err != nil {
108 t.Errorf("Expected a primary image, got %s", err)
109 }
110 if handle, err := ctx.GetPrimaryImageHandle(); err != nil {
111 t.Errorf("Could not get primary image handle: %s", err)
112 } else {
113 if !handle.IsPrimaryImage() {
114 t.Error("Expected primary image")
115 }
116 CheckHeifImage(t, handle, false)
117 }
118 }
119
120 func TestReadFromFile(t *testing.T) {
121 ctx, err := NewContext()
122 if err != nil {
123 t.Fatalf("Can't create context: %s", err)
124 }
125
126 filename := path.Join("..", "..", "examples", "example.heic")
127 if err := ctx.ReadFromFile(filename); err != nil {
128 t.Fatalf("Can't read from %s: %s", filename, err)
129 }
130
131 CheckHeifFile(t, ctx)
132 }
133
134 func TestReadFromMemory(t *testing.T) {
135 ctx, err := NewContext()
136 if err != nil {
137 t.Fatalf("Can't create context: %s", err)
138 }
139
140 filename := path.Join("..", "..", "examples", "example.heic")
141 data, err := ioutil.ReadFile(filename)
142 if err != nil {
143 t.Fatalf("Can't read file %s: %s", filename, err)
144 }
145 if err := ctx.ReadFromMemory(data); err != nil {
146 t.Fatalf("Can't read from memory: %s", err)
147 }
148 data = nil // Make sure future processing works if "data" is GC'd
149
150 CheckHeifFile(t, ctx)
151 }
152
153 func TestReadImage(t *testing.T) {
154 filename := path.Join("..", "..", "examples", "example.heic")
155 fp, err := os.Open(filename)
156 if err != nil {
157 t.Fatalf("Could not open %s: %s", filename, err)
158 }
159 defer fp.Close()
160
161 config, format1, err := image.DecodeConfig(fp)
162 if err != nil {
163 t.Fatalf("Could not load image config from %s: %s", filename, err)
164 }
165 if format1 != "heif" {
166 t.Errorf("Expected format heif, got %s", format1)
167 }
168 if _, err := fp.Seek(0, 0); err != nil {
169 t.Fatalf("Could not seek to start of %s: %s", filename, err)
170 }
171
172 img, format2, err := image.Decode(fp)
173 if err != nil {
174 t.Fatalf("Could not load image from %s: %s", filename, err)
175 }
176 if format2 != "heif" {
177 t.Errorf("Expected format heif, got %s", format2)
178 }
179
180 r := img.Bounds()
181 if config.Width != (r.Max.X-r.Min.X) || config.Height != (r.Max.Y-r.Min.Y) {
182 fmt.Printf("Image size %+v does not match config %+v\n", r, config)
183 }
184 }