npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

iiif-processor

v5.0.0

Published

IIIF 2.1 & 3.0 Image API modules for NodeJS

Downloads

706

Readme

iiif-processor

Build Status Maintainability Test Coverage

This module provides a full-featured image processor supporting the IIIF Image API versions 2.1 and 3.0. It covers only the image processing pipeline itself, leaving input and output to the caller.

Install with NPM

npm install iiif-processor --save

Usage

const IIIF = require('iiif-processor');

const processor = new IIIF.Processor(url, streamResolver, opts);
  • url (string, required) - the URL of the IIIF resource to process
  • streamResolver (function, required) – a callback function that returns a readable image stream for a given request (see below)
  • opts:
    • dimensionFunction (function) – a callback function that returns the image dimensions for a given request (see below)
    • max (object) – optional maximum size constraints of an image that can be returned
      • width (integer) - the maximum pixel width of the returned image
      • height (integer) - the maximum pixel height of the returned image
      • area (integer) - the maximum total number of pixels in the returned image
    • includeMetadata (boolean) – if true, all metadata from the source image will be copied to the result
    • density (integer) – the pixel density to be included in the result image in pixels per inch
      • This has no effect whatsoever on the size of the image that gets returned; it's simply for convenience when using the resulting image in software that calculates a default print size based on the height, width, and density
    • pathPrefix (string) – the default prefix that precedes the id part of the URL path (default: /iiif/2/)
    • version (number) – the major version (2 or 3) of the IIIF Image API to use (default: inferred from /iiif/{version}/)

Examples

Full Self-Contained Application

See the TinyIIIF example.

Stream Resolver

The calling function must supply the processor with a Stream Resolver callback function, which takes information about the request (id and baseUrl) and returns an open Readable Stream from which the source image can be read.

Pairtree File Source

function streamResolver({ id, baseUrl }) {
  let imagePath = '/path/to/image/root/' + id.match(/.{1,2}/g).join('/') + '/image.tif';
  return fs.createReadStream(imagePath);
}

The Stream Resolver can also take an async callback as its second parameter, in which case it should return the value of applying the callback to the stream. This allows the function to do its own cleanup.

Amazon S3 Bucket Source

const AWS = require('aws-sdk');

async function streamResolver({ id, baseUrl }, callback) {
  let s3 = new AWS.S3();
  let key = id + '.tif';
  let request = s3.getObject({ Bucket: 'my-tiff-bucket', Key: key });
  let stream = request.createReadStream();
  try {
    return await callback(stream);
  } finally {
    stream.end().destroy();
    request.abort();
  }
}

Dimension Function

The calling function can also supply the processor with an optional Dimension callback that takes information about the request (id and baseUrl) and returns the dimensions of the source image. This allows for caching dimensions and avoiding an expensive image request.

The function should return either:

  • a {width: w, height: h} object indicating the dimensions of the source image
  • an array of {width: w, height: h} objects indicating the dimensions of all of the pages available within the source image, if it is a multi-resolution image (e.g., a pyramidal TIFF), e.g.:
    [
      { width: 14499, height: 12069 },
      { width: 7249, height: 6034 },
      { width: 3624, height: 3017 },
      { width: 1812, height: 1508 },
      { width: 906, height: 754 },
      { width: 453, height: 377 },
      { width: 226, height: 188 }
    ]

Providing the dimensions of all available pages allows the processor to choose the most efficient starting image for the size requested.

async function dimensionFunction({ id, baseUrl }) {
  let dimensions = lookDimensionsUpInDatabase(id);
  return { width: dimensions.width, height: dimensions.height };
}

Processing

Promise

const IIIF = require('iiif-processor');

let url = 'http://iiif.example.com/iiif/2/abcdefgh/full/400,/0/default.jpg'
let processor = new IIIF.Processor(url, streamResolver, { dimensionFunction });
processor.execute()
  .then(result => handleResult(result))
  .catch(err => handleError(err));

Async/Await

const IIIF = require('iiif-processor');

let url = 'http://iiif.example.com/iiif/2/abcdefgh/full/400,/0/default.jpg'
let processor = new IIIF.Processor(url, streamResolver, { dimensionFunction });
try {
  let result = await processor.execute();
  return result;
} catch (err) {
  handleError(err);
}

id / baseUrl

The stream resolver and dimensions function functions both accept an object with id and baseUrl specified.

For instance, for the request:

https://example.org/iiif/assets/42562145-0998-4b67-bab0-6028328f8319.png/10,20,30,40/pct:50/45/default.png

The id parameter is 42562145-0998-4b67-bab0-6028328f8319.png and the baseUrl is https://example.org/iiif/assets.

Contributing

Contributions are welcome in the form of bug reports, suggestions, pull requests, and/or documentation.

If you're working on a PR for this project, create a feature branch off of main.

This project uses the debug library for selective debugging output. To view all IIIF-related debug messages, set the environment variable DEBUG=iiif-processor:*. To view just the main or transformer contexts, set DEBUG=iiif-processor:main or DEBUG=iiif-processor:transform.

This repository follows the Samvera Community Code of Conduct and language recommendations. Please do not create a branch called master for this repository or as part of your pull request; the branch will either need to be removed or renamed before it can be considered for inclusion in the code base and history of this repository.

License

node-iiif is available under the Apache 2.0 license.