1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
|
import { CZIHeightMap, CZITile,
WholeCZIHierarchy } from '../types/customPyramidIndex';
import { Dimension, Segment } from '../types/cziBinaryTypings';
import * as fs from 'fs';
import { SupportedViews, TileBounds, checkForOutputDirectories, writeJSONToFile, execpaths } from '../types/helpers';
import { logger, Logger } from '../../logger';
import * as sharp from 'sharp';
// tslint:disable-next-line:no-duplicate-imports
import { SharpInstance } from 'sharp';
import { isTileRelated, regionToExtract } from '../types/tileExtraction';
import * as parsexml from 'xml-parser';
import { uuid } from '../../uuid'
import { queue as jobQueue, Promiser as Job } from '../../ppq'
import { profiler } from '../../profiler';
import { exec } from '../types/exec'
let log: Logger;
const THREAD_LIMIT: number = 2;
sharp.concurrency(THREAD_LIMIT);
const readFile = require('util').promisify(fs.readFile);
const readdir = require('util').promisify(fs.readdir);
const exists = require('util').promisify(fs.exists);
const queueSize = profiler.metric({
name: "Queued Image Jobs",
agg_type: "sum",
value : () => jobQueue.size
})
// Various constants for placing files and defining tiles
let extractDirectory: string = "";
let outputImageData: string = "";
let outputImageDirectory: string = "";
let supportedViews: SupportedViews;
let totalSizeX: number = -1;
let totalSizeY: number = -1;
let fileName: string = "unset filename";
const tileOverlap: number = 0; // Overlap is only half implemented
const tileSize: number = 1024;
const maxZoom: number = 64;
const shell = require('shelljs');
const finalCZIJson: WholeCZIHierarchy = {
c_value_count: 0,
total_files: 0,
complete: false,
c_values: [],
zoom_level_count: 0
};
let dataBlocks: Segment[] = [];
/**
* Simple function to find the tile bounds version of an original tile
* in the base image
*/
const getOriginalTileBounds: (originalTile: Segment) => TileBounds = (
originalTile: Segment
): TileBounds => {
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;
continue;
default:
continue;
}
}
// 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
) {
throw new Error(
`One of the bounds had a dimension less than zero: ${originalTile.Data.Data}`
);
}
// 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: (activeSegments: Segment[], desired: TileBounds) =>
Segment[] = (
activeSegments: Segment[], desired: TileBounds
): Segment[] => {
const relatedTiles: Segment[] = [];
// For all tiles within the region being checked
for (const baseTile of activeSegments) {
if (isTileRelated(getOriginalTileBounds(baseTile), desired)) {
relatedTiles.push(baseTile);
}
}
return relatedTiles;
};
/**
* This gets the particular dimension from a segment given a segment
* and dimension ID
*/
const getDimension: (segment: Segment, name: string) => Dimension = (
segment: Segment, name: string
): Dimension => {
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: (segments: Segment[]) => Segment[][] = (
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) {
throw new Error(`Segment array contains multiple values of C -\
this overlay is currently unsupported within this method`);
}
xCoords.push(getDimension(seg, 'X').Start);
yCoords.push(getDimension(seg, 'Y').Start);
}
// Filter to get Unique Values
const filter: (item: number, index: number, array: number[]) => boolean = (
item: number, index: number, array: number[]
): boolean => array.indexOf(item) === index;
xCoords = xCoords.filter(filter);
yCoords = yCoords.filter(filter);
// Use the filtered list to order the segments into rows
for (const yVal of yCoords) {
// Get all segments with the same y value
const row: Segment[] = [];
for (const seg of segments) {
if (getDimension(seg, 'Y').Start === yVal) {
row.push(seg);
}
}
// Sort the row of y values by x value
const sortedRow: Segment[] = [];
for (const xVal of xCoords) {
for (const seg of row) {
if (getDimension(seg, 'X').Start === xVal) {
sortedRow.push(seg);
break;
}
}
}
// Push this to the finalised grid
output.push(sortedRow);
}
// Return the result
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: (
segmanet: Segment, desired: TileBounds
) => TileBounds = (
segment: Segment, desired: TileBounds
): TileBounds => {
// 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: (
segments: Segment[][], desiredRegion: TileBounds
) => Promise<SharpInstance> = async(
segments: Segment[][], desiredRegion: TileBounds
): Promise<SharpInstance> => {
// Create a buffer for the rows part of raw image data
const tileVerticalBuffer: Buffer[] = [];
// For all ros (y coords) in the image
for (const segRow of segments) {
// Create a buffer for this row
const 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);
let localSharp: SharpInstance = sharp(`${extractDirectory}${segCol.Data.Data}`);
// Extract this section of image data into a sharp buffer
let data: Job<Buffer> =
localSharp
.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 jobQueue.enqueue(10, data, localSharp));
}
// Prepare variables to squash all of the column data into one, for this row
let imageVerticalSlice: SharpInstance = sharp(tileRow[0]);
const bufferBound: TileBounds = 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 = imageVerticalSlice.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: number = 1; index < segRow.length; index++) {
imageVerticalSlice = 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 jobQueue.enqueue(10, imageVerticalSlice.toBuffer as Job<Buffer>, imageVerticalSlice));
}
// Prepare variables used to combine all rows into one image
let finalImage: SharpInstance = sharp(tileVerticalBuffer[0]);
const bufferBound: TileBounds =
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 = finalImage
.background({
r: 0,
g: 0,
b: 0,
alpha: 1
}).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: number = 1; index < segments.length; index++) {
finalImage = finalImage.overlayWith(
tileVerticalBuffer[index],
{top: getDimension(segments[index - 1][0], 'Y').Size, left: 0});
}
// Return the finalised image.
if ((await jobQueue.enqueue(5, finalImage.metadata as Job<sharp.Metadata>, finalImage)).channels != 4) {
return sharp(Buffer.alloc(tileSize, tileSize), {
create: {
width: tileSize,
height: tileSize,
channels: 4,
background: { r: 0, g: 0, b: 0, alpha: 1 }
}
}).overlayWith(await jobQueue.enqueue(5, finalImage.toBuffer as Job<Buffer>, finalImage));
} else {
return finalImage;
}
// .toFile("/cs/home/cjd24/Documents/current/groupPractical/project-code/src/conversion/czi/test1.png");
}
/**
* Used to remove obtuse JSON data,
* where it is not needed in the SupportedView object
*/
const cleanSupportedViewObject: (obj: parsexml.Node[]) => object = (
obj: parsexml.Node[]
): object => {
const newChild: any = {};
let hasAttr: boolean = false;
let hasChildren: boolean = false;
let hasContent: boolean = false;
// For all nodes in the json object
for (const node of obj) {
// Check if this node has attributes
hasAttr = (node.attributes !== undefined
&& node.attributes !== null
&& Object.keys(node.attributes).length !== 0
);
// Check for further children
hasChildren = (node.children !== undefined && node.children.length > 0);
// Check for simple content object
hasContent = (node.content !== undefined
&& node.content !== null
&& node.content !== '');
// 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);
}
} 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;
} else if (hasContent) {
newChild[node.name] = node.content;
}
}
return newChild;
};
/**
* Clean up all vales of C within the JSON for supported views
*/
const cleanSupportedViews: (view: SupportedViews) => void = (
view: SupportedViews
): void => {
if (!view.scalable_image) {
throw new Error("SupportedView.scalable_image does not exist where it should.");
}
for (const channel of view.scalable_image.channels) {
channel.metadata = cleanSupportedViewObject(
channel.metadata as parsexml.Node[]
);
}
};
/* tslint:disable:curly cyclomatic-complexity */
/*
* Function to parse the xml file from the original extraction from CZI,
* its nasty!
*/
const parseExtractedXML: (xmlFile: string) => void = async(
xmlFile: string
): Promise<void> => {
const metaXML: parsexml.Document = parsexml(await readFile(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 :'(
if (!supportedViews.scalable_image) {
throw new Error("SupportedView.scalable_image does not exist where it should.");
}
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
});
}
}
}
break;
}
break;
}
break;
}
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') {
switch (dist.attributes.Id) {
case '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;
} else if (attr.name === 'DefaultUnitFormat') {
supportedViews.scalable_image.real_width.units = attr.content;
}
}
break;
}
case '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;
} else if (attr.name === 'DefaultUnitFormat') {
supportedViews.scalable_image.real_height.units = attr.content;
}
}
break;
}
}
}
break;
}
break;
}
break;
}
supportedViews.scalable_image.metadata = metaXML;
};
/* tslint:enable:curly cyclomatic-complexity */
////////////////////////////////////////////////////////////////////////////////
// Begin work on getting the new tiles all nice
////////////////////////////////////////////////////////////////////////////////
/**
* This is used to create the next zoomed out tier of the given height map
*/
const zoomTier: (
previousHeightMap: CZIHeightMap, p: number, c: number
) => Promise<CZIHeightMap> = async(
previousHeightMap: CZIHeightMap, p: number, c: number
): Promise<CZIHeightMap> => {
const squashRow = async (previousPlane: CZITile[][], ys: number, p: number): Promise<CZITile[]> => {
const squashQuad = async (previousPlane: CZITile[][], ys: number, xs: number, p: number): Promise<CZITile> => {
if (await exists(`${outputImageDirectory}img-c${c}-p${p}-y${ys * tileSize}-x${xs * tileSize}.png`) && (ys < previousPlane.length)) {
finalCZIJson.total_files++;
return {
x_offset: xs * tileSize,
y_offset: ys * tileSize,
width: tileSize,
height: tileSize,
file: `img-c${c}-p${p}-y${ys}-x${xs}.png`
};
}
// Combine 4 tiles into a new tile and scale down for this tier
// Top left
let quadrents:string = `${outputImageDirectory}${previousPlane[ys][xs].file} `;
let across:number = 1;
let quadrentCount: number = 1;
// Combine top right
const moreToRight: boolean = (xs + 1) < previousPlane[0].length;
if (moreToRight) {
quadrents += `${outputImageDirectory}${previousPlane[ys][xs + 1].file} `;
across++; quadrentCount++;
}
// Combine bottom left
const moreToBottom: boolean = (ys + 1) < previousPlane.length;
if (moreToBottom) {
quadrents += `${outputImageDirectory}${previousPlane[ys + 1][xs].file} `;
quadrentCount++;
}
// Combine bottom right
if (moreToBottom && moreToRight) {
quadrents += `${outputImageDirectory}${previousPlane[ys + 1][xs + 1].file} `;
}
let outputFileName: string =`${outputImageData}/tmp/${uuid.generate()}.png`;
try {
await new Promise((res) => {
shell.exec(`${execpaths} vips arrayjoin "${quadrents}" ${outputFileName} --across ${across} --vips-concurrency=${THREAD_LIMIT}`);
res();
});
} catch (err) {
if (err) {
log.fatal(err);
}
}
// Rescale and push to file
if (quadrentCount !== 3) {
let extRight: number = 0, extBottom: number = 0;
if (moreToRight) {
extBottom = tileSize;
} else if (moreToBottom){
extRight = tileSize;
}
let fullTileQuadRef: SharpInstance = sharp(outputFileName)
.background({r: 0, g: 0, b: 0, alpha: 1})
.extend({
top: 0, left: 0,
bottom: extBottom, right: extRight
});
let fullTileQuad: Buffer = await jobQueue.enqueue(5, fullTileQuadRef.toBuffer as Job<Buffer>, fullTileQuadRef);
let rescaledTileQuadRef: SharpInstance = sharp(fullTileQuad)
.resize(tileSize, tileSize);
await jobQueue.enqueue(5, rescaledTileQuadRef.toFile as Job<sharp.OutputInfo>, rescaledTileQuadRef,
`${outputImageDirectory}img-c${c}-p${p}-y${ys * tileSize}-x${xs * tileSize}.png`);
} else {
let fullTileQuadRef: SharpInstance = sharp(outputFileName)
.resize(tileSize, tileSize)
await jobQueue.enqueue(5, fullTileQuadRef.toFile as Job<sharp.OutputInfo>, fullTileQuadRef,
`${outputImageDirectory}img-c${c}-p${p}-y${ys * tileSize}-x${xs * tileSize}.png`);
}
fs.unlink(`${outputFileName}`, (err) => {
if (err) {
log.warn(fileName + ' Error when deleting redundant resource: \n' + err.stack)
}
});
finalCZIJson.total_files++;
return {
x_offset: previousPlane[ys][xs].x_offset,
y_offset: previousPlane[ys][xs].y_offset,
width: previousPlane[ys][xs].width * 2,
height: previousPlane[ys][xs].height * 2,
file: `img-c${c}-p${p}-y${ys * tileSize}-x${xs * tileSize}.png`
};
}
const cziRow: Promise<CZITile>[] = [];
// For all columns within the previous plane
for (let xs: number = 0; xs < previousPlane[0].length; xs += 2) {
cziRow.push(squashQuad(previousPlane, ys, xs, p));
}
return await Promise.all(cziRow);
}
const 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) {
log.notice(fileName +
' Task(1/1): ' +
'Stage 100% complete. ' +
'Stage was skipped as maximum sensible zoom level was reached.'
);
return previousHeightMap;
}
const newplane: CZITile[][] = [];
// For all rows within the previous plane
for (let ys: number = 0; ys < previousHeightMap.plane.length; ys += 2) {
newplane.push(await squashRow(previousHeightMap.plane, ys, p));
log.debug(fileName +
` Task(${ys / 2 + 1}/${
Math.floor(previousHeightMap.plane.length / 2) + 1}): ` +
`Stage ${Math.ceil(((ys / previousHeightMap.plane.length)
* 100) * 100) / 100}% complete. `
);
}
newZoomTier.plane = newplane;
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: (
cVal: number, totalCs: number, maxZoom: number
) => Promise<CZIHeightMap[]> = async(
cVal: number, totalCs: number, maxZoom: number
): Promise<CZIHeightMap[]> => {
const doOneRow = (ys: number, filteredDataBlocks: Segment[]): Promise<CZITile[]> => {
const doOneBaseTile = async(xs: number, ys: number, filteredDataBlocks: Segment[]): Promise<CZITile> => {
if (await exists(`${outputImageDirectory}img-c${cVal}-p0-y${ys}-x${xs}.png`) && (ys + tileSize < totalSizeY)) {
///////////////////////////////////////////////////////////////////////////////////This if statement should be temporary??????????????????????????????????/
finalCZIJson.total_files++;
return {
x_offset: xs,
y_offset: ys,
width: tileSize,
height: tileSize,
file: `img-c${cVal}-p0-y${ys}-x${xs}.png`
};
}
const desired: TileBounds = new TileBounds(
xs - tileOverlap,
xs + tileSize + tileOverlap,
ys - tileOverlap,
ys + tileSize + tileOverlap
);
const sortedSegments: Segment[][] =
orderSegments(findRelatedTiles(filteredDataBlocks, desired));
(await extractAndStitchChunks(sortedSegments, desired))
.toFile(`${outputImageDirectory}img-c${cVal}-p0-y${ys}-x${xs}.png`);
return {
x_offset: xs,
y_offset: ys,
width: tileSize,
height: tileSize,
file: `img-c${cVal}-p0-y${ys}-x${xs}.png`
};
}
const rowPromises: Promise<CZITile>[] = [];
for (let xs: number = 0; xs < totalSizeX; xs += tileSize) {
rowPromises.push(doOneBaseTile(xs, ys, filteredDataBlocks));
finalCZIJson.total_files++;
}
return Promise.all(rowPromises);
}
const filteredDataBlocks: Segment[] = dataBlocks.filter(
(item: Segment, index: number, array: Segment[]) =>
getDimension(item, 'C').Start === cVal
);
// Begin output for the base tier
// console.log('=========================================================');
log.notify(`${fileName} > Beginning Dimension Extrapolation for \'C\' : ${cVal}`);
// console.log('=========================================================');
// console.log('>> Computing base tier, this will take a while...\n');
const baseCZIHeightMap: CZIHeightMap = {zoom_level: 1, plane: []};
log.notify(`${fileName} > Stage (${cVal * Math.log2(maxZoom) + 1}/${totalCs}):`);
// For all rows within the total base pixel height
const plane: CZITile[][] = [];
for (let ys: number = 0; ys < totalSizeY; ys += tileSize) {
plane.push(await doOneRow(ys, filteredDataBlocks));
process.stdout.write("\r");
log.info(fileName +
` Task(${ys / tileSize}/${Math.ceil(totalSizeY / tileSize)}): ` +
`Stage ${Math.ceil(((ys / totalSizeY) * 100) * 100) / 100}% complete. `
);
}
baseCZIHeightMap.plane = plane;
baseCZIHeightMap.tile_height_count = baseCZIHeightMap.plane.length;
baseCZIHeightMap.tile_width_count = baseCZIHeightMap.plane[0].length;
// BUILD UP THE HIGHER ZOOM LAYERS.
// console.log(`>> Base tier complete for \'C\': ${cVal
// }; Begin computing zoom tiers...\n`);
const retHeightMap: CZIHeightMap[] = [baseCZIHeightMap];
writeJSONToFile(`${outputImageData}/intermediate-stage-map.json`, retHeightMap);
for (let stage: number = 1; stage <= Math.log2(maxZoom); stage++) {
log.notify(`${fileName} > Stage (${cVal * Math.log2(maxZoom) + 1 + stage}/${totalCs}):`);
await zoomTier(retHeightMap[stage - 1], stage, cVal)
.then((v: CZIHeightMap) => {
retHeightMap.push(v);
return;
}
);
writeJSONToFile(`${outputImageData}/intermediate-stage-map.json`, retHeightMap);
}
log.notify(`${fileName} > Completed Extrapolation for dimension, \'C\': ${cVal}`);
return retHeightMap;
};
/**
* used to write the supported views object to file both in total and "cleaned"
*/
const createSupportedViewsObject: (skipping: boolean) => void = async(skipping: boolean): Promise<void> => {
dataBlocks = require(`${extractDirectory}outputjson.json`)
.czi.filter((s: Segment) =>
s.Id === 'ZISRAWSUBBLOCK' && s.Data.Data !== 'empty'
);
if (!skipping) {
supportedViews = {
scalable_image: {
width: -1,
height: -1,
channels: [{
channel_id: '-1',
channel_name: ''
}],
metadata: {}
}
};
if (!supportedViews.scalable_image) {
throw new Error("SupportedView.scalable_image does not exist where it should.");
}
supportedViews.scalable_image.channels.pop();
// Parse the xml
await parseExtractedXML(`${extractDirectory}FILE-META-1.xml`);
// Create the total supported Views object
// !!!!! This line was removed in order to reduce the output file size and
// !!!!! increase the usability by only giving the striped down version
// writeJSONToFile(`${outputImageData}/supported_views.json`, supportedViews);
// Cleanup and create "smart" supported views object without whole metadata
cleanSupportedViews(supportedViews);
if (!supportedViews.scalable_image) {
throw new Error("SupportedView.scalable_image does not exist where it should.");
}
supportedViews.scalable_image.metadata = {
tile_size: tileSize,
max_zoom: maxZoom,
tile_overlap: tileOverlap
};
writeJSONToFile(`${outputImageData}/supported_views.json`, supportedViews);
}
};
/**
* Used to build up a pyramid for each value of c from the base images
*/
const buildCustomPyramids: () => Promise<boolean> = async(): Promise<boolean> => {
// Set some variables for errors and and percent counts
let successfulBuild: boolean = false;
finalCZIJson.zoom_level_count = Math.log2(maxZoom) + 1;
// For all channels in the base image
if (!supportedViews.scalable_image) {
throw new Error("SupportedView.scalable_image does not exist where it should.");
}
for (const cval of supportedViews.scalable_image.channels) {
successfulBuild = true;
if (successfulBuild) {
// 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 * 2),
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.message);
console.log(err.stack);
}
successfulBuild = false;
}
);
} else {
log.error(`${fileName} > An error occurred while extracting dimension: ${cval}`);
log.warn(`${fileName} > 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);
}
if (successfulBuild) {
finalCZIJson.complete = true;
}
writeJSONToFile(`${outputImageData}/layout.json`, finalCZIJson);
return successfulBuild;
};
const initialExtractAndConvert: (absFilePath: string, space: string) => Promise<void> = async (absFilePath: string, space: string): Promise<void> => {
checkForOutputDirectories([outputImageData, extractDirectory]);
log.silly(`${fileName} > This will complete at roughly 2GB/min`);
await exec(`${execpaths} CZICrunch "${absFilePath}" "${extractDirectory}"`);
await exec(`${execpaths} python3 ./ext/bin/convertJxrs.py "${extractDirectory}"`);
// let totalFiles: number = 0, counter: number = 0;
// console.log("=========== BEGIN JXR CONVERSION ===========")
//
// let filenames: string[] = (await readdir(extractDirectory)).filter((x: string) => x.endsWith(".jxr"));
// counter = 0
// totalFiles = filenames.length;
//
// const doOne: (n: number, file: string, asTif: string) => Promise<void> = async(n: number, file: string, asTif: string): Promise<void> => {
// shell.exec(`${execpaths} JxrDecApp -i ${file} -o ${asTif}`);
// shell.exec(`convert ${asTif} ${file.substring(0, file.lastIndexOf('.'))}.png`);
// shell.exec(`rm ${file} ${asTif}`);
// console.log (`> Processed File: ${n}/${totalFiles} : ${file} --> ${file.substring(0, file.lastIndexOf('.'))}.png`);
// }
//
// try {
// let promises: Promise<void>[] = [];
// for (let index: number = 0; index < filenames.length; index++) {
//
// if (promises.length < 30) {
// let file = extractDirectory + filenames[index];
// promises.push(doOne(counter++, file, file.substring(0, file.lastIndexOf('.')) + ".tif"));
// } else {
// index--;
// await Promise.all(promises);
// promises.length = 0;
// }
// }
// await Promise.all(promises);
// } catch (err) {
// console.log("Processing Failed. Probably couldn't find decoder due to invalid system environment path, check this before debugging. Actual Err:");
// console.error(err);
// }
// console.log("=========== FINISH JXR CONVERSION ===========")
}
process.on("warning", (err: any) => console.warn(err))
/**
* Main function used to call the rest of the relevant code to crunch a CZI
* extraction.
*/
export const main: (original: string, space:string) => Promise<void> = async (original: string, space:string): Promise<void> => {
fileName = `${original.split('/')[original.split('/').length - 1]}`;
log = logger.for({component: "CZI Crunch", targetFile: original});
log.notice("CZI Convertor Received new file: " + fileName);
try {
outputImageData = space;
extractDirectory = outputImageData + "/extract/";
outputImageDirectory = outputImageData + "/data/";
if (!(await exists(`${outputImageData}/supported_views.json`))) {
log.information(`${fileName} > Begin Extracting and Converting to PNG`);
await initialExtractAndConvert(original, space);
log.information(`${fileName} > Checking/Creating output directories...`);
checkForOutputDirectories([outputImageDirectory, `${outputImageData}/tmp/`]);
log.information(`${fileName} > Creating Supported Views and writing files...`);
await createSupportedViewsObject(false);
} else {
log.notify("It appears as though this CZI has already been extracted, loading files...");
await createSupportedViewsObject(true);
supportedViews = require(`${outputImageData}/supported_views.json`);
}
if (!supportedViews.scalable_image) {
throw new Error("lol this wont ever show :)");
}
totalSizeX = supportedViews.scalable_image.width;
totalSizeY = supportedViews.scalable_image.height;
if (await buildCustomPyramids()) {
log.alert(`UNCOMMENT ME AGAIN ${fileName} > Removing the initial extraction bloat`);
// shell.exec(`rm -rf ${outputImageData}/extract/`)
};
log.success(`${fileName} > Done`);
} catch (Err) {
log.critical(`${fileName} > This was a really big boi error, and something is sad: `);
log.critical(Err);
throw Err;
}
};
// console.log("REMEMBER TO REMOVE THE MAIN CALL AGAIN MR GOOSEMUN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
//main("/cs/scratch/cjd24/0702.czi", "/cs/scratch/cjd24/0702-extract");
// /* tslint:disable */
// async function main2() {
//
// let layout: WholeCZIHierarchy = require(`${outputImageData}layout.json`);
//
// for (let stage:number = 1; stage < Math.sqrt(maxZoom); stage++) {
// await zoomTier(layout.c_values[0].height_map[stage - 1])
// .then((v: CZIHeightMap) =>
// {
// writeJSONToFile(`${outputImageData}stageMap-${stage}.json`, v);
// return;
// }
// );
// }
// }
//
// main2().then().catch(e => console.log(e));
|