Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add single frame encoder for type-gif #899

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.