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. Valid Bitmap parameters include: dimensions are greater than zero; each dimension fits in 29 bits; ColorType and AlphaType are valid, and ColorType is not kUnknown_ColorType; row bytes are large enough to hold one row of pixels; pixel address is not nil.

    Declaration

    Swift

    public init?(_ bitmap: Bitmap)

    Parameters

    bitmap

    ImageInfo, row bytes, and pixels

    Return Value

    created Image, or nil

  • Creates Image from ImageInfo, sharing pixels. Image is returned if ImageInfo is valid. Valid ImageInfo parameters include: dimensions are greater than zero; each dimension fits in 29 bits; ColorType and AlphaType are valid, and ColorType is not kUnknown_ColorType; rowBytes are large enough to hold one row of pixels; pixels is not nil, and contains enough data for Image.

    Declaration

    Swift

    public init?(info: ImageInfo, data: SKData, rowBytes: Int)

    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, or nil

  • Creates Image from pixmap, sharing Pixmap pixels. Pixels must remain valid and unchanged until rasterReleaseProc is called. rasterReleaseProc is passed releaseContext when Image is deleted or no longer refers to pixmap pixels. Pass nil for rasterReleaseProc to share Pixmap without requiring a callback when Image is released. Pass nil for releaseContext if rasterReleaseProc does not require state. Image is returned if pixmap is valid. Valid Pixmap parameters include: dimensions are greater than zero; each dimension fits in 29 bits; ColorType and AlphaType are valid, and ColorType is not kUnknown_ColorType; row bytes are large enough to hold one row of pixels; pixel address is not nil.

    Declaration

    Swift

    public init?(pixmap: Pixmap, releaseProc: sk_image_raster_release_proc? = nil, context: UnsafeMutableRawPointer? = nil)

    Parameters

    pixmap

    ImageInfo, pixel address, and row bytes

    rasterReleaseProc

    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.

    Declaration

    Swift

    public init?(data: SKData, subset: IRect? = nil)

    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, or nil

  • Encodes Image pixels, returning result as Data. Returns existing encoded data if present; otherwise, Image is encoded with EncodedImageFormat::kPNG. ia must be built with SK_HAS_PNG_LIBRARY to encode Image. Returns nil if existing encoded data is missing or invalid, and encoding fails.

    Declaration

    Swift

    public func encode() -> SKData?

    Return Value

    encoded Image, or nil

  • Encodes Image pixels, returning result as Data. Returns nil 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 or nil

  • 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 after Image is created. Any operation to create a new Image 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 with Image. The reference count of ColorSpace is unchanged. The returned ColorSpace is immutable. ColorSpace returned was passed to an Image constructor, or was parsed from encoded data. ColorSpace returned may be ignored when Image is drawn, depending on the capabilities of the Surface receiving the drawing.

    Declaration

    Swift

    public var colorSpace: ColorSpace { get }

    Return Value

    ColorSpace in Image, or nil

  • 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 as Data, if Image 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. Returns nil if Image contents are not encoded.

    Declaration

    Swift

    public var encodedData: SKData { get }

    Return Value

    encoded Image, or nil

  • Creates Shader from Image. Shader dimensions are taken from Image. Shader uses transforming Image before Canvas 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 containing Image

  • Creates Shader from Image. Shader dimensions are taken from Image. Shader uses TileMode rules to fill drawn area outside Image. localMatrix permits transforming Image before Canvas 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, or nil

    Return Value

    Shader containing Image

  • Copies Image pixel address, row bytes, and ImageInfo 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 cache Bitmap generated by decoding Image, or by copying Image from GPU to CPU. The default behavior allows caching Bitmap.

    Choose kDisallow_CachingHint if Image pixels are to be used only once, or if Image pixels reside in a cache outside of ia, or to reduce memory pressure.

    Choosing kAllow_CachingHint does not ensure that pixels will be cached. Image pixels may not be cached if memory requirements are too large or pixels are not accessible.

    See more

    Declaration

    Swift

    public enum CachingHint : UInt32
  • Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY), and does not exceed Image (width(), height()). dstInfo specifies width, height, ColorType, AlphaType, and ColorSpace 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 is nil Pixels are copied only if pixel conversion is possible. If Image ColorType is .gray8, or .alpha8; dstInfo.colorType() must match. If Image ColorType is .gray8, dstInfo.colorSpace() must match. If Image AlphaType is .opaque, dstInfo.alphaType() must match. If Image ColorSpace is nil, dstInfo.colorSpace must match. Returns false if pixel conversion is not possible. srcX and srcY may be negative to copy only top or left of source. Returns false if width or height 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

  • Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and does not exceed Image (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 is nil Pixels are copied only if pixel conversion is possible. If Image ColorType is .gray8, or .alpha8; dstInfo.colorType() must match. If Image ColorType is .gray8, dstInfo.colorSpace() must match. If Image AlphaType is .opaque, dstInfo.alphaType() must match. If Image ColorSpace is nil, dstInfo.colorSpace must match. Returns false if pixel conversion is not possible. srcX and srcY may be negative to copy only top or left of source. Returns false if width or height 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

    destination Pixmap: ImageInfo, pixels, row bytes

    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

  • 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() is nil, or dst.rowBytes() is less than dst ImageInfo::minRowBytes. Pixels are copied only if pixel conversion is possible. If Image ColorType is .gray8, or .alpha8; dstInfo.colorType() must match. If Image ColorType is .gray8, dstInfo.colorSpace() must match. If Image AlphaType is .opaque, dstInfo.alphaType() must match. If Image ColorSpace is nil, dstInfo.colorSpace must match. Returns false if pixel conversion is not possible. Scales the image, with quality, 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

    Parameters

    dst

    destination Pixmap: ImageInfo, pixels, row bytes

    cachingHint

    defaults to .allow

    Return Value

    true if pixels are scaled to fit dst

  • Returns subset of Image. subset must be fully contained by Image dimensions(). The implementation may share pixels, or may copy them. Returns nil if subset is empty, or subset is not contained by bounds, or pixels in Image 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, or nil

  • Returns a raster-based image of the current image.

    Declaration

    Swift

    public func toRasterImage() -> Image