Image
public final class Image
Image
describes a two dimensional array of pixels to draw. The pixels may be
decoded in a raster bitmap, encoded in a Picture
or compressed data stream,
or located in GPU memory as a GPU texture.
Image
cannot be modified after it is created. Image
may allocate additional
storage as needed; for instance, an encoded Image
may decode when drawn.
Image
width and height are greater than zero. Creating an Image
with zero width
or height returns Image
equal to nil
.
Image
may be created from Bitmap
, Pixmap
, Surface
, Picture
, encoded streams,
GPU texture, YUV_ColorSpace data, or hardware buffer. Encoded streams supported
include BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP. Supported encoding details
vary with platform.
-
Creates
Image
from bitmap, sharing or copying bitmap pixels. If the bitmap is marked immutable, and its pixel memory is shareable, it may be shared instead of copied.Image
is returned if bitmap is valid. ValidBitmap
parameters include: dimensions are greater than zero; each dimension fits in 29 bits;ColorType
andAlphaType
are valid, andColorType
is not kUnknown_ColorType
; row bytes are large enough to hold one row of pixels; pixel address is notnil
.Declaration
Swift
public init?(_ bitmap: Bitmap)
Parameters
bitmap
ImageInfo
, row bytes, and pixelsReturn Value
created
Image
, ornil
-
Creates
Image
fromImageInfo
, sharing pixels.Image
is returned ifImageInfo
is valid. ValidImageInfo
parameters include: dimensions are greater than zero; each dimension fits in 29 bits;ColorType
andAlphaType
are valid, andColorType
is not kUnknown_ColorType
; rowBytes are large enough to hold one row of pixels; pixels is notnil
, and contains enough data forImage
.Parameters
info
contains width, height,
AlphaType
,ColorType
,ColorSpace
pixels
address or pixel storage
rowBytes
size of pixel row or larger
Return Value
Image
sharing pixels, ornil
-
Creates
Image
from pixmap, sharingPixmap
pixels. Pixels must remain valid and unchanged until rasterReleaseProc is called. rasterReleaseProc is passed releaseContext whenImage
is deleted or no longer refers to pixmap pixels. Passnil
for rasterReleaseProc to sharePixmap
without requiring a callback whenImage
is released. Passnil
for releaseContext if rasterReleaseProc does not require state.Image
is returned if pixmap is valid. ValidPixmap
parameters include: dimensions are greater than zero; each dimension fits in 29 bits;ColorType
andAlphaType
are valid, andColorType
is not kUnknown_ColorType
; row bytes are large enough to hold one row of pixels; pixel address is notnil
.Declaration
Swift
public init?(pixmap: Pixmap, releaseProc: sk_image_raster_release_proc? = nil, context: UnsafeMutableRawPointer? = nil)
Parameters
pixmap
ImageInfo
, pixel address, and row bytesrasterReleaseProc
function called when pixels can be released; or
nil
releaseContext
state passed to rasterReleaseProc; or
nil
Return Value
Image
sharing pixmap -
Return an image backed by the encoded data, but attempt to defer decoding until the image is actually used/drawn. This deferral allows the system to cache the result, either on the CPU or on the GPU, depending on where the image is drawn. If memory is low, the cache may be purged, causing the next draw of the image to have to re-decode.
The subset parameter specifies a area within the decoded image to create the image from. If subset is null, then the entire image is returned.
This is similar to DecodeTo[Raster,Texture], but this method will attempt to defer the actual decode, while the DecodeTo… method explicitly decode and allocate the backend when the call is made.
If the encoded format is not supported, or subset is outside of the bounds of the decoded image,
nil
is returned.Parameters
encoded
the encoded data
subset
optional, the bounds of the pixels within the decoded image to return. may be null.
Return Value
created
Image
, ornil
-
Encodes
Image
pixels, returning result asData
. Returns existing encoded data if present; otherwise,Image
is encoded withEncodedImageFormat
::kPNG.ia
must be built with SK_HAS_PNG_LIBRARY to encodeImage
. Returnsnil
if existing encoded data is missing or invalid, and encoding fails.Declaration
Swift
public func encode() -> SKData?
Return Value
encoded
Image
, ornil
-
Encodes
Image
pixels, returning result asData
. Returnsnil
if encoding fails, or if encodedImageFormat is not supported.Image
encoding in a format requires both building with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support for the encoded format.If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can additionally be one of:
EncodedImageFormat.ico
,EncodedImageFormat.bmp
,EncodedImageFormat.gif
` quality is a platform and format specific metric trading off size and encoding error. When used, quality equaling 100 encodes with the least error. quality may be ignored by the encoder.Declaration
Swift
public func encode(format: EncodedImageFormat, quality: Int32) -> SKData?
Parameters
encodedImageFormat
one of:
.jpeg
,.png
,.webp
quality
encoder specific metric with 100 equaling best
Return Value
encoded
Image
ornil
-
Returns pixel count in each row.
Declaration
Swift
public var width: Int32 { get }
-
Returns pixel row count.
Declaration
Swift
public var height: Int32 { get }
-
Returns value unique to image.
Image
contents cannot change afterImage
is created. Any operation to create a newImage
will receive generate a new unique number.Declaration
Swift
public var uniqueID: UInt32 { get }
-
The AlphaType returned was a parameter to an SkImage constructor, or was parsed from encoded data.
Declaration
Swift
public var alphaType: AlphaType { get }
-
SkColorType if known; otherwise, returns
.unknown
.Declaration
Swift
public var colorType: ColorType { get }
-
Returns
ColorSpace
, the range of colors, associated withImage
. The reference count ofColorSpace
is unchanged. The returnedColorSpace
is immutable.ColorSpace
returned was passed to anImage
constructor, or was parsed from encoded data.ColorSpace
returned may be ignored whenImage
is drawn, depending on the capabilities of theSurface
receiving the drawing.Declaration
Swift
public var colorSpace: ColorSpace { get }
Return Value
ColorSpace
inImage
, ornil
-
Returns true if
Image
pixels represent transparency only. If true, each pixel is packed in 8 bits as defined by kAlpha_8_ColorType
.Declaration
Swift
public var isAlphaOnly: Bool { get }
Return Value
true if pixels represent a transparency mask
-
Returns encoded
Image
pixels asData
, ifImage
was created from supported encoded stream format. Platform support for formats vary and may require building with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY. Returnsnil
ifImage
contents are not encoded.Declaration
Swift
public var encodedData: SKData { get }
Return Value
encoded
Image
, ornil
-
Creates
Shader
fromImage
.Shader
dimensions are taken fromImage
.Shader
uses transformingImage
beforeCanvas
matrix is applied.Declaration
Swift
public func toShader(tileX: ShaderTileMode, tileY: ShaderTileMode) -> Shader
Parameters
tileX
tiling in the x direction
tileY
tiling in the y direction
Return Value
Shader
containingImage
-
Creates
Shader
fromImage
.Shader
dimensions are taken fromImage
.Shader
usesTileMode
rules to fill drawn area outsideImage
. localMatrix permits transformingImage
beforeCanvas
matrix is applied.Declaration
Swift
public func toShader(tileX: ShaderTileMode, tileY: ShaderTileMode, localMatrix: Matrix) -> Shader
Parameters
tileX
tiling in the x direction
tileY
tiling in the y direction
localMatrix
Image
transformation, ornil
Return Value
Shader
containingImage
-
Copies
Image
pixel address, row bytes, andImageInfo
to pixmap, if address is available, and returns true. If pixel address is not available, return false and leave pixmap unchanged.Declaration
Swift
public func peekPixels(pixmap: Pixmap) -> Bool
Parameters
pixmap
storage for pixel state if pixels are readable; otherwise, ignored
Return Value
true if
Image
has direct access to pixels -
Returns true the contents of
Image
was created on or uploaded to GPU memory, and is available as a GPU texture.Declaration
Swift
public var isTextureBacked: Bool { get }
Return Value
true if
Image
is a GPU texture -
Returns true if
Image
is backed by an image-generator or other service that creates and caches its pixels or texture on-demand.Declaration
Swift
public var isLazyGenerated: Bool { get }
Return Value
true if
Image
is created as needed -
CachingHint selects whether
ia
may internally cacheBitmap
generated by decodingImage
, or by copyingImage
from GPU to CPU. The default behavior allows cachingBitmap
.Choose kDisallow_CachingHint if
Image
pixels are to be used only once, or ifImage
pixels reside in a cache outside ofia
, or to reduce memory pressure.Choosing kAllow_CachingHint does not ensure that pixels will be cached.
See moreImage
pixels may not be cached if memory requirements are too large or pixels are not accessible.Declaration
Swift
public enum CachingHint : UInt32
-
Copies
Rect
of pixels fromImage
to dstPixels. Copy starts at offset (srcX, srcY), and does not exceedImage
(width(), height()). dstInfo specifies width, height,ColorType
,AlphaType
, andColorSpace
of destination. dstRowBytes specifies the gap from one destination row to the next. Returns true if pixels are copied. Returns false if:- dstInfo.addr() equals
nil
- dstRowBytes is less than dstInfo.minRowBytes()
PixelRef
isnil
Pixels are copied only if pixel conversion is possible. IfImage
ColorType
is.gray8
, or.alpha8
; dstInfo.colorType() must match. IfImage
ColorType
is.gray8
, dstInfo.colorSpace() must match. IfImage
AlphaType
is.opaque
, dstInfo.alphaType() must match. IfImage
ColorSpace
isnil
,dstInfo.colorSpace
must match. Returns false if pixel conversion is not possible.srcX
andsrcY
may be negative to copy only top or left of source. Returns false ifwidth
orheight
is zero or negative. Returns false if abs(srcX) >= Image width(), or if abs(srcY) >= Image height(). If cachingHint is.allow
, pixels may be retained locally. If cachingHint is.disallow
, pixels are not added to the local cache.
Declaration
Swift
public func readPixels(dstInfo: ImageInfo, dstPixels: UnsafeMutableRawPointer, dstRowBytes: Int, srcX: Int32, srcY: Int32, cachingHint: CachingHint = .allow) -> Bool
Parameters
dstInfo
destination width, height,
ColorType
,AlphaType
,ColorSpace
dstPixels
destination pixel storage
dstRowBytes
destination row length
srcX
column index whose absolute value is less than
width
srcY
row index whose absolute value is less than
height
cachingHint
defaults to
.allow
Return Value
true if pixels are copied to dstPixels
- dstInfo.addr() equals
-
Copies a
Rect
of pixels fromImage
to dst. Copy starts at (srcX, srcY), and does not exceedImage
(width, height). dst specifies width, height,ColorType
,AlphaType
,ColorSpace
, pixel storage, and row bytes of destination. dst.rowBytes() specifics the gap from one destination row to the next. Returns true if pixels are copied. Returns false if:- dst pixel storage equals
nil
- dst.rowBytes is less than
ImageInfo
::minRowBytes PixelRef
isnil
Pixels are copied only if pixel conversion is possible. IfImage
ColorType
is.gray8
, or.alpha8
; dstInfo.colorType() must match. IfImage
ColorType
is.gray8
, dstInfo.colorSpace() must match. IfImage
AlphaType
is.opaque
, dstInfo.alphaType() must match. IfImage
ColorSpace
isnil
,dstInfo.colorSpace
must match. Returns false if pixel conversion is not possible.srcX
andsrcY
may be negative to copy only top or left of source. Returns false ifwidth
orheight
is zero or negative. Returns false if abs(srcX) >= Image width(), or if abs(srcY) >= Image height(). If cachingHint is.allow
, pixels may be retained locally. If cachingHint is.disallow
, pixels are not added to the local cache.
Declaration
Swift
public func readPixels(pixmap: Pixmap, srcX: Int32, srcY: Int32, cachingHint: CachingHint = .allow) -> Bool
Parameters
dst
srcX
column index whose absolute value is less than
width
srcY
row index whose absolute value is less than
height
cachingHint
defaults to
.allow
Return Value
true if pixels are copied to dst
- dst pixel storage equals
-
Copies
Image
to dst, scaling pixels to fit dst.width() and dst.height(), and converting pixels to match dst.colorType() and dst.alphaType(). Returns true if pixels are copied. Returns false if dst.addr() isnil
, or dst.rowBytes() is less than dstImageInfo
::minRowBytes. Pixels are copied only if pixel conversion is possible. IfImage
ColorType
is.gray8
, or.alpha8
; dstInfo.colorType() must match. IfImage
ColorType
is.gray8
, dstInfo.colorSpace() must match. IfImage
AlphaType
is.opaque
, dstInfo.alphaType() must match. IfImage
ColorSpace
isnil
,dstInfo.colorSpace
must match. Returns false if pixel conversion is not possible. Scales the image, withquality
, to match dst.width() and dst.height(). filterQuality kNone_FilterQuality
is fastest, typically implemented with nearest neighbor filter. kLow_FilterQuality
is typically implemented with bilerp filter. kMedium_FilterQuality
is typically implemented with bilerp filter, and mip-map filter when size is reduced. kHigh_FilterQuality
is slowest, typically implemented with bicubic filter. If cachingHint is kAllow_CachingHint, pixels may be retained locally. If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.Declaration
Swift
public func scalePixels(dest: Pixmap, quality: FilterQuality, cachingHint: CachingHint = .allow) -> Bool
Return Value
true if pixels are scaled to fit dst
-
Returns subset of
Image
. subset must be fully contained byImage
dimensions(). The implementation may share pixels, or may copy them. Returnsnil
if subset is empty, or subset is not contained by bounds, or pixels inImage
could not be read or copied.Declaration
Swift
public func subset(_ subset: IRect) -> Image?
Parameters
subset
bounds of returned
Image
Return Value
partial or full
Image
, ornil
-
Returns a raster-based image of the current image.
Declaration
Swift
public func toRasterImage() -> Image