Skip to content

fix(OpenGL): read labels at their stored value in label outlines - #3766

Open
xapple wants to merge 1 commit into
Kitware:masterfrom
xapple:fix-label-outline-scale
Open

xapple wants to merge 1 commit into
Kitware:masterfrom
xapple:fix-label-outline-scale

Conversation

@xapple

@xapple xapple commented Nov 19, 2026

Copy link
Copy Markdown

Context

The WebGL label outlines (setUseLabelOutline(true)) of vtkVolumeMapper, vtkImageMapper and vtkImageResliceMapper find the label under a fragment as int(sample * 255.0). That is the label only for an 8-bit labelmap, whose texture is normalized by 255. For any other type the segment is wrong, in a way that depends on how the texture stores it:

  • Stored as float or half float (a Float32Array labelmap, or a 16-bit one without norm16): label n reads as 255·n. The outline is drawn, but its per-segment thickness (and, in the image and reslice mappers, opacity) is read past the given values, from the first entry. Per-segment thickness does nothing, and neither does hiding one segment's outline with a thickness of 0.
  • Stored as norm16 (which these mappers use for 16-bit data when EXT_texture_norm16 is available): every label below 257 reads as 0 (below 129 for signed data), and nothing is drawn. Through Cornerstone3D, a Uint16Array labelmap drew no outline at all.

Nothing documents an 8-bit limit (setLabelOutlineTextureWidth() even suggests a wider texture "if you have more than 1024 labels"), and the WebGPU volume pass already reads the label whatever its type: i32(round(centerValue.r * volumeSSBO.values[vNum].labelOutline.z)), the texture's scale.

Results

The image mapper with a Uint16Array labelmap whose segment 2 has an outline thickness of 0. Before, segment 2 is outlined anyway; after, it is drawn as with a Uint8Array:

Before (master) and after, 16-bit labels

New tests draw the same labelmap through each of the three mappers, against one baseline per mapper: stored as Uint8Array, Uint16Array, Float32Array and, for the volume and reslice mappers, Int16Array; and, for the volume and image mappers, an 8-bit one with a 2048-wide outline table. On master all but the three plain 8-bit cases fail. With the change all thirteen pass, both with SwiftShader, as in CI, and on an Apple GPU. The full suite passes.

vtk.js's test browser (Playwright's Chromium headless shell) doesn't offer EXT_texture_norm16, so the 16-bit cases run on half float there. With Playwright's full Chromium, which offers it, the 16-bit cases also fail on master and pass with the change, in all three mappers.

Changes

  • vtkVolumeFS.glsl and OpenGL/VolumeMapper: the volume uniform gets each component's texture scale and offset (volume.textureScale, volume.textureOffset), and getColorForLabelOutline() reads the label as int(round(sample * scale + offset)).
  • OpenGL/ImageMapper: a labelScale uniform, the image texture's normalization (255; 65535 or 32767 for unsigned or signed norm16; 1 for float and half float). Not getShiftAndScale(), which maps signed samples half a unit off for the color lookup (see below) and would put each signed label on a rounding boundary.
  • OpenGL/ImageResliceMapper: labelScale<i> and labelOffset<i> uniforms per input, used by the single-input, multi-input and slab label outlines, including the slab outline's label-to-color lookup and its test for an empty slab. The tests cover the single-input outline; the multi-input and slab outlines take the same uniforms.
  • The volume and image mappers sample the outline thickness texture with the width it was built with, through a labelOutlineTextureWidth uniform as the reslice mapper already does, rather than 1024.0, so setLabelOutlineTextureWidth() takes effect there too. The image mapper now rebuilds its outline textures when that width changes.
  • New tests testImageLabelOutlineDataTypes, testImageResliceMapperLabelOutlineDataTypes and testLabelOutlineDataTypes (volume mapper), with baselines. No API change.

Left as it is:

  • LABELMAP_EDGE_PROJECTION_BLEND still reads its labels raw (right for float storage; fix(vtkVolumeFS): round the labels read by labelmap edge projection #3765 fixes its truncation) and tracks 256 segments.
  • The WebGPU image and reslice mappers still read labels as * 255.
  • vtkImageMapper's color lookup of a signed norm16 texture: getShiftAndScale() decodes SHORT as sample * 32767.5 - 0.5, the desktop GL mapping, where WebGL2 stores c / 32767. A signed 16-bit labelmap's segments are colored as if half a unit lower, which is why the image test has no Int16Array case.

PR Checklist

  • GitHub Actions CI passed: semantic-release commit messages, lint, and tests
  • Test coverage added
  • Documentation and TypeScript definitions are updated to match these changes (no API change)

The WebGL label outlines of vtkVolumeMapper, vtkImageMapper and
vtkImageResliceMapper read the label under a fragment as
int(sample * 255.0), which is the label only for an 8-bit labelmap. A
16-bit or float labelmap had label n read as 255 * n, so its per-segment
outline thickness and opacity came from the wrong entries; stored as
norm16, every label read as 0 and nothing was drawn.

Scale the sample back by the texture's own normalization, and round, as
the WebGPU volume pass does. The volume and image mappers also sample
the thickness texture with the width it was built with rather than
1024, and the image mapper rebuilds its outline textures when that
width changes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant