Skip to content

Commit 16222b3

Browse files
committed
Tweak transparency algorithm
1 parent 099f290 commit 16222b3

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

Diff for: Ice/Utilities/Extensions.swift

+6-7
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ extension CGImage {
151151
// MARK: Trim Transparent Pixels
152152

153153
/// A context for handling transparency data in an image.
154-
private final class TransparencyContext {
154+
private struct TransparencyContext: ~Copyable {
155155
private let image: CGImage
156156
private let maxAlpha: UInt8
157157
private let cgContext: CGContext
@@ -243,7 +243,7 @@ extension CGImage {
243243
}
244244
}
245245

246-
private func isPixelOpaque(column: Int, row: Int) -> Bool {
246+
private func isPixelOpaque(row: Int, column: Int) -> Bool {
247247
guard let bitmapData = cgContext.data else {
248248
return false
249249
}
@@ -261,18 +261,17 @@ extension CGImage {
261261
if memcmp(rowByteBlock, zeroByteBlock, image.width) == 0 {
262262
return true
263263
}
264-
// We found a non-zero row. Check each pixel's alpha until we find one
265-
// that is "opaque".
264+
// We found a non-zero row. Check each pixel until we find one that is opaque.
266265
return columnRange.contains { column in
267-
isPixelOpaque(column: column, row: row)
266+
isPixelOpaque(row: row, column: column)
268267
}
269268
}
270269
}
271270

272271
private func firstOpaqueColumn<S: Sequence>(in columnRange: S) -> Int? where S.Element == Int {
273272
columnRange.first { column in
274273
rowRange.contains { row in
275-
isPixelOpaque(column: column, row: row)
274+
isPixelOpaque(row: row, column: column)
276275
}
277276
}
278277
}
@@ -289,7 +288,7 @@ extension CGImage {
289288
around edges: Set<CGRectEdge> = [.minXEdge, .maxXEdge, .minYEdge, .maxYEdge],
290289
maxAlpha: CGFloat = 0
291290
) -> CGImage? {
292-
let maxAlpha = min(UInt8(maxAlpha * 255), 255)
291+
let maxAlpha = UInt8(maxAlpha.clamped(to: 0...1) * 255)
293292
let context = TransparencyContext(image: self, maxAlpha: maxAlpha)
294293
return context?.trim(edges: edges)
295294
}

0 commit comments

Comments
 (0)