// blog
A JPEG Lossless Decoder in About 200 Lines of TypeScript, Tested Against DCMTK
JPEG lossless (process 14 of ITU-T T.81) stores each sample as a Huffman-coded difference from a prediction made from its left, upper and upper-left neighbors. A decoder reads the frame header (SOF3), the Huffman tables and the scan header, then for each sample decodes a difference category, reads its extra bits and adds the prediction. With a 16-bit lookup table for the Huffman codes it fits in about 200 lines of TypeScript and decodes a 512 × 512 CT slice in about 15 ms. Test it against DCMTK on every predictor.

Our DICOM viewer counts which compressions visitors’ files use. Soon after launch, one arrived as JPEG lossless: 208 MR images, read, but nothing to show. Browsers decode the JPEG in photos. They don’t decode this one.
So we wrote a decoder. Here is how it works.
The format in one screen
JPEG lossless (the standard calls it process 14) doesn’t transform anything. For each sample:
- Predict it from neighbors already decoded: left (a), above (b), above-left ©.
- Read the difference to the real value: a Huffman code for its size category (0-16), then that many extra bits.
- Add them.
The file says which of seven predictors it uses:
| Predictor | Value |
|---|---|
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | a + b − c |
| 5 | a + (b − c) / 2 |
| 6 | b + (a − c) / 2 |
| 7 | (a + b) / 2 |
The first row uses the left neighbor, the first column the one above, and the very first sample a fixed middle value. DICOM’s “.70” files always use predictor 1.
The parts that matter
Markers. Read SOF3 (size, precision, components), DHT (Huffman tables), DRI (restart interval) and SOS (predictor, point transform). Anything else is skipped.
Huffman codes, fast. A table of 65,536 entries maps the next 16 bits of the stream straight to “code length and symbol”. One lookup per sample, no bit-by-bit tree walk.
Byte stuffing. In the data, FF 00 means a plain FF. Any other FF xx is a marker: stop feeding bits there.
The odd cases. Category 16 means a difference of 32,768 with no extra bits. Sums wrap at 16 bits. A point transform shifts the result left. A restart marker resets the bit reader and the prediction.
const a = pl[i - 1], b = pl[i - width], c = pl[i - width - 1];
switch (predictor) {
case 1: pred = a; break;
case 4: pred = a + b - c; break;
case 7: pred = (a + b) >> 1; break;
// ...
}
pl[i] = (pred + decode(table)) & 0xffff;
How we know it’s right
- Every predictor against DCMTK.
dcmcjpegcompressed one MR with each of the seven predictors, and a point transform; our output matcheddcmdjpeg’s, pixel for pixel. A 512 × 512 CT slice decodes in about 15 ms. - Files we didn’t make. Public test files from pydicom match their uncompressed references.
- Restart markers. DCMTK doesn’t write them, so the test encodes a small image with restarts itself and checks every pixel comes back.
- Broken files. The fuzz test damages 3,050 files, compressed ones included. Each is read or refused with a reason, never a hang.
JPEG 2000: we didn’t write that one
JPEG 2000 is a wavelet codec, many times the size of this one. We use OpenJPEG, compiled to plain JavaScript and loaded only when a file needs it. One trap: the copy that ships with a popular PDF library reduces every image to 8 bits, fine for a PDF and useless for a CT. The DICOM build keeps all 16.
Need a file format read in the browser, or an old one you can’t open anymore? Tell me about it.
Frequently asked questions
Why not use the browser's JPEG decoder?
It decodes baseline and progressive JPEG only, 8 bits per sample. JPEG lossless is a different process of the same standard, with up to 16 bits per sample, which CT and MRI need.
Why not WebAssembly?
For this format plain TypeScript is fast enough, and the site's Content Security Policy doesn't allow WebAssembly on the viewer's pages. For JPEG 2000, which is far more complex, we use OpenJPEG compiled to plain JavaScript for the same reason.
Where else is it used?
Camera RAW files in DNG format keep their sensor data as JPEG lossless too, so our image editor's RAW reader uses the same decoder.
































































































































