aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/conversion/czi/czi.ts780
-rwxr-xr-xsrc/conversion/czi/cziConvert.sh5
-rw-r--r--src/conversion/czi/pyramidWorker.ts179
-rw-r--r--src/conversion/czi/tileExtraction.ts129
-rw-r--r--src/conversion/types/customPyramidIndex.ts27
-rw-r--r--src/conversion/types/cziBinaryTypings.ts23
-rw-r--r--src/conversion/types/helperCZITypes.ts39
7 files changed, 953 insertions, 229 deletions
diff --git a/src/conversion/czi/czi.ts b/src/conversion/czi/czi.ts
index addf43e..ea22c0a 100644
--- a/src/conversion/czi/czi.ts
+++ b/src/conversion/czi/czi.ts
@@ -1,95 +1,38 @@
-
+//imports required to make the conversion
import * as fs from 'fs';
+import * as parsexml from 'xml-parser';
import { logger } from '../../logger';
import * as sharp from 'sharp';
-import * as parsexml from 'xml-parser';
import { SharpInstance } from 'sharp';
-
-const extractionDirectory: string = '/cs/scratch/cjd24/0701-extraction/';
-const outputImageDirectory: string = '/cs/scratch/cjd24/0701-extraction-processed2/'
-const tileOverlap: number = 0;
+import { CZITile, CZIHeightMap, WholeCZIHierarchy } from '../types/customPyramidIndex';
+import { Dimension, DirectoryEntry, SubBlock, Segment } from '../types/cziBinaryTypings';
+import { SupportedViews, TileBounds } from '../types/helperCZITypes';
+import { isTileRelated, regionToExtract } from './tileExtraction';
+
+//various constants for placing files and defining tiles
+const baseDirname: string = '/cs/scratch/cjd24/0701-extraction';
+const extractionDirectory: string = `${baseDirname}/`;
+const outputImageData: string = `${baseDirname}-processed/`;
+const outputImageDirectory: string = `${baseDirname}-processed/data/`;
+const tileOverlap: number = 0; // Overlap is only half implemented
const tileSize: number = 1024;
const maxZoom: number = 64;
-interface Dimension {
- Dimension: string;
- Start: number;
- Size: number;
-}
-interface DirectoryEntry {
- DimensionCount: number;
- DimensionEntries: Dimension[];
-}
-interface SubBlock {
- DirectoryEntry: DirectoryEntry;
- Data: string;
-}
-interface Segment {
- Id: string;
- UsedSize: number;
- Data: SubBlock;
-}
-class TileBounds {
- public left!: number;
- public right!: number;
- public top!: number;
- public bottom!: number;
- public constructor(leftBound: number, rightBound: number, topBound: number, bottomBound: number) {
- this.left = leftBound;
- this.right = rightBound;
- this.top = topBound;
- this.bottom = bottomBound;
- }
-}
-interface SupportedViews {
- scalable_image: {
- width: number;
- height: number;
- channels: [
- {
- channel_id: string;
- channel_name: string;
- metadata?: {};
- }
- ];
- real_width?: {
- value: number;
- units: string;
- };
- real_height?: {
- value: number;
- units: string;
- };
- metadata?: {};
- }
-}
-interface CZITile {
- x_offset: number;
- y_offset: number;
- width: number;
- height: number;
- file: string;
-}
-interface CZIHeightMap {
- zoom_level: number,
- plane: CZITile[][];
-}
-interface WholeCZIHierarchy {
- c_values: [{
- channel_id: string,
- height_map: CZIHeightMap[];
- }];
-}
-
+/*
+ * Simple function to find the tile bounds version of an original tile in the base image
+ */
const getOriginalTileBounds: Function = function(originalTile: Segment): (TileBounds | null) {
const bounds: TileBounds = new TileBounds(-1, -1, -1, -1);
+ //loop over all dimensions to get the x and y
for (const dimension of originalTile.Data.DirectoryEntry.DimensionEntries) {
switch (dimension.Dimension) {
+ //If we have found x, then match the start and width
case 'X':
bounds.left = dimension.Start;
bounds.right = bounds.left + dimension.Size;
continue;
+ //If we have found y, then match the top and bottom
case 'Y':
bounds.top = dimension.Start;
bounds.bottom = bounds.top + dimension.Size;
@@ -99,74 +42,72 @@ const getOriginalTileBounds: Function = function(originalTile: Segment): (TileBo
}
}
+ //If one of the bounds was not created, then there must have been an error
if (bounds.left < 0 || bounds.right < 0 || bounds.top < 0 || bounds.bottom < 0) {
return null;
}
-
+ //Successful return
return bounds;
};
+/*
+ * Function used to find which of the original base tiles are
+ * overlapping with the area of the to-be-created tile
+ */
const findRelatedTiles: Function = function(activeSegments: Segment[], desired: TileBounds): Segment[] {
const relatedTiles: Segment[] = [];
+ //For all tiles within the region being checked
for (const baseTile of activeSegments) {
+ //find out the coordinates of the current base tile being checked
let origCoords: TileBounds | null;
-
if ((origCoords = getOriginalTileBounds(baseTile)) === null) {
throw new Error('There was a Sad Boi error');
}
- // Desired X on left is within current tile
- if (((desired.left >= origCoords.left) &&
- (desired.left < origCoords.right)) ||
-
- // Desired X on right is within the current tile
- ((desired.right > origCoords.left) &&
- (desired.right <= origCoords.right)) ||
-
- // Desired tile X overlaps base tile
- ((desired.left < origCoords.left) &&
- (desired.right > origCoords.right))) {
-
- // Desired Y on top is within current tile
- if (((desired.top >= origCoords.top) &&
- (desired.top < origCoords.bottom)) ||
-
- // Desired Y on right is within the current tile
- ((desired.bottom > origCoords.top) &&
- (desired.bottom <= origCoords.bottom)) ||
-
- // Desired tile Y overlaps whole base tile
- ((desired.top < origCoords.top) &&
- (desired.bottom > origCoords.bottom))) {
-
- relatedTiles.push(baseTile);
- }
+ if (isTileRelated(origCoords, desired)) {
+ relatedTiles.push(baseTile);
}
}
return relatedTiles;
};
+/*
+ * This gets the particular dimension from a segment given a segment and dimesnion ID
+ */
const getDimension: Function = function(segment: Segment, name: string): Dimension {
+
+ //loop over all dimensions within this segment
for (const dimension of segment.Data.DirectoryEntry.DimensionEntries) {
+ //If the current dimension is the one we are looking for then return it.
if (dimension.Dimension === name) {
return dimension;
}
}
+ //If we have gotten to here, then the dimension doesnt exist, so throw error.
throw new Error('Dimension: ' + name + " doesn't exist for segment: " + segment);
};
+/*
+ * This takes a selection of segments and ensures that they are ordered
+ * left to right, and top to bottom like the words in a book, so that the
+ * stiching process will take the correct slice from them all.
+ * (this is based on the pixel x-y coords of the segment.)
+ */
const orderSegments: Function = function(segments: Segment[]): Segment[][] {
const output: Segment[][] = [];
+ //So long as there is at least 2 segments to order
if (segments.length > 1) {
let xCoords: number[] = [];
let yCoords: number[] = [];
// Get all x/y's;
+ // However also ensure that all of the segments to be ordered, all come
+ // from the same value of 'C' dimension, otherwise there could be overlaps.
const initC: number = getDimension(segments[0], 'C').Start;
for (const seg of segments) {
if (getDimension(seg, 'C').Start !== initC) {
@@ -175,7 +116,7 @@ const orderSegments: Function = function(segments: Segment[]): Segment[][] {
xCoords.push(getDimension(seg, 'X').Start);
yCoords.push(getDimension(seg, 'Y').Start);
}
- // Filter to get Uniaue Values
+ // Filter to get Unique Values
xCoords = xCoords.filter(
(item: number, index: number, array: number[]) => array.indexOf(item) === index);
yCoords = yCoords.filter(
@@ -209,52 +150,43 @@ const orderSegments: Function = function(segments: Segment[]): Segment[][] {
// Return the result
return output;
- } else {
- output.push(segments);
- return output;
}
+
+ //There is only one segment, so no need to re-order.
+ return [segments];
};
+/*
+ * Takes a base tile, and the whole bounds of a new tile, and uses this
+ * to calculate the region within the base tile that is overlapping the new tile
+ * to be extracted.
+ */
const findRegionToExtract: Function = function(segment: Segment, desired: TileBounds): TileBounds {
- const baseBounds: TileBounds = getOriginalTileBounds(segment);
- let chunkToExtract: TileBounds = new TileBounds(-1, -1, -1, -1);
- if (desired.left <= baseBounds.left) {
- chunkToExtract.left = 0;
- } else {
- chunkToExtract.left = desired.left - baseBounds.left;
- }
-
- if (desired.right >= baseBounds.right) {
- chunkToExtract.right = baseBounds.right - baseBounds.left;
- } else {
- chunkToExtract.right = desired.right - baseBounds.left;
- }
-
- if (desired.top <= baseBounds.top) {
- chunkToExtract.top = 0;
- } else {
- chunkToExtract.top = desired.top - baseBounds.top;
- }
-
- if (desired.bottom >= baseBounds.bottom) {
- chunkToExtract.bottom = baseBounds.bottom - baseBounds.top;
- } else {
- chunkToExtract.bottom = desired.bottom - baseBounds.top;
- }
-
- return chunkToExtract;
+ //return the slice within the base tile for extraction
+ return regionToExtract(getOriginalTileBounds(segment), desired, 1);
};
+/*
+ * Take an ordered selection of segments and cookie cutter out a tile from this
+ * into a new image
+ */
const extractAndStitchChunks: Function = async function(segments: Segment[][], desiredRegion: TileBounds): Promise<SharpInstance> {
+ //create a buffer for the rows part of raw image data
let tileVerticalBuffer: Buffer[] = [];
+ //for all ros (y coords) in the image
for (const segRow of segments) {
+ //create a buffer for this row
let tileRow: Buffer[] = [];
let chunkToExtract: TileBounds = new TileBounds(-1, -1, -1, -1);
+
+ //For all of the columns within this row
for (const segCol of segRow) {
+ //find out the region of the base tile to be added to the new image
chunkToExtract = findRegionToExtract(segCol, desiredRegion);
+ //extract this section of image data into a sharp buffer
let data: Promise<Buffer> =
sharp(`${extractionDirectory}${segCol.Data.Data}`)
.extract({
@@ -265,134 +197,378 @@ const extractAndStitchChunks: Function = async function(segments: Segment[][], d
})
.toBuffer();
+ //push this data into the row buffer
tileRow.push(await data);
}
+ //prepare variables to squash all of the column data into one, for this row
let imageVerticalSlice: SharpInstance = sharp(tileRow[0]);
let bufferBound = findRegionToExtract(segRow[0], desiredRegion);
+ //check if the inital chunk needs to be extended to support the other columns
if ((bufferBound.right - bufferBound.left) < (tileSize - tileOverlap)) {
- imageVerticalSlice = await imageVerticalSlice.extend(
+ imageVerticalSlice = await imageVerticalSlice.background({r: 0, g: 0, b: 0, alpha: 0}).extend(
{top:0, left:0, bottom:0, right: (tileSize - (bufferBound.right - bufferBound.left))});
}
+ //for every column along this row, join it to the previous column data
for (let index = 1; index < segRow.length; index++) {
imageVerticalSlice = await imageVerticalSlice
.overlayWith(tileRow[index],
{top: 0, left: (bufferBound.right - bufferBound.left) + ((index -1) * tileSize)});
}
+ //add this horizontal data buffer to the vertical buffer
tileVerticalBuffer.push(await imageVerticalSlice.toBuffer());
}
+ //prepare variables used to combine all rows into one image
let finalImage: SharpInstance = sharp(tileVerticalBuffer[0]);
let bufferBound = findRegionToExtract(segments[0][0], desiredRegion);
+ //Check if the inital row needs to be extended to support further rows
if ((bufferBound.bottom - bufferBound.top) < (tileSize - tileOverlap)) {
- finalImage = await finalImage.extend(
+ finalImage = await finalImage.background({r: 0, g: 0, b: 0, alpha: 0}).extend(
{top:0, left:0, bottom: (tileSize - (bufferBound.bottom - bufferBound.top)), right: 0});
}
+ //For all further rows in the image, combine it with the previous rows' data
for (let index = 1; index < segments.length; index++) {
finalImage = finalImage.overlayWith(
tileVerticalBuffer[index],
{top: getDimension(segments[index -1][0], 'Y').Size, left: 0});
}
+ //return the finalised image.
return finalImage;
};
+/*
+ * Used to remove obtuse JSON data, where it is not needed in the SupportedView object
+ */
+const cleanSupportedViewObject: Function = function(obj: parsexml.Node[]): Object {
+ let newChild: any = {};
+ let hasAttr: boolean = false, hasChildren: boolean = false, hasContent: boolean = false;
+
+ //for all nodes in the json object
+ for (const node of obj) {
+
+ //Check if this node has attributes
+ if (node.attributes !== undefined && node.attributes !== null && Object.keys(node.attributes).length !== 0)
+ {hasAttr = true;} else {hasAttr = false;}
+ //Check for further children
+ if (node.children !== undefined && node.children.length > 0)
+ {hasChildren = true;} else {hasChildren = false;}
+ //Check for simple content object
+ if (node.content !== undefined && node.content !== null && node.content !== '')
+ {hasContent = true;} else {hasContent = false;}
+
+ //If the Node has further children, then deal with this
+ if (hasChildren) {
+ let soloChildren: boolean = true;
+
+ //Check if this node just contains children, or its own data too
+ newChild[node.name] = {}
+ if (hasAttr) {
+ //IF there are also attributes, then set this in the output
+ newChild[node.name].attributes = node.attributes;
+ soloChildren = false;
+ }
+ if (hasContent) {
+ //If there is also a 'content' then set this in the output
+ newChild[node.name].content = node.content;
+ soloChildren = false;
+ }
+ //If there are only chilren, then set this as this objects data
+ if (soloChildren) {
+ //recursively call this function to clean up sub-children too.
+ newChild[node.name] = cleanSupportedViewObject(node.children);
+ } else {
+ //Otherwise, if there is also data, then add a children object
+ newChild[node.name].children = cleanSupportedViewObject(node.children);
+ }
+ }
+
+ //If this node only has attributes, then add this
+ else if (hasAttr) {
+ newChild[node.name] = {};
+
+ //Check for content too, if so, add a discrete child for this.
+ if (hasContent) {
+ newChild[node.name].content = node.content;
+ }
+ //add the attributes as a sub child regardless,
+ // since it exists on this key.
+ newChild[node.name].attributes = node.attributes;
+ }
+
+ //If this node only has a content object then remove all other content
+ // and only attatch the content result as the value for this node key
+ else if (hasContent) {
+ newChild[node.name] = node.content;
+ }
+ }
+
+ return newChild;
+};
+/*
+ * Clean up all vales of C within the JSON for supported views
+ */
+const cleanSupportedViews: Function = function(view: SupportedViews): void {
+ for (const channel of view.scalable_image.channels) {
+ channel.metadata = cleanSupportedViewObject(channel.metadata);
+ }
+};
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//create the objects needed for parsing the XML object of the initial extraction.
const dataBlocks: Segment[] = require(`${extractionDirectory}outputjson.json`).czi.filter(
(s: Segment) => s.Id === 'ZISRAWSUBBLOCK' && s.Data.Data !== 'empty');
-const inspect = require('util').inspect;
-const metaXML: parsexml.Document = parsexml(fs.readFileSync(`${extractionDirectory}FILE-META-1.xml`, 'utf8'));
-
-let supportedViews: SupportedViews = {scalable_image: {width: -1, height: -1, channels: [{channel_id: '-1', channel_name: ''}], metadata: metaXML}};
-supportedViews.scalable_image.channels.pop();
let totalSizeX: number = -1;
let totalSizeY: number = -1;
+let supportedViews: SupportedViews = {scalable_image: {width: -1, height: -1, channels: [{channel_id: '-1', channel_name: ''}], metadata: {}}};
+supportedViews.scalable_image.channels.pop();
+
+/*
+ * Function to parse the xml file from the original extraction from CZI, its nasty!
+ */
+const parseExtractedXML: Function = function(xmlFile: string): void {
+ // const inspect = require('util').inspect; // << Used when console logging stuff to prettify it
+ const metaXML: parsexml.Document = parsexml(fs.readFileSync(xmlFile, 'utf8'));
+ // parse the XML :(
+ // THE FOLLOWING 2 FOR LOOPS ARE DISGUSTING AND I HATE IT, AND I WISH I DIDNT WRITE IT BUT DON'T SEE ANY OTHER WAY TO DO IT LOL :'(
+ for (const child of metaXML.root.children) {
+ if (child.name === 'Metadata') {
+ for (const entry of child.children) {
+ if (entry.name === 'Information') {
+ for (const properties of entry.children) {
+ if (properties.name === 'Image') {
+ for (const property of properties.children) {
+ if (property.name === 'SizeX' && property.content) {
+ totalSizeX = Number.parseInt(property.content, 10);
+ supportedViews.scalable_image.width = totalSizeX;
+ continue;
+ } else if (property.name === 'SizeY' && property.content) {
+ totalSizeY = Number.parseInt(property.content, 10);
+ supportedViews.scalable_image.height = totalSizeY;
+ continue;
+ } else if(property.name === 'Dimensions') {
+ for (const chan of property.children) {
+ if (chan.name === 'Channels') {
+ for (const channel of chan.children) {
+ supportedViews.scalable_image.channels.push(
+ {
+ channel_id: channel.attributes.Id.split(":")[1],
+ channel_name: channel.attributes.Name,
+ metadata: channel.children
+ }
+ );
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ for (const child of metaXML.root.children) {
+ if (child.name === 'Metadata') {
+ for (const entry of child.children) {
+ if (entry.name === 'Scaling') {
+ for (const properties of entry.children) {
+ if (properties.name === 'Items') {
+ for (const dist of properties.children) {
+ if (dist.name === 'Distance') {
+ if (dist.attributes.Id === 'X') {
+ supportedViews.scalable_image.real_width = {};
+ for (const attr of dist.children) {
+ if (attr.name === 'Value' && attr.content !== undefined)
+ supportedViews.scalable_image.real_width.value = Number.parseFloat(attr.content) * 1000000 * totalSizeX;
+ if (attr.name === 'DefaultUnitFormat')
+ supportedViews.scalable_image.real_width.units = attr.content;
+ }
+ }
+ if (dist.attributes.Id === 'Y') {
+ supportedViews.scalable_image.real_height = {};
+ for (const attr of dist.children) {
+ if (attr.name === 'Value' && attr.content !== undefined)
+ supportedViews.scalable_image.real_height.value = Number.parseFloat(attr.content) * 1000000 * totalSizeY;
+ if (attr.name === 'DefaultUnitFormat')
+ supportedViews.scalable_image.real_height.units = attr.content;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ supportedViews.scalable_image.metadata = metaXML;
+};
-// THE FOLLOWING FOR LOOP IS DISGUSTING AND I HATE IT, AND I WISH I DIDNT WRITE IT BUT DON'T SEE ANY OTHER WAY TO DO IT LOL :'(
-for (const child of metaXML.root.children) {
- if (child.name === 'Metadata') {
- for (const entry of child.children) {
- if (entry.name === 'Information') {
- for (const properties of entry.children) {
- if (properties.name === 'Image') {
- for (const property of properties.children) {
- if (property.name === 'SizeX' && property.content) {
- totalSizeX = Number.parseInt(property.content, 10);
- supportedViews.scalable_image.width = totalSizeX;
- continue;
- } else if (property.name === 'SizeY' && property.content) {
- totalSizeY = Number.parseInt(property.content, 10);
- supportedViews.scalable_image.height = totalSizeY;
- continue;
- } else if(property.name === 'Dimensions') {
- for (const chan of property.children) {
- if (chan.name === 'Channels') {
- for (const channel of chan.children) {
- supportedViews.scalable_image.channels.push(
- {
- channel_id: channel.attributes.Id.split(":")[1],
- channel_name: channel.attributes.Name,
- metadata: channel.children
- }
- );
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
-}
-// Get all values of X and Y respectively.
-const uniqueX: number[] = dataBlocks.map((x: Segment) => getDimension(x, 'X').Start).filter(
- (item: number, index: number, array: number[]) => array.indexOf(item) === index);
-const uniqueY: number[] = dataBlocks.map((x: Segment) => getDimension(x, 'Y').Start).filter(
- (item: number, index: number, array: number[]) => array.indexOf(item) === index);
-
-const uniqueTileSizes: number[][] = [];
-for (const seg of dataBlocks) {
-
- const x: number = getDimension(seg, 'X').Size;
- const y: number = getDimension(seg, 'Y').Size;
- let found: boolean = false;
- for (const dimPair of uniqueTileSizes) {
- if (x === dimPair[0] && y === dimPair[1]) {
- found = true;
- break;
- }
- }
- if (!found) {
- uniqueTileSizes.push([x, y]);
- }
-}
+// // Get all values of X and Y respectively.
+// const uniqueX: number[] = dataBlocks.map((x: Segment) => getDimension(x, 'X').Start).filter(
+// (item: number, index: number, array: number[]) => array.indexOf(item) === index);
+// const uniqueY: number[] = dataBlocks.map((x: Segment) => getDimension(x, 'Y').Start).filter(
+// (item: number, index: number, array: number[]) => array.indexOf(item) === index);
+//
+// const uniqueTileSizes: number[][] = [];
+// for (const seg of dataBlocks) {
+//
+// const x: number = getDimension(seg, 'X').Size;
+// const y: number = getDimension(seg, 'Y').Size;
+// let found: boolean = false;
+// for (const dimPair of uniqueTileSizes) {
+// if (x === dimPair[0] && y === dimPair[1]) {
+// found = true;
+// break;
+// }
+// }
+// if (!found) {
+// uniqueTileSizes.push([x, y]);
+// }
+// }
+
+
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Begin work on getting the new tiles all nice //
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+//used to filter all databocks to get certain values of 'C'.
let filteredDataBlocks: Segment[] = [];
-const filterC: Function = function(to: number): void {
+const filterC: Function = function(c_value_filter: number): void
+{
filteredDataBlocks = dataBlocks.filter(
- (item: Segment, index: number, array: Segment[]) => getDimension(item, 'C').Start === to
+ (item: Segment, index: number, array: Segment[]) => getDimension(item, 'C').Start === c_value_filter
);
};
-
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-// Begin work on getting the new tiles all nice
// Print out all of the related tiles to every new tile to be created.
let filecoutner: number = 0;
-async function extrapolateDimension(cVal: number, totalCs: number, maxZoomLevel: number): Promise<CZIHeightMap[]> {
- console.log(`Stage (${cVal + 1}/${totalCs}): Task(0/${Math.ceil(totalSizeY/tileSize)}): 0% complete.`)
- for (let ys: number = 0; ys < totalSizeY;) {
- var timing = process.hrtime();
+/*
+ * This is used to create the next zoomed out tier of the given height map
+ */
+const zoomTier: Function = async function(previousHeightMap:CZIHeightMap): Promise<CZIHeightMap> {
+
+ let ys:number = 0, timing = process.hrtime(), iterTimes: number[] = [50], avgItrTime: number;;
+ let newZoomTier: CZIHeightMap = {zoom_level: previousHeightMap.zoom_level * 2, plane: []};
+
+ //Check the condition where another zoom level does not make sense
+ if (previousHeightMap.plane.length === 1 && previousHeightMap.plane[0].length === 1) {
+ console.log(
+ ` Task(1/1): ` +
+ `Stage 100% complete. ` +
+ `(0 sec/iter) Stage was skipped as maximum sensible zoom level was reached.`
+ );
+ return previousHeightMap;
+ }
+
+ //For all rows within the previous plane
+ for (;ys < previousHeightMap.plane.length; ys += 2) {
+ timing = process.hrtime();
+ let cziRow: CZITile[] = [];
+ //for all columns within the previous plane
+ for (let xs: number = 0; xs < previousHeightMap.plane[0].length; xs += 2) {
+
+// console.error(`ys: ${ys} ymax:${previousHeightMap.plane.length} xs: ${xs} xmax: ${previousHeightMap.plane[0].length}`)
+
+ //combine 4 tiles into a new tile and scale down for this tier
+
+ let combinedTile: SharpInstance;
+ //top left
+// console.error(`SharpInitFile: ${previousHeightMap.plane[ys][xs].file}`)
+ combinedTile = await sharp(`${outputImageDirectory}${previousHeightMap.plane[ys][xs].file}`)
+ .background({r: 0, g: 0, b: 0, alpha: 0}).extend(
+ {top:0, left:0, bottom:tileSize, right: tileSize});
+
+ //combine top right
+ let moreToRight: boolean = (xs + 1) < previousHeightMap.plane[0].length;
+// console.error(`More to right: ${moreToRight}`)
+ if (moreToRight) {
+// console.error(`Top Right: ${previousHeightMap.plane[ys][xs + 1].file}`)
+ combinedTile = sharp(await combinedTile.overlayWith(
+ `${outputImageDirectory}${previousHeightMap.plane[ys][xs + 1].file}`,
+ {top: 0, left: tileSize}).toBuffer());
+ }
+
+ //combine bottom left
+ let moreToBottom: boolean = (ys + 1) < previousHeightMap.plane.length;
+// console.error(`More to bottom: ${moreToBottom}`)
+ if (moreToBottom) {
+// console.error(`Bottom Left: ${previousHeightMap.plane[ys + 1][xs].file}`)
+ combinedTile = sharp(await combinedTile.overlayWith(
+ `${outputImageDirectory}${previousHeightMap.plane[ys + 1][xs].file}`,
+ {top: tileSize, left: 0}).toBuffer());
+ }
+
+ //combine bottom right
+ if (moreToBottom && moreToRight) {
+// console.error(`Bottom Right: ${previousHeightMap.plane[ys + 1][xs + 1].file}`)
+ combinedTile = sharp(await combinedTile.overlayWith(
+ `${outputImageDirectory}${previousHeightMap.plane[ys + 1][xs + 1].file}`,
+ {top: tileSize, left: tileSize}).toBuffer());
+ }
+
+// console.error("\n\n");
+ //rescale and push to file
+ await combinedTile.resize(tileSize, tileSize)
+ .toFile(`${outputImageDirectory}img-${filecoutner}`);/*.toFile(`${outputImageData}/thing/img-${filecoutner}`);*/
+
+ cziRow.push({
+ x_offset: previousHeightMap.plane[ys][xs].x_offset,
+ y_offset: previousHeightMap.plane[ys][xs].y_offset,
+ width: previousHeightMap.plane[ys][xs].width * 2,
+ height: previousHeightMap.plane[ys][xs].height * 2,
+ file: `img-${filecoutner++}`
+ });
+ finalCZIJson.total_files++;
+ }
+
+ newZoomTier.plane.push(cziRow);
+ timing = process.hrtime(timing);
+ iterTimes.push(timing[0]);
+ if (iterTimes.length > 5) iterTimes.shift();
+ avgItrTime = iterTimes.reduce((p, c, i) => {return p+(c-p)/(i+1)}, 0);
+ console.log(
+ ` Task(${ys/2 + 1}/${Math.floor(previousHeightMap.plane.length/2) + 1}): ` +
+ `Stage ${Math.ceil(((ys/previousHeightMap.plane.length)*100) *100)/100}% complete. ` +
+ `(${timing[0]} sec/iter, est: ${((avgItrTime * ((Math.floor(previousHeightMap.plane.length/2) + 1) - (ys/2 + 1)))/60).toFixed(2)} mins remain.)`
+ );
+ }
+ newZoomTier.tile_height_count = newZoomTier.plane.length;
+ newZoomTier.tile_width_count = newZoomTier.plane[0].length;
+
+ return newZoomTier;
+}
+
+/*
+ * Big boi function to build up the base tier of each value of c, then builds
+ * Our pre-computed pyramid of the rest of the zoom levels.
+ */
+const extrapolateDimension:Function = async function(cVal: number, totalCs: number, maxZoom:number): Promise<CZIHeightMap[]> {
+
+ let ys:number = 0, timing = process.hrtime(), iterTimes: number[] = [100], avgItrTime: number;
+ filterC(cVal);
+ //begin output for the base tier
+ console.log('=========================================================');
+ console.log(`>> Beginning Dimension Extrapolation for \'C\' : ${cVal}`);
+ console.log('=========================================================');
+ console.log(">> Computing base tier, this will take a while...\n")
+ let baseCZIHeightMap: CZIHeightMap = {zoom_level: 1, plane: []},
+ width_in_tiles = 0, height_in_tiles = 0;
+
+ console.log(`Stage (${cVal * Math.log2(maxZoom) + 1}/${totalCs}):`);
+ //For all rows within the total base pixel height
+ for (;ys < totalSizeY;) {
+ timing = process.hrtime();
+ let cziRow: CZITile[] = [];
for (let xs: number = 0; xs < totalSizeX; xs += tileSize) {
const desired = new TileBounds (
xs - tileOverlap,
@@ -400,32 +576,182 @@ async function extrapolateDimension(cVal: number, totalCs: number, maxZoomLevel:
ys - tileOverlap,
ys + tileSize + tileOverlap
);
- filterC(cVal);
- // console.log(filteredDataBlocks.map((x: any) => x.Data.DirectoryEntry.DimensionEntries[2]));
+ cziRow.push({
+ x_offset: xs,
+ y_offset: ys,
+ width: tileSize,
+ height: tileSize,
+ file: `img-${filecoutner}`
+ });
const sortedSegments: Segment[][] = orderSegments(findRelatedTiles(filteredDataBlocks, desired));
(await extractAndStitchChunks(sortedSegments, desired)).toFile(`${outputImageDirectory}img-${filecoutner++}`);
+ finalCZIJson.total_files++;
+ width_in_tiles = xs/tileSize;
}
+ baseCZIHeightMap.plane.push(cziRow);
ys += tileSize;
timing = process.hrtime(timing);
- console.log(`Stage (${cVal + 1}/${totalCs}): Task(${ys/tileSize}/${Math.ceil(totalSizeY/tileSize)}): ${Math.ceil(((ys/totalSizeY)*100) *100)/100}% complete. (${timing[0]} sec/iter)`);
+ iterTimes.push(timing[0]);
+ if (iterTimes.length > 5) iterTimes.shift();
+ avgItrTime = iterTimes.reduce((p, c, i) => {return p+(c-p)/(i+1)}, 0);
+ console.log(
+ ` Task(${ys/tileSize}/${Math.ceil(totalSizeY/tileSize)}): ` +
+ `Stage ${Math.ceil(((ys/totalSizeY)*100) *100)/100}% complete. ` +
+ `(${timing[0]} sec/iter, est: ${((avgItrTime * (Math.ceil(totalSizeY/tileSize) - ys/tileSize))/60).toFixed(2)} mins remain.)`
+ );
+ height_in_tiles = ys/tileSize;
}
- return [];
-}
+ baseCZIHeightMap.tile_height_count = height_in_tiles;
+ baseCZIHeightMap.tile_width_count = width_in_tiles;
-let finalCZIJson: WholeCZIHierarchy = {} as any;
-async function main(): Promise<void> {
- for (const cval of supportedViews.scalable_image.channels) {
- await extrapolateDimension(Number.parseInt(cval.channel_id, 10), supportedViews.scalable_image.channels.length, maxZoom).then(v =>
+ //BUILD UP THE HIGHER ZOOM LAYERS.
+ console.log(">> Base tier complete for \'C\': " + cVal + "; Begin computing zoom tiers...\n")
+
+ let retHeightMap: CZIHeightMap[] = [baseCZIHeightMap];
+ for (let stage:number = 1; stage < Math.log2(maxZoom); stage++) {
+ console.log(`Stage (${cVal * Math.log2(maxZoom) + 1 + stage}/${totalCs}):`);
+ await zoomTier(retHeightMap[stage - 1])
+ .then((v: CZIHeightMap) =>
{
- finalCZIJson.c_values.push({channel_id: cval.channel_id, height_map: v});
- console.log(`Completed Tier: ${cval.channel_id}`);
+ retHeightMap.push(v);
return;
- }, err => {
- console.error(err);
}
);
+ writeJSONtoFile(`${outputImageData}stageMap-${cVal}-${stage}`, retHeightMap);
}
- fs.writeFile(`${outputImageDirectory}layout.json`, JSON.stringify(finalCZIJson), (err) => console.error(err));
+
+ console.log(`>> Completed Extrapolation for dimension, \'C\': ${cVal}\n\n`);
+ return retHeightMap;
+}
+
+/*
+ * function used to ensure that the directories required have been created
+ */
+const checkForOutputDirectories: Function = function(directories: string[]): void
+{
+ for (const dir of directories) {
+ if (!fs.existsSync(`${dir}`)) {
+ fs.mkdirSync(`${dir}`);
+ }
+ }
+};
+
+/*
+ * Write given JSON object to given filepath
+ */
+const writeJSONtoFile: Function = function(filePath:string, object:Object): void
+{
+ fs.writeFile(filePath,
+ JSON.stringify(object, null, 2),
+ (err) =>
+ {
+ if (err) {
+ console.error(err)
+ throw err;
+ }
+ }
+ );
+}
+
+/*
+ * used to write the supported views object to file both in total and "cleaned"
+ */
+const createSupportedViewsObject: Function = function(): void
+{
+ //parse the xml
+ parseExtractedXML(`${extractionDirectory}FILE-META-1.xml`);
+
+ //create the total supported Views object
+ writeJSONtoFile(`${outputImageData}supported_views.json`, supportedViews);
+
+ //cleanup and create "smart" supported views object without whole metadata
+ cleanSupportedViews(supportedViews);
+ supportedViews.scalable_image.metadata = {};
+ writeJSONtoFile(`${outputImageData}supported_views_minus_xml.json`, supportedViews);
+}
+
+/*
+ * Used to build up a pyramid for each value of c from the base images
+ */
+const buildCustomPyramids:Function = async function buildCustomPyramids(): Promise<void>
+{
+ //set some variables for errors and and percent counts
+ let errorOccurred: Boolean;
+ finalCZIJson.zoom_level_count = Math.log2(maxZoom);
+ finalCZIJson.c_values = [] as any;
+ finalCZIJson.c_value_count = 0;
+ finalCZIJson.total_files = 0;
+ finalCZIJson.complete = false;
+
+ //for all channels in the base image
+ for (const cval of supportedViews.scalable_image.channels) {
+ errorOccurred = false;
+ if (!errorOccurred) {
+ //extract the base tiles and build up custom pyramid data
+ await extrapolateDimension(Number.parseInt(cval.channel_id, 10), supportedViews.scalable_image.channels.length * Math.log2(maxZoom), maxZoom)
+ .then((v: CZIHeightMap[]) =>
+ {
+ // on success, add the new heightmap to the final json data block
+ finalCZIJson.c_values.push({channel_id: cval.channel_id, height_map: v});
+ finalCZIJson.c_value_count++;
+ return;
+ }
+ ).catch((err: Error) =>
+ {
+ // on error, write the error to console and set an error true.
+ if (err) {
+ console.error(err);
+ }
+ errorOccurred = true;
+ }
+ );
+ }
+ else {
+ console.error("\nAn error occurred while extracting dimension: " + cval);
+ console.error("Skipping futher elements and moving to next dimension.\n\n");
+ }
+ //write the final json data to file with most recent changes
+ writeJSONtoFile(`${outputImageData}layout.json`, finalCZIJson);
+ }
+ finalCZIJson.complete = true;
+}
+
+
+let finalCZIJson: WholeCZIHierarchy = {} as any;
+/*
+ * Main function used to call the rest of the relevant code to crunch a CZI extraction.
+ */
+const main: Function = function() {
+ console.log("\n");
+
+ console.log(">> Checking/Creating output directories...")
+ checkForOutputDirectories([outputImageData, outputImageDirectory]);
+
+ console.log(">> Creating Supported Views and writing files...")
+ createSupportedViewsObject();
+
+ console.log("");
+ buildCustomPyramids();
}
main();
+// async function main2() {
+//
+// let layout: WholeCZIHierarchy = require(`${outputImageData}layout.json`);
+// let retHeightMap: any[] = [{}, {}, require(`${outputImageData}stageMap-2.json`)];
+// filecoutner = 5530;
+//
+// for (let stage:number = 3; stage < Math.sqrt(maxZoom); stage++) {
+// console.log(`Stage (${0 * Math.sqrt(maxZoom) + 1 + stage}/${1}):`);
+// await zoomTier(retHeightMap[stage -1])
+// .then((v: CZIHeightMap) =>
+// {
+// retHeightMap.push(v);
+// writeJSONtoFile(`${outputImageData}stageMap-${stage}.json`, v);
+// return;
+// }
+// );
+// }
+// }
+//
+// main2().then();
diff --git a/src/conversion/czi/cziConvert.sh b/src/conversion/czi/cziConvert.sh
index 639251c..2529d44 100755
--- a/src/conversion/czi/cziConvert.sh
+++ b/src/conversion/czi/cziConvert.sh
@@ -1,8 +1,9 @@
#!/bin/sh
+FILENAME='2016_12_17__0701'
CZI_WORKING_DIR=${1:-"/cs/scratch"}
-CZI_EXTRACTION_DIR=${2:-"0701-extraction"}
-CZI_FILE=${3:-"2016_12_17__0701.czi"}
+CZI_EXTRACTION_DIR=${2:-${FILENAME}}
+CZI_FILE=${3:-${FILENAME}".czi"}
gcc -Wall -Wextra --pedantic -std=c99 cziconvert.c
mkdir -p ${CZI_WORKING_DIR}/${USER}/${CZI_EXTRACTION_DIR}
diff --git a/src/conversion/czi/pyramidWorker.ts b/src/conversion/czi/pyramidWorker.ts
new file mode 100644
index 0000000..286fd6b
--- /dev/null
+++ b/src/conversion/czi/pyramidWorker.ts
@@ -0,0 +1,179 @@
+import { CZITile, CZIHeightMap, WholeCZIHierarchy } from '../types/customPyramidIndex';
+import { TileBounds } from '../types/helperCZITypes';
+import * as sharp from 'sharp';
+import { SharpInstance } from 'sharp';
+import * as fs from 'fs';
+import { isTileRelated, regionToExtract } from './tileExtraction';
+
+const processedDirname: string = '/cs/scratch/cjd24/0701-extraction-processed/';
+const processedImages: string = `${processedDirname}data/`;
+const layout: WholeCZIHierarchy = require(`${processedDirname}layout.json`);
+const TEMP_OUTPUT_THING: string = `${processedDirname}finalisedTiles/`;
+const TEMP_REQUESTED_BOUNDS: TileBounds[] =
+[
+ new TileBounds(16386, 38000, 15000, 40000),
+ new TileBounds(6000, 15000, 9000, 18000)
+]
+const TEMP_C_VALUE: number = 1, ZOOM_LEVEL: number = 8, TILE_SIZE: number = 1024, TILE_OVERLAP: number = 0;
+
+
+const findRelatedTiles:Function = function(plane: CZIHeightMap, desired: TileBounds): CZITile[][] {
+
+ let relatedTiles: CZITile[][] = [];
+ for (const planeTileRow of plane.plane) {
+ let tileRow: CZITile[] = [], 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;
+}
+let help: number = 0;
+const extractAndStitch: Function = async function(tiles: CZITile[][], desiredRegion: TileBounds, zoom: number): Promise<SharpInstance> {
+
+ //create a buffer for the rows part of raw image data
+ let tileHorizontalBuffer: Buffer[] = [], bufferBound: TileBounds, rowPixelWidth:number = 0, columnPixelHeight: number = 0;
+
+ //for all rows (y coords) in the image
+ for (const row of tiles) {
+ //create a buffer for this row
+ let tileRow: Buffer[] = [];
+ let chunkToExtract: TileBounds = new TileBounds(-1, -1, -1, -1);
+ rowPixelWidth = 0;
+
+ //For all of the columns within this row
+ for (const tile of row) {
+ //find out the region of the base tile to be added to the new image
+ chunkToExtract = regionToExtract(new TileBounds(tile.x_offset, tile.x_offset + tile.width, tile.y_offset, tile.y_offset + tile.height), desiredRegion, zoom);
+ rowPixelWidth += (chunkToExtract.right - chunkToExtract.left);
+
+ //extract this section of image data into a sharp buffer
+ let data: Promise<Buffer> =
+ sharp(`${processedImages}${tile.file}`)
+ .extract({
+ left: chunkToExtract.left,
+ top: chunkToExtract.top,
+ width: chunkToExtract.right - chunkToExtract.left,
+ height: chunkToExtract.bottom - chunkToExtract.top
+ })
+ .toBuffer();
+
+ //push this data into the row buffer
+ tileRow.push(await data);
+ }
+
+ //prepare variables to squash all of the column data into one, for this row
+ let imageVerticalSlice: SharpInstance = sharp(tileRow[0]);
+ bufferBound = regionToExtract(new TileBounds(
+ row[0].x_offset,
+ row[0].x_offset + row[0].width,
+ row[0].y_offset,
+ row[0].y_offset + row[0].height,
+ ), desiredRegion, zoom);
+ //check if the inital chunk needs to be extended to support the other columns
+ if (row.length > 1) {
+ imageVerticalSlice = await imageVerticalSlice.background({r: 0, g: 0, b: 0, alpha: 0}).extend(
+ {top:0, left:0, bottom:0, right: rowPixelWidth - (bufferBound.right - bufferBound.left)});
+ }
+ for (let index = 1; index < row.length; index++) {
+ imageVerticalSlice = sharp(await imageVerticalSlice
+ .overlayWith(tileRow[index],
+ {top: 0, left: ((bufferBound.right - bufferBound.left) + ((index -1) * TILE_SIZE))}).toBuffer());
+ }
+
+ //add this horizontal data buffer to the vertical buffer
+ tileHorizontalBuffer.push(await imageVerticalSlice.toBuffer());
+ columnPixelHeight += bufferBound.bottom - bufferBound.top;
+ }
+
+ // prepare variables used to combine all rows into one image
+ let finalImage: SharpInstance = sharp(tileHorizontalBuffer[0]);
+ bufferBound = regionToExtract(new TileBounds(
+ tiles[0][0].x_offset,
+ tiles[0][0].x_offset + tiles[0][0].width,
+ tiles[0][0].y_offset,
+ tiles[0][0].y_offset + tiles[0][0].height
+ ), desiredRegion, zoom);
+ //Check if the inital row needs to be extended to support further rows
+ if (tiles.length > 1) {
+ finalImage = await finalImage.background({r: 0, g: 0, b: 0, alpha: 0}).extend(
+ {top:0, left:0, bottom: (columnPixelHeight - (bufferBound.bottom - bufferBound.top)), right: 0});
+ }
+ //For all further rows in the image, combine it with the previous rows' data
+ for (let index = 1; index < tiles.length; index++) {
+ finalImage = sharp(await finalImage.overlayWith(
+ tileHorizontalBuffer[index],
+ {top: ((bufferBound.bottom - bufferBound.top) + ((index -1) * TILE_SIZE)), left: 0}).toBuffer());
+ }
+
+ // return the finalised image.
+ console.log(`Width: ${rowPixelWidth} Height: ${columnPixelHeight}`)
+ return await finalImage
+ // .resize(
+ // rowPixelWidth,
+ // columnPixelHeight
+ // )
+ // .crop(sharp.gravity.northwest);
+};
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+const main: Function = function() {
+ if (!layout.complete) {
+ console.error(`The files under: ${processedDirname} have not been finalised yet, please try again later`);
+ return;
+ }
+
+ if (!fs.existsSync(`${TEMP_OUTPUT_THING}`)) {
+ fs.mkdirSync(`${TEMP_OUTPUT_THING}`);
+ }
+
+ let requestCount: number = 0;
+ for (const bound of TEMP_REQUESTED_BOUNDS) {
+ extractAndStitch(
+ findRelatedTiles(layout.c_values[TEMP_C_VALUE].height_map[Math.log2(ZOOM_LEVEL)], bound), bound, ZOOM_LEVEL
+ ).then((x: SharpInstance) => x.toFile(`${TEMP_OUTPUT_THING}request-${requestCount++}`));
+ }
+}
+main();
diff --git a/src/conversion/czi/tileExtraction.ts b/src/conversion/czi/tileExtraction.ts
new file mode 100644
index 0000000..be8bd3c
--- /dev/null
+++ b/src/conversion/czi/tileExtraction.ts
@@ -0,0 +1,129 @@
+import { CZITile } from '../types/customPyramidIndex';
+import { TileBounds } from '../types/helperCZITypes';
+import { SharpInstance } from 'sharp';
+import * as sharp from 'sharp';
+
+export const isTileRelated:Function = function(origCoords: TileBounds, desired: TileBounds): boolean {
+
+ if (((desired.left >= origCoords.left) &&
+ (desired.left < origCoords.right)) ||
+
+ // Desired X on right is within the current tile
+ ((desired.right > origCoords.left) &&
+ (desired.right <= origCoords.right)) ||
+
+ // Desired tile X overlaps base tile
+ ((desired.left < origCoords.left) &&
+ (desired.right > origCoords.right)))
+ {
+
+ // Desired Y on top is within current tile
+ if (((desired.top >= origCoords.top) &&
+ (desired.top < origCoords.bottom)) ||
+
+ // Desired Y on right is within the current tile
+ ((desired.bottom > origCoords.top) &&
+ (desired.bottom <= origCoords.bottom)) ||
+
+ // Desired tile Y overlaps whole base tile
+ ((desired.top < origCoords.top) &&
+ (desired.bottom > origCoords.bottom)))
+ {
+
+ //If all of the if statements have passed, then this tile
+ //is part of the overlap for the new tile to be created.
+ return true;
+ }
+ }
+ return false;
+}
+
+
+/*
+ * Takes a base tile, and the whole bounds of a new tile, and uses this
+ * to calculate the region within the base tile that is overlapping the new tile
+ * to be extracted.
+ */
+export const regionToExtract: Function = function(baseBounds: TileBounds, desired: TileBounds, zoomLevel: number): TileBounds {
+
+ //get the base tile area and create an object for the chunk to extract
+ let chunkToExtract: TileBounds = new TileBounds(-1, -1, -1, -1);
+
+ // check if the new tile overlaps the leftmost boundary
+ if (desired.left <= baseBounds.left) {
+ chunkToExtract.left = 0;
+ } else {
+ chunkToExtract.left = desired.left - baseBounds.left;
+ }
+
+ // check if the new tile overlaps the rightmost boundary
+ if (desired.right >= baseBounds.right) {
+ chunkToExtract.right = baseBounds.right - baseBounds.left;
+ } else {
+ chunkToExtract.right = desired.right - baseBounds.left;
+ }
+
+ // check if the new tile overlaps the top boundary
+ if (desired.top <= baseBounds.top) {
+ chunkToExtract.top = 0;
+ } else {
+ chunkToExtract.top = desired.top - baseBounds.top;
+ }
+
+ // check if the new tile overlaps the bottom boundary
+ if (desired.bottom >= baseBounds.bottom) {
+ chunkToExtract.bottom = baseBounds.bottom - baseBounds.top;
+ } else {
+ chunkToExtract.bottom = desired.bottom - baseBounds.top;
+ }
+
+ if (chunkToExtract.left < 0 || chunkToExtract.right < 0 || chunkToExtract.top < 0 || chunkToExtract.bottom < 0) {
+ throw new Error("Chunk to extract from base tile has invalid bound (< 0).");
+ }
+
+ chunkToExtract.left = Math.ceil(chunkToExtract.left / zoomLevel);
+ chunkToExtract.right = Math.ceil(chunkToExtract.right / zoomLevel);
+ chunkToExtract.top = Math.ceil(chunkToExtract.top / zoomLevel);
+ chunkToExtract.bottom = Math.ceil(chunkToExtract.bottom / zoomLevel);
+
+ //return the slice within the base tile for extraction
+ return chunkToExtract;
+};
+
+
+// export const createHorizontalImageSlice: Function = async function (bufferBound: TileBounds, tileSize: number, tileOverlap: number, rowLength:number, row: Buffer[]): Promise<Buffer> {
+// //prepare variables to squash all of the column data into one, for this row
+// let imageVerticalSlice: SharpInstance = sharp(row[0]);
+// //check if the inital chunk needs to be extended to support the other columns
+// if ((bufferBound.right - bufferBound.left) < (tileSize - tileOverlap)) {
+// imageVerticalSlice = await imageVerticalSlice.background({r: 0, g: 0, b: 0, alpha: 0}).extend(
+// {top:0, left:0, bottom:0, right: (tileSize - (bufferBound.right - bufferBound.left))});
+// }
+// //for every column along this row, join it to the previous column data
+// for (let index = 1; index < rowLength; index++) {
+// imageVerticalSlice = await imageVerticalSlice
+// .overlayWith(row[index],
+// {top: 0, left: (bufferBound.right - bufferBound.left) + ((index -1) * tileSize)});
+// }
+// return imageVerticalSlice.toBuffer();
+// }
+//
+//
+// export const createImageFromHorizontalSlices: Function = async function(bufferBound: TileBounds, horizontalSlices: Buffer[], tileSize: number, tileOverlap: number, rowCount: number, tiles: CZITile[][]): Promise<SharpInstance> {
+// //prepare variables used to combine all rows into one image
+// let finalImage: SharpInstance = sharp(horizontalSlices[0]);
+//
+// //Check if the inital row needs to be extended to support further rows
+// if ((bufferBound.bottom - bufferBound.top) < (tileSize - tileOverlap)) {
+// finalImage = await finalImage.background({r: 0, g: 0, b: 0, alpha: 0}).extend(
+// {top:0, left:0, bottom: (tileSize - (bufferBound.bottom - bufferBound.top)), right: 0});
+// }
+// //For all further rows in the image, combine it with the previous rows' data
+// for (let index = 1; index < rowCount; index++) {
+// finalImage = finalImage.overlayWith(
+// horizontalSlices[index],
+// {top: tiles[index - 1][0].height, left: 0});
+// }
+//
+// return finalImage;
+// }
diff --git a/src/conversion/types/customPyramidIndex.ts b/src/conversion/types/customPyramidIndex.ts
new file mode 100644
index 0000000..f362372
--- /dev/null
+++ b/src/conversion/types/customPyramidIndex.ts
@@ -0,0 +1,27 @@
+//The following are interfaces for the Custom object storing Information
+//about the CUSTOM pyramid being created for this image
+ export interface CZITile {
+ x_offset: number;
+ y_offset: number;
+ width: number;
+ height: number;
+ file: string;
+ }
+
+ export interface CZIHeightMap {
+ zoom_level: number,
+ tile_width_count?: number,
+ tile_height_count?: number,
+ plane: CZITile[][];
+ }
+
+ export interface WholeCZIHierarchy {
+ c_values: [{
+ channel_id: string,
+ height_map: CZIHeightMap[];
+ }],
+ c_value_count: number,
+ zoom_level_count: number;
+ total_files: number;
+ complete: boolean;
+ }
diff --git a/src/conversion/types/cziBinaryTypings.ts b/src/conversion/types/cziBinaryTypings.ts
new file mode 100644
index 0000000..a78b4e9
--- /dev/null
+++ b/src/conversion/types/cziBinaryTypings.ts
@@ -0,0 +1,23 @@
+//Interfaces used to define the tiles incoming from the CZI extraction
+
+export interface Dimension {
+ Dimension: string;
+ Start: number;
+ Size: number;
+}
+
+export interface DirectoryEntry {
+ DimensionCount: number;
+ DimensionEntries: Dimension[];
+}
+
+export interface SubBlock {
+ DirectoryEntry: DirectoryEntry;
+ Data: string;
+}
+
+export interface Segment {
+ Id: string;
+ UsedSize: number;
+ Data: SubBlock;
+}
diff --git a/src/conversion/types/helperCZITypes.ts b/src/conversion/types/helperCZITypes.ts
new file mode 100644
index 0000000..2d57b15
--- /dev/null
+++ b/src/conversion/types/helperCZITypes.ts
@@ -0,0 +1,39 @@
+//Interface used to define the structure of the "supportedViews" section
+//of the protocol specification for scalable images
+import { Node } from 'xml-parser';
+export interface SupportedViews {
+ scalable_image: {
+ width: number;
+ height: number;
+ channels: [
+ {
+ channel_id: string;
+ channel_name: string;
+ metadata?: Node[];
+ }
+ ];
+ real_width?: {
+ value?: number;
+ units?: string;
+ };
+ real_height?: {
+ value?: number;
+ units?: string;
+ };
+ metadata?: {};
+ }
+}
+
+//Interface used to define the pixel-bounds of a 'section' within the image
+export class TileBounds {
+ public left: number;
+ public right: number;
+ public top: number;
+ public bottom: number;
+ public constructor(leftBound: number, rightBound: number, topBound: number, bottomBound: number) {
+ this.left = leftBound;
+ this.right = rightBound;
+ this.top = topBound;
+ this.bottom = bottomBound;
+ }
+}