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?) {
let ver = instance.version.lowercased()
if ver.contains("glitch") {

View File

@ -87,7 +87,7 @@ class PostService: ObservableObject {
private func getData(for attachment: CompositionAttachment) async throws -> (Data, UTType) {
return try await withCheckedThrowingContinuation { continuation in
attachment.data.getData { result in
attachment.data.getData(features: mastodonController.instanceFeatures) { result in
switch result {
case let .success(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 {
case let .image(image):
// Export as JPEG instead of PNG, otherweise photos straight from the camera are too large
@ -71,21 +71,26 @@ enum CompositionAttachmentData {
return
}
guard !skipAllConversion else {
completion(.success((data, UTType(dataUTI)!)))
return
}
let utType: UTType
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
// 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 sRGB = CGColorSpace(name: CGColorSpace.sRGB)!
let colorSpace = needsColorSpaceConversion || image.colorSpace != nil ? CGColorSpace(name: CGColorSpace.sRGB)! : image.colorSpace!
if dataUTI == "public.png" {
data = context.pngRepresentation(of: image, format: .ARGB8, colorSpace: sRGB)!
data = context.pngRepresentation(of: image, format: .ARGB8, colorSpace: colorSpace)!
utType = .png
} else {
data = context.jpegRepresentation(of: image, colorSpace: sRGB)!
data = context.jpegRepresentation(of: image, colorSpace: colorSpace)!
utType = .jpeg
}
} else {

View File

@ -15,6 +15,7 @@ struct ComposeAttachmentRow: View {
@ObservedObject var draft: Draft
@ObservedObject var attachment: CompositionAttachment
@EnvironmentObject var mastodonController: MastodonController
@EnvironmentObject var uiState: ComposeUIState
@State private var mode: Mode = .allowEntry
@State private var isShowingTextRecognitionFailedAlert = false
@ -90,7 +91,7 @@ struct ComposeAttachmentRow: View {
mode = .recognizingText
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
do {
try data = result.get().0