aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcjd242018-04-01 19:05:00 +0100
committercjd242018-04-01 19:05:00 +0100
commit709d6ec75364a51498139ed37f40b297e5fc9766 (patch)
treec36c3382be469a96ff713f884b3299416cb79f30 /src
parentSmall changes to fix up last type stuff (diff)
downloadmedicine-709d6ec75364a51498139ed37f40b297e5fc9766.tar.gz
medicine-709d6ec75364a51498139ed37f40b297e5fc9766.zip

first working iteration of final tile extraction

Diffstat (limited to 'src')
-rw-r--r--src/conversion/czi/czi.ts2
-rw-r--r--src/conversion/czi/pyramidWorker.ts167
-rw-r--r--src/conversion/czi/tileDims.pngbin0 -> 292647 bytes
-rw-r--r--src/conversion/types/customPyramidIndex.ts10
-rw-r--r--src/conversion/types/helperCZITypes.ts18
5 files changed, 79 insertions, 118 deletions
diff --git a/src/conversion/czi/czi.ts b/src/conversion/czi/czi.ts
index 5682586..e6a4220 100644
--- a/src/conversion/czi/czi.ts
+++ b/src/conversion/czi/czi.ts
@@ -778,7 +778,7 @@ const main: () => void = (): void => {
logger.info('\n');
logger.info('>> Checking/Creating output directories...');
- checkForOutputDirectories([outputImageData, outputImageDirectory]);
+ checkForOutputDirectories([outputImageData, outputImageDirectory, `${outputImageData}tmp/`]);
logger.info('>> Creating Supported Views and writing files...');
createSupportedViewsObject();
diff --git a/src/conversion/czi/pyramidWorker.ts b/src/conversion/czi/pyramidWorker.ts
index 81110e7..aea7936 100644
--- a/src/conversion/czi/pyramidWorker.ts
+++ b/src/conversion/czi/pyramidWorker.ts
@@ -1,30 +1,26 @@
-import { CZIHeightMap, CZITile, WholeCZIHierarchy } from '../types/customPyramidIndex';
-import * as fs from 'fs';
+import { CZIHeightMap, CZITile, CZITileRequest } from '../types/customPyramidIndex';
import { TileBounds } from '../types/helperCZITypes';
import * as sharp from 'sharp';
-import { SharpInstance } from 'sharp';
-import { isTileRelated, regionToExtract } from './tileExtraction';
-import { logger } from '../../logger';
-
-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;
+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[] = [], foundTiles: boolean = false;
+ 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)) {
+ 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;
}
@@ -35,109 +31,46 @@ const findRelatedTiles: Function = function(plane: CZIHeightMap, desired: TileBo
}
return relatedTiles;
};
-const 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;
+const getFinalTile: Function = async function(imageDir: string, imageTier: CZIHeightMap, cVal: string, desiredRegion: TileBounds): Promise<CZITileRequest> {
- //for all rows (y coords) in the image
- for (const row of tiles) {
- //create a buffer for this row
- const tileRow: Buffer[] = [];
- let chunkToExtract: TileBounds = new TileBounds(-1, -1, -1, -1);
- rowPixelWidth = 0;
+ console.error("Has the calling function checked that the image has completed processing in WholeCZIHierarchy??");
- //For all of the columns within this row
+ let involvedTiles: string = '';
+ for (const row of imageTier.plane) {
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
- const 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());
+ involvedTiles += `${imageDir}${tile.file} `
}
-
- //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 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();
+ 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
+ };
+}
diff --git a/src/conversion/czi/tileDims.png b/src/conversion/czi/tileDims.png
new file mode 100644
index 0000000..70e2484
--- /dev/null
+++ b/src/conversion/czi/tileDims.png
Binary files differ
diff --git a/src/conversion/types/customPyramidIndex.ts b/src/conversion/types/customPyramidIndex.ts
index d209898..9a3c8c3 100644
--- a/src/conversion/types/customPyramidIndex.ts
+++ b/src/conversion/types/customPyramidIndex.ts
@@ -1,3 +1,5 @@
+import { TileBounds } from './helperCZITypes'
+
//The following are interfaces for the Custom object storing Information
//about the CUSTOM pyramid being created for this image
export interface CZITile {
@@ -25,3 +27,11 @@ export interface WholeCZIHierarchy {
total_files: number;
complete: boolean;
}
+
+export interface CZITileRequest {
+ dimensions: TileBounds,
+ original_bounds: TileBounds,
+ zoom_level: number,
+ c_value: string,
+ file_path: string
+}
diff --git a/src/conversion/types/helperCZITypes.ts b/src/conversion/types/helperCZITypes.ts
index 51d5d85..8f6310a 100644
--- a/src/conversion/types/helperCZITypes.ts
+++ b/src/conversion/types/helperCZITypes.ts
@@ -45,4 +45,22 @@ export class TileBounds {
this.top = topBound;
this.bottom = bottomBound;
}
+ public width(): number {
+ return this.right - this.left;
+ }
+ public height(): number {
+ return this.bottom - this.top;
+ }
+ public scaleDown(level: number) {
+ this.left = Math.ceil(this.left / level),
+ this.right = Math.ceil(this.right / level),
+ this.top = Math.ceil(this.top / level),
+ this.bottom = Math.ceil(this.bottom / level)
+ }
+ public scaleUp(level: number) {
+ this.left = Math.floor(this.left * level),
+ this.right = Math.ceil(this.right * level),
+ this.top = Math.ceil(this.top * level),
+ this.bottom = Math.ceil(this.bottom * level)
+ }
}