# `BB.Camera.FFmpeg.Test.JpegBuilder`
[🔗](https://github.com/beam-bots/bb_camera_ffmpeg/blob/main/lib/bb/camera/ffmpeg/test/jpeg_builder.ex#L5)

Builds structurally valid JPEG byte streams for testing the scanner.

The scanner's job is to be right about the awkward cases — stuffed bytes,
restart markers, fill bytes, an `EOI` buried inside an EXIF thumbnail — and
real camera output gives no control over which of those show up in a given
capture. This builder makes each one reproducible on demand.

The images are structurally valid, not decodable: quantisation and Huffman
tables are filler. Nothing here decodes a JPEG, so that costs nothing, and it
keeps the fixtures small enough to assert on byte-exactly.

It ships in `lib`, not `test/support`, because every camera driver built on
this package needs it to feed `BB.Camera.FFmpeg.Test.StubBackend`, and a
dependency's `test/support` is never compiled. It is for tests only.

# `build`

```elixir
@spec build(keyword()) :: binary()
```

Build one JPEG image.

## Options

- `:width`, `:height` — dimensions written into the frame header. Default
  `64` x `48`.
- `:components` — number of colour components. Default `3`.
- `:sampling` — the luma component's `{horizontal, vertical}` sampling
  factors, which decide the chroma format. Default `{2, 2}`, which is 4:2:0.
- `:precision` — sample precision in bits. Default `8`.
- `:progressive?` — emit an `SOF2` rather than an `SOF0` frame header.
  Default `false`.
- `:frame_header?` — emit a frame header at all. Default `true`; `false`
  builds the degenerate marker-soup image that has no dimensions.
- `:entropy` — bytes for the scan. Any `0xFF` is byte-stuffed for you, so
  arbitrary binaries are safe to pass. Default 256 bytes including an
  explicit `0xFF`.
- `:restarts` — how many `RST` markers to interleave into the scan data.
  Default `0`.
- `:segments` — `{marker_byte, payload}` pairs emitted before the frame
  header, for `APP`/`COM` segments. Default `[]`.
- `:fill` — how many `0xFF` fill bytes to put before the `EOI` marker.
  Default `0`.
- `:scans` — how many scans to emit. More than one is what a progressive
  image looks like. Default `1`.

# `chunks`

```elixir
@spec chunks(binary(), pos_integer()) :: [binary()]
```

Split a binary into chunks of at most `size` bytes.

Capture hands over whatever the pipe had ready, so every boundary is a
boundary the framer has to survive.

# `stream`

```elixir
@spec stream([binary()]) :: binary()
```

Concatenate images into an MJPEG stream.

# `thumbnail_segment`

```elixir
@spec thumbnail_segment(keyword()) :: {byte(), binary()}
```

An `APP1` segment carrying a whole JPEG, the way an EXIF thumbnail does.

Pass it through `:segments` to build an image that contains `FF D9` long
before its own end — the case that breaks framing by naive marker search.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
