Hi, I’m trying to create a program that reads a PNG image (finds the RGBA values of each pixel) and then changes them (for example, sets all red values to maximum and the original image gets a red tint). So far, I’m just trying to read all the pixels and then set them unchanged as the texture of the sprite to which the original PNG image belonged. The problem is that instead of the program drawing the image the way it read it, it transforms it very strangely (you can see it in the attached images). Does anyone know what I’m doing wrong?
Here is my code:
function set_pixel(stream, width, x, y, color)
local index = 4 * (x + y * width) + 1
stream[index + 0] = color.x
stream[index + 1] = color.y
stream[index + 2] = color.z
stream[index + 3] = color.w
end
function get_pixel_rgba(pixels, width, x, y)
local index = 4 * (x + y * width) + 1
local r = pixels[index + 0]
local g = pixels[index + 1]
local b = pixels[index + 2]
local a = pixels[index + 3]
return r,g,b,a
end
function init(self)
msg.post(".", "acquire_input_focus")
local file = io.open("images\\kraken.png", "rb")
local file = file:read("*a")
local buf, w, h = png.decode_rgba(file, true)
local pixels = buffer.get_stream(buf, hash("pixels"))
self.buffer = buffer.create(w * h, {{name = hash("rgba"), type = buffer.VALUE_TYPE_UINT8, count = 4}})
local stream = buffer.get_stream(self.buffer, hash("rgba"))
tables = {}
for x = 0, w-1 do
for y = 0, h-1 do
r,g,b,a = get_pixel_rgba(pixels, w, x, y)
table.insert(tables,vmath.vector4(r,g,b,a))
end
end
b = 1
for x = 0, w-1 do
for y = 0, h-1 do
clr = tables[b]
set_pixel(stream, w, x, y, clr)
b = b+1
end
end
local resource_path = go.get("#sprite", "texture0")
local header = {width = w, height = h, type = resource.TEXTURE_TYPE_2D, format = resource.TEXTURE_FORMAT_RGBA, num_mip_maps = 1}
resource.set_texture(resource_path, header, self.buffer)
end
Thank you