Page 1 of 1

Converting imageData:getPixel() numbers to the 0-255 range

Posted: Mon Feb 18, 2019 9:51 am
by Doctory
So I recently came back to love after quite some time and decided to look at my old project. For some reason it didn't work and the culprit is imageData:getPixel() returning numbers from 0-1 instead of 0-255. This is a big deal because I created maps from reading image files and it depended on the color of the pixel to create tiles as you can see in these files: https://github.com/stefankoxd/overly-en ... verter.lua and https://github.com/stefankoxd/overly-en ... ainter.lua

Is there any way to fix this?

Re: Converting imageData:getPixel() numbers to the 0-255 range

Posted: Mon Feb 18, 2019 10:15 am
by grump

Code: Select all

math.floor(c * 255 +.5)
should do the trick.

Or use cindy. Either call require('cindy').applyPatch() to have the old behavior restored in all relevant functions, or use the provided *Bytes alternatives.

Code: Select all

function converter:imagetomap(img)
	local pixelData = {}
	local function map(x, y, r, g, b, a) 
		pixelData[#pixelData + 1] = { x = x, y = y, r = r, g = g, b = b }
		return r, g, b, a
	end

	img:mapPixelBytes(map)
	return pixelData
end

Re: Converting imageData:getPixel() numbers to the 0-255 range

Posted: Mon Feb 18, 2019 11:15 am
by Doctory
grump wrote: Mon Feb 18, 2019 10:15 am -snip-
Cindy worked perfectly, thanks for the recommendation