Class: Identify::Image::JFIF

Inherits:
Identify::Image show all
Defined in:
lib/identify.rb

Overview

PNG

Constant Summary collapse

SOF =
[0xc0, 0xc1, 0xc2, 0xc3, 0xc5, 0xc6, 0xc7, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Identify::Image

#as_hash, formats, identify, inherited, parse

Class Method Details

.handle?(data) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/identify.rb', line 152

def self.handle? data
  data.index("JFIF", 4) == 6
end

Instance Method Details

#parse(data) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/identify.rb', line 156

def parse data
  index = 4
  block = data[index].ord * 256 + data[index + 1].ord

  {}.tap do |meta|
    while (index += block) < data.size
      break unless data[index].ord == 0xff
      if SOF.include?(data[index + 1].ord)
        meta.merge! as_hash('jpeg', *data.unpack("x#{index + 5}nn").reverse)
        break
      else
        index += 2
        block  = data[index].ord * 256 + data[index + 1].ord
      end
    end
  end
end