JFLib

img_pdn#

paint.net documents (.pdn) -- crate root lib/img_pdn/lib.rs

img_pdn -- paint.net documents (.pdn): read and write, compatible with paint.net 5.1, and
the MS-NRBF (.NET BinaryFormatter) parser / writer they are built on
                                                   [src/img/pdn.s, src/img/nrbf.s]

  CODEC                              the IMGCODEC (pdn_codec): probe, flat decode (the
                                     composite), flat encode (one-layer document)
  decode(data, dec) -> PdnDoc        pdn_decode: the file with every layer
  decode_ex(data, dec, flags)        pdn_decode_ex: PDND_PROBE = size, resolution and
                                     metadata only (no layers)
  encode(doc, opt) -> file bytes     pdn_encode (IO_EFFORT = deflate level 1..9, default 1)
  encode_ex(doc, opt, flags)         pdn_encode_ex: PDNE_BLENDOP = the 3.x blendOp classes
  flatten(doc) -> Surface            pdn_flatten: the composite of the visible layers
                                     (FMT_ARGB, paint.net blend modes and opacity)
  probe / decode_flat / encode_flat  pdn_probe / pdn_decode_flat / pdn_encode_flat
  nrbf::Nrbf, nrbf::Nw               the NRBF reader / writer (nrbf.s)

The document records of the assembly (img.inc PDNDOC / PDNLAYER) are PdnDoc / PdnLayer:
the reader returns an owned PdnDoc (pdn_free = drop).  The writer takes a borrowed view,
DocRef / LayerRef, so a caller hands its own layers over without copying pixels (JFPainter's
save_pdn borrows its layer surfaces the same way); PdnDoc::view() makes one of a PdnDoc.
JFPainter (PaintCraft in R11): open_pdn = decode(data, Some(&dec)), then move each layer's surface, name
and metadata blob out of doc.layers (core::mem::take) and use doc.unit / dpux / dpuy;
save_pdn = a DocRef over its layers (LayerRef per layer) and encode(&docref, Some(&opt)).
Field semantics are those of the asm records: opacity / visible / blend / isbg as stored,
blend values > PDNB_XOR mean Normal, the metadata blobs are {u32 klen, key, u32 vlen,
value}* in file order, the writer accepts FMT_XRGB / PARGB / ARGB / A8 layers.
Threads: deferred payloads with several chunks are decoded, and every layer's chunks are
compressed, on the plat::parallel_for pool (as the assembly's Z1 worker-pool loops).

File format (confirmed on paint.net 5.1.12 files):
  "PDN3" | u24 LE n | n bytes UTF-8 XML header (size + thumbnail PNG in base64) |
  00 01 | MS-NRBF object graph | the deferred MemoryBlock payloads, in the order of the
  MemoryBlock objects in the stream: u8 format (0 gzip, 1 raw), u32 BE chunk size, then
  ceil(length / chunk) chunks in any order {u32 BE index, u32 BE size, data}.
  Legacy: "PDN3" + header + 1F 8B = gzip of everything else; a bare gzip file (<= 2.0).
Resolution and the ICC profile live in the document metadata as EXIF items
("$exif.tagN[0]" = <exif id="296|282|283|34675" ... value="base64" />).

Modules: nrbf (nrbf.s), dec (the reader: pdn.s decoding), enc (the writer: pdn.s encoding),
template (the NRBF class templates, generated by tools/gen_pdn_template.py).

img_pdn API#

constants (img.inc)

PDU_PIXEL: u32 = 1  // PdnDoc::unit (paint.net MeasurementUnit values)
PDU_INCH: u32 = 2
PDU_CM: u32 = 3
PDNB_NORMAL: u8 = 0  // PdnLayer::blend: paint.net LayerBlendMode = gfx BM_*
PDNB_MULTIPLY: u8 = 1
PDNB_ADDITIVE: u8 = 2
PDNB_COLORBURN: u8 = 3
PDNB_COLORDODGE: u8 = 4
PDNB_REFLECT: u8 = 5
PDNB_GLOW: u8 = 6
PDNB_OVERLAY: u8 = 7
PDNB_DIFFERENCE: u8 = 8
PDNB_NEGATION: u8 = 9
PDNB_LIGHTEN: u8 = 10
PDNB_DARKEN: u8 = 11
PDNB_SCREEN: u8 = 12
PDNB_XOR: u8 = 13
PDND_PROBE: u32 = 1  // decode_ex: size, resolution and metadata only (no layers)
PDNE_BLENDOP: u32 = 1  // encode_ex: BitmapLayerProperties.blendOp = the UserBlendOps
  • static CODEC: Codec the codec descriptor [pdn_codec]: IO_EFFORT = deflate level of the pixel chunks, IO_META's ICC profile with IOF_EMBED_ICC

  • struct PdnDoc a paint.net document (PDNDOC)

    • w: u32
    • h: u32
    • unit: u32 -- PDU_* (paint.net MeasurementUnit)
    • dpux: f64 -- dots per unit (IEEE double, as stored by paint.net)
    • dpuy: f64
    • layers: Vec<PdnLayer> -- bottom layer first (PD_N = layers.len(); none after a PDND_PROBE decode)
    • nvc: Vec<u8> -- Document.Metadata, serialized: items {u32 klen, key, u32 vlen, value} (UTF-8, the KeyValuePair / NameValueCollection entries in file order); empty = none
    • meta: Option<ImgMeta> -- decoded: IMGF_PDN, size, 8-bit RGBA, resolution (dpix / dpiy / resunit from the metadata EXIF items 296 / 282 / 283) and the ICC profile (34675); for view(): the ICC profile to embed
    • xml: Vec<u8> -- the file's XML header (UTF-8)
  • struct PdnLayer a layer (PDNLAYER)

    • sf: Surface -- decoded: FMT_ARGB straight (paint.net ColorBgra); for the writer any of FMT_XRGB, FMT_PARGB, FMT_ARGB, FMT_A8 with the document's size
    • name: Vec<u8> -- UTF-8 name; the writer (and the assembly's NUL-terminated strings) end it at the first NUL, the reader cuts it there
    • opacity: u8 -- 0..255
    • visible: u8 -- 0 / 1
    • blend: u8 -- PDNB_*
    • isbg: u8 -- 1 = background layer
    • meta: Vec<u8> -- layer metadata, serialized like PdnDoc::nvc; empty = none
  • struct DocRef<'a> the writer's view of a document: everything borrowed

    • w: u32
    • h: u32
    • unit: u32
    • dpux: f64
    • dpuy: f64
    • layers: Vec<LayerRef<'a>>
    • nvc: &'a [u8]
    • icc: &'a [u8] -- the ICC profile to embed (EXIF item 34675); empty = none
  • struct LayerRef<'a> the writer's view of a layer

    • sf: &'a Surface
    • name: &'a [u8]
    • opacity: u8
    • visible: u8
    • blend: u8
    • isbg: u8
    • meta: &'a [u8]

impl PdnLayer

  • fn view(&self) -> LayerRef<'_>

impl PdnDoc

  • fn view(&self) -> DocRef<'_> the document as the writer sees it (ICC profile from meta)

  • fn flatten(doc: &DocRef) -> Result<Surface, i32> a new FMT_ARGB surface with the composite of the visible layers (paint.net blend modes and opacity, k_bm_row); layers of any surface format with the document's size (ERR_DIMS otherwise) [pdn_flatten]

From lib/img_pdn/dec.rs:

  • fn decode(data: &[u8], dec: Option<&ImgDec>) -> Result<PdnDoc, i32> read a .pdn: every layer [pdn_decode]

  • fn decode_ex(data: &[u8], dec: Option<&ImgDec>, flags: u32) -> Result<PdnDoc, i32> pdn_decode; flags PDND_PROBE: only size, resolution and metadata (no layers) [pdn_decode_ex]

  • fn probe(data: &[u8], m: &mut ImgMeta) -> bool "PDN3" header: width / height from the XML, 8-bit straight BGRA; legacy gzip files are recognised without a size [pdn_probe]

  • fn decode_flat(data: &[u8], out: &mut Surface, dec: &ImgDec, meta: &mut ImgMeta) -> i32 img_decode_ex's entry: the composite of the visible layers (FMT_ARGB); IMGD_PROBE: metadata only; IMGD_ICC: the profile of the document metadata [pdn_decode_flat]

From lib/img_pdn/enc.rs:

  • fn encode(doc: &DocRef, opt: Option<&ImgOpt>) -> Result<Vec<u8>, i32> write the document as a .pdn file (opt: IO_EFFORT = deflate level 1..9, default 1) [pdn_encode]

  • fn encode_ex(doc: &DocRef, opt: Option<&ImgOpt>, flags: u32) -> Result<Vec<u8>, i32> pdn_encode with PDNE_* flags; errors: ERR_DIMS (sizes, a layer of another size, no layers), ERR_UNSUPPORTED (surface format), ERR_LIMIT (XML header > 16 MB, the composite over the default pixel budget), ERR_CANCEL [pdn_encode_ex]

  • fn encode_flat(sf: &Surface, opt: &ImgOpt, out: &mut Vec<u8>) -> i32 img_encode's entry: sf as the background layer of a one-layer document (resolution from opt.meta dpix / dpiy, ICC profile with IOF_EMBED_ICC) [pdn_encode_flat]

img_pdn modules#

The codec's building blocks (public for the codec test app codectest; see their source headers):

ModuleContent
img_pdn::nrbfMS-NRBF (.NET BinaryFormatter) reader and template-driven writer

Source: docs/API.md in the JFLib repository.