Class: EmbedXMP::ImageFile
- Inherits:
-
Object
- Object
- EmbedXMP::ImageFile
- Defined in:
- lib/embed_xmp/image_file.rb
Overview
Basic image file representation.
Instance Method Summary collapse
-
#initialize(input_image) ⇒ ImageFile
constructor
A new instance of ImageFile.
-
#insert_into_file(offset, data) ⇒ Object
Insert a
chunkof data atfile_offsetfrom the start offile_data. -
#read_io_or_string(thing) ⇒ Object
Read from a readable
IOobject or aString(file path or a thing). -
#write(output_file, data: nil) ⇒ Object
Write image to file (or return if argument is
nil).
Constructor Details
#initialize(input_image) ⇒ ImageFile
Returns a new instance of ImageFile.
12 13 14 |
# File 'lib/embed_xmp/image_file.rb', line 12 def initialize(input_image) @image_data = read_io_or_string(input_image) end |
Instance Method Details
#insert_into_file(offset, data) ⇒ Object
Insert a chunk of data at file_offset from the start of file_data.
30 31 32 |
# File 'lib/embed_xmp/image_file.rb', line 30 def insert_into_file(offset, data) @image_data = @image_data[0..offset - 1] + data + @image_data[offset..-1] end |
#read_io_or_string(thing) ⇒ Object
Read from a readable IO object or a String (file path or a thing).
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/embed_xmp/image_file.rb', line 17 def read_io_or_string(thing) if thing.respond_to?(:encoding) return IO.binread(thing) if File.exist?(thing) return thing elsif thing.respond_to?(:read) return IO.binread(thing) end raise 'FileNotAnIOorString' end |
#write(output_file, data: nil) ⇒ Object
Write image to file (or return if argument is nil).
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/embed_xmp/image_file.rb', line 35 def write(output_file, data: nil) data = @image_data if data.nil? return data if output_file.nil? written_bytes = 0 File.open(output_file, 'wb') do |file| written_bytes = file.write(data) end written_bytes > 0 && written_bytes == data.b.length end |