Class: Gifenc::Extension::Application

Inherits:
Gifenc::Extension show all
Defined in:
lib/extensions.rb

Overview

This generic extension was added to the GIF specification to allow software developers to inject their own features into the format. Only one became truly standard, the Netscape extension, which introduced GIF looping. An Application Extension is a container in itself, and the specific contents are to be defined in the data method, which must be implemented by any subclass. The structure of this extension block, as per the GIF specification, is the following:

  • Application Identifier (8 bytes): Sequence of ASCII characters used to identify the application owning this extension.
  • Application Authentication Code (3 bytes): Used to authenticate the Application Identifier. An Application program may use an algorithm to compute a binary code that uniquely identifies it as the application owning the Application Extension.
  • Application Data: The actual contents of the extension block, specific to each type of application extension.

Direct Known Subclasses

Netscape

Constant Summary collapse

LABEL =

Label identifying an Application Extension block.

"\xFF".freeze

Constants inherited from Gifenc::Extension

EXTENSION_INTRODUCER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Gifenc::Extension

#encode

Constructor Details

#initialize(id, code) ⇒ Application

Create a new generic Application Extension block.

Parameters:

  • id (String)

    The Application Identifier. Should be an 8 character ASCII string (note that it will be converted to ASCII and truncated and padded to 8 characters during encoding).

  • code (String)

    The Application Authentication Code. Should be a 3 character binary string (note that it will be truncated and padded to 3 characters during encoding).



217
218
219
220
221
# File 'lib/extensions.rb', line 217

def initialize(id, code)
  super(LABEL)
  @id   = id   # Application Identifier
  @code = code # Application Authentication Code
end

Instance Attribute Details

#codeString

Application authentication code. Must be an arbitrary 3-byte binary string. It was originally intended as a way for applications to validate the extension, but is in practice just a suffix of the identifier.

Returns:

  • (String)

    The authentication string.



206
207
208
# File 'lib/extensions.rb', line 206

def code
  @code
end

#idString

Application identifier. Must be an 8 character ASCII string.

Returns:

  • (String)

    The identifier string.



200
201
202
# File 'lib/extensions.rb', line 200

def id
  @id
end

Instance Method Details

#bodyString

Encode the extension block as a binary string (id + code + data), as it will appear in the actual GIF file.

Returns:

  • (String)

    The encoded extension block.



226
227
228
229
230
231
232
233
# File 'lib/extensions.rb', line 226

def body
  # Sanitize fields
  id   = @id.force_encoding('US-ASCII').scrub[0...8].ljust(8, "\x00")
  code = @code[0...3].ljust(3, "\x00")

  # Build string
  "\x0B" + id + code + Util.blockify(data)
end