Disable attachment colorspace conversion on Mastodon v4

This commit is contained in:
Shadowfacts 2022-11-15 21:45:42 -05:00
parent ce7ce3ac92
commit a5ad8e43b1
4 changed files with 21 additions and 7 deletions

View File

@ -76,6 +76,14 @@ struct InstanceFeatures {
} }
} }
var needsWideColorGamutHack: Bool {
if case .mastodon(_, .some(let version)) = instanceType {
return version < Version(4, 0, 0)
} else {
return true
}
}
mutating func update(instance: Instance, nodeInfo: NodeInfo?) { mutating func update(instance: Instance, nodeInfo: NodeInfo?) {
let ver = instance.version.lowercased() let ver = instance.version.lowercased()
if ver.contains("glitch") { if ver.contains("glitch") {

View File

@ -87,7 +87,7 @@ class PostService: ObservableObject {
private func getData(for attachment: CompositionAttachment) async throws -> (Data, UTType) { private func getData(for attachment: CompositionAttachment) async throws -> (Data, UTType) {
return try await withCheckedThrowingContinuation { continuation in return try await withCheckedThrowingContinuation { continuation in
attachment.data.getData { result in attachment.data.getData(features: mastodonController.instanceFeatures) { result in
switch result { switch result {
case let .success(res): case let .success(res):
continuation.resume(returning: res) continuation.resume(returning: res)

View File

@ -51,7 +51,7 @@ enum CompositionAttachmentData {
} }
} }
func getData(completion: @escaping (Result<(Data, UTType), Error>) -> Void) { func getData(features: InstanceFeatures, skipAllConversion: Bool = false, completion: @escaping (Result<(Data, UTType), Error>) -> Void) {
switch self { switch self {
case let .image(image): case let .image(image):
// Export as JPEG instead of PNG, otherweise photos straight from the camera are too large // Export as JPEG instead of PNG, otherweise photos straight from the camera are too large
@ -71,21 +71,26 @@ enum CompositionAttachmentData {
return return
} }
guard !skipAllConversion else {
completion(.success((data, UTType(dataUTI)!)))
return
}
let utType: UTType let utType: UTType
let image = CIImage(data: data)! let image = CIImage(data: data)!
let needsColorSpaceConversion = image.colorSpace?.name != CGColorSpace.sRGB let needsColorSpaceConversion = features.needsWideColorGamutHack && image.colorSpace?.name != CGColorSpace.sRGB
// neither Mastodon nor Pleroma handles HEIC well, so convert to JPEG // 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) // 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 that changes in the future, we'll need to pass the InstanceFeatures in here somehow and gate the conversion
if needsColorSpaceConversion || dataUTI == "public.heic" { if needsColorSpaceConversion || dataUTI == "public.heic" {
let context = CIContext() let context = CIContext()
let sRGB = CGColorSpace(name: CGColorSpace.sRGB)! let colorSpace = needsColorSpaceConversion || image.colorSpace != nil ? CGColorSpace(name: CGColorSpace.sRGB)! : image.colorSpace!
if dataUTI == "public.png" { if dataUTI == "public.png" {
data = context.pngRepresentation(of: image, format: .ARGB8, colorSpace: sRGB)! data = context.pngRepresentation(of: image, format: .ARGB8, colorSpace: colorSpace)!
utType = .png utType = .png
} else { } else {
data = context.jpegRepresentation(of: image, colorSpace: sRGB)! data = context.jpegRepresentation(of: image, colorSpace: colorSpace)!
utType = .jpeg utType = .jpeg
} }
} else { } else {

View File

@ -15,6 +15,7 @@ struct ComposeAttachmentRow: View {
@ObservedObject var draft: Draft @ObservedObject var draft: Draft
@ObservedObject var attachment: CompositionAttachment @ObservedObject var attachment: CompositionAttachment
@EnvironmentObject var mastodonController: MastodonController
@EnvironmentObject var uiState: ComposeUIState @EnvironmentObject var uiState: ComposeUIState
@State private var mode: Mode = .allowEntry @State private var mode: Mode = .allowEntry
@State private var isShowingTextRecognitionFailedAlert = false @State private var isShowingTextRecognitionFailedAlert = false
@ -90,7 +91,7 @@ struct ComposeAttachmentRow: View {
mode = .recognizingText mode = .recognizingText
DispatchQueue.global(qos: .userInitiated).async { DispatchQueue.global(qos: .userInitiated).async {
self.attachment.data.getData { (result) in self.attachment.data.getData(features: mastodonController.instanceFeatures, skipAllConversion: true) { (result) in
let data: Data let data: Data
do { do {
try data = result.get().0 try data = result.get().0