aboutsummaryrefslogtreecommitdiff
path: root/src/conversion/leica/scn.ts
blob: a7d4573bea47717e6f87e96ef2259f1a26b36b92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import * as fs from 'fs';
import * as sharp from 'sharp';
// tslint:disable-next-line:no-duplicate-imports
import { SharpInstance } from 'sharp';
import { SupportedViews, TileBounds, writeJSONToFile, checkForOutputDirectories, execpaths } from '../types/helpers';
import { uuid } from '../../uuid'
import { logger, Logger } from '../../logger';
let log: Logger;

// CHANGE ME FOR TESTING!!!!!
const input: string = "diseessse1.scn"
const baseDirname: string = '/cs/scratch/cjd24/tifers/';



// Various constants for placing files and defining tiles
const extensionBinariesDir: string = __dirname + `/../../../ext/bin/`;

const tileOverlap: number = 0; // Overlap is only half implemented
const tileSize: number = 512;


export const crunchLeica: (original: string, space:string) => Promise<void> = async (original: string, space:string): Promise<void> => {
	log = logger.for({component: "SCN Crunch", targetFile: original});
	try {
		let leica: SharpInstance = sharp(original).png();

		checkForOutputDirectories([space]);
		log.info("Creating new output directory for LECIA > DZI @ " + space);

		let meta: sharp.Metadata = await leica.metadata();

		let width: number = -1;
		let height: number = -1;
		if (meta.width !== undefined) {
			width = meta.width;
		}
		if (meta.height !== undefined) {
			height = meta.height;
		}

		let supportedViews: SupportedViews = {
			scalable_image: {
				width: width,
				height: height,
				channels:
					[{
						channel_id: '-1',
						channel_name: '-1',
						metadata: {}
					}],
				metadata: {
					tile_size: tileSize,
					max_zoom: "Not yet supported in this version of convertor",
					tile_overlap: tileOverlap,
					exif: meta.exif
				}
			}
		}

		let channels: any = [];
		if (meta.channels !== undefined) {
			for (let index: number = 0; index < meta.channels; index++) {
				channels.push({
					channel_id: `${index}`,
					channel_name: `${meta.space}`,
					metadata: {}
				})
			}
			if (!supportedViews.scalable_image) {
				log.error(`Target File is not a scalable_image`)
				throw new Error("Not a scalable image");
			}
			supportedViews.scalable_image.channels = channels;
		}

		writeJSONToFile(`${space}/supported_views.json`, supportedViews)
		log.info("Wrote supported_views object in: " + space);

		await sharp(baseDirname + input)
			.png()
			.tile({
				size: tileSize,
				overlap: tileOverlap,
				layout: "dz"
			})
			.toFile(`${space}/output`);
		log.success(`Finalised output of ${input} as .dzi`);
	}
	catch (err) {
		log.failure(`Lecia conversion failed with error: ${err}`)
	}
};


// crunchLeica().then((ret:string) => console.log(ret));