Remove unused JPEG format support.

This commit is contained in:
Brent Simmons committed 2026-10-30 21:31:55 -07:00
1 parent 8239e7af20
commit 3b368771be
2 files changed
+12 -38

No files matched your search

+4 -27
View File
@@ -24,12 +24,6 @@ public typealias RSColor = UIColor
private let RSImageLogger = Logger(subsystem: Logger.nnwSubsystem, category: "RSImage")
public enum ScaledImageFormat: Sendable {
case png
/// Images with an alpha channel are encoded as PNG, since JPEG can’t store transparency.
case jpeg(compressionQuality: Double)
}
public extension RSImage {
/// Create a colored image from the source image using a specified color.
@@ -167,11 +161,11 @@ public extension RSImage {
return largest
}
/// Scales image data down to maxPixelSize and returns the result encoded in `format`.
/// Scales image data down to maxPixelSize and returns the result encoded as PNG.
/// If the image is a single frame already at or below maxPixelSize, returns
/// the original data unchanged. For multi-image sources (like ICO), extracts
/// the largest frame. Returns nil if the data can't be decoded as an image.
static func scaledImageData(_ data: Data, maxPixelSize: Int, format: ScaledImageFormat = .png) -> Data? {
static func scaledImageData(_ data: Data, maxPixelSize: Int) -> Data? {
guard let imageSource = CGImageSourceCreateWithData(data as CFData, nil) else {
return nil
}
@@ -210,12 +204,11 @@ public extension RSImage {
return nil
}
let (type, destinationProperties) = destinationTypeAndProperties(format, cgImage)
guard let mutableData = CFDataCreateMutable(nil, 0),
let destination = CGImageDestinationCreateWithData(mutableData, type.identifier as CFString, 1, nil) else {
let destination = CGImageDestinationCreateWithData(mutableData, UTType.png.identifier as CFString, 1, nil) else {
return nil
}
CGImageDestinationAddImage(destination, cgImage, destinationProperties as CFDictionary?)
CGImageDestinationAddImage(destination, cgImage, nil)
guard CGImageDestinationFinalize(destination) else {
return nil
}
@@ -229,19 +222,3 @@ public extension RSImage {
return mutableData as Data
}
}
private extension RSImage {
static func destinationTypeAndProperties(_ format: ScaledImageFormat, _ cgImage: CGImage) -> (UTType, [CFString: Any]?) {
switch format {
case .png:
return (.png, nil)
case .jpeg(let compressionQuality):
let isOpaque = [.none, .noneSkipFirst, .noneSkipLast].contains(cgImage.alphaInfo)
guard isOpaque else {
return (.png, nil)
}
return (.jpeg, [kCGImageDestinationLossyCompressionQuality: compressionQuality])
}
}
}
@@ -14,24 +14,21 @@ import UniformTypeIdentifiers
@Suite struct ScaledImageDataTests {
@Test func jpegFormatResizesOpaqueImageToJPEGAndTransparentImageToPNG() throws {
let opaqueData = try #require(pngData(width: 2000, height: 1000, alphaInfo: .noneSkipLast))
let transparentData = try #require(pngData(width: 2000, height: 1000, alphaInfo: .premultipliedLast))
@Test func largeImageIsResizedToPNG() throws {
let data = try #require(pngData(width: 2000, height: 1000))
let scaledOpaqueData = try #require(RSImage.scaledImageData(opaqueData, maxPixelSize: 512, format: .jpeg(compressionQuality: 0.8)))
let scaledTransparentData = try #require(RSImage.scaledImageData(transparentData, maxPixelSize: 512, format: .jpeg(compressionQuality: 0.8)))
let scaledData = try #require(RSImage.scaledImageData(data, maxPixelSize: 512))
#expect(imageType(scaledOpaqueData) == UTType.jpeg.identifier)
#expect(imageType(scaledTransparentData) == UTType.png.identifier)
#expect(RSImage.largestFramePixelSize(scaledOpaqueData)?.width == 512)
#expect(RSImage.largestFramePixelSize(scaledOpaqueData)?.height == 256)
#expect(imageType(scaledData) == UTType.png.identifier)
#expect(RSImage.largestFramePixelSize(scaledData)?.width == 512)
#expect(RSImage.largestFramePixelSize(scaledData)?.height == 256)
}
}
// MARK: - Helpers
private func pngData(width: Int, height: Int, alphaInfo: CGImageAlphaInfo) -> Data? {
guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: alphaInfo.rawValue) else {
private func pngData(width: Int, height: Int) -> Data? {
guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue) else {
return nil
}
context.setFillColor(red: 0.2, green: 0.4, blue: 0.6, alpha: 1)