Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

JPEG and JPEG 2000

Notes on JPEG and JPEG 2000: block-DCT-based and wavelet-based image compression standards, their pipelines, math, and exam-style examples.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Engineers use JPEG and JPEG 2000 when they need to store, transmit or display photographic images with smaller file size while keeping acceptable visual quality. JPEG is the most widely supported standard for general photos (web, cameras); JPEG 2000 is used where better compression, scalability or lossless modes are needed (medical imaging, archiving, some broadcast and cinema workflows).

Key ideas

Start from first principles: an image is a 2D array of pixel samples. Compression reduces the number of bits to represent that array by removing statistical redundancy (lossless compression) or by removing perceptually less-important information (lossy compression).

  • Compression: any process that replaces data by a shorter representation. We distinguish lossless (exactly recoverable) and lossy (approximate recovery).

  • JPEG (commonly called Baseline JPEG for the simple mode) is a lossy image compression standard based on block-wise Discrete Cosine Transform (DCT), scalar quantization, and entropy coding (Huffman). Assumptions: typical photographic images are locally smooth; energy compacts into low-frequency DCT coefficients.

    Precise JPEG pipeline (baseline sequential):

    1. Colour-space conversion: convert RGB (red, green, blue) to YCbCr where Y is luma (brightness) and Cb, Cr are chroma (colour difference) channels. This separates luminance from chrominance because the human eye is more sensitive to Y.
    2. Chroma subsampling (optional): reduce spatial resolution of Cb/Cr (e.g., 4:2:0) because chroma can be sampled at lower resolution.
    3. Tiling into blocks: split each component into non-overlapping 8×8 blocks (JPEG baseline uses 8×8). Each block is processed independently.
    4. Level shift: subtract 128 from each sample to make values centered around zero for DCT.
    5. 2D DCT on each 8×8 block: transforms spatial samples to frequency coefficients (one DC coefficient and 63 AC coefficients). The 2D DCT is separable: apply 1D DCT to rows, then to columns.
    6. Quantization: divide each DCT coefficient by a quantization step (different for each frequency), then round to integer. This is the primary lossy step.
    7. Zig-zag scan and run-length encoding (RLE): reorder coefficients from low to high frequency so long runs of zeros appear and can be RLE-compressed.
    8. Entropy coding: encode DC differences and AC RLE symbols with Huffman coding. (Arithmetic coding exists in non-baseline JPEG modes but is not used in Baseline Sequential.)

    Draw for exam: draw an 8×8 block of pixels, arrow to level-shifted block (label values minus 128), arrow to 8×8 DCT coefficient matrix (label DC at top-left), arrow to quantized matrix, arrow to zig-zag order list, arrow to Huffman-coded bitstream. Label each arrow with operation name.

  • JPEG 2000 is a different standard using Discrete Wavelet Transform (DWT), scalar quantization (for lossy) or integer reversible transform (for lossless), and Embedded Block Coding with Optimized Truncation (EBCOT) plus arithmetic coding (MQ coder). Key features: multi-resolution (progressive by resolution), quality scalability (embedded bitstream), better compression at low bitrates, region-of-interest (ROI) coding, and lossless mode.

    JPEG 2000 pipeline (core idea):

    1. Colour-space conversion (optional). 2. Optional tiling (like JPEG). 3. DWT applied to each tile producing sub-bands (LL, LH, HL, HH) at each level. 4. Quantization; for lossless, use the reversible 5/3 DWT (integer-to-integer) and unit step sizes (no quantization), and for lossy, use the irreversible 9/7 DWT with non-unit step sizes. 5. Partition sub-bands into small codeblocks. 6. Each codeblock is encoded by EBCOT (bit-plane coding + context modeling) and compressed with an arithmetic coder (MQ). 7. Packets are assembled in a flexible order to allow progressive decoding by resolution, quality, or position.

    Important JPEG 2000 choices: the reversible integer wavelet (CDF 5/3) enables exact lossless compression; the irreversible floating-point wavelet (CDF 9/7) gives better lossy compression.

Definitions to remember (bolded terms above):

  • DCT: a transform that expresses a finite sequence of samples as sums of cosine basis functions. It is separable and energy-compacting for smooth signals.
  • DWT: a multi-scale transform that decomposes a signal into low-pass and high-pass components at multiple scales (resolutions).
  • Quantization: mapping a continuous or high-resolution set of values to a smaller discrete set; in image compression often done by dividing coefficients by step sizes and rounding.
  • Entropy coding: lossless coding that approaches the symbol entropy (e.g., Huffman, arithmetic coding).

Mathematical note (assumptions): JPEG assumes local stationarity within 8×8 blocks; blocking artifacts can appear when quantization is heavy. JPEG 2000 avoids block boundary artifacts by using wavelets spanning larger regions (but can introduce ringing near edges for some settings).

Formulas

F(u,v) = (1/4)·α(u)·α(v)·Σ_{x=0..7} Σ_{y=0..7} f(x,y)·cos[(2x+1)·u·π/16]·cos[(2y+1)·v·π/16] where α(0)=1/√2, α(k)=1 otherwise. Units: coefficient value (same units as pixel intensity). Applies to 2D DCT on an 8×8 block in JPEG.

f(x,y) = (1/4)·Σ_{u=0..7} Σ_{v=0..7} α(u)·α(v)·F(u,v)·cos[(2x+1)·u·π/16]·cos[(2y+1)·v·π/16] inverse 2D DCT; symbols as above.

Y = 0.299·R + 0.587·G + 0.114·B where Y is luma (unit: same as pixel intensity). Applies to standard BT.601-like RGB→YCbCr.

Cb = 0.564·(B - Y) and Cr = 0.713·(R - Y) where Cb, Cr are chroma components. Units: same as pixel intensity. (Check exact coefficients used by a given profile.)

Q(u,v) = round( F(u,v) / S(u,v) ) where S(u,v) is quantization step from quantization matrix; Q(u,v) is quantized coefficient (integer). Units: dimensionless integer. Applies during JPEG quantization.

MSE = (1/(M·N)) · Σ_{i=0..M-1} Σ_{j=0..N-1} (I(i,j) - K(i,j))^2 where I is original image, K is reconstructed image, pixels in same units.

PSNR = 10 · log10( MAX_I^2 / MSE ) where MAX_I is maximum possible pixel value (e.g., 255 for 8-bit). Units: decibels (dB). Applies to image quality measurement.

Worked examples

Example 1 — small DCT + quantization (teaching-size 2×2 example). Note: JPEG uses 8×8, but we use 2×2 to keep arithmetic exam-friendly; show method is identical.

Given 2×2 grayscale block f = [[60, 62],[59, 61]] (8-bit samples). Steps:

  1. Level shift: subtract 128 from each to center around 0: f' = [[-68, -66],[-69, -67]]
  2. 2×2 DCT (use 2×2 separable orthonormal DCT basis; for 2×2 the DCT matrix C = (1/√2)·[[1,1],[1,-1]] ). Compute F = C·f'·C^T. C·f' = (1/√2)·[[1,1],[1,-1]]·[[-68,-66],[-69,-67]] = (1/√2)·[[-137,-133], [1,1]] So F = (C·f')·C^T = (1/2)·[[-137,-133],[1,1]]·[[1,1],[1,-1]] = (1/2)·[[-270, -4],[2,0]] = [[-135, -2],[1,0]] (Round intermediate carefully; final F ≈ [[-135, -2], [1, 0]] ).
  3. Quantize with a made-up quant matrix S = [[10,20],[20,40]]: Q = round(F / S) = [[round(-13.5), round(-0.1)],[round(0.05), round(0)]] = [[-14, 0],[0, 0]].
  4. Dequantize and inverse DCT to reconstruct; inverse will show most energy in DC.

Final answer: quantized coefficients (DC=-14, others 0). This example shows how quantization kills small AC terms — the main source of compression.

Example 2 — 1-level Haar DWT and quantization (illustration for JPEG 2000 ideas).

Given 4-sample 1D signal [52, 55, 54, 58] (example row). Apply 1-level Haar DWT (pairwise average and difference):

  1. Averages: (52+55)/2=53.5, (54+58)/2=56 Details: (52-55)/2=-1.5, (54-58)/2=-2
  2. After one level we have low-pass [53.5,56] and high-pass [-1.5,-2]. If we apply another level to low-pass, we'd get multi-resolution.
  3. Quantize details more coarsely (e.g., step=2) and averages less (e.g., step=1) to reduce bits.

This shows wavelet multi-resolution: low-pass is coarse-scale image, high-pass are details. JPEG 2000 uses more sophisticated wavelets (CDF 5/3, 9/7) and encodes quantized coefficients codeblock-by-codeblock with bit-plane coding and arithmetic coder.

Example 3 — PSNR calculation (numerical): Original 2×2 image I = [[100,102],[98,101]]; reconstructed K = [[99,103],[97,100]] (8-bit).

  1. Compute squared errors: (1^2, (-1)^2, 1^2, 1^2) => [1,1,1,1], MSE = (1+1+1+1)/4 = 1.
  2. PSNR with MAX_I=255: PSNR = 10·log10(255^2 / 1) = 10·log10(65025) ≈ 10·(4.813) = 48.13 dB. PSNR = 48.13 dB.

Common mistakes

  • Mixing up YCbCr coefficients — different profiles (BT.601, BT.709) use slightly different constants; state which you use.
  • Saying JPEG always uses Huffman coding; baseline uses Huffman, but JPEG also has arithmetic modes (less used).
  • Claiming JPEG 2000 is just "JPEG with better compression" — it's different: uses DWT, EBCOT, MQ arithmetic coder, precincts/codeblocks, embedded codestream.
  • Giving exact wavelet filter coefficients (e.g., CDF 9/7 numeric taps) unless sure; instead name the filters and their properties (reversible or irreversible).

For your exam

Typical question types:

  • Define and explain (e.g., define DCT, quantization, DWT, EBCOT) — answer should give formal definitions, assumptions and consequences.
  • Short notes (e.g., JPEG pipeline, JPEG 2000 features) — list steps, describe purpose of each.
  • Derivation (e.g., show separability of 2D DCT from 1D DCT) — show step-by-step matrix product or summation separation.
  • Numerical problem (e.g., perform 2D DCT on a small block, quantize and show compressed coefficients) — show level shift, DCT formula, quantize, show RLE ordering.
  • Diagram question (draw JPEG block diagram; show dataflow from RGB to compressed bitstream) — include labels: colour conversion, subsampling, blocks, DCT, quant, zig-zag, entropy coder.
  • Compare/contrast (e.g., JPEG vs JPEG 2000) — list differences: transform, artefacts, scalability, lossless support, computational cost.

A full-marks answer will: define every term used, give precise pipeline steps in order, show relevant formulas and derivations, and for numerical answers carry units/values through and round correctly.

Quick check

  1. What transform does baseline JPEG use? 2. What block size does JPEG normally use? 3. Name the reversible wavelet used in JPEG 2000.

Answers: DCT; 8×8 blocks; CDF 5/3 (integer reversible).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?