TECHNICAL NOTE / NATIVE EXAMPLE
Rounding subpixel bounds with fixed.Int26_6
A raw Int26_6 value of 32 represents half a pixel. Converting its bounds to integer pixels requires both an arithmetic contract and an empty-selection rule.
This note uses x/image revision b06f1de3… and Go 1.27.2. The original source defines Int26_6 as signed int32, with 64 raw units per pixel. Its Ceil adds 63 in that type, then shifts right by six.
The upper 63 raw values wrap
At raw 2147483584, Ceil returns 33554431. At 2147483585, adding 63 wraps to −2147483648 before the shift, producing −33554432. Every raw value through 2147483647 has that same ceiling result. Replacing the addition with unrestricted int64 arithmetic would change the API's behavior.
| Raw int32 | Pixels | Floor | Ceil |
|---|---|---|---|
| -65 | -1.015625 | -2 | -1 |
| -32 | -0.500000 | -1 | 0 |
| 0 | 0.000000 | 0 | 0 |
| 32 | 0.500000 | 0 | 1 |
| 64 | 1.000000 | 1 | 1 |
| 2147483583 | 33554430.984375 | 33554430 | 33554431 |
| 2147483584 | 33554431.000000 | 33554431 | 33554431 |
| 2147483585 | 33554431.015625 | 33554431 | -33554432 |
| 2147483647 | 33554431.984375 | 33554431 | -33554432 |
The Gooo Floor/Ceil ports preserve these results inside the original int32 domain. Their explicit API difference is a {in_fixed_domain, value} record. Raw inputs outside int32 return {false, 0} before arithmetic or narrowing.
Check the original selection before rounding
A useful graphics operation is to round a subpixel rectangle outward and intersect it with image bounds. The upstream OpenType glyph code uses Floor on minimum coordinates and Ceil on maximum coordinates.
For example, raw bounds (32,32)–(96,96) represent (0.5,0.5)–(1.5,1.5) pixels and expand to integer bounds (0,0)–(2,2) before clipping.
But raw X bounds [32,32) are empty. Rounding their endpoints first produces [0,1), incorrectly giving the selection width. Reversed raw bounds such as [2,1) need the same early empty check.
The native PixelBounds consumer applies four steps:
- Reject any raw coordinate outside signed int32.
- Return an accepted empty result for a raw zero-width, zero-height or reversed selection.
- For a nonempty selection, reject either maximum coordinate above 2147483584, where Ceil wraps.
- Call the imported Gooo Floor/Ceil functions, then the existing RectangleIntersect with the original int64 image bounds.
The consumer's rejection rule is separate from the ports' compatibility contract. Rejection reports accepted:false; accepted empty selections report accepted:true. Both have zero output coordinates and has_pixels:false. Image bounds are never normalized.
Reproduce and keep the scope precise
The native example instructions include execution, saved replay and a checker for 16 literal cases. This table records nine illustrative inputs; a separate check covered the 63-value upper tail against the byte-preserved Go source. Those sets overlap. These are finite observations, not an exhaustive test of all 232 raw inputs. The example computes coordinates; it does not render glyphs or process image pixels.