-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(stack): decode stack from tiff Closes: #421 * chore: run prettier * fix: export decodeStack
- Loading branch information
Showing
11 changed files
with
100 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export * from './compute/histogram'; | ||
export * from './compute/maxImage'; | ||
export * from './compute/meanImage'; | ||
export * from './compute/medianImage'; | ||
export * from './load/decodeStack'; | ||
export * from './compute/minImage'; | ||
export * from './compute/sum'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { TestImagePath } from '../../../../test/TestImagePath'; | ||
import { decodeStack } from '../decodeStack'; | ||
|
||
test.each([ | ||
{ | ||
name: 'formats/tif/grey8-multi.tif', | ||
colorModel: 'GREY', | ||
bitDepth: 8, | ||
pages: 2, | ||
}, | ||
{ | ||
name: 'formats/tif/grey16-multi.tif', | ||
colorModel: 'GREY', | ||
bitDepth: 16, | ||
pages: 2, | ||
}, | ||
{ | ||
name: 'formats/tif/color8-multi.tif', | ||
colorModel: 'RGB', | ||
bitDepth: 8, | ||
pages: 2, | ||
}, | ||
{ | ||
name: 'formats/tif/color16-multi.tif', | ||
colorModel: 'RGB', | ||
bitDepth: 16, | ||
pages: 2, | ||
}, | ||
])('stacks with 2 images ($colorModel, bitDepth = $bitDepth)', (data) => { | ||
const buffer = testUtils.loadBuffer(data.name as TestImagePath); | ||
const stack = decodeStack(buffer); | ||
expect(stack.size).toBe(data.pages); | ||
for (const image of stack) { | ||
expect(image.colorModel).toBe(data.colorModel); | ||
expect(image.bitDepth).toBe(data.bitDepth); | ||
} | ||
}); | ||
|
||
test('invalid data format', () => { | ||
const buffer = testUtils.loadBuffer('formats/grey8.png'); | ||
expect(() => decodeStack(buffer)).toThrow('invalid data format: image/png'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import imageType from 'image-type'; | ||
|
||
import { Stack } from '../../Stack'; | ||
|
||
import { decodeStackFromTiff } from './decodeTiff'; | ||
|
||
/** | ||
* Decode input data and create stack. Data format is automatically detected. | ||
* Possible formats: tiff. | ||
* @param data - Data to decode. | ||
* @returns The decoded image. | ||
*/ | ||
export function decodeStack(data: ArrayBufferView): Stack { | ||
const typedArray = new Uint8Array( | ||
data.buffer, | ||
data.byteOffset, | ||
data.byteLength, | ||
); | ||
const type = imageType(typedArray); | ||
switch (type?.mime) { | ||
case 'image/tiff': | ||
return decodeStackFromTiff(typedArray); | ||
default: | ||
throw new RangeError(`invalid data format: ${type?.mime}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { decode } from 'tiff'; | ||
|
||
import { Stack } from '../../Stack'; | ||
import { getImageFromIFD } from '../../load/decodeTiff'; | ||
|
||
/** | ||
* Decode a TIFF and create a stack of images. | ||
* @param buffer - The data to decode. | ||
* @returns The stack of images. | ||
*/ | ||
export function decodeStackFromTiff(buffer: Uint8Array): Stack { | ||
const decoded = decode(buffer); | ||
const images = []; | ||
for (const IFD of decoded) { | ||
images.push(getImageFromIFD(IFD)); | ||
} | ||
|
||
return new Stack(images); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.