TextureMap

概述

Texture Map 是一个 Swift 6 包,用于处理图像和纹理。可以在各种格式之间转换图像或原始数据。专为 iOS、macOS 和 visionOS 打造。由 Metal 提供支持。


特性

  1. 跨平台支持:
    • 提供对 macOS 和 iOS 的无缝支持,可以互换使用 NSImageUIImage
  2. 高位深颜色支持
    • 处理 8 bit16 bit32 bit 的图形。
  3. 图像格式:
    • UIImage/NSImageCGImageCIImageCVPixelBufferCMSampleBufferMTLTexture 之间进行转换。
    • 跨平台支持获取 TIFFPNGJPG 数据。
  4. Metal 纹理实用工具:
    • 创建具有指定像素格式和尺寸的空纹理。
    • 支持 2D、3D 和数组纹理。
    • 纹理复制和采样。
  5. 原始数据操作:
    • 提取归一化或原始纹理数据,格式为 UInt8Float16Float32
    • 从原始数据数组创建纹理。

安装

通过将其作为 Swift 包集成,将 Texture Map 添加到您的项目中。使用仓库 URL

dependencies: [
    .package(url: "https://github.com/heestand-xyz/TextureMap", from: "2.0.0")
]

要求


用法

将图像转换为纹理

import TextureMap

let image: UIImage = UIImage(named: "Example")!
let texture: MTLTexture = try TextureMap.texture(image: image)

从纹理中提取原始数据

let rawChannels: [UInt8] = try TextureMap.raw8(texture: texture)

将纹理转换为图像

let outputImage: UIImage = try await texture.image(colorSpace: .sRGB, bits: ._8)

复制 Metal 纹理

let originalTexture: MTLTexture = ... // Your Metal texture

do {
    let copiedTexture: MTLTexture = try await originalTexture.copy()
    print("Copied texture: \(copiedTexture)")
} catch {
    print("Error copying texture: \(error)")
}

从原始归一化数据创建纹理

let rawTexture: MTLTexture = ...
let bits: TMBits = ._8

do {
    let normalizedRawData: [CGFloat] = try await TextureMap.rawNormalized(texture: rawTexture, bits: bits)
    print("Normalized raw data: \(normalizedRawData)")
} catch {
    print("Error extracting raw data: \(error)")
}

转换纹理颜色空间

let inputTexture: MTLTexture = ...
let fromColorSpace: CGColorSpace = CGColorSpace(name: CGColorSpace.sRGB)!
let toColorSpace: CGColorSpace = CGColorSpace(name: CGColorSpace.displayP3)!

do {
    let convertedTexture: MTLTexture = try await inputTexture.convertColorSpace(from: fromColorSpace, to: toColorSpace)
    print("Converted texture: \(convertedTexture)")
} catch {
    print("Error converting texture color space: \(error)")
}

颜色空间


贡献

欢迎通过提交 pull request 或报告问题来做出贡献。


许可证

该库在 MIT 许可证下可用。


致谢

Anton Heestand 开发