1.4.5.2
This commit is contained in:
1461
EdgeNode/internal/utils/heif/_libheif/heif.go
Normal file
1461
EdgeNode/internal/utils/heif/_libheif/heif.go
Normal file
File diff suppressed because it is too large
Load Diff
178
EdgeNode/internal/utils/heif/_libheif/heif_test.go
Normal file
178
EdgeNode/internal/utils/heif/_libheif/heif_test.go
Normal file
@@ -0,0 +1,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 _libheif
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
12
EdgeNode/internal/utils/heif/_libheif/main/build.sh
Normal file
12
EdgeNode/internal/utils/heif/_libheif/main/build.sh
Normal file
@@ -0,0 +1,12 @@
|
||||
#!/bin/bash
|
||||
|
||||
env GOOS=linux \
|
||||
GOARCH=amd64 \
|
||||
CC="/Users/WorkSpace/gcc/aarch64/13.2.0/x86_64-unknown-linux-gnu/bin/x86_64-linux-gnu-gcc" \
|
||||
CXX="/Users/WorkSpace/gcc/aarch64/13.2.0/x86_64-unknown-linux-gnu/bin/x86_64-linux-gnu-g++" \
|
||||
CGO_ENABLED=1 \
|
||||
CGO_LDFLAGS="-L/Users/WorkSpace/EdgeProject/EdgeNode/libs/_libheif/amd64 -lstdc++ -lm -ldl -lheif" \
|
||||
CGO_CFLAGS="-I/Users/WorkSpace/EdgeProject/EdgeNode/libs/_libheif/src/libheif" \
|
||||
CGO_CXXFLAGS="-I/Users/WorkSpace/EdgeProject/EdgeNode/libs/_libheif/src/libheif" \
|
||||
go build -o avif-test -ldflags "-linkmode external -extldflags -static -s -w" main.go
|
||||
#scp avif-test root@192.168.2.61:/root/
|
||||
47
EdgeNode/internal/utils/heif/_libheif/main/main.go
Normal file
47
EdgeNode/internal/utils/heif/_libheif/main/main.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/heif/libheif"
|
||||
"image"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
var dir = cwd + "/internal/utils/heif/libheif/main"
|
||||
data, err := os.ReadFile(dir + "/test.jpg")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println(image.DecodeConfig(bytes.NewReader(data)))
|
||||
|
||||
img, _, err := image.Decode(bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Print(reflect.TypeOf(img).String())
|
||||
|
||||
ctx, err := _libheif.EncodeFromImage(img, _libheif.CompressionUndefined, 30, _libheif.LosslessModeDisabled, _libheif.LoggingLevelBasic)
|
||||
if err != nil {
|
||||
log.Fatal(fmt.Errorf("encode failed: %w", err))
|
||||
}
|
||||
err = ctx.WriteToFile(dir + "/test.avif")
|
||||
if err != nil {
|
||||
log.Fatal(fmt.Errorf("write failed: %w", err))
|
||||
}
|
||||
}
|
||||
BIN
EdgeNode/internal/utils/heif/_libheif/main/test.avif
Normal file
BIN
EdgeNode/internal/utils/heif/_libheif/main/test.avif
Normal file
Binary file not shown.
BIN
EdgeNode/internal/utils/heif/_libheif/main/test.jpg
Normal file
BIN
EdgeNode/internal/utils/heif/_libheif/main/test.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.6 MiB |
BIN
EdgeNode/internal/utils/heif/_libheif/main/test.png
Normal file
BIN
EdgeNode/internal/utils/heif/_libheif/main/test.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 226 KiB |
Reference in New Issue
Block a user