aboutsummaryrefslogtreecommitdiff
path: root/src/conversion/czi/pyramidWorker.ts
blob: 19acbf9322e1057bb88354e85e76b6244afdc944 (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
import { CZIHeightMap, CZITile, CZITileRequest } from '../types/customPyramidIndex';
import { TileBounds } from '../types/helperCZITypes';
import * as sharp from 'sharp';
import { isTileRelated } from './tileExtraction';
import { uuid } from '../../uuid'

const findRelatedTiles: Function = function(plane: CZIHeightMap, desired: TileBounds): CZITile[][] {

	const relatedTiles: CZITile[][] = [];
	for (const planeTileRow of plane.plane) {
		let tileRow: CZITile[] = [];
		let foundTiles: boolean = false;

		for (const tile of planeTileRow) {

			if (isTileRelated(
                new TileBounds(
                    tile.x_offset,
                    tile.x_offset + tile.width,
                    tile.y_offset,
                    tile.y_offset + tile.height
                ), desired)
            ) {
				tileRow.push(tile);
				foundTiles = true;
			}
		}
		if (foundTiles) {
			relatedTiles.push(tileRow);
		}
	}
	return relatedTiles;
};

const getFinalTile: Function = async function(imageDir: string, imageTier: CZIHeightMap, cVal: string, desiredRegion: TileBounds): Promise<CZITileRequest> {

	console.error("Has the calling function checked that the image has completed processing in WholeCZIHierarchy??");

	let involvedTiles: string = '';
	for (const row of imageTier.plane) {
		for (const tile of row) {
			involvedTiles += `${imageDir}/data/${tile.file} `
		}
	}

	let outputFileName: string =`${imageDir}/tmp/${uuid.generate()}.png`
	require('shelljs').exec(`vips arrayjoin "${involvedTiles}" ${outputFileName} --across ${imageTier.plane[0].length}`)

	let extractionRegion = new TileBounds(
		desiredRegion.left - imageTier.plane[0][0].width,
		desiredRegion.right - imageTier.plane[0][0].width,
		desiredRegion.top - imageTier.plane[0][0].height,
		desiredRegion.bottom - imageTier.plane[0][0].height
	);
	extractionRegion.scaleDown(imageTier.zoom_level);

	await sharp(outputFileName)
		.extract({
			top: extractionRegion.top,
			left: extractionRegion.left,
			width: extractionRegion.width(),
			height: extractionRegion.height()
		})
		.toFile(outputFileName);

	return {
		dimensions: new TileBounds(
							0, extractionRegion.width(),
							0, extractionRegion.height()
						),
		original_bounds: desiredRegion,
		zoom_level: imageTier.zoom_level,
		c_value: cVal,
		file_path: outputFileName
	};
}