Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions src/gifcodec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,7 @@

const Omggif = require('omggif');
const { Gif, GifError } = require('./gif');

// allow circular dependency with GifUtil
function GifUtil() {
const data = require('./gifutil');

GifUtil = function () {
return data;
};

return data;
}

const GifUtil = require('./gifutil');
const { GifFrame } = require('./gifframe');

const PER_GIF_OVERHEAD = 200; // these are guesses at upper limits
Expand Down Expand Up @@ -102,7 +91,7 @@ class GifCodec
if (frames === null || frames.length === 0) {
throw new GifError("there are no frames");
}
const dims = GifUtil().getMaxDimensions(frames);
const dims = GifUtil.getMaxDimensions(frames);

spec = Object.assign({}, spec); // don't munge caller's spec
spec.width = dims.maxWidth;
Expand Down Expand Up @@ -179,10 +168,10 @@ class GifCodec
_encodeGif(frames, spec) {
let colorInfo;
if (spec.colorScope === Gif.LocalColorsOnly) {
colorInfo = GifUtil().getColorInfo(frames, 0);
colorInfo = GifUtil.getColorInfo(frames, 0);
}
else {
colorInfo = GifUtil().getColorInfo(frames, 256);
colorInfo = GifUtil.getColorInfo(frames, 256);
if (!colorInfo.colors) { // if global palette impossible
if (spec.colorScope === Gif.GlobalColorsOnly) {
throw new GifError(
Expand Down
11 changes: 6 additions & 5 deletions src/gifutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ const ImageQ = require('image-q');
const BitmapImage = require('./bitmapimage');
const { GifFrame } = require('./gifframe');
const { GifError } = require('./gif');
const { GifCodec } = require('./gifcodec');

const INVALID_SUFFIXES = ['.jpg', '.jpeg', '.png', '.bmp'];

const defaultCodec = new GifCodec();

/**
* cloneFrames() clones provided frames. It's a utility method for cloning an entire array of frames at once.
*
Expand Down Expand Up @@ -212,7 +209,9 @@ exports.quantizeWu = function (imageOrImages, maxColorIndexes, significantBits,
*/

exports.read = function (source, decoder) {
decoder = decoder || defaultCodec;
if(decoder == null){
throw "Decoder cannot be null. Provide decoder"
}
if (Buffer.isBuffer(source)) {
return decoder.decodeGif(source);
}
Expand Down Expand Up @@ -253,7 +252,9 @@ exports.shareAsJimp = function (jimp, bitmapImageToShare) {
*/

exports.write = function (path, frames, spec, encoder) {
encoder = encoder || defaultCodec;
if(encoder == null){
throw "Encoder cannot be null. Provide encoder"
}
const matches = path.match(/\.[a-zA-Z]+$/); // prevent accidents
if (matches !== null &&
INVALID_SUFFIXES.includes(matches[0].toLowerCase()))
Expand Down
20 changes: 10 additions & 10 deletions test/test_decoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("single frame decoding", () => {

const name = 'singleFrameMonoOpaque';
const bitmap = Tools.getBitmap(name);
return GifUtil.read(Tools.getGifPath(name))
return GifUtil.read(Tools.getGifPath(name), new GifCodec())
.then(gif => {

_compareGifToSeries(gif, [bitmap], {
Expand All @@ -25,7 +25,7 @@ describe("single frame decoding", () => {

const name = 'singleFrameMultiOpaque';
const bitmap = Tools.getBitmap(name);
return GifUtil.read(Tools.getGifPath(name))
return GifUtil.read(Tools.getGifPath(name), new GifCodec())
.then(gif => {

_compareGifToSeries(gif, [bitmap], {
Expand All @@ -39,7 +39,7 @@ describe("single frame decoding", () => {

const name = 'singleFrameNoColorTrans';
const bitmap = Tools.getBitmap(name, 0);
return GifUtil.read(Tools.getGifPath(name))
return GifUtil.read(Tools.getGifPath(name), new GifCodec())
.then(gif => {

_compareGifToSeries(gif, [bitmap], {
Expand All @@ -53,7 +53,7 @@ describe("single frame decoding", () => {

const name = 'singleFrameMonoTrans';
const bitmap = Tools.getBitmap(name, 0);
return GifUtil.read(Tools.getGifPath(name))
return GifUtil.read(Tools.getGifPath(name), new GifCodec())
.then(gif => {

_compareGifToSeries(gif, [bitmap], {
Expand All @@ -67,7 +67,7 @@ describe("single frame decoding", () => {

const name = 'singleFrameMultiTrans';
const bitmap = Tools.getBitmap(name, 0);
return GifUtil.read(Tools.getGifPath(name))
return GifUtil.read(Tools.getGifPath(name), new GifCodec())
.then(gif => {

_compareGifToSeries(gif, [bitmap], {
Expand Down Expand Up @@ -100,7 +100,7 @@ describe("multiframe decoding", () => {

const name = 'twoFrameMultiOpaque';
const series = Tools.getSeries(name);
return GifUtil.read(Tools.getGifPath(name))
return GifUtil.read(Tools.getGifPath(name), new GifCodec())
.then(gif => {

_compareGifToSeries(gif, series, {
Expand All @@ -115,7 +115,7 @@ describe("multiframe decoding", () => {

const name = 'threeFrameMonoTrans';
const series = Tools.getSeries(name);
return GifUtil.read(Tools.getGifPath(name))
return GifUtil.read(Tools.getGifPath(name), new GifCodec())
.then(gif => {

_compareGifToSeries(gif, series, {
Expand All @@ -128,7 +128,7 @@ describe("multiframe decoding", () => {

it("reads a large multiframe file w/out transparency", () => {

return GifUtil.read(Tools.getGifPath('nburling-public'))
return GifUtil.read(Tools.getGifPath('nburling-public'), new GifCodec())
.then(gif => {

assert.strictEqual(gif.width, 238);
Expand All @@ -154,7 +154,7 @@ describe("multiframe decoding", () => {

it("reads a large multiframe file w/ offsets, w/out transparency", () => {

return GifUtil.read(Tools.getGifPath('rnaples-offsets-public'))
return GifUtil.read(Tools.getGifPath('rnaples-offsets-public'), new GifCodec())
.then(gif => {

assert.strictEqual(gif.width, 480);
Expand Down Expand Up @@ -205,7 +205,7 @@ describe("partial frame decoding", () => {
it("renders partial frames properly onto full frames", () => {
let actualBitmapImage;

return GifUtil.read(Tools.getGifPath('rnaples-offsets-public'))
return GifUtil.read(Tools.getGifPath('rnaples-offsets-public'), new GifCodec())
.then(actualGif => {
actualBitmapImage = actualGif.frames[10];
return Tools.loadBitmapImage(Tools.getImagePath('rnaples-frame-10.png'));
Expand Down
24 changes: 12 additions & 12 deletions test/test_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const assert = require('chai').assert;
const Tools = require('./lib/tools');
const { Gif, GifFrame, GifCodec, GifError, GifUtil } = require('../src/index');
const { GifFrame, GifCodec, GifUtil } = require('../src/index');

const defaultCodec = new GifCodec();

Expand Down Expand Up @@ -58,7 +58,7 @@ describe("Gif loop count", () => {

it("decodes an infinite loop", () => {

return GifUtil.read(Tools.getGifPath('countConstantDelay0'))
return GifUtil.read(Tools.getGifPath('countConstantDelay0'), defaultCodec)
.then(gif => {

assert.strictEqual(gif.loops, 0);
Expand All @@ -67,7 +67,7 @@ describe("Gif loop count", () => {

it("decodes a single-pass loop", () => {

return GifUtil.read(Tools.getGifPath('countConstantDelay1'))
return GifUtil.read(Tools.getGifPath('countConstantDelay1'), defaultCodec)
.then(gif => {

assert.strictEqual(gif.loops, 1);
Expand All @@ -76,7 +76,7 @@ describe("Gif loop count", () => {

it("decodes a three-pass loop", () => {

return GifUtil.read(Tools.getGifPath('countConstantDelay3'))
return GifUtil.read(Tools.getGifPath('countConstantDelay3'), defaultCodec)
.then(gif => {

assert.strictEqual(gif.loops, 3);
Expand All @@ -103,7 +103,7 @@ describe("Gif transparency", () => {

it("indicates no transparency", () => {

return GifUtil.read(Tools.getGifPath('twoFrameMultiOpaque'))
return GifUtil.read(Tools.getGifPath('twoFrameMultiOpaque'), defaultCodec)
.then(readGif => {

assert.strictEqual(readGif.usesTransparency, false);
Expand All @@ -117,7 +117,7 @@ describe("Gif transparency", () => {

it("indicates transparency", () => {

return GifUtil.read(Tools.getGifPath('threeFrameMonoTrans'))
return GifUtil.read(Tools.getGifPath('threeFrameMonoTrans'), defaultCodec)
.then(readGif => {

assert.strictEqual(readGif.usesTransparency, true);
Expand All @@ -134,7 +134,7 @@ describe("GifFrame x/y-offsets", () => {

it("accommodates/encodes offsets", () => {

return GifUtil.read(Tools.getGifPath('count5x7'))
return GifUtil.read(Tools.getGifPath('count5x7'), defaultCodec)
.then(readGif => {

const frames = readGif.frames;
Expand Down Expand Up @@ -174,7 +174,7 @@ describe("GifFrame delay", () => {

it("confirms a constant frame-delay GIF", () => {

return GifUtil.read(Tools.getGifPath('countConstantDelay0'))
return GifUtil.read(Tools.getGifPath('countConstantDelay0'), defaultCodec)
.then(readGif => {

readGif.frames.forEach(frame => {
Expand All @@ -186,7 +186,7 @@ describe("GifFrame delay", () => {

it("confirms a variable frame-delay GIF", () => {

return GifUtil.read(Tools.getGifPath('countIncreasingDelay'))
return GifUtil.read(Tools.getGifPath('countIncreasingDelay'), defaultCodec)
.then(readGif => {

const frames = readGif.frames;
Expand All @@ -198,7 +198,7 @@ describe("GifFrame delay", () => {

it("encodes varying frame delays", () => {

return GifUtil.read(Tools.getGifPath('countConstantDelay0'))
return GifUtil.read(Tools.getGifPath('countConstantDelay0'), defaultCodec)
.then(readGif => {

const frames = readGif.frames;
Expand All @@ -225,7 +225,7 @@ describe("GifFrame disposal method", () => {

it("encodes/decodes disposal", () => {

return GifUtil.read(Tools.getGifPath('countConstantDelay0'))
return GifUtil.read(Tools.getGifPath('countConstantDelay0'), defaultCodec)
.then(readGif => {

const frames = readGif.frames;
Expand Down Expand Up @@ -255,7 +255,7 @@ describe("GifFrame disposal method", () => {
});

function _verifyEncodesLoopCount(sourceFilename, loopCount) {
return GifUtil.read(Tools.getGifPath(sourceFilename))
return GifUtil.read(Tools.getGifPath(sourceFilename), defaultCodec)
.then(readGif => {

return defaultCodec.encodeGif(readGif.frames, { loops: loopCount });
Expand Down