diff --git a/lib/fastimage.rb b/lib/fastimage.rb index e3bc0f6..0166d3a 100644 --- 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 diff --git a/test/fixtures/heic/inverted.heic b/test/fixtures/heic/inverted.heic new file mode 100644 index 0000000..8ec900b Binary files /dev/null and b/test/fixtures/heic/inverted.heic differ diff --git a/test/test.rb b/test/test.rb index ceeadb3..1638ae2 100644 --- 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]] }