paskvil

up: projects

wavelets

wavelet image compression, live in the browser, in plain TypeScript

a small demo of what makes wavelets so good at image compression: transform an image, keep only the most important coefficients, zero the rest, transform back - and see how little of it the image actually needed. four wavelet families, a slider for how much to keep, and views of the error and of the coefficients themselves. like BDPT, it was written to be read - plain TypeScript, no GPU, no frameworks, every step a short loop you can follow

a few things to try:

  • drag keep down to 1%, then 0.5% - and switch between haar and 9/7
  • the error view with haar at 1%: the error is made of blocks. with 9/7 it's soft ringing along the edges
  • the coefficients view, channel y: the few coefficients that survive sit along the edges - the eye, the beak, the feathers
  • the same view, channel cb, at 1%: the finest level is completely empty - more on that below
  • your own image - any photo, decoded right in your browser, never uploaded anywhere
  • compare with 8×8 dct (jpeg) puts JPEG's transform next to it, at the same budget

the code is on GitHub, and the wiki has a walkthrough of it, step by step - this page is about the ideas

why wavelets compress

a wavelet transform rewrites an image as a small, coarse version of itself, plus details - at every scale, how far the finer image is from what the coarser one would guess. in smooth regions the guess is good and the details are close to zero, and natural images are mostly smooth regions

so most coefficients are tiny, and dropping them changes the image very little. the default budget keeps 1% of them - 99% of the numbers gone, and the parrots are still clearly parrots. go lower, and at some point it all falls apart, quite suddenly…

lifting

all four wavelets are built from the same three moves - the lifting scheme (Wim Sweldens, 1990s):

  • split the signal into even and odd samples
  • predict each odd sample from its even neighbours, and keep only the miss - that's the detail
  • update the even samples from the details around them, so they become a smooth half-size version of the signal - the approximation

the beauty of it: each step changes one half using only the other half, so it can always be undone - recompute the same correction and subtract it. the whole transform is invertible no matter what the predict and update are, and the wavelet families differ in nothing else

CDF 5/3, the whole forward transform - src/wavelets/cdf53.ts
forward(x) {
  const { even, odd } = split(x)
  const n = even.length

  // predict: the line through the two even neighbours
  for (let i = 0; i < n; i++) {
    odd[i] -= (even[i] + evenSymmetric(even, i + 1)) / 2
  }
  // update: a quarter of the neighbouring details
  for (let i = 0; i < n; i++) {
    even[i] += (oddSymmetric(odd, i - 1) + odd[i]) / 4
  }

  return concat(even, odd)
},

four wavelets

  • Haar - predict "the same as the neighbour". only constants come out exact, so anything sloped leaves detail behind, and compressed it falls apart into blocks
  • CDF 5/3 - predict the straight line through both neighbours, so lines leave no detail. the wavelet of lossless JPEG 2000 (the code has an integer-to-integer version too - integers in, integers out, exactly invertible)
  • CDF 9/7 - two rounds of predict and update, and cubics come out exact. the wavelet of lossy JPEG 2000, and the best of the four here
  • Daubechies D4 - orthogonal like Haar, exact for lines like 5/3, but its basis functions are lopsided - compressed, it smears to one side

for an image, every row is transformed, then every column, then the same again on the top-left quarter - the coarse image - and so on, 1 to 6 times. the coefficients view shows the result: a tiny image in the top-left corner, and the details of each scale around it

what to keep

the demo ranks every coefficient by how much dropping it would cost, keeps the top N, and zeros the rest. "how much it would cost" isn't just the size of the coefficient - for 5/3, the same value matters up to 4× more in some subbands than in others, and ranking by plain size costs 5-7 dB. so each coefficient is weighed by what it paints when the image is rebuilt

and there are no exceptions - no "always keep the coarse image", no "always keep the averages". everything competes for the same budget

a bit of chroma subsampling, for free

the image is converted to YCbCr first - brightness, plus two colour channels - like JPEG does. and the ranking runs over all three channels at once, with one cutoff

the colour channels of a natural image carry little detail, so their coefficients lose the competition first: at 1%, about 70% of the survivors are brightness, and the finest colour details are all gone. that's roughly what JPEG's chroma subsampling does on purpose (half-resolution colour, because the eye barely notices) - here it simply falls out of the ranking

the numbers

PSNR in dB on the parrots (higher is better, ~40 dB is hard to tell from the original):

keep Haar D4 CDF 5/3 CDF 9/7
0.5% 28.75 29.60 31.01 31.37
1% 31.30 32.43 33.88 34.23
2% 34.06 35.64 37.05 37.38
5% 38.24 40.22 41.58 41.81
10% 41.96 43.74 44.97 45.16

the order holds at every budget - Haar, D4, 5/3, 9/7 - and the eye agrees

and JPEG?

JPEG doesn't use wavelets - it cuts the image into 8×8 blocks and transforms each on its own with the discrete cosine transform. switch on the comparison and it gets the same treatment: the same ranking, the same budget

above ~5% kept, it's within 1-2 dB of 9/7. below that it collapses - at 0.5%, 20.6 dB against 31.4. every block has an average (its DC term), and those alone are 1/64 ≈ 1.6% of all coefficients - so on a smaller budget some blocks lose even their average and go flat grey, or take on the wrong colour. and where it holds up, the blocks still know nothing of each other, so the seams show up as a grid, where the wavelets only blur

real JPEG never gets that bad - it doesn't rank coefficients out, it rounds them, and keeps every block's average. but the grid is the real thing, and the reason JPEG 2000 went with wavelets

what it isn't

a codec - the budget here is a count of coefficients, not bytes. a real one would quantize them, entropy-code them, and store where the kept ones are. the percentages aren't compression ratios, they're a fair way to compare the transforms

a few gotchas

  • lifting inverts anything - so a test that transforms and transforms back can't catch a wrong constant. the useful tests are the vanishing moments: a polynomial below the wavelet's degree must leave exactly zero detail. it caught a real bug - D4 with two steps in the wrong order, round-tripping perfectly
  • the ends of the signal - a step at the edge wants a sample past the end. mirroring the signal without repeating the end sample works, repeating it breaks invertibility (it would hand a step the very sample it's correcting), and wrapping around leaves a jump that looks just like a compression artifact
  • big photos are shrunk in halves - one big jump skips pixels and leaves false fine detail, exactly what the demo measures
  • no Lena - the classic test image is a crop of a Playboy centrefold, and its subject has asked for it to be retired. the parrots are from Kodak's test set, released for unrestricted use

references