PNG Sub Filter: Reconstruct a Preview Row Before Treating Bytes as Pixels
Trace PNG filter type 1 with reconstructed left bytes and modulo arithmetic, using a small offline example that keeps compression and image validation separate.

Locate the byte representation before investigating a dark preview
A diagnostic extract from a PNG preview can contain values that look unlike the displayed image because the extract is still filtered. Before describing those values as damaged pixels, establish which stage of the decoding process produced them. A sequence obtained after decompression is not necessarily the reconstructed image row. For a drone data handoff, that distinction helps keep a low-level investigation specific: the question is whether the diagnostic has interpreted the received representation correctly, rather than whether a plausible-looking byte sequence proves that the preview content changed.
This tutorial reconstructs one small row using PNG filter type 1, called Sub. The input is constructed in memory and represents two teaching pixels with three bytes each. It is not captured aerial imagery or an actual NiVO export. The exercise neither opens a PNG nor decompresses its image stream. It demonstrates one reconstruction step so that a reviewer can explain a byte-level discrepancy accurately. Complete image readability, color interpretation, spatial references, and the underlying survey deliverable remain separate parts of a real receiving workflow.
Read the Sub rule at the byte level
The W3C PNG specification defines Sub reconstruction as the filtered byte plus the reconstructed corresponding byte in the pixel immediately to its left. Bytes preceding the first pixel are treated as zero. The arithmetic is unsigned modulo 256, and filtering operates on bytes rather than whole pixel values. With three bytes per pixel, the relevant earlier byte is three positions back, not simply the previous byte in the sequence. That distance is essential when explaining a row that contains several components per pixel.
Keep the filter type byte separate from the row data supplied to this calculation. PNG places a filter type before each applicable scanline, but that byte is not itself an image component. The function below receives only filtered component bytes after that prefix has already been identified and removed. It supports selected one-byte grayscale and three-byte pixel teaching profiles, not every PNG bit depth or color type. A different filter, packed samples, interlacing, or another decoding boundary requires its own handling rather than an undocumented change to the distance argument.

Product context only; this photograph does not depict the constructed examples in this article.
Reconstruct the row with a small Python function
The constructed filtered sequence is 250, 20, 30, 11, 1, 2. For the first pixel, no left pixel exists, so the three values remain 250, 20, 30. For the second pixel, add those reconstructed values component by component. The first calculation wraps: 11 plus 250 equals 261, and modulo 256 gives 5. The other two results are 21 and 32. The reconstructed row is therefore 250, 20, 30, 5, 21, 32. The cover shows this exact teaching calculation rather than an actual image crop.
Run the example locally with Python. The function requires immutable, nonempty bytes and a whole number of pixels under its selected profile. It appends reconstructed bytes in order, which makes the left dependency available for the next pixel. The printed list should match the six values above. No project file is read or rewritten. Changing the distance to one for this three-byte example would use the wrong dependency, even though the resulting numbers would still fit in bytes. An in-range result alone does not establish that reconstruction used the correct layout.
def reconstruct_sub(filtered, bytes_per_pixel):
if type(bytes_per_pixel) is not int or bytes_per_pixel not in (1, 3):
raise ValueError("This teaching profile supports one or three bytes per pixel")
if not isinstance(filtered, bytes) or not filtered or len(filtered) % bytes_per_pixel:
raise ValueError("Expected nonempty whole-pixel filtered bytes without the filter prefix")
restored = []
for i, value in enumerate(filtered):
left = restored[i - bytes_per_pixel] if i >= bytes_per_pixel else 0
restored.append((value + left) % 256)
return bytes(restored)
# Constructed Sub-filtered component bytes; the filter-type byte is excluded.
toy = bytes([250, 20, 30, 11, 1, 2])
print(list(reconstruct_sub(toy, 3)))Use reconstructed neighbors rather than the original residuals
The dependency becomes especially clear with more than two pixels. Each new component refers to the corresponding byte of the already reconstructed left pixel. Reusing a filtered residual from that earlier position would apply a different recurrence. Preserve this distinction in a custom diagnostic and test a sequence long enough to expose it. A one-pixel example cannot demonstrate whether the implementation follows the left dependency correctly, because its left inputs are all zero. Include a known multi-pixel case before applying the method to an actual receiving investigation.
Modulo arithmetic should also remain explicit. Clipping 261 to 255 would be incorrect for this reconstruction rule, as would allowing an unrestricted integer to stand in for the output byte. The wrap in the teaching example provides a useful check of that behavior. Do not interpret the residual 11 as a measured intensity or describe the reconstructed 5 as a calibrated quantity. Both are byte representations in a decoding exercise. Their relationship explains the reconstruction operation without supplying any evidence about radiometry, visual quality, or the physical scene represented by a real preview.
Keep the rest of PNG decoding outside the finding
A real PNG workflow must establish the file structure, dimensions, sample representation, compressed stream, row boundaries, and selected filters using a suitable decoder. This function handles none of those tasks. It assumes that the caller has already isolated a Sub-filtered row and supplied the correct supported distance. A successful result does not establish a valid PNG signature, matching chunk checksums, correct decompression, or complete image decoding. Record which step was tested instead of allowing one successful arithmetic demonstration to become a general image-integrity statement.
For a project discrepancy, preserve the received file and the exact decoder or diagnostic version. Identify the row and the representation being compared, then compare equivalent reconstructed values from suitable tools. If a custom extract was interpreted too early in the decoding pipeline, correct that interpretation and repeat the comparison. Avoid re-saving the image merely to make the diagnostic values resemble an expected array. A new export can be useful when deliberately requested, but it should remain an identified derivative with its own review evidence and relationship to the original.
Deliver a precise reconstruction explanation
A useful note identifies the unchanged input, the decoding stage, the filter type, the supported pixel representation, and the row selected for review. Show a small verified calculation when it helps explain the finding, keeping synthetic demonstration output distinct from actual imagery evidence. State whether complete decoding and visual review were performed elsewhere. If the relevant filter or sample layout falls outside this exercise, report that limit and use an appropriate reader rather than forcing the data through a familiar function. A visible boundary makes the result easier for another analyst to reproduce.
During a NiVO-related preview handoff, this explanation can prevent a representational difference from being mistaken for a content defect. The reconstructed byte row is still only one stage of understanding the delivered image. Continue the agreed checks for display, metadata, spatial context, and intended use with their own evidence. The practical lesson is to compare the same decoding stage on both sides and to preserve the original while investigating. That produces a clearer technical question without inventing a successful survey, a camera setting, or a verified image-quality outcome.



