Proposed improvement: support the new ImageData.pixelFormat colorType in SC #161
KaliedaRik
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
The new
ImageData.pixelFormatwork is not just an ImageData issue. It is part of a broader change in the HTML canvas / 2D Canvas API model: the 2D canvas backing store is becoming more explicitly configurable in terms of color space and color type.In the current platform model, a 2D context can be created with settings including
colorSpace,colorType,alpha,desynchronized, andwillReadFrequently:colorTypeis now defined as either"unorm8"or"float16"ImageData is following the same direction:
ImageData.pixelFormatcan now be"rgba-unorm8"or"rgba-float16", andImageData.datamay therefore be either a Uint8ClampedArray or a Float16Array.Main conclusion
Scrawl-canvas does not need an urgent rewrite, but it does need a deliberate audit and a policy. The immediate risk is not "canvas is broken"; it is that parts of SC may have implicit
unorm8assumptions baked into code paths, especially where pixel data is read, written, packed, unpacked, filtered, or exposed through convenience APIs.Recommended immediate policy for SC
For now, SC should probably continue to define a conservative 2D-canvas policy:
"unorm8"wherever supportedThis keeps SC stable while allowing later float-capable work to be added intentionally. The platform defaults still favour ordinary unorm8 behaviour, so this is a safe transitional stance.
Work areas to consider
Scrawl-canvas should treat the new
ImageData.pixelFormatwork as part of a wider 2D-canvas evolution toward explicit colorSpace + colorType backing stores, and should respond by hardeningunorm8assumptions now while leaving room for selective, experimental float16 support later.1. Context creation and feature detection
SC should centralise 2D context creation so that context attributes are requested deliberately rather than inherited accidentally. That means adding explicit handling for
colorTypeduringgetContext("2d", …)setup, and adding feature detection so SC can tell whether the browser actually honoured the requested settings. getContextAttributes() is now the obvious inspection point for that.This is also where SC should decide what to do with browser-specific extras. Chrome currently exposes a
toneMappingattribute in getContextAttributes(), but tone mapping is not part of the settled 2D canvas settings model in the HTML Standard yet; there is still active standards discussion about adding tone-mapping controls for floating-point / wide-gamut canvases. SC should therefore treattoneMappingas experimental / browser-specific metadata for now, not as a stable API commitment - thus out-of-scope for this issue.2. ImageData audit
SC should audit every place where it assumes that
ImageData.datais always byte-backed RGBA. The critical assumptions to look for are:instanceof Uint8ClampedArraydata.buffer0..255ImageData.dataas alwaysunorm8RGBAThose assumptions are valid only for
"rgba-unorm8", not for"rgba-float16"3. Filter engine
This is likely the biggest SC subsystem affected by the new model.
Most current filters appear to assume
unorm8RGBA input/output. That is fine for today, but SC should introduce format awareness into the filter pipeline so filters can dispatch by pixel format rather than silently assuming byte RGBA. The practical model is:unorm8pathsFilters based on packed integer tricks will remain unorm8-specific for now. Filters that already work internally in float side-buffers, especially OK-based or multi-pass filters, are much better candidates for future float16 support. The goal of a first float-capable phase would be to preserve more information at ingress/egress, not to solve "full HDR semantics" on day one.
4. Direct pixel manipulation APIs (
cell.js)The current
getCellData/paintCellDatafunction pair is really a unorm8-oriented experimental convenience API. It assumes byte RGBA storage, uses a packedUint32Arrayview, and exposes per-pixel channel values as 8-bit integers. That should probably remain true.If SC later supports float-capable direct pixel access, it would be better to add a separate experimental API function pair rather than forcing both models into the same abstraction. A float-aware pair would likely use different backing stores, different hooks, and a different public contract. It should be thought of as a more numeric / field-oriented API rather than merely "the same pixel objects, but float". That keeps both APIs cleaner and avoids contaminating the current one with too many conditional branches.
5. Color engine
The color engine looks relatively safe if canvas interaction is limited to resolving CSS named colors and SC does the rest of its color parsing/manipulation computationally. In that case, the main requirement is simply to ensure that the resolver canvas remains a boring, controlled srgb + unorm8 compatibility shim. This area looks lower risk than filters and direct pixel APIs. The main work here is documenting that assumption clearly.
6. Image asset ingestion / draw pipelines
Image files should not be thought of as "having canvas colorType".
colorTypebelongs to the destination canvas backing store. When an image is drawn to a 2D canvas, the browser decodes the source image and converts it into the destination canvas representation. For SC, the practical implication is:unorm8canvas is a normalizing sinkThat means no urgent work is required here beyond understanding that source image richness can be lost when drawn into an ordinary
unorm8canvas.All reactions