Skip to content

Open Zeiss CZI images, in 2D and 3D - #572

Merged
prete merged 3 commits into
mainfrom
claude/zeiss-czi-support-ec312e
Dec 4, 2026
Merged

prete merged 3 commits into
mainfrom
claude/zeiss-czi-support-ec312e

Conversation

@prete

@prete prete commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Phase 39 M1 and M2: a Zeiss CZI URL (or Zenodo's .czi/content) opens as an image-only dataset when each channel, z plane, time point and scene is one full-resolution subblock: a confocal stack, or a tile scan ZEN has stitched. M2 (second commit) adds JPEG and LZW subblocks and the 3D view. No gallery example is added.

What's in it

  • src/czi/: a reader written for the app from Zeiss's CZI layout, as libCZI reads it.
    • cziFormat.ts: segments, the file header, DV directory entries, subblock headers, the metadata segment and the Zstd1 header.
    • cziLayout.ts: planes by C/Z/T/S. Pyramid copies are left out, mosaics are counted, and other dimensions (phase, rotation, illumination, view, block) are read at their first coordinate, with a notice.
    • cziMetadata.ts: scaling, significant bits, channel names and dyes, colours, display windows, and scene names.
  • Pixels: uncompressed subblocks are read in ≤ 8 MiB bands of whole rows, as ND2 frames are. Zstd0 and Zstd1, with or without its low/high byte split, are decoded whole in the existing strip decode workers through geotiff's Zstandard decoder, so no new worker or dependency is needed. Supported pixel types are Gray8, Gray16, Bgr24 and Bgr48; BGR is swapped to RGB as tiles are cut.
  • Subblock header: the directory gives where a subblock starts but not the size of its own metadata, so each plane makes one 288-byte read for its subblock header before its rows. That read is cached with the bands and in the byte-range cache.
  • Scenes: they are the p axis, labelled Scene. #c=, #z=, #t= and #p= select a plane, as for ND2.
  • Display values from the file:
    • Contrast is DisplaySetting Low/High times the pixel type's maximum (libCZI defines them as fractions of the pixel-type range).
    • Colours are #AARRGGBB or #RRGGBB, read as Bio-Formats reads them; libCZI's parser fails on 6 digits, which the test file uses.
    • Names are Name (Fluor), e.g. Ch2-T1 (Alexa Fluor 568).
  • Refused with a message that points to stitching in ZEN or to bioformats2raw: mosaics, JPEG/JPEG XR/LZW, 32-bit and floating-point pixels, compressed subblocks over 128 MB, and multi-file CZIs.
  • Refactor (no behaviour change): ND2's range reads and #c/z/t/p parsing move unchanged into src/data/rangeFileReads.ts and src/data/planeSelection.ts, which both readers now share.
  • Diagnose Dataset range-checks CZI files.
  • Docs: docs/plan/phases/39-czi.md, a CZI section in docs/modalities/image.md, the data contract, and the format lists in the README, user guide, hosting, image-only, manifest and UI-reference docs.

Checked

Image 4_Stitch.czi (7.25 GB on webatlasdata1) is an LSM 700 confocal stack stitched in ZEN 2014: 2 channels × 194 z planes, each an uncompressed 2,851 × 3,277 Gray16 subblock. Measured in headless Chromium with the GPU on the dev server, from a cold cache, at about 9 MB/s to the bucket:

View Requests Bytes Last band read
Open, middle plane, both channels 13 37.8 MB 5.2 s
Next plane from the Z plane slider 8 37.4 MB 4.1–4.6 s
Zoom in on that plane 0 0 —

The plane the app showed matches a standalone Python render of the same subblocks' bytes.

Tests run on synthetic CZI files from src/test/cziFixtures.ts, with Zstandard written by node:zlib. They cover the container, metadata, layout, open request counts, per-channel and per-plane tiles, shared bands, subblock metadata, both Zstandard forms, colour, scenes, missing planes, long directories, hidden Content-Range, and each refusal.

Passing locally: the full vitest suite (6,696 tests), tsc -b, eslint ., Prettier, check:docs and vite build. The Playwright e2e suite was not run locally.

M2: JPEG, LZW and 3D (second commit)

  • JPEG (1) and LZW (2) subblocks decode whole in the strip decode workers, like Zstandard. The channel order follows Christoph Gohlke's czifile:
    • A JPEG subblock is a whole JPEG file that decodes to RGB, so it is not swapped, and its frame header must match the subblock's size and samples.
    • LZW is TIFF's LZW over BGR pixels, so it is swapped like uncompressed data.
    • Lossless JPEG (3) and chunked compression (7) stay refused.
  • Raw fallback: a subblock whose data is exactly its pixels' size is read as raw rows whatever its compression flag says, as czifile does. This works around a known writer bug.
  • 3D view:
    • The loader lists its own volume levels (x/y halvings down to 64 px, every z plane), since CZI files have none. The 3D planner picks the finest that fits, as for OME-Zarr.
    • loadVolume reads each plane whole, two at a time per channel, and averages each square of pixels into a voxel as the bands arrive.
    • Volume reads reuse bands the 2D view already holds but add nothing to the band cache or the persistent cache (new StripBandCache.peek, and write: false on the persistent cache).
    • Colour channels have no 3D view.
  • Volume key: volumes are now keyed by position as well as level and time point, so switching CZI scene (or BigDataViewer view) in 3D reads the new one instead of keeping the old volume.
  • Small helpers: jpegFrameHeader in jpegSequential.ts, and the test LZW encoder moved to src/test/lzwFixtures.ts.

Checked (M2)

  • OpenSlide Zeiss-5-SlidePreview-Zstd0.czi and -Zstd1-HiLo.czi (real Bgr48 files, CC0): the loader's pixels matched Python's own compression.zstd decode at every sampled point, in both files. This also confirms M1's Zstandard paths on real data.
  • idr0011 Plate1-Blue-A-02-Scene-1-P2-E1-01.czi (3 channels × 21 planes, 672 × 512): opened in 3D at full resolution in 8 s, with 131 requests and 43.2 MB (the whole file).
  • Image 4_Stitch.czi in 3D: the planner takes level 4 of 7, 357 × 410 × 194 voxels per channel, and reads the whole 7.25 GB file at about 9.5 MB/s. The full run was still going at commit time (206 of 388 planes in 400 s).
  • JPEG and LZW are tested on synthetic files only: no public JPEG or LZW CZI was found among OME's and OpenSlide's samples.
  • Local checks pass: the full vitest suite (6,711), tsc -b, ESLint, Prettier, check:docs and vite build.

Known gaps

  • Mosaics (slide scans, unstitched tile scans) are refused (M3).
  • JPEG XR is refused (M4). It was dropped on 2026-06-07, so it waits for a decision.
  • The 3D view reads every plane whole, since the file has no reduced levels: 7.25 GB for the test file. The block averages are computed on the main thread, one band at a time.
  • Zstd1's byte split is undone on the main thread, once per subblock. Time points have no time step.
  • Real files tried: Image 4_Stitch.czi, OpenSlide's two Zstandard slide previews (48-bit colour), and an idr0011 stack. JPEG, LZW, scene and other-dimension files have been tried only as synthetic ones.

prete added 2 commits December 4, 2026 17:09
A `.czi` URL (or Zenodo's `.czi/content`) now opens as an image-only dataset
when each channel, z plane, time point and scene is one full-resolution
subblock: a confocal stack, or a tile scan ZEN has stitched (Phase 39 M1).

- src/czi/: the ZISRAW container (file header, DV directory, subblock and
  metadata segments), planes by C/Z/T/S with pyramid copies left out and
  other dimensions read at their first coordinate, and the metadata XML
  (scaling, significant bits, channel names, dyes, colours, display windows,
  scene names).
- Uncompressed subblocks are read in bands of whole rows, as ND2 frames are;
  Zstd0 and Zstd1 (with or without its low/high byte split) are decoded whole
  in the strip decode workers through geotiff's Zstandard decoder. Gray8,
  Gray16, Bgr24 and Bgr48; colour pixels turned RGB as tiles are cut.
- Scenes are the `p` axis, named `Scene`; `#c=`, `#z=`, `#t=`, `#p=` select
  a plane.
- Display windows are DisplaySetting Low/High times the pixel type's maximum
  (libCZI's definition); colours `#AARRGGBB` or `#RRGGBB`, read as
  Bio-Formats does.
- Mosaics, JPEG, JPEG XR and LZW subblocks, 32-bit and float pixels, and
  multi-file CZIs are refused with a message.
- ND2's range reads and plane selections move unchanged to
  src/data/rangeFileReads.ts and src/data/planeSelection.ts, shared by both
  readers.
- Docs: Phase 39 plan, a CZI section in docs/modalities/image.md, and the
  format lists in the user-facing docs.
Phase 39 M2, on top of M1.

- JPEG (compression 1) and LZW (2) subblocks are decoded whole in the strip
  decode workers, as Zstandard ones are. A JPEG subblock is a whole JPEG
  file that decodes to RGB, so it is not swapped, and its frame header must
  match the subblock; LZW is TIFF's LZW over BGR pixels, swapped like
  uncompressed data. Both follow czifile. Lossless JPEG (3) and chunked
  compression (7) stay refused.
- A subblock whose data is exactly its pixels' size is read as raw rows
  whatever its compression says, as czifile does (a writer bug).
- The 3D view: the loader lists its own volume levels (x/y halvings down to
  64 px, every z plane), reads each plane whole, two at a time per channel,
  and averages each square of pixels into a voxel as the bands arrive. Its
  reads use bands the 2D view holds but add nothing to the band cache or
  the persistent cache (StripBandCache.peek, write: false).
- Volumes are keyed by the image's position too, so another CZI scene (or
  BigDataViewer view) is read instead of the last one shown again.
- jpegFrameHeader in jpegSequential.ts; the test LZW encoder moves to
  src/test/lzwFixtures.ts.
- Docs: Phase 39 M2 (mosaics are now M3, JPEG XR M4), the CZI and 3D View
  sections, and the format notes.
@prete prete changed the title Open Zeiss CZI images (one image per plane) Open Zeiss CZI images, in 2D and 3D Dec 4, 2026
…ort-ec312e

# Conflicts:
#	README.md
#	docs/image-only-and-geojson.md
#	docs/user-guide.md
#	src/data/morphologyImage.ts
#	src/datasets/datasetFactory.test.ts
#	src/datasets/datasetFactory.ts
@prete
prete added this pull request to stack #574 December 4, 2026 18:34
@prete
prete merged commit 887132d into main Dec 4, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant