Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function generateGIF(frames, callback) {
// TODO: Weird: using a simple JS array instead of a typed array,
// the files are WAY smaller o_o. Patches/explanations welcome!
var buffer = []; // new Uint8Array(width * height * frames.length * 5);
var globalPalette;
var gifOptions = { loop: repeat };
// Using global palette but only if we're also using dithering
if(dithering !== null && palette !== null) {
globalPalette = palette;
gifOptions.palette = globalPalette;
}
var gifWriter = new GifWriter(buffer, width, height, gifOptions);
generatingGIF = true;
frames.forEach(function(frame, index) {
var framePalette;
if(!globalPalette) {
framePalette = frame.palette;
}
onRenderProgressCallback(0.75 + 0.25 * frame.position * 1.0 / frames.length);
gifWriter.addFrame(0, 0, width, height, frame.pixels, {
palette: framePalette,
delay: delay
});
function generateGIF(frames, callback) {
// TODO: Weird: using a simple JS array instead of a typed array,
// the files are WAY smaller o_o. Patches/explanations welcome!
var buffer = []; // new Uint8Array(width * height * frames.length * 5);
var globalPalette;
var gifOptions = { loop: repeat };
// Using global palette but only if we're also using dithering
if(dithering !== null && palette !== null) {
globalPalette = palette;
gifOptions.palette = globalPalette;
}
var gifWriter = new GifWriter(buffer, width, height, gifOptions);
generatingGIF = true;
frames.forEach(function(frame, index) {
var framePalette;
if(!globalPalette) {
framePalette = frame.palette;
}
onRenderProgressCallback(0.75 + 0.25 * frame.position * 1.0 / frames.length);
gifWriter.addFrame(0, 0, width, height, frame.pixels, {
palette: framePalette,
delay: delay
});
png.on('parsed', () => {
if (!this.encoder) {
let { width, height } = png;
this.width = width;
this.height = height;
this.gif = [];
this.encoder = new GIF(this.gif, width, height, { loop: 0 });
}
let { width, height, quality } = this;
let out = processFrame(png.data, width, height, quality);
this.encoder.addFrame(0, 0, width, height, out.pixels, {
// by spec, delay is specified in hundredths of seconds
delay: Math.round(delay / 10),
palette: out.palette
});
resolve(true);
});
png.write(buf);
// GIF requires palette size be a power of 2, so pad with black
while ((num_colors & (num_colors - 1)) || (num_colors < 2)) {
palette_data.push( 0x000000 );
num_colors++;
}
this.logDebug(6, "Compressing into GIF", {
palette_size: num_colors,
transparent_index: transparent_index
} );
// construct GIF buffer
var buf = Buffer.alloc( (width * height) + 1024 );
var gf = null;
try {
gf = new OMGGIF.GifWriter( buf, width, height, { palette: palette_data } );
gf.addFrame( 0, 0, width, height, iq_pixels, { transparent: transparent_index } );
}
catch (err) {
return this.doError('gif', "GIF compression error: " + err, callback);
}
// and we're done!
this.logDebug(6, "GIF compression complete");
callback( false, buf.slice(0, gf.end()) );
}
function _encodeGlobal(frames, spec, bufferSizeEst, globalPalette) {
// would be inefficient for frames to lookup colors in extended palette
const extendedGlobalPalette = {
colors: globalPalette.colors.slice(),
usesTransparency: globalPalette.usesTransparency
};
_extendPaletteToPowerOf2(extendedGlobalPalette);
const options = {
palette: extendedGlobalPalette.colors,
loop: spec.loops
};
let buffer = new Buffer(bufferSizeEst);
let gifWriter;
try {
gifWriter = new Omggif.GifWriter(buffer, spec.width, spec.height,
options);
}
catch (err) {
throw new GifError(err);
}
for (let i = 0; i < frames.length; ++i) {
buffer = _writeFrame(gifWriter, i, frames[i], globalPalette, false);
}
return new Gif(buffer.slice(0, gifWriter.end()), frames, spec);
}
function _encodeLocal(frames, spec, bufferSizeEst, localPalettes) {
const options = {
loop: spec.loops
};
let buffer = new Buffer(bufferSizeEst);
let gifWriter;
try {
gifWriter = new Omggif.GifWriter(buffer, spec.width, spec.height,
options);
}
catch (err) {
throw new GifError(err);
}
for (let i = 0; i < frames.length; ++i) {
buffer = _writeFrame(gifWriter, i, frames[i], localPalettes[i], true);
}
return new Gif(buffer.slice(0, gifWriter.end()), frames, spec);
}
switch(this.paletteMethod) {
case paletteMethods.KMEANS:
this.buildPaletteInternal = this.buildPaletteKMeans;
break;
case paletteMethods.VOTES:
this.buildPaletteInternal = this.buildPaletteVotes;
break;
case paletteMethods.NEUQUANT:
this.buildPaletteInternal = this.buildPaletteNeuQuant;
break;
default:
throw new Error('Unknown Palette method.');
}
var buffer = new Uint8Array(this.size.width * this.size.height * this.frames);
var gif = new OMGGIF.GifWriter(buffer, this.size.width, this.size.height, {
loop: 0
});
var context3d = this.renderer.getContext();
var imageDataArraySource = new Uint8Array(this.size.width * this.size.height * 4);
this.context3d = context3d;
this.imageDataArraySource = imageDataArraySource;
this.buffer = buffer;
this.gif = gif;
}
//convert loopLimit back into Netscape Block formatting
let loopLimit = props.loopLimit;
if (loopLimit === 1) {
loopLimit = null;
} else if (loopLimit === null) {
loopLimit = 0;
}
const opts = {
loop: loopLimit
};
const buffer = new Uint8Array(
pImg.width * pImg.height * props.numFrames * gifFormatDelay
);
const gifWriter = new omggif.GifWriter(buffer, pImg.width, pImg.height, opts);
const palette = [];
//loop over frames and build pixel -> palette index for each
for (let i = 0; i < props.numFrames; i++) {
const pixelPaletteIndex = new Uint8Array(pImg.width * pImg.height);
const data = props.frames[i].image.data;
const dataLength = data.length;
for (let j = 0, k = 0; j < dataLength; j += 4, k++) {
const r = data[j + 0];
const g = data[j + 1];
const b = data[j + 2];
const color = (r << 16) | (g << 8) | (b << 0);
const index = palette.indexOf(color);
if (index === -1) {
pixelPaletteIndex[k] = palette.length;
palette.push(color);
} else {