diff options
| author | cjd24 | 2018-04-01 19:05:00 +0100 |
|---|---|---|
| committer | cjd24 | 2018-04-01 19:05:00 +0100 |
| commit | 709d6ec75364a51498139ed37f40b297e5fc9766 (patch) | |
| tree | c36c3382be469a96ff713f884b3299416cb79f30 | |
| parent | Small changes to fix up last type stuff (diff) | |
| download | medicine-709d6ec75364a51498139ed37f40b297e5fc9766.tar.gz medicine-709d6ec75364a51498139ed37f40b297e5fc9766.zip | |
first working iteration of final tile extraction
| -rwxr-xr-x | getmecziconversion.sh | 10 | ||||
| -rw-r--r-- | package.json | 6 | ||||
| -rw-r--r-- | src/conversion/czi/czi.ts | 2 | ||||
| -rw-r--r-- | src/conversion/czi/pyramidWorker.ts | 167 | ||||
| -rw-r--r-- | src/conversion/czi/tileDims.png | bin | 0 -> 292647 bytes | |||
| -rw-r--r-- | src/conversion/types/customPyramidIndex.ts | 10 | ||||
| -rw-r--r-- | src/conversion/types/helperCZITypes.ts | 18 | ||||
| -rw-r--r-- | yarn.lock | 14 |
8 files changed, 101 insertions, 126 deletions
diff --git a/getmecziconversion.sh b/getmecziconversion.sh index 4fccdb0..7a4316c 100755 --- a/getmecziconversion.sh +++ b/getmecziconversion.sh @@ -7,3 +7,13 @@ make ${PWD}/build/JxrDecApp cp ./build/JxrDecApp ../src/conversion/czi/JxrDecApp cd ../ rm -rf jxrlib + +curl -L https://github.com/jcupitt/libvips/releases/download/v8.6.3/vips-8.6.3.tar.gz > vips-8.6.3.tar.gz +tar xf vips-8.6.3.tar.gz +rm vips-8.6.3.tar.gz +cd vips-8.6.3 +./configure +make +cp ./tools/vips .. +cd .. +rm -rf vips-8.6.3 diff --git a/package.json b/package.json index b21bed5..4453e05 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "run:postgres": "pg_ctl start", "start": "ts-node src/index.ts", "_lint": "tslint -p './tsconfig.json' -c './tslint.json'", - "lint": "yarn _lint --fix --force './src/**/*.ts'", + "lint": "yarn _lint --fix --force './src/**/*.ts'", "lint:tests": "yarn _lint --fix --force './test/**/*.ts'" }, "repository": { @@ -54,9 +54,9 @@ "@types/passport": "^0.4.2", "@types/passport-local": "^1.0.32", "@types/sequelize": "^4.27.5", + "@types/sharp": "^0.17.6", "@types/sqlite3": "^3.1.1", "@types/supertest": "^2.0.4", - "@types/sharp": "^0.17.6", "@types/uuid": "^3.4.3", "@types/winston": "^2.3.8", "@types/xml-parser": "^1.2.29", @@ -76,9 +76,9 @@ "reflect-metadata": "^0.1.12", "sequelize": "^4.32.7", "sequelize-typescript": "^0.6.2", + "sharp": "^0.20.1", "sqlite3": "^4.0.0", "supertest": "^3.0.0", - "sharp": "^0.20.0", "ts-node": "^3.3.0", "typescript": "^2.7.1", "uuid": "^3.2.1", 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 Binary files differnew file mode 100644 index 0000000..70e2484 --- /dev/null +++ b/src/conversion/czi/tileDims.png 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) + } } @@ -1411,11 +1411,15 @@ ms@^2.0.0, ms@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.1.tgz#30a5864eb3ebb0a66f2ebe6d727af06a09d86e0a" +nan@^2.10.0: + version "2.10.0" + resolved "https://registry.yarnpkg.com/nan/-/nan-2.10.0.tgz#96d0cd610ebd58d4b4de9cc0c6828cda99c7548f" + nan@^2.3.0: version "2.8.0" resolved "https://registry.yarnpkg.com/nan/-/nan-2.8.0.tgz#ed715f3fe9de02b57a5e6252d90a96675e1f085a" -nan@^2.9.2, nan@~2.9.2: +nan@~2.9.2: version "2.9.2" resolved "https://registry.yarnpkg.com/nan/-/nan-2.9.2.tgz#f564d75f5f8f36a6d9456cca7a6c4fe488ab7866" @@ -1902,14 +1906,14 @@ setprototypeof@1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/setprototypeof/-/setprototypeof-1.1.0.tgz#d0bd85536887b6fe7c0d818cb962d9d91c54e656" -sharp@^0.20.0: - version "0.20.0" - resolved "https://registry.yarnpkg.com/sharp/-/sharp-0.20.0.tgz#2ff9f1ae11f068ce7d7591f531cad52c7821d033" +sharp@^0.20.1: + version "0.20.1" + resolved "https://registry.yarnpkg.com/sharp/-/sharp-0.20.1.tgz#13adde896bc9ec0372b0907554e0329f35a7bce6" dependencies: color "^3.0.0" detect-libc "^1.0.3" fs-copy-file-sync "^1.0.1" - nan "^2.9.2" + nan "^2.10.0" npmlog "^4.1.2" prebuild-install "^2.5.1" semver "^5.5.0" |
