Sha256: b79a8b0bf02e1d187488f72bc4534623514bce3596340b6cf738c22fcafb9b9f

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

require "webp_ffi/c"

module WebpFfi
  class << self
    
    def decoder_version
      pointer = FFI::MemoryPointer.new(:char, 10)
      C.decoder_version(pointer)
      pointer.null? ? nil : pointer.read_string()
    end
    
    def encoder_version
      pointer = FFI::MemoryPointer.new(:char, 10)
      C.encoder_version(pointer)
      pointer.null? ? nil : pointer.read_string()
    end
    
    # get webp image size
    def webp_size(data)
      return nil if data.nil?
      size = data.respond_to?(:bytesize) ? data.bytesize : data.size
      width_ptr = FFI::MemoryPointer.new(:int, 2)
      height_ptr = FFI::MemoryPointer.new(:int, 2)
      memBuf = FFI::MemoryPointer.new(:char, size)
      memBuf.put_bytes(0, data)
      if C.webp_get_info(memBuf, size, width_ptr, height_ptr) == 0
        [width_ptr.null? ? nil : width_ptr.read_int, height_ptr.null? ? nil : height_ptr.read_int]
      else
        raise InvalidImageFormatError, "invalid webp image"
        return nil
      end
    end
    
    # decode
    def decode(input_file, output_file, options = {})
      C.webp_decode(input_file, output_file)
    end
    
    # encode
    def encode(input_file, output_file, options = {})
      C.webp_encode(input_file, output_file)
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
webp-ffi-0.1.0 lib/webp_ffi/webp_ffi.rb