Search guides and the APICtrl K
  1. API reference
  2. Files and codecs

gfx/png.h

PNG, into the same premultiplied RGBA a Bitmap holds.

#include <sonore/gfx/png.h>
  • 2types
On this page

Design notes

Why a plugin needs it

A logo. A texture behind a panel. A knob drawn by a designer rather than by arithmetic. All of it was free in a webview -- an <img> tag -- and impossible in the native UI, which could draw shapes and text and nothing that came out of an image editor.

PNG only, and 8 bits only

Not JPEG: a plugin's artwork is flat colour and hard edges, which is what PNG is for and what JPEG is worst at, and a JPEG decoder is a DCT and a Huffman decoder and a colour transform for a format nobody should be using here.

16-bit-per-channel PNGs are read and reduced to 8, because a Bitmap is 8 and keeping the extra bits would mean carrying them nowhere. Interlaced (Adam7) PNGs are REFUSED by name rather than decoded wrong -- they are rare, they are a second pass over the whole image, and a decoder that silently produced a quarter-resolution picture would be worse than one that says no.

Every length in the file is attacker-controlled

A PNG arrives from wherever a plugin's assets came from, which for a generated plugin is a pipeline nobody audits. Width times height times four overflows a 32-bit size at a width and height that both fit in the header, so the size is computed in 64 bits and refused before anything is allocated.

Types 2

A decoded image, or a reason it is not one.

The reason is a string rather than an enum because it goes in front of a person: "interlaced PNGs are not supported" tells somebody what to do, where kErrorUnsupported tells them to go and read a header.

Decodes a PNG from bytes in memory into a premultiplied RGBA Bitmap. What it reads and refuses, and why every length is distrusted, is in the file comment.

Stateless: decode() is a static function of its input, safe from any thread but not the audio thread (it inflates and allocates). Any failure, including a header that asks for more than kMaxPixels, comes back as an empty bitmap and a sentence in error. ImageFileFormat is the entry point that also reads JPEG and GIF.

↑↓ to move↵ to openEvery public name in the headers, and every guide