Initializerswift-png 4.4.5PNG
init(packing:size:layout:metadata:indexer:)
Creates an image from a scalar pixel array, using a custom indexing function.
PNG.Image.swift:985init<T>(packing pixels: [T], size: (x: Int, y: Int), layout: PNG.Layout, metadata: PNG.Metadata = .init(), indexer: ([(r: UInt8, g: UInt8, b: UInt8, a: UInt8)]) -> (UInt8) -> Int) where T : FixedWidthInteger, T : UnsignedInteger
Parameters
- pixels
A scalar pixel array. Its elements are arranged in row-major order. The first pixel in this array corresponds to the top-left corner of the image.
The length of this array must match
size.x * size.y
. Passing an array of the wrong length will result in a precondition failure.- size
The size of the image. Both dimensions must be greater than zero. Passing an invalid image size will result in a precondition failure.
- layout
An image layout.
- metadata
A metadata structure. The default value is an empty metadata structure.
- indexer
A function which uses the palette entries in the color
format
to generate a referencing function. This function will only be invoked if the image colorformat
is an indexed format.
For an image with a grayscale-alpha color format
, this function assigns the gray channel to the given scalars, and sets the alpha channel to T.max
.
For an image with an RGB or BGR color format
, this function assigns all channels to the given scalars, replicating each scalar three times.
For an image with an RGBA or BGRA color format
, this function assigns all opaque channels to the given scalars, replicating each scalar three times, and sets the alpha channel to T.max
.
The scalar values are assumed to fill the entire range of T
.
See the indexed color tutorial for more about the semantics of this function.
Other members in extension
Type members
init<Color>(packing: [Color], size: (x: Int, y: Int), layout: PNG.Layout, metadata: PNG.Metadata
) Creates an image from a pixel array.
init<T>(packing: [T], size: (x: Int, y: Int), layout: PNG.Layout, metadata: PNG.Metadata
) Creates an image from a scalar pixel array.
init<Color>(packing: [Color], size: (x: Int, y: Int), layout: PNG.Layout, metadata: PNG.Metadata, indexer: ([(r: UInt8, g: UInt8, b: UInt8, a: UInt8)]) -> (Color.Aggregate) -> Int
) Creates an image from a pixel array, using a custom indexing function.
static func decompress(path: String
) throws -> `Self`? Decompresses and decodes a PNG from a file at the given file path.
static func decompress<Source>(stream: inout Source
) throws -> PNG.Image Decompresses and decodes a PNG from the given bytestream.
Instance members
let layout: PNG.Layout
The layout of this image.
var metadata: PNG.Metadata
The metadata in this image.
let size: (x: Int, y: Int)
The size of this image, measured in pixels.
var storage: [UInt8]
The raw backing storage of the image content.
func bindStorage(to: PNG.Layout
) -> PNG.Image Rebinds this image to a compatible layout.
func compress(path: String, level: Int, hint: Int
) throws -> Void? Encodes and compresses a PNG to a file at the given file path.
func compress<Destination>(stream: inout Destination, level: Int, hint: Int
) throws Encodes and compresses a PNG to the given bytestream.
func unpack<Color>(as: Color.Type
) -> [Color] Unpacks this image to a pixel array.
func unpack<T>(as: T.Type
) -> [T] Unpacks this image to a scalar pixel array.
func unpack<Color>(as: Color.Type, deindexer: ([(r: UInt8, g: UInt8, b: UInt8, a: UInt8)]) -> (Int) -> Color.Aggregate
) -> [Color] Unpacks this image to a pixel array, using a custom deindexing function.
func unpack<T>(as: T.Type, deindexer: ([(r: UInt8, g: UInt8, b: UInt8, a: UInt8)]) -> (Int) -> UInt8
) -> [T] Unpacks this image to a scalar pixel array, using a custom deindexing function.