Image
is a convenience class for working with bitmap images in Lime.
Although ImageBuffer
holds the actual bitmap data, Image
includes convenience methods for
loading from files, loading from bytes, and performing many pixel operations over an
ImageBuffer
instance.
Static methods
staticfromBase64(base64:String, type:String):Image
staticfromBytes(bytes:Bytes):Image
Converts a Bytes
object to an Image
instance
Some platforms (such as Flash and HTML5) cannot convert Bytes
to an
Image
synchronously, and may not work properly.
Image.loadFromBytes
works asynchronously, and should work
consistently on all platforms.
Parameters:
bytes | A |
---|
Returns:
A new Image
instance
staticfromFile(path:String):Image
Loads an Image
from a path synchronously.
Some platforms, such as Flash and HTML5, cannot load images synchronously.
Image.loadFromFile
works asynchronously, and should
behave consistently on all platforms.
Parameters:
path | The source file path or URL of an encoded image file |
---|
Returns:
A new Image
instance
staticfromImageElement(image:ImageElement):Image
Available on HTML5
Converts a js.html.Image
instance to an Image
Parameters:
image | An |
---|
Returns:
A new Image
instance
staticloadFromBase64(base64:String, type:String):Future<Image>
staticloadFromBytes(bytes:Bytes):Future<Image>
Constructor
new(?buffer:ImageBuffer, offsetX:Int = 0, offsetY:Int = 0, width:Int = -1, height:Int = -1, ?color:Int, ?type:ImageType)
Creates a new Image
instance.
If buffer
is null, but width
and height
are set, a new ImageBuffer
is allocated matching the color
requested.
Parameters:
buffer | (Optional) The |
---|---|
offsetX | (Optional) A logical offset value within the |
offsetY | (Optional) A logical offset value within the |
width | (Optional) A logical width value within the |
height | (Optional) A logical height value within the |
color | (Optional) A fill color to use if the |
type |
Variables
data:UInt8Array
Retrieves UInt8Array
data from the ImageBuffer
. If the ImageBuffer
is not in a data format, it will be converted.
dirty:Bool
Whether the Image
is dirty. This is set to true
when pixel operations
have been performed on the underlying buffer
, and can be set to false
again by your own renderer.
format:PixelFormat
Get or set the PixelFormat
for the underlying ImageBuffer
. This can be
used to convert the ImageBuffer
to a new format.
height:Int
The logical height of the Image
. This can be smaller than the height
of the underlying buffer
.
offsetX:Int
offsetY:Int
powerOfTwo:Bool
Get or set whether the ImageBuffer
dimensions are both a power-of-two
(such as 2, 4, 8, 16, so on). Setting this value may resize the underlying
buffer
premultiplied:Bool
Get or set whether this Image
has premultiplied alpha. Setting this value
may multiply or unmultiply data if the underlying ImageBuffer
uses a
different format.
src:Dynamic
A higher-level representation of the source ImageBuffer
. This might be an
HTML5 Image, CanvasElement or a Flash BitmapData instance.
version:Int
The version
of the Image
increases each time it is modified, helpful to determining
whether a cache is out-of-date.
width:Int
The logical width of the Image
. This can be smaller than the width
of the underlying buffer
.
x:Float
A convenience property, unused internally, which may be helpful for different renderer implementations
y:Float
A convenience property, unused internally, which may be helpful for different renderer implementations
Methods
colorTransform(rect:Rectangle, colorMatrix:ColorMatrix):Void
Applies a color transform to the underlying ImageBuffer
data
Parameters:
rect | The target rectangle to transform |
---|---|
colorMatrix | A |
copyChannel(sourceImage:Image, sourceRect:Rectangle, destPoint:Vector2, sourceChannel:ImageChannel, destChannel:ImageChannel):Void
Copy a color channel from one Image
to another. This can also be within the same Image
instance.
Parameters:
sourceImage | The |
---|---|
sourceRect | The source rectangle to copy from in the |
destPoint | The destination point to apply the channel in the current |
sourceChannel | The source color channel to copy the data from |
destChannel | The destination color channel to apply the data into |
copyPixels(sourceImage:Image, sourceRect:Rectangle, destPoint:Vector2, ?alphaImage:Image, ?alphaPoint:Vector2, mergeAlpha:Bool = false):Void
Copies pixels from one Image
to another. The source Image
can also be this Image
Parameters:
sourceImage | The source |
---|---|
sourceRect | The source rectangle to use when copying |
destPoint | The destination point to use when copying |
alphaImage | (Optional) A different |
alphaPoint | (Optional) A point in the alpha image to use when copying |
mergeAlpha | (Optional) Whether to blend the source and destination alpha ( |
encode(?format:ImageFileFormat, quality:Int = 90):Bytes
Encodes this Image
into an image file format, such as PNG or JPEG.
Parameters:
format | (Optional) An |
---|---|
quality | (Optional) A quality value to use when encoding as JPEG (from 0 to 100) |
Returns:
fillRect(rect:Rectangle, color:Int, ?format:PixelFormat):Void
Fill a rectangle in the Image
with a solid color
Parameters:
rect | A destination rectangle in this |
---|---|
color | The color to use when filling this |
format | (Optional) The |
floodFill(x:Int, y:Int, color:Int, ?format:PixelFormat):Void
Applies a flood fill to this Image
, starting with the point specified.
A flood fill behaves similarly to the "paint can" tool in many image editors, the fill will apply the chosen color to neighboring pixels of the same color.
Parameters:
x | The target x coordinate within the |
---|---|
y | The target y coordinate within the |
color | The color to use when performing the fill |
format | (Optional) The |
getColorBoundsRect(mask:Int, color:Int, findColor:Bool = true, ?format:PixelFormat):Rectangle
Finds a region in the Image
that includes pixels all of a certain color (when findColor
is true
) or
excludes a certain color (findColor
is false
)
Parameters:
mask | A hexadecimal mask to use when comparing colors. You can use this to compare all of a color, or only certain color channels |
---|---|
color | The color value to use in comparisons |
findColor | (Optional) Whether to find a region that does match the color ( |
format | (Optional) The |
Returns:
The matching bounds Rectangle
, or null
if no matching region is found
getPixel(x:Int, y:Int, ?format:PixelFormat):Int
Gets a 24-bit pixel from the Image
(red, green and blue, but no alpha)
Parameters:
x | The |
---|---|
y | The |
format | (Optional) The |
Returns:
The specified pixel, or 0
if it is out-of-bounds
getPixel32(x:Int, y:Int, ?format:PixelFormat):Int
Gets a 32-bit pixel from the Image
, including alpha
Parameters:
x | The |
---|---|
y | The |
format | (Optional) The |
Returns:
The specified pixel, or 0
if it is out-of-bounds
getPixels(rect:Rectangle, ?format:PixelFormat):Bytes
merge(sourceImage:Image, sourceRect:Rectangle, destPoint:Vector2, redMultiplier:Int, greenMultiplier:Int, blueMultiplier:Int, alphaMultiplier:Int):Void
Blits a second Image
onto this one, using optional color multipliers
Parameters:
sourceImage | An |
---|---|
sourceRect | The source rectangle to use when copying |
destPoint | The destination point in this |
redMultiplier | A red multiplier to use when blitting |
greenMultiplier | A green multiplier to use when blitting |
blueMultiplier | A blue multiplier to use when blitting |
alphaMultiplier | An alpha multiplier to use when blitting |
resize(newWidth:Int, newHeight:Int):Void
Resizes the current Image
, reallocating the ImageBuffer
to a new size.
The resize algorithm for most platforms is bilinear.
Parameters:
newWidth | A new width for the |
---|---|
newHeight | A new height for the |
scroll(x:Int, y:Int):Void
Scrolls the content of this Image
.
Pixels on the edges of the scroll will remain repeated, while others within the scroll area will be shifted
Parameters:
x | The amount of horizontal scroll to apply |
---|---|
y | The amount of vertical scroll to apply |
setPixel(x:Int, y:Int, color:Int, ?format:PixelFormat):Void
Sets a pixel in the current Image
in 24-bit color format (red, green, blue, no alpha)
Parameters:
x | The x coordinate of the pixel |
---|---|
y | The y coordinate of the pixel |
color | The color to use |
format | (Optional) The |
setPixel32(x:Int, y:Int, color:Int, ?format:PixelFormat):Void
Sets a pixel in the current Image
in 32-bit color format (includes alpha)
Parameters:
x | The x coordinate of the pixel |
---|---|
y | The y coordinate of the pixel |
color | The color to use |
format | (Optional) The |
setPixels(rect:Rectangle, bytePointer:BytePointer, ?format:PixelFormat, ?endian:Endian):Void
Sets a region of pixels at once using a BytePointer
Parameters:
rect | The region of pixels in this |
---|---|
bytePointer | A |
format | (Optional) The |
endian | (Optional) The endianness of the incoming bytes (default is the system endianness) |
threshold(sourceImage:Image, sourceRect:Rectangle, destPoint:Vector2, operation:String, threshold:Int, color:Int = 0x00000000, mask:Int = 0xFFFFFFFF, copySource:Bool = false, ?format:PixelFormat):Int
Tests pixel values in an image against a specified threshold and sets
pixels that pass the test to new color values. Using the
threshold()
method, you can isolate and replace color ranges
in an image and perform other logical operations on image pixels.
The threshold()
method's test logic is as follows:
- If
((pixelValue & mask) operation(threshold & mask))
, then set the pixel tocolor
; - Otherwise, if
copySource == true
, then set the pixel to corresponding pixel value fromsourceBitmap
.
The operation
parameter specifies the comparison operator
to use for the threshold test. For example, by using "==" as the
operation
parameter, you can isolate a specific color value
in an image. Or by using {operation: "<", mask: 0xFF000000,
threshold: 0x7F000000, color: 0x00000000}
, you can set all
destination pixels to be fully transparent when the source image pixel's
alpha is less than 0x7F. You can use this technique for animated
transitions and other effects.
Parameters:
sourceImage | The input bitmap image to use. The source image can be a different |
---|---|
sourceRect | A rectangle that defines the area of the source image to use as input. |
destPoint | The point within the destination image (the current |
operation | One of the following comparison operators, passed as a |
threshold | The value that each pixel is tested against to see if it meets or exceeds the threshhold. |
color | The color value that a pixel is set to if the threshold test succeeds. The default value is 0x00000000. |
mask | The mask to use to isolate a color component. |
copySource | If the value is |
Returns:
The number of pixels that were changed.