1
0
mirror of https://github.com/coalaura/ffwebp.git synced 2025-07-18 06:14:34 +00:00
Files
ffwebp/ffwebp_test.go

74 lines
1.3 KiB
Go
Raw Normal View History

2024-09-08 00:32:52 +02:00
package main
import (
"bytes"
"image/png"
"log"
2025-01-22 16:58:09 +01:00
"os"
2024-09-08 00:32:52 +02:00
"testing"
)
var (
TestFiles = []string{
"test/image.avif",
"test/image.bmp",
"test/image.gif",
"test/image.heic",
"test/image.heif",
"test/image.ico",
"test/image.jpg",
"test/image.png",
"test/image.tif",
"test/image.tiff",
"test/image.webp",
"test/image.jxl",
}
)
func TestFFWebP(t *testing.T) {
2025-01-22 16:58:09 +01:00
opts.Silent = true
2025-01-22 18:19:20 +01:00
opts.Format = "png"
encoder, _ := ResolveImageEncoder()
2024-09-08 00:32:52 +02:00
for _, file := range TestFiles {
log.Printf("Testing file: %s\n", file)
2025-01-22 16:58:09 +01:00
in, err := os.OpenFile(file, os.O_RDONLY, 0)
if err != nil {
log.Fatalf("Failed to read %s: %v", file, err)
}
2024-09-08 00:32:52 +02:00
2025-01-22 16:58:09 +01:00
defer in.Close()
2024-09-08 00:32:52 +02:00
2025-01-22 16:58:09 +01:00
img, err := ReadImage(in)
2024-09-08 00:32:52 +02:00
if err != nil {
2025-01-22 16:58:09 +01:00
log.Fatalf("Failed to decode %s: %v", file, err)
}
2024-09-08 00:32:52 +02:00
2025-01-22 16:58:09 +01:00
before := img.Bounds()
var result bytes.Buffer
2025-01-22 18:19:20 +01:00
err = encoder(&result, img)
2025-01-22 16:58:09 +01:00
if err != nil {
log.Fatalf("Failed to encode png image: %v", err)
2024-09-08 00:32:52 +02:00
}
2025-01-22 16:58:09 +01:00
img, err = png.Decode(&result)
2024-09-08 00:32:52 +02:00
if err != nil {
log.Println(" - FAILED")
log.Fatalf("Failed to decode PNG image: %v\n", err)
}
2025-01-22 16:58:09 +01:00
after := img.Bounds()
if before.Max.X != after.Max.X || before.Max.Y != after.Max.Y {
2024-09-08 00:32:52 +02:00
log.Println(" - FAILED")
2025-01-22 16:58:09 +01:00
log.Fatalf("Invalid image (%dx%d != %dx%d) for file: %s\n", before.Max.X, before.Max.Y, after.Max.X, after.Max.Y, file)
2024-09-08 00:32:52 +02:00
}
log.Println(" - PASSED")
}
}