Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seems like “libheif-1.8.0” is arbitrarily creating unopenable (corrupt?) HEIC files in macOS? #331

Closed
JackSzwergold opened this issue Sep 17, 2020 · 15 comments

Comments

@JackSzwergold
Copy link

JackSzwergold commented Sep 17, 2020

I discovered this issue while using ImageMagick to batch convert some images. Posting here because it seems that something in libheif-1.8.0 is causing this issue.

The longer post with some other details can be found in this ImageMagick issue I posted here when I believed this was simply an ImageMagick issue. The process I am using with ImageMagick runs a simple Bash script to traverse a directory full of JPEG images to convert them to HEIC like this:

convert source.jpeg destination.heic

This worked fine for converting hundreds of images back in July 2020 when I was using an earlier version of ImageMagick with libheif-1.7.0.

But now — after doing a Homebrew package update that updated ImageMagick as well as libheif-1.7.0 — it seems like a few out of every bunch of images I attempt to convert are unopenable and corrupt.

This happens to seemingly arbitrary images (not random images) but consistently. Meaning, if I give the script a pile of images to convert — such as one.jpg, two.jpg, three.jpg, etc… — and two.jpg is converted to two.heic which is corrupt, that image will always convert as a corrupt HEIC.

The same exact results — some images solid HEIC files and others unopenable/corrupt — can be seen if I use heif-enc to convert the images like this:

heif-enc source.jpeg -o destination.heic

Here are some test images I found (via the Wikimedia commons) that recreates the issue; 1280px-Test_(student_assessment).jpeg always results in a corrupted and inoperable HEIC image:

@farindk
Copy link
Contributor

farindk commented Sep 21, 2020

I've tested this on Linux with v1.8.0 on your images, but I cannot see any problem.
Could you please attach your encoded (corrupt) images?
Which x265 version do you use?

@JackSzwergold
Copy link
Author

JackSzwergold commented Sep 21, 2020

@farindk Thanks for the response! Here is the output of x265 --version which was installed by Homebrew on macOS Catalina 10.15.6 (19G2021):

x265 [info]: HEVC encoder version 0.0
x265 [info]: build info [Mac OS X][clang 11.0.3][64 bit] 8bit+10bit+12bit
x265 [info]: using cpu capabilities: MMX2 SSE2Fast LZCNT SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2

That version 0.0 seems weird. Checking the actual directory Homebrew installed it in shows 3.4 as the version:

/usr/local/Cellar/x265/3.4

And here is 1599px-Test_(student_assessment).heic.zip — compressed as a Zip so it could be attached here — which is an image file I cannot open that was created with this command:

heif-enc /Users/jack/Desktop/1599px-Test_\(student_assessment\).jpeg -o /Users/jack/Desktop/1599px-Test_\(student_assessment\).heic

@farindk
Copy link
Contributor

farindk commented Sep 21, 2020

Hm, I have no problems displaying your 1599px-Test_(student_assessment).heic image (with current libheif based viewers).
Maybe it's a bug in your viewer? What are you using?

I noticed that I get corrupted output when decoding this with an old version of libheif. Try, e.g., the online decoder, which is still an old version: https://strukturag.github.io/libheif/
Do you get the same kind of distortions? These are probably due to the odd image size (1599px) that was not supported in old versions. Your other image only has even sizes. I'm curious whether that decodes correctly on the online decoder at the link above.

@JackSzwergold
Copy link
Author

JackSzwergold commented Sep 21, 2020

@farindk Just checked with the online libheif decoder and the image is distorted and skewed; attached a screenshot below for reference.

As for the way I attempt to display them in macOS, I am just using the built in macOS “Preview” application. I converted hundreds of very high res scans this past July 2020 with ImageMagick (which uses libheif) and all of those images are still viewable on my side despite this.

1599px-Test_(student_assessment) heic as viewed via online HEIC decoder

@farindk
Copy link
Contributor

farindk commented Sep 21, 2020

Just checked. The image above even decodes correctly in libheif v1.0.0. Hence, it's obviously a bug in the online decoder that leads to the corrupted display. I guess it's because of the odd image width.

What does the picture in native macOS look like?

@JackSzwergold
Copy link
Author

JackSzwergold commented Sep 21, 2020

“What does the picture in native macOS look like?”

@farindk It doesn’t open at all so I don’t even see a distorted image.

When I convert the image it shows no preview image in the Finder and then if I attempt to double-click it to open it, I simply get the message:

The file “Test_(student_assessment).heic” could not be opened.
It may be damaged or use a file format that Preview doesn’t recognize.

If I try to open it with another application — such as Pixelmator — I get a similar message and the image doesn’t open.

Screenshots below of the finder with the thumbnail missing and the message I get when I attempt to open it.

Finder image files

Damaged image message

@farindk
Copy link
Contributor

farindk commented Sep 21, 2020

Can you convert it to HEIC using some macOS tools and send that to me? I'd like to see how macOS handles the odd image size in its own encoder.

@farindk
Copy link
Contributor

farindk commented Sep 21, 2020

Pixelmator probably uses the native macOS API and hence gives the same error.

@JackSzwergold
Copy link
Author

JackSzwergold commented Sep 21, 2020

@farindk Here is the Test_(student_assessment).jpeg (as a Zip file) converted manually via an export from the macOS “Preview” application. It opens fine on my side when handled this way: Test_(student_assessment).zip

But what is odd is if I use the online decoder, it shows the same skewed/distorted output while I can see it 100% clear and fine on my desktop.

@JackSzwergold
Copy link
Author

FWIW, I just decided to see what happens if I convert the unopenable HEIC to a JPEG with ImageMagick like this:

convert Test_\(student_assessment\).heic Test_\(student_assessment\).jpg

And magically that HEIC image cleanly converts to a JPEG without any issues. So perhaps there is something in the generated HEIC file that chokes on macOS built-in decoders/tools?

@farindk
Copy link
Contributor

farindk commented Sep 22, 2020

I analyzed the image written by macOS. The image contains 512x512 tiles arranged into a grid, with the grid size set to the output image size. Libheif, on the other hand, saves the image as a single image with an additional clap crop transform. It seems that macOS cannot read images with crop transforms.

Not sure whether we should do anything about this, as it is a bug in the macOS reader. As a workaround, we would have to write heif images embedded into a tile grid, just to avoid the macOS limitation. But this will lead to larger files.

@JackSzwergold
Copy link
Author

@farindk Excellent explanation. For my own purposes I will adjust my ImageMagick scripts to do cropping of 1 pixel from odd numbered widths/heights and then do an HEIC conversion. As for the larger macOS issue, might report this to Apple. Thanks again for the help in tracking this down!

farindk added a commit that referenced this issue Sep 23, 2020
@farindk
Copy link
Contributor

farindk commented Sep 23, 2020

I have added the option '-E'/'--even-size' to heif_enc which crops the image to even size if necessary. That might be useful in scripts converting to HEIF until there is a fix for macOS or until we have a workaround.

@JackSzwergold
Copy link
Author

@farindk Excellent! My solution — for now — is to have ImageMagick round up to the next even pixel size using a command like this. Worked great for my recent needs:

convert source.jpg -background white -extent  $(convert source.jpg -format '%[fx:2*int((w+1)/2)]x%[fx:2*int((h+1)/2)]!' info:) destination.heic

@JackSzwergold
Copy link
Author

JackSzwergold commented Sep 25, 2020

@farindk Just a followup but I updated my local Homebrew install of libheif to version 1.9.0 and your new option works great! When I ran this basic command; just like what I have done before:

heif-enc source.jpeg -o destination.heic

I get this helpful warning:

Warning: HEIF images with odd width or height cannot be displayed by many programs on macOS (tested up to Catalina 10.15.6). Until macOS is fixed or we have a workaround, it is advised to use option -E to make the image size an even number.

And the resulting image is just like before; valid but not openable in macOS.

But if I run the same exact command with the -E option like this:

heif-enc -E source.jpeg -o destination.heic

I get a 100% valid HEIC image that is open in macOS!

Thanks again for quickly addressing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants