Skip to content

Commit

Permalink
[#1] add tiff support
Browse files Browse the repository at this point in the history
  • Loading branch information
Quramy committed Jul 18, 2017
1 parent 5324027 commit 8f5a122
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ The following codecs are available for input image files.

- [x] png
- [x] jpeg
- [x] tiff (limited. See https://github.com/Quramy/decode-tiff#compatibility )
- [ ] bmp

`imgDiff` detects the input image format from it's extension name. For example, if the input file name ends with ".jpeg", `imgDiff` attempts to decode in JPEG way regardless of the actual file format.
Expand Down
14 changes: 14 additions & 0 deletions lib/decode-tiff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fs = require("fs");
const { decode } = require("decode-tiff");

function decodeTiff(filename) {
return new Promise((resolve, reject) => {
fs.readFile(filename, (err, rawBuffer) => {
if (err) return reject(err);
const tiffData = decode(rawBuffer);
resolve(tiffData);
});
});
}

module.exports = decodeTiff;
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const pixelmatch = require("pixelmatch");
const { PNG } = require("pngjs");
const decodePng = require("./decode-png");
const decodeJpeg = require("./decode-jpeg");
const decodeTiff = require("./decode-tiff");
const expand = require("./expand");

const extensionDecoderMap = { };
Expand All @@ -28,6 +29,7 @@ function decode(filename) {

registerDecoder(["png"], decodePng);
registerDecoder(["jpg", "jpeg"], decodeJpeg);
registerDecoder(["tiff"], decodeTiff);

function compare(img1, img2, diffFilename, options) {
const { dataList, width, height } = expand(img1, img2);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"homepage": "https://github.com/reg-viz/img-diff-js#readme",
"dependencies": {
"decode-tiff": "^0.2.0",
"jpeg-js": "^0.3.3",
"pixelmatch": "^4.0.2",
"pngjs": "^3.2.0"
Expand Down
11 changes: 11 additions & 0 deletions test/decode-tiff.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import test from "ava";
import path from "path";
const decodeTiff = require("../lib/decode-tiff");

test("decode tiff file", async t => {
const file = path.resolve(__dirname, "images/actual.tiff");
const jpeg = await decodeTiff(file);
t.is(typeof jpeg.width, "number");
t.is(typeof jpeg.height, "number");
t.is(jpeg.data.length, jpeg.width * jpeg.height * 4);
});
Binary file added test/images/actual.tiff
Binary file not shown.
Binary file added test/images/expected.tiff
Binary file not shown.
11 changes: 11 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ test("compare with 2 files whose dimension are different", async t => {
t.truthy(fs.statSync(path.resolve(__dirname, "images/diff_generated.wide.png")));
});

test("compare with 2 jpeg files", async t => {
const diffFilename = path.resolve(__dirname, "images/diff_generated.tiff.png");
rimraf.sync(diffFilename);
const { width, height } = await imgDiff({
diffFilename,
actualFilename: path.resolve(__dirname, "images/actual.tiff"),
expectedFilename: path.resolve(__dirname, "images/expected.tiff"),
});
t.truthy(fs.statSync(path.resolve(__dirname, "images/diff_generated.tiff.png")));
});

test("compare with 2 jpeg files", async t => {
const diffFilename = path.resolve(__dirname, "images/diff_generated.jpg.png");
rimraf.sync(diffFilename);
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,10 @@ decamelize@^1.1.2:
version "1.2.0"
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"

decode-tiff@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/decode-tiff/-/decode-tiff-0.2.0.tgz#540b2949efedee689e3e20224cc04af6b82ad88a"

deep-equal@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/deep-equal/-/deep-equal-1.0.1.tgz#f5d260292b660e084eff4cdbc9f08ad3247448b5"
Expand Down

0 comments on commit 8f5a122

Please sign in to comment.