mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-25 11:14:01 +00:00
Strip location data when any image in a file has it
Only the first image was checked for GPS before stripping, so a file whose later images carried a location, but not its first, was sent unchanged with that location. Every image is checked now. GPS stored only in XMP was already caught, because ImageIO reports it in the image's GPS properties.
This commit is contained in:
@@ -374,13 +374,15 @@ nonisolated struct MediaUploadingPreprocessor {
|
|||||||
throw .failedStrippingLocationData
|
throw .failedStrippingLocationData
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let originalMetadata = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil),
|
let count = CGImageSourceGetCount(imageSource)
|
||||||
(originalMetadata as NSDictionary).value(forKeyPath: "\(kCGImagePropertyGPSDictionary)") != nil else {
|
let hasLocation = (0..<count).contains { index in
|
||||||
|
(CGImageSourceCopyPropertiesAtIndex(imageSource, index, nil) as? [CFString: Any])?[kCGImagePropertyGPSDictionary] != nil
|
||||||
|
}
|
||||||
|
guard hasLocation else {
|
||||||
MXLog.info("No GPS metadata found. Nothing to do.")
|
MXLog.info("No GPS metadata found. Nothing to do.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let count = CGImageSourceGetCount(imageSource)
|
|
||||||
let metadataKeysToRemove: [CFString: Any] = [kCGImagePropertyGPSDictionary: kCFNull as Any]
|
let metadataKeysToRemove: [CFString: Any] = [kCGImagePropertyGPSDictionary: kCFNull as Any]
|
||||||
|
|
||||||
let data = NSMutableData()
|
let data = NSMutableData()
|
||||||
|
|||||||
@@ -376,6 +376,17 @@ final class MediaUploadingPreprocessorTests {
|
|||||||
#expect(CGImageSourceCopyAuxiliaryDataInfoAtIndex(imageSource, 2, kCGImageAuxiliaryDataTypeHDRGainMap) == nil)
|
#expect(CGImageSourceCopyAuxiliaryDataInfoAtIndex(imageSource, 2, kCGImageAuxiliaryDataTypeHDRGainMap) == nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
func multiImageProcessingStripsLocationFoundOnlyOnLaterImage() async throws {
|
||||||
|
let imageSource = try await processedImageSource(at: makeImageWithLocationOnSecondImage())
|
||||||
|
|
||||||
|
#expect(CGImageSourceGetCount(imageSource) == 2)
|
||||||
|
for index in 0..<CGImageSourceGetCount(imageSource) {
|
||||||
|
let metadata = try #require(CGImageSourceCopyPropertiesAtIndex(imageSource, index, nil) as NSDictionary?)
|
||||||
|
#expect(metadata.value(forKeyPath: "\(kCGImagePropertyGPSDictionary)") == nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
func spatialImageProcessingKeepsStereoPair() async throws {
|
func spatialImageProcessingKeepsStereoPair() async throws {
|
||||||
let imageSource = try await processedImageSource(at: makeSpatialImageWithLocation())
|
let imageSource = try await processedImageSource(at: makeSpatialImageWithLocation())
|
||||||
@@ -673,6 +684,29 @@ final class MediaUploadingPreprocessorTests {
|
|||||||
return url
|
return url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generates a two image HEIC with GPS metadata on its second image only in a unique temporary directory.
|
||||||
|
private func makeImageWithLocationOnSecondImage() throws -> URL {
|
||||||
|
let directory = FileManager.default.temporaryDirectory.appendingPathComponent("\(Self.self)-\(UUID().uuidString)")
|
||||||
|
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
||||||
|
let url = directory.appendingPathComponent("second_image_location_test_image.heic")
|
||||||
|
|
||||||
|
let extent = CGRect(x: 0, y: 0, width: 64, height: 64)
|
||||||
|
let image = try #require(CIContext().createCGImage(CIImage(color: .gray).cropped(to: extent), from: extent))
|
||||||
|
let destination = try #require(CGImageDestinationCreateWithURL(url as NSURL, UTType.heic.identifier as CFString, 2, nil))
|
||||||
|
CGImageDestinationAddImage(destination, image, nil)
|
||||||
|
let location = [kCGImagePropertyGPSDictionary: [kCGImagePropertyGPSLatitude: 51.5, kCGImagePropertyGPSLongitude: 0.1]]
|
||||||
|
CGImageDestinationAddImage(destination, image, location as NSDictionary)
|
||||||
|
try #require(CGImageDestinationFinalize(destination))
|
||||||
|
|
||||||
|
let imageSource = try #require(CGImageSourceCreateWithURL(url as NSURL, nil))
|
||||||
|
let firstMetadata = try #require(CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as NSDictionary?)
|
||||||
|
let secondMetadata = try #require(CGImageSourceCopyPropertiesAtIndex(imageSource, 1, nil) as NSDictionary?)
|
||||||
|
try #require(firstMetadata.value(forKeyPath: "\(kCGImagePropertyGPSDictionary)") == nil)
|
||||||
|
try #require(secondMetadata.value(forKeyPath: "\(kCGImagePropertyGPSDictionary)") != nil)
|
||||||
|
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
|
||||||
/// Generates a HEIC laid out like an iPhone spatial photo in a unique temporary directory: a monoscopic primary image, then the right and
|
/// Generates a HEIC laid out like an iPhone spatial photo in a unique temporary directory: a monoscopic primary image, then the right and
|
||||||
/// left eyes, all in one stereo pair group. Every image has GPS metadata and an HDR gain map of its own size, and each eye has its camera.
|
/// left eyes, all in one stereo pair group. Every image has GPS metadata and an HDR gain map of its own size, and each eye has its camera.
|
||||||
private func makeSpatialImageWithLocation() throws -> URL {
|
private func makeSpatialImageWithLocation() throws -> URL {
|
||||||
|
|||||||
Reference in New Issue
Block a user