Really basic JavaScript library written in ECMAScript 2017 for parsing GIF (/gɪf/ not /ˈdʒɪf/) files.
fetch("//url.to/my.gif") /* request for a GIF file, can also be a filesystem
* read if you use Node or similar */
.then(GIF)
.then(async (gifObj) => {
// code to manipulate raw GIF data
const inflatedFrameData = await gifObj.inflate(0);
/* returns an array containing decompressed color codes of the first
* frame and also expands the frame's object with this array
* gifObj.frames[0].data === inflatedFrameData => true */
const deinterlacedFrameData = await gifObj.deinterlace(0);
/* returns an array containing deinterlaced color codes of the first
* frame and also expands the frame's object with this array
* gifObj.frames[0].deinterlacedData === deinterlacedFrameData => true
*
* please note that you can call this function without calling
* .inflate() first, because it will call it for you when the `data`
* property is missing */
const imageData = await gifObj.toImageData(0);
/* returns an ImageData object ready to be used in a <canvas>
* using this will NOT extend gifObj.frames[0] with an properties
*
* please note that you can call this function without calling
* .inflate() first *and .deinterlace() if necessary*, because it
* will call them for you when necessary */
})You'll notice that the Response object can be passed directly to the GIF
function. This is because GIF has a property (GIF.Response) which will be
checked using instanceof against the first argument. If it matches, then the
[GIF.responseMethod] method of the input will be called, which is by default
arrayBuffer, to compliment fetch in the browsers perfectly. Obviously, you
may change this as you wish depending on what you use to make requests.
I needed a versatile library to process GIFs for personal use, but the already available libraries out there all came with extra fluff that made it look difficult to achieve what I wanted using them.
GIF will resolve into an Object if successfull. Will be referenced
as gifObj from here on out. Please do note that a gifObj contains function
methods and Symbol properties, so structured clone algorithm will fail to
copy it properly to a worker. Importing the library into a worker itself is
recommended instead.
These methods are, non-enumerable, non-configurable and non-writeable properties
of gifObj. They are also asynchronous, non-generic functions that will throw
if this is not a gifObj.
-
inflate: accepts two parametersindex, ?clearRawDataindex: index of the frame to be processed, must be anintbigger than0, will throw otherwise
?clearRawData: if this parameter is set to any truthy value, then therawDataproperty ofgifObj.frames[index]will be set tovoid 0Returns an
Arraycontaining the LZW decompressed color codes of the frame and expandsgifObj.frames[index]with this array to thedataproperty. -
deinterlace: accepts two parametersindex, ?overwriteDataindex: index of the frame to be processed, must be anintbigger than0, will throw otherwise
?overwriteData: if this parameter is set to any truthy value, then thedataproperty ofgifObj.frames[index]will be overwritten with the resultReturns an
Arraycontaining the deinterlaced color codes of the frame and expandsgifObj.frames[index]with this array to thedeinterlacedDataproperty. IfoverwriteDatais true, thendatawill contain the result anddeinterlacedDatawill be set tonull. If the user didn't use.inflate(), then this function will do that with the optional parameter (clearRawData) set to true, to avoid needlessly taking up memory space. This method will only be called if the user omitted preprocessing the frames themselves. -
toImageData: accepts one parameterindexindex: index of the frame to be processed, must be anintbigger than0, will throw otherwiseReturns an
Arraywith this structure:[ImageData, offsetLeft, offsetTop]. It is recommended to destructure this array as parameters like soctx.putImageData(...gifObj.toImageData(0)). If the user didn't use.inflate()or.deinterlace()if needed, then this function will do that with both optional parameters (clearRawData,overwriteData) set to true, to avoid needlessly taking up memory space. These methods will only be called if the user omitted preprocessing the frames themselves.
Property names are in line with the GIF specification, for more detailed
explanation follow the link. gifObj also contains 2 Symbol properties for
internal uses.
descriptor: object containing theLogical Screen Descriptor- type:Objectwidth: width of the GIF - type:uint16height: height of the GIF - type:uint16backgroundColorIndex: type:uint8pixelAspectRatio: type:uint8packed: the packed byte - type:ObjectglobalColorTableFlag: indicates whether aGlobal Color Tableis present or not - type:int,0or1colorResolution: type:intsortFlag: indicates whether theGlobal Color Tableis sorted or not - type:int,0or1size: indicates the size ofGlobal Color Table(2 ** ( size + 1 )) - type:int,0-7
globalColorTable: type:ArrayifglobalColorTableFlagequals1, otherwiseundefined
individual colors are stored inArrays with the length of3repeat: number of times for the GIF to be repeated, where0means repeat forever - type:uint8frames: array containing the frames of the GIF - type:Array
The details of the frames are stored in an Object.
graphicExtension: object containing theGraphics Control Extension- type:ObjectdisposalMethod: type:int,0-7userInputFlag: type:int,0or1transparentColorFlag: type:int,0or1delay: the duration of which the frame should be displayed for in milliseconds - type:inttransparentColorIndex: type:uint8
descriptor: object containing theImage Descriptor- type:Objectleft: offset of the Image Data from the left of the GIF - type:uint16top: offset of the Image Data from the top of the GIF - type:uint16width: width of the Image Data - type:uint16height: height of the Image Data - type:uint16packed: the packed byte - type:ObjectlocalColorTableFlag: indicates whether aLocal Color Tableis present or not - type:int,0or1interlaceFlag: indicates whether the color codes are interlaced or not - type:int,0or1sortFlag: indicates whether theLocal Color Tableis sorted or not - type:int,0or1size: indicates the size ofLocal Color Table(2 ** ( size + 1 )) - type:int,0-7
localColorTable: type:ArrayiflocalColorTableFlagequals1, otherwiseundefined
individual colors are stored inArrays with the length of3minCodeSize: minimum code size required for color table building - type:uint8,2-8rawData: contains the concatenated Image Data sub-blocks - type:Array, will beundefinedif you call.inflate(index, true)
individual bytes are stored asuint8sdata: contains decompressed color codes - type:Arrayif.inflate()was called, otherwiseundefined
individual codes are stored asuint8sdeinterlacedData: contains deinterlaced color codes - type:Arrayif.deinterlace()was called ornullif.deinterlace(index, true)was called, otherwiseundefined
individual codes are stored asuint8s
See the project page on my GitHub Pages for an example usage. Worker addition can be found in this Gist.
What's In A GIF - Bit by Byte - Very easy to understand and detailed blog
by Matthew Flickinger
gifuct-js - LZW and deinterlace by Matt Way
WTFPL