liup a01a5aa773 | 3 hafta önce | |
---|---|---|
.. | ||
LICENSE | 3 hafta önce | |
README.md | 3 hafta önce | |
UTIF.js | 3 hafta önce | |
package.json | 3 hafta önce |
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.
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 dataUTIF.decodeImages(buffer, ifds)
buffer
: ArrayBuffer containing TIFF or EXIF dataifds
: the output of UTIF.decode()width
: the width of the imageheight
: the height of the imagedata
: decompressed pixel data of the imageTIFF 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())function imgLoaded(e) {
var ifds = UTIF.decode(e.target.response);
UTIF.decodeImages(e.target.response, ifds)
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();
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.
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)UTIF.encode(ifds)
ifds
: array of IFDs (image file directories). An IFD is a JS object with properties “tXYZ” (where XYZ are TIFF tags)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.