SwiftarrImage

public class SwiftarrImage

Image wrapper backed by libvips. Manages a VipsImage pointer with automatic cleanup. Operations return new instances — vips images are immutable.

  • The underlying libvips image pointer.

    Declaration

    Swift

    private var vipsImage: UnsafeMutablePointer<VipsImage>
  • Retains the encoded source buffer for images loaded via init(data:). libvips loads encoded buffers lazily and reads pixel data on demand, so the buffer must outlive the image. Holding it here ties its lifetime to this instance instead of the caller’s local Data. Nil for images created from owned pixel data or derived from operations.

    Declaration

    Swift

    private let sourceData: Data?
  • Image dimensions.

    Declaration

    Swift

    public var size: Size { get }
  • Whether the image has an alpha channel.

    Declaration

    Swift

    public var hasAlpha: Bool { get }
  • One-time libvips initialization. Call once at app startup (e.g. in configure.swift).

    Declaration

    Swift

    public static func initializeVips()
  • Load an image from a data buffer. Auto-detects format and auto-rotates per EXIF.

    Throws

    ImageError.invalidImage if the data can’t be parsed

    Declaration

    Swift

    public init(data: Data) throws

    Parameters

    data

    Raw image bytes (JPEG, PNG, GIF, WebP, TIFF, BMP, HEIC, AVIF, JXL)

  • Create an image from raw pixel data (e.g. for identicon/QR code generation).

    Declaration

    Swift

    public init(width: Int, height: Int, pixels: [UInt8], bands: Int = 3) throws

    Parameters

    width

    Image width in pixels

    height

    Image height in pixels

    pixels

    Raw pixel data, must be exactly width * height * bands bytes

    bands

    Number of color bands (3 for RGB, 4 for RGBA). Defaults to 3.

  • Internal init from an already-owned VipsImage pointer.

    Declaration

    Swift

    private init(vipsImage: UnsafeMutablePointer<VipsImage>)

Operations

  • Resize to exact width and height.

    Declaration

    Swift

    public func resizedTo(width: Int, height: Int) -> SwiftarrImage?
  • Resize to exact width and height using nearest-neighbor interpolation. Produces crisp pixel-art scaling — use for QR codes and identicons.

    Declaration

    Swift

    public func resizedToNearest(width: Int, height: Int) -> SwiftarrImage?
  • Resize to a target width, preserving aspect ratio.

    Declaration

    Swift

    public func resizedTo(width: Int) -> SwiftarrImage?
  • Resize to a target height, preserving aspect ratio.

    Declaration

    Swift

    public func resizedTo(height: Int) -> SwiftarrImage?
  • Crop a rectangular region from the image.

    Declaration

    Swift

    public func cropped(to rect: Rectangle) -> SwiftarrImage?
  • Flatten alpha channel onto a solid background color. Returns nil if the image has no alpha channel.

    Declaration

    Swift

    public func flattened(background: Color = .white) -> SwiftarrImage?

Export