Giter Club home page Giter Club logo

glif.js's Introduction

glif.js

glif.js is a fork of omggif that speeds up gif decoding using WebGL.

install

For those using bower

$ bower install glif.js

usage

raw decoding

var gr = new GifReader(byteArray);
var glif = new GLIF(canvas);

gr.decodeAndGLIF(frame_num, glif);
  • byteArray is a Uint8Array containing the data to be decoded.
  • canvas is a HTMLCanvasElement where the gif will be displayed.
  • frame_num is the frame to display

animating

Use viewer.js to help animate the gif.

var view = viewer(canvas, data, callback);
var timeoutID = view(false);
  • canvas is a HTMLCanvasElement where the gif will be displayed.
  • data is one of ArrayBuffer, Uint8Array, or GifReader (from omggif).
  • callback is a function that is called after each frame is drawn. One parameter is passed - the timeoutID of the setTimeout() call. clearTimeout(timeoutId) will stop the animation.

viewer returns a function (view) to draw the next frame of the gif. The function takes one argument, once. If once is true, then only one frame is drawn. The function returns the timeoutID of the first frame.

how

GIF decoding is slow because LZW decompression must be performed serially. But the next step, mapping palette indices to RGB colors, is easily parallelizable by the GPU.

More specifically, it turns this slow for loop:

for (var i = 0, il = index_stream.length; i < il; ++i) {
	var index = index_stream[i];

	if (xleft === 0) {  // Beginning of new scan line
		op += scanstride;
		xleft = framewidth;
	}

	if (index === trans) {
		op += 4;
	} else {
		var r = buf[palette_offset + index * 3];
		var g = buf[palette_offset + index * 3 + 1];
		var b = buf[palette_offset + index * 3 + 2];
		pixels[op++] = r;
		pixels[op++] = g;
		pixels[op++] = b;
		pixels[op++] = 255;
	}
	--xleft;
}

into this fragment shader:

varying highp vec2 vTextureCoord;

uniform sampler2D uIndexStream;
uniform sampler2D uPalette;

uniform bool uTransparency;
uniform mediump float uTransparent;

void main(void) {
	mediump float uIndex = texture2D(uIndexStream, vTextureCoord).r;

	if (uTransparency && uIndex == uTransparent) {
		gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
	} else {
		gl_FragColor = texture2D(uPalette, vec2(uIndex, 0.5));
	}
}

License

(c) Dean McNamee <[email protected]>, 2013.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.

glif.js's People

Contributors

deanm avatar ericleong avatar

Watchers

James Cloos avatar Jihan Kim avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.