Class: FileData::Jpeg

Inherits:
Object
  • Object
show all
Defined in:
lib/file_data/file_types/jpeg.rb

Overview

Represents a Jpeg image stream

Constant Summary collapse

SOI_BYTES =
[255, 216].freeze
EOI_BYTES =
[255, 217].freeze
SECTION_HEADER_SIZE =
4
INVALID_HEADER_MSG =
'the given file is not a jpeg file since it does not'\
'begin with the start of image (SOI) bytes.'.freeze

Class Method Summary collapse

Class Method Details

.current_section(stream, marker) ⇒ Object

def self.section_pos?(stream)

# Make sure that there are enough bytes for a section header.
# This also handles an ending two byte JPEG EOI sequence.
stream.size >= SECTION_HEADER_SIZE

end



41
42
43
44
# File 'lib/file_data/file_types/jpeg.rb', line 41

def self.current_section(stream, marker)
  content_stream = Helpers::SizedField.create_view(stream, 2)
  JpegSection.new(marker, content_stream)
end

.each_section(stream) ⇒ Object



13
14
15
16
17
# File 'lib/file_data/file_types/jpeg.rb', line 13

def self.each_section(stream)
  view = Helpers::StreamView.new(stream)
  read_header(view)
  Enumerator.new { |e| yield_sections(view, e) }.lazy
end

.read_header(stream) ⇒ Object

Raises:



19
20
21
22
# File 'lib/file_data/file_types/jpeg.rb', line 19

def self.read_header(stream)
  soi = stream.each_byte.take(SOI_BYTES.size)
  raise INVALID_HEADER_MSG unless soi == SOI_BYTES
end

.yield_sections(stream, enumerator) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/file_data/file_types/jpeg.rb', line 24

def self.yield_sections(stream, enumerator)
  until stream.eof?
    marker = stream.each_byte.take(2)
    break if marker == EOI_BYTES

    section = current_section(stream, marker)
    enumerator.yield section
    stream.seek(section.content_stream.end_pos + 1)
  end
end