Scientific detector format

Read CBF X-ray diffraction images in JavaScript

CBF is the binary form of imgCIF used for X-ray diffraction area-detector frames. PureJsImage reads one common binary subset and preserves detector counts as a quantitative 2D raster.

Import

import { openCbf, renderScientificPlane } from 'purejsimage/scientific'

Node.js example

import { FileSource } from 'purejsimage'
import { openCbf, renderScientificPlane } from 'purejsimage/scientific'

const frame = await openCbf(await FileSource.open('diffraction.cbf'))
const display = await renderScientificPlane(frame, {
  plane: { z: 0, c: 0, t: 0 },
  range: { mode: 'percentile', low: 1, high: 99.9 },
  scale: 'log',
  palette: 'inferno',
})

Browser example

const file = document.querySelector<HTMLInputElement>('#cbf')?.files?.[0]
if (!file) throw new Error('Choose a CBF file')
const frame = await openCbf(file, { maxInputBytes: file.size })
console.log(frame.detector, frame.sizeX, frame.sizeY)

Supported subset

FeatureSupport
Transfer encodingBINARY
Compressionx-CBF_BYTE_OFFSET
Element byte orderLITTLE_ENDIAN
Element typessigned and unsigned 8-bit, 16-bit, and 32-bit integers
Data shapeone 2D detector frame

Binary section validation

The reader uses a bounded header scan to locate the CBF binary marker. It validates MIME headers, declared binary bytes, element count, two-dimensional shape, padding, and the closing binary boundary before exposing the dataset. Offset and size arithmetic uses checked values.

Byte-offset compression

Byte-offset data stores differences from a running base value. One-byte deltas are read directly. The reserved value -128 selects a 16-bit delta, -32768 selects a 32-bit delta, and -2147483648 selects a 64-bit delta. Escape values use little-endian order. Decoding writes directly into native integer output blocks without retaining a separate delta list.

Native detector counts

The dataset has Z, C, and T sizes of 1. No RGB conversion occurs until renderScientificPlane() is called. Detector name, exposure time, and wavelength are exposed only when their standard imgCIF fields are present. Other simple single-line CIF fields remain in metadata.

Display transfer

Diffraction frames often have a wide numeric range. A log display transfer can make low detector counts visible after percentile or explicit range mapping. It changes display pixels only. It does not change detector counts or apply crystallographic analysis.

Memory and source reads

Byte-offset compression is sequential. A region request must decode preceding deltas, but it retains only one bounded compressed input buffer and the selected output rows. Browser File inputs remain Blob-backed.

Unsupported features

x-CBF_PACKED, x-CBF_PACKED_V2, x-CBF_CANONICAL, predictor compression, non-binary imgCIF transfer encodings, big-endian byte-offset data, multiple arrays, and 3D arrays reject explicitly. Peak finding, ring integration, indexing, background subtraction, and beam-center estimation are outside this reader.

Official references

IUCr CBFlib manual and binary section definition · IUCr imgCIF dictionary

Scientific Raster Explorer · MRC guide · API