Skip to content

Commit

Permalink
fix(heif): Fixes to recent orientation support (#4184)
Browse files Browse the repository at this point in the history
We inadvertently started using libheif functions that only became
available in their version 1.16.0. Guard against that and now we
recommend at least that version (but we aren't yet changing the minimum
requirement). This fixes a build break introduced in #4142 (but only
breaks for older libheif).

Signed-off-by: Larry Gritz <[email protected]>
  • Loading branch information
lgritz authored Mar 16, 2024
1 parent f2ad2c7 commit e41ac03
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
3 changes: 2 additions & 1 deletion INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ NEW or CHANGED MINIMUM dependencies since the last major release are **bold**.
* giflib >= 4.1 (tested through 5.2; 5.0+ is strongly recommended for
stability and thread safety)
* If you want support for HEIF/HEIC or AVIF images:
* libheif >= 1.3 (1.7 required for AVIF support, tested through 1.17.6)
* libheif >= 1.3 (1.7 required for AVIF support, 1.16 required for
correct orientation support, tested through 1.17.6)
* libheif must be built with an AV1 encoder/decoder for AVIF support.
* Avoid libheif 1.10 on Mac, it is very broken. Libheif 1.11+ is fine.
* If you want support for DICOM medical image files:
Expand Down
4 changes: 2 additions & 2 deletions src/cmake/externalpackages.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ checked_find_package (GIF

# For HEIF/HEIC/AVIF formats
checked_find_package (Libheif VERSION_MIN 1.3
RECOMMEND_MIN 1.7
RECOMMEND_MIN_REASON "for AVIF support")
RECOMMEND_MIN 1.16
RECOMMEND_MIN_REASON "for orientation support")
if (APPLE AND LIBHEIF_VERSION VERSION_GREATER_EQUAL 1.10 AND LIBHEIF_VERSION VERSION_LESS 1.11)
message (WARNING "Libheif 1.10 on Apple is known to be broken, disabling libheif support")
set (Libheif_FOUND 0)
Expand Down
7 changes: 6 additions & 1 deletion src/heif.imageio/heifinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ HeifInput::seek_subimage(int subimage, int miplevel)
}
}

#if LIBHEIF_NUMERIC_VERSION >= MAKE_LIBHEIF_VERSION(1, 16, 0, 0)
// Try to discover the orientation. The Exif is unreliable. We have to go
// through the transformation properties ourselves. A tricky bit is that
// the C++ API doesn't give us a direct way to get the context ptr, we
Expand All @@ -321,7 +322,6 @@ HeifInput::seek_subimage(int subimage, int miplevel)
= reinterpret_cast<std::shared_ptr<heif_context>*>(m_ctx.get())->get();
int xpcount = heif_item_get_transformation_properties(raw_ctx, id, nullptr,
100);
orientation = 1;
xpcount = std::min(xpcount, 100); // clamp to some reasonable limit
std::vector<heif_property_id> xprops(xpcount);
heif_item_get_transformation_properties(raw_ctx, id, xprops.data(),
Expand All @@ -348,6 +348,11 @@ HeifInput::seek_subimage(int subimage, int miplevel)
}
}
}
#else
// Prior to libheif 1.16, the get_transformation_properties API was not
// available, so we have to rely on the Exif orientation tag.
int orientation = m_spec.get_int_attribute("Orientation", 1);
#endif

// Erase the orientation metadata because libheif appears to be doing
// the rotation-to-canonical-direction for us.
Expand Down

0 comments on commit e41ac03

Please sign in to comment.