Packed PNG Palette Indices: Read the High Bits Before Interpreting a Drone Preview
Unpack a supplied 1-, 2-, or 4-bit indexed PNG row, stop at the declared width, and separate palette references from unused trailing bits.

Identify the packed representation before counting pixels
A diagnostic byte dump from an indexed PNG preview may contain fewer bytes than the number of pixels in a row. That can be an expected consequence of packing several palette indices into one byte. Treating each byte as a complete pixel skips the image layout and can produce an incorrect width or impossible palette lookup. In a drone-preview handoff, identify the color type, bit depth, declared row width, and decoding stage before describing such a mismatch as missing image content.
This tutorial inspects two invented reconstructed bytes containing three four-bit palette indices. The input is already past decompression and filter reconstruction, and its filter-type byte is excluded. It does not open a PNG, inspect a camera image, parse a palette chunk, or establish that NiVO products export this representation. The catalogue photograph provides separate product context. The function isolates one byte-to-index step, with no claim about displayed color, transparency, spatial location, survey quality, or the meaning of an actual image.
Read indexed samples from the high-order bits
The W3C PNG specification states that sub-byte samples are packed with the leftmost sample in the high-order bits, and indexed-color pixels use that same packing for palette indices. Each row begins on a byte boundary. Some low-order bits in its final byte can be unused, and their contents are unspecified. For the constructed bytes hexadecimal twelve and three-F at four-bit depth, the successive nibbles are one, two, three, and F. A width of three selects only the first three indices.
The final F is therefore not a fourth pixel in this example. Do not enlarge the row to consume it or reject it merely because it exceeds the supplied palette size. The declared width establishes which packed fields are actual pixel indices. Conversely, an out-of-range index inside that width is a finding that must remain visible. Keep this distinction in a diagnostic trace so unused packing bits cannot be confused with a malformed pixel, an extra classification code, or a transparent area in a map preview.

Product context only; this photograph does not depict the constructed examples in this article.
Unpack a small reconstructed row with explicit bounds
Run the following Python function locally. It accepts immutable reconstructed row bytes, a pixel width from one through sixty-four, a bit depth of one, two, or four, and the actual palette-entry count supplied separately. The width limit is a teaching profile, not a PNG format limit. The function requires exactly the number of row bytes implied by width and depth, then extracts only the declared pixels. It does not guess these inputs from a byte pattern or silently trim an unexpectedly long buffer.
For the supplied example, the returned list should be one, two, three. The palette-entry count of four makes those indices valid, while the unused F remains uninterpreted. The input bytes are unchanged. This result explains packing only; it does not return RGB values, alpha values, map coordinates, or semantic classes. Keep the original reconstructed row and its layout labels beside the result so another reviewer can reproduce why two bytes yield three palette references instead of two or four complete pixels.
def unpack_palette_row(row, width, depth, palette_entries):
if type(row) is not bytes:
raise ValueError("Expected immutable reconstructed row bytes")
if type(width) is not int or not 1 <= width <= 64:
raise ValueError("Teaching profile: pixel width 1..64")
if type(depth) is not int or depth not in (1, 2, 4):
raise ValueError("Expected packed indexed depth 1, 2, or 4")
if type(palette_entries) is not int or not 1 <= palette_entries <= 1 << depth:
raise ValueError("Palette size outside the indexed depth range")
if len(row) != (width * depth + 7) // 8:
raise ValueError("Row byte length differs from the declared layout")
indices = []
for pixel in range(width):
bit = pixel * depth
shift = 8 - depth - bit % 8
value = (row[bit // 8] >> shift) & ((1 << depth) - 1)
if value >= palette_entries:
raise ValueError("Declared pixel index outside the supplied palette")
indices.append(value)
return indices
# Constructed reconstructed bytes; final low nibble is unused.
print(unpack_palette_row(bytes.fromhex("12 3f"), 3, 4, 4))Test partial bytes separately from invalid palette references
Change only the final low nibble from F to zero while keeping the width equal to three. The extracted indices should remain one, two, three because that nibble is outside the declared row. Then change the width to four while retaining the original bytes and a four-entry palette. The F becomes an actual index and the function should reject it. These two cases use the same byte positions to demonstrate why width is part of the interpretation rather than merely a convenient output label.
Also test a one-bit row with a width that stops inside a byte, a two-bit row crossing a byte boundary, and a four-bit row with an even width. Verify the high-to-low extraction order with independently written bit strings, not just another copy of the same shifts. Reject missing or extra row bytes, unsupported depths, invalid palette sizes, and Boolean layout values under the explicit input contract. Preserve the original bytes and record a rejection rather than inventing replacement palette entries to make the row appear valid.
Keep palette lookup and filter reconstruction as separate stages
For indexed color, PNG requires a palette, permits fewer entries than the depth can represent, and treats an actual out-of-range pixel index as an error. The palette-entry count passed here must come from the correct image context; the function cannot verify its origin. A value that fits the packed bit width can still lack a corresponding palette entry. Retain the palette identity and entry count with a real trace, and distinguish that lookup boundary from whether the extraction arithmetic itself was performed correctly.
The function expects reconstructed bytes, not compressed data or filtered residuals. Applying it before the appropriate prior decoding steps can produce plausible indices that describe the wrong representation. Likewise, each row or relevant reduced-image row must be supplied with its own correct width and boundary. This example does not handle interlacing, choose a filter, parse PNG chunks, or validate checksums. Continue those checks with an established decoder and retain their separate evidence rather than allowing a successful isolated unpacking result to certify a whole preview.
Do not stretch the extracted index values across a display-intensity range merely because the packed depth is smaller than eight bits. In this indexed example, one, two, and three are references into the supplied palette context. Changing their numeric values would select different entries rather than simply exposing the packed representation. If another tool displays colors instead of indices, compare the correct stage using the actual palette instead of treating that display difference as an error in the unpacked row.
Deliver a labeled byte-to-index trace with the unchanged image
For an actual preview investigation, retain the received image identity, selected row or pass, color type, bit depth, width, reconstructed byte range, palette-entry count, and decoder version. Include a short trace marking the bits assigned to each actual pixel and the unused tail. Keep any subsequent palette or transparency interpretation attached to its own metadata and method. A numeric palette index does not by itself establish a land-cover category, an invalid-data mask, or an observation from the underlying aerial survey.
In a NiVO-related handoff discussion, this approach explains why hexadecimal twelve and three-F produce three palette indices when the declared width is three. It makes no product-format, image-quality, accuracy, or survey-acceptance claim. Preserve the unchanged source and continue the independent checks required for the preview and its associated deliverables. The useful outcome is an auditable packing explanation that respects the declared width, with unresolved palette and decoding questions still visible for the next stage of review.



