image-size
Advanced tools
Comparing version 0.1.17 to 0.1.20
@@ -6,1 +6,2 @@ ##### Contributors | ||
* [Rudy Krol](https://github.com/rkrol) | ||
* [Linus Unnebäck](https://github.com/LinusU) |
@@ -8,3 +8,47 @@ // Abstract reading multi-byte unsigned integers | ||
} | ||
Buffer.prototype.readUInt = readUInt; | ||
module.exports = Buffer.prototype.readUInt = readUInt; | ||
function readBits (bits, offset) { | ||
offset = offset || 0; | ||
var value = 0, byte, start, bitsToRead; | ||
while (bits > 0 && this.length) { | ||
value = value << (bits > 8 ? 8 : bits); | ||
start = Math.floor(offset / 8); | ||
byte = this[start]; | ||
bitsToRead = (start + 1) * 8 - offset; | ||
value |= byte & (Math.pow(2, bitsToRead) - 1); | ||
offset += bitsToRead; | ||
bits -= bitsToRead; | ||
} | ||
return value; | ||
// var byteCount = Math.ceil(bits / 8); | ||
// var byteOffset = Math.floor(offset / 8); | ||
// var source = [].slice.call(this, | ||
// byteOffset, byteOffset + byteCount); | ||
// var start = ((byteOffset + 1) * 8) - offset; | ||
// console.log(source, start); | ||
// var value = 0, read = 0; | ||
// for (var i = 0; i < bits;) { | ||
// if ((bits - i) >= 8 && ((offset & 7) === 0)) { | ||
// value |= (bytes[offset >> 3] << i); | ||
// read = 8; | ||
// } else { | ||
// value |= ((this[offset >> 3] >> (offset & 7) & 0x1) << i); | ||
// read = 1; | ||
// } | ||
// offset += read; | ||
// i += read; | ||
// } | ||
// return value >>> 0; | ||
} | ||
Buffer.prototype.readBits = readBits; | ||
module.exports = {}; |
@@ -7,4 +7,4 @@ module.exports = [ | ||
'jpg', | ||
'tiff' | ||
// 'webp' | ||
]; | ||
'tiff', | ||
'webp' | ||
]; |
@@ -7,3 +7,2 @@ // based on https://developers.google.com/speed/webp/docs/riff_container | ||
var vp8Header = 'VP8' === buffer.toString('ascii', 12, 15); | ||
// console.log(buffer.readUInt32LE(4)); | ||
return (riffHeader && webpHeader && vp8Header); | ||
@@ -13,25 +12,37 @@ } | ||
function calculate (buffer) { | ||
var chunkHeader = buffer.toString('ascii', 12, 16); | ||
var lossless = ('L' === chunkHeader[3]); | ||
if (lossless) { | ||
// var chunkSize = buffer.readUInt32LE(16); | ||
var signature = buffer.toString('hex', 20, 21); | ||
if (signature === '2f') { | ||
// read 14 bits of width & 14 bits of height | ||
var bits = buffer.slice(21, 25).toJSON(); | ||
bits = bits.map(function (dec) { | ||
// console.log(dec.toString(2)); | ||
return dec.toString(2); | ||
}).join(''); | ||
// console.log(bits); | ||
// var width = (bits[0] << 6) + (bits[1] >> 2); | ||
// var height = bits[] | ||
// console.log(bits, width, (bits[0] << 6), (bits[1] >> 2)); | ||
} | ||
} else { | ||
// TODO: implementation for lossy webp | ||
buffer = buffer.slice(20, 30); | ||
// Lossless webp stream signature | ||
if (chunkHeader === 'VP8 ' && buffer[0] !== 0x2f) { | ||
return calculateLossy(buffer); | ||
} | ||
// Lossy webp stream signature | ||
var signature = buffer.toString('hex', 3, 6); | ||
if (chunkHeader === 'VP8L' && signature !== '9d012a') { | ||
return calculateLossless(buffer); | ||
} | ||
return false; | ||
} | ||
function calculateLossless (buffer) { | ||
return { | ||
'width': 1 + (((buffer[2] & 0x3F) << 8) | buffer[1]), | ||
'height': 1 + (((buffer[4] & 0xF) << 10) | (buffer[3] << 2) | | ||
((buffer[2] & 0xC0) >> 6)) | ||
}; | ||
} | ||
function calculateLossy (buffer) { | ||
// `& 0x3fff` returns the last 14 bits | ||
// TODO: include webp scaling in the calculations | ||
return { | ||
'width': buffer.readInt16LE(6) & 0x3fff, | ||
'height': buffer.readInt16LE(8) & 0x3fff | ||
}; | ||
} | ||
module.exports = { | ||
@@ -38,0 +49,0 @@ 'detect': isWebP, |
{ | ||
"name": "image-size", | ||
"version": "0.1.17", | ||
"version": "0.1.20", | ||
"description": "get dimensions of any image file", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17370
402