Skip to content

Commit

Permalink
Merge pull request #131 from stefanoverna/fix-heic-rotation-angle
Browse files Browse the repository at this point in the history
Use irot box to get HEIC/HEIF rotation angle
  • Loading branch information
SamSaffron committed Aug 9, 2021
2 parents 4bdc514 + 6025bd8 commit f655910
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/fastimage.rb
Expand Up @@ -589,6 +589,7 @@ def initialize(stream)
end

def width_and_height
@rotation = 0
@max_size = nil
@primary_box = nil
@ipma_boxes = []
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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

Expand Down
Binary file added test/fixtures/heic/inverted.heic
Binary file not shown.
1 change: 1 addition & 0 deletions test/test.rb
Expand Up @@ -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]]
}

Expand Down

0 comments on commit f655910

Please sign in to comment.