class EmbedXMP::RIFF

Resource Interchange File Format (container format for WebP)

Constants

RIFF_HEAD

Public Instance Methods

chunk(offset) click to toggle source

rubocop: disable Metrics/AbcSize Return chunk at offset from the beginning of the file.

# File lib/embed_xmp/riff.rb, line 32
def chunk(offset)
  raise 'ChunksMustBeTwoBytesAligned' if offset.odd?

  chunk_id = @image_data[offset, 4]

  data_length = @image_data[offset + 4, 4].b.unpack1('V')
  chunk_length = data_length + (data_length % 2) + 8

  raise 'ChunkExceedsFileLength' if offset + chunk_length > @image_data.length

  data = @image_data[offset + 8, data_length]

  [chunk_id, chunk_length, data]
end
file_header() click to toggle source

Return the RIFF file header.

# File lib/embed_xmp/riff.rb, line 14
def file_header
  riff_id, file_length, data = chunk(0)
  form_type = data[0, 4]
  real_file_length = @image_data.length

  raise 'NoRIFFHeader' if RIFF_HEAD != riff_id
  raise 'FileHeaderLongerThanFile' if real_file_length != file_length

  [riff_id, file_length, form_type]
end
new_chunk(chunk_id, data) click to toggle source

Create a new RIFF chunk with data with pad byte when needed.

# File lib/embed_xmp/riff.rb, line 70
def new_chunk(chunk_id, data)
  unless chunk_id.match?(/[a-zA-Z0-9 ]{4}/)
    raise 'RIFFChunkIdentifierMustBeFourChar'
  end

  data_length = data.length
  data += '\0'.b if data_length.odd?

  chunk_id.b + [data_length].pack('V') + data
end
remove_chunk(offset) click to toggle source

Remove the chunk at offset from the beginning of the file.

# File lib/embed_xmp/riff.rb, line 49
def remove_chunk(offset)
  _, chunk_length, = chunk(offset)

  @image_data.slice!(offset, chunk_length)

  update_file_length_header
end
replace_chunk(offset, chunk_id, data) click to toggle source

Replace the chunk at offset from the beginning of the file with a new chunk

# File lib/embed_xmp/riff.rb, line 59
def replace_chunk(offset, chunk_id, data)
  remove_chunk(offset)

  chunk = new_chunk(chunk_id, data)

  insert_into_file(offset, chunk)

  update_file_length_header
end
update_file_length_header() click to toggle source

Updates the file length value in the WebP file header.

# File lib/embed_xmp/riff.rb, line 26
def update_file_length_header
  @image_data[4, 4] = [@image_data.length - 8].pack('V')
end