Skip to content

Commit

Permalink
Fix heic and heif file format detection
Browse files Browse the repository at this point in the history
`.heic` and `.heif` file extensions was recognised as static images when format set to nil
  • Loading branch information
starkdmi committed Apr 6, 2024
1 parent d0d00e5 commit 55a1d82
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
17 changes: 16 additions & 1 deletion Sources/Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,24 @@ public struct ImageTool {

// Animation
var isAnimated: Bool = !settings.skipAnimation && totalFrames > 1

// Fix HEIC format based on animation presence
if !isHDR, isAnimated, format == .heic {
format = .heics
}
if !isAnimated, format == .heics {
format = .heic
}

// Fix HEIF animated images (only when format wasn't passed)
if !isHDR, isAnimated, settings.format == nil, format == .heif || format == .heif10 {
format = .heics
}

if isAnimated, format?.isAnimationSupported == false {
isAnimated = false
}

if isAnimated, framework == .ciImage {
// `CIImage` has no support for animated sequences
framework = ImageFramework.animatedFramework(
Expand Down Expand Up @@ -265,7 +280,7 @@ public struct ImageTool {
frameRate = updatedFrameRate
}

// Fix HEIC format based on animation presence
// Fix HEIC format based on animation presence (duplicate, properties may have changed)
if !isHDR, isAnimated, format == .heic {
format = .heics
}
Expand Down
18 changes: 14 additions & 4 deletions Sources/Types/Image/ImageFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,21 @@ public enum ImageFormat: String, CaseIterable {
}

/// Init `ImageFormat` using file extension
public init?(_ filenameExtension: String) {
public init?(_ fileExtension: String) {
// Extension `.heif` isn't associated with HEIF image internally
var filenameExtension = filenameExtension
var filenameExtension = fileExtension
if filenameExtension == "heif" {
filenameExtension = "heic"
}

if #available(macOS 11, iOS 14, tvOS 14, visionOS 1, *) {
if let type = UTType(filenameExtension: filenameExtension), let format = ImageFormat(type) {
self = format
if format == .heif, fileExtension == "heic" { // type == .heic
// Fix `.heic` file extension recognized as `.heif` format
self = .heic
} else {
self = format
}
} else {
return nil
}
Expand All @@ -140,7 +145,12 @@ public enum ImageFormat: String, CaseIterable {
#if os(visionOS)
// Warning: dublicate code for visionOS
if let type = UTType(filenameExtension: filenameExtension), let format = ImageFormat(type) {
self = format
if format == .heif, fileExtension == "heic" { // type == .heic
// Fix `.heic` file extension recognized as HEIF image
self = .heic
} else {
self = format
}
} else {
return nil
}
Expand Down

0 comments on commit 55a1d82

Please sign in to comment.