Module: WebP

Defined in:
lib/webp/c.rb,
lib/webp_ffi.rb,
lib/webp/libc.rb,
lib/webp/webp.rb,
lib/webp/error.rb,
lib/webp/options.rb,
lib/webp/version.rb

Defined Under Namespace

Modules: C, LibC Classes: DecoderError, EncoderError, InvalidImageFormatError, Options

Constant Summary collapse

ENCODER_ERRORS =
[
"Version mismatch", 
"Invalid configuration", 
"Cannot read input picture file", 
"Cannot open output file", 
"Cannot crop picture", 
"Cannot resize picture",
"Cannot encode picture as WebP"]
DECODER_ERRORS =
[
"Version mismatch", 
"Invalid webp image", 
"Invalid output format",
"Decoding failed"]
VERSION =
"0.3.1"

Class Method Summary collapse

Class Method Details

.decode(input_file, output_file, options = {}) ⇒ Object

decode



39
40
41
42
43
44
# File 'lib/webp/webp.rb', line 39

def decode(input_file, output_file, options = {})
  unless 0 == (decode_res = C.webp_decode(input_file, output_file, init_options_object(options).decode_pointer))
    raise DecoderError, DECODER_ERRORS[decode_res - 1]
  end
  true
end

.decoder_versionObject



7
8
9
10
11
# File 'lib/webp/webp.rb', line 7

def decoder_version
  pointer = FFI::MemoryPointer.new(:char, 10)
  C.decoder_version(pointer)
  pointer.null? ? nil : pointer.read_string()
end

.encode(input_file, output_file, options = {}) ⇒ Object

encode



31
32
33
34
35
36
# File 'lib/webp/webp.rb', line 31

def encode(input_file, output_file, options = {})
  unless 0 == (encode_res = C.webp_encode(input_file, output_file, init_options_object(options).encode_pointer))
    raise EncoderError, ENCODER_ERRORS[encode_res - 1]
  end
  true
end

.encoder_versionObject



13
14
15
16
17
# File 'lib/webp/webp.rb', line 13

def encoder_version
  pointer = FFI::MemoryPointer.new(:char, 10)
  C.encoder_version(pointer)
  pointer.null? ? nil : pointer.read_string()
end

.webp_size(data) ⇒ Object

get webp image size



20
21
22
23
24
25
26
27
28
# File 'lib/webp/webp.rb', line 20

def webp_size(data)
  return nil if data.nil?
  pointers = get_pointers_for_webp_size(data)
  if 0 == C.webp_get_info(pointers[:data], pointers[:data_size], pointers[:width], pointers[:height])
    [(pointers[:width].null? ? nil : pointers[:width].read_int), (pointers[:height].null? ? nil : pointers[:height].read_int)]
  else
    raise InvalidImageFormatError, "invalid WebP image data"
  end
end