Files

51 lines
1.3 KiB
Go
Raw Permalink Normal View History

package webcam
import "testing"
func TestDecodeBMP_roundtripRGB(t *testing.T) {
// 2x2 24-bit BI_RGB bottom-up BMP
row := []byte{
0, 0, 255, // BGR red
0, 255, 0, // green
0, 0, // pad to 4-byte boundary: 6 bytes -> pad 2
}
row2 := []byte{
255, 0, 0, // blue
255, 255, 255, // white
0, 0,
}
pixelData := append(append([]byte{}, row2...), row...) // bottom row first
header := make([]byte, 54)
header[0], header[1] = 'B', 'M'
binaryPutUint32 := func(b []byte, v uint32) {
b[0] = byte(v)
b[1] = byte(v >> 8)
b[2] = byte(v >> 16)
b[3] = byte(v >> 24)
}
binaryPutUint16 := func(b []byte, v uint16) {
b[0] = byte(v)
b[1] = byte(v >> 8)
}
fileSize := uint32(54 + len(pixelData))
binaryPutUint32(header[2:], fileSize)
binaryPutUint32(header[10:], 54)
binaryPutUint32(header[14:], 40) // DIB header size
binaryPutUint32(header[18:], 2) // width
binaryPutUint32(header[22:], 2) // height (bottom-up)
binaryPutUint16(header[26:], 1) // planes
binaryPutUint16(header[28:], 24) // bpp
img, err := decodeBMP(append(header, pixelData...))
if err != nil {
t.Fatal(err)
}
if img.Bounds().Dx() != 2 || img.Bounds().Dy() != 2 {
t.Fatalf("size = %v", img.Bounds())
}
r, g, b, _ := img.At(0, 0).RGBA()
if r>>8 != 255 || g>>8 != 0 || b>>8 != 0 {
t.Fatalf("pixel 0,0 = %d,%d,%d want red", r>>8, g>>8, b>>8)
}
}