forked from mtusi2026/mtusi2026lab5
195 lines
5.8 KiB
Go
195 lines
5.8 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/binary"
|
|
"image"
|
|
"image/color"
|
|
"image/jpeg"
|
|
"image/png"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestFitSizePreservesAspectAndNeverUpscales(t *testing.T) {
|
|
cases := []struct {
|
|
w, h, maxW, maxH int
|
|
wantW, wantH int
|
|
}{
|
|
{320, 200, 256, 256, 256, 160},
|
|
{200, 320, 256, 256, 160, 256},
|
|
{100, 50, 256, 256, 100, 50},
|
|
{4000, 1000, 1280, 1280, 1280, 320},
|
|
{1000, 4000, 256, 256, 64, 256},
|
|
{1, 4000, 256, 256, 1, 256},
|
|
}
|
|
for _, tc := range cases {
|
|
w, h := fitSize(tc.w, tc.h, tc.maxW, tc.maxH)
|
|
if w != tc.wantW || h != tc.wantH {
|
|
t.Fatalf("fitSize(%d,%d,%d,%d) = %dx%d, want %dx%d",
|
|
tc.w, tc.h, tc.maxW, tc.maxH, w, h, tc.wantW, tc.wantH)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProcessReportsTiming(t *testing.T) {
|
|
file := pngFile(t, 320, 200, false)
|
|
_, stats, err := New(40_000_000, 256<<20).Process(context.Background(), file)
|
|
if err != nil {
|
|
t.Fatalf("Process: %v", err)
|
|
}
|
|
if stats.Decode <= 0 || stats.Scale <= 0 || stats.JPEGEncode <= 0 || stats.WebPEncode <= 0 {
|
|
t.Fatalf("incomplete timing stats: %+v", stats)
|
|
}
|
|
if stats.Total() != stats.Decode+stats.Scale+stats.JPEGEncode+stats.WebPEncode {
|
|
t.Fatalf("total %v does not match its parts: %+v", stats.Total(), stats)
|
|
}
|
|
if stats.Total() <= 0 {
|
|
t.Fatalf("total timing must be positive: %+v", stats)
|
|
}
|
|
}
|
|
|
|
func TestProcessProducesContractVariants(t *testing.T) {
|
|
file := pngFile(t, 320, 200, false)
|
|
variants, _, err := New(40_000_000, 256<<20).Process(context.Background(), file)
|
|
if err != nil {
|
|
t.Fatalf("Process: %v", err)
|
|
}
|
|
if len(variants) != 3 {
|
|
t.Fatalf("got %d variants, want 3", len(variants))
|
|
}
|
|
|
|
byName := map[string]Encoded{}
|
|
for _, variant := range variants {
|
|
byName[variant.Name] = variant
|
|
}
|
|
thumbnail := byName["thumbnail"]
|
|
if thumbnail.ContentType != "image/jpeg" || thumbnail.Width != 256 || thumbnail.Height != 160 {
|
|
t.Fatalf("thumbnail = %s %dx%d, want image/jpeg 256x160",
|
|
thumbnail.ContentType, thumbnail.Width, thumbnail.Height)
|
|
}
|
|
decoded, err := jpeg.Decode(bytes.NewReader(thumbnail.Bytes))
|
|
if err != nil {
|
|
t.Fatalf("decode thumbnail: %v", err)
|
|
}
|
|
if decoded.Bounds().Dx() != 256 || decoded.Bounds().Dy() != 160 {
|
|
t.Fatalf("actual thumbnail bounds = %v", decoded.Bounds())
|
|
}
|
|
|
|
crop := byName["crop"]
|
|
if crop.ContentType != "image/jpeg" || crop.Width != 128 || crop.Height != 128 {
|
|
t.Fatalf("crop = %s %dx%d", crop.ContentType, crop.Width, crop.Height)
|
|
}
|
|
optimized := byName["optimized"]
|
|
if optimized.ContentType != "image/webp" || optimized.Width != 320 || optimized.Height != 200 {
|
|
t.Fatalf("optimized = %s %dx%d, want image/webp 320x200", optimized.ContentType, optimized.Width, optimized.Height)
|
|
}
|
|
}
|
|
|
|
func TestThumbnailCompositesAlphaOnWhite(t *testing.T) {
|
|
file := pngFile(t, 64, 64, true)
|
|
variants, _, err := New(40_000_000, 256<<20).Process(context.Background(), file)
|
|
if err != nil {
|
|
t.Fatalf("Process: %v", err)
|
|
}
|
|
decoded, err := jpeg.Decode(bytes.NewReader(variants[0].Bytes))
|
|
if err != nil {
|
|
t.Fatalf("decode thumbnail: %v", err)
|
|
}
|
|
r, g, b, _ := decoded.At(32, 32).RGBA()
|
|
if r < 0xf000 || g < 0xf000 || b < 0xf000 {
|
|
t.Fatalf("transparent pixel is not white: %04x %04x %04x", r, g, b)
|
|
}
|
|
}
|
|
|
|
func TestEXIFOrientationRotatesBeforeResize(t *testing.T) {
|
|
file := jpegFileWithOrientation(t, 320, 200, 6)
|
|
if got := jpegOrientation(file); got != 6 {
|
|
t.Fatalf("jpegOrientation = %d, want 6", got)
|
|
}
|
|
variants, _, err := New(40_000_000, 256<<20).Process(context.Background(), file)
|
|
if err != nil {
|
|
t.Fatalf("Process: %v", err)
|
|
}
|
|
if variants[0].Width != 160 || variants[0].Height != 256 {
|
|
t.Fatalf("oriented thumbnail = %dx%d, want 160x256", variants[0].Width, variants[0].Height)
|
|
}
|
|
}
|
|
|
|
func TestParseTIFFOrientationBigEndian(t *testing.T) {
|
|
tiff := []byte{
|
|
'M', 'M', 0x00, 0x2a, 0x00, 0x00, 0x00, 0x08,
|
|
0x00, 0x01,
|
|
0x01, 0x12, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00,
|
|
}
|
|
if got := parseTIFFOrientation(tiff); got != 8 {
|
|
t.Fatalf("parseTIFFOrientation = %d, want 8", got)
|
|
}
|
|
}
|
|
|
|
func pngFile(t *testing.T, width, height int, transparent bool) *os.File {
|
|
t.Helper()
|
|
img := image.NewRGBA(image.Rect(0, 0, width, height))
|
|
for y := 0; y < height; y++ {
|
|
for x := 0; x < width; x++ {
|
|
alpha := uint8(0xff)
|
|
if transparent {
|
|
alpha = 0
|
|
}
|
|
img.Set(x, y, color.RGBA{R: 10, G: 120, B: 200, A: alpha})
|
|
}
|
|
}
|
|
var buf bytes.Buffer
|
|
if err := png.Encode(&buf, img); err != nil {
|
|
t.Fatalf("encode png: %v", err)
|
|
}
|
|
return tempFile(t, "image.png", buf.Bytes())
|
|
}
|
|
|
|
func jpegFileWithOrientation(t *testing.T, width, height int, orientation uint16) *os.File {
|
|
t.Helper()
|
|
img := image.NewRGBA(image.Rect(0, 0, width, height))
|
|
var buf bytes.Buffer
|
|
if err := jpeg.Encode(&buf, img, nil); err != nil {
|
|
t.Fatalf("encode jpeg: %v", err)
|
|
}
|
|
|
|
payload := []byte{'E', 'x', 'i', 'f', 0, 0}
|
|
tiff := make([]byte, 26)
|
|
copy(tiff, []byte{'I', 'I'})
|
|
binary.LittleEndian.PutUint16(tiff[2:4], 42)
|
|
binary.LittleEndian.PutUint32(tiff[4:8], 8)
|
|
binary.LittleEndian.PutUint16(tiff[8:10], 1)
|
|
binary.LittleEndian.PutUint16(tiff[10:12], 0x0112)
|
|
binary.LittleEndian.PutUint16(tiff[12:14], 3)
|
|
binary.LittleEndian.PutUint32(tiff[14:18], 1)
|
|
binary.LittleEndian.PutUint16(tiff[18:20], orientation)
|
|
payload = append(payload, tiff...)
|
|
|
|
segment := append([]byte{0xff, 0xe1}, 0, 0)
|
|
binary.BigEndian.PutUint16(segment[2:4], uint16(len(payload)+2))
|
|
segment = append(segment, payload...)
|
|
|
|
data := append([]byte{}, buf.Bytes()[:2]...)
|
|
data = append(data, segment...)
|
|
data = append(data, buf.Bytes()[2:]...)
|
|
return tempFile(t, "oriented.jpg", data)
|
|
}
|
|
|
|
func tempFile(t *testing.T, name string, data []byte) *os.File {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), name)
|
|
if err := os.WriteFile(path, data, 0o600); err != nil {
|
|
t.Fatalf("write %s: %v", path, err)
|
|
}
|
|
file, err := os.Open(path)
|
|
if err != nil {
|
|
t.Fatalf("open %s: %v", path, err)
|
|
}
|
|
t.Cleanup(func() { _ = file.Close() })
|
|
return file
|
|
}
|