Sha256: 04ef273af4924983648033298ee9eebcd44c04e1fafe336bbabf5f38404a6cc3

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

class FormatParser::CR3Parser
  include FormatParser::IOUtils
  require_relative 'cr3_parser/decoder'

  CR3_MIME_TYPE = 'image/x-canon-cr3'
  MAGIC_BYTES = 'ftypcrx '

  def likely_match?(filename)
    filename =~ /\.cr3$/i
  end

  def call(io)
    @buf = FormatParser::IOConstraint.new(io)

    return unless matches_cr3_definition?

    atom_tree = Decoder.new.build_atom_tree(0xffffffff, @buf)
    moov_atom = atom_tree.find { |atom| atom.type == 'moov' }
    cmt1_atom = moov_atom&.find_first_descendent(['CMT1'])
    return unless cmt1_atom

    width = cmt1_atom.fields[:image_width]
    height = cmt1_atom.fields[:image_length]
    rotated = cmt1_atom.fields[:rotated]
    orientation = cmt1_atom.fields[:orientation_sym]
    FormatParser::Image.new(
      format: :cr3,
      content_type: CR3_MIME_TYPE,
      width_px: width,
      height_px: height,
      orientation: orientation,
      display_width_px: rotated ? height : width,
      display_height_px: rotated ? width : height,
      intrinsics: {
        atom_tree: atom_tree,
        exif: cmt1_atom.fields,
      },
    )
  end

  private

  def matches_cr3_definition?
    skip_bytes(4)
    matches = read_string(8) == MAGIC_BYTES
    @buf.seek(0)
    matches
  end

  FormatParser.register_parser new, natures: [:image], formats: [:cr3]
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
format_parser-2.3.0 lib/parsers/cr3_parser.rb
format_parser-2.2.1 lib/parsers/cr3_parser.rb
format_parser-2.2.0 lib/parsers/cr3_parser.rb