Class: EmbedXMP::JFIF

Inherits:
ImageFile show all
Defined in:
lib/embed_xmp/jfif.rb

Overview

JPEG File Interchange Format (container format for JPEG)

Direct Known Subclasses

JPEG

Constant Summary collapse

JFIF_SOI =
"\xFF\xD8".b
JFIF_END =
"\xFF\xD9".b

Instance Method Summary collapse

Methods inherited from ImageFile

#initialize, #insert_into_file, #read_io_or_string, #write

Constructor Details

This class inherits a constructor from EmbedXMP::ImageFile

Instance Method Details

#check_file_markersObject

Check if file has JFIF markers.



15
16
17
18
# File 'lib/embed_xmp/jfif.rb', line 15

def check_file_markers
  raise 'NoJPEGStartOfFile' if JFIF_SOI != @image_data[0..1]
  raise 'NoJPEGEndOfFile' if JFIF_END != @image_data[-2..-1]
end

#new_segment(marker, data) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/embed_xmp/jfif.rb', line 39

def new_segment(marker, data)
  raise 'SegmentMarkerNotTwoBytes' if marker.length != 2
  raise 'SegmentMarkerDoesNotBeginWithNullByte' if marker == '\b'.b

  length = [2 + data.length].pack('n')

  marker + length + data
end

#remove_segment(offset) ⇒ Object

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



33
34
35
36
37
# File 'lib/embed_xmp/jfif.rb', line 33

def remove_segment(offset)
  _, length, = segment(offset)

  @image_data.slice!(offset, length)
end

#segment(offset) ⇒ Object

Return segment at offset from the beginning of the file.



21
22
23
24
25
26
27
28
29
30
# File 'lib/embed_xmp/jfif.rb', line 21

def segment(offset)
  marker = @image_data[offset, 2]
  length = @image_data[offset + 2, 2].b.unpack1('n') + 2

  raise 'SegmentExceedFileLength' if offset + length > @image_data.length

  data = @image_data[offset + 2, length]

  [marker, length, data]
end