Bitmap 1.3.1

Bitmap 1.3.1

Darren Ford维护。



Bitmap 1.3.1

  • 作者
  • Darren Ford

Bitmap

tag Swift License MIT SPM

macOS iOS tvOS watchOS macCatalyst

Swift 便捷方式用于加载数据/保存和操纵位图图像。

为什么?

我希望有一个简单的 Swift 界面来封装我在日常图像需求中遇到的一些常见的图像操作,主要有直接绘制到位图图像的便利。

  • 支持所有 Apple 平台(在某些平台上(如 watchOS)有限制)
  • 支持加载非 RGBA 格式的图像(例如 CMYK),通过在加载期间将图像转换为 RGBA 来实现。
  • 通过其 representation 属性轻松保存对象。
  • 支持处理位图的可变性和不可变性方法(函数式风格)。
  • 位图操作函数
  • 支持将位图信息推送到任务之间的 Sendable

定义

Bitmap 对象表示一个 RGBA 图像。图像数据内部存储在一个简单的 1-D 字节数组中,表示像素的 R、G、B、A 序列。

位图对象保留并管理位图的 CGContext 表示,以便允许直接在位图本身上进行 CG 操作。

  • Bitmap 的坐标系统从左下角的 (0, 0) 开始。
  • 所有操作/操纵/坐标都从左下角发生。

创建一个新的图像

简单又方便 :-)

var bitmap = try Bitmap(width: 640, height: 480)

将图像加载到 Bitmap 中

支持加载时支持的任何文件格式,例如 NSImageUIImageCGImageSource

// From a file...
var bitmap = try Bitmap(fileURL: ...)

// From image data
var bitmap = try Bitmap(imageData: ...)

保存位图

支持保存时支持的任何文件格式,例如 NSImageUIImageCGImageSource

Bitmap 提供写入某些不同格式(如 png)的方法。位图上的 representation 属性包含用于生成不同格式的图像数据的方法

var bitmap = try Bitmap(fileURL: ...)
...
let pngData = bitmap.representation?.png()

在位图中绘制

使用 CG 方法在位图上下文中绘制

var bitmap = try Bitmap(width: 640, height: 480)
bitmap.draw { ctx in
   ctx.setFillColor(.black)
   ctx.fill([CGRect(x: 5, y: 5, width: 20, height: 20)])
}

将图像绘制到位图中

var bitmap = try Bitmap(width: 640, height: 480)
// Draw an image into a defined rectangle (with optional scaling types aspectFit, aspectFill, axes independent)
bitmap.drawImage(image, in: CGRect(x: 50, y: 50, width: 100, height: 100))
// Draw a bitmap at a point
bitmap.drawBitmap(bitmap, at: CGPoint(x: 300, y: 300))

获取/设置单个像素

您可以通过下标或在 Bitmap 上的 get/setPixel 方法中直接访问位图中的像素信息

var bitmap = Bitmap(...)

// Retrieve the color of the pixel at x = 4, y = 3.
let rgbaPixel = bitmap[4, 3]   // or bitmap.getPixel(x: 4, y: 3)

// Set the color of the pixel at x = 4, y = 3. Only available when `bitmap` is mutable
bitmap[4, 3] = Bitmap.RGBA(r: 255, g: 0, b: 0, a: 255)   // or bitmap.setPixel(x: 4, y: 3, ...)

位图操作

Bitmap 提供了一些内建的操作(例如旋转、着色)来操作 Bitmap 实例。

可变

可变函数在位图变量上操作。每个方法都会修改原始的位图实例。

// Load an image, rotate it and convert it to grayscale
var bitmap = try Bitmap(fileURL: ...)
try bitmap.rotate(by: 1.4)
try bitmap.grayscale()

不可变

不可变函数在位图的副本上工作,并返回副本。原始位图(可以是 let 变量)保持不变。

不可变函数变体以 'ing' 结尾命名,例如 tinting()scaling()

// Load an image, rotate it and convert it to grayscale
let bitmap = try Bitmap(fileURL: ...)
let rotated = try bitmap.rotating(by: 1.4)
let grayscale = try rotated.grayscaling()

这允许在位图上进行函数式链式调用。

// Functional style
let bitmap = try Bitmap(fileURL: ...)
	.rotating(by: 1.4)
	.grayscaling()

可用的位图操作函数

  • 调整大小/缩放
  • 填充/内边距
  • 裁剪/蒙版
  • 旋转
  • 滚动
  • 去除透明度
  • 挖洞
  • 颜色 Manipulation(如灰度、着色、伽玛调整、饱和度等)
  • 翻转
  • 绘制(如线条、形状、路径、填充/描边)
  • 绘制文本
  • 绘制边框

…等等!

Sendable 支持

您无法直接在任务之间发送 Bitmap 对象。这是由于 Bitmap 内部缓存一个 CGContext 实例,以提高性能并减少内存占用。

在内部,Bitmap 使用 Bitmap.RGBAData 对象来存储像素信息,这些对象符合 Sendable 规范。这些对象可以在任务之间安全传递,并且可以很容易地重新组成新的 Bitmap 对象。

var myBitmap = try Bitmap(...)
let bitmapData = myBitmap.bitmapData

// Pass `bitmapData` to another Task

var myBitmap = try Bitmap(bitmapData)

许可证

MIT License

Copyright (c) 2024 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.