Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datasource/precomputed): check PNG image dimensions less strictly #654

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/neuroglancer/json_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ class _Map(Map):


def typed_set(wrapped_type: Callable[[Any], T]):
def wrapper(x, _readonly=False) -> Callable[[Any], Union[set[T], frozenset[T]]]:
def wrapper(x, _readonly=False) -> Union[set[T], frozenset[T]]:
set_type = frozenset if _readonly else set
kwargs: dict[str, Any] = dict()
if hasattr(wrapped_type, "supports_readonly"):
Expand Down
9 changes: 5 additions & 4 deletions src/async_computation/decode_jpeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,22 @@ registerAsyncComputation(
data: Uint8Array,
width: number | undefined,
height: number | undefined,
area: number | undefined,
numComponents: number | undefined,
convertToGrayscale: boolean,
) => {
const parser = new JpegDecoder();
parser.parse(data);
// Just check that the total number pixels matches the expected value.
if (
width !== undefined &&
height !== undefined &&
parser.width * parser.height !== width * height
(width !== undefined && width !== parser.width) ||
(height !== undefined && height !== parser.height) ||
(area !== undefined && parser.width * parser.height !== area)
) {
throw new Error(
"JPEG data does not have the expected dimensions: " +
`width=${parser.width}, height=${parser.height}, ` +
`expected width=${width}, expected height=${height}`,
`expected width=${width}, expected height=${height}, expected area=${area}`,
);
}
width = parser.width;
Expand Down
21 changes: 11 additions & 10 deletions src/async_computation/decode_jpeg_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
import type { DecodedImage } from "#src/async_computation/decode_png_request.js";
import { asyncComputation } from "#src/async_computation/index.js";

export const decodeJpeg =
asyncComputation<
(
data: Uint8Array,
width: number | undefined,
height: number | undefined,
numComponents: number | undefined,
convertToGrayscale: boolean,
) => DecodedImage
>("decodeJpeg");
export const decodeJpeg = asyncComputation<
(
data: Uint8Array,
width: number | undefined,
height: number | undefined,
// Expected width * height
area: number | undefined,
numComponents: number | undefined,
convertToGrayscale: boolean,
) => DecodedImage
>("decodeJpeg");
3 changes: 3 additions & 0 deletions src/async_computation/decode_png.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ registerAsyncComputation(
data: Uint8Array,
width: number | undefined,
height: number | undefined,
// Expected width * height
area: number | undefined,
numComponents: number | undefined,
bytesPerPixel: number,
convertToGrayscale: boolean,
Expand All @@ -32,6 +34,7 @@ registerAsyncComputation(
data,
width,
height,
area,
numComponents,
bytesPerPixel,
convertToGrayscale,
Expand Down
23 changes: 12 additions & 11 deletions src/async_computation/decode_png_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ export interface DecodedImage {
uint8Array: Uint8Array;
}

export const decodePng =
asyncComputation<
(
data: Uint8Array,
width: number | undefined,
height: number | undefined,
numComponents: number | undefined,
bytesPerPixel: number,
convertToGrayscale: boolean,
) => DecodedImage
>("decodePng");
export const decodePng = asyncComputation<
(
data: Uint8Array,
width: number | undefined,
height: number | undefined,
// Expected width * height
area: number | undefined,
numComponents: number | undefined,
bytesPerPixel: number,
convertToGrayscale: boolean,
) => DecodedImage
>("decodePng");
2 changes: 2 additions & 0 deletions src/datasource/deepzoom/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export class DeepzoomImageTileSource extends WithParameters(
new Uint8Array(responseBuffer),
undefined,
undefined,
undefined,
3,
1,
false,
Expand All @@ -133,6 +134,7 @@ export class DeepzoomImageTileSource extends WithParameters(
new Uint8Array(responseBuffer),
undefined,
undefined,
undefined,
3,
false,
);
Expand Down
5 changes: 3 additions & 2 deletions src/datasource/render/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ chunkDecoders.set(
cancellationToken,
[response],
new Uint8Array(response),
chunkDataSize[0],
chunkDataSize[1] * chunkDataSize[2],
undefined,
undefined,
chunkDataSize[0] * chunkDataSize[1] * chunkDataSize[2],
3,
true,
);
Expand Down
5 changes: 3 additions & 2 deletions src/sliceview/backend_chunk_decoders/jpeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ export async function decodeJpegChunk(
cancellationToken,
[response],
new Uint8Array(response),
chunkDataSize[0],
chunkDataSize[1] * chunkDataSize[2],
undefined,
undefined,
chunkDataSize[0] * chunkDataSize[1] * chunkDataSize[2],
chunkDataSize[3] || 1,
false,
);
Expand Down
5 changes: 3 additions & 2 deletions src/sliceview/backend_chunk_decoders/png.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ export async function decodePngChunk(
cancellationToken,
[response],
/*buffer=*/ new Uint8Array(response),
/*width=*/ chunkDataSize[0],
/*height=*/ chunkDataSize[1] * chunkDataSize[2],
/*width=*/ undefined,
/*height=*/ undefined,
/*area=*/ chunkDataSize[0] * chunkDataSize[1] * chunkDataSize[2],
/*numComponents=*/ chunkDataSize[3] || 1,
/*bytesPerPixel=*/ DATA_TYPE_BYTES[dataType],
/*convertToGrayscale=*/ false,
Expand Down
13 changes: 8 additions & 5 deletions src/sliceview/png/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export async function decompressPng(
buffer: Uint8Array,
width: number | undefined,
height: number | undefined,
area: number | undefined,
numComponents: number | undefined,
bytesPerPixel: number,
convertToGrayscale: boolean,
Expand All @@ -187,15 +188,17 @@ export async function decompressPng(
if (
(width !== undefined && sx !== width) ||
(height !== undefined && sy !== height) ||
(area !== undefined && sx * sy !== area) ||
(numComponents !== undefined && numComponents !== numChannels) ||
bytesPerPixel !== dataWidth
) {
throw new Error(
`png: Image decode parameters did not match expected chunk parameters.
Expected: width: ${width} height: ${height} channels: ${numComponents} bytes per pixel: ${bytesPerPixel}
Decoded: width: ${sx} height: ${sy} channels: ${numChannels} bytes per pixel: ${dataWidth}
Convert to Grayscale? ${convertToGrayscale}
`,
`png: Image decode parameters did not match expected chunk parameters. ` +
`Expected: width: ${width} height: ${height} area: ${area} ` +
`channels: ${numComponents} bytes per pixel: ${bytesPerPixel}. ` +
`Decoded: width: ${sx} height: ${sy} channels: ${numChannels} ` +
`bytes per pixel: ${dataWidth}. ` +
`Convert to Grayscale? ${convertToGrayscale}`,
);
}

Expand Down
Loading