package ui
import (
"bytes"
"encoding/base64"
"encoding/binary"
"image"
"image/color"
"image/draw"
"image/jpeg"
"image/png"
"runtime"
"testing"
"weak"
"golang.org/x/image/bmp"
)
// stripedImage returns a w×h image of vertical stripes, black and white, each
// n pixels wide, shifted by off pixels.
func stripedImage(w, h, n, off int) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, w, h))
for y := range h {
for x := range w {
c := uint8(0)
if (x+off)/n%2 == 1 {
c = 255
}
img.Set(x, y, color.RGBA{c, c, c, 255})
}
}
return img
}
func TestBitmapShowsSmallerSmoothly(t *testing.T) {
// Stripes of 4 pixels, whose middles a GPU's samples, one in 8 pixels,
// fall on: all white.
bm := NewBitmap(stripedImage(512, 512, 4, 2))
tt := coreNewTester(func(c *context) {
coreImage(c, bm).Size(64, 64).Fit(FillBox)
}, 100, 100)
// Every pixel shows as many black stripes as white: gray, not stripes
// of its own.
img := tt.Image()
lo, hi := uint8(255), uint8(0)
for y := 4; y < 60; y++ {
for x := 4; x < 60; x++ {
v := img.RGBAAt(x, y).R
lo, hi = min(lo, v), max(hi, v)
}
}
if lo < 100 || hi > 160 {
t.Errorf("the stripes, 8 times smaller, range from %d to %d", lo, hi)
}
if len(bm.levels) != 3 {
t.Errorf("%d levels made", len(bm.levels))
}
// At its size, it is itself.
if got := bm.smaller(1); got != bm.img {
t.Error("a level for the bitmap at its size")
}
}
func TestBitmapPixelated(t *testing.T) {
// Stripes of one pixel, 8 times as large: a pixelated bitmap shows
// stripes of 8 pixels, black or white, a smooth one ramps between.
for _, pixelated := range []bool{false, true} {
bm := NewBitmap(stripedImage(4, 4, 1, 0))
if pixelated {
bm.Pixelated()
}
tt := coreNewTester(func(c *context) {
coreImage(c, bm).Size(32, 32).Fit(FillBox)
}, 40, 40)
img := tt.Image()
grays := 0
for x := range 32 {
if v := img.RGBAAt(x, 16).R; v > 10 && v < 245 {
grays++
}
}
if pixelated && grays != 0 {
t.Errorf("a pixelated bitmap shows %d grays between its pixels", grays)
}
if !pixelated && grays < 8 {
t.Errorf("a smooth bitmap shows %d grays between its pixels", grays)
}
if pixelated && (img.RGBAAt(4, 16).R != 0 || img.RGBAAt(12, 16).R != 255) {
t.Errorf("pixels %d and %d", img.RGBAAt(4, 16).R, img.RGBAAt(12, 16).R)
}
}
}
func TestImageRegion(t *testing.T) {
// A sheet of four 2×2 parts: red, green, blue and white.
sheet := image.NewRGBA(image.Rect(0, 0, 4, 4))
colors := [4]color.RGBA{{255, 0, 0, 255}, {0, 255, 0, 255}, {0, 0, 255, 255}, {255, 255, 255, 255}}
for y := range 4 {
for x := range 4 {
sheet.Set(x, y, colors[y/2*2+x/2])
}
}
// The left half of the green part red, to see it flip.
sheet.Set(2, 0, colors[0])
sheet.Set(2, 1, colors[0])
bm := NewBitmap(sheet).Pixelated()
green, white := Rect{X: 2, W: 2, H: 2}, Rect{X: 2, Y: 2, W: 2, H: 2}
tt := coreNewTester(func(c *context) {
coreBox(c).Size(80, 20).Draw(func(p *Painter, r Rect) {
p.ImageRegion(bm, green, Rect{X: r.X, Y: r.Y, W: 20, H: 20}, ImageOptions{})
p.ImageRegion(bm, green, Rect{X: r.X + 30, Y: r.Y, W: 20, H: 20}, ImageOptions{FlipX: true})
p.ImageRegion(bm, white, Rect{X: r.X + 60, Y: r.Y, W: 20, H: 20}, ImageOptions{Tint: RGB(255, 0, 0)})
})
}, 80, 20)
img := tt.Image()
px := func(x, y int) color.RGBA { return img.RGBAAt(x, y) }
for _, c := range []struct {
x int
want color.RGBA
}{
{5, colors[0]}, {15, colors[1]}, // red then green
{35, colors[1]}, {45, colors[0]}, // flipped: green then red
{65, colors[0]}, {75, colors[0]}, // white tinted red
} {
if got := px(c.x, 10); got != c.want {
t.Errorf("pixel %d = %v, want %v", c.x, got, c.want)
}
}
// Nothing of the blue part below the green one shows at its edge.
if got := px(15, 19); got != colors[1] {
t.Errorf("the green part's bottom edge = %v", got)
}
}
func TestHalve(t *testing.T) {
// 3×3 of values 0, 40, 80 by column: an odd column counts twice.
img := image.NewRGBA(image.Rect(0, 0, 3, 3))
for y := range 3 {
for x := range 3 {
img.Set(x, y, color.RGBA{uint8(40 * x), 0, 0, 255})
}
}
h := halve(NewBitmap(img).img)
if h.W != 2 || h.H != 2 {
t.Fatalf("%d×%d", h.W, h.H)
}
if r0, r1 := h.Pix[0], h.Pix[4]; r0 != 20 || r1 != 80 {
t.Errorf("columns %d and %d", r0, r1)
}
}
// withOrientation returns a JPEG with an EXIF segment giving orientation
// o, in byte order bo.
func withOrientation(t *testing.T, img image.Image, o uint16, bo binary.ByteOrder) []byte {
t.Helper()
var buf bytes.Buffer
if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: 95}); err != nil {
t.Fatal(err)
}
tiff := make([]byte, 8+2+12+4)
if bo == binary.LittleEndian {
copy(tiff, "II")
} else {
copy(tiff, "MM")
}
bo.PutUint16(tiff[2:], 42)
bo.PutUint32(tiff[4:], 8)
bo.PutUint16(tiff[8:], 1) // one entry
bo.PutUint16(tiff[10:], 0x0112) // Orientation
bo.PutUint16(tiff[12:], 3) // SHORT
bo.PutUint32(tiff[14:], 1)
bo.PutUint16(tiff[18:], o)
app1 := append([]byte("Exif\x00\x00"), tiff...)
seg := []byte{0xff, 0xe1, 0, 0}
binary.BigEndian.PutUint16(seg[2:], uint16(2+len(app1)))
data := buf.Bytes()
return append(append(append([]byte{}, data[:2]...), append(seg, app1...)...), data[2:]...)
}
func TestDecodeBitmapTurnsPhotosUpright(t *testing.T) {
// Stored on its side, 32×16: red on the left, blue on the right.
img := image.NewRGBA(image.Rect(0, 0, 32, 16))
for y := range 16 {
for x := range 32 {
c := color.RGBA{255, 0, 0, 255}
if x >= 16 {
c = color.RGBA{0, 0, 255, 255}
}
img.Set(x, y, c)
}
}
red := func(b *Bitmap, x, y int) bool {
p := b.img.Pix[4*(y*b.w+x):]
return p[0] > 200 && p[2] < 60
}
for _, c := range []struct {
o uint16
bo binary.ByteOrder
w, h int
redX, redY int // a pixel that is red
}{
{1, binary.BigEndian, 32, 16, 4, 8},
{3, binary.LittleEndian, 32, 16, 28, 8}, // upside down
{6, binary.BigEndian, 16, 32, 8, 4}, // turned right: red on top
{8, binary.LittleEndian, 16, 32, 8, 28}, // turned left: red at the bottom
} {
b, err := DecodeBitmap(withOrientation(t, img, c.o, c.bo))
if err != nil {
t.Fatal(err)
}
if b.w != c.w || b.h != c.h || !red(b, c.redX, c.redY) {
t.Errorf("orientation %d: %d×%d, red at (%d, %d): %v", c.o, b.w, b.h, c.redX, c.redY, red(b, c.redX, c.redY))
}
}
if exifOrientation([]byte("not a jpeg")) != 0 {
t.Error("an orientation in what is no JPEG")
}
}
func TestDecodeBitmapOfWebPAndBMP(t *testing.T) {
webp, _ := base64.StdEncoding.DecodeString("UklGRiQAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAwA0JaQAA3AA/vuUAAA=")
b, err := DecodeBitmap(webp)
if err != nil || b.w != 1 || b.h != 1 {
t.Errorf("WebP: %v", err)
}
var buf bytes.Buffer
if err := bmp.Encode(&buf, stripedImage(6, 4, 1, 0)); err != nil {
t.Fatal(err)
}
if b, err = DecodeBitmap(buf.Bytes()); err != nil || b.w != 6 || b.h != 4 || b.img.Pix[4] != 255 {
t.Errorf("BMP: %v", err)
}
}
func TestDecodeImageOfBitmapsAndSVGs(t *testing.T) {
var buf bytes.Buffer
if err := png.Encode(&buf, stripedImage(6, 4, 1, 0)); err != nil {
t.Fatal(err)
}
src, err := DecodeImage(buf.Bytes())
if b, ok := src.(*Bitmap); err != nil || !ok || b.w != 6 || b.h != 4 {
t.Errorf("PNG: %T, %v", src, err)
}
for _, data := range []string{
``,
"\xef\xbb\xbf \n\t\n", // a BOM and spaces
``,
} {
src, err := DecodeImage([]byte(data))
if s, ok := src.(*SVG); err != nil || !ok || s.w != 30 || s.h != 20 {
t.Errorf("%q: %T, %v", data, src, err)
}
}
for _, data := range []string{"", "not a picture \x00", `