-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
136 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict' | ||
|
||
var gif = require('./types/gif') | ||
var png = require('./types/png') | ||
|
||
/** | ||
* Checks if buffer contains animated image | ||
* | ||
* @param {Buffer} buffer | ||
* @returns {boolean} | ||
*/ | ||
function isAnimated (buffer) { | ||
if (gif.isGIF(buffer)) { | ||
return gif.isAnimated(buffer) | ||
} | ||
|
||
if (png.isPNG(buffer)) { | ||
return png.isAnimated(buffer) | ||
} | ||
|
||
return false | ||
} | ||
|
||
module.exports = isAnimated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
exports.isPNG = function (buffer) { | ||
var header = buffer.slice(0, 8).toString('hex') | ||
return (header === '89504e470d0a1a0a') // \211 P N G \r \n \032 'n | ||
} | ||
|
||
exports.isAnimated = function (buffer) { | ||
var hasACTL = false | ||
var hasIDAT = false | ||
var hasFDAT = false | ||
|
||
var previousChunkType = null | ||
|
||
var offset = 8 | ||
|
||
while (offset < buffer.length) { | ||
var chunkLength = buffer.readUInt32BE(offset) | ||
var chunkType = buffer.slice(offset + 4, offset + 8).toString('ascii') | ||
|
||
switch (chunkType) { | ||
case 'acTL': | ||
hasACTL = true | ||
break | ||
case 'IDAT': | ||
if (!hasACTL) { | ||
return false | ||
} | ||
|
||
if (previousChunkType !== 'fcTL') { | ||
return false | ||
} | ||
|
||
hasIDAT = true | ||
break | ||
case 'fdAT': | ||
if (!hasIDAT) { | ||
return false | ||
} | ||
|
||
if (previousChunkType !== 'fcTL') { | ||
return false | ||
} | ||
|
||
hasFDAT = true | ||
break | ||
} | ||
|
||
previousChunkType = chunkType | ||
offset += 4 + 4 + chunkLength + 4 | ||
} | ||
|
||
return (hasACTL && hasIDAT && hasFDAT) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,49 @@ | ||
'use strict' | ||
|
||
var fs = require('fs') | ||
var path = require('path') | ||
var test = require('tape') | ||
var isAnimated = require('../index') | ||
var isAnimated = require('../lib') | ||
|
||
test('Test animated GIFs', function (t) { | ||
var images = fs.readdirSync('./test/animated') | ||
var types = [ 'gif', 'png' ] | ||
|
||
t.plan(images.length) | ||
types.forEach(function (type) { | ||
test('Test animated ' + type.toUpperCase() + ' images', function (t) { | ||
var images = fs.readdirSync('./test/animated').filter(function (name) { | ||
return path.extname(name).slice(1) === type | ||
}) | ||
|
||
images.forEach(function (imgName) { | ||
var buffer = fs.readFileSync('./test/animated/' + imgName) | ||
t.true(isAnimated(buffer), imgName) | ||
t.plan(images.length) | ||
|
||
images.forEach(function (imgName) { | ||
var buffer = fs.readFileSync('./test/animated/' + imgName) | ||
t.true(isAnimated(buffer), imgName) | ||
}) | ||
}) | ||
}) | ||
|
||
test('Test static GIFs', function (t) { | ||
var images = fs.readdirSync('./test/static') | ||
test('Test static ' + type.toUpperCase() + ' images', function (t) { | ||
var images = fs.readdirSync('./test/static').filter(function (name) { | ||
return path.extname(name).slice(1) === type | ||
}) | ||
|
||
t.plan(images.length) | ||
t.plan(images.length) | ||
|
||
images.forEach(function (imgName) { | ||
var buffer = fs.readFileSync('./test/static/' + imgName) | ||
t.false(isAnimated(buffer), imgName) | ||
images.forEach(function (imgName) { | ||
var buffer = fs.readFileSync('./test/static/' + imgName) | ||
t.false(isAnimated(buffer), imgName) | ||
}) | ||
}) | ||
}) | ||
|
||
test('Test invalid GIFs', function (t) { | ||
var images = fs.readdirSync('./test/invalid') | ||
test('Test invalid ' + type.toUpperCase() + ' images', function (t) { | ||
var images = fs.readdirSync('./test/invalid').filter(function (name) { | ||
return path.extname(name).slice(1) === type | ||
}) | ||
|
||
t.plan(images.length) | ||
t.plan(images.length) | ||
|
||
images.forEach(function (imgName) { | ||
var buffer = fs.readFileSync('./test/invalid/' + imgName) | ||
t.false(isAnimated(buffer), imgName) | ||
images.forEach(function (imgName) { | ||
var buffer = fs.readFileSync('./test/invalid/' + imgName) | ||
t.false(isAnimated(buffer), imgName) | ||
}) | ||
}) | ||
}) |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.