PNG Paeth Filters: Reconstruct Preview Bytes with the Correct Neighbor and Tie Order
Rebuild a small supplied Paeth-filtered row, preserve exact predictor comparisons, and separate PNG byte reconstruction from map preview interpretation.

Identify the byte-reconstruction step in a preview investigation
A PNG preview supplied alongside drone mapping or inspection material can contain filtered image bytes that differ from the reconstructed row. Reading those bytes directly as image values skips a representation step. Paeth, filter type four, uses neighboring reconstructed bytes to predict the current one. A diagnostic reader therefore needs more context than the current residual alone. Before interpreting a suspicious value as a visual change in an aerial preview, identify the row, its filter type, and the stage of decoding that produced the displayed numbers.
This tutorial reconstructs one tiny supplied row from invented bytes. It does not open a PNG, decompress IDAT data, inspect a real camera image, or establish a NiVO export capability. The previous row is already reconstructed, and the current input excludes its filter-type byte. A catalogue photograph provides unrelated product context. These boundaries let the example isolate a useful debugging question: which neighbor supplied the prediction, and how did the residual become one reconstructed byte without altering the original inputs?
Preserve the documented predictor and comparison order
The W3C PNG specification defines Paeth from reconstructed left, above, and upper-left bytes, with unavailable neighbors treated as zero. It forms p from left plus above minus upper-left, then selects the neighbor with the smallest absolute distance to p. Ties prefer left, then above, then upper-left. Predictor arithmetic is exact; the final reconstructed byte is the filtered byte plus the predictor modulo 256. Filters operate on bytes rather than complete pixel values. These rules define the narrow algorithm used below.
For the constructed previous row ten, twenty, thirty and filtered current row five, two hundred fifty, seven, use a one-byte pixel stride. The first reconstructed byte is fifteen. At the second position, the predictor is twenty and the sum wraps from two hundred seventy to fourteen. At the third position, reconstructed left is fourteen, above is thirty, and upper-left is twenty. The selected predictor is twenty, giving twenty-seven. Using the current filtered byte as the left neighbor would follow a different computation and lose the intended reconstruction.

Product context only; this photograph does not depict the constructed examples in this article.
Rebuild one supplied row with explicit input contracts
Run the following Python function locally. It requires immutable bytes for the filtered row and, when present, the already reconstructed previous row. Their lengths must agree. None explicitly supplies a zero previous row for this isolated first-row case. The teaching profile permits one through sixty-four bytes and a stride from one through eight bytes. Those limits keep the demonstration small; they are not a complete PNG validity test. The caller is responsible for deriving the actual byte stride from the image layout.
The function builds a fresh output buffer from left to right, so each left reference uses a value already reconstructed on the current row. Above and upper-left come from the supplied prior row. The printed list should be fifteen, fourteen, twenty-seven. Keep that expected sequence next to the inputs while reviewing an implementation. A different sequence deserves a trace of individual neighbors and predictors before any change to a preview file, color interpretation, or downstream map product is considered.
def paeth(a, b, c):
p = a + b - c
pa, pb, pc = abs(p - a), abs(p - b), abs(p - c)
if pa <= pb and pa <= pc:
return a
if pb <= pc:
return b
return c
def reconstruct_paeth(filtered, previous, stride):
if type(filtered) is not bytes or not 1 <= len(filtered) <= 64:
raise ValueError("Teaching profile: 1..64 immutable filtered bytes")
if type(stride) is not int or not 1 <= stride <= 8:
raise ValueError("Teaching profile: byte stride 1..8")
if previous is None:
previous = bytes(len(filtered))
if type(previous) is not bytes or len(previous) != len(filtered):
raise ValueError("Expected equal-length reconstructed prior bytes")
out = bytearray()
for i, value in enumerate(filtered):
a = out[i - stride] if i >= stride else 0
b = previous[i]
c = previous[i - stride] if i >= stride else 0
out.append((value + paeth(a, b, c)) % 256)
return bytes(out)
# Isolated constructed row, not actual image or map data.
print(list(reconstruct_paeth(bytes([5, 250, 7]), bytes([10, 20, 30]), 1)))Test ties and wrapping before expanding the row reader
Use a direct predictor case with left zero, above three, and upper-left two. The left and upper-left distances tie, so the function must select left zero. With left zero, above three, and upper-left one, the above and upper-left distances tie, so above three wins. These constructed cases are more revealing than a large visually plausible image because a changed comparison order can fail them immediately. Include a case where all three neighbors agree, and keep the exact supplied numbers in the test record.
Test a predictor of two hundred fifty with a residual of ten, yielding reconstructed byte four after wrapping. Separately test the first row with no previous data, the first pixel with no left neighbor, and a stride of three for an illustrative multi-byte pixel layout. Reject mismatched row lengths and unsupported input types rather than manufacturing a partial row. Preserve the original immutable byte sequences and verify that repeated calls give the same result. These checks support the reconstruction function without suggesting that a complete PNG decoder has been tested.
Keep byte stride and image interpretation outside the shortcut
The stride argument is a byte distance to the corresponding component in the preceding pixel, not an arbitrary count of visible columns. For a constructed eight-bit three-component row, a stride of three keeps each component associated with its own left neighbor. A different bit depth or packed representation needs the correct layout derivation before this function is called. The reader does not inspect an image header, unpack samples, combine sixteen-bit components, or choose a filter from the supplied data. Do not infer those missing steps from a successful byte-row result.
Likewise, a reconstructed number does not establish a map coordinate, physical measurement, palette entry meaning, or display color. Retain the image representation and interpretation metadata for the appropriate later decoding stages. If investigating a full preview, preserve the received file and use an established decoder alongside a narrow reproducible trace. Record any separate integrity, decompression, interlacing, or image-structure checks under their actual scope. A byte-level explanation should remain attached to the row it examined rather than expanding into an unsupported statement about the whole product.
A useful comparison report labels every row as filtered or reconstructed and states whether its filter-type byte is included. Without those labels, two correct tools may appear to disagree simply because they display different stages. Request the smallest comparable byte range and its immediate prior-row context before drawing a conclusion. Keep that extracted range associated with the unchanged source file so the demonstration remains reproducible rather than becoming an unattributed list of numbers.
Deliver a reproducible predictor trace with the original preview
For a real debugging handoff, record the source identity, selected image or pass, row location, declared layout, derived byte stride, filter type, prior reconstructed row, filtered row, and resulting bytes. Include one small trace showing left, above, upper-left, selected predictor, and wrapped result. Keep tool and version information with that evidence. If the full decoder and the isolated trace disagree, investigate their inputs and boundaries before editing the original asset or treating either output as a corrected survey deliverable.
In a NiVO-related preview discussion, this method helps explain why a residual of two hundred fifty can reconstruct to fourteen and why the next byte may select the upper-left neighbor. It does not establish image quality, camera accuracy, survey coverage, or a particular product format. Preserve the unchanged inputs and continue the independent file and image checks required by the task. The useful outcome is a precise account of one reconstruction step, including the tie order and byte boundary that another reviewer can reproduce.



