Convert wide-gamut images to sRGB before uploading

This commit is contained in:
Shadowfacts 2022-11-11 21:02:38 -05:00
parent 0413f326a0
commit 5c479e3bf0
1 changed files with 16 additions and 7 deletions

View File

@ -69,13 +69,22 @@ enum CompositionAttachmentData {
}
let utType: UTType
if dataUTI == "public.heic" {
// neither Mastodon nor Pleroma handles HEIC well, so convert to JPEG
let image = CIImage(data: data)!
let needsColorSpaceConversion = image.colorSpace?.name != CGColorSpace.sRGB
// neither Mastodon nor Pleroma handles HEIC well, so convert to JPEG
// they also do a bad job converting wide color gamut images (they seem to just drop the profile, letting the wide-gamut values be reinterprete as sRGB)
// if that changes in the future, we'll need to pass the InstanceFeatures in here somehow and gate the conversion
if needsColorSpaceConversion || dataUTI == "public.heic" {
let context = CIContext()
let colorSpace = image.colorSpace ?? CGColorSpace(name: CGColorSpace.sRGB)!
data = context.jpegRepresentation(of: image, colorSpace: colorSpace, options: [:])!
let sRGB = CGColorSpace(name: CGColorSpace.sRGB)!
if dataUTI == "public.png" {
data = context.pngRepresentation(of: image, format: .ARGB8, colorSpace: sRGB)!
utType = .png
} else {
data = context.jpegRepresentation(of: image, colorSpace: sRGB)!
utType = .jpeg
}
} else {
utType = UTType(dataUTI)!
}