GIF流編碼器和解碼器:gif-stream

jopen 10年前發布 | 20K 次閱讀 圖形/圖像處理 gif-stream

gif-stream是一個用于Node和瀏覽器的GIF流編碼器和解碼器。

Decoding 示例

This example uses the concat-frames module to collect the output of the GIF decoder into an array of frame objects.

var GIFDecoder = require('gif-stream/decoder');
var concat = require('concat-frames');

// decode a GIF file to RGB pixels
fs.createReadStream('in.gif')
  .pipe(new GIFDecoder)
  .pipe(concat(function(frames) {
    // frames is an array of frame objects
    // each one has a `pixels` property containing
    // the raw RGB pixel data for that frame, as
    // well as the width, height, etc.
  }));

Encoding 示例

You can encode a GIF by writing or piping indexed/quantized data to a GIFEncoder stream. If you write data to it manually, you need to first quantize the pixel data to produce a color palette and a buffer of indexed pixels. You can use the neuquant module to do this.

Alternatively, if you have a stream of RGB data already, you can pipe it first to a neuquant stream, and then to a GIF encoder, which will do the hard work of quantizing and writing indexed data for you.

var GIFEncoder = require('gif-stream/encoder');
var neuquant = require('neuquant');

// encode an animated GIF file by writing pixels to it.
// you need to manually quantize the data to produce a palette and indexed pixels.
var q = neuquant.quantize(pixels);

var enc = new GIFEncoder(width, height, { palette: q.palette });
enc.pipe(fs.createWriteStream('out.gif'));

// write indexed data
enc.end(q.indexed);

// or, pipe data from another RGB stream
// boom: streaming image transcoding!
fs.createReadStream('rgb.png')
  .pipe(new PNGDecoder)
  .pipe(new neuquant.Stream)
  .pipe(new GIFEncoder)
  .pipe(fs.createWriteStream('out.gif'));

項目主頁:http://www.baiduhome.net/lib/view/home/1416137994258

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!