class Dimscan::JPEGScanner

Scans dimensions from a JPEG image

Constants

SOF0

Protected Instance Methods

extract_dimensions(bytes) click to toggle source
# File lib/dimscan/jpeg_scanner.rb, line 24
def extract_dimensions(bytes)
  # height is before width in jpeg header
  byte_string = bytes.map(&:chr).join
  byte_string.unpack('nn').reverse
end
scan(bytes) { |first.chr| ... } click to toggle source
# File lib/dimscan/jpeg_scanner.rb, line 10
def scan(bytes)
  # Dimensions are in bytes 4-8 after SOF0
  last_slice = nil
  bytes.each_cons(SOF0.size + 4) do |slice|
    yield slice.first.chr
    last_slice = slice
    if slice.first(SOF0.size) == SOF0
      return extract_dimensions(slice.last(4))
    end
  end
  yield last_slice[1..-1].map(&:chr).join
  nil
end