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
chunk
of data atfile_offset
from the start offile_data
. -
#read_io_or_string(thing) ⇒ Object
Read from a readable
IO
object 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.
11 12 13 |
# File 'lib/embed_xmp/image_file.rb', line 11 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
.
29 30 31 |
# File 'lib/embed_xmp/image_file.rb', line 29 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).
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/embed_xmp/image_file.rb', line 16 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
).
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/embed_xmp/image_file.rb', line 34 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 |