169 lines
4.4 KiB
Go
169 lines
4.4 KiB
Go
package thumb
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func ppm(w, h int, fill func(x, y int) [3]byte) []byte {
|
|
var b bytes.Buffer
|
|
fmt.Fprintf(&b, "P6\n# qemu\n%d %d\n255\n", w, h)
|
|
for y := 0; y < h; y++ {
|
|
for x := 0; x < w; x++ {
|
|
p := fill(x, y)
|
|
b.Write(p[:])
|
|
}
|
|
}
|
|
return b.Bytes()
|
|
}
|
|
|
|
func TestDecodePPM(t *testing.T) {
|
|
data := ppm(3, 2, func(x, y int) [3]byte { return [3]byte{byte(x * 100), byte(y * 100), 7} })
|
|
img, err := decodePPM(bufio.NewReader(bytes.NewReader(data)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if img.Bounds().Dx() != 3 || img.Bounds().Dy() != 2 {
|
|
t.Fatalf("size %v", img.Bounds())
|
|
}
|
|
r, g, b, _ := img.At(2, 1).RGBA()
|
|
if r>>8 != 200 || g>>8 != 100 || b>>8 != 7 {
|
|
t.Fatalf("pixel (2,1) = %d,%d,%d", r>>8, g>>8, b>>8)
|
|
}
|
|
}
|
|
|
|
func TestDecodePPMRejectsP3(t *testing.T) {
|
|
if _, err := decodePPM(bufio.NewReader(bytes.NewReader([]byte("P3\n1 1\n255\n0 0 0\n")))); err == nil {
|
|
t.Fatal("want error for ASCII PPM")
|
|
}
|
|
}
|
|
|
|
func TestDecodePPMRejectsTruncatedPixels(t *testing.T) {
|
|
data := ppm(3, 2, func(x, y int) [3]byte { return [3]byte{} })
|
|
if _, err := decodePPM(bufio.NewReader(bytes.NewReader(data[:len(data)-1]))); err == nil {
|
|
t.Fatal("want error for truncated pixel data")
|
|
}
|
|
}
|
|
|
|
func BenchmarkDecodePPM4K(b *testing.B) {
|
|
data := ppm(3840, 2160, func(x, y int) [3]byte { return [3]byte{byte(x), byte(y), 7} })
|
|
b.SetBytes(int64(len(data)))
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
if _, err := decodePPM(bufio.NewReader(bytes.NewReader(data))); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestScaleKeepsAspect(t *testing.T) {
|
|
img := image.NewRGBA(image.Rect(0, 0, 1920, 1080))
|
|
out := Scale(img, 640)
|
|
if out.Bounds().Dx() != 640 || out.Bounds().Dy() != 360 {
|
|
t.Fatalf("scaled to %v", out.Bounds())
|
|
}
|
|
if Scale(img, 4000) != image.Image(img) {
|
|
t.Fatal("smaller-than-max image should pass through")
|
|
}
|
|
}
|
|
|
|
// opaqueImage hides the *image.RGBA type so Scale takes its generic path.
|
|
type opaqueImage struct{ image.Image }
|
|
|
|
func TestScaleRGBAMatchesGenericPath(t *testing.T) {
|
|
img := image.NewRGBA(image.Rect(0, 0, 1931, 1087)) // odd sizes: uneven boxes
|
|
for i := range img.Pix {
|
|
img.Pix[i] = byte(i*7 + i/4)
|
|
if i%4 == 3 {
|
|
img.Pix[i] = 255
|
|
}
|
|
}
|
|
fast := Scale(img, 640).(*image.RGBA)
|
|
slow := Scale(opaqueImage{img}, 640).(*image.RGBA)
|
|
if !bytes.Equal(fast.Pix, slow.Pix) {
|
|
t.Fatal("RGBA fast path differs from the generic box filter")
|
|
}
|
|
}
|
|
|
|
func BenchmarkScale4K(b *testing.B) {
|
|
img := image.NewRGBA(image.Rect(0, 0, 3840, 2160))
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
Scale(img, 640)
|
|
}
|
|
}
|
|
|
|
func BenchmarkPreview4K(b *testing.B) {
|
|
path := filepath.Join(b.TempDir(), "4k.ppm")
|
|
data := ppm(3840, 2160, func(x, y int) [3]byte { return [3]byte{byte(x), byte(y), 7} })
|
|
if err := os.WriteFile(path, data, 0o600); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
img, err := LoadPreview(path, "ppm", 640)
|
|
if err != nil || img == nil {
|
|
b.Fatalf("load preview: %v", err)
|
|
}
|
|
if _, err := JPEG(img, 80); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPreviewMatchesFullFrame(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "screen.ppm")
|
|
for _, tc := range []struct{ w, h, width int }{{301, 167, 64}, {11, 3, 640}, {43, 1, 5}, {100, 99, 1}} {
|
|
for _, flat := range []bool{false, true} {
|
|
data := ppm(tc.w, tc.h, func(x, y int) [3]byte {
|
|
if flat {
|
|
return [3]byte{32, 32, 32}
|
|
}
|
|
return [3]byte{byte(x * 53), byte(y * 77), byte(x + y)}
|
|
})
|
|
if err := os.WriteFile(path, data, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
full, err := Load(path, "ppm")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := LoadPreview(path, "ppm", tc.width)
|
|
if err != nil || (got == nil) != Blank(full) {
|
|
t.Fatalf("preview %v: blank or decode mismatch: %v", tc, err)
|
|
}
|
|
if got != nil {
|
|
want := Scale(full, tc.width).(*image.RGBA)
|
|
if got.Bounds() != want.Bounds() || !bytes.Equal(got.(*image.RGBA).Pix, want.Pix) {
|
|
t.Fatalf("preview pixels differ for %v", tc)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBlank(t *testing.T) {
|
|
flat := image.NewRGBA(image.Rect(0, 0, 200, 100))
|
|
if !Blank(flat) {
|
|
t.Fatal("all-black frame should be blank")
|
|
}
|
|
busy := image.NewRGBA(image.Rect(0, 0, 200, 100))
|
|
for y := 0; y < 100; y++ {
|
|
for x := 0; x < 200; x++ {
|
|
if (x/20+y/20)%2 == 0 {
|
|
busy.Set(x, y, color.White)
|
|
}
|
|
}
|
|
}
|
|
if Blank(busy) {
|
|
t.Fatal("checkerboard frame should not be blank")
|
|
}
|
|
}
|