This commit is contained in:
2022-09-30 05:39:11 +00:00
parent 41ee9463ae
commit 4687fa49bc
11418 changed files with 1312504 additions and 0 deletions

View File

@ -0,0 +1,83 @@
'use strict';
var Transform = require('stream').Transform;
var streamParser = require('stream-parser');
var inherits = require('util').inherits;
function ParserStream() {
Transform.call(this, { readableObjectMode: true });
}
inherits(ParserStream, Transform);
streamParser(ParserStream.prototype);
exports.ParserStream = ParserStream;
exports.sliceEq = function (src, start, dest) {
for (var i = start, j = 0; j < dest.length;) {
if (src[i++] !== dest[j++]) return false;
}
return true;
};
exports.str2arr = function (str, format) {
var arr = [], i = 0;
if (format && format === 'hex') {
while (i < str.length) {
arr.push(parseInt(str.slice(i, i + 2), 16));
i += 2;
}
} else {
for (; i < str.length; i++) {
/* eslint-disable no-bitwise */
arr.push(str.charCodeAt(i) & 0xFF);
}
}
return arr;
};
exports.readUInt16LE = function (data, offset) {
return data[offset] | (data[offset + 1] << 8);
};
exports.readUInt16BE = function (data, offset) {
return data[offset + 1] | (data[offset] << 8);
};
exports.readUInt32LE = function (data, offset) {
return data[offset] |
(data[offset + 1] << 8) |
(data[offset + 2] << 16) |
(data[offset + 3] * 0x1000000);
};
exports.readUInt32BE = function (data, offset) {
return data[offset + 3] |
(data[offset + 2] << 8) |
(data[offset + 1] << 16) |
(data[offset] * 0x1000000);
};
function ProbeError(message, code, statusCode) {
Error.call(this);
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
if (code) this.code = code;
if (statusCode) this.statusCode = statusCode;
}
// Inherit from Error
require('inherits')(ProbeError, Error);
exports.ProbeError = ProbeError;

View File

@ -0,0 +1,36 @@
'use strict';
var ParserStream = require('../common').ParserStream;
var str2arr = require('../common').str2arr;
var sliceEq = require('../common').sliceEq;
var SIG_BM = str2arr('BM');
module.exports = function () {
var parser = new ParserStream();
parser._bytes(26, function (data) {
parser._skipBytes(Infinity);
if (!sliceEq(data, 0, SIG_BM)) {
parser.push(null);
return;
}
parser.push({
width: data.readUInt16LE(18),
height: data.readUInt16LE(22),
type: 'bmp',
mime: 'image/bmp',
wUnits: 'px',
hUnits: 'px'
});
parser.push(null);
});
return parser;
};

View File

@ -0,0 +1,37 @@
'use strict';
var ParserStream = require('../common').ParserStream;
var str2arr = require('../common').str2arr;
var sliceEq = require('../common').sliceEq;
var SIG_GIF87a = str2arr('GIF87a');
var SIG_GIF89a = str2arr('GIF89a');
module.exports = function () {
var parser = new ParserStream();
parser._bytes(10, function (data) {
parser._skipBytes(Infinity);
if (!sliceEq(data, 0, SIG_GIF87a) && !sliceEq(data, 0, SIG_GIF89a)) {
parser.push(null);
return;
}
parser.push({
width: data.readUInt16LE(6),
height: data.readUInt16LE(8),
type: 'gif',
mime: 'image/gif',
wUnits: 'px',
hUnits: 'px'
});
parser.push(null);
});
return parser;
};

View File

@ -0,0 +1,117 @@
'use strict';
var ParserStream = require('../common').ParserStream;
// part of parseJpegMarker called after skipping initial FF
function parseJpegMarker_afterFF(parser, callback) {
parser._bytes(1, function (data) {
var code = data[0];
if (code === 0xFF) {
// padding byte, skip it
parseJpegMarker_afterFF(parser, callback);
return;
}
// standalone markers, according to JPEG 1992,
// http://www.w3.org/Graphics/JPEG/itu-t81.pdf, see Table B.1
if ((0xD0 <= code && code <= 0xD9) || code === 0x01) {
callback(code, 0);
return;
}
// the rest of the unreserved markers
if (0xC0 <= code && code <= 0xFE) {
parser._bytes(2, function (length) {
callback(code, length.readUInt16BE(0) - 2);
});
return;
}
// unknown markers
callback();
});
}
function parseJpegMarker(parser, callback) {
parser._bytes(1, function (data) {
if (data[0] !== 0xFF) {
// not a JPEG marker
callback();
return;
}
parseJpegMarker_afterFF(parser, callback);
});
}
function getJpegSize(parser) {
parseJpegMarker(parser, function (code, length) {
if (!code || length < 0) {
// invalid jpeg
parser._skipBytes(Infinity);
parser.push(null);
return;
}
if (code === 0xD9 /* EOI */ || code === 0xDA /* SOS */) {
// end of the datastream
parser._skipBytes(Infinity);
parser.push(null);
return;
}
if (length <= 0) {
// e.g. empty comment
getJpegSize(parser);
return;
}
if (length >= 5 &&
(0xC0 <= code && code <= 0xCF) &&
code !== 0xC4 && code !== 0xC8 && code !== 0xCC) {
parser._bytes(length, function (data) {
parser._skipBytes(Infinity);
parser.push({
width: data.readUInt16BE(3),
height: data.readUInt16BE(1),
type: 'jpg',
mime: 'image/jpeg',
wUnits: 'px',
hUnits: 'px'
});
parser.push(null);
});
return;
}
parser._skipBytes(length, function () {
getJpegSize(parser);
});
});
}
module.exports = function () {
var parser = new ParserStream();
parser._bytes(2, function (data) {
if (data[0] !== 0xFF || data[1] !== 0xD8) {
// first marker of the file MUST be 0xFFD8
parser._skipBytes(Infinity);
parser.push(null);
return;
}
getJpegSize(parser);
});
return parser;
};

View File

@ -0,0 +1,44 @@
'use strict';
var ParserStream = require('../common').ParserStream;
var str2arr = require('../common').str2arr;
var sliceEq = require('../common').sliceEq;
var SIG_PNG = str2arr('\x89PNG\r\n\x1a\n');
var SIG_IHDR = str2arr('IHDR');
module.exports = function () {
var parser = new ParserStream();
parser._bytes(24, function (data) {
parser._skipBytes(Infinity);
// check PNG signature
if (!sliceEq(data, 0, SIG_PNG)) {
parser.push(null);
return;
}
// check that first chunk is IHDR
if (!sliceEq(data, 12, SIG_IHDR)) {
parser.push(null);
return;
}
parser.push({
width: data.readUInt32BE(16),
height: data.readUInt32BE(20),
type: 'png',
mime: 'image/png',
wUnits: 'px',
hUnits: 'px'
});
parser.push(null);
});
return parser;
};

View File

@ -0,0 +1,40 @@
'use strict';
var ParserStream = require('../common').ParserStream;
var str2arr = require('../common').str2arr;
var sliceEq = require('../common').sliceEq;
var SIG_8BPS = str2arr('8BPS\x00\x01');
module.exports = function () {
var parser = new ParserStream();
parser._bytes(6, function (data) {
// signature + version
if (!sliceEq(data, 0, SIG_8BPS)) {
parser._skipBytes(Infinity);
parser.push(null);
return;
}
parser._bytes(16, function (data) {
parser._skipBytes(Infinity);
parser.push({
width: data.readUInt32BE(12),
height: data.readUInt32BE(8),
type: 'psd',
mime: 'image/vnd.adobe.photoshop',
wUnits: 'px',
hUnits: 'px'
});
parser.push(null);
});
});
return parser;
};

View File

@ -0,0 +1,188 @@
'use strict';
/* eslint-disable consistent-return */
var Transform = require('stream').Transform;
var STATE_IDENTIFY = 0; // look for '<'
var STATE_PARSE = 1; // extract width and height from svg tag
var STATE_IGNORE = 2; // we got all the data we want, skip the rest
// max size for pre-svg-tag comments plus svg tag itself
var MAX_DATA_LENGTH = 65536;
var SVG_HEADER_RE = /<svg\s[^>]+>/;
var SVG_WIDTH_RE = /[^-]\bwidth="([^%]+?)"|[^-]\bwidth='([^%]+?)'/;
var SVG_HEIGHT_RE = /\bheight="([^%]+?)"|\bheight='([^%]+?)'/;
var SVG_VIEWBOX_RE = /\bview[bB]ox="(.+?)"|\bview[bB]ox='(.+?)'/;
var SVG_UNITS_RE = /in$|mm$|cm$|pt$|pc$|px$|em$|ex$/;
function isWhiteSpace(chr) {
return chr === 0x20 || chr === 0x09 || chr === 0x0D || chr === 0x0A;
}
// Filter NaN, Infinity, < 0
function isFinitePositive(val) {
return typeof val === 'number' && isFinite(val) && val > 0;
}
function svgAttrs(str) {
var width = str.match(SVG_WIDTH_RE);
var height = str.match(SVG_HEIGHT_RE);
var viewbox = str.match(SVG_VIEWBOX_RE);
return {
width: width && (width[1] || width[2]),
height: height && (height[1] || height[2]),
viewbox: viewbox && (viewbox[1] || viewbox[2])
};
}
function units(str) {
if (!SVG_UNITS_RE.test(str)) return 'px';
return str.match(SVG_UNITS_RE)[0];
}
function parseSvg(str) {
if (!SVG_HEADER_RE.test(str)) return;
var attrs = svgAttrs(str.match(SVG_HEADER_RE)[0]);
var width = parseFloat(attrs.width);
var height = parseFloat(attrs.height);
// Extract from direct values
if (attrs.width && attrs.height) {
if (!isFinitePositive(width) || !isFinitePositive(height)) return;
return {
width: width,
height: height,
type: 'svg',
mime: 'image/svg+xml',
wUnits: units(attrs.width),
hUnits: units(attrs.height)
};
}
// Extract from viewbox
var parts = (attrs.viewbox || '').split(' ');
var viewbox = {
width: parts[2],
height: parts[3]
};
var vbWidth = parseFloat(viewbox.width);
var vbHeight = parseFloat(viewbox.height);
if (!isFinitePositive(vbWidth) || !isFinitePositive(vbHeight)) return;
if (units(viewbox.width) !== units(viewbox.height)) return;
var ratio = vbWidth / vbHeight;
if (attrs.width) {
if (!isFinitePositive(width)) return;
return {
width: width,
height: width / ratio,
type: 'svg',
mime: 'image/svg+xml',
wUnits: units(attrs.width),
hUnits: units(attrs.width)
};
}
if (attrs.height) {
if (!isFinitePositive(height)) return;
return {
width: height * ratio,
height: height,
type: 'svg',
mime: 'image/svg+xml',
wUnits: units(attrs.height),
hUnits: units(attrs.height)
};
}
return {
width: vbWidth,
height: vbHeight,
type: 'svg',
mime: 'image/svg+xml',
wUnits: units(viewbox.width),
hUnits: units(viewbox.height)
};
}
module.exports = function () {
var state = STATE_IDENTIFY;
var data_len = 0;
var str = '';
var parser = new Transform({
readableObjectMode: true,
transform: function transform(chunk, encoding, next) {
switch (state) {
case STATE_IDENTIFY:
var i = 0, max = chunk.length;
while (i < max && isWhiteSpace(chunk[i])) i++;
if (i >= max) {
data_len += chunk.length;
if (data_len > MAX_DATA_LENGTH) {
state = STATE_IGNORE;
parser.push(null);
}
} else if (chunk[i] === 0x3c /* < */) {
state = STATE_PARSE;
return transform(chunk, encoding, next);
} else {
state = STATE_IGNORE;
parser.push(null);
}
break;
case STATE_PARSE:
str += chunk.toString();
var result = parseSvg(str);
if (result) {
parser.push(result);
parser.push(null);
break;
}
data_len += chunk.length;
if (data_len > MAX_DATA_LENGTH) {
state = STATE_IGNORE;
parser.push(null);
}
break;
}
next();
},
flush: function () {
state = STATE_IGNORE;
parser.push(null);
}
});
return parser;
};

View File

@ -0,0 +1,112 @@
'use strict';
var ParserStream = require('../common').ParserStream;
var str2arr = require('../common').str2arr;
var sliceEq = require('../common').sliceEq;
var SIG_1 = str2arr('II\x2A\0');
var SIG_2 = str2arr('MM\0\x2A');
function readUInt16(buffer, offset, is_big_endian) {
return is_big_endian ? buffer.readUInt16BE(offset) : buffer.readUInt16LE(offset);
}
function readUInt32(buffer, offset, is_big_endian) {
return is_big_endian ? buffer.readUInt32BE(offset) : buffer.readUInt32LE(offset);
}
function readIFDValue(data, data_offset, is_big_endian) {
var type = readUInt16(data, data_offset + 2, is_big_endian);
var values = readUInt32(data, data_offset + 4, is_big_endian);
if (values !== 1 || (type !== 3 && type !== 4)) {
return null;
}
if (type === 3) {
return readUInt16(data, data_offset + 8, is_big_endian);
}
return readUInt32(data, data_offset + 8, is_big_endian);
}
module.exports = function () {
var parser = new ParserStream();
// read header
parser._bytes(8, function (data) {
// check TIFF signature
if (!sliceEq(data, 0, SIG_1) && !sliceEq(data, 0, SIG_2)) {
parser._skipBytes(Infinity);
parser.push(null);
return;
}
var is_big_endian = (data[0] === 77 /* 'MM' */);
var count = readUInt32(data, 4, is_big_endian) - 8;
if (count < 0) {
parser._skipBytes(Infinity);
parser.push(null);
return;
}
function safeSkip(parser, count, callback) {
if (count === 0) { // parser._skipBytes throws error if count === 0
callback();
return;
}
parser._skipBytes(count, callback);
}
// skip until IFD
safeSkip(parser, count, function () {
// read number of IFD entries
parser._bytes(2, function (data) {
var ifd_size = readUInt16(data, 0, is_big_endian) * 12;
if (ifd_size <= 0) {
parser._skipBytes(Infinity);
parser.push(null);
return;
}
// read all IFD entries
parser._bytes(ifd_size, function (data) {
parser._skipBytes(Infinity);
var i, width, height, tag;
for (i = 0; i < ifd_size; i += 12) {
tag = readUInt16(data, i, is_big_endian);
if (tag === 256) {
width = readIFDValue(data, i, is_big_endian);
} else if (tag === 257) {
height = readIFDValue(data, i, is_big_endian);
}
}
if (width && height) {
parser.push({
width: width,
height: height,
type: 'tiff',
mime: 'image/tiff',
wUnits: 'px',
hUnits: 'px'
});
}
parser.push(null);
});
});
});
});
return parser;
};

View File

@ -0,0 +1,104 @@
'use strict';
/* eslint-disable no-bitwise */
var ParserStream = require('../common').ParserStream;
var str2arr = require('../common').str2arr;
var sliceEq = require('../common').sliceEq;
var SIG_RIFF = str2arr('RIFF');
var SIG_WEBPVP8 = str2arr('WEBPVP8');
function parseVP8(parser) {
parser._bytes(14, function (data) {
parser._skipBytes(Infinity);
if (data[7] !== 0x9D || data[8] !== 0x01 || data[9] !== 0x2A) {
// bad code block signature
parser.push(null);
return;
}
parser.push({
width: data.readUInt16LE(10) & 0x3FFF,
height: data.readUInt16LE(12) & 0x3FFF,
type: 'webp',
mime: 'image/webp',
wUnits: 'px',
hUnits: 'px'
});
parser.push(null);
});
}
function parseVP8L(parser) {
parser._bytes(9, function (data) {
parser._skipBytes(Infinity);
if (data[4] !== 0x2F) {
// bad code block signature
parser.push(null);
return;
}
var bits = data.readUInt32LE(5);
parser.push({
width: (bits & 0x3FFF) + 1,
height: ((bits >> 14) & 0x3FFF) + 1,
type: 'webp',
mime: 'image/webp',
wUnits: 'px',
hUnits: 'px'
});
parser.push(null);
});
}
function parseVP8X(parser) {
parser._bytes(14, function (data) {
parser._skipBytes(Infinity);
parser.push({
// TODO: replace with `data.readUIntLE(8, 3) + 1`
// when 0.10 support is dropped
width: ((data[10] << 16) | (data[9] << 8) | data[8]) + 1,
height: ((data[13] << 16) | (data[12] << 8) | data[11]) + 1,
type: 'webp',
mime: 'image/webp',
wUnits: 'px',
hUnits: 'px'
});
parser.push(null);
});
}
module.exports = function () {
var parser = new ParserStream();
parser._bytes(16, function (data) {
// check /^RIFF....WEBPVP8([ LX])$/ signature
if (sliceEq(data, 0, SIG_RIFF) && sliceEq(data, 8, SIG_WEBPVP8)) {
switch (data[15]) {
case 32/*' '*/: parseVP8(parser); return;
case 76/* L */: parseVP8L(parser); return;
case 88/* X */: parseVP8X(parser); return;
}
} else {
parser._skipBytes(Infinity);
parser.push(null);
}
});
return parser;
};

View File

@ -0,0 +1,25 @@
'use strict';
/* eslint-disable consistent-return */
var str2arr = require('../common').str2arr;
var sliceEq = require('../common').sliceEq;
var readUInt16LE = require('../common').readUInt16LE;
var SIG_BM = str2arr('BM');
module.exports = function (data) {
if (data.length < 26) return;
if (!sliceEq(data, 0, SIG_BM)) return;
return {
width: readUInt16LE(data, 18),
height: readUInt16LE(data, 22),
type: 'bmp',
mime: 'image/bmp',
wUnits: 'px',
hUnits: 'px'
};
};

View File

@ -0,0 +1,27 @@
'use strict';
/* eslint-disable consistent-return */
var str2arr = require('../common').str2arr;
var sliceEq = require('../common').sliceEq;
var readUInt16LE = require('../common').readUInt16LE;
var SIG_GIF87a = str2arr('GIF87a');
var SIG_GIF89a = str2arr('GIF89a');
module.exports = function (data) {
if (data.length < 10) return;
if (!sliceEq(data, 0, SIG_GIF87a) && !sliceEq(data, 0, SIG_GIF89a)) return;
return {
width: readUInt16LE(data, 6),
height: readUInt16LE(data, 8),
type: 'gif',
mime: 'image/gif',
wUnits: 'px',
hUnits: 'px'
};
};

View File

@ -0,0 +1,65 @@
'use strict';
/* eslint-disable consistent-return */
var readUInt16BE = require('../common').readUInt16BE;
module.exports = function (data) {
if (data.length < 2) return;
// first marker of the file MUST be 0xFFD8
if (data[0] !== 0xFF || data[1] !== 0xD8) return;
var offset = 2;
for (;;) {
if (data.length - offset < 2) return;
// not a JPEG marker
if (data[offset++] !== 0xFF) return;
var code = data[offset++];
var length;
// skip padding bytes
while (code === 0xFF) code = data[offset++];
// standalone markers, according to JPEG 1992,
// http://www.w3.org/Graphics/JPEG/itu-t81.pdf, see Table B.1
if ((0xD0 <= code && code <= 0xD9) || code === 0x01) {
length = 0;
} else if (0xC0 <= code && code <= 0xFE) {
// the rest of the unreserved markers
if (data.length - offset < 2) return;
length = readUInt16BE(data, offset) - 2;
offset += 2;
} else {
// unknown markers
return;
}
if (code === 0xD9 /* EOI */ || code === 0xDA /* SOS */) {
// end of the datastream
return;
}
if (length >= 5 &&
(0xC0 <= code && code <= 0xCF) &&
code !== 0xC4 && code !== 0xC8 && code !== 0xCC) {
if (data.length - offset < length) return;
return {
width: readUInt16BE(data, offset + 3),
height: readUInt16BE(data, offset + 1),
type: 'jpg',
mime: 'image/jpeg',
wUnits: 'px',
hUnits: 'px'
};
}
offset += length;
}
};

View File

@ -0,0 +1,31 @@
'use strict';
/* eslint-disable consistent-return */
var str2arr = require('../common').str2arr;
var sliceEq = require('../common').sliceEq;
var readUInt32BE = require('../common').readUInt32BE;
var SIG_PNG = str2arr('\x89PNG\r\n\x1a\n');
var SIG_IHDR = str2arr('IHDR');
module.exports = function (data) {
if (data.length < 24) return;
// check PNG signature
if (!sliceEq(data, 0, SIG_PNG)) return;
// check that first chunk is IHDR
if (!sliceEq(data, 12, SIG_IHDR)) return;
return {
width: readUInt32BE(data, 16),
height: readUInt32BE(data, 20),
type: 'png',
mime: 'image/png',
wUnits: 'px',
hUnits: 'px'
};
};

View File

@ -0,0 +1,27 @@
'use strict';
/* eslint-disable consistent-return */
var str2arr = require('../common').str2arr;
var sliceEq = require('../common').sliceEq;
var readUInt32BE = require('../common').readUInt32BE;
var SIG_8BPS = str2arr('8BPS\x00\x01');
module.exports = function (data) {
if (data.length < 6 + 16) return;
// signature + version
if (!sliceEq(data, 0, SIG_8BPS)) return;
return {
width: readUInt32BE(data, 6 + 12),
height: readUInt32BE(data, 6 + 8),
type: 'psd',
mime: 'image/vnd.adobe.photoshop',
wUnits: 'px',
hUnits: 'px'
};
};

View File

@ -0,0 +1,131 @@
'use strict';
/* eslint-disable consistent-return */
function isWhiteSpace(chr) {
return chr === 0x20 || chr === 0x09 || chr === 0x0D || chr === 0x0A;
}
// Filter NaN, Infinity, < 0
function isFinitePositive(val) {
return typeof val === 'number' && isFinite(val) && val > 0;
}
function canBeSvg(buf) {
var i = 0, max = buf.length;
while (i < max && isWhiteSpace(buf[i])) i++;
if (i === max) return false;
return buf[i] === 0x3c; /* < */
}
var SVG_HEADER_RE = /<svg\s[^>]+>/;
var SVG_WIDTH_RE = /[^-]\bwidth="([^%]+?)"|[^-]\bwidth='([^%]+?)'/;
var SVG_HEIGHT_RE = /\bheight="([^%]+?)"|\bheight='([^%]+?)'/;
var SVG_VIEWBOX_RE = /\bview[bB]ox="(.+?)"|\bview[bB]ox='(.+?)'/;
var SVG_UNITS_RE = /in$|mm$|cm$|pt$|pc$|px$|em$|ex$/;
function svgAttrs(str) {
var width = str.match(SVG_WIDTH_RE);
var height = str.match(SVG_HEIGHT_RE);
var viewbox = str.match(SVG_VIEWBOX_RE);
return {
width: width && (width[1] || width[2]),
height: height && (height[1] || height[2]),
viewbox: viewbox && (viewbox[1] || viewbox[2])
};
}
function units(str) {
if (!SVG_UNITS_RE.test(str)) return 'px';
return str.match(SVG_UNITS_RE)[0];
}
module.exports = function (data) {
if (!canBeSvg(data)) return;
var str = '';
for (var i = 0; i < data.length; i++) {
// 1. We can't rely on buffer features
// 2. Don't care about UTF16 because ascii is enougth for our goals
str += String.fromCharCode(data[i]);
}
if (!SVG_HEADER_RE.test(str)) return;
var attrs = svgAttrs(str.match(SVG_HEADER_RE)[0]);
var width = parseFloat(attrs.width);
var height = parseFloat(attrs.height);
// Extract from direct values
if (attrs.width && attrs.height) {
if (!isFinitePositive(width) || !isFinitePositive(height)) return;
return {
width: width,
height: height,
type: 'svg',
mime: 'image/svg+xml',
wUnits: units(attrs.width),
hUnits: units(attrs.height)
};
}
// Extract from viewbox
var parts = (attrs.viewbox || '').split(' ');
var viewbox = {
width: parts[2],
height: parts[3]
};
var vbWidth = parseFloat(viewbox.width);
var vbHeight = parseFloat(viewbox.height);
if (!isFinitePositive(vbWidth) || !isFinitePositive(vbHeight)) return;
if (units(viewbox.width) !== units(viewbox.height)) return;
var ratio = vbWidth / vbHeight;
if (attrs.width) {
if (!isFinitePositive(width)) return;
return {
width: width,
height: width / ratio,
type: 'svg',
mime: 'image/svg+xml',
wUnits: units(attrs.width),
hUnits: units(attrs.width)
};
}
if (attrs.height) {
if (!isFinitePositive(height)) return;
return {
width: height * ratio,
height: height,
type: 'svg',
mime: 'image/svg+xml',
wUnits: units(attrs.height),
hUnits: units(attrs.height)
};
}
return {
width: vbWidth,
height: vbHeight,
type: 'svg',
mime: 'image/svg+xml',
wUnits: units(viewbox.width),
hUnits: units(viewbox.height)
};
};

View File

@ -0,0 +1,86 @@
'use strict';
/* eslint-disable consistent-return */
var str2arr = require('../common').str2arr;
var sliceEq = require('../common').sliceEq;
var readUInt16LE = require('../common').readUInt16LE;
var readUInt16BE = require('../common').readUInt16BE;
var readUInt32LE = require('../common').readUInt32LE;
var readUInt32BE = require('../common').readUInt32BE;
var SIG_1 = str2arr('II\x2A\0');
var SIG_2 = str2arr('MM\0\x2A');
function readUInt16(buffer, offset, is_big_endian) {
return is_big_endian ? readUInt16BE(buffer, offset) : readUInt16LE(buffer, offset);
}
function readUInt32(buffer, offset, is_big_endian) {
return is_big_endian ? readUInt32BE(buffer, offset) : readUInt32LE(buffer, offset);
}
function readIFDValue(data, data_offset, is_big_endian) {
var type = readUInt16(data, data_offset + 2, is_big_endian);
var values = readUInt32(data, data_offset + 4, is_big_endian);
if (values !== 1 || (type !== 3 && type !== 4)) return null;
if (type === 3) {
return readUInt16(data, data_offset + 8, is_big_endian);
}
return readUInt32(data, data_offset + 8, is_big_endian);
}
module.exports = function (data) {
if (data.length < 8) return;
// check TIFF signature
if (!sliceEq(data, 0, SIG_1) && !sliceEq(data, 0, SIG_2)) return;
var is_big_endian = (data[0] === 77 /* 'MM' */);
var count = readUInt32(data, 4, is_big_endian) - 8;
if (count < 0) return;
// skip until IFD
var offset = count + 8;
if (data.length - offset < 2) return;
// read number of IFD entries
var ifd_size = readUInt16(data, offset + 0, is_big_endian) * 12;
if (ifd_size <= 0) return;
offset += 2;
// read all IFD entries
if (data.length - offset < ifd_size) return;
var i, width, height, tag;
for (i = 0; i < ifd_size; i += 12) {
tag = readUInt16(data, offset + i, is_big_endian);
if (tag === 256) {
width = readIFDValue(data, offset + i, is_big_endian);
} else if (tag === 257) {
height = readIFDValue(data, offset + i, is_big_endian);
}
}
if (width && height) {
return {
width: width,
height: height,
type: 'tiff',
mime: 'image/tiff',
wUnits: 'px',
hUnits: 'px'
};
}
};

View File

@ -0,0 +1,80 @@
'use strict';
/* eslint-disable no-bitwise */
/* eslint-disable consistent-return */
var str2arr = require('../common').str2arr;
var sliceEq = require('../common').sliceEq;
var readUInt16LE = require('../common').readUInt16LE;
var readUInt32LE = require('../common').readUInt32LE;
var SIG_RIFF = str2arr('RIFF');
var SIG_WEBPVP8 = str2arr('WEBPVP8');
function parseVP8(data) {
if (data.length < 16 + 14) return;
if (data[16 + 7] !== 0x9D || data[16 + 8] !== 0x01 || data[16 + 9] !== 0x2A) {
// bad code block signature
return;
}
return {
width: readUInt16LE(data, 16 + 10) & 0x3FFF,
height: readUInt16LE(data, 16 + 12) & 0x3FFF,
type: 'webp',
mime: 'image/webp',
wUnits: 'px',
hUnits: 'px'
};
}
function parseVP8L(data) {
if (data.length < 16 + 9) return;
if (data[16 + 4] !== 0x2F) return;
var bits = readUInt32LE(data, 16 + 5);
return {
width: (bits & 0x3FFF) + 1,
height: ((bits >> 14) & 0x3FFF) + 1,
type: 'webp',
mime: 'image/webp',
wUnits: 'px',
hUnits: 'px'
};
}
function parseVP8X(data) {
if (data.length < 16 + 14) return;
return {
// TODO: replace with `data.readUIntLE(8, 3) + 1`
// when 0.10 support is dropped
width: ((data[16 + 10] << 16) | (data[16 + 9] << 8) | data[16 + 8]) + 1,
height: ((data[16 + 13] << 16) | (data[16 + 12] << 8) | data[16 + 11]) + 1,
type: 'webp',
mime: 'image/webp',
wUnits: 'px',
hUnits: 'px'
};
}
module.exports = function (data) {
if (data.length < 16) return;
// check /^RIFF....WEBPVP8([ LX])$/ signature
if (sliceEq(data, 0, SIG_RIFF) && sliceEq(data, 8, SIG_WEBPVP8)) {
switch (data[15]) {
case 32/*' '*/: return parseVP8(data);
case 76/* L */: return parseVP8L(data);
case 88/* X */: return parseVP8X(data);
}
}
};

View File

@ -0,0 +1,13 @@
'use strict';
module.exports = {
bmp: require('./parse_stream/bmp'),
gif: require('./parse_stream/gif'),
jpeg: require('./parse_stream/jpeg'),
png: require('./parse_stream/png'),
psd: require('./parse_stream/psd'),
svg: require('./parse_stream/svg'),
tiff: require('./parse_stream/tiff'),
webp: require('./parse_stream/webp')
};

View File

@ -0,0 +1,13 @@
'use strict';
module.exports = {
bmp: require('./parse_sync/bmp'),
gif: require('./parse_sync/gif'),
jpeg: require('./parse_sync/jpeg'),
png: require('./parse_sync/png'),
psd: require('./parse_sync/psd'),
svg: require('./parse_sync/svg'),
tiff: require('./parse_sync/tiff'),
webp: require('./parse_sync/webp')
};