148 lines
3.4 KiB
Go
148 lines
3.4 KiB
Go
package imageprocessor
|
|
|
|
import (
|
|
"bytes"
|
|
"image"
|
|
"image/color"
|
|
"image/jpeg"
|
|
"image/png"
|
|
"testing"
|
|
|
|
"github.com/davidbyttow/govips/v2/vips"
|
|
"sneak.berlin/go/pixa/internal/imagerequest"
|
|
)
|
|
|
|
// createHalfTransparentPNG creates a PNG whose left half is fully transparent
|
|
// and whose right half is opaque red, 8 or 16 bits per sample.
|
|
func createHalfTransparentPNG(t *testing.T, sixteenBit bool) []byte {
|
|
t.Helper()
|
|
|
|
const width, height = 40, 20
|
|
|
|
var img image.Image
|
|
|
|
red := color.NRGBA{R: 255, A: 255}
|
|
|
|
if sixteenBit {
|
|
rgba := image.NewNRGBA64(image.Rect(0, 0, width, height))
|
|
for y := range height {
|
|
for x := width / 2; x < width; x++ {
|
|
rgba.Set(x, y, red)
|
|
}
|
|
}
|
|
|
|
img = rgba
|
|
} else {
|
|
rgba := image.NewNRGBA(image.Rect(0, 0, width, height))
|
|
for y := range height {
|
|
for x := width / 2; x < width; x++ {
|
|
rgba.Set(x, y, red)
|
|
}
|
|
}
|
|
|
|
img = rgba
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := png.Encode(&buf, img)
|
|
if err != nil {
|
|
t.Fatalf("failed to encode test PNG: %v", err)
|
|
}
|
|
|
|
return buf.Bytes()
|
|
}
|
|
|
|
// createHalfTransparentGreyPNG creates a grey PNG with an alpha channel,
|
|
// two bands, whose left half is fully transparent and whose right half is
|
|
// opaque. Go's PNG encoder cannot write grey with alpha, so libvips does.
|
|
func createHalfTransparentGreyPNG(t *testing.T) []byte {
|
|
t.Helper()
|
|
|
|
img, err := vips.NewImageFromBuffer(createHalfTransparentPNG(t, false))
|
|
if err != nil {
|
|
t.Fatalf("failed to decode test PNG: %v", err)
|
|
}
|
|
|
|
defer img.Close()
|
|
|
|
err = img.ToColorSpace(vips.InterpretationBW)
|
|
if err != nil {
|
|
t.Fatalf("failed to convert test PNG to grey: %v", err)
|
|
}
|
|
|
|
if img.Bands() != 2 {
|
|
t.Fatalf("grey test image has %d bands, want 2", img.Bands())
|
|
}
|
|
|
|
output, _, err := img.ExportPng(&vips.PngExportParams{
|
|
Compression: pngCompression,
|
|
Filter: vips.PngFilterNone,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("failed to save grey test PNG: %v", err)
|
|
}
|
|
|
|
return output
|
|
}
|
|
|
|
// TestImageProcessor_JPEGFillsTransparencyWithWhite verifies that the
|
|
// transparent areas of a source become white in JPEG output, for 8-bit,
|
|
// 16-bit and grey sources with an alpha channel.
|
|
func TestImageProcessor_JPEGFillsTransparencyWithWhite(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
input func(t *testing.T) []byte
|
|
}{
|
|
{"8-bit RGBA", func(t *testing.T) []byte {
|
|
t.Helper()
|
|
|
|
return createHalfTransparentPNG(t, false)
|
|
}},
|
|
{"16-bit RGBA", func(t *testing.T) []byte {
|
|
t.Helper()
|
|
|
|
return createHalfTransparentPNG(t, true)
|
|
}},
|
|
{"grey with alpha", createHalfTransparentGreyPNG},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
result, err := New(Params{
|
|
MaxInputBytes: 50 << 20,
|
|
MaxConcurrentProcessing: 1,
|
|
}).Process(
|
|
t.Context(), bytes.NewReader(tt.input(t)),
|
|
imagerequest.FormatPNG,
|
|
&imagerequest.Transform{Format: imagerequest.FormatJPEG},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("Process() error = %v", err)
|
|
}
|
|
|
|
defer func() { _ = result.Content.Close() }()
|
|
|
|
decoded, err := jpeg.Decode(result.Content)
|
|
if err != nil {
|
|
t.Fatalf("failed to decode JPEG output: %v", err)
|
|
}
|
|
|
|
r, g, b, _ := decoded.At(5, 10).RGBA()
|
|
t.Logf("transparent area came out as %d,%d,%d",
|
|
r>>8, g>>8, b>>8)
|
|
|
|
// JPEG is lossy, so white is any value close to 255
|
|
const nearWhite = 250
|
|
if r>>8 < nearWhite || g>>8 < nearWhite || b>>8 < nearWhite {
|
|
t.Errorf("transparent area = %d,%d,%d, want white",
|
|
r>>8, g>>8, b>>8)
|
|
}
|
|
})
|
|
}
|
|
}
|