65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package generator
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
"testing"
|
|
)
|
|
|
|
func TestBuildLogoPlane(t *testing.T) {
|
|
// 8x4 logo: white left half, near-75% red right half (8-bit 191 is
|
|
// 0.749, a hair under the exact-0.75 bars value, hence Y=203 not 204),
|
|
// one transparent pixel at (2,1).
|
|
img := image.NewRGBA(image.Rect(0, 0, 8, 4))
|
|
for yy := 0; yy < 4; yy++ {
|
|
for xx := 0; xx < 8; xx++ {
|
|
if xx < 4 {
|
|
img.Set(xx, yy, color.RGBA{255, 255, 255, 255})
|
|
} else {
|
|
img.Set(xx, yy, color.RGBA{191, 0, 0, 255})
|
|
}
|
|
}
|
|
}
|
|
img.Set(2, 1, color.RGBA{0, 0, 0, 0})
|
|
|
|
plane, err := BuildLogoPlane(img)
|
|
if err != nil {
|
|
t.Fatalf("BuildLogoPlane: %v", err)
|
|
}
|
|
if len(plane) != 1+8*4 {
|
|
t.Fatalf("plane len=%d, want 33", len(plane))
|
|
}
|
|
if hdr := plane[0]; hdr != 8|4<<16 {
|
|
t.Fatalf("header=%#x, want w=8 h=4", hdr)
|
|
}
|
|
cases := []struct {
|
|
dx, dy int
|
|
opaque bool
|
|
y, cb, cr uint32
|
|
}{
|
|
{1, 1, true, 940, 512, 512}, // white
|
|
{5, 2, true, 203, 435, 848}, // near-75% red
|
|
{2, 1, false, 0, 0, 0}, // transparent
|
|
}
|
|
for _, c := range cases {
|
|
s := plane[1+c.dy*8+c.dx]
|
|
if got := (s >> 30) & 1; got != boolToBit(c.opaque) {
|
|
t.Fatalf("pixel (%d,%d): opaque=%d, want %v", c.dx, c.dy, got, c.opaque)
|
|
}
|
|
if !c.opaque {
|
|
continue
|
|
}
|
|
if s&0x3FF != c.y || (s>>10)&0x3FF != c.cb || (s>>20)&0x3FF != c.cr {
|
|
t.Fatalf("pixel (%d,%d): got %d/%d/%d, want %d/%d/%d",
|
|
c.dx, c.dy, s&0x3FF, (s>>10)&0x3FF, (s>>20)&0x3FF, c.y, c.cb, c.cr)
|
|
}
|
|
}
|
|
}
|
|
|
|
func boolToBit(b bool) uint32 {
|
|
if b {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|