1package heic
2
3import (
4	"fmt"
5	"path"
6	"testing"
7
8	"github.com/k0kubun/pp"
9)
10
11func TestExifCount(t *testing.T) {
12	ctx, err := NewContext()
13	if err != nil {
14		t.Fatalf("Can't create context: %s", err)
15	}
16
17	filename := path.Join("examples", "mont.heic")
18	if err := ctx.ReadFromFile(filename); err != nil {
19		t.Fatalf("Can't read from %s: %s", filename, err)
20	}
21
22	if count := ctx.GetNumberOfTopLevelImages(); count != 16 {
23		t.Errorf("Expected %d top level images, got %d", 16, count)
24	}
25	if ids := ctx.GetListOfTopLevelImageIDs(); len(ids) != 16 {
26		t.Errorf("Expected %d top level image ids, got %+v", 16, ids)
27	}
28	if _, err := ctx.GetPrimaryImageID(); err != nil {
29		t.Errorf("Expected a primary image, got %s", err)
30	}
31	handle, err := ctx.GetPrimaryImageHandle()
32	if err != nil {
33		t.Errorf("Could not get primary image handle: %s", err)
34	}
35	if !handle.IsPrimaryImage() {
36		t.Error("Expected primary image")
37	}
38
39	exifCount := handle.ExifCount()
40	fmt.Println("Exif count", exifCount)
41
42	exifIDs := handle.ExifIDs()
43	fmt.Println("Exif IDs", exifIDs)
44
45	metadataCount := handle.MetadataCount()
46	fmt.Println("Metadata count", metadataCount)
47
48	metadataIDs := handle.MetadataIDs()
49	fmt.Println("Metadata IDs", metadataIDs)
50
51	metadataID := metadataIDs[0]
52
53	times, err := handle.ImageTimes(metadataID)
54	if err != nil {
55		t.Fail()
56	}
57	pp.Print(times)
58
59	thumbnail := false
60
61	handle.GetWidth()
62	handle.GetHeight()
63	handle.HasAlphaChannel()
64	handle.HasDepthImage()
65	count := handle.GetNumberOfDepthImages()
66	if ids := handle.GetListOfDepthImageIDs(); len(ids) != count {
67		t.Errorf("Expected %d depth image ids, got %d", count, len(ids))
68	}
69	if !thumbnail {
70		count = handle.GetNumberOfThumbnails()
71		ids := handle.GetListOfThumbnailIDs()
72		if len(ids) != count {
73			t.Errorf("Expected %d thumbnail image ids, got %d", count, len(ids))
74		}
75		for _, id := range ids {
76			if thumb, err := handle.GetThumbnail(id); err != nil {
77				t.Errorf("Could not get thumbnail %d: %s", id, err)
78			} else {
79				CheckHeifImage(t, thumb, true)
80			}
81		}
82	}
83
84	if img, err := handle.DecodeImage(ColorspaceUndefined, ChromaUndefined, nil); err != nil {
85		t.Errorf("Could not decode image: %s", err)
86	} else {
87		img.GetColorspace()
88		img.GetChromaFormat()
89	}
90
91	decodeTests := []decodeTest{
92		decodeTest{ColorspaceYCbCr, Chroma420},
93		decodeTest{ColorspaceYCbCr, Chroma422},
94		decodeTest{ColorspaceYCbCr, Chroma444},
95		decodeTest{ColorspaceRGB, Chroma444},
96		decodeTest{ColorspaceRGB, ChromaInterleavedRGB},
97		decodeTest{ColorspaceRGB, ChromaInterleavedRGBA},
98		decodeTest{ColorspaceRGB, ChromaInterleavedRRGGBB_BE},
99		decodeTest{ColorspaceRGB, ChromaInterleavedRRGGBBAA_BE},
100	}
101	for _, test := range decodeTests {
102		if img, err := handle.DecodeImage(test.colorspace, test.chroma, nil); err != nil {
103			t.Errorf("Could not decode image with %v / %v: %s", test.colorspace, test.chroma, err)
104		} else {
105			img.GetColorspace()
106			img.GetChromaFormat()
107
108			if _, err := img.GetImage(); err != nil {
109				t.Errorf("Could not get image with %v /%v: %s", test.colorspace, test.chroma, err)
110				continue
111			}
112		}
113	}
114
115}
116