







If Part 10 set up the frame of the Rust/WASM factory, the next job was to refine it into an engine the graphics editor could call with confidence. The idea of simply writing a few fast functions in Rust was not enough. A graphics editor is a tool users keep touching by hand. Placing an image, correcting color, blurring, touching up with a brush, saving assets, loading them again and exporting: all of it has to move inside one workflow.
So I divided the Rust code by the nature of the computation rather than by algorithm name. Point-wise transforms that read a single pixel, local operations that reference neighboring pixels, geometric operations that change the coordinate system, and structural parsing that reads a whole document each differ in time complexity and memory access. If you lump them together just because they are all image processing, the lifetimes of temporary buffers and the error conditions get mixed up. I drew the boundary so that TypeScript handles interaction and document state, while Rust takes the areas where contiguous memory and numerical computation matter.
Still, I did not project this structure onto the outside callers as it was. The internal representation of the computation module and the browser's object model change at different speeds. At the boundary, only the shape of memory, the unit of coordinates, the color representation and the failure conditions remain as a contract, while the data structures and optimizations used inside stay hidden. A good FFI is not a window into the internal implementation; it is closer to a narrow mathematical contract that lets two different runtimes exchange data without misunderstanding.
The first principle of the Rust core was not to mix computation scopes. Point-wise operations map only the same position of input and output, so they are easy to parallelize and split into tiles. Convolution needs neighbors out to its radius, so tile edges need a halo. A geometric transform has to compute the inverse function to find which position of the source each output pixel references, and in file parsing the length and version read earlier decide the meaning of what comes later. All four deal with bytes, but their conditions for correctness differ.
This classification connected directly to complexity analysis. For an image of width W and height H, a point-wise transform is generally `O(WH)`. Applying a 2D kernel of radius r as it is gives `O(WHr²)`, but if the kernel is separable it can be brought down to `O(WHr)`. For vectorization or document parsing, the cost cannot be explained by pixel count alone; the complexity of boundaries, the number of path segments and the size of the reference graph all come in. Throughput estimates had to start from the input dimensions, not from the feature name.
From the memory point of view, I distinguished read-only input, mutable output and reusable work buffers. If input and output share a buffer, memory shrinks, but neighbors not yet read can be overwritten first. Conversely, making a full copy at every stage makes bandwidth a bigger cost than the computation itself on large images. It was important to separate the operations that are mathematically safe in place from those that need a separate buffer, and to keep the maximum lifetime of intermediate arrays short.
This division also makes the meaning of a call clear on the front end. A tool on screen forms the user's intent, and the computation layer performs a transform with a clearly defined domain and codomain. What the user sees is one graphics editor, but internally the computation is separated by locality, complexity and memory ownership. With this criterion in place, the whole factory does not collapse into one giant conditional as tools multiply.
I was fairly careful in setting the boundary between Rust and TypeScript, too. Different runtimes differ in object memory layout, string representation and exception handling. So rather than handing over one side's rich object as it is, you have to state the minimum information needed for interpretation: dimensions, stride, channel order and byte length. Even for the same four-channel image, the result is completely different depending on whether it is RGBA or BGRA, whether there is padding between rows, and whether the color values are linear light or gamma-encoded.
In linear memory, even a 2D image is laid out in a single line in the end. In a row-major image, the channel start of coordinate `(x, y)` is usually computed as `base + y × stride + x × channels`. If you assume `stride = width × channels` here, you can misread input that has extra bytes at the end of each row for alignment. The contract had to include not only width and height but the actual row spacing, and the last access position had to be validated in a large-integer space to make sure it does not exceed the total buffer length.
The number of memory copies was part of the API design as well. Moving a browser object into the computation module's linear memory and taking the result back out costs at least two passes of bandwidth. If you call small operations many times, the same image crosses the boundary again and again. So where possible, it is more efficient to bundle a series of operations into one computation graph, let the input cross the boundary once, finish several stages, and get back only the final result. It was a matter of reducing the number of data movements, not the number of functions.
What mattered at the WASM boundary was a steady API, more than a convenient one. Once the caller starts to know the internal data structures, swapping an optimization or an algorithm becomes a compatibility problem. At the boundary I left only the units and permitted ranges of the input, the ownership of the output, and whether state changed when an error occurred. With this contract clear, the inside of the computation can change from a scalar loop to SIMD, or from whole-buffer to tile processing, and the outside meaning stays the same.
Mathematically, this boundary can be expressed as a domain, a codomain and invariants. If D is the set of images the function can accept and C the set of normal outputs, an implementation that merely satisfies `f: D -> C` is not enough. It also has to preserve invariants such as alpha range, dimensions, color space and determinism. For invalid input, it must clearly report that it lies outside D and leave no partial result. This functional boundary is what made the computation engine a stable black box.
This Rust factory was, in the end, also a mathematics factory. Phrases like “blur the image slightly,” “keep only the bright areas” or “restore a specific part with a brush” look simple in the UI, but inside the engine they all turn into math problems. A color is a vector, a pixel buffer is a function on a grid, and a filter is an operator that sends that function to another function.
An RGBA image can be seen as a function like `I: Z² -> [0, 255]^4`. At each integer grid coordinate `(x, y)` sits a vector with four components: red, green, blue and alpha. Some filters change each pixel independently. Levels, invert and HSL adjustment are mostly pointwise transforms that send one pixel's color vector to another color vector. Blur, sharpen, healing and perspective sampling, on the other hand, also look at relationships with surrounding pixels. From there on, local neighborhoods, interpolation, kernels and weighted averages come in.
This distinction affects the code structure directly. Pointwise transforms are easy to parallelize and have simple memory access patterns. Convolution and bilinear sampling keep referencing surrounding pixels, so cache locality and boundary conditions matter. Brush tools add a mask function on top. A radial weight field is built inside radius r around the user's mouse position, and that weight is mixed into the alpha composition.
Putting in a lot of math should not make the editor look more complicated. Users touch only sliders and handles. Inside the Rust factory, though, coordinates, colors, probabilities, curves, matrices, compression and page assembly have to move within clear boundaries. The polish of a graphics editor ultimately depends on how quietly and stably this math layer runs.
The most basic thing in image work was pixel buffer validation. In a graphics editor you cannot know what size of image the user will bring in. Even if the front end has already checked, the Rust core has to check again. When computing width × height × 4, I did not compute it directly in u32; I computed it in u64 first and then checked the usize range. It looks like a small habit, but on the WASM side this kind of defense matters.
If the pixel buffer length is shorter than expected, an error is returned immediately. When an ImageBuffer cannot be created, that too comes back as a clear error. Inside the Rust factory I decided not to allow silent failure. If a wrong pixel quietly passes in graphics editing, it can later turn into on-screen artifacts, export errors or a corrupted saved file. When something is going to fail, it is better to fail early.
Color values are mostly clamped to the range 0 to 255. Operations like blur, noise, curves, HSL, sharpen and the healing brush easily let floats spike in intermediate calculations. When sending results back down to u8, they must be fitted to the range. Laying this handling down as a common practice keeps the output format steady even as more filters are added.
I kept watching the alpha channel too. JPEG has no alpha, while PNG and canvas do. SVG rasterization delivers premultiplied alpha, and when exporting to PNG it has to be turned back into straight alpha. In perspective transforms and bilinear sampling as well, if the RGB of transparent pixels gets mixed in, black borders can appear. So how the alpha space is handled directly affects graphics quality.
Mathematically, alpha is not just a fourth channel. The result of an operation differs depending on whether RGB holds actual color values or a premultiplied vector multiplied by alpha. If you linearly interpolate `(r, g, b, a)` as it is in straight alpha, the meaningless RGB of transparent pixels can contaminate the color. In premultiplied alpha, on the other hand, you interpolate `(ar, ag, ab, a)` and finally divide by a to return to straight space. This difference is immediately visible at the edges of a perspective transform, at the semi-transparent borders of SVG rasterization, and in brush feathering.
Blur looks like a basic effect in a graphics editor, but in the actual implementation cost management mattered. Running a 2D kernel as it is makes the amount of computation grow quickly with pixel count and kernel size. So I split the Gaussian blur into separable convolution. A horizontal 1D pass runs first, and its result is processed again by a vertical 1D pass. It relies on the property that the 2D Gaussian `G(x, y)` can be factored into `Gx(x)Gy(y)`.
The kernel is built from sigma. The basic form is `exp(-(x²)/(2σ²))`, and the radius is set to roughly 3σ. Since most of the Gaussian distribution's mass falls within that range, a continuous function with infinite support can be approximated by a finite discrete kernel. The weights are normalized to sum to 1. That way blur does not darken or brighten the image, and behaves like an averaging operation that preserves total energy.
I put an upper bound on sigma as well. A user can push the slider all the way, and odd values can come in from outside. NaN, Infinity and values of zero or less are handled defensively, and an excessively large sigma is clamped to the upper bound. Even when brush and filter values come from the UI, the core has to validate them again.
I added noise to create a film-grain feel. Instead of pulling in a separate large random-number dependency, I used a small PRNG. Considering WASM binary size and dependencies, such a small random generator was enough. Given a seed, it produces reproducible results, and if the seed is 0 it creates an automatic seed based on some of the input pixels and the image size. Even the same filter can produce a subtly different texture each time.
Noise is not simply a matter of adding random numbers either. When a pixel value is near 0 or 255, the added value is clamped and the distribution gets cut off. So the visual density of the result is affected by the interaction between noise amplitude and the clamped range. On top of that, the frequency content changes depending on whether you apply blur first or noise first. Blur shaves off high frequencies, and noise injects them back. That is why, for background images and textures, blur alone is too smooth, while adding a little noise looks more natural.
Image adjustment was, in the end, a matter of the coordinate system that represents color and the functions defined on it. The RGB values in storage often do not correspond linearly to light intensity. If you average gamma-encoded display values as they are, the result can come out darker than the physical average of light. Operations that mix energy, such as blur and resizing, should be done in linear-light space where possible, while the hue and saturation a person adjusts should be interpreted in a perceptual coordinate system suited to the purpose.
A curve is a transfer function defined on a finite interval. When the user sets a few control points, you can interpolate between them to make a continuous function, but depending on where the points sit, the slope can turn negative or overshoot can occur. For adjustments that must preserve the order of brightness, monotone interpolation matters, while for effects that deliberately invert contrast, monotonicity itself becomes a constraint. Even a small curve in the UI carries the function's continuity, differentiability and value range.
In actual pixel computation, rather than solving a continuous function every time, you can sample it into a finite lookup table. If the input bit depth is b, there are `2^b` possible channel values, and precomputing the output for each reduces the per-pixel cost to almost a single lookup. For higher bit depths or float images, though, the full table gets large, so interpolating between a small table is better. A balance arises between accuracy and cache locality.
A coordinate system that separates brightness from color was not a single right answer either. HSL is easy to explain in a UI but does not guarantee perceptually equal distances, and linear RGB favors compositing but does not directly correspond to the saturation people perceive. Print color spaces in turn depend on devices and profiles. So instead of forcing every tool into one color space, I set up a conversion scheme that moves into the space that fits the meaning of each operation and then returns to the reference space.
What mattered here was not the number of tools. It was settling in which space which function was applied, whether round-trip conversion preserves the value range and alpha, and whether the same input leads to the same output. Once this mathematical floor is ready, the way controls look on screen can change while the meaning of the computed results stays steady.
Graphics restoration was closer to a question of how to set boundary conditions than to copying values. When you change the pixels inside a region Ω, it is not enough for the texture inside the region to look natural. If values or gradients at the boundary `∂Ω` break abruptly against the surrounding image, the human eye finds the seam at once. A restoration result has to satisfy the interior information and the outer boundary at the same time.
A brush mask is a weight function that softens this boundary. If d is the distance from the center and r the radius, it can be normalized as `t = d/r`, and the weight becomes a function `w(t)` on the interval `[0, 1]`. If not just the value but also the derivative approaches 0 at the boundary, the composited brightness change looks more natural. The smoothstep family is used often because it easily secures continuity of slope as well as of position.
Looked at more generally, image restoration can be written as an energy minimization problem. One can think of an objective like `min ∫Ω |∇f - v|²`: inside, follow the gradient field taken from the source, while at the boundary connect to the target image. Moving to the Euler-Lagrange equation connects it to the Poisson equation. A real-time tool can approximate within its computation budget, but knowing what is being approximated makes clear why texture and low-frequency brightness have to be handled separately.
Compositing is not simple overwriting either. If foreground and background are expressed in premultiplied alpha, source-over has an associative property that preserves color and transparency together. When a spatial weight function enters, one stroke becomes a position-dependent convex combination of two images. If the weight goes out of range, or straight alpha and premultiplied alpha are mixed, bright edges and dark fringes appear.
This stage was also completed stably. The Rust factory was organized not as a mere bundle of WASM utilities but as a graphics computation engine that clearly handles complexity, memory ownership, color spaces and boundary conditions. I reviewed inputs of various sizes and strides, transparent edges, extreme adjustment values and repeated compositing, and confirmed that the determinism and numeric ranges of the results all held steady. With that wrapped up, the next step became much clearer. Deeper inside the graphics engine lay linear algebra and projective geometry, and that part turned out to be a good enough topic to deserve a separate article.
