-
Notifications
You must be signed in to change notification settings - Fork 3
/
libheif_test.go
119 lines (97 loc) · 3.2 KB
/
libheif_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
package libheif
import (
"image"
"os"
"strings"
"testing"
)
// List of image names
var heicAndAvifImages = []string{"images/example-from-libheif.heic", "images/example-from-libheif.avif", "images/image1-from-me.heic", "images/samsung-generated.heic"}
// It has problems with converting images to HEIC format. Couldn't convert these images:
// var pngAndJpegImages = []string{"images/apple.jpg", "images/deer.png", "images/go.png"}
// But as I found it can easily convert libheif generated images back to heic
var pngAndJpegImages = []string{"images/libheif-generated.jpeg"}
func TestHeifToJpeg(t *testing.T) {
for _, imageName := range heicAndAvifImages {
heifImagePath := imageName
jpegImagePath := "test_images/" + strings.Replace(imageName, "/", "-", 1) + ".jpeg"
err := HeifToJpeg(heifImagePath, jpegImagePath, 80)
if err != nil {
t.Errorf("Failed to convert HEIF to JPEG: %v", err)
}
// Check if the file exists
if _, err := os.Stat(jpegImagePath); os.IsNotExist(err) {
t.Errorf("Output JPEG file does not exist: %v", err)
}
// Cleanup
t.Cleanup(func() {
os.Remove(jpegImagePath)
})
}
}
func TestHeifToPng(t *testing.T) {
for _, imageName := range heicAndAvifImages {
heifImagePath := imageName
pngImagePath := "test_images/" + strings.Replace(imageName, "/", "-", 1) + ".png"
err := HeifToJpeg(heifImagePath, pngImagePath, 80)
if err != nil {
t.Errorf("Failed to convert HEIF to JPEG: %v", err)
}
// Check if the file exists
if _, err := os.Stat(pngImagePath); os.IsNotExist(err) {
t.Errorf("Output JPEG file does not exist: %v", err)
}
// Cleanup
t.Cleanup(func() {
os.Remove(pngImagePath)
})
}
}
func TestImageToHeif(t *testing.T) {
for _, imageName := range pngAndJpegImages {
jpegOrPngImagePath := imageName
heifImagePath := "test_images/" + strings.Replace(imageName, "/", "-", 1) + ".heic"
err := ImageToHeif(jpegOrPngImagePath, heifImagePath)
if err != nil {
t.Errorf("Failed to convert Image to HEIF: %v", err)
}
// Check if the file exists
if _, err := os.Stat(heifImagePath); os.IsNotExist(err) {
t.Errorf("Output HEIF file does not exist: %v", err)
}
// Cleanup
t.Cleanup(func() {
os.Remove(heifImagePath)
})
}
}
func TestReturnImageFromHeif(t *testing.T) {
for _, imageName := range heicAndAvifImages {
heifImagePath := imageName
// Check if error is returned when converting to and returning image from HEIF
_, err := ReturnImageFromHeif(heifImagePath)
if err != nil {
t.Errorf("Failed to return image from HEIF: %v", err)
}
}
}
// Also it works well with images generated by Golang
func TestSaveImageAsHeif(t *testing.T) {
// For the purpose of this test, we will create an empty image
// with a set width and height, and save it as HEIF.
img := image.NewRGBA(image.Rect(0, 0, 100, 100))
format := "png"
newHeifImagePath := "test_images/" + "go-generated-png" + ".heic"
err := SaveImageAsHeif(img, format, newHeifImagePath)
if err != nil {
t.Errorf("Failed to save image as HEIF: %v", err)
}
// Check if the file exists
if _, err := os.Stat(newHeifImagePath); os.IsNotExist(err) {
t.Errorf("Output HEIF file does not exist: %v", err)
}
// Cleanup
t.Cleanup(func() {
os.Remove(newHeifImagePath)
})
}