Home

Awesome

UTIF.js

A small, fast and advanced TIFF / EXIF (+ DNG, CR2, NEF and other TIFF-ish files) decoder and encoder. It is the main TIFF library for Photopea image editor. Try to open your TIFF file with Photopea to see, if UTIF.js can parse it.

For RAW files, UTIF.js only decodes raw sensor data (and JPG previews, if there are any). It does not convert the raw data into a displayable image (RGBA). Such conversion is complex and out of scope of this library.

Installation

Download and include the UTIF.js file in your code.

UTIF.decode(buffer)

UTIF.decodeImage(buffer, ifd)

TIFF files may have various number of channels and various color depth. The interpretation of data depends on many tags (see the TIFF 6 specification). The following function converts any TIFF image into a 8-bit RGBA image.

UTIF.toRGBA8(ifd)

Example

function imgLoaded(e) {
  var ifds = UTIF.decode(e.target.response);
  UTIF.decodeImage(e.target.response, ifds[0])
  var rgba  = UTIF.toRGBA8(ifds[0]);  // Uint8Array with RGBA pixels
  console.log(ifds[0].width, ifds[0].height, ifds[0]);
}

var xhr = new XMLHttpRequest();
xhr.open("GET", "my_image.tif");
xhr.responseType = "arraybuffer";
xhr.onload = imgLoaded;   xhr.send();

Use TIFF images in HTML

If you are not a programmer, you can use TIFF images directly inside the <img> element of HTML. Then, it is enough to call UTIF.replaceIMG() once at some point.

UTIF.replaceIMG()

<body onload="UTIF.replaceIMG()">
...
<img src="image.tif" />  <img src="dog.tif" /> ...

And UTIF.js will do the rest. Internally, the "src" attribute of the image will be replaced with a new URI of the image (base64-encoded PNG). Note, that you can also insert DNG, CR2, NEF and other raw images into HTML this way.

Encoding TIFF images

You should not save images into TIFF format in the 21st century. Save them as PNG instead (e.g. using UPNG.js). If you still want to use TIFF format for some reason, here it is.

UTIF.encodeImage(rgba, w, h, metadata)

UTIF.encode(ifds)