diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/conversion/czi/czi.ts | 12 | ||||
| -rw-r--r-- | src/conversion/czi/pyramidWorker.ts | 160 | ||||
| -rw-r--r-- | src/conversion/czi/tileExtraction.ts | 7 |
3 files changed, 101 insertions, 78 deletions
diff --git a/src/conversion/czi/czi.ts b/src/conversion/czi/czi.ts index b27cfec..ea22c0a 100644 --- a/src/conversion/czi/czi.ts +++ b/src/conversion/czi/czi.ts @@ -164,7 +164,7 @@ const orderSegments: Function = function(segments: Segment[]): Segment[][] { const findRegionToExtract: Function = function(segment: Segment, desired: TileBounds): TileBounds { //return the slice within the base tile for extraction - return regionToExtract(getOriginalTileBounds(segment) , desired); + return regionToExtract(getOriginalTileBounds(segment), desired, 1); }; /* @@ -564,7 +564,7 @@ const extrapolateDimension:Function = async function(cVal: number, totalCs: numb let baseCZIHeightMap: CZIHeightMap = {zoom_level: 1, plane: []}, width_in_tiles = 0, height_in_tiles = 0; - console.log(`Stage (${cVal * Math.sqrt(maxZoom) + 1}/${totalCs}):`); + console.log(`Stage (${cVal * Math.log2(maxZoom) + 1}/${totalCs}):`); //For all rows within the total base pixel height for (;ys < totalSizeY;) { timing = process.hrtime(); @@ -609,8 +609,8 @@ const extrapolateDimension:Function = async function(cVal: number, totalCs: numb console.log(">> Base tier complete for \'C\': " + cVal + "; Begin computing zoom tiers...\n") let retHeightMap: CZIHeightMap[] = [baseCZIHeightMap]; - for (let stage:number = 1; stage < Math.sqrt(maxZoom); stage++) { - console.log(`Stage (${cVal * Math.sqrt(maxZoom) + 1 + stage}/${totalCs}):`); + 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) => { @@ -678,7 +678,7 @@ const buildCustomPyramids:Function = async function buildCustomPyramids(): Promi { //set some variables for errors and and percent counts let errorOccurred: Boolean; - finalCZIJson.zoom_level_count = Math.sqrt(maxZoom); + finalCZIJson.zoom_level_count = Math.log2(maxZoom); finalCZIJson.c_values = [] as any; finalCZIJson.c_value_count = 0; finalCZIJson.total_files = 0; @@ -689,7 +689,7 @@ const buildCustomPyramids:Function = async function buildCustomPyramids(): Promi 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.sqrt(maxZoom), maxZoom) + 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 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<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()); + } -const extractAndStitch: Function = async function(segments: CZITile[][], 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({ - // 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++}`)); } } diff --git a/src/conversion/czi/tileExtraction.ts b/src/conversion/czi/tileExtraction.ts index 9c87dd9..be8bd3c 100644 --- a/src/conversion/czi/tileExtraction.ts +++ b/src/conversion/czi/tileExtraction.ts @@ -44,7 +44,7 @@ export const isTileRelated:Function = function(origCoords: TileBounds, desired: * 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): TileBounds { +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); @@ -81,6 +81,11 @@ export const regionToExtract: Function = function(baseBounds: TileBounds, desire 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; }; |
