Permalink
Browse files

refactor

  • Loading branch information...
1 parent dd2c30e commit 625f2e957a069c22d12e11262bd1f6286541f5d7 @netroy netroy committed Jan 22, 2014
Showing with 87 additions and 58 deletions.
  1. +45 −1 lib/patches.js
  2. +17 −57 lib/types/webp.js
  3. +25 −0 specs/patches.spec.js
View
@@ -5,5 +5,49 @@ function readUInt (bits, offset, isBigEndian) {
var method = this['readUInt' + bits + endian];
return method.call(this, offset);
}
+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 = {};
View
@@ -10,76 +10,36 @@ function isWebP (buffer) {
function calculate (buffer) {
var chunkHeader = buffer.toString('ascii', 12, 16);
+ buffer = buffer.slice(20, 30);
- switch (chunkHeader) {
- case 'VP8 ': return calculateLossy(buffer);
- case 'VP8L': return calculateLossless(buffer);
+ // 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) {
- /*
-
- https://gerrit.chromium.org/gerrit/gitweb?p=webm/libwebp.git;a=blob;f=doc/webp-lossless-bitstream-spec.txt;hb=master
-
- The first 28 bits of the bitstream specify the width and height of the
- image. Width and height are decoded as 14-bit integers as follows:
-
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- int image_width = ReadBits(14) + 1;
- int image_height = ReadBits(14) + 1;
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
- */
-
- var signature = buffer[20];
-
- if(signature !== 0x2f) { return false; }
-
- var b0 = buffer[21],
- b1 = buffer[22],
- b2 = buffer[23],
- b3 = buffer[24];
-
return {
- width: 1 + (((b1 & 0x3F) << 8) | b0),
- height: 1 + (((b3 & 0xF) << 10) | (b2 << 2) | ((b1 & 0xC0) >> 6))
+ 'width': 1 + (((buffer[2] & 0x3F) << 8) | buffer[1]),
+ 'height': 1 + (((buffer[4] & 0xF) << 10) | (buffer[3] << 2) |
+ ((buffer[2] & 0xC0) >> 6))
};
}
function calculateLossy (buffer) {
- /*
-
- http://tools.ietf.org/html/rfc6386#page-30
-
- ---- Begin code block --------------------------------------
-
- Start code byte 0 0x9d
- Start code byte 1 0x01
- Start code byte 2 0x2a
-
- 16 bits : (2 bits Horizontal Scale << 14) | Width (14 bits)
- 16 bits : (2 bits Vertical Scale << 14) | Height (14 bits)
-
- ---- End code block ----------------------------------------
-
- */
-
- var offset = 23;
- var startCodes = buffer.slice(offset, offset + 3);
-
- if(startCodes[0] !== 0x9d) { return false; }
- if(startCodes[1] !== 0x01) { return false; }
- if(startCodes[2] !== 0x2a) { return false; }
-
- var width = buffer.readInt16LE(offset + 3);
- var height = buffer.readInt16LE(offset + 5);
-
+ // `& 0x3fff` returns the last 14 bits
+ // TODO: include webp scaling in the calculations
return {
- width: (width & 0x3fff),
- height: (height & 0x3fff)
+ 'width': buffer.readInt16LE(6) & 0x3fff,
+ 'height': buffer.readInt16LE(8) & 0x3fff
};
}
View
@@ -0,0 +1,25 @@
+require('../lib/patches');
+
+var expect = require('expect.js');
+
+describe('readBits', function () {
+
+ // it('should read bits for offset 0', function () {
+ // var buffer = new Buffer([0x40, 0x20]);
+ // var expected = (((buffer[1] & 0x3F) << 8) | buffer[0]);
+ // var got = buffer.readBits(14);
+
+ // expect(got).to.equal(expected);
+ // expect(got).to.equal(8256);
+ // });
+
+ // it('should read bits for non-zero offset', function () {
+ // var buffer = new Buffer([0x2f, 0x7a, 0xc0, 0x71, 0x00]);
+ // var expected = (((buffer[2] & 0x3F) << 8) | buffer[1]);
+ // var got = buffer.readBits(14, 8);
+
+ // expect(got).to.equal(expected);
+ // expect(got).to.equal(122);
+ // expect(buffer.readBits(14, 22)).to.equal(455);
+ // });
+});

0 comments on commit 625f2e9

Please sign in to comment.