Difference between revisions of "(Image):getData"

(Examples)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{newin|[[0.9.0]]|090|type=function}}
+
{{newinoldin|[[0.9.0]]|090|[[11.0]]|110|type=function|text=Create an [[ImageData]] from a file, create the Image from that, and keep a reference to the ImageData, instead of using this function}}
 
Gets the original [[ImageData]] or [[CompressedData]] used to create the Image.
 
Gets the original [[ImageData]] or [[CompressedData]] used to create the Image.
  
Line 43: Line 43:
 
         image:refresh()
 
         image:refresh()
 
     end
 
     end
 +
end
 +
</source>
 +
 +
 +
But you can get the ImageData if you draw it as canvas (probably too slow):
 +
<source lang="lua">
 +
local function newImageData (image)
 +
local canvas = love.graphics.newCanvas (image:getDimensions())
 +
love.graphics.setCanvas (canvas)
 +
love.graphics.setColor (1,1,1)
 +
love.graphics.draw (image)
 +
love.graphics.setCanvas ()
 +
return canvas:newImageData()
 
end
 
end
 
</source>
 
</source>

Latest revision as of 10:02, 10 March 2023

Available since LÖVE 0.9.0 and removed in LÖVE 11.0
Create an ImageData from a file, create the Image from that, and keep a reference to the ImageData, instead of using this function.

Gets the original ImageData or CompressedData used to create the Image.

All Images keep a reference to the Data that was used to create the Image. The Data is used to refresh the Image when love.window.setMode or Image:refresh is called.

Function

Synopsis

data = Image:getData( )

Arguments

None.

Returns

ImageData data
The original ImageData used to create the Image, if the image is not compressed.

Function

Synopsis

data = Image:getData( )

Arguments

None.

Returns

CompressedData data
The original CompressedData used to create the Image, if the image is compressed.

Examples

Edit the Image's ImageData and refresh the Image using the edited ImageData.

function love.load()
    image = love.graphics.newImage("pig.png")
end

function love.draw()
    love.graphics.draw(image)
end

function love.keypressed(key)
    -- If the image is compressed, it will return CompressedData which doesn't have a mapPixel method.
    -- Currently only dds files can become compressed images.
    if key == "e" and not image:isCompressed() then
        local data = image:getData()
        data:mapPixel(function(x, y, r, g, b, a) return r/2, g/2, b/2, a/2 end)
        image:refresh()
    end
end


But you can get the ImageData if you draw it as canvas (probably too slow):

local function newImageData (image)
	local canvas = love.graphics.newCanvas (image:getDimensions())
	love.graphics.setCanvas (canvas)
		love.graphics.setColor (1,1,1)
		love.graphics.draw (image)
	love.graphics.setCanvas ()
	return canvas:newImageData()
end

See Also

Other Languages