JFLib

img_core/io.rs#

Source: lib/img_core/io.rs

io -- image files: format detection, codec dispatch, load and save; the format table and
the glue of the core codecs                    [src/img/imgio.s, imgreg.s, imgcore.s glue]

Classic API (FMT_PARGB, PNG / JPEG / BMP / GIF only, through the codecs' decode_fl / save):
  decode(data) -> Ok(surface) / Err(ERR_*)     PNG, JPEG, GIF or BMP by magic bytes (and SVG
                                               once img_svg::register entered IMGF_SVG)
  load(path) -> Ok(surface) / Err(ERR_*)
  save(path, sf) / save_ex(path, sf, quality) -> 0 / ERR_*   by extension (.png .jpg .jpeg
                                               .jfif .jpe .bmp .dib .gif, any case)
  format(path) -> IMGF_* of those extensions (0 = unknown)
Extended API:
  detect(data, hint) -> IMGF_* / 0             magic bytes (TGA: hint or its probe)
  probe(data, meta) -> IMGF_* / 0              detect + header scan into meta
  decode_ex(data, dec, meta) -> Ok(surface) / Err(ERR_*)
  load_ex(path, dec, meta) -> Ok(surface) / Err(ERR_*)
  encode(sf, opt) -> Ok(file bytes) / Err(ERR_*)
  save_opt(path, sf, opt) -> 0 / ERR_*         atomic write (the file hook)
  opt_init(fmt) -> ImgOpt                      paint.net's defaults
  format_info(fmt) -> Option<FmtInfo>          format_from_path(path) -> IMGF_* / 0
  filter_list(need, cap) -> the ui_filedlg list "png=PNG\0jpg;...=JPEG\0...\0" (empty when
                                               it does not fit into cap bytes)
  register(fmt, codec) -> 0 / ERR_UNSUPPORTED  register_dyn(name, exts, mime, props, codec)
  register_slot(IMGF_SVG, name, exts, mime, props, codec)   (svg) a built-in format without
                                               a static entry (img_svg::register; IMGF_JP2
                                               and IMGF_JBIG2: img_all::use_all_codecs)
  set_file_io(FileIo)                          the file functions of load / save (img_core
                                               does not depend on plat: the app sets them)
Glue for the core codecs (imgcore.s):
  core_decode(data, sf, dec, meta, scan, decode_fl) -> 0 / ERR_*   scanner, frame / size
                                               checks, then the classic decoder
  core_probe(data, meta, scan) -> bool
  dpi_ppm(dpi) / ppm_dpi(ppm)                  16.16 dpi <-> pixels per metre

The format table has a static entry per built-in format (name, extensions, MIME,
properties); a codec crate's `CODEC` gets into it through register (img_all::
use_core_codecs / use_all_codecs).  Runtime formats (plugins) use the slots IMG_DYN0 ..
IMGF_COUNT - 1.

decode_ex: the codec gets a normalised copy of dec (maxpx filled in; ICC / EXIF / XMP
dropped when the caller passed no ImgMeta); the ImgMeta is cleared first and gets fmt,
orient 1, frames 1.  After a successful decode the orientation is applied (unless
IMGD_NO_ORIENT or the codec did it: IMF_ORIENT_APPLIED) and the pixels are converted to
FMT_ARGB (IMGD_STRAIGHT) or FMT_PARGB.  On an error the blobs are dropped (the other
metadata fields stay).
  • fn format_info(fmt: u32) -> Option<FmtInfo> the format table entry of fmt (None for 0, out of range or a free runtime slot) [img_format_info]

  • fn codec(fmt: u32) -> Option<&'static Codec> the registered codec of fmt [img_codec]

  • fn register(fmt: u32, c: Option<&'static Codec>) -> i32 put a codec descriptor (or None to remove one) into the format table: caps = the format's properties | the codec's caps, iom = the codec's iom [img_register]

  • fn register_dyn(name: &'static str, exts: &'static str, mime: &'static str, props: u32, c: &'static Codec) -> u32 a new runtime format (plugins): name "QOI", exts "qoi=QOI", mime, props (IFC_* format properties; DECODE / ENCODE come from the codec) -> its IMGF (IMG_DYN0..) or 0 when every slot is taken or name / exts is empty [img_register_dyn]

  • fn register_external(name: &'static str, exts: &'static str, props: u32, c: &'static Codec) -> u32 Separate slots for external codec hosts, preserving the native plugin ABI's IDs: 23 to 51, or 0 when they are taken (the last three slots, IMGF_JBIG2, IMGF_JP2 and IMGF_SVG, are kept for JBIG2, JPEG 2000 and SVG; slots 52 and 53 went to JBIG2 and JPEG 2000, so a 30th registration gets 0).

  • fn register_slot(fmt: u32, name: &'static str, exts: &'static str, mime: &'static str, props: u32, c: Option<&'static Codec>) -> i32 a built-in format that has no entry in the static table (IMGF_SVG, IMGF_JP2, IMGF_JBIG2): its name, extension list, MIME type, properties and codec into slot fmt (None empties the slot) -> 0 / ERR_UNSUPPORTED (fmt out of range, or a static / runtime format's slot)

  • fn filter_list(need: u32, cap: usize) -> Vec<u8> a ui_filedlg extension list "png=PNG\0jpg;jpeg;...=JPEG\0...\0" of every format whose caps contain all bits of need; empty when it does not fit into cap bytes (incl. the final empty entry) [img_filter_list]

  • fn opt_init<'a>(fmt: u32) -> ImgOpt<'a> an ImgOpt with paint.net's defaults for fmt: quality 95 (JPEG / WebP / AVIF / HEIC), 90 (JPEG XL / XR), dithering 7, transparency threshold 128, JPEG chroma 4:2:0, TGA 32 bit + RLE, DDS BC1, JPEG XL effort 7 [img_opt_init]

  • fn format(path: &str) -> u32 IMGF_PNG / JPEG / BMP / GIF from the file extension (any case), 0 when unknown [img_format]

  • fn format_from_path(path: &str) -> u32 IMGF_* from the file extension (any case), 0 when unknown; the registered runtime formats after the built-in ones [img_format_from_path]

  • fn format_from_bytes(path: &[u8]) -> u32 format_from_path of a path given as bytes (up to the first NUL)

  • struct FileIo the file functions of load / save (img_core sits below plat)

    • read: fn(&str) -> Option<Vec<u8>>
    • write: fn(&str, &[u8]) -> bool -- atomic write
  • fn set_file_io(io: FileIo) set the file functions (call once at start-up, e.g. with plat::read_file / write_file)

  • fn decode(data: &[u8]) -> Result<Surface, i32> decode PNG, JPEG, GIF or BMP data (by its magic bytes) into a new FMT_PARGB surface [img_decode]

  • fn load(path: &str) -> Result<Surface, i32> read a PNG, JPEG, GIF or BMP file into a new FMT_PARGB surface [img_load]

  • fn save(path: &str, sf: &Surface) -> i32 write sf in the format of the file extension (see format); JPEG with quality 90 [img_save]

  • fn save_ex(path: &str, sf: &Surface, quality: i32) -> i32 save with a JPEG quality (1..100, 0 = 90) [img_save_ex]

detection

PNG_SIG: [u8; 8] = [0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A]
  • fn detect(data: &[u8], hint: u32) -> u32 the format of data by its magic bytes (order: PNG, JPEG, GIF, DDS, WebP, TIFF / BigTIFF, JPEG XR, ISOBMFF (AVIF / HEIC), JPEG XL container, PDN, gzip (legacy PDN), JPEG XL codestream, BMP, runtime formats, TGA by hint or its registered probe) -> IMGF_* / 0 [img_detect]

  • fn probe(data: &[u8], meta: Option<&mut ImgMeta>) -> u32 detect without a hint; with a meta: it is cleared (fmt, orient 1, frames 1 set) and the registered codec's probe reads the header into it (w = 0 when the header could not be read; no blobs) [img_probe]

  • fn decode_ex(data: &[u8], dec: Option<&ImgDec>, meta: Option<&mut ImgMeta>) -> Result<Surface, i32> decode any registered format into a new surface: FMT_ARGB with IMGD_STRAIGHT, else FMT_PARGB (see the module header) [img_decode_ex]

  • fn load_ex(path: &str, dec: Option<&ImgDec>, meta: Option<&mut ImgMeta>) -> Result<Surface, i32> read a file and decode_ex it; a zero dec.hint comes from the file extension; ERR_IO when unreadable [img_load_ex]

  • fn encode(sf: &Surface, opt: Option<&ImgOpt>) -> Result<Vec<u8>, i32> encode sf (FMT_XRGB, PARGB, ARGB or A8) as opt.fmt [img_encode] Errors: ERR_UNSUPPORTED (format unknown / not registered / no encoder, or another surface format), ERR_DIMS (empty surface), ERR_CANCEL and the codec errors.

  • fn save_opt(path: &str, sf: &Surface, opt: Option<&ImgOpt>) -> i32 encode and write the file atomically; fmt 0 takes the format from the extension; ERR_IO when writing fails [img_save_opt]

  • fn core_decode(data: &[u8], sf: &mut Surface, dec: &ImgDec, meta: &mut ImgMeta, scan: ScanFn, decode_fl: DecodeFlFn) -> i32 run the metadata scanner; with IMGD_PROBE return its result; else refuse frame > 0, check the header size (ERR_LIMIT) and call the classic decoder with JDF_STRAIGHT (IMGD_STRAIGHT) | JDF_NO_ORIENT (IMGD_NO_ORIENT) [core_decode]

  • fn core_probe(data: &[u8], meta: &mut ImgMeta, scan: ScanFn) -> bool the scanner without blobs: recognised (meta filled) [core_probe]

  • fn dpi_ppm(dpi: u32) -> u32 pixels per metre from a 16.16 dpi value (rounded, at most 0x7FFFFFFF) [dpi_ppm]

  • fn ppm_dpi(ppm: u32) -> u32 16.16 dpi from pixels per metre (0 -> 0) [ppm_dpi]

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