internal/atlas: add a test to modify pixels after ReplacePixels call

This commit is contained in:
Hajime Hoshi 2022-06-11 23:44:29 +09:00
parent 08b52698c6
commit 253f2757d1
2 changed files with 48 additions and 0 deletions

View File

@ -532,6 +532,7 @@ func (i *Image) replacePixels(pix []byte, mask []byte) {
}
if i.paddingSize() == 0 {
// Copy pixels in the case when pix is modified before the graphics command is executed.
pix2 := make([]byte, len(pix))
copy(pix2, pix)
var mask2 []byte

View File

@ -690,4 +690,51 @@ func TestImageIsNotReputOnAtlasWithoutUsingAsSource(t *testing.T) {
}
}
func TestImageReplacePixelsModify(t *testing.T) {
for _, typ := range []atlas.ImageType{atlas.ImageTypeRegular, atlas.ImageTypeVolatile, atlas.ImageTypeUnmanaged} {
const size = 16
img := atlas.NewImage(size, size, typ)
defer img.MarkDisposed()
pix := make([]byte, 4*size*size)
for j := 0; j < size; j++ {
for i := 0; i < size; i++ {
pix[4*(i+j*size)] = byte(i + j)
pix[4*(i+j*size)+1] = byte(i + j)
pix[4*(i+j*size)+2] = byte(i + j)
pix[4*(i+j*size)+3] = byte(i + j)
}
}
img.ReplacePixels(pix, nil)
// Modify pix after ReplacePixels.
for j := 0; j < size; j++ {
for i := 0; i < size; i++ {
pix[4*(i+j*size)] = 0
pix[4*(i+j*size)+1] = 0
pix[4*(i+j*size)+2] = 0
pix[4*(i+j*size)+3] = 0
}
}
// Check the pixels are the original ones.
pix, err := img.Pixels(ui.GraphicsDriverForTesting())
if err != nil {
t.Fatal(err)
}
for j := 0; j < size; j++ {
for i := 0; i < size; i++ {
want := color.RGBA{byte(i + j), byte(i + j), byte(i + j), byte(i + j)}
r := pix[4*(size*j+i)]
g := pix[4*(size*j+i)+1]
b := pix[4*(size*j+i)+2]
a := pix[4*(size*j+i)+3]
got := color.RGBA{r, g, b, a}
if got != want {
t.Errorf("Type: %d, At(%d, %d): got: %v, want: %v", typ, i, j, got, want)
}
}
}
}
}
// TODO: Add tests to extend image on an atlas out of the main loop