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
Imagefrom 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.Imageis returned if bitmap is valid. ValidBitmapparameters include: dimensions are greater than zero; each dimension fits in 29 bits;ColorTypeandAlphaTypeare valid, andColorTypeis 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
bitmapImageInfo, row bytes, and pixelsReturn Value
created
Image, ornil -
Creates
ImagefromImageInfo, sharing pixels.Imageis returned ifImageInfois valid. ValidImageInfoparameters include: dimensions are greater than zero; each dimension fits in 29 bits;ColorTypeandAlphaTypeare valid, andColorTypeis not kUnknown_ColorType; rowBytes are large enough to hold one row of pixels; pixels is notnil, and contains enough data forImage.Parameters
infocontains width, height,
AlphaType,ColorType,ColorSpacepixelsaddress or pixel storage
rowBytessize of pixel row or larger
Return Value
Imagesharing pixels, ornil -
Creates
Imagefrom pixmap, sharingPixmappixels. Pixels must remain valid and unchanged until rasterReleaseProc is called. rasterReleaseProc is passed releaseContext whenImageis deleted or no longer refers to pixmap pixels. Passnilfor rasterReleaseProc to sharePixmapwithout requiring a callback whenImageis released. Passnilfor releaseContext if rasterReleaseProc does not require state.Imageis returned if pixmap is valid. ValidPixmapparameters include: dimensions are greater than zero; each dimension fits in 29 bits;ColorTypeandAlphaTypeare valid, andColorTypeis 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
pixmapImageInfo, pixel address, and row bytesrasterReleaseProcfunction called when pixels can be released; or
nilreleaseContextstate passed to rasterReleaseProc; or
nilReturn Value
Imagesharing 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,
nilis returned.Parameters
encodedthe encoded data
subsetoptional, the bounds of the pixels within the decoded image to return. may be null.
Return Value
created
Image, ornil -
Encodes
Imagepixels, returning result asData. Returns existing encoded data if present; otherwise,Imageis encoded withEncodedImageFormat::kPNG.iamust be built with SK_HAS_PNG_LIBRARY to encodeImage. Returnsnilif existing encoded data is missing or invalid, and encoding fails.Declaration
Swift
public func encode() -> SKData?Return Value
encoded
Image, ornil -
Encodes
Imagepixels, returning result asData. Returnsnilif encoding fails, or if encodedImageFormat is not supported.Imageencoding 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
encodedImageFormatone of:
.jpeg,.png,.webpqualityencoder specific metric with 100 equaling best
Return Value
encoded
Imageornil -
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.
Imagecontents cannot change afterImageis created. Any operation to create a newImagewill 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 ofColorSpaceis unchanged. The returnedColorSpaceis immutable.ColorSpacereturned was passed to anImageconstructor, or was parsed from encoded data.ColorSpacereturned may be ignored whenImageis drawn, depending on the capabilities of theSurfacereceiving the drawing.Declaration
Swift
public var colorSpace: ColorSpace { get }Return Value
ColorSpaceinImage, ornil -
Returns true if
Imagepixels 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
Imagepixels asData, ifImagewas 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. ReturnsnilifImagecontents are not encoded.Declaration
Swift
public var encodedData: SKData { get }Return Value
encoded
Image, ornil -
Creates
ShaderfromImage.Shaderdimensions are taken fromImage.Shaderuses transformingImagebeforeCanvasmatrix is applied.Declaration
Swift
public func toShader(tileX: ShaderTileMode, tileY: ShaderTileMode) -> ShaderParameters
tileXtiling in the x direction
tileYtiling in the y direction
Return Value
ShadercontainingImage -
Creates
ShaderfromImage.Shaderdimensions are taken fromImage.ShaderusesTileModerules to fill drawn area outsideImage. localMatrix permits transformingImagebeforeCanvasmatrix is applied.Declaration
Swift
public func toShader(tileX: ShaderTileMode, tileY: ShaderTileMode, localMatrix: Matrix) -> ShaderParameters
tileXtiling in the x direction
tileYtiling in the y direction
localMatrixImagetransformation, ornilReturn Value
ShadercontainingImage -
Copies
Imagepixel address, row bytes, andImageInfoto 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) -> BoolParameters
pixmapstorage for pixel state if pixels are readable; otherwise, ignored
Return Value
true if
Imagehas direct access to pixels -
Returns true the contents of
Imagewas 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
Imageis a GPU texture -
Returns true if
Imageis 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
Imageis created as needed -
CachingHint selects whether
iamay internally cacheBitmapgenerated by decodingImage, or by copyingImagefrom GPU to CPU. The default behavior allows cachingBitmap.Choose kDisallow_CachingHint if
Imagepixels are to be used only once, or ifImagepixels reside in a cache outside ofia, or to reduce memory pressure.Choosing kAllow_CachingHint does not ensure that pixels will be cached.
See moreImagepixels may not be cached if memory requirements are too large or pixels are not accessible.Declaration
Swift
public enum CachingHint : UInt32 -
Copies
Rectof pixels fromImageto dstPixels. Copy starts at offset (srcX, srcY), and does not exceedImage(width(), height()). dstInfo specifies width, height,ColorType,AlphaType, andColorSpaceof 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()
PixelRefisnilPixels are copied only if pixel conversion is possible. IfImageColorTypeis.gray8, or.alpha8; dstInfo.colorType() must match. IfImageColorTypeis.gray8, dstInfo.colorSpace() must match. IfImageAlphaTypeis.opaque, dstInfo.alphaType() must match. IfImageColorSpaceisnil,dstInfo.colorSpacemust match. Returns false if pixel conversion is not possible.srcXandsrcYmay be negative to copy only top or left of source. Returns false ifwidthorheightis 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) -> BoolParameters
dstInfodestination width, height,
ColorType,AlphaType,ColorSpacedstPixelsdestination pixel storage
dstRowBytesdestination row length
srcXcolumn index whose absolute value is less than
widthsrcYrow index whose absolute value is less than
heightcachingHintdefaults to
.allowReturn Value
true if pixels are copied to dstPixels
- dstInfo.addr() equals
-
Copies a
Rectof pixels fromImageto 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 PixelRefisnilPixels are copied only if pixel conversion is possible. IfImageColorTypeis.gray8, or.alpha8; dstInfo.colorType() must match. IfImageColorTypeis.gray8, dstInfo.colorSpace() must match. IfImageAlphaTypeis.opaque, dstInfo.alphaType() must match. IfImageColorSpaceisnil,dstInfo.colorSpacemust match. Returns false if pixel conversion is not possible.srcXandsrcYmay be negative to copy only top or left of source. Returns false ifwidthorheightis 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) -> BoolParameters
dstsrcXcolumn index whose absolute value is less than
widthsrcYrow index whose absolute value is less than
heightcachingHintdefaults to
.allowReturn Value
true if pixels are copied to dst
- dst pixel storage equals
-
Copies
Imageto 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. IfImageColorTypeis.gray8, or.alpha8; dstInfo.colorType() must match. IfImageColorTypeis.gray8, dstInfo.colorSpace() must match. IfImageAlphaTypeis.opaque, dstInfo.alphaType() must match. IfImageColorSpaceisnil,dstInfo.colorSpacemust match. Returns false if pixel conversion is not possible. Scales the image, withquality, to match dst.width() and dst.height(). filterQuality kNone_FilterQualityis fastest, typically implemented with nearest neighbor filter. kLow_FilterQualityis typically implemented with bilerp filter. kMedium_FilterQualityis typically implemented with bilerp filter, and mip-map filter when size is reduced. kHigh_FilterQualityis 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) -> BoolReturn Value
true if pixels are scaled to fit dst
-
Returns subset of
Image. subset must be fully contained byImagedimensions(). The implementation may share pixels, or may copy them. Returnsnilif subset is empty, or subset is not contained by bounds, or pixels inImagecould not be read or copied.Declaration
Swift
public func subset(_ subset: IRect) -> Image?Parameters
subsetbounds of returned
ImageReturn Value
partial or full
Image, ornil -
Returns a raster-based image of the current image.
Declaration
Swift
public func toRasterImage() -> Image
View on GitHub
Install in Dash
Image Class Reference