From 94acccd371a233cda1743d0d387247f0b9b55986 Mon Sep 17 00:00:00 2001 From: cjd24 Date: Thu, 29 Mar 2018 03:50:47 +0100 Subject: Please dont use this commit lol --- src/conversion/czi/pyramidWorker.ts | 161 ++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 src/conversion/czi/pyramidWorker.ts (limited to 'src/conversion/czi/pyramidWorker.ts') diff --git a/src/conversion/czi/pyramidWorker.ts b/src/conversion/czi/pyramidWorker.ts new file mode 100644 index 0000000..bdbcbfa --- /dev/null +++ b/src/conversion/czi/pyramidWorker.ts @@ -0,0 +1,161 @@ +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(0, 5000, 2000, 7000), + new TileBounds(1000, 6000, 3000, 8000) +] +const TEMP_C_VALUE: number = 0, ZOOM_LEVEL: number = 2, 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; +} + +const extractAndStitch: Function = async function(segments: CZITile[][], desiredRegion: TileBounds): Promise { + + // //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 = + // sharp(`${extractionDirectory}${segCol.Data.Data}`) + // .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]); + // 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.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.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; + return; +}; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +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[ZOOM_LEVEL], bound), bound + ).then((x: SharpInstance) => x.toFile(`${TEMP_OUTPUT_THING}request-${requestCount++}`)); + } +} +main(); -- cgit v1.2.1 From feea9d9360409e6bd3cd0d874c7773523e47a7a4 Mon Sep 17 00:00:00 2001 From: cjd24 Date: Thu, 29 Mar 2018 09:33:23 +0100 Subject: v basic and slow working version of final tile builder - Be Wary, this commit might break the old czi.ts builder --- src/conversion/czi/pyramidWorker.ts | 160 ++++++++++++++++++++---------------- 1 file changed, 89 insertions(+), 71 deletions(-) (limited to 'src/conversion/czi/pyramidWorker.ts') diff --git a/src/conversion/czi/pyramidWorker.ts b/src/conversion/czi/pyramidWorker.ts index bdbcbfa..286fd6b 100644 --- a/src/conversion/czi/pyramidWorker.ts +++ b/src/conversion/czi/pyramidWorker.ts @@ -11,10 +11,10 @@ const layout: WholeCZIHierarchy = require(`${processedDirname}layout.json`); const TEMP_OUTPUT_THING: string = `${processedDirname}finalisedTiles/`; const TEMP_REQUESTED_BOUNDS: TileBounds[] = [ - new TileBounds(0, 5000, 2000, 7000), - new TileBounds(1000, 6000, 3000, 8000) + new TileBounds(16386, 38000, 15000, 40000), + new TileBounds(6000, 15000, 9000, 18000) ] -const TEMP_C_VALUE: number = 0, ZOOM_LEVEL: number = 2, TILE_SIZE: number = 1024, TILE_OVERLAP: number = 0; +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[][] { @@ -35,74 +35,92 @@ const findRelatedTiles:Function = function(plane: CZIHeightMap, desired: TileBou } return relatedTiles; } +let help: number = 0; +const extractAndStitch: Function = async function(tiles: CZITile[][], desiredRegion: TileBounds, zoom: number): Promise { + + //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 = + 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()); + } -const extractAndStitch: Function = async function(segments: CZITile[][], desiredRegion: TileBounds): Promise { - - // //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 = - // sharp(`${extractionDirectory}${segCol.Data.Data}`) - // .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]); - // 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.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.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; - return; + // return the finalised image. + console.log(`Width: ${rowPixelWidth} Height: ${columnPixelHeight}`) + return await finalImage + // .resize( + // rowPixelWidth, + // columnPixelHeight + // ) + // .crop(sharp.gravity.northwest); }; @@ -154,7 +172,7 @@ const main: Function = function() { let requestCount: number = 0; for (const bound of TEMP_REQUESTED_BOUNDS) { extractAndStitch( - findRelatedTiles(layout.c_values[TEMP_C_VALUE].height_map[ZOOM_LEVEL], bound), bound + 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++}`)); } } -- cgit v1.2.1 From b9c3dd19483972c68adfe15f2370485f8535fece Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Thu, 29 Mar 2018 12:22:06 +0100 Subject: Clean up some of czi.ts --- src/conversion/czi/pyramidWorker.ts | 77 ++++++++++--------------------------- 1 file changed, 20 insertions(+), 57 deletions(-) (limited to 'src/conversion/czi/pyramidWorker.ts') diff --git a/src/conversion/czi/pyramidWorker.ts b/src/conversion/czi/pyramidWorker.ts index 286fd6b..459f8f9 100644 --- a/src/conversion/czi/pyramidWorker.ts +++ b/src/conversion/czi/pyramidWorker.ts @@ -1,8 +1,8 @@ -import { CZITile, CZIHeightMap, WholeCZIHierarchy } from '../types/customPyramidIndex'; +import { CZIHeightMap, CZITile, WholeCZIHierarchy } from '../types/customPyramidIndex'; +import * as fs from 'fs'; 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/'; @@ -13,13 +13,12 @@ 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[][] { -const findRelatedTiles:Function = function(plane: CZIHeightMap, desired: TileBounds): CZITile[][] { - - let relatedTiles: CZITile[][] = []; + const relatedTiles: CZITile[][] = []; for (const planeTileRow of plane.plane) { let tileRow: CZITile[] = [], foundTiles: boolean = false; for (const tile of planeTileRow) { @@ -34,17 +33,17 @@ const findRelatedTiles:Function = function(plane: CZIHeightMap, desired: TileBou } } return relatedTiles; -} -let help: number = 0; +}; +const help: number = 0; const extractAndStitch: Function = async function(tiles: CZITile[][], desiredRegion: TileBounds, zoom: number): Promise { //create a buffer for the rows part of raw image data - let tileHorizontalBuffer: Buffer[] = [], bufferBound: TileBounds, rowPixelWidth:number = 0, columnPixelHeight: number = 0; + 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[] = []; + const tileRow: Buffer[] = []; let chunkToExtract: TileBounds = new TileBounds(-1, -1, -1, -1); rowPixelWidth = 0; @@ -55,7 +54,7 @@ const extractAndStitch: Function = async function(tiles: CZITile[][], desiredReg rowPixelWidth += (chunkToExtract.right - chunkToExtract.left); //extract this section of image data into a sharp buffer - let data: Promise = + const data: Promise = sharp(`${processedImages}${tile.file}`) .extract({ left: chunkToExtract.left, @@ -75,17 +74,17 @@ const extractAndStitch: Function = async function(tiles: CZITile[][], desiredReg row[0].x_offset, row[0].x_offset + row[0].width, row[0].y_offset, - row[0].y_offset + row[0].height, - ), desiredRegion, zoom); + 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)}); + {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()); + {top: 0, left: ((bufferBound.right - bufferBound.left) + ((index - 1) * TILE_SIZE))}).toBuffer()); } //add this horizontal data buffer to the vertical buffer @@ -100,22 +99,22 @@ const extractAndStitch: Function = async function(tiles: CZITile[][], desiredReg 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); + ), 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}); + {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()); + {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 + console.log(`Width: ${rowPixelWidth} Height: ${columnPixelHeight}`); + return finalImage; // .resize( // rowPixelWidth, // columnPixelHeight @@ -123,42 +122,6 @@ const extractAndStitch: Function = async function(tiles: CZITile[][], desiredReg // .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`); @@ -175,5 +138,5 @@ const main: Function = function() { 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(); -- cgit v1.2.1 From 426befe551d563f7394f67e9053705a2bdd21126 Mon Sep 17 00:00:00 2001 From: cjd24 Date: Thu, 29 Mar 2018 12:47:59 +0100 Subject: Small changes to fix up last type stuff --- src/conversion/czi/pyramidWorker.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'src/conversion/czi/pyramidWorker.ts') diff --git a/src/conversion/czi/pyramidWorker.ts b/src/conversion/czi/pyramidWorker.ts index 459f8f9..81110e7 100644 --- a/src/conversion/czi/pyramidWorker.ts +++ b/src/conversion/czi/pyramidWorker.ts @@ -4,6 +4,7 @@ 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/`; -- cgit v1.2.1 From 709d6ec75364a51498139ed37f40b297e5fc9766 Mon Sep 17 00:00:00 2001 From: cjd24 Date: Sun, 1 Apr 2018 19:05:00 +0100 Subject: first working iteration of final tile extraction --- src/conversion/czi/pyramidWorker.ts | 167 +++++++++++------------------------- 1 file changed, 50 insertions(+), 117 deletions(-) (limited to 'src/conversion/czi/pyramidWorker.ts') 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 { - //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 { - //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 = - 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 + }; +} -- cgit v1.2.1 From 740536ef8a9d3622180aa81c167a25e3c419fd26 Mon Sep 17 00:00:00 2001 From: cjd24 Date: Sun, 1 Apr 2018 21:42:18 +0100 Subject: Swapped over the zoom tier section to use vips directly --- src/conversion/czi/pyramidWorker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/conversion/czi/pyramidWorker.ts') diff --git a/src/conversion/czi/pyramidWorker.ts b/src/conversion/czi/pyramidWorker.ts index aea7936..fdaa038 100644 --- a/src/conversion/czi/pyramidWorker.ts +++ b/src/conversion/czi/pyramidWorker.ts @@ -39,7 +39,7 @@ const getFinalTile: Function = async function(imageDir: string, imageTier: CZIHe let involvedTiles: string = ''; for (const row of imageTier.plane) { for (const tile of row) { - involvedTiles += `${imageDir}${tile.file} ` + involvedTiles += `${imageDir}/data/${tile.file} ` } } -- cgit v1.2.1 From 139b0299d016f8dab6738333c767a3add47bfc14 Mon Sep 17 00:00:00 2001 From: cjd24 Date: Mon, 2 Apr 2018 04:37:05 +0100 Subject: Working on converting to Vips, didnt commit czi in this one as is sadly broken --- src/conversion/czi/pyramidWorker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/conversion/czi/pyramidWorker.ts') diff --git a/src/conversion/czi/pyramidWorker.ts b/src/conversion/czi/pyramidWorker.ts index fdaa038..19acbf9 100644 --- a/src/conversion/czi/pyramidWorker.ts +++ b/src/conversion/czi/pyramidWorker.ts @@ -44,7 +44,7 @@ const getFinalTile: Function = async function(imageDir: string, imageTier: CZIHe } let outputFileName: string =`${imageDir}/tmp/${uuid.generate()}.png` - require('shelljs').exec(`./vips arrayjoin "${involvedTiles}" ${outputFileName} --across ${imageTier.plane[0].length}`) + require('shelljs').exec(`vips arrayjoin "${involvedTiles}" ${outputFileName} --across ${imageTier.plane[0].length}`) let extractionRegion = new TileBounds( desiredRegion.left - imageTier.plane[0][0].width, -- cgit v1.2.1 From 32dede1bd9b84b46ffba8939cdb98641d06c4f20 Mon Sep 17 00:00:00 2001 From: cjd24 Date: Fri, 20 Apr 2018 22:02:44 +0100 Subject: Seems to have got the big boi CZI working again, added support for lecia files --- src/conversion/czi/pyramidWorker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/conversion/czi/pyramidWorker.ts') diff --git a/src/conversion/czi/pyramidWorker.ts b/src/conversion/czi/pyramidWorker.ts index 19acbf9..5e6911c 100644 --- a/src/conversion/czi/pyramidWorker.ts +++ b/src/conversion/czi/pyramidWorker.ts @@ -1,5 +1,5 @@ import { CZIHeightMap, CZITile, CZITileRequest } from '../types/customPyramidIndex'; -import { TileBounds } from '../types/helperCZITypes'; +import { TileBounds, execpaths } from '../types/helpers'; import * as sharp from 'sharp'; import { isTileRelated } from './tileExtraction'; import { uuid } from '../../uuid' @@ -44,7 +44,7 @@ const getFinalTile: Function = async function(imageDir: string, imageTier: CZIHe } let outputFileName: string =`${imageDir}/tmp/${uuid.generate()}.png` - require('shelljs').exec(`vips arrayjoin "${involvedTiles}" ${outputFileName} --across ${imageTier.plane[0].length}`) + require('shelljs').exec(`${execpaths} vips arrayjoin "${involvedTiles}" ${outputFileName} --across ${imageTier.plane[0].length}`) let extractionRegion = new TileBounds( desiredRegion.left - imageTier.plane[0][0].width, -- cgit v1.2.1