fix: cap camera snap payload size
This commit is contained in:
@@ -7,6 +7,7 @@ public enum JPEGTranscodeError: LocalizedError, Sendable {
|
||||
case decodeFailed
|
||||
case propertiesMissing
|
||||
case encodeFailed
|
||||
case sizeLimitExceeded(maxBytes: Int, actualBytes: Int)
|
||||
|
||||
public var errorDescription: String? {
|
||||
switch self {
|
||||
@@ -16,6 +17,8 @@ public enum JPEGTranscodeError: LocalizedError, Sendable {
|
||||
"Failed to read image properties"
|
||||
case .encodeFailed:
|
||||
"Failed to encode JPEG"
|
||||
case let .sizeLimitExceeded(maxBytes, actualBytes):
|
||||
"JPEG exceeds size limit (\(actualBytes) bytes > \(maxBytes) bytes)"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,7 +35,8 @@ public struct JPEGTranscoder: Sendable {
|
||||
public static func transcodeToJPEG(
|
||||
imageData: Data,
|
||||
maxWidthPx: Int?,
|
||||
quality: Double) throws -> (data: Data, widthPx: Int, heightPx: Int)
|
||||
quality: Double,
|
||||
maxBytes: Int? = nil) throws -> (data: Data, widthPx: Int, heightPx: Int)
|
||||
{
|
||||
guard let src = CGImageSourceCreateWithData(imageData as CFData, nil) else {
|
||||
throw JPEGTranscodeError.decodeFailed
|
||||
@@ -58,7 +62,7 @@ public struct JPEGTranscoder: Sendable {
|
||||
let orientedHeight = rotates90 ? pixelWidth : pixelHeight
|
||||
|
||||
let maxDim = max(orientedWidth, orientedHeight)
|
||||
let targetMaxPixelSize: Int = {
|
||||
var targetMaxPixelSize: Int = {
|
||||
guard let maxWidthPx, maxWidthPx > 0 else { return maxDim }
|
||||
guard orientedWidth > maxWidthPx else { return maxDim } // never upscale
|
||||
|
||||
@@ -66,28 +70,66 @@ public struct JPEGTranscoder: Sendable {
|
||||
return max(1, Int((Double(maxDim) * scale).rounded(.toNearestOrAwayFromZero)))
|
||||
}()
|
||||
|
||||
let thumbOpts: [CFString: Any] = [
|
||||
kCGImageSourceCreateThumbnailFromImageAlways: true,
|
||||
kCGImageSourceCreateThumbnailWithTransform: true,
|
||||
kCGImageSourceThumbnailMaxPixelSize: targetMaxPixelSize,
|
||||
kCGImageSourceShouldCacheImmediately: true,
|
||||
]
|
||||
func encode(maxPixelSize: Int, quality: Double) throws -> (data: Data, widthPx: Int, heightPx: Int) {
|
||||
let thumbOpts: [CFString: Any] = [
|
||||
kCGImageSourceCreateThumbnailFromImageAlways: true,
|
||||
kCGImageSourceCreateThumbnailWithTransform: true,
|
||||
kCGImageSourceThumbnailMaxPixelSize: maxPixelSize,
|
||||
kCGImageSourceShouldCacheImmediately: true,
|
||||
]
|
||||
|
||||
guard let img = CGImageSourceCreateThumbnailAtIndex(src, 0, thumbOpts as CFDictionary) else {
|
||||
throw JPEGTranscodeError.decodeFailed
|
||||
guard let img = CGImageSourceCreateThumbnailAtIndex(src, 0, thumbOpts as CFDictionary) else {
|
||||
throw JPEGTranscodeError.decodeFailed
|
||||
}
|
||||
|
||||
let out = NSMutableData()
|
||||
guard let dest = CGImageDestinationCreateWithData(out, UTType.jpeg.identifier as CFString, 1, nil) else {
|
||||
throw JPEGTranscodeError.encodeFailed
|
||||
}
|
||||
let q = self.clampQuality(quality)
|
||||
let encodeProps = [kCGImageDestinationLossyCompressionQuality: q] as CFDictionary
|
||||
CGImageDestinationAddImage(dest, img, encodeProps)
|
||||
guard CGImageDestinationFinalize(dest) else {
|
||||
throw JPEGTranscodeError.encodeFailed
|
||||
}
|
||||
|
||||
return (out as Data, img.width, img.height)
|
||||
}
|
||||
|
||||
let out = NSMutableData()
|
||||
guard let dest = CGImageDestinationCreateWithData(out, UTType.jpeg.identifier as CFString, 1, nil) else {
|
||||
throw JPEGTranscodeError.encodeFailed
|
||||
}
|
||||
let q = self.clampQuality(quality)
|
||||
let encodeProps = [kCGImageDestinationLossyCompressionQuality: q] as CFDictionary
|
||||
CGImageDestinationAddImage(dest, img, encodeProps)
|
||||
guard CGImageDestinationFinalize(dest) else {
|
||||
throw JPEGTranscodeError.encodeFailed
|
||||
guard let maxBytes, maxBytes > 0 else {
|
||||
return try encode(maxPixelSize: targetMaxPixelSize, quality: quality)
|
||||
}
|
||||
|
||||
return (out as Data, img.width, img.height)
|
||||
let minQuality = max(0.2, self.clampQuality(quality) * 0.35)
|
||||
let minPixelSize = 256
|
||||
var best = try encode(maxPixelSize: targetMaxPixelSize, quality: quality)
|
||||
if best.data.count <= maxBytes {
|
||||
return best
|
||||
}
|
||||
|
||||
for _ in 0..<6 {
|
||||
var q = self.clampQuality(quality)
|
||||
for _ in 0..<6 {
|
||||
let candidate = try encode(maxPixelSize: targetMaxPixelSize, quality: q)
|
||||
best = candidate
|
||||
if candidate.data.count <= maxBytes {
|
||||
return candidate
|
||||
}
|
||||
if q <= minQuality { break }
|
||||
q = max(minQuality, q * 0.75)
|
||||
}
|
||||
|
||||
let nextPixelSize = max(Int(Double(targetMaxPixelSize) * 0.85), minPixelSize)
|
||||
if nextPixelSize == targetMaxPixelSize {
|
||||
break
|
||||
}
|
||||
targetMaxPixelSize = nextPixelSize
|
||||
}
|
||||
|
||||
if best.data.count > maxBytes {
|
||||
throw JPEGTranscodeError.sizeLimitExceeded(maxBytes: maxBytes, actualBytes: best.data.count)
|
||||
}
|
||||
|
||||
return best
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,52 @@ import UniformTypeIdentifiers
|
||||
return out as Data
|
||||
}
|
||||
|
||||
private func makeNoiseJPEG(width: Int, height: Int) throws -> Data {
|
||||
let bytesPerPixel = 4
|
||||
let byteCount = width * height * bytesPerPixel
|
||||
var data = Data(count: byteCount)
|
||||
let cs = CGColorSpaceCreateDeviceRGB()
|
||||
let bitmapInfo = CGImageAlphaInfo.premultipliedLast.rawValue
|
||||
|
||||
let out = try data.withUnsafeMutableBytes { rawBuffer -> Data in
|
||||
guard let base = rawBuffer.baseAddress?.assumingMemoryBound(to: UInt8.self) else {
|
||||
throw NSError(domain: "JPEGTranscoderTests", code: 6)
|
||||
}
|
||||
for idx in 0..<byteCount {
|
||||
base[idx] = UInt8.random(in: 0...255)
|
||||
}
|
||||
|
||||
guard
|
||||
let ctx = CGContext(
|
||||
data: base,
|
||||
width: width,
|
||||
height: height,
|
||||
bitsPerComponent: 8,
|
||||
bytesPerRow: width * bytesPerPixel,
|
||||
space: cs,
|
||||
bitmapInfo: bitmapInfo)
|
||||
else {
|
||||
throw NSError(domain: "JPEGTranscoderTests", code: 7)
|
||||
}
|
||||
|
||||
guard let img = ctx.makeImage() else {
|
||||
throw NSError(domain: "JPEGTranscoderTests", code: 8)
|
||||
}
|
||||
|
||||
let encoded = NSMutableData()
|
||||
guard let dest = CGImageDestinationCreateWithData(encoded, UTType.jpeg.identifier as CFString, 1, nil) else {
|
||||
throw NSError(domain: "JPEGTranscoderTests", code: 9)
|
||||
}
|
||||
CGImageDestinationAddImage(dest, img, nil)
|
||||
guard CGImageDestinationFinalize(dest) else {
|
||||
throw NSError(domain: "JPEGTranscoderTests", code: 10)
|
||||
}
|
||||
return encoded as Data
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
@Test func downscalesToMaxWidthPx() throws {
|
||||
let input = try makeSolidJPEG(width: 2000, height: 1000)
|
||||
let out = try JPEGTranscoder.transcodeToJPEG(imageData: input, maxWidthPx: 1600, quality: 0.9)
|
||||
@@ -69,5 +115,14 @@ import UniformTypeIdentifiers
|
||||
#expect(out.widthPx == 1000)
|
||||
#expect(out.heightPx == 2000)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func respectsMaxBytes() throws {
|
||||
let input = try makeNoiseJPEG(width: 1600, height: 1200)
|
||||
let out = try JPEGTranscoder.transcodeToJPEG(
|
||||
imageData: input,
|
||||
maxWidthPx: 1600,
|
||||
quality: 0.95,
|
||||
maxBytes: 180_000)
|
||||
#expect(out.data.count <= 180_000)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user