NiVO / BY TECHNOGISGermany · Indonesia
Preview byte tutorial

PNG Average Filter: Reconstruct Preview Bytes with Floor Division and the Correct Neighbors

Rebuild a constructed PNG Average-filtered row, preserving floor division, reconstructed left bytes, prior-row context, and modulo arithmetic without changing an actual image.

PNG Average Filter: Reconstruct Preview Bytes with Floor Division and the Correct Neighbors

Locate the filtering boundary before comparing preview values

A filtered PNG row is not yet the reconstructed row that an image reader will interpret. The Average filter predicts each byte from neighboring reconstructed bytes, then stores a residual relative to that prediction. Looking directly at the residual can therefore make a normal decoding step appear to change the image values. Before investigating a drone-preview discrepancy, identify whether the supplied buffer contains compressed data, filtered bytes, reconstructed bytes, or already interpreted image samples.

This tutorial reconstructs one short row of invented bytes. It does not open a PNG, decompress an image stream, edit a camera frame, or establish a NiVO export capability. The prior row is supplied already reconstructed, and the residual input excludes its filter-type byte. The catalogue photograph provides separate product context. The function isolates the arithmetic of Average reconstruction so another reviewer can reproduce it without mistaking a small successful byte calculation for full image, color, geospatial, or delivery validation.

Read the Average predictor and its arithmetic order

The W3C PNG specification identifies Average as filter type three. Reconstruction adds the filtered byte to the floor of half the sum of the reconstructed left and above bytes, then applies unsigned byte arithmetic modulo two hundred fifty-six. The neighbor sum must be evaluated without overflow. Left refers to the corresponding byte of the preceding pixel, or the preceding byte for packed low-depth data; it is not necessarily the immediately preceding array element for every image layout.

In the constructed stride-one example, residuals five, two hundred fifty, and seven use a prior row of eleven, twenty-one, and thirty-one. The first left value is zero, so the first predictor is floor of eleven divided by two, giving five and reconstructing ten. The next predictor uses that reconstructed ten with above twenty-one, giving fifteen. Adding two hundred fifty wraps to nine. The final predictor uses nine and thirty-one, giving twenty and reconstructing twenty-seven. Each step depends on the reconstructed current row.

Restored NiVO VTOL V3 product image from the supplied brochure
Restored NiVO VTOL V3 product image from the supplied brochure.Source: NiVO / TechnoGIS catalogue materials supplied by the site owner · Catalogue photo
Product context only; this photograph does not depict the constructed examples in this article.

Reconstruct one supplied row with an explicit byte stride

Run the following Python function locally. It accepts immutable residual bytes no longer than sixty-four bytes, an equally sized reconstructed prior row or None, and a positive byte stride from one through eight. The size and stride limits are teaching choices, not PNG capacity limits. None means a zero prior row for the first scanline of the relevant reduced image. The caller must supply the correct stride from the actual serialized layout rather than guessing it from a file extension or visual appearance.

The function returns new immutable bytes and never alters either input. For positions before the stride, the left neighbor is zero. Later positions use an already reconstructed byte at the specified distance. The above neighbor comes from the same position of the supplied prior row. Integer division supplies the required floor because these byte values are nonnegative. The sum is calculated in Python integer arithmetic before the final remainder operation; there is no premature reduction of the neighbor sum to a single byte.

def reconstruct_average(residual, prior=None, stride=1):
    if type(residual) is not bytes or len(residual) > 64:
        raise ValueError("Expected a short immutable residual row")
    if type(stride) is not int or not 1 <= stride <= 8:
        raise ValueError("Expected a teaching byte stride from 1 through 8")
    if prior is None:
        prior = bytes(len(residual))
    if type(prior) is not bytes or len(prior) != len(residual):
        raise ValueError("Expected an equally sized reconstructed prior row")
    out = bytearray(len(residual))
    for i, value in enumerate(residual):
        left = out[i - stride] if i >= stride else 0
        above = prior[i]
        predictor = (left + above) // 2
        out[i] = (value + predictor) % 256
    return bytes(out)

# Constructed row; input excludes the filter-type byte.
print(list(reconstruct_average(bytes([5, 250, 7]), bytes([11, 21, 31]), 1)))

Test odd sums, wide sums, and boundary neighbors

Odd neighbor sums expose incorrect rounding. The first predictor in the displayed case is five, not six, and the second is fifteen, not sixteen. Test a case where both neighbors are two hundred fifty-five as well: their sum is five hundred ten and its half is two hundred fifty-five. Reducing that sum modulo two hundred fifty-six before dividing would produce a different predictor. Keep this intermediate calculation wider than a byte even though every input and final output is a byte.

Test final-byte wrapping independently from predictor calculation. A large residual plus a valid predictor can exceed two hundred fifty-five, and the final result must wrap rather than clip. Repeat with an absent prior row, explicit zero prior bytes, several strides, an empty row, and a row shorter than the stride. The empty row here tests the local function only; it does not assert that an empty PNG scanline exists. Reject a mismatched prior row or invalid stride rather than padding or inferring a layout silently.

Keep filter reconstruction separate from image interpretation

Filtering operates on bytes regardless of sample depth. Reconstructing a byte sequence therefore does not by itself identify its channels, pixel count, alpha interpretation, palette entries, or multibyte sample values. A complete decoder must establish the image header, serialized scanline layout, filter method and row filter type, compression stream, and interlace context. This example consumes only the already isolated residual row for filter type three. It cannot determine those surrounding facts from three supplied bytes.

Prior-row context must also belong to the same reconstruction sequence. A row from another image, pass, or stage may have the correct length while supplying the wrong above neighbors. Preserve the relevant row identity and processing stage rather than considering length equality sufficient evidence. For interlaced material, the first row of a reduced image needs the appropriate boundary context. The function can represent that zero-prior case, but it does not organize interlace passes or discover where a new reduced image begins.

A numerical match is useful for explaining one reconstruction step, but it does not prove that a suspicious preview is complete or visually correct. Color management, transparency, palette interpretation, spatial metadata, and the receiving display remain separate questions. Do not alter a real image merely because this tiny fixture produces a familiar-looking row. Preserve the original preview and investigate the exact stage responsible for the discrepancy before proposing any authorized transformation of a delivery.

Deliver a reproducible row trace beside the original preview

For an actual preview investigation, retain the unchanged file identity, decoder version, header interpretation, row or reduced-image position, filter type, byte stride, residual bytes, reconstructed prior row, and expected output. Include a small independently checked odd-sum example and a separate wrapping case. Record which surrounding decoder stages were verified elsewhere. Another reviewer should be able to reproduce the predictor sequence without assuming that the example itself validated compression, complete sample interpretation, or the intended visual content.

In a NiVO-related image handoff, this method explains why residual values five, two hundred fifty, and seven become ten, nine, and twenty-seven under the stated constructed context. It makes no claim about actual drone imagery, mapped coverage, camera quality, customer files, or product rendering behavior. The useful result is a precise account of byte reconstruction, with neighbor selection, floor division, and final modulo arithmetic visible for the independent checks required by the real task.

Technical references

  1. W3C PNG filtering: neighbor definitions and reconstruction table ↗
  2. W3C PNG Average filter: floor and overflow requirements ↗
← All articles

Related articles

All articles ↗
LET’S TAKE FLIGHT

Your next mission
starts with a conversation.

Talk to our team