Image Transform Doodling and Restoration in the Browser with JavaScript

    This article builds an interactive browser-based experiment for image transform-domain doodling and restoration using pure front-end JavaScript. It covers the mathematical background of three reversible representations—inverse matrix transforms, Fourier magnitude and phase spectra, and the row-derivative domain—and explains how five doodling modes behave differently in each domain. All computation runs locally without uploading images. The article also shares engineering practices for resolution control, asynchronous chunking, numerical stability, reversible encoding, and side-by-side visualization.

    1. Background: Why Doodle in a Transform Domain?

    Traditional image editors work only in the spatial domain (the pixel matrix): users modify exactly what they see. A more interesting class of requirements is to first transform an image into another mathematical representation, freely paint, mask, or add noise in that representation, and then apply the inverse transform back to the spatial domain to observe what broke and what survived. This is excellent material for teaching demonstrations and an intuitive experiment for understanding image redundancy, information distribution, and robustness.

    This article uses pure front-end JavaScript to build an interactive experiment that runs in the browser. It supports five doodle-and-restore modes: inverse matrix transform, magnitude spectrum, phase spectrum, simultaneous magnitude and phase processing, and the row-derivative domain. All computation is performed locally, and no image is uploaded, which preserves both privacy and performance.

    2. Mathematical Foundations: Three Reversible Representations

    2.1 Linear Transforms and the Inverse Matrix

    Treat an image block as a vector and multiply it by an invertible matrix to obtain the transform-domain representation. The key requirement is that the transform matrix must be strictly invertible; otherwise, information is lost during restoration. In engineering practice, orthogonal transforms (such as discrete cosine-type transforms) are commonly used to ensure numerical stability.

    2.2 Fourier Decomposition: Magnitude Spectrum + Phase Spectrum

    The two-dimensional discrete Fourier transform decomposes an image into a magnitude spectrum and a phase spectrum. A widely observed property is that the phase spectrum carries more structural information, while the magnitude spectrum mainly reflects energy distribution. This property is the theoretical basis for the “doodle separately, then restore” experiment.

    2.3 Row-Derivative Domain

    Applying a first-order difference to each row produces a row-derivative image. It emphasizes horizontal edges and textures, while flat regions are compressed to zero. Doodling in this domain therefore produces a corruption pattern completely different from that in the spatial domain.

    3. Implementation Highlights of the Five Modes

    1. // General framework: transform -> doodle -> inverse transform
    2. function pipeline(img, forward, inverse, mask, brush){
    3. const domain = forward(img); // forward transform
    4. const painted = brush(domain, mask); // paint in the transform domain
    5. return inverse(painted); // inverse transform back to spatial domain
    6. }
    • Inverse matrix mode: split the image into blocks and left-multiply by the transform matrix; after doodling, right-multiply by the inverse matrix to restore.
    • Magnitude spectrum mode: keep the phase and mask only the magnitude; after restoration, contours remain but brightness is scrambled.
    • Phase spectrum mode: keep the magnitude and perturb the phase; after restoration, the structure collapses while the energy distribution remains approximately intact.
    • Dual-spectrum mode: paint on both magnitude and phase simultaneously, producing the most thorough corruption.
    • Row-derivative mode: integrate after doodling to restore, which requires handling the baseline drift caused by the integration constant.

    4. Engineering Practices and Performance Optimization

    1. Resolution control: the computational complexity of spectral operations grows with the pixel count, so downsample to within 512×512 before demonstrating.
    2. Asynchronous chunking: use chunking and asynchronous scheduling to avoid long blocking of the main thread and keep the interface interactive.
    3. Numerical stability: apply truncation and normalization after the inverse transform to suppress ringing and overflow.
    4. Reversible encoding: if complete restoration after doodling is required, write a compressed backup of the original coefficients into the masked region to enable lossless rollback.
    5. Visual comparison: display the original image, the transform domain, the doodled result, and the restored result side by side to help readers build intuition.

    5. Extended Value

    This experiment can serve as a unified vehicle for teaching image coding, watermark robustness, frequency-domain filtering, and redundant structures. Furthermore, the transform and doodling workflow can be encapsulated as general operators, combined with object storage to save experiment samples, logging services to record operation traces, and monitoring and alerting to track front-end performance metrics, forming a reusable online experiment platform. Generating the initial operator skeleton with an AI coding tool and then manually verifying numerical stability can significantly shorten the distance from prototype to a usable system.