class Compass::Core::SassExtensions::Functions::ImageSize::JPEG

Attributes

bits[R]
height[R]
width[R]

Public Class Methods

new(file) click to toggle source
# File lib/compass/core/sass_extensions/functions/image_size.rb, line 76
def initialize(file)
  if file.kind_of? IO
    examine(file)
  else
    File.open(file, 'rb') { |io| examine(io) }
  end
end

Private Instance Methods

examine(io) click to toggle source
# File lib/compass/core/sass_extensions/functions/image_size.rb, line 85
def examine(io)
  class << io
    unless method_defined?(:readbyte)
      def readbyte
        getc
      end
    end
    def readint; (readbyte << 8) + readbyte; end
    def readframe; read(readint - 2); end
    def readsof; [readint, readbyte, readint, readint, readbyte]; end
    def next
      c = readbyte while c != 0xFF
      c = readbyte while c == 0xFF
      c
    end
  end

  raise 'malformed JPEG!' unless io.readbyte == 0xFF && io.readbyte == 0xD8 # SOI

  while marker = io.next
    case marker
      when 0xC0..0xC3, 0xC5..0xC7, 0xC9..0xCB, 0xCD..0xCF # SOF markers
        length, @bits, @height, @width, components = io.readsof
        raise 'malformed JPEG' unless length == 8 + components * 3
      when 0xD9, 0xDA then  break # EOI, SOS
      when 0xFE then @comment = io.readframe # COM
      when 0xE1 then io.readframe # APP1, contains EXIF tag
      else io.readframe # ignore frame
    end
  end
end