JPEG blocking, ringing and detail loss

Rounding the weights of 8×8 image patterns can expose block seams, create ripples beside edges and erase fine texture.

Visible effect

Seams, ripples, missing texture

Look at a smooth shaded surface, a sharp silhouette and a patch of fine texture. Strong JPEG compression can change each differently: a grid appears in the shading, alternating light and dark ripples cling to the silhouette, and the texture becomes a flat patch. These are blocking, ringing and detail loss.

The grid follows the encoder’s blocks, rather than objects in the scene. Ringing sits beside an edge; it does not require a bright light or an optical reflection. Lost texture can resemble defocus, but here its disappearance can coincide with block seams and edge ripples. A single symptom alone does not identify the cause.

Blocking · smooth ramp

Input
Reconstructed

Neighbouring blocks acquire different errors. The original slope develops seams.

Ringing · sharp edge

Input
Reconstructed

Changed pattern weights no longer cancel beside the edge. Ripples appear on both sides.

Detail loss · weak texture

Input
Reconstructed

Small fine-pattern weights round to zero. Their texture disappears from the sum.

The same 128×96 target used in the experiment, split into three bands. Each component uses 8×8 blocks; the strength is 4×. The grid and ripples are computed reconstruction errors, with no drawn seam or halo.

Physics

The loss enters at coefficient rounding

Here the cause is numerical, inside the encoder. JPEG describes an 8×8 block of component samples as a weighted sum of 64 cosine patterns. The discrete cosine transform, or DCT, finds those weights. Keeping every weight exactly would recover the block; taking the transform alone does not cause these artifacts.

Quantisation replaces each weight with a multiple of a chosen step. A weight of 37 with a step of 20 becomes 40; a weight of 8 becomes 0. The decoder adds the altered patterns. Entropy coding subsequently packs the rounded values without another loss. How transform coding compresses an image follows that path through a real JPEG file.

Follow one 8×8 blockEvery row has the same edge

Input row

Reconstructed row

Rows are identical, so vertical variation is zero. These eight horizontal weights carry the block. Frequency u runs from the constant pattern at 0 to the finest pattern at 7. Each weight is rounded to a multiple of its step:

Frequency u01234567
Weight160.0-535.8-209.177.9160.052.1-86.6-106.6
Step at 4×6444406496160204244
Rounded192.0-528.0-200.064.0192.0000
The input has two flat levels, 48 and 208. After rounding, samples on each side differ and can cross those levels: that is ringing. Zeroed fine-pattern weights no longer help cancel the ripples. Displayed weights are rounded to one decimal; the reconstruction uses full precision.

Each block is rounded independently. On a smooth gradient, different reconstruction errors can meet at its boundary and reveal the block grid. Weak fine texture needs small high-frequency weights: when those become zero, the texture is absent from the decoded sum.

These outcomes share a cause but depend on the input. A constant neutral block at the shifted zero level needs no nonzero weights and remains unchanged. A stronger setting need not make every individual ripple larger: rounding changes in discrete jumps.

Mathematics

Round the weights, then add the patterns again

The experiment starts with encoded RGB8 values from 0 to 255 and forms full-range Y′CbCr. Y′ is encoded luma, not physical luminance; Cb and Cr describe colour differences. Subtract 128 from Y′; the colour differences are already centred on zero. All three components retain a sample at every pixel: 4:4:4, with no chroma subsampling. The colour-representation Principle explains these component grids.

For one component block, S contains its centred samples; D is the orthonormal eight-sample cosine-transform matrix. Rows of D are patterns ordered from constant to increasingly fine variation. C contains the resulting weights: column u varies across the picture, row v varies down it, with both indices from 0 to 7.

Here sgn keeps the coefficient’s sign and the floor brackets round down. The strength α scales each entry Q from JPEG’s example luma and chroma tables in Annex K. For positive strength, round the scaled entry to an integer and clamp it to 1…255, the table range for 8-bit JPEG samples. The strength ranges from 0 to 8; zero bypasses coefficient rounding. It is a table scale, not a JPEG quality percentage. Larger steps make fine-pattern weights easier to lose.

The constant coefficient C₀,₀ equals eight times the block’s mean centred value. An error of 16 in that coefficient shifts every sample by 2. At strength 8, its step is 128, so the reconstructed luma mean advances in steps of 16 code values before display clipping. This can turn smooth shading into flat tonal patches even when the block grid is too small to see. Errors in the other coefficients add spatial variation instead. After the inverse transform, add 128 to luma, convert the components back to encoded RGB and clip only the final display values to 0…255.

Shader

Cache the transform, vary the rounding

The four passes below use the same source-pixel grid. Pass 0 transforms rows, converting RGB to centred components as it reads. Pass 1 transforms columns and saves unquantised coefficients. Pass 2 rounds those coefficients and applies the vertical inverse transform. Pass 3 applies the horizontal inverse transform and converts to display RGB.

Processing pipelineBoxes mark actual render-pass boundaries.
  1. Encoded RGB8Complete 8×8 blocks · 4:4:4 components
  2. 0 · Horizontal DCTNew image or video frame
    • Convert to centred Y′CbCr
    • Transform each block row
  3. 1 · Vertical DCTNew image or video frame
    • Transform each block column
    • Cache signed coefficients in RGBA32F
  4. 2 · Quantise + vertical inverseNew frame or strength change
    • Round weights using the table steps
    • Reconstruct each block column
  5. 3 · Horizontal inverseNew frame or strength change
    • Reconstruct each block row
    • Convert to RGB and clip for display
  6. Reconstructed RGB8 picture

Each pass sums eight samples per output texel. The runtime uploads an 8×8 basis texture and an 8×8 table texture once. Intermediate RGBA32F textures hold three signed component values per texel; an ordinary unsigned colour buffer would destroy negative coefficients. Coefficient textures use exact integer fetches, without interpolation.

Four separable DCT and reconstruction passesGLSL
// WHAT: Reconstruct 8×8 blocks with rounded pattern weights: seams, rings and lost detail.
// HOW: Two separable forward DCT passes, table rounding, two inverse DCT passes.
// WHY: Independent block quantisation changes the decoded samples before bits are packed.
// u_input stores RGB8 in pass 0, then signed code-value coefficients in float textures.
// Integer texel coordinates keep blocks anchored to the input, including after CSS resizing.
uniform sampler2D u_input;
uniform sampler2D u_basis; // x = position, y = frequency; .r is an orthonormal weight.
uniform sampler2D u_table; // x = u, y = v; .rgb = luma, blue-difference, red-difference steps.
uniform int u_pass;
uniform float u_strength;
out vec4 outColor;

vec3 components(vec3 rgb) {
  rgb *= 255.0;
  float y = dot(rgb, vec3(0.299, 0.587, 0.114));
  return vec3(y - 128.0, (rgb.b - y) / 1.772, (rgb.r - y) / 1.402);
}

vec3 encodedRgb(vec3 value) {
  float y = value.x + 128.0;
  float r = y + 1.402 * value.z;
  float b = y + 1.772 * value.y;
  return vec3(r, (y - 0.299 * r - 0.114 * b) / 0.587, b) / 255.0;
}

vec3 roundedWeights(vec3 coefficient, ivec2 frequency) {
  if (u_strength == 0.0) return coefficient;
  // Round and bound the scaled table to the legal range for 8-bit JPEG samples.
  vec3 step = clamp(floor(texelFetch(u_table, frequency, 0).rgb * u_strength + 0.5), vec3(1.0), vec3(255.0));
  // Explicit symmetric half-step rule; GLSL round() leaves ties implementation dependent.
  vec3 scaled = abs(coefficient) / step;
  vec3 halfStep = floor(scaled) + 0.5;
  // Stabilise exact ties perturbed by floating-point transform arithmetic.
  scaled = mix(scaled, halfStep, lessThanEqual(abs(scaled - halfStep), vec3(0.00001)));
  return sign(coefficient) * floor(scaled + 0.5) * step;
}

void main() {
  ivec2 pixel = ivec2(gl_FragCoord.xy);
  // Array uploads are top row first; only the final canvas has a bottom-up viewport.
  if (u_pass == 3) pixel.y = textureSize(u_input, 0).y - 1 - pixel.y;
  ivec2 local = pixel % 8;
  ivec2 origin = pixel - local;
  vec3 sum = vec3(0.0);
  for (int i = 0; i < 8; i++) {
    ivec2 address;
    ivec2 weight;
    if (u_pass == 0 || u_pass == 3) {
      address = ivec2(origin.x + i, pixel.y);
      weight = u_pass == 0 ? ivec2(i, local.x) : ivec2(local.x, i);
    } else {
      address = ivec2(pixel.x, origin.y + i);
      weight = u_pass == 1 ? ivec2(i, local.y) : ivec2(local.y, i);
    }
    vec3 value = texelFetch(u_input, address, 0).rgb;
    if (u_pass == 0) value = components(value);
    if (u_pass == 2) value = roundedWeights(value, ivec2(local.x, i));
    sum += value * texelFetch(u_basis, weight, 0).r;
  }
  // Keep signed, unclipped intermediate values. Only the display conversion clips.
  outColor = vec4(u_pass == 3 ? clamp(encodedRgb(sum), 0.0, 1.0) : sum, 1.0);
}

Only passes 2 and 3 repeat when the strength changes. A new video frame repeats all four passes. The runtime captures one native-resolution snapshot for both panes, uploads it without reading pixels back to the CPU, and schedules processing from decoded-frame callbacks. Static or paused images retain their forward coefficients. Array rows run top to bottom, while the canvas viewport runs bottom to top, so the final pass reverses the row address. CSS scaling does not move the block grid.

Floating-point transforms can put an exact half-step a fraction above or below its mathematical value. The rounding function treats values within 0.00001 of a half-integer in step units as ties, then rounds away from zero. The static examples and CPU reference use the same numerical convention.

Notes

What this isolates

This is the transform-loss core of an 8-bit JPEG-style 4:4:4 reconstruction. It does not create a JPEG bitstream, measure compressed size or reproduce a particular encoder’s quality scale, integer transform or colour-conversion rounding. The component conversions and intermediate transforms retain floating-point precision. There is no subsampling, deblocking, optical blur or added noise.

Video uses the same spatial transform independently on every frame, as an intra-frame experiment. Its 1280-pixel width is ten times the diagnostic target’s: when fitted into the same small pane, its 8×8 seams and ripples can disappear from view while coarse tone and colour steps remain visible. Pause and zoom to 100% or more to inspect the source pixels. The supplied videos are already decoded source material; this adds another loss to them. There is no motion compensation, changing bit budget or damaged reference chain, so it cannot demonstrate interframe error propagation.

The diagnostic and chart use complete blocks; both videos retain their native 1280×720 dimensions. Arbitrary dimensions would need an explicit edge-padding policy before the first transform. WebGL2 with floating-point render targets is required for the experiment; the computed examples and explanation remain available without it. GPU throughput depends on the device and input resolution.

References

ITU-T T.81 — JPEG, A.3.3–A.3.6, B.2.4.1 and K.1 — defines the 8×8 DCT, coefficient quantisation, reconstruction, the integer table range 1…255 for 8-bit samples, and example luma/chroma tables. Annex K tables are examples, not mandatory settings.

ITU-T T.871 — JPEG File Interchange Format, §7 — defines full-range RGB/Y′CbCr conversion. This experiment retains floating-point components instead of rounding them to JFIF’s 8-bit sample grid.

Keysers, Lampert and Breuel — Color image dequantization by Constrained Diffusion (2006) — discusses JPEG blocking and ringing as consequences of quantisation and independent block reconstruction.

Khronos — EXT_color_buffer_float — specifies the floating-point render targets used to retain signed DCT coefficients between passes.