//go:build windows package clipboard import ( "bytes" "image" "image/color" "image/png" "testing" ) func TestDIBRoundTrip(t *testing.T) { // Build a small 4x3 RGBA image with distinct corner colors. img := image.NewRGBA(image.Rect(0, 0, 4, 3)) img.Set(0, 0, color.RGBA{255, 0, 0, 255}) // red img.Set(3, 0, color.RGBA{0, 255, 0, 255}) // green img.Set(0, 2, color.RGBA{0, 0, 255, 255}) // blue img.Set(3, 2, color.RGBA{255, 255, 0, 128}) // semi-transparent yellow var buf bytes.Buffer if err := png.Encode(&buf, img); err != nil { t.Fatalf("png.Encode: %v", err) } pngData := buf.Bytes() dib, err := pngToDIB(pngData) if err != nil { t.Fatalf("pngToDIB: %v", err) } got, err := dibToPNG(dib) if err != nil { t.Fatalf("dibToPNG: %v", err) } decoded, err := png.Decode(bytes.NewReader(got)) if err != nil { t.Fatalf("decode round-trip: %v", err) } if decoded.Bounds() != img.Bounds() { t.Errorf("bounds = %v, want %v", decoded.Bounds(), img.Bounds()) } // Spot-check the corner colors survived the round-trip. for _, tc := range []struct { x, y int want color.RGBA }{ {0, 0, color.RGBA{255, 0, 0, 255}}, {3, 0, color.RGBA{0, 255, 0, 255}}, {0, 2, color.RGBA{0, 0, 255, 255}}, } { r, g, b, a := decoded.At(tc.x, tc.y).RGBA() got := color.RGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), uint8(a >> 8)} if got != tc.want { t.Errorf("pixel (%d,%d) = %v, want %v", tc.x, tc.y, got, tc.want) } } } func TestDIBRejectsUnsupportedCompression(t *testing.T) { dib := make([]byte, 44) dib[14] = 32 // biBitCount // biCompression = BI_RLE8 (1) at offset 16 dib[16] = 1 dib[4] = 1 // width 1 dib[8] = 1 // height 1 if _, err := dibToPNG(dib); err == nil { t.Error("expected error for unsupported compression, got nil") } } func BenchmarkDIBRoundTrip(b *testing.B) { img := image.NewRGBA(image.Rect(0, 0, 256, 256)) var buf bytes.Buffer _ = png.Encode(&buf, img) pngData := buf.Bytes() dib, err := pngToDIB(pngData) if err != nil { b.Fatalf("pngToDIB: %v", err) } b.ResetTimer() for i := 0; i < b.N; i++ { if _, err := dibToPNG(dib); err != nil { b.Fatalf("dibToPNG: %v", err) } } }