JavaScript Image Frequency-Domain Scribbling and Restoration: Five Transform-Based Techniques

    This article explores how to apply reversible scribble-like perturbations to an image and then recover the original through inverse transforms in JavaScript. It covers five modes: inverse-matrix domain, magnitude spectrum, phase spectrum, combined magnitude and phase, and row-derivative domain. For each mode, it explains the core idea, provides JavaScript code, and describes the expected restoration behavior, followed by engineering and performance tips for browser-based implementations.

    1. Background and Overall Approach

    Image processing includes an interesting class of problems: how to apply a seemingly destructive perturbation to an image and then fully recover the original through a mathematical transform. The key is not to randomly overwrite pixels, but to first map the image into an invertible transform domain, modify selected components locally, and then apply the inverse transform to return to the spatial domain. Because the transform itself is a reversible linear operation, the restoration can approximate the original as long as the untouched components remain intact.

    The overall workflow has four steps:

    1. Read the image and convert it into a grayscale matrix for uniform processing.
    2. Apply the forward transform to obtain complex-domain coefficients or a derivative-domain matrix.
    3. Scribble on target components in the transform domain by zeroing, masking, or adding noise.
    4. Apply the inverse transform, normalize and clip the result, and render it back to the canvas.

    The following sections discuss five common modes.

    2. Mode 1: Scribbling and Restoration in the Inverse-Matrix Domain

    The inverse-matrix approach treats an image block as the input of a linear system and performs the round trip by constructing an invertible matrix and computing its inverse. Let the image block be a vector x and the transform matrix be A. The forward result is y = A·x, and restoration is x = A⁻¹·y. As long as A is invertible, scribbling on y preserves the structures in x that correspond to the untouched components.

    1. function matMul(A, B) {
    2. const n = A.length, m = B[0].length, k = B.length;
    3. const C = Array.from({length: n}, () => new Array(m).fill(0));
    4. for (let i = 0; i < n; i++)
    5. for (let p = 0; p < k; p++)
    6. for (let j = 0; j < m; j++) C[i][j] += A[i][p] * B[p][j];
    7. return C;
    8. }
    9. // Restoration: invert the scribbled y first, then left-multiply by the original matrix

    In practice, an orthogonal matrix (such as a discrete-cosine-like transform matrix) is usually preferred, because the inverse of an orthogonal matrix equals its transpose, which significantly reduces computation and numerical error.

    3. Mode 2: Scribbling and Restoration of the Magnitude Spectrum

    The 2D discrete Fourier transform decomposes an image into a magnitude spectrum and a phase spectrum. The magnitude spectrum describes the strength of each frequency component, with energy concentrated in the low-frequency region. Scribbling on the central area of the magnitude spectrum removes the main structural information and makes the restored image blurry; scribbling on the high-frequency region mainly loses details and edges.

    1. function fft2(re, im) { /* Combine 1D FFTs row-wise then column-wise; implementation omitted */ }
    2. function scribbleMagnitude(mag, cx, cy, r) {
    3. for (let y = cy - r; y <= cy + r; y++)
    4. for (let x = cx - r; x <= cx + r; x++)
    5. if (x >= 0 && y >= 0 && x < mag.length && y < mag[0].length)
    6. mag[y][x] = 0; // Zeroing the magnitude is the scribble
    7. }

    For restoration, recombine the modified magnitude spectrum with the original phase spectrum and apply the inverse transform. The observed result is that outlines remain, but brightness and texture change significantly.

    4. Mode 3: Scribbling and Restoration of the Phase Spectrum

    The phase spectrum carries position and alignment information. Keeping the magnitude while destroying the phase usually produces a texture-noise-like restored image, showing that the human eye is more sensitive to phase. In implementation, simply zero or shuffle the corresponding region of the phase matrix, then combine it with the magnitude spectrum into a complex spectrum and apply the inverse transform.

    1. function scribblePhase(phase, cx, cy, r) {
    2. for (let y = cy - r; y <= cy + r; y++)
    3. for (let x = cx - r; x <= cx + r; x++)
    4. if (x >= 0 && y >= 0 && x < phase.length && y < phase[0].length)
    5. phase[y][x] = 0;
    6. }

    5. Mode 4: Scribbling Magnitude and Phase Simultaneously

    Destroying both at once makes the restoration approach random noise, which is the strongest test of information integrity. In practice, first apply a low-pass-style mask to the magnitude, then apply random perturbation to the phase, and finally apply the inverse transform uniformly. This mode is often used to demonstrate the boundary of being “irrecoverable in an information-theoretic sense.”

    6. Mode 5: Scribbling and Restoration in the Row-Derivative Domain

    The row derivative uses the difference between adjacent pixels as a new matrix. Scribbling on the derivative creates local “breaks.” Restoration reconstructs the image by cumulative integration: the first column keeps its original value, and each subsequent column is obtained by adding the derivative to the previous column.

    1. function rowDerivative(img) {
    2. const d = img.map(row => row.slice());
    3. for (let y = 0; y < img.length; y++)
    4. for (let x = 1; x < img[0].length; x++)
    5. d[y][x] = img[y][x] - img[y][x - 1];
    6. return d;
    7. }
    8. function integrateRows(d, firstCol) {
    9. const out = d.map(row => row.slice());
    10. for (let y = 0; y < d.length; y++)
    11. for (let x = 1; x < d[0].length; x++)
    12. out[y][x] = out[y][x - 1] + d[y][x];
    13. return out;
    14. }

    7. Engineering Practices and Performance Optimization

    • Use blocking and in-place operations to avoid large numbers of intermediate arrays.
    • Use two separate arrays (real and imaginary parts) for complex arithmetic instead of objects, to reduce garbage-collection pressure.
    • Downsample large images first, verify the logic, and then run at full resolution.
    • After restoration, uniformly clip to [0, 255] and round to prevent overflow.
    • For higher performance, move hot loops to WebAssembly or GPU computation, which is a general acceleration technique.

    8. Summary

    The five modes reveal a common rule: the reversibility of an image depends on the integrity of information distribution in the transform domain. Magnitude, phase, derivatives, and the inverse matrix are just different ways of decomposing the same problem, and the scribble location determines restoration quality. Mastering the engineering implementation of these general transforms and inverse operations makes it possible to build intuitive yet rigorous image-processing demos in the browser.