Expand description
Turning a frame’s bytes into samples: unpack, normalise, demosaic, colour-convert.
The one implementation of pixel conversion in the project: the library’s
Frame::to_rgb8 and image writer, and Viva Studio’s viewer, all call it.
No I/O, no dependencies; it compiles for wasm32-unknown-unknown.
§What each step does
- Unpack (
unpack_u16) reads a single-component format —Mono*orBayer*— into oneu16per pixel holding the camera’s value: lsb-unpacked formats from their little-endian bytes (PFNC 2.4 §6.1.1),10p/12pas an lsb-first bit stream (§6.3.1), and the GigE Vision 2.010Packed/12Packedformats from their two-pixels-in-three-bytes layout (seePixelFormat::Mono12Packed). - Normalise to 8 bits keeps a sample’s top eight bits:
v >> (bits − 8). - Demosaic is bilinear: each missing colour is the truncated integer mean of the two or four nearest samples of that colour, and a neighbour outside the image is replaced by the nearest sample inside it (clamp-to-edge), so the outermost row and column mix in the wrong colour. It is the kernel Viva Studio’s viewer used before this module, a preview quality demosaic, not an ISP. To 8 bits, the mosaic is normalised first and then demosaiced; at full depth it is demosaiced at its own depth.
- Y’CbCr to RGB uses the full-range equations of PFNC 2.4 §8.2.1,
rounded and clamped to 0-255. PFNC calls the legacy
YUVformats Y’CbCr with U and V offset by 128 (Table 3-1) and gives them no equations of their own, so they get the same ones. A 4:2:2 or 4:1:1 pixel uses the Cb/Cr of its group. - RGB family:
BGRis reordered and an alpha byte dropped.
use viva_pfnc::PixelFormat;
use viva_pfnc::convert::{self, Converter};
// Two 12-bit pixels, 0xABC and 0x123, in the GigE Vision layout.
let bytes = [0xAB, 0x3C, 0x12];
let samples = convert::unpack_u16(PixelFormat::Mono12Packed, 2, 1, None, &bytes)?;
assert_eq!(samples, [0xABC, 0x123]);
// For display: the top eight bits, through buffers kept between frames.
let mut converter = Converter::new();
let grey = converter.gray8(PixelFormat::Mono12Packed, 2, 1, None, &bytes)?;
assert_eq!(grey, [0xAB, 0x12]);§Geometry
Without a row stride, pixels are read as one continuous run from the
first line to the last — PFNC 2.4 §6.7’s image padding. A bit-packed
payload must hold the bytes up to its last pixel’s: ⌈pixels × bits / 8⌉
for an lsb-packed one (…p), and for GigE Vision’s …Packed, two pixels
in three bytes whatever their bits, every whole group and a lone last
pixel’s first two bytes. Any other must hold at least
PixelFormat::image_bytes; bytes past that are ignored. §6.7 completes
the packing group the image ends inside, but an Allied Vision Manta
G-125B does not: at 1291 × 963 in Mono12Packed, an odd pixel count, it
sends ⌈pixels × 12 / 8⌉ bytes, one short of the completed group. A payload
with the group completed decodes the same.
With a row stride — a camera that sends bytes after every line, as the
GVSP image leader’s padding_x declares — each line is decoded on its
own: PixelFormat::line_bytes of pixels, a packing group the line ends
inside completed (§6.7 line padding), then the next line row_stride
bytes after the start of this one.
The two readings differ only when a line ends inside a packing group (an
odd width in 12p, say). PFNC leaves the choice to the transport
standard. The Manta G-125B declares no padding at an odd width in
Mono12Packed and runs its pixels on, which is the first reading; how a
camera that declares padding_x counts the group its line ends inside is
open (backlog PX-05).
Structs§
- Converter
- Converts frames, keeping its buffers between them: after the first frame of a size, converting another allocates nothing.
Enums§
- Convert
Error - Why a frame could not be converted.
- Pixels
- An image at the camera’s own depth, as
full_depthreturns it.
Functions§
- full_
depth - The frame without losing depth: monochrome as grey, everything else as RGB, 16-bit when the camera’s samples are wider than 8 bits — a Bayer mosaic demosaiced at its own depth.
- supports
- Whether
to_rgb8convertsformat— the formats a viewer can draw. - to_rgb8
- The frame as 8-bit RGB, three bytes per pixel.
- unpack_
u16 - The camera’s sample values of a single-component format (
Mono*,Bayer*), one per pixel, row-major. A Bayer mosaic is returned as it is.