Skip to content

Commit

Permalink
add image size to annotations and canvases
Browse files Browse the repository at this point in the history
  • Loading branch information
edsilv committed Sep 29, 2018
1 parent a92bb1c commit a3731df
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 11 deletions.
27 changes: 20 additions & 7 deletions Canvas.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { basename, dirname, extname, join } = require('path');
const annotationBoilerplate = require('./boilerplate/annotation');
const imageServiceBoilerplate = require('./boilerplate/imageservice');
const chalk = require('chalk');
const config = require('./config');
const imageServiceBoilerplate = require('./boilerplate/imageservice');
const Jimp = require("jimp");
const urljoin = require('url-join');
import { Directory } from './Directory';
import { Motivations } from './Motivations';
Expand Down Expand Up @@ -196,13 +197,13 @@ export class Canvas {
return Utils.compare(a, b);
});

this._annotateFiles(canvasJson, paintableFiles);
await this._annotateFiles(canvasJson, paintableFiles);
}

} else {
// a file was passed (not a directory starting with an underscore)
// therefore, just annotate that file onto the canvas.
this._annotateFiles(canvasJson, [this.filePath]);
await this._annotateFiles(canvasJson, [this.filePath]);
}

if (!canvasJson.items[0].items.length) {
Expand All @@ -213,10 +214,10 @@ export class Canvas {
await Utils.getThumbnail(this.canvasJson, this.directory, this.directoryPath);
}

private _annotateFiles(canvasJson: any, files: string[]): void {
private async _annotateFiles(canvasJson: any, files: string[]): Promise<void> {

files.forEach((file: string) => {

await Promise.all(files.map(async (file: string) => {
file = Utils.normaliseFilePath(file);
const extName: string = extname(file);

Expand Down Expand Up @@ -245,8 +246,20 @@ export class Canvas {
annotationJson.body.format = defaultPaintingExtension.format;
annotationJson.body.label = Utils.getLabel(this.infoYml.label);
canvasJson.items[0].items.push(annotationJson);

// if it's an image, get the width and height and add to the annotation body and canvas
if (defaultPaintingExtension.type.toLowerCase() === Types.IMAGE) {
const image: any = await Jimp.read(file);
const width: number = image.bitmap.width;
const height: number = image.bitmap.height;
canvasJson.width = Math.max(canvasJson.width || 0, width);
canvasJson.height = Math.max(canvasJson.height || 0, height);
annotationJson.body.width = width;
annotationJson.body.height = height;
}
}
});

}));
}

private async _getInfo(): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "biiif",
"version": "0.3.11",
"version": "0.3.12",
"description": "A CLI to build IIIF collections",
"main": "index.js",
"repository": {
Expand Down
53 changes: 53 additions & 0 deletions test/tests/image-dimensions-manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const common = require("../common");
const assert = common.assert;
const build = common.build;
const Utils = common.Utils;

let manifestJson, canvasJson, annotationPage, annotation, annotationBody;
const manifest = '/image-dimensions-manifest';
const manifestUrl = 'http://test.com/image-dimensions-manifest';

it('can build manifest', async () => {
assert(await Utils.fileExists(manifest));
return build(manifest, manifestUrl, true);
}).timeout(1000); // should take less than a second

it('can find manifest index.json', async () => {
const file = '/image-dimensions-manifest/index.json';
assert(await Utils.fileExists(file));
manifestJson = await Utils.readJson(file);
});

it('can find canvas', async () => {
canvasJson = manifestJson.items[0];
assert(canvasJson);
});

it('has correct canvas id', async () => {
assert(canvasJson.id === manifestUrl + '/index.json/canvas/0');
});

it('has correct canvas width and height', async () => {
assert(canvasJson.width === 582);
assert(canvasJson.height === 328);
});

it('has an annotation page', async () => {
annotationPage = canvasJson.items[0];
assert(annotationPage);
});

it('has annotation', async () => {
annotation = annotationPage.items[0];
assert(annotation);
});

it('has an annotation body', async () => {
annotationBody = annotation.body;
assert(annotationBody);
});

it('has correct annotation width and height', async () => {
assert(annotationBody.width === 582);
assert(annotationBody.height === 328);
});
2 changes: 1 addition & 1 deletion test/tests/sort-canvases-numeric-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const manifestUrl = 'http://test.com/sort-canvases-numeric-manifest';
it('can build sort canvases numeric manifest', async () => {
assert(await Utils.fileExists(manifest));
return build(manifest, manifestUrl);
}).timeout(1000); // should take less than a second
}).timeout(2000); // should take less than two seconds

it('can find manifest index.json', async () => {
const file = '/sort-canvases-numeric-manifest/index.json';
Expand Down
2 changes: 1 addition & 1 deletion test/tests/sort-files-numeric-manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const manifestUrl = 'http://test.com/sort-files-numeric-manifest';
it('can build sort files numeric manifest', async () => {
assert(await Utils.fileExists(manifest));
return build(manifest, manifestUrl);
}).timeout(1000); // should take less than a second
}).timeout(2000); // should take less than two seconds

it('can find manifest index.json', async () => {
const file = '/sort-files-numeric-manifest/index.json';
Expand Down
8 changes: 7 additions & 1 deletion test/top.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ before(async () => {
'_page-2': {
'file.jpg': new Buffer(require('./fixtures/cat-jpg'))
}
},
'/image-dimensions-manifest': {
'_page-1': {
'file.jpg': new Buffer(require('./fixtures/cat-jpg'))
}
}
});
})
Expand Down Expand Up @@ -361,4 +366,5 @@ importTest('dat-gateway', './tests/dat-gateway');
importTest('canvas-with-dimensions-manifest', './tests/canvas-with-dimensions-manifest');
importTest('canvas-with-presentation-3-image-service-manifest', './tests/canvas-with-presentation-3-image-service-manifest');
importTest('behavior-paged-manifest', './tests/behavior-paged-manifest');
importTest('multiple-behavior-manifest', './tests/multiple-behavior-manifest');
importTest('multiple-behavior-manifest', './tests/multiple-behavior-manifest');
importTest('image-dimensions-manifest', './tests/image-dimensions-manifest');

0 comments on commit a3731df

Please sign in to comment.