JPEG doesn’t store pixels — it stores how much of each cosine wave to add back. Keep the big coefficients, throw the small ones away, and the eye barely notices: reconstruction through a lossy wall, by design. Slide how many cosines you keep (or tap).
The Discrete Cosine Transform, the heart of JPEG. A signal of N=16 samples is written as a sum of cosine basis functions, Xₖ = Σₙ xₙ·cos(π/N·(n+½)·k); keeping only the first k coefficients and inverting gives a reconstruction that is exact at k=N and gracefully degraded below — because natural signals put most of their energy in the low-frequency coefficients, a few of them carry the picture. The reconstruction, the kept coefficients and the RMS error are computed live. A fail-loud self-check throws unless k=N reconstructs exactly and the error decreases as k grows.
A 1-D 16-sample signal stands in for JPEG's 8×8 blocks; real JPEG also quantises the coefficients and encodes them entropically. The DCT, the truncated inverse and the error are computed exactly.