Skip Menu |

This queue is for tickets about the Imager CPAN distribution.

Report information
The Basics
Id: 128445
Status: resolved
Priority: 0/
Queue: Imager

People
Owner: Nobody in particular
Requestors: dcantrell [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Please add support for HEIC/HEIF images
Possibly by wrapping around libheif <https://github.com/strukturag/libheif>
CC: ;
Subject: Re: [rt.cpan.org #128445] Please add support for HEIC/HEIF images
Date: Thu, 7 Feb 2019 08:49:57 +1100
To: David Cantrell via RT <bug-Imager [...] rt.cpan.org>
From: Tony Cook <tony [...] develop-help.com>
On Wed, Feb 06, 2019 at 08:07:02AM -0500, David Cantrell via RT wrote: Show quoted text
> Wed Feb 06 08:07:01 2019: Request 128445 was acted upon. > Transaction: Ticket created by DCANTRELL > Queue: Imager > Subject: Please add support for HEIC/HEIF images > Broken in: (no value) > Severity: (no value) > Owner: Nobody > Requestors: dcantrell@cpan.org > Status: new > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=128445 > > > > Possibly by wrapping around libheif <https://github.com/strukturag/libheif>
Do you have any idea where I could get the file format specification cheaply? For C11 I have the final draft, but I haven't seen anything similar for HEIF (aka ISO/IEC DIS 23008-12). Since HEIF is a container format[1], libheif would work for .heic (purely H.265 encoding), but it doesn't appear to support other encodings. Tony [1] based on Wikipedia https://en.wikipedia.org/wiki/High_Efficiency_Image_File_Format
CC: ;
Subject: Re: [rt.cpan.org #128445] Please add support for HEIC/HEIF images
Date: Thu, 7 Feb 2019 10:03:22 +1100
To: David Cantrell via RT <bug-Imager [...] rt.cpan.org>
From: Tony Cook <tony [...] develop-help.com>
On Wed, Feb 06, 2019 at 05:22:40PM -0500, David Cantrell via RT wrote: Show quoted text
Thanks, that works.
CC: ;
Subject: Re: [rt.cpan.org #128445] Please add support for HEIC/HEIF images
Date: Sat, 9 Feb 2019 17:56:34 +1100
To: David Cantrell via RT <bug-Imager [...] rt.cpan.org>
From: Tony Cook <tony [...] develop-help.com>
On Wed, Feb 06, 2019 at 08:07:02AM -0500, David Cantrell via RT wrote: Show quoted text
> Wed Feb 06 08:07:01 2019: Request 128445 was acted upon. > Transaction: Ticket created by DCANTRELL > Queue: Imager > Subject: Please add support for HEIC/HEIF images > Broken in: (no value) > Severity: (no value) > Owner: Nobody > Requestors: dcantrell@cpan.org > Status: new > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=128445 > > > > Possibly by wrapping around libheif <https://github.com/strukturag/libheif>
Have a play with: https://github.com/tonycoz/imager-file-heif It requires that libheif, libde265 and libx265 all be installed, along with their development headers. I've only tested detection with pkg-config, so you may need to set PKG_CONFIG_PATH so it points at the directory containing the .pc files for those libraries. Tony
Show quoted text
Thanks! Using the libraries and headers from debian-testing, looks like it at least mostly works. I could read a HEIC file from an iPhone's camera, scale it, and save it back out as another format, which is the specific use-case we have at work. Reading the same original file, scaling it, and saving it back out as heic/heif resulted in something that Preview.app on a Mac couldn't read, but Imager::File::HEIF *could* read it and convert it to another format which Preview could read, so I'm inclined to think that that may be a limitation in Preview.app only being able to decode the particular subset of files generated by other Apple software. $ perl -Mblib -MImager -e '$foo=Imager->new(file=>"IMG_2781.HEIC",type=>"heif"); $thumb=$foo->scale(scalefactor=>0.3);$thumb->write(file=>"foo.tga")' gives the expected results $ perl -Mblib -MImager -e '$foo=Imager->new(file=>"IMG_2781.HEIC",type=>"heif"); $thumb=$foo->scale(scalefactor=>0.3);$thumb->write(file=>"foo.heif")' gives a file that Preview.app can't read, but feeding the output from that into ... $ perl -Mblib -MImager -e '$foo=Imager->new(file=>"foo.heif",type=>"heif"); $thumb=$foo->scale(scalefactor=>0.3);$thumb->write(file=>"foo.tga")' again gives the expected results, a scaled image that Preview can decode In case it helps figure out what Apple are doing (if, indeed, you care to work around their limitations!) I've attached the original input file that I used
Subject: IMG_2781.HEIC
Download IMG_2781.HEIC
image/heic 966.6k
IMG_2781.HEIC
CC: ;
Subject: Re: [rt.cpan.org #128445] Please add support for HEIC/HEIF images
Date: Tue, 12 Feb 2019 08:47:43 +1100
To: David Cantrell via RT <bug-Imager [...] rt.cpan.org>
From: Tony Cook <tony [...] develop-help.com>
On Mon, Feb 11, 2019 at 11:02:00AM -0500, David Cantrell via RT wrote: Show quoted text
> Queue: Imager > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=128445 > >
> > Have a play with: > > > > https://github.com/tonycoz/imager-file-heif
> > Thanks! > > Using the libraries and headers from debian-testing, looks like it at least mostly works. > > I could read a HEIC file from an iPhone's camera, scale it, and save it back out as another format, which is the specific use-case we have at work. > > Reading the same original file, scaling it, and saving it back out as heic/heif resulted in something that Preview.app on a Mac couldn't read, but Imager::File::HEIF *could* read it and convert it to another format which Preview could read, so I'm inclined to think that that may be a limitation in Preview.app only being able to decode the particular subset of files generated by other Apple software. > > $ perl -Mblib -MImager -e '$foo=Imager->new(file=>"IMG_2781.HEIC",type=>"heif"); $thumb=$foo->scale(scalefactor=>0.3);$thumb->write(file=>"foo.tga")' > > gives the expected results > > $ perl -Mblib -MImager -e '$foo=Imager->new(file=>"IMG_2781.HEIC",type=>"heif"); $thumb=$foo->scale(scalefactor=>0.3);$thumb->write(file=>"foo.heif")' > > gives a file that Preview.app can't read, but feeding the output from that into ... >
Preview.app could read the original and the output here, even on my older 10.13.6 OS X (the hardware is too old to upgrade.) Windows 10 wanted me to pay for a codec to open it in Windows 10 Photos. GIMP opened it fine, but I think that's using libheif. Show quoted text
> $ perl -Mblib -MImager -e '$foo=Imager->new(file=>"foo.heif",type=>"heif"); $thumb=$foo->scale(scalefactor=>0.3);$thumb->write(file=>"foo.tga")' > > again gives the expected results, a scaled image that Preview can decode > > In case it helps figure out what Apple are doing (if, indeed, you care to work around their limitations!) I've attached the original input file that I used
Can you send me the scaled foo.heif? I'm using libheif out of git, so maybe something has been fixed there. Tony
CC: ;
Subject: Re: [rt.cpan.org #128445] Please add support for HEIC/HEIF images
Date: Tue, 12 Feb 2019 09:35:28 +1100
To: "tony [...] develop-help.com via RT" <bug-Imager [...] rt.cpan.org>
From: Tony Cook <tony [...] develop-help.com>
On Mon, Feb 11, 2019 at 04:48:02PM -0500, tony@develop-help.com via RT wrote: Show quoted text
> Can you send me the scaled foo.heif? > > I'm using libheif out of git, so maybe something has been fixed there.
Also, can you try saving the scaled result as a PNG, using the heif-enc example bundled with libheif to produce a .heif file and try that in Preview? heif-enc -o foo.heif foo.png Tony
Show quoted text
> Can you send me the scaled foo.heif?
Attached
Subject: foo.heif
Download foo.heif
image/heif 493.5k
foo.heif
Show quoted text
> Also, can you try saving the scaled result as a PNG, using the > heif-enc example bundled with libheif to produce a .heif file and try > that in Preview? > > heif-enc -o foo.heif foo.png
Preview.app can read the results, and the file is considerably smaller than that produced by Imager (but maybe that's just down to different default compression settings). The especially interesting bit, though, is that the png is 1210x907 but heif-enc produces a 1210x906 file. I'm using libheif 1.3.2-1+b1
Subject: foo.png
Download foo.png
image/png 1.3m
foo.png
Subject: foo.png.heif
Download foo.png.heif
image/heif 153.1k
foo.png.heif
On Tue Feb 12 10:08:10 2019, DCANTRELL wrote: Show quoted text
> > Also, can you try saving the scaled result as a PNG, using the > > heif-enc example bundled with libheif to produce a .heif file and try > > that in Preview? > > > > heif-enc -o foo.heif foo.png
> > Preview.app can read the results, and the file is considerably smaller > than that produced by Imager (but maybe that's just down to different > default compression settings). > > The especially interesting bit, though, is that the png is 1210x907 > but heif-enc produces a 1210x906 file. > > I'm using libheif 1.3.2-1+b1
Please try Imager::File::HEIF from github https://github.com/tonycoz/imager-file-heif when tested with foo.png, Imager::File::HEIF and heif-enc now produce the exact same file (with the defaults.) Tony
Show quoted text
> Please try Imager::File::HEIF from github > > https://github.com/tonycoz/imager-file-heif > > when tested with foo.png, Imager::File::HEIF and heif-enc now produce > the exact same file (with the defaults.)
OK, so starting with my original input file, read it, scale it, and spit out foo.png: $ perl -Mblib -MImager -e '$foo=Imager->new(file => "IMG_2781.HEIC", type => "heif"); $thumb = $foo->scale(scalefactor => 0.3); $thumb->write(file => "foo.png")' use Imager to convert that to foo.heif: $ perl -Mblib -MImager -e '$foo=Imager->new(file => "foo.png",type=>"png");$foo->write(file=>"foo.heif",type=>"heif")' and use heif-enc to convert foo.png to foo.png.heif: $ heif-enc -o foo.png.heif foo.png I now have two *.heif files of the same size, visually indistinguishable, both readable by Preview.app, but with different MD5 sums: $ md5 *heif MD5 (foo.heif) = be3492ecaa260a73f63dd5b360ef3afc MD5 (foo.png.heif) = a3a1a9bd644b143c686cea8b495daac0 The difference is: $ diff <(hexdump -C foo.heif) <(hexdump -C foo.png.heif) 4,5c4,5 < 00000030 00 00 00 00 70 69 63 74 6f 23 eb 38 00 00 55 e8 |....picto#.8..U.| < 00000040 6f 23 ed 78 00 00 00 00 0e 70 69 74 6d 00 00 00 |o#.x.....pitm...| --- Show quoted text
> 00000030 00 00 00 00 70 69 63 74 00 00 00 00 00 00 00 00 |....pict........| > 00000040 00 00 00 00 00 00 00 00 0e 70 69 74 6d 00 00 00 |.........pitm...|
However, if I read the original, scale, and write back out as heif all in Imager without an intermediate file: $ perl -Mblib -MImager -e '$foo=Imager->new("IMG_2781.HEIC",type=>"heif");$thumb=$foo->scale(scalefactor=>0.3);$thumb->write(file=>"foo.heif")' The resulting file has yet another MD5 sum and can't be read by Preview.app.
On Tue Feb 19 11:43:58 2019, DCANTRELL wrote: Show quoted text
> > Please try Imager::File::HEIF from github > > > > https://github.com/tonycoz/imager-file-heif > > > > when tested with foo.png, Imager::File::HEIF and heif-enc now produce > > the exact same file (with the defaults.)
> > OK, so starting with my original input file, read it, scale it, and > spit out foo.png: > > $ perl -Mblib -MImager -e '$foo=Imager->new(file => "IMG_2781.HEIC", > type => "heif"); $thumb = $foo->scale(scalefactor => 0.3); $thumb-
> >write(file => "foo.png")'
> > use Imager to convert that to foo.heif: > > $ perl -Mblib -MImager -e '$foo=Imager->new(file => > "foo.png",type=>"png");$foo->write(file=>"foo.heif",type=>"heif")' > > and use heif-enc to convert foo.png to foo.png.heif: > > $ heif-enc -o foo.png.heif foo.png > > I now have two *.heif files of the same size, visually > indistinguishable, both readable by Preview.app, but with different > MD5 sums: > > $ md5 *heif > MD5 (foo.heif) = be3492ecaa260a73f63dd5b360ef3afc > MD5 (foo.png.heif) = a3a1a9bd644b143c686cea8b495daac0 > > The difference is: > > $ diff <(hexdump -C foo.heif) <(hexdump -C foo.png.heif) > 4,5c4,5 > < 00000030 00 00 00 00 70 69 63 74 6f 23 eb 38 00 00 55 e8 > |....picto#.8..U.| > < 00000040 6f 23 ed 78 00 00 00 00 0e 70 69 74 6d 00 00 00 > |o#.x.....pitm...| > ---
> > 00000030 00 00 00 00 70 69 63 74 00 00 00 00 00 00 00 00 > > |....pict........| > > 00000040 00 00 00 00 00 00 00 00 0e 70 69 74 6d 00 00 00 > > |.........pitm...|
> > However, if I read the original, scale, and write back out as heif all > in Imager without an intermediate file: > > $ perl -Mblib -MImager -e '$foo=Imager-
> >new("IMG_2781.HEIC",type=>"heif");$thumb=$foo- > >scale(scalefactor=>0.3);$thumb->write(file=>"foo.heif")'
> > The resulting file has yet another MD5 sum and can't be read by > Preview.app.
The difference there is in the reserved fields of the hdlr box: aligned(8) class HandlerBox extends FullBox(‘hdlr’, version = 0, 0) { unsigned int(32) pre_defined = 0; unsigned int(32) handler_type; const unsigned int(32)[3] reserved = 0; string name; } handler_type is the "pict" you see in the dump, the 12 bytes following should be NULs, but they aren't. Commit 0a03d6fc2b4e26c3fbb0b541f70f83e45a56bd5f added initializers to the reserved fields of the Box_hdlr class: commit 0a03d6fc2b4e26c3fbb0b541f70f83e45a56bd5f Author: seunghoon.baek <seunghoon.baek@linecorp.com> Date: Wed Oct 10 14:35:41 2018 +0900 Initialize a member of hdlf box. diff --git a/libheif/box.h b/libheif/box.h index 1b20784..91e43fd 100644 --- a/libheif/box.h +++ b/libheif/box.h @@ -246,7 +246,7 @@ namespace heif { private: uint32_t m_pre_defined = 0; uint32_t m_handler_type = fourcc("pict"); - uint32_t m_reserved[3]; + uint32_t m_reserved[3] = {0, }; std::string m_name; }; Zeroing these out in the failed file makes no difference. But, I ran a comparison of the heic-info -d of a file Preview likes and your sample that it doesn't, one of the differences was: @@ -41,7 +41,7 @@ | | version: 2 | | flags: 0 | | item_ID: 1 -| | item_protection_index: 32656 +| | item_protection_index: 0 | | item_type: hvc1 | | item_name: | | content_type: which is part of the infe box. Box_infe was modified in 6f5658898e46cf51be5a80fa60cdd4503037615d (v1.3.2-93-g6f56588): @@ -377,8 +378,8 @@ namespace heif { Error parse(BitstreamRange& range) override; private: - heif_item_id m_item_ID; - uint16_t m_item_protection_index; + heif_item_id m_item_ID = 0; + uint16_t m_item_protection_index = 0; std::string m_item_type; std::string m_item_name; to initialize that field to zero. If I zero *that* out as well in the failing .heic file, Preview manages to load the image. Unfortunately this isn't something I have control over from Imager::File::HEIF - the only fix is to use the newer library. Tony
On Fri Feb 22 04:58:50 2019, TONYC wrote: Show quoted text
> Unfortunately this isn't something I have control over from > Imager::File::HEIF - the only fix is to use the newer library.
Which should become easier soon: https://github.com/strukturag/libheif/issues/88#issuecomment-467474665 Tony
Using libheif 1.4.0 from Debian experimental, reading a HEIF file, scaling it, and writing it out again as HEIF, without going via an intermediary PNG or whatever, produces something that Preview can read. Huzzah!
On Thu Apr 04 06:02:42 2019, DCANTRELL wrote: Show quoted text
> Using libheif 1.4.0 from Debian experimental, reading a HEIF file, > scaling it, and writing it out again as HEIF, without going via an > intermediary PNG or whatever, produces something that Preview can > read. Huzzah!
Thanks, I've released Imager::File::HEIF 0.003 which requires libheif 1.4.0 or higher. Closing this ticket, if you encounter any other problems please open a new ticket. Tony