Row-wise Run-Length Encoding Overview
- Row-wise RLE is a lossless encoding technique that scans data row by row to store maximal contiguous segments instead of individual elements.
- It applies across various domains—from 1D strings to 2D images and 3D voxel grids—by adapting traversal orders and run representations to the data geometry.
- Recent advancements leverage R-RLE for optimized morphological computations and tokenization in transformer models, improving both efficiency and predictive accuracy.
Row-wise Run-Length Encoding (R-RLE) denotes a family of lossless encodings in which data are traversed in a row-wise, scanline, or scanline-like order and maximal contiguous segments of identical values are stored as runs rather than as individual elements. In the cited literature, this basic scheme appears in several closely related forms: classical 1D string RLE, binary-image encodings that store horizontal row intervals, row-major mask tokenizations that encode foreground scanlines, and 3D voxel linearizations that compress an occupancy stream after a prescribed traversal of the volume (Clifford et al., 2019, Ehrensperger et al., 2015, Singh et al., 25 Feb 2026, Lee et al., 2023). Across these settings, the central design variables are the traversal order, the run representation, and the extent to which run structure is exploited algorithmically rather than merely as a storage format.
1. Formal model and representational variants
In its standard 1D form, run-length encoding represents a string as a sequence of maximal runs. One formalization writes a compressed string as
with , , adjacent runs carrying different characters, and compressed length equal to the number of runs; another equivalent notation is
This maximality condition is fundamental: a run is not an arbitrary segment but the largest contiguous block of identical symbols compatible with the surrounding context (Clifford et al., 2019, Akagi et al., 2022).
In 2D binary imagery, R-RLE typically means horizontal scanline encoding. A run is represented as
so each run is a closed integer interval on a fixed row . A binary image is then represented as a finite union . A compact representation requires concatenated foreground pixels to be stored as a single run and forbids overlapping runs (Ehrensperger et al., 2015).
In row-major semantic-mask encodings, the same scanline idea is expressed after flattening the mask. For a subsampled mask of spatial size , row-major indexing is
0
with 1 the row index and 2 the column index, using 0-based indexing. Binary masks encode only foreground runs as 3, while the naïve multi-class extension uses 4. Background is implicit and no explicit row separators are introduced; row boundaries are recoverable from the known width 5 (Singh et al., 25 Feb 2026).
In 3D voxel settings, the same principle is applied after a deterministic linearization of the volume. SnakeVoxFormer uses binary occupancy values E and F, concatenates them into a 1D string according to a chosen traversal, and applies standard RLE so that runs are represented as 6. The paper gives the examples 7 and 8, the latter illustrating negative compression. The encoding is lossless and symmetric, with compression and decompression both of complexity 9 in the length of the encoded sequence (Lee et al., 2023).
| Setting | Traversal unit | Run representation |
|---|---|---|
| 1D string | Maximal same-character substring | 0 or 1 |
| 2D binary image | Horizontal foreground interval on row 2 | 3 |
| 2D segmentation mask | Row-major foreground scanline | 4, 5 |
| 3D voxel grid | Linearized E/F occupancy stream |
6 |
These variants differ in geometry, but they share the same operational invariant: traversal induces a 1D sequence, and runs are maximal contiguous constant subsequences under that traversal.
2. Scan order, linearization, and multidimensional generalization
The distinctive feature of R-RLE is not only the existence of runs but the choice of row order. In 2D masks, the canonical scheme is row-major: iterate rows top-to-bottom and columns left-to-right. Under this convention, the flattening index 7 makes R-RLE identical to scanline encoding in the classical image-processing sense. The same work also notes that row-major and column-major flattening yield similar average sequence length, with row-major better for short-wide objects and column-major better for tall-narrow ones (Singh et al., 25 Feb 2026).
For 3D voxel data, the notion of a “row” is generalized. SnakeVoxFormer stores ShapeNet voxel grids in Binvox format at resolution 8, traverses the volume column-wise along the 9-axis at each 0, and then orders those 1-columns over the 2-3 plane using one of three strategies: raster scanning, snake traversal, or spiral traversal. Raster scanning “mimics the scan algorithm of CRT monitors by starting each new line at the beginning”; snake traversal alternates direction across adjacent lines; spiral traversal starts on the outer border and spirals toward the center. Before compression, the linearized sequence has length 4 (Lee et al., 2023).
The choice of traversal materially affects compression statistics. For SnakeVoxFormer, the average RLE sizes reported over categories are 458.97 B for snake, 463.93 B for raster scanning, and 748.17 B for spiral traversal. The same source observes that spiral order can be advantageous when “the scene is almost empty and most of the voxels are centered,” because it preserves large outer empty regions as long runs (Lee et al., 2023). This establishes a general property of R-RLE: the encoding is not defined solely by the data type but by the interaction between geometry and traversal-induced locality.
Sequence-design constraints in autoregressive settings reinforce this point. In segmentation tokenization, flattened start indices require 5 start tokens, whereas encoding 2D start coordinates separately increases sequence length by approximately 50% relative to flattened starts. The same work therefore favors shorter sequences even at the cost of larger vocabularies (Singh et al., 25 Feb 2026). A plausible implication is that, in learned systems, scan order must be judged not only by raw compression ratio but also by the statistical regularity of the resulting token stream.
3. Morphological computation on R-RLE images
R-RLE is not merely a storage format; it can be the native domain of nontrivial image algorithms. For binary morphology, images and structuring elements are both represented as unions of horizontal runs, and erosion and dilation are defined set-theoretically as
6
A useful run-level identity expresses erosion as
7
but the main efficiency gain comes from augmenting R-RLE with distance tables and a skeleton of the structuring element (Ehrensperger et al., 2015).
The key transform is the erosion transform
8
with 9. Under this choice, 0 behaves as a left-to-right counter within each run, while 1 gives the corresponding right-to-left count. For a run 2, the transform values increase from 3 at 4 to 5 at 6. Thus the distance tables store, for every foreground pixel, the distance to the next background pixel on the left and on the right along the row (Ehrensperger et al., 2015).
The structuring element is summarized by a skeleton. With the same 7, the skeleton of a run-length encoded structuring element is obtained by taking the rightmost pixel of each run. Since each run contributes exactly one skeleton point, the skeleton size equals the number of runs in the structuring element. This reduction allows erosion containment checks to be performed at skeleton points rather than at all pixels of the structuring element (Ehrensperger et al., 2015).
The resulting erosion algorithm combines several R-RLE-native optimizations. Runs shorter than the shortest structuring-element run can be discarded because they cannot contribute to the erosion. In addition, after translating the structuring element so that the rightmost pixel of its longest run is at the origin, the first 8 pixels of each sufficiently long image run can be cut off before scanning. During the scan, the Jump-Miss theorem allows the algorithm to skip several positions at once when a skeleton comparison fails, while the Jump-Hit theorem allows it to emit an entire eroded output run when all skeleton tests pass. The complexity bounds given are 9 for skeleton construction, 0 for building the distance tables, and 1 for erosion, where 2 is the number of runs in the structuring element (Ehrensperger et al., 2015).
This usage is characteristic of mature R-RLE algorithmics: the representation is enriched just enough to support direct computation on runs, avoiding dense-pixel iteration except where unavoidable.
4. Tokenization for transformers and autoregressive mask generation
Recent work has recast R-RLE as a tokenization interface between spatial data and sequence models. In SnakeVoxFormer, a single 2D image is mapped to a 3D voxel model by first linearizing the occupancy grid, then applying RLE, and finally compressing repeated RLE subsequences with a codebook that maps frequent blocks to discrete tokens 3. The method is described as inspired by Huffman encoding: the longest most repeating RLE sequence is substituted by a single token, and the transformer predicts token sequences rather than raw voxels. The 1D representation is reported to use only about 1% of the original data size, and the model is reported to improve state-of-the-art 3D voxel reconstruction from images by at least 2.8% and up to 19.8% (Lee et al., 2023).
The tokenization layer in SnakeVoxFormer is not run-by-run but block-by-block. Example codebook entries include 4, 5, and 6. This converts the compressed voxel description into an NLP-like discrete vocabulary suitable for a standard sequence-to-sequence transformer trained with CrossEntropy. Traversal order affects not only compression but predictive quality: mean IoU over 13 categories at 4, 8, 16, and 20 views is reported as 7 for snake, 8 for raster, and 9 for spiral (Lee et al., 2023).
In semantic segmentation, R-RLE is used more directly. Masks are flattened row-major and encoded as sequences of run starts and run lengths; the flattened start vocabulary has size 0, length tokens are capped at 1, and long runs are split when necessary. The encoding deliberately uses distinct token sets for starts and lengths, so that length prediction need only range over the 2 length tokens rather than the full vocabulary. For multi-class masks, the Lengths-As-Class (LAC) scheme merges length and class into a single composite token, reducing each run from three tokens 3 to two tokens 4. For videos, Time-As-Class (TAC) collapses temporal label patterns into a 2D class code, after which standard 2D row-wise RLE can again be applied (Singh et al., 25 Feb 2026).
The same line of work gives explicit resource-oriented design bounds. On an RTX 3090 with 24 GB, training is described as reliable for 5, unstable near 6, and manageable for vocabularies up to about 7. Patching and subsampling are used to keep both sequence length and vocabulary within those limits. This suggests that, in learned token pipelines, R-RLE serves not just as a compressor but as a structural prior that determines the feasible operating regime of the decoder (Singh et al., 25 Feb 2026).
5. Compressed algorithms on run-structured strings
Although many formal results concern 1D strings rather than 2D images, they are directly relevant to R-RLE whenever each row is treated as an independent RLE string. The strongest example is edit distance on run-length encoded strings. For two strings of compressed lengths 8 and 9, the edit distance can be computed in
0
time. The method partitions the standard dynamic-programming table into blocks induced by pairs of runs, represents block-border values as piecewise-linear functions with slopes in 1, and supports operations such as split, join, shift, gradient change, sliding-window minimum, and combine in a balanced-BST structure. The same source explicitly notes that if 2D data are processed row by row, each row is simply an independent 1D RLE string and the algorithm applies directly to each row pair (Clifford et al., 2019).
Run structure also supports output-sensitive combinatorial indexing. For minimal absent words (MAWs), an RLE string
2
admits a decomposition of 3 into five disjoint classes 4. The reported bounds are 5, 6 in the worst case, 7 unbounded by 8, 9, and 0. Despite the unboundedness of 1, there exists an 2-space data structure that can output all MAWs in optimal 3 time, and the structure can be built in 4 time using truncated RLE suffix arrays (Akagi et al., 2022).
Abelian regularities provide a third illustration. Given a string of length 5 and RLE size 6, all regular Abelian periods and all Abelian squares can be computed in 7 time, while the longest common Abelian factors of two strings with total RLE size 8 and total length 9 can be computed in 0 time. These algorithms rely on a successor function 1 marking the next run boundary and on breakpoints defined by distances from sliding-window endpoints to those boundaries. Between breakpoints, Parikh-vector differences evolve linearly, allowing many positions to be handled in constant time per interval rather than one position at a time (Sugimoto et al., 2017).
Taken together, these results show that run boundaries are not merely delimiters for compression. They induce block decompositions, monotonicity properties, and low-complexity border representations that can collapse the effective search space of dynamic programs and combinatorial enumeration.
6. Trade-offs, preprocessing, and limitations
R-RLE is advantageous when the chosen traversal produces long homogeneous segments, but it is not a universal compression guarantee. Standard RLE can expand alternating patterns, as illustrated by the voxel example 2 (Lee et al., 2023). In learned 3D reconstruction, this limitation is described more broadly: RLE assumes non-noisy structures, is suitable for man-made objects, and the cited model fails on small structures such as sharp edges and small object parts; it is also demonstrated only for individual objects rather than entire scenes (Lee et al., 2023).
A separate line of work therefore studies how to make data more RLE-friendly before encoding. One reported reversible pipeline consists of a Burrows–Wheeler–Scott transform on bytes, dynamic byte remapping, vertical byte reading, bit-wise RLE with capped run length, and Huffman coding of run lengths. On the Canterbury Corpus, plain bit-level RLE yields an average relative size of 337.06% and 26.95 bits per symbol, whereas the proposed pipeline yields 42.34% and 3.40 bits per symbol, described as a factor-of-8 average improvement over standard RLE compression (Fiergolla et al., 2021). This is not intrinsically row-wise, but it shows that traversal alone may be insufficient; preprocessing can be decisive when raw run statistics are poor.
In autoregressive segmentation, the principal trade-off is between sequence length 3 and vocabulary size 4. The reported design choice is to keep 5 short even when this increases 6, because memory limitations are dominated by sequence length. The same work also argues that RLE is more tolerant to noisy tokens than polygon or quadtree encodings, since each run covers only a small local region, but it identifies a countervailing weakness: object-level structure is harder to learn when an object is represented as many independent scanlines (Singh et al., 25 Feb 2026).
Algorithmically, 1D compressed results do not automatically solve genuinely multidimensional problems. The edit-distance framework on RLE strings applies directly to row-by-row comparisons, but the same source states that true 2D edit operations on matrices or images would require substantial new ideas; the piecewise-linear border machinery depends on the geometry of the standard 1D edit-distance grid (Clifford et al., 2019). A similar caution applies to 3D extensions: SnakeVoxFormer shows that row permutations such as snake, raster, and spiral materially change both compression and learnability, so “row-wise” in higher dimensions is a modeling choice rather than a unique construction (Lee et al., 2023).
In this sense, R-RLE is best understood as a structural interface between geometry and runs. Its effectiveness depends on how well the traversal aligns with the spatial, symbolic, or temporal coherence of the underlying data, and the most successful uses in current literature are precisely those that exploit that alignment algorithmically rather than treating RLE as a passive serialization format.