Difference between revisions of "love.graphics.newImage"

(Load image if it exists)
 
(34 intermediate revisions by 14 users not shown)
Line 1: Line 1:
{{notice|Images are susceptible to [http://love2d.org/wiki/PO2_Syndrome power of 2 syndrome] on many computers with lower end graphics.}}
+
Creates a new [[Image]] from a filepath, [[FileData]], an [[ImageData]], or a [[CompressedImageData]], and optionally generates or specifies mipmaps for the image.
Creates a new [[Image]] from a filepath or an opened [[File]] or an [[ImageData]]. (Note that currently the x and y pixel dimensions of the image must be a power of 2, e.g. 8x8, 128x512, or some graphics cards will not display it because of the [http://www.opengl.org/wiki/NPOT_Texture requirement set in earlier hardware]).
+
 
 +
Version [[11.0]] updated love.graphics.newImage to treat file names ending with "@2x", "@3x", etc. as a pixel density scale factor if none is explicitly supplied.
 +
{{newobjectnotice}}
 +
 
 
== Function ==
 
== Function ==
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
image = love.graphics.newImage( filename )
+
image = love.graphics.newImage( filename, settings )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
{{param|string|filename|The filepath to the image file.}}
+
{{param|string|filename|The filepath to the image file (or a [[FileData]] or [[ImageData]] or [[CompressedImageData]] or [[ByteData]] object).}}
 +
{{New feature|0.10.0|
 +
{{param|table|settings (nil)|Optional table of settings to configure the image, containing the following fields:}}
 +
{{subparam|boolean|mipmaps (false)|If true, mipmaps for the image will be automatically generated (or taken from the images's file if possible, if the image originated from a [[CompressedImageData]]).}}
 +
{{subparam|boolean|linear (false)|True to treat the image's pixels as linear instead of sRGB, when [[love.graphics.isGammaCorrect|gamma correct rendering]] is enabled. Most images are authored as sRGB.}}
 +
{{New feature|11.0|
 +
{{subparam|number|dpiscale (1)|The DPI scale to use when drawing the image and calling [[Texture:getWidth|getWidth]]/[[Texture:getHeight|getHeight]].}}
 +
|110}}
 +
|100}}
 
=== Returns ===
 
=== Returns ===
{{param|Image|image|An Image object which can be drawn on screen.}}
+
{{param|Image|image|A new Image object which can be drawn on screen.}}
 +
 
 
== Function ==
 
== Function ==
 +
{{newinoldin|[[0.9.1]]|091|[[0.10.0]]|100|type=variant}}
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
image = love.graphics.newImage( file )
+
image = love.graphics.newImage( filename, format )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
{{param|File|file|A File pointing to an image.}}
+
{{param|string|filename|The filepath to the image file (or a [[FileData]] or [[ImageData]] or [[CompressedImageData]] object.)}}
 +
{{param|TextureFormat|format|The format to interpret the image's data as.}}
 
=== Returns ===
 
=== Returns ===
 
{{param|Image|image|An Image object which can be drawn on screen.}}
 
{{param|Image|image|An Image object which can be drawn on screen.}}
== Function ==
+
 
=== Synopsis ===
+
== Examples ==
 +
=== Draw a bunny on the screen ===
 +
<source lang="lua">
 +
function love.load()
 +
bunny = love.graphics.newImage("MyBunny.png")
 +
end
 +
function love.draw()
 +
love.graphics.draw(bunny, 0, 0)
 +
end
 +
</source>
 +
 
 +
=== Load image if it exists ===
 
<source lang="lua">
 
<source lang="lua">
image = love.graphics.newImage( imageData )
+
function loadImage (path)
 +
local info = love.filesystem.getInfo( path )
 +
if info then
 +
return love.graphics.newImage( path )
 +
end
 +
end
 +
 
 +
image = loadImage ("graphics/image-1.png")
 
</source>
 
</source>
=== Arguments ===
+
 
{{param|ImageData|imageData|An ImageData object.}}
 
=== Returns ===
 
{{param|Image|image|An Image object which can be drawn on screen.}}
 
 
== See Also ==
 
== See Also ==
 
* [[parent::love.graphics]]
 
* [[parent::love.graphics]]
 +
* [[love.graphics.draw]]
 +
* [[love.graphics.newQuad]]
 +
* [[Constructs::Image]]
 +
* [[love.image.newImageData]]
 
[[Category:Functions]]
 
[[Category:Functions]]
{{#set:Description=Creates a new Image from a file path. Note that the x and y dimensions of the image must (currently) be a power of 2, or some graphics cards will not display it.}}
+
[[Sub-Category::Object Creation| ]]
 +
{{#set:Description=Creates a new [[Image]].}}
 +
{{#set:Since=000}}
 +
 
 +
== Other Languages ==
 +
{{i18n|love.graphics.newImage}}

Latest revision as of 18:39, 17 May 2022

Creates a new Image from a filepath, FileData, an ImageData, or a CompressedImageData, and optionally generates or specifies mipmaps for the image.

Version 11.0 updated love.graphics.newImage to treat file names ending with "@2x", "@3x", etc. as a pixel density scale factor if none is explicitly supplied.

O.png This function can be slow if it is called repeatedly, such as from love.update or love.draw. If you need to use a specific resource often, create it once and store it somewhere it can be reused!  



Function

Synopsis

image = love.graphics.newImage( filename, settings )

Arguments

string filename
The filepath to the image file (or a FileData or ImageData or CompressedImageData or ByteData object).
Available since LÖVE 0.10.0
table settings (nil)
Optional table of settings to configure the image, containing the following fields:
boolean mipmaps (false)
If true, mipmaps for the image will be automatically generated (or taken from the images's file if possible, if the image originated from a CompressedImageData).
boolean linear (false)
True to treat the image's pixels as linear instead of sRGB, when gamma correct rendering is enabled. Most images are authored as sRGB.
Available since LÖVE 11.0
number dpiscale (1)
The DPI scale to use when drawing the image and calling getWidth/getHeight.

Returns

Image image
A new Image object which can be drawn on screen.

Function

Available since LÖVE 0.9.1 and removed in LÖVE 0.10.0
This variant is not supported in earlier or later versions.

Synopsis

image = love.graphics.newImage( filename, format )

Arguments

string filename
The filepath to the image file (or a FileData or ImageData or CompressedImageData object.)
TextureFormat format
The format to interpret the image's data as.

Returns

Image image
An Image object which can be drawn on screen.

Examples

Draw a bunny on the screen

function love.load()
	bunny = love.graphics.newImage("MyBunny.png")
end
function love.draw()
	love.graphics.draw(bunny, 0, 0)
end

Load image if it exists

function loadImage (path)
	local info = love.filesystem.getInfo( path )
	if info then
		return love.graphics.newImage( path )
	end
end

image = loadImage ("graphics/image-1.png")

See Also



Other Languages