Repository: sdsykes/fastimage (ruby)

Issue:
Inverted width and height for HEIC portrait image (taken in upright position)
Hello,

I was testing the HEIC support on 2.2.4 and noticed the width and height are inverted for images taken on an iPhone XR (heic format; iOS 14.4.2). The phone was upright, in portrait orientation and not rotated to any side, so the width of the image in its correct orientation should really be the lesser value (3024) but instead FastImage returns 4032. The image in question is named `Sanex-Portrait-Upright.heic` and is available at the URL in the logs below. A second image was taken with the same phone rotated to the right (90 degrees) and has been included for comparison and test; this one has the correct width and height.

```
irb(main):008:0> FastImage.new('https://abe-pix.s3.amazonaws.com/Sanex-Portrait-Upright.heic').size
=> [4032, 3024]
irb(main):009:0> FastImage.new('https://abe-pix.s3.amazonaws.com/Jabra-Lenovo.heic').size
=> [4032, 3024]
```
I know HEIC format support has been added recently, but is this a known bug yet? Grateful to know of any workaround/fixes, if available yet.

I have included the `exiftool` outputs for both images. The Rotation of 270 on Sanex-Portrait-Upright.heic caught my eye.

Thank you.

<details>
<summary>exiftool output for Sanex-Portrait-Upright.heic</summary>
<pre>
ExifTool Version Number         : 11.88
File Name                       : Sanex-Portrait-Upright.heic
Directory                       : .
File Size                       : 1868 kB
File Modification Date/Time     : 2021:07:16 11:04:44+04:00
File Access Date/Time           : 2021:07:16 11:05:11+04:00
File Inode Change Date/Time     : 2021:07:16 11:05:10+04:00
File Permissions                : rw-rw-r--
File Type                       : HEIC
File Type Extension             : heic
MIME Type                       : image/heic
Major Brand                     : High Efficiency Image Format HEVC still image (.HEIC)
Minor Version                   : 0.0.0
Compatible Brands               : mif1, MiPr, miaf, MiHB, heic
Handler Type                    : Picture
Primary Item Reference          : 49
Exif Byte Order                 : Big-endian (Motorola, MM)
Make                            : Apple
Camera Model Name               : iPhone XR
Orientation                     : Rotate 90 CW
X Resolution                    : 72
Y Resolution                    : 72
Resolution Unit                 : inches
Software                        : 14.4.2
Modify Date                     : 2021:07:16 11:03:35
Host Computer                   : iPhone XR
Y Cb Cr Positioning             : Centered
Exposure Time                   : 1/30
F Number                        : 1.8
Exposure Program                : Program AE
ISO                             : 640
Exif Version                    : 0232
Date/Time Original              : 2021:07:16 11:03:35
Create Date                     : 2021:07:16 11:03:35
Offset Time                     : +04:00
Offset Time Original            : +04:00
Offset Time Digitized           : +04:00
Components Configuration        : Y, Cb, Cr, -
Shutter Speed Value             : 1/30
Aperture Value                  : 1.8
Brightness Value                : -0.6311776391
Exposure Compensation           : 0
Metering Mode                   : Multi-segment
Flash                           : Off, Did not fire
Focal Length                    : 4.2 mm
Subject Area                    : 2013 1511 2217 1330
Run Time Flags                  : Valid
Run Time Value                  : 32967269717166
Run Time Scale                  : 1000000000
Run Time Epoch                  : 0
Acceleration Vector             : -0.006256081162 -0.9754549861 -0.2277138979
Sub Sec Time Original           : 353
Sub Sec Time Digitized          : 353
Flashpix Version                : 0100
Color Space                     : Uncalibrated
Exif Image Width                : 4032
Exif Image Height               : 3024
Sensing Method                  : One-chip color area
Scene Type                      : Directly photographed
Exposure Mode                   : Auto
White Balance                   : Auto
Focal Length In 35mm Format     : 26 mm
Scene Capture Type              : Standard
Lens Info                       : 4.25mm f/1.8
Lens Make                       : Apple
Lens Model                      : iPhone XR back camera 4.25mm f/1.8
Composite Image                 : General Composite Image
GPS Latitude Ref                : South
GPS Longitude Ref               : East
GPS Altitude Ref                : Above Sea Level
GPS Speed Ref                   : km/h
GPS Speed                       : 0
GPS Img Direction Ref           : True North
GPS Img Direction               : 148.2856597
GPS Dest Bearing Ref            : True North
GPS Dest Bearing                : 148.2856597
GPS Date Stamp                  : 2021:07:16
GPS Horizontal Positioning Error: 56.0282416 m
Profile CMM Type                : Apple Computer Inc.
Profile Version                 : 4.0.0
Profile Class                   : Display Device Profile
Color Space Data                : RGB
Profile Connection Space        : XYZ
Profile Date Time               : 2017:07:07 13:22:32
Profile File Signature          : acsp
Primary Platform                : Apple Computer Inc.
CMM Flags                       : Not Embedded, Independent
Device Manufacturer             : Apple Computer Inc.
Device Model                    : 
Device Attributes               : Reflective, Glossy, Positive, Color
Rendering Intent                : Perceptual
Connection Space Illuminant     : 0.9642 1 0.82491
Profile Creator                 : Apple Computer Inc.
Profile ID                      : ca1a9582257f104d389913d5d1ea1582
Profile Description             : Display P3
Profile Copyright               : Copyright Apple Inc., 2017
Media White Point               : 0.95045 1 1.08905
Red Matrix Column               : 0.51512 0.2412 -0.00105
Green Matrix Column             : 0.29198 0.69225 0.04189
Blue Matrix Column              : 0.1571 0.06657 0.78407
Red Tone Reproduction Curve     : (Binary data 32 bytes, use -b option to extract)
Chromatic Adaptation            : 1.04788 0.02292 -0.0502 0.02959 0.99048 -0.01706 -0.00923 0.01508 0.75168
Blue Tone Reproduction Curve    : (Binary data 32 bytes, use -b option to extract)
Green Tone Reproduction Curve   : (Binary data 32 bytes, use -b option to extract)
HEVC Configuration Version      : 1
General Profile Space           : Conforming
General Tier Flag               : Main Tier
General Profile IDC             : Main Still Picture Profile
Gen Profile Compatibility Flags : Main Still Picture, Main 10, Main
Constraint Indicator Flags      : 176 0 0 0 0 0
General Level IDC               : 90 (level 3.0)
Min Spatial Segmentation IDC    : 0
Parallelism Type                : 0
Chroma Format                   : 4:2:0
Bit Depth Luma                  : 8
Bit Depth Chroma                : 8
Average Frame Rate              : 0
Constant Frame Rate             : Unknown
Num Temporal Layers             : 1
Temporal ID Nested              : No
Image Width                     : 4032
Image Height                    : 3024
Image Spatial Extent            : 4032x3024
Rotation                        : 270
Image Pixel Depth               : 8 8 8
Media Data Size                 : 1909033
Media Data Offset               : 3432
Run Time Since Power Up         : 9:09:27
Aperture                        : 1.8
Image Size                      : 4032x3024
Megapixels                      : 12.2
Scale Factor To 35 mm Equivalent: 6.1
Shutter Speed                   : 1/30
Create Date                     : 2021:07:16 11:03:35.353+04:00
Date/Time Original              : 2021:07:16 11:03:35.353+04:00
Modify Date                     : 2021:07:16 11:03:35+04:00
GPS Altitude                    : 38.5 m Above Sea Level
GPS Latitude                    : 20 deg 6' 27.84" S
GPS Longitude                   : 57 deg 41' 42.03" E
Circle Of Confusion             : 0.005 mm
Field Of View                   : 69.4 deg
Focal Length                    : 4.2 mm (35 mm equivalent: 26.0 mm)
GPS Position                    : 20 deg 6' 27.84" S, 57 deg 41' 42.03" E
Hyperfocal Distance             : 2.04 m
Light Value                     : 3.9
</pre>
</details>

<details>
<summary>exiftool output for Jabra-Lenovo.heic</summary>
<pre>
ExifTool Version Number         : 11.88
File Name                       : Jabra-Lenovo.heic
Directory                       : .
File Size                       : 1440 kB
File Modification Date/Time     : 2021:07:16 10:29:34+04:00
File Access Date/Time           : 2021:07:16 10:39:56+04:00
File Inode Change Date/Time     : 2021:07:16 10:39:46+04:00
File Permissions                : rw-rw-r--
File Type                       : HEIC
File Type Extension             : heic
MIME Type                       : image/heic
Major Brand                     : High Efficiency Image Format HEVC still image (.HEIC)
Minor Version                   : 0.0.0
Compatible Brands               : mif1, MiPr, miaf, MiHB, heic
Handler Type                    : Picture
Primary Item Reference          : 49
Exif Byte Order                 : Big-endian (Motorola, MM)
Make                            : Apple
Camera Model Name               : iPhone XR
Orientation                     : Rotate 180
X Resolution                    : 72
Y Resolution                    : 72
Resolution Unit                 : inches
Software                        : 14.4.2
Modify Date                     : 2021:07:16 10:28:44
Host Computer                   : iPhone XR
Y Cb Cr Positioning             : Centered
Exposure Time                   : 1/60
F Number                        : 1.8
Exposure Program                : Program AE
ISO                             : 250
Exif Version                    : 0232
Date/Time Original              : 2021:07:16 10:28:44
Create Date                     : 2021:07:16 10:28:44
Offset Time                     : +04:00
Offset Time Original            : +04:00
Offset Time Digitized           : +04:00
Components Configuration        : Y, Cb, Cr, -
Shutter Speed Value             : 1/60
Aperture Value                  : 1.8
Brightness Value                : 1.699890519
Exposure Compensation           : 0
Metering Mode                   : Multi-segment
Flash                           : Off, Did not fire
Focal Length                    : 4.2 mm
Subject Area                    : 2013 1511 2217 1330
Run Time Flags                  : Valid
Run Time Value                  : 32399352062000
Run Time Scale                  : 1000000000
Run Time Epoch                  : 0
Acceleration Vector             : 0.9033768773 -0.1174672618 -0.3998516504
Sub Sec Time Original           : 125
Sub Sec Time Digitized          : 125
Flashpix Version                : 0100
Color Space                     : Uncalibrated
Exif Image Width                : 4032
Exif Image Height               : 3024
Sensing Method                  : One-chip color area
Scene Type                      : Directly photographed
Exposure Mode                   : Auto
White Balance                   : Auto
Focal Length In 35mm Format     : 26 mm
Scene Capture Type              : Standard
Lens Info                       : 4.25mm f/1.8
Lens Make                       : Apple
Lens Model                      : iPhone XR back camera 4.25mm f/1.8
Composite Image                 : General Composite Image
GPS Latitude Ref                : South
GPS Longitude Ref               : East
GPS Altitude Ref                : Above Sea Level
GPS Speed Ref                   : km/h
GPS Speed                       : 0
GPS Img Direction Ref           : True North
GPS Img Direction               : 284.750084
GPS Dest Bearing Ref            : True North
GPS Dest Bearing                : 284.750084
GPS Date Stamp                  : 2021:07:16
GPS Horizontal Positioning Error: 12.44248371 m
Profile CMM Type                : Apple Computer Inc.
Profile Version                 : 4.0.0
Profile Class                   : Display Device Profile
Color Space Data                : RGB
Profile Connection Space        : XYZ
Profile Date Time               : 2017:07:07 13:22:32
Profile File Signature          : acsp
Primary Platform                : Apple Computer Inc.
CMM Flags                       : Not Embedded, Independent
Device Manufacturer             : Apple Computer Inc.
Device Model                    : 
Device Attributes               : Reflective, Glossy, Positive, Color
Rendering Intent                : Perceptual
Connection Space Illuminant     : 0.9642 1 0.82491
Profile Creator                 : Apple Computer Inc.
Profile ID                      : ca1a9582257f104d389913d5d1ea1582
Profile Description             : Display P3
Profile Copyright               : Copyright Apple Inc., 2017
Media White Point               : 0.95045 1 1.08905
Red Matrix Column               : 0.51512 0.2412 -0.00105
Green Matrix Column             : 0.29198 0.69225 0.04189
Blue Matrix Column              : 0.1571 0.06657 0.78407
Red Tone Reproduction Curve     : (Binary data 32 bytes, use -b option to extract)
Chromatic Adaptation            : 1.04788 0.02292 -0.0502 0.02959 0.99048 -0.01706 -0.00923 0.01508 0.75168
Blue Tone Reproduction Curve    : (Binary data 32 bytes, use -b option to extract)
Green Tone Reproduction Curve   : (Binary data 32 bytes, use -b option to extract)
HEVC Configuration Version      : 1
General Profile Space           : Conforming
General Tier Flag               : Main Tier
General Profile IDC             : Main Still Picture Profile
Gen Profile Compatibility Flags : Main Still Picture, Main 10, Main
Constraint Indicator Flags      : 176 0 0 0 0 0
General Level IDC               : 90 (level 3.0)
Min Spatial Segmentation IDC    : 0
Parallelism Type                : 0
Chroma Format                   : 4:2:0
Bit Depth Luma                  : 8
Bit Depth Chroma                : 8
Average Frame Rate              : 0
Constant Frame Rate             : Unknown
Num Temporal Layers             : 1
Temporal ID Nested              : No
Image Width                     : 4032
Image Height                    : 3024
Image Spatial Extent            : 4032x3024
Rotation                        : 180
Image Pixel Depth               : 8 8 8
Media Data Size                 : 1471586
Media Data Offset               : 3432
Run Time Since Power Up         : 8:59:59
Aperture                        : 1.8
Image Size                      : 4032x3024
Megapixels                      : 12.2
Scale Factor To 35 mm Equivalent: 6.1
Shutter Speed                   : 1/60
Create Date                     : 2021:07:16 10:28:44.125+04:00
Date/Time Original              : 2021:07:16 10:28:44.125+04:00
Modify Date                     : 2021:07:16 10:28:44+04:00
GPS Altitude                    : 38.8 m Above Sea Level
GPS Latitude                    : 20 deg 6' 27.93" S
GPS Longitude                   : 57 deg 41' 41.78" E
Circle Of Confusion             : 0.005 mm
Field Of View                   : 69.4 deg
Focal Length                    : 4.2 mm (35 mm equivalent: 26.0 mm)
GPS Position                    : 20 deg 6' 27.93" S, 57 deg 41' 41.78" E
Hyperfocal Distance             : 2.04 m
Light Value                     : 6.3
</pre>
</details>


Discussion:
@stefanoverna any ideas here? Looks to me like we would be open for a PR to fix this. 

@azharbeebeejaun can you confirm we compensate for rotation on jpgs? 
 
@SamSaffron yes indeed, jpgs work great.

Here are the output for pictures taken on the same phone but in .jpg format (changed the format setting to most compatible from high efficiency).
```
irb(main):001:0> FastImage.new('https://abe-pix.s3.amazonaws.com/XR-jpg-p.jpg').size
=> [3024, 4032]
irb(main):002:0> FastImage.new('https://abe-pix.s3.amazonaws.com/XR-jpg-l.jpg').size
=> [4032, 3024]
```

<details>
<summary>exiftool output for XR-jpg-p.jpg</summary>
<pre>
ExifTool Version Number         : 11.88
File Name                       : XR-jpg-p.jpg
Directory                       : .
File Size                       : 2013 kB
File Modification Date/Time     : 2021:07:19 09:12:37+04:00
File Access Date/Time           : 2021:07:19 09:13:03+04:00
File Inode Change Date/Time     : 2021:07:19 09:12:49+04:00
File Permissions                : rw-rw-r--
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
Exif Byte Order                 : Big-endian (Motorola, MM)
Make                            : Apple
Camera Model Name               : iPhone XR
Orientation                     : Rotate 90 CW
X Resolution                    : 72
Y Resolution                    : 72
Resolution Unit                 : inches
Software                        : 14.4.2
Modify Date                     : 2021:07:19 09:08:05
Host Computer                   : iPhone XR
Y Cb Cr Positioning             : Centered
Exposure Time                   : 1/871
F Number                        : 1.8
Exposure Program                : Program AE
ISO                             : 25
Exif Version                    : 0232
Date/Time Original              : 2021:07:19 09:08:05
Create Date                     : 2021:07:19 09:08:05
Offset Time                     : +04:00
Offset Time Original            : +04:00
Offset Time Digitized           : +04:00
Components Configuration        : Y, Cb, Cr, -
Shutter Speed Value             : 1/871
Aperture Value                  : 1.8
Brightness Value                : 8.452682127
Exposure Compensation           : 0
Metering Mode                   : Multi-segment
Flash                           : Off, Did not fire
Focal Length                    : 4.2 mm
Subject Area                    : 2013 1511 2217 1330
Run Time Flags                  : Valid
Run Time Value                  : 36778734825458
Run Time Scale                  : 1000000000
Run Time Epoch                  : 0
Acceleration Vector             : -0.002466715639 -0.8906965256 -0.454434067
Sub Sec Time Original           : 510
Sub Sec Time Digitized          : 510
Flashpix Version                : 0100
Color Space                     : Uncalibrated
Exif Image Width                : 4032
Exif Image Height               : 3024
Sensing Method                  : One-chip color area
Scene Type                      : Directly photographed
Exposure Mode                   : Auto
White Balance                   : Auto
Focal Length In 35mm Format     : 26 mm
Scene Capture Type              : Standard
Lens Info                       : 4.25mm f/1.8
Lens Make                       : Apple
Lens Model                      : iPhone XR back camera 4.25mm f/1.8
Composite Image                 : General Composite Image
GPS Latitude Ref                : South
GPS Longitude Ref               : East
GPS Altitude Ref                : Above Sea Level
GPS Speed Ref                   : km/h
GPS Speed                       : 0
GPS Img Direction Ref           : True North
GPS Img Direction               : 17.17580414
GPS Dest Bearing Ref            : True North
GPS Dest Bearing                : 17.17580414
GPS Date Stamp                  : 2021:07:19
GPS Horizontal Positioning Error: 20.85939082 m
Compression                     : JPEG (old-style)
Thumbnail Offset                : 2442
Thumbnail Length                : 7155
Profile CMM Type                : Apple Computer Inc.
Profile Version                 : 4.0.0
Profile Class                   : Display Device Profile
Color Space Data                : RGB
Profile Connection Space        : XYZ
Profile Date Time               : 2017:07:07 13:22:32
Profile File Signature          : acsp
Primary Platform                : Apple Computer Inc.
CMM Flags                       : Not Embedded, Independent
Device Manufacturer             : Apple Computer Inc.
Device Model                    : 
Device Attributes               : Reflective, Glossy, Positive, Color
Rendering Intent                : Perceptual
Connection Space Illuminant     : 0.9642 1 0.82491
Profile Creator                 : Apple Computer Inc.
Profile ID                      : ca1a9582257f104d389913d5d1ea1582
Profile Description             : Display P3
Profile Copyright               : Copyright Apple Inc., 2017
Media White Point               : 0.95045 1 1.08905
Red Matrix Column               : 0.51512 0.2412 -0.00105
Green Matrix Column             : 0.29198 0.69225 0.04189
Blue Matrix Column              : 0.1571 0.06657 0.78407
Red Tone Reproduction Curve     : (Binary data 32 bytes, use -b option to extract)
Chromatic Adaptation            : 1.04788 0.02292 -0.0502 0.02959 0.99048 -0.01706 -0.00923 0.01508 0.75168
Blue Tone Reproduction Curve    : (Binary data 32 bytes, use -b option to extract)
Green Tone Reproduction Curve   : (Binary data 32 bytes, use -b option to extract)
Image Width                     : 4032
Image Height                    : 3024
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:2:0 (2 2)
Run Time Since Power Up         : 10:12:59
Aperture                        : 1.8
Image Size                      : 4032x3024
Megapixels                      : 12.2
Scale Factor To 35 mm Equivalent: 6.1
Shutter Speed                   : 1/871
Create Date                     : 2021:07:19 09:08:05.510+04:00
Date/Time Original              : 2021:07:19 09:08:05.510+04:00
Modify Date                     : 2021:07:19 09:08:05+04:00
Thumbnail Image                 : (Binary data 7155 bytes, use -b option to extract)
GPS Altitude                    : 39.3 m Above Sea Level
GPS Latitude                    : 20 deg 6' 27.83" S
GPS Longitude                   : 57 deg 41' 42.04" E
Circle Of Confusion             : 0.005 mm
Field Of View                   : 69.4 deg
Focal Length                    : 4.2 mm (35 mm equivalent: 26.0 mm)
GPS Position                    : 20 deg 6' 27.83" S, 57 deg 41' 42.04" E
Hyperfocal Distance             : 2.04 m
Light Value                     : 13.5
</pre>
</details>

<details>
<summary>exiftool output for XR-jpg-l.jpg</summary>
<pre>
ExifTool Version Number         : 11.88
File Name                       : XR-jpg-l.jpg
Directory                       : .
File Size                       : 1973 kB
File Modification Date/Time     : 2021:07:19 09:13:11+04:00
File Access Date/Time           : 2021:07:19 09:13:49+04:00
File Inode Change Date/Time     : 2021:07:19 09:13:11+04:00
File Permissions                : rw-rw-r--
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
Exif Byte Order                 : Big-endian (Motorola, MM)
Make                            : Apple
Camera Model Name               : iPhone XR
Orientation                     : Rotate 180
X Resolution                    : 72
Y Resolution                    : 72
Resolution Unit                 : inches
Software                        : 14.4.2
Modify Date                     : 2021:07:19 09:07:56
Host Computer                   : iPhone XR
Y Cb Cr Positioning             : Centered
Exposure Time                   : 1/429
F Number                        : 1.8
Exposure Program                : Program AE
ISO                             : 25
Exif Version                    : 0232
Date/Time Original              : 2021:07:19 09:07:56
Create Date                     : 2021:07:19 09:07:56
Offset Time                     : +04:00
Offset Time Original            : +04:00
Offset Time Digitized           : +04:00
Components Configuration        : Y, Cb, Cr, -
Shutter Speed Value             : 1/429
Aperture Value                  : 1.8
Brightness Value                : 7.694290935
Exposure Compensation           : 0
Metering Mode                   : Multi-segment
Flash                           : Off, Did not fire
Focal Length                    : 4.2 mm
Subject Area                    : 2013 1511 2217 1330
Run Time Flags                  : Valid
Run Time Value                  : 36769393391125
Run Time Scale                  : 1000000000
Run Time Epoch                  : 0
Acceleration Vector             : 0.9488855599 -0.0628478825 -0.2945564392
Sub Sec Time Original           : 336
Sub Sec Time Digitized          : 336
Flashpix Version                : 0100
Color Space                     : Uncalibrated
Exif Image Width                : 4032
Exif Image Height               : 3024
Sensing Method                  : One-chip color area
Scene Type                      : Directly photographed
Exposure Mode                   : Auto
White Balance                   : Auto
Focal Length In 35mm Format     : 26 mm
Scene Capture Type              : Standard
Lens Info                       : 4.25mm f/1.8
Lens Make                       : Apple
Lens Model                      : iPhone XR back camera 4.25mm f/1.8
Composite Image                 : General Composite Image
GPS Latitude Ref                : South
GPS Longitude Ref               : East
GPS Altitude Ref                : Above Sea Level
GPS Speed Ref                   : km/h
GPS Speed                       : 0.9206481575
GPS Img Direction Ref           : True North
GPS Img Direction               : 342.0844037
GPS Dest Bearing Ref            : True North
GPS Dest Bearing                : 342.0844037
GPS Date Stamp                  : 2021:07:19
GPS Horizontal Positioning Error: 24.15145428 m
Compression                     : JPEG (old-style)
Thumbnail Offset                : 2442
Thumbnail Length                : 5711
Profile CMM Type                : Apple Computer Inc.
Profile Version                 : 4.0.0
Profile Class                   : Display Device Profile
Color Space Data                : RGB
Profile Connection Space        : XYZ
Profile Date Time               : 2017:07:07 13:22:32
Profile File Signature          : acsp
Primary Platform                : Apple Computer Inc.
CMM Flags                       : Not Embedded, Independent
Device Manufacturer             : Apple Computer Inc.
Device Model                    : 
Device Attributes               : Reflective, Glossy, Positive, Color
Rendering Intent                : Perceptual
Connection Space Illuminant     : 0.9642 1 0.82491
Profile Creator                 : Apple Computer Inc.
Profile ID                      : ca1a9582257f104d389913d5d1ea1582
Profile Description             : Display P3
Profile Copyright               : Copyright Apple Inc., 2017
Media White Point               : 0.95045 1 1.08905
Red Matrix Column               : 0.51512 0.2412 -0.00105
Green Matrix Column             : 0.29198 0.69225 0.04189
Blue Matrix Column              : 0.1571 0.06657 0.78407
Red Tone Reproduction Curve     : (Binary data 32 bytes, use -b option to extract)
Chromatic Adaptation            : 1.04788 0.02292 -0.0502 0.02959 0.99048 -0.01706 -0.00923 0.01508 0.75168
Blue Tone Reproduction Curve    : (Binary data 32 bytes, use -b option to extract)
Green Tone Reproduction Curve   : (Binary data 32 bytes, use -b option to extract)
Image Width                     : 4032
Image Height                    : 3024
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:2:0 (2 2)
Run Time Since Power Up         : 10:12:49
Aperture                        : 1.8
Image Size                      : 4032x3024
Megapixels                      : 12.2
Scale Factor To 35 mm Equivalent: 6.1
Shutter Speed                   : 1/429
Create Date                     : 2021:07:19 09:07:56.336+04:00
Date/Time Original              : 2021:07:19 09:07:56.336+04:00
Modify Date                     : 2021:07:19 09:07:56+04:00
Thumbnail Image                 : (Binary data 5711 bytes, use -b option to extract)
GPS Altitude                    : 39.5 m Above Sea Level
GPS Latitude                    : 20 deg 6' 27.58" S
GPS Longitude                   : 57 deg 41' 41.61" E
Circle Of Confusion             : 0.005 mm
Field Of View                   : 69.4 deg
Focal Length                    : 4.2 mm (35 mm equivalent: 26.0 mm)
GPS Position                    : 20 deg 6' 27.58" S, 57 deg 41' 41.61" E
Hyperfocal Distance             : 2.04 m
Light Value                     : 12.4
</pre>
</details>
I don't have much time at this moment, so PRs are super welcome :) I'll try to do my best in the next days!
@stefanoverna I tried get the exifs location, but stopped when I had to parse the iloc box...
https://github.com/sdsykes/fastimage/compare/master...ombr:heic-orientation?expand=1

I might get back to it later, but put it there meanwhile.

Patch:
diff --git a/lib/fastimage.rb b/lib/fastimage.rb
--- a/lib/fastimage.rb
+++ b/lib/fastimage.rb
@@ -589,6 +589,7 @@ def initialize(stream)
     end
 
     def width_and_height
+      @rotation = 0
       @max_size = nil
       @primary_box = nil
       @ipma_boxes = []
@@ -599,11 +600,19 @@ def width_and_height
         read_boxes!
       end
 
-      @final_size
+      if [90, 270].include?(@rotation)
+        @final_size.reverse
+      else
+        @final_size
+      end
     end
 
     private
 
+    # Format specs: https://www.loc.gov/preservation/digital/formats/fdd/fdd000525.shtml
+
+    # If you need to inspect a heic/heif file, use
+    # https://gpac.github.io/mp4box.js/test/filereader.html
     def read_boxes!(max_read_bytes = nil)
       end_pos = max_read_bytes.nil? ? nil : @stream.pos + max_read_bytes
       index = 0
@@ -624,6 +633,8 @@ def read_boxes!(max_read_bytes = nil)
           handle_hdlr_box(box_size)
         when "iprp", "ipco"
           read_boxes!(box_size)
+        when "irot"
+          handle_irot_box
         when "ispe"
           handle_ispe_box(box_size, index)
         when "mdat"
@@ -636,6 +647,10 @@ def read_boxes!(max_read_bytes = nil)
       end
     end
 
+    def handle_irot_box
+      @rotation = (read_uint8! & 0x3) * 90
+    end
+
     def handle_ispe_box(box_size, index)
       throw :finish if box_size < 12
 


Test patch:
diff --git a/test/fixtures/heic/inverted.heic b/test/fixtures/heic/inverted.heic
new file mode 100644
Binary files /dev/null and b/test/fixtures/heic/inverted.heic differ
diff --git a/test/test.rb b/test/test.rb
--- a/test/test.rb
+++ b/test/test.rb
@@ -48,6 +48,7 @@
   "heic/heic-maybebroken.HEIC"=>[:heic,[4032,3024]],
   "heic/heic-single.heic"=>[:heif,[1440,960]],
   "heic/heic-collection.heic"=>[:heif,[1440,960]],
+  "heic/inverted.heic"=>[:heic,[3024, 4032]],
   "test6.svg" => [:svg, [450, 450]]
 }