pixel: fix Image[Monochrome].Set for larger images

For bigger images, the pixel index might not fit in a int16. Therefore,
int is needed during the calculation.

While fixing this bug, I've added a few tests that verify the Image
implementation by creating images, filling them with random data, and
then checking whether they still contain the same data. This test failed
before the patch.
This commit is contained in:
Ayke van Laethem
2024-05-25 15:42:11 +02:00
committed by Ron Evans
parent 831982ad33
commit 7d983647ad
2 changed files with 71 additions and 3 deletions
+3 -3
View File
@@ -104,9 +104,9 @@ func (img Image[T]) setPixel(index int, c T) {
switch {
case zeroColor.BitsPerPixel() == 1:
// Monochrome.
x := int16(index) % img.width
y := int16(index) / img.width
offset := x + (y/8)*img.width
x := index % int(img.width)
y := index / int(img.width)
offset := x + (y/8)*int(img.width)
ptr := (*byte)(unsafe.Add(img.data, offset))
if c != zeroColor {
*((*byte)(ptr)) |= 1 << uint8(y%8)