What is utif?
The utif npm package is a library for parsing and encoding TIFF (Tagged Image File Format) images. It allows users to decode TIFF files into a format that can be used in web applications and to encode image data into TIFF format.
What are utif's main functionalities?
Decoding TIFF images
This feature allows you to decode TIFF images into a usable format. The code sample reads a TIFF file, decodes it, and converts it to an RGBA buffer.
const UTIF = require('utif');
const fs = require('fs');
const tiffData = fs.readFileSync('image.tiff');
const ifds = UTIF.decode(tiffData);
UTIF.decodeImage(tiffData, ifds[0]);
const rgba = UTIF.toRGBA8(ifds[0]);
// rgba now contains a Uint8Array of the image data
Encoding images to TIFF
This feature allows you to encode image data into a TIFF file. The code sample creates a new TIFF file from a given RGBA buffer.
const UTIF = require('utif');
const fs = require('fs');
const width = 800, height = 600;
const rgba = new Uint8Array(width * height * 4); // Replace with actual image data
const tiff = UTIF.encodeImage(rgba, width, height);
fs.writeFileSync('output.tiff', new Buffer(tiff));
Other packages similar to utif
sharp
Sharp is a high-performance Node.js image processing library that can convert large images in common formats to smaller, web-friendly JPEG, PNG, WebP, GIF, AVIF, and TIFF images. It is faster than utif as it's built on libvips which is a fast image processing library, but it does much more than just handling TIFF images.
image-size
This package is used to get dimensions of any image file without fully decoding it. It supports a variety of formats, including TIFF. It is not a full-fledged image processing library like utif but is useful for quickly obtaining image metadata.
jimp
Jimp is an image processing library for Node.js that allows for editing and manipulation of images in various formats, including TIFF. It is less focused on TIFF specifically compared to utif and offers a broader range of image manipulation features.
tiff
The 'tiff' npm package is a pure JavaScript implementation for TIFF encoding and decoding. It is similar to utif in functionality but may differ in API, performance, and additional features.
UTIF.js
A small, fast and advanced TIFF / EXIF (+ DNG 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.
- Supports Black & White, Grayscale, RGB and Paletted images
- Supports Fax 3 and Fax 4 (CCITT), JPEG, LZW, PackBits and other compressions (1,3,4,5,6,7,8,32773,32809)
- E.g. this 8 MPix image with Fax 4 compression is just 56 kB ( Open in Photopea )
Installation
Download and include the UTIF.js
file in your code. If you're in NodeJS or otherwise using NPM, run:
npm install utif
UTIF.decode(buffer)
buffer
: ArrayBuffer containing TIFF or EXIF data- returns an array of "IFDs" (image file directories). Each IFD is an object, keys are "tXYZ" (XYZ is a TIFF tag number), values are values of these tags. You can get the the dimension (and other properties, "metadata") of the image without decompressing pixel data.
UTIF.decodeImages(buffer, ifds)
buffer
: ArrayBuffer containing TIFF or EXIF dataifds
: the output of UTIF.decode()- loops through each IFD. If there is an image inside it, it is decoded and three new properties are added to the IFD:
-
width
: the width of the image
-
height
: the height of the image
-
data
: decompressed pixel data of the image
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)
ifd
: image file directory (element of "ifds" returned by UTIF.decode(), processed by UTIF.decodeImages())- returns Uint8Array of the image in RGBA format, 8 bits per channel (ready to use in context2d.putImageData() etc.)
Example
function imgLoaded(e) {
var ifds = UTIF.decode(e.target.response);
UTIF.decodeImages(e.target.response, ifds)
var rgba = UTIF.toRGBA8(ifds[0]);
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, an Image elements will be replaced by a Canvas elements. The attributes "id", "class" and "style" will be copied from the original Image to the new Canvas. Use CSS to style such images.
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)
rgba
: ArrayBuffer containing RGBA pixel dataw
: image widthh
: image heightmetadata
[optional]: IFD object (see below)- returns ArrayBuffer of the binary TIFF file. No compression right now.
UTIF.encode(ifds)
ifds
: array of IFDs (image file directories). An IFD is a JS object with properties "tXYZ" (where XYZ are TIFF tags)- returns ArrayBuffer of binary data. You can use it to encode EXIF data.
Dependencies
TIFF format sometimes uses Inflate algorithm for compression (but it is quite rare). Right now, UTIF.js calls Pako.js for the Inflate method.
TIFF format sometimes uses JPEG compression (but it is quite rare). Right now, UTIF.js calls "JpegDecoder" constructor, which comes from pdf.js. You can find it "separated" from pdf.js in libraries such as jpg.js.