JSPM

decode-tiff

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 200474
  • Score
    100M100P100Q164583F
  • License MIT

Lightweight tiff decoder

Package Exports

  • decode-tiff

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (decode-tiff) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

decode-tiff

npm version License: MIT

⚡ A lightweight pure JavaScript TIFF decoder. 🎨

How to use it

Node.js

npm i tiff-decode

The following example reads .tiff and converts to .png file using pngjs.

const { decode } = require("decode-tiff");

const { PNG } = require("pngjs");
const fs = require("fs");

const { width, height, data } = decode(fs.readFileSync(__dirname + "/lena_color.tiff"));

const png =  new PNG({ width, height });
png.data = data;
fs.writeFileSync(__dirname + "/lena.png", PNG.sync.write(png));

Browser

This example show metadata of the dropped file. Working demonstration is here.

const { decode } = require("decode-tiff");

const elm = document.getElementById("drop");

elm.addEventListener("dragenter", e => e.preventDefault());
elm.addEventListener("dragover", e => e.preventDefault());
elm.addEventListener("drop", (e) => {
  e.preventDefault();
  const file = e.dataTransfer.files[0];
  const reader = new FileReader();
  reader.addEventListener("load", (e) => {
    const arrayBuffer = e.target.result;
    const { width, height, ifdEntries } = decode(arrayBuffer);
    const IFDs = JSON.stringify({ width, height, ifdEntries }, null, 2);
    elm.innerHTML = `
      <pre>${IFDs}</pre>
    `;
  });
  reader.readAsArrayBuffer(file);
});

API

decode(buffer: ArrayBuffer | Buffer, options?: { singlePage?: boolean }): TiffImage | TiffImage[]

  • params
    • buffer - Required - Buffer of the target TIFF image. Node.js Buffer and ECMA Script's ArrayBuffer are acceptable.
    • options.singlePage - Optional (default: true) - If true, this function returns a single TiffImage object. If the input has 2 or more pages, return value will be the first page.
  • returns
    • TiffImage - An object.
    • TiffImage.width - number - Width of the input image.
    • TiffImage.height - number - Height of the input image.
    • TiffImage.data - Uint8Array - Image pixel data. Every pixel consists 4 bytes: R, G, B, A (opacity)
    • TiffImage.ifdEntries - {[key: string]: Array} - Each IFD entries of the input image.

Compatibility

  • Byte Order
    • Little endian
    • Big endian
  • Color resolusion
    • 32bit Full Colors
    • 24bit Full Colors
    • 8bit Gray scale
    • Color Pallet
    • Bilevel(white)
    • Bilevel(black)
  • Compression
    • No Compression
    • LZW Compression
    • Packbits

License

MIT. See LICENSE.txt.