Page 1 of 1

String representation of an image

Posted: Tue May 06, 2014 10:29 pm
by scorpii
Hi,
What is the difference between the string representation ("imgString" below) of an image in these two examples?

Code: Select all

local imgString, size = love.filesystem.read("image.png")

Code: Select all

local image1 = love.graphics.newImage("image.png")
local imageData = image1:getData()
local imgString = imageData:getString()

Re: String representation of an image

Posted: Tue May 06, 2014 10:32 pm
by slime
In the first example, you're getting the PNG file as a string (still encoded as a PNG). In the second example, you're getting the raw pixels of the decoded image represented as bytes in a string.

Re: String representation of an image

Posted: Tue May 06, 2014 10:48 pm
by scorpii
Aha.. Can I convert the string of bytes from the second example to an image object again?

Basically I want to send an image object to another love process over the network. So I'm trying to find out my alternatives. Since I will already have the image object loaded from file I'd like to be able to do this without having to read from the filesystem again.

Re: String representation of an image

Posted: Wed May 07, 2014 6:19 am
by bartbes
I'd recommend sending the encoded data anyway, because it's a lot smaller, so I would tell you to look at [wiki]ImageData:encode[/wiki], but it only supports writing to disk as well, sadly.

Re: String representation of an image

Posted: Wed May 07, 2014 5:32 pm
by scorpii
Yes, it makes sense to send the already encoded data. So I'll use the love.filesystem.read alternative.
Thanks guys! :)

Re: String representation of an image

Posted: Sun Jul 06, 2014 3:41 pm
by sashwoopwoop
Bumping this because slime has just told me on IRC how to turn an ImageData's string representation back into an ImageData instance. You can make use of the FFI library which allows you to call external C functions. We are making use of FFI's copy() method:

Code: Select all

local ffi = require("ffi")
local imageDataString = getThisFromSomewhere()
local imageData = love.image.newImageData(imageWidth, imageHeight)
assert(imageData:getSize() == #imageDataString , "Incorrect ImageData size: " .. imageData:getSize() .. " ~= " .. #imageDataString )
ffi.copy(imageData:getPointer(), imageDataString)