Scripting: why does image:putPixel draw every fourth pixel in indexed mode?

I’m creating a custom file format using a Lua script. While doing this, I’ve found a need to take in a sequence of bytes, and use image:putPixel() to draw it to an image, in indexed mode.
Before doing that, I checked to see if I got the drawing code right, by drawing the colour at index 1 (so the one right after transparency) to every pixel in the image, like so:

for y = 0, height-1,1 do
	for x = 0, width-1,1 do
		image:putPixel(x,y, 1)
	end
end

Where width and height have been previously defined to be equal to the image’s width and height.
With a palette where the colour with index of 1 stands for black, one would assume this creates a black rectangle. However, it produced an image like this:
afbeelding
Instead of every single pixel, every fourth pixel has been drawn to!
I’ve tried everything to remedy this: dividing x and y by four just cut away a fourth of the image.
I tried setting the image data directly with Image.bytes. However, it gave me an error message that I needed to use four(!) times the amount of bytes. So what’s going on here? Is there some byte alignment thing I’m not taking into account? If it were, I’d expect drawPixel() to allow me to every pixel regardless, right?

Duplicating the data four times remedied the error, but gave a somewhat expected result:
afbeelding
What’s interesting about this is that, instead of collumns being skipped, it’s actually every fourth row that is showing. Still, though, this only adds to my confusion at the moment…

I found my issue! It turned out that I had to set the ColorMode.INDEXED for the image file as well. I had only done it for the sprite…

1 Like