Class: FormatParser::WebpParser

Inherits:
Object
  • Object
show all
Includes:
EXIFParser, IOUtils
Defined in:
lib/parsers/webp_parser.rb

Overview

WebP is an image format that provides superior lossless and lossy compression for images on the web, with support for transparency. It uses predictive coding to encode an image, predicting the values in a block of pixels based on the values of neighbouring blocks. A WebP file consists of VP8 or VP8L data, and a container based on RIFF. There is also an extended file format, VP8X, that optionally encodes various information such as the color profile, animation control data, transparency, and EXIF and/or XMP metadata.

For more information, visit developers.google.com/speed/webp.

TODO: Decide how to determine color mode (depends on variant, transformations, flags, etc.; maybe not worth it).

Constant Summary collapse

WEBP_MIME_TYPE =
'image/webp'

Constants included from IOUtils

IOUtils::INTEGER_DIRECTIVES

Constants included from EXIFParser

EXIFParser::ORIENTATIONS

Instance Method Summary collapse

Methods included from IOUtils

#read_bytes, #read_fixed_point, #read_int, #safe_read, #safe_skip, #skip_bytes

Methods included from EXIFParser

#exif_from_tiff_io

Instance Method Details

#call(io) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/parsers/webp_parser.rb', line 21

def call(io)
  @buf = FormatParser::IOConstraint.new(io)

  # All WebP files start with the following 20 bytes:
  #
  # Offset  | Description
  # -------------------------------------------------------------------------------------
  # 0...3   | "RIFF" (Since WebP is based on the RIFF file container format).
  # 4...7   | The size of the file in bytes - 8 bytes.
  # 8...11  | "WEBP" (To signify that this is a WebP file).
  # 12...15 | The VB8 variant in use ("VB8 ", "VP8L" or "VB8X")
  # 16...19 | The length of the VB8 data in bytes (i.e. The size of the file - 20 bytes).
  riff, webp, variant = safe_read(@buf, 20).unpack('A4x4A4A4')
  return unless riff == 'RIFF' && webp == 'WEBP'
  read_data(variant)
end

#likely_match?(filename) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/parsers/webp_parser.rb', line 17

def likely_match?(filename)
  filename =~ /\.webp$/i
end