PPMKit

使用 Swift 语言简单地读写 PPM 文件 (P3/P6) (macOS, iOS, tvOS, watchOS, macCatalyst, Linux)。

Swift Package Manager

读取 PPM 文件

将 PPM 文件加载为 CGImage

let ppmData = Data(...)
let cgImage = try PPM.readImage(data: ppmData)

从文件加载原始 PPM 图像数据

let image: PPM.Image = try PPM.read(fileURL: ppm1URL)
// width = image.width
// height = image.height
// rawRGBBytes = image.data 

写入 PPM 文件

将 CGImage 写入 PPM 文件

注意:PPM 不支持透明度,因此任何透明度信息都会丢失。

let cgImage = CGImage(...)
let data = try PPM.writeImage(cgImage, format: .P3)

将原始 RGB 数据写入 PPM 文件

let image = PPM.Image(...)
try image.write(format: .P3, fileURL: <some_url>)

从原始像素创建 PPM 数据

public static func image(rgbData: [PPM.RGB], width: Int, height: Int) throws -> PPM.Image

注意:rgbData 数组中的像素数量必须等于 width * height

let pixels: [PPM.RGB] = [ ... ]
let image = PPM.image(rgbData: pixels, width: 100, height: 100)

Linux 支持

使用 Mac 构建/测试 Linux 支持

请参阅:使用 Docker 在 Linux 上测试 Swift 包

  1. 在您的 Mac 上安装 适用于 Mac 的 Docker Desktop
  2. 确保 Docker 正在运行(否则,下一个命令将失败,并显示奇怪的或没有错误消息)
  3. 在您想要镜像到 Linux 的目录中运行以下命令
docker run --rm --privileged --interactive --tty --volume "$(pwd):/src" --workdir "/src" swift:latest

现在,从 Docker 容器内部运行

swift build
swift test

请注意,Linux 容器中的 /src 目录是主机操作系统上当前目录的直接镜像,而不是副本。 如果您删除 Linux 容器中 /src 中的文件,该文件也将在主机操作系统上消失。

许可证

MIT License

Copyright (c) 2022 Darren Ford

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.