Skip to content
io
png
PngLevel

PngLevel

Mojo struct 🡭

PngLevel

@memory_only
struct PngLevel

How hard write_png works to make the file small.

Every level produces a fully valid PNG that decodes to exactly the same pixels; they differ only in how long the encode takes and how many bytes come out. Two knobs move together: how far DEFLATE’s match search walks a hash chain, and how much effort goes into choosing between unfiltered and Sub-filtered scanlines.

FAST settles the filter choice without compressing anything, when the image is flat enough for the answer not to be in doubt; it produces the same bytes as DEFAULT there, falling back to sampling only once content gets grainy enough that the probe declines. DEFAULT is what the writer has always done and stays the default. SMALL compresses the whole image both ways rather than sampling every eighth row, and walks a hash chain four times longer.

Two other ways to make FAST faster were tried and dropped: a shorter chain with the look-ahead off enlarges a gradient’s output, because unfiltered gradient rows are slow to deflate; and bounding how much of a match goes into the hash chains, the one knob that still moves LZ77 time, does not bind at any setting that leaves smooth images intact.

Aliases

  • FAST = PngLevel(Int(0))
  • DEFAULT = PngLevel(Int(1))
  • SMALL = PngLevel(Int(2))

Implemented traits

AnyType, Copyable, Deinitable, Equatable, ImplicitlyCopyable, Movable, Writable

Methods

__init__

fn def __init__(out self, value: Int)

Prefer the FAST/DEFAULT/SMALL comptime constants over constructing one directly.

Args:

  • value (Int): 0 for FAST, 1 for DEFAULT, 2 for SMALL.
  • self (Self)

Returns:

Self

__eq__

fn def __eq__(self, other: Self) -> Bool

Args:

  • self (Self)
  • other (Self)

Returns:

Bool

__ne__

fn def __ne__(self, other: Self) -> Bool

Args:

  • self (Self)
  • other (Self)

Returns:

Bool

write_to

fn def write_to[W: Writer](self, mut writer: W)

Parameters:

  • W (Writer)

Args:

  • self (Self)
  • writer (W)