Skip to content

Commit

Permalink
little-endian tiff suport
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Oct 11, 2013
1 parent 497d3f1 commit 8a2826b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var libpath = process.env.TEST_COV ? '../lib-cov/' : '../lib/';
var detector = require(libpath + 'detector');

var handlers = {};
var types = ['png', 'gif', 'bmp', 'psd', 'jpg'];
var types = ['png', 'gif', 'bmp', 'psd', 'jpg', 'tiff'];

// load all available handlers
types.forEach(function (type) {
Expand Down
69 changes: 69 additions & 0 deletions lib/types/tiff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// based on http://www.compix.com/fileformattif.htm
// TODO: support big-endian as well

var fs = require('fs');

module.exports = function (buffer, filepath) {

if (!filepath) {
throw new Error('Tiff can\'t support buffers, pass file instead');
}

var signature = buffer.toString('ascii', 0, 2);

var read;
// if ('II' === signature) {
// read = [
// buffer.readUInt16LE,
// buffer.readUInt32LE
// ];
// } else { //if ('MM' === signature) {
// read = [
// buffer.readUInt16BE,
// buffer.readUInt32BE
// ];
// }

// TODO: check if other versions exist
var version = buffer.readUInt16LE(2);
if (version !== 42) {
throw new TypeError('only version 42 Tiff images allowed');
}

var ifdOffset = buffer.readUInt32LE(4);

// read only till the end of the file
var bufferSize = 1024;
var fileSize = fs.statSync(filepath).size;
if (ifdOffset + bufferSize > fileSize) {
bufferSize = fileSize - ifdOffset - 10;
}

// populate the buffer
var endBuffer = new Buffer(bufferSize);
var descriptor = fs.openSync(filepath, 'r');
fs.readSync(descriptor, endBuffer, 0, bufferSize, ifdOffset);

var ifdLength = endBuffer.readUInt16LE(0);
var ifdBuffer = endBuffer.slice(2);

var tag, width, height;
while (ifdBuffer.length) {
tag = ifdBuffer.readUInt16LE(0);
if (tag === 256) {
width = ifdBuffer.readUInt32LE(8);
} else if (tag === 257) {
height = ifdBuffer.readUInt32LE(8);
}
if (tag === 0 || (width && height)) {
break;
} else {
ifdBuffer = ifdBuffer.slice(12);
}
}

return {
'width': width,
'height': height
};
};
16 changes: 12 additions & 4 deletions specs/all.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('Valid images', function () {

describe(file, function() {

var bufferDimensions, asyncDimensions;
var type, bufferDimensions, asyncDimensions;
var bufferSize = 8192;

beforeEach(function (done) {
Expand All @@ -46,7 +46,12 @@ describe('Valid images', function () {
var filepath = path.resolve(file);
var descriptor = fs.openSync(filepath, 'r');
fs.readSync(descriptor, buffer, 0, bufferSize, 0);
bufferDimensions = imageSize(buffer);
type = detector(buffer);

// tiff cannot support buffers, unless the buffer contains the entire file
if (type !== 'tiff') {
bufferDimensions = imageSize(buffer);
}

imageSize(file, function (err, _dim) {
asyncDimensions = _dim;
Expand All @@ -58,8 +63,11 @@ describe('Valid images', function () {
var expected = sizes[file] || sizes.default;
expect(asyncDimensions.width).to.be(expected.width);
expect(asyncDimensions.height).to.be(expected.height);
expect(bufferDimensions.width).to.be(expected.width);
expect(bufferDimensions.height).to.be(expected.height);

if (type !== 'tiff') {
expect(bufferDimensions.width).to.be(expected.width);
expect(bufferDimensions.height).to.be(expected.height);
}
});
});
});
Expand Down
File renamed without changes.

0 comments on commit 8a2826b

Please sign in to comment.