String representation of an image

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
scorpii
Prole
Posts: 9
Joined: Tue Apr 22, 2014 9:29 pm
Location: Sweden

String representation of an image

Post 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()
Löve noob
Lua noob
Life noob
User avatar
slime
Solid Snayke
Posts: 3161
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: String representation of an image

Post 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.
User avatar
scorpii
Prole
Posts: 9
Joined: Tue Apr 22, 2014 9:29 pm
Location: Sweden

Re: String representation of an image

Post 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.
Löve noob
Lua noob
Life noob
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: String representation of an image

Post 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.
User avatar
scorpii
Prole
Posts: 9
Joined: Tue Apr 22, 2014 9:29 pm
Location: Sweden

Re: String representation of an image

Post by scorpii »

Yes, it makes sense to send the already encoded data. So I'll use the love.filesystem.read alternative.
Thanks guys! :)
Löve noob
Lua noob
Life noob
User avatar
sashwoopwoop
Prole
Posts: 8
Joined: Fri Jun 27, 2014 10:53 am

Re: String representation of an image

Post 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)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 2 guests