jnordberg / gif.js

JavaScript GIF encoding library
http://jnordberg.github.io/gif.js/
MIT License
4.76k stars 668 forks source link

Comment Extension Support #38

Open thebyrd opened 9 years ago

thebyrd commented 9 years ago

Awesome project!

I'm trying to figure out how to encode/decode the comment extension using this library? Is it supported? If not, what are the first steps in adding support for it, because I'd be happy to do so.

mootari commented 5 years ago

I could not find any info on wether comment blocks are actually read by applications. Wikipedia states that many applications use custom application extension blocks to store metadata.

Adhering to the XMP standard would probably be the best option:

The Extensible Metadata Platform (XMP) metadata standard introduced an unofficial but now widespread "XMP Data" application extension block for including XMP data in GIF files. Since the XMP data is encoded using UTF-8 without NUL characters, there are no 0 bytes in the data. Rather than break the data into formal sub-blocks, the extension block terminates with a "magic trailer" that routes any application treating the data as sub-blocks to a final 0 byte that terminates the sub-block chain.

(Source)

mootari commented 5 years ago

Here's a rough draft:

GIFEncoder.prototype.writeXMPExt = function() {
  this.out.writeByte(0x21); // extension introducer
  this.out.writeByte(0xff); // app extension label
  this.out.writeByte(0x0b); // block size
  this.out.writeUTFBytes('XMP DataXMP'); // app id + auth code

  this.out.writeUTFBytes(this.xmpData); // xmp data

  // magic trailer
  this.out.writeByte(0x01); 
  var i = 256;
  while(i--) this.out.writeByte(i);  

  this.out.writeByte(0); // block terminator
};

The XMP data itself is an XML string, and creating that one would probably be way out of scope for gif.js. The implementation could simply provide the means to add an XMP string to the file, so that the onus of creating the string is on the user.

References: