Skip to content

Commit

Permalink
Add single frame encoder for type-gif (#899)
Browse files Browse the repository at this point in the history
* Add single frame encoder for type-gif

* Fix linting errors

* Update packages/type-gif/package.json

Co-authored-by: Andrew Lisowski <lisowski54@gmail.com>
  • Loading branch information
jeffbseeking and hipstersmoothie committed Jun 5, 2020
1 parent 612cef4 commit fcc5b23
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/jimp/test/filetypes.test.js
Expand Up @@ -36,6 +36,13 @@ describe('FileType', () => {

image.getMIME().should.be.equal(clone.getMIME());
});

it('clones gif with the correct MIME type', async () => {
const image = await Jimp.read(imagesDir + '/flower.gif');
const clone = image.clone();

image.getMIME().should.be.equal(clone.getMIME());
});
});

describe('hasAlpha', () => {
Expand Down
Binary file added packages/jimp/test/images/flower.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/type-gif/package.json
Expand Up @@ -19,6 +19,7 @@
"dependencies": {
"@babel/runtime": "^7.7.2",
"@jimp/utils": "link:../utils",
"gifwrap": "^0.9.2",
"omggif": "^1.0.9"
},
"peerDependencies": {
Expand Down
13 changes: 13 additions & 0 deletions packages/type-gif/src/index.js
@@ -1,4 +1,5 @@
import GIF from 'omggif';
import { GifUtil, GifFrame, BitmapImage, GifCodec } from 'gifwrap';

const MIME_TYPE = 'image/gif';

Expand All @@ -22,5 +23,17 @@ export default () => ({
height: gifObj.height
};
}
},

encoders: {
[MIME_TYPE]: data => {
const bitmap = new BitmapImage(data.bitmap);
GifUtil.quantizeDekker(bitmap, 256);
const newFrame = new GifFrame(bitmap);
const gifCodec = new GifCodec();
return gifCodec.encodeGif([newFrame], {}).then(newGif => {
return newGif.buffer;
});
}
}
});
29 changes: 29 additions & 0 deletions packages/type-gif/test/gif.test.js
@@ -0,0 +1,29 @@
import { Jimp, getTestDir } from '@jimp/test-utils';
import configure from '@jimp/custom';

import gif from '../src';

const jimp = configure({ types: [gif] }, Jimp);

describe('GIF', () => {
const imagesDir = getTestDir(__dirname) + '/images';

it('load GIF', async () => {
const image = await jimp.read(imagesDir + '/flower.gif');
image.getPixelColor(10, 10).should.be.equal(0xe5e6d9ff);
});

it('load animated GIF', async () => {
const image = await jimp.read(imagesDir + '/animated.gif');
image.getPixelColor(10, 10).should.be.equal(0xa1d2f1ff);
});

it('export GIF', async () => {
const jgd = await jimp.read(imagesDir + '/flower.gif');
const buffer = await jgd.getBufferAsync('image/gif');
buffer
.toString()
.startsWith('GIF')
.should.be.equal(true);
});
});
Binary file added packages/type-gif/test/images/animated.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/type-gif/test/images/flower.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fcc5b23

Please sign in to comment.