PNG Up Filter: Reconstruct Drone Preview Bytes from the Decoded Row Above
Reverse PNG filter type 2 on a constructed byte row, handle modulo wrap and first-row zeroes, and keep byte reconstruction separate from image validation.

Find the byte dependency before diagnosing a preview
A PNG preview in a drone mapping handoff can look wrong when an experimental reader mishandles scanline filtering. Before changing the image or blaming its source, reduce the problem to a small controlled byte example. PNG filter type two, named Up, depends on the decoded row above at the same byte position. Confusing that row with its stored filtered bytes can produce incorrect reconstruction while still yielding ordinary byte values. This tutorial isolates the dependency in memory. It does not open a customer preview, diagnose an actual NiVO export or establish the quality of a survey image.
Keep the original file unchanged and record which part of the decoding pipeline is under investigation. Identifying chunk boundaries, checking integrity, decompressing image data, reconstructing filtered bytes and interpreting samples are different stages. A correct result at one stage does not certify the others. Here the input has already been selected as one row of Up residual bytes with its filter selector removed. The prior row, when supplied, has already been reconstructed. These preconditions are essential: the function cannot identify a real PNG scanline inside arbitrary bytes or infer the image layout from its short arrays.
Read the Up rule at byte granularity
The W3C PNG specification gives the Up reconstruction rule as the filtered byte plus the reconstructed byte above, using unsigned arithmetic modulo 256. Filtering applies to bytes regardless of sample bit depth. At the first scanline of a reduced image, the absent prior scanline is treated as zeroes. The filter selector is separate from the image bytes. These rules determine the small operation below; they do not supply a complete PNG reader, a color interpretation or a geospatial reference. The example deliberately stops when the reconstructed byte row is available.
Byte granularity matters even when the eventual image contains larger samples or several color components. The function adds corresponding byte positions; it does not add a previous pixel as a single integer. It also does not use a bytes-per-pixel stride to find the byte above. That position is the same index in the already decoded prior row. Keep the row length and its origin visible in a diagnostic record. A pair of equal-length arrays is a necessary local condition for this operation, but it is not evidence that either array belongs to the intended image or interlace pass.

Product context only; this photograph does not depict the constructed examples in this article.
Reconstruct a small row without editing an image
Run this Python function on the constructed residuals and prior row. The code requires nonempty bytes of bounded length and either an equally long decoded prior row or None to represent the first reduced-image row. None is an explicit caller decision, not a guess made from the contents of the residuals. The small length limit keeps the teaching fixture inspectable; it is not a PNG capacity limit. Returning a new bytes object preserves both inputs. A type or length mismatch stops the demonstration instead of letting a short zip silently omit positions from the result.
def reconstruct_up(filtered, prior=None):
if not isinstance(filtered, bytes) or not 1 <= len(filtered) <= 64:
raise ValueError("expected 1 to 64 residual bytes without the selector")
if prior is None:
above = bytes(len(filtered))
elif isinstance(prior, bytes) and len(prior) == len(filtered):
above = prior
else:
raise ValueError("expected an equally long decoded prior row")
return bytes((value + predictor) % 256
for value, predictor in zip(filtered, above))
residuals = bytes([5, 250, 7])
decoded_prior = bytes([11, 21, 31])
print(list(reconstruct_up(residuals, decoded_prior)))
print(list(reconstruct_up(residuals)))Check modulo wrap and the first row explicitly
The reconstructed row is 16, 15 and 38. The middle addition produces 271 before reduction modulo 256, leaving 15. Clipping it to 255 would implement a different operation. Likewise, treating 250 as a signed negative residual without a clearly equivalent modular calculation can make an explanation harder to audit. The code keeps byte inputs as their ordinary unsigned values and applies the documented reduction after addition. Record the intermediate sum in a teaching note when a wrapped result looks surprising. A small explicit calculation is easier to verify than a screenshot of a damaged preview.
With no prior row, the second printed result is 5, 250 and 7. Zero predictors leave the residuals unchanged. That is a boundary condition for the first scanline of a reduced image, not permission to substitute zeroes whenever a prior row is unavailable. If a decoder lost earlier data, it cannot honestly label a later row as the first simply to keep processing. Interlaced images require correct pass boundaries as well. This helper does not discover those boundaries, and its caller must establish the appropriate prior-row relationship before applying the operation.
Use decoded history rather than another residual row
A useful second experiment reconstructs a new row using the first returned row as its prior. Save that returned value and pass it directly to the next call. Do not reuse the original residuals as though they were already decoded history. The difference becomes visible even in a tiny example and helps locate a state-management error in an experimental decoder. Keep each step labelled as filtered input, decoded prior and decoded output. Those names prevent a visually plausible byte list from being used in the wrong role merely because its length happens to match.
Unlike a filter that uses reconstructed values to the left on the current row, this isolated Up calculation has no current-row recurrence. Every output byte depends on its own residual and the corresponding prior-row byte. That observation helps explain the formula, but it is not a performance claim or a recommendation to redesign a production decoder. Real implementations still need correct buffer ownership, decompression and image layout handling. Keep this small operation as an independent diagnostic fixture, with separate tests around the surrounding pipeline rather than extending its apparent authority to code it never exercised.
Report a byte result without certifying the image
For a real investigation, retain the unchanged file revision, the identified scanline and pass, the residual bytes, the decoded prior bytes and the reconstructed output. Include the selected filter type and make clear that its selector was removed before calling this function. If another tool disagrees, compare the same stage and the same row rather than contrasting final display colors with intermediate bytes. A matching reconstruction can narrow a problem; it does not demonstrate correct chunk integrity, sample interpretation, alpha handling, color management, map coordinates or suitability for a delivery decision.
The NiVO catalogue photograph supplies independent aircraft context and is not a decoded result from these invented arrays. It establishes neither a particular PNG workflow nor a product feature. The practical habit is to make the dependency explicit: Up adds the decoded byte above, keeps positions aligned and wraps the sum into a byte. Preserve the first-row condition and the distinction between residual and reconstructed history. With those boundaries visible, a drone handoff reviewer can discuss a concrete decoding observation while leaving complete image validation and geospatial acceptance to the separate checks required by the project.



