JFLib

img_core crate root#

Re-exports: gfx::Surface

  • type ParallelFor = fn(usize, &(dyn Fn(usize) + Sync)) runs f(i) for every i in 0..n, possibly on several threads (thr_parallel_for)

  • fn set_parallel(f: ParallelFor) the loop the codecs' parallel parts run on (the asm's thr_parallel_for: ICC LUT grids, YUV bands); the apps pass a wrapper of plat::parallel_for -- img_core does not depend on plat. Without it everything runs on the calling thread (the same bytes)

  • fn parallel() -> Option<ParallelFor> the loop set by set_parallel

errors (jfgui.inc)

ERR_FAIL: i32 = -1
ERR_TRUNC: i32 = -1  // input ends early
ERR_HDR: i32 = -2  // bad header / signature
ERR_BLOCK: i32 = -3  // bad deflate block or PNG filter
ERR_CODE: i32 = -4  // bad Huffman code
ERR_DIST: i32 = -5  // distance too far back / invalid
ERR_OVERFLOW: i32 = -6  // output larger than the buffer
ERR_CHECKSUM: i32 = -7  // Adler-32 mismatch
ERR_NOMEM: i32 = -8  // allocation failed
ERR_UNSUPPORTED: i32 = -9  // valid but unsupported variant
ERR_CRC: i32 = -10  // PNG chunk CRC mismatch
ERR_CHUNK: i32 = -11  // bad / missing PNG chunk
ERR_DIMS: i32 = -12  // dimensions out of range
ERR_IO: i32 = -13  // file could not be read / written
ERR_CANCEL: i32 = -14  // cancelled through a cancel flag
ERR_LIMIT: i32 = -15  // resource limit

formats (jfgui.inc IMGF_*)

IMGF_PNG: u32 = 1
IMGF_JPEG: u32 = 2
IMGF_BMP: u32 = 3
IMGF_GIF: u32 = 4
IMGF_TIFF: u32 = 5
IMGF_TGA: u32 = 6
IMGF_DDS: u32 = 7
IMGF_WEBP: u32 = 8
IMGF_AVIF: u32 = 9
IMGF_HEIC: u32 = 10
IMGF_JXL: u32 = 11
IMGF_JXR: u32 = 12
IMGF_PDN: u32 = 13
IMG_DYN0: u32 = 14  // runtime formats (plugins), start inclusive
IMG_DYN_END: u32 = 22  // runtime formats, end exclusive
// Keep the eight established plugin slots (14 through 21) stable; new built-ins follow them.
IMGF_PSD: u32 = 22
IMG_EXTERNAL0: u32 = 23  // out-of-process codecs; native PNAPI IDs stay stable
IMGF_COUNT: u32 = 55
/// (svg) SVG: the last slot, outside the static table until img_svg::register fills it
/// (register_external stops before it)
IMGF_SVG: u32 = IMGF_COUNT - 1
/// JPEG 2000 (JP2, JPX, J2K): the slot before SVG's, outside the static table until
/// img_all::use_all_codecs fills it with img_jp2's codec (a stored 53 names JPEG 2000;
/// register_external stops before IMGF_JBIG2)
IMGF_JP2: u32 = IMGF_COUNT - 2
/// JBIG2 files (their first page): the slot before JPEG 2000's, outside the static table until
/// img_all::use_all_codecs fills it with img_jbig2's codec.  register_external stops before it:
/// the external codecs keep their numbers 23 to 51; a 30th external registration, which got
/// slot 52 before, now gets 0, and a stored 52 now names JBIG2
IMGF_JBIG2: u32 = IMGF_COUNT - 3

limits

IMG_MAXDIM: u32 = 65535  // largest width / height (ERR_DIMS)
IMG_MAXPX_DEF: u64 = 1 << 28  // default pixel budget (ERR_LIMIT)

decode flags (ImgDec::flags)

IMGD_STRAIGHT: u32 = 1  // FMT_ARGB instead of FMT_PARGB
IMGD_ICC: u32 = 2  // copy an embedded ICC profile into ImgMeta::icc
IMGD_EXIF: u32 = 4  // copy the EXIF block (TIFF structure)
IMGD_XMP: u32 = 8  // copy the XMP packet
IMGD_NO_ORIENT: u32 = 16  // do not apply the EXIF / container orientation
IMGD_PROBE: u32 = 32  // header + metadata only
IMGD_THUMB: u32 = 64  // an embedded preview / reduced size is acceptable

metadata (ImgMeta)

IMM_GRAY: u8 = 1  // colour models
IMM_RGB: u8 = 2
IMM_PAL: u8 = 3
IMM_CMYK: u8 = 4
IMM_YCC: u8 = 5
IMM_LAB: u8 = 6
IMM_YCCK: u8 = 7
IMF_ALPHA: u32 = 1  // flags
IMF_SRC_PREMUL: u32 = 2
IMF_LOSSLESS: u32 = 4
IMF_ANIMATED: u32 = 8
IMF_HDR: u32 = 16
IMF_ORIENT_APPLIED: u32 = 32
IMF_FLOAT: u32 = 64
IMF_INTERLACED: u32 = 128
IMF_SRGB: u32 = 256
IMR_NONE: u32 = 0  // resolution units
IMR_INCH: u32 = 1
IMR_CM: u32 = 2

encode options (ImgOpt)

IOF_LOSSLESS: u32 = 1
IOF_RLE: u32 = 2
IOF_MIPMAPS: u32 = 4
IOF_SRGB: u32 = 8
IOF_CUBE_CROSS: u32 = 16
IOF_EMBED_ICC: u32 = 32
IOF_EMBED_EXIF: u32 = 64
IOF_EMBED_XMP: u32 = 128
IOF_INTERLACE: u32 = 256
DDSF_BC1: u32 = 1
DDSF_BC2: u32 = 2
DDSF_BC3: u32 = 3
DDSF_BC4: u32 = 4
DDSF_BC5: u32 = 5
DDSF_BC6H: u32 = 6
DDSF_BC7: u32 = 7
DDSF_BGRA8: u32 = 8
DDSF_BGRX8: u32 = 9
DDSF_RGBA8: u32 = 10
DDSF_BGR5A1: u32 = 11
DDSF_BGRA4: u32 = 12
DDSF_B5G6R5: u32 = 13
DDSF_L8: u32 = 14
DDSF_A8: u32 = 15
DDSF_R8G8: u32 = 16
MIPF_BOX: u32 = 1
MIPF_LINEAR: u32 = 2
MIPF_CUBIC: u32 = 3
MIPF_NEAREST: u32 = 4
TIFC_NONE: u32 = 1
TIFC_LZW: u32 = 2
TIFC_DEFLATE: u32 = 3
TIFC_PACKBITS: u32 = 4
IOM_QUALITY: u32 = 1  // which ImgOpt fields an encoder honours
IOM_LOSSLESS: u32 = 2
IOM_RLE: u32 = 4
IOM_MIPMAPS: u32 = 8
IOM_BITDEPTH: u32 = 16
IOM_SAMPLEBITS: u32 = 32
IOM_DITHER: u32 = 64
IOM_ATHRESH: u32 = 128
IOM_CHROMA: u32 = 256
IOM_EFFORT: u32 = 512
IOM_DDSFMT: u32 = 1024
IOM_MIPFILTER: u32 = 2048
IOM_TIFFCOMP: u32 = 4096
IOM_SRGB: u32 = 8192
IOM_CUBE: u32 = 16384
IOM_ICC: u32 = 32768
IOM_EXIF: u32 = 65536
IOM_XMP: u32 = 131072
IOM_INTERLACE: u32 = 262144

format table (FmtInfo::caps)

IFC_DECODE: u32 = 1
IFC_ENCODE: u32 = 2
IFC_ALPHA: u32 = 4
IFC_LAYERS: u32 = 8
IFC_ANIM: u32 = 16
IFC_PAGES: u32 = 32
IFC_MIPS: u32 = 64
IFC_LOSSY: u32 = 128
IFC_LOSSLESS: u32 = 256
IFC_HDR: u32 = 512
IFC_ICC: u32 = 1024
IFC_EXIF: u32 = 2048
IFC_XMP: u32 = 4096
IFC_CORE: u32 = 8192
  • struct ImgDec<'a> decode options (IMGDEC); Default = premultiplied output, orientation applied, frame 0, no hint, IMG_MAXPX_DEF pixels, no metadata blobs, no cancel flag
    • flags: u32 -- IMGD_*
    • frame: u32 -- frame / page / mip level
    • hint: u32 -- IMGF_* expected from the file name (0 = none)
    • maxpx: u64 -- max w * h (0 = IMG_MAXPX_DEF) -> ERR_LIMIT
    • maxmem: u64 -- memory budget for codec scratch (0 = no limit)
    • cancel: Option<&'a AtomicU32>

impl ImgDec<'_>

  • fn max_pixels(&self) -> u64 the pixel budget (maxpx, or the default)

  • fn cancelled(&self) -> bool the cancel flag is set

  • struct ImgMeta decoded metadata (IMGMETA)

    • fmt: u32 -- IMGF_*
    • w: u32 -- stored width (before orientation)
    • h: u32 -- stored height
    • bits: u8 -- bits per sample of the source (1..16, 32 = float)
    • model: u8 -- IMM_*
    • orient: u8 -- EXIF orientation 1..8
    • channels: u8 -- samples per pixel incl. alpha (0 = unknown)
    • flags: u32 -- IMF_*
    • frames: u32 -- frames / pages / mip levels
    • dpix: u32 -- dots per inch 16.16 (0 = unknown)
    • dpiy: u32
    • cicp: u32 -- primaries | transfer << 8 | matrix << 16 | full range << 24
    • gamma: u32 -- file gamma * 100000 (PNG gAMA)
    • icc: Vec<u8> -- IMGD_ICC
    • exif: Vec<u8> -- IMGD_EXIF: TIFF structure without "Exif\0\0"
    • xmp: Vec<u8> -- IMGD_XMP
    • delay: u32 -- ms (animations)
    • loops: u32 -- animation loop count (0 = forever)
    • quality: u32 -- estimated encoder quality 1..100
    • resunit: u32 -- IMR_*
    • chrm: [u16; 8] -- IM_CHRM: PNG cHRM white x, y, red x, y, green x, y, blue x, y / 50000
    • dxgi: u16 -- IM_DXGI: DXGI_FORMAT of DDS pixel data
    • texfl: u8 -- IM_TEXFL: TXF_*
    • texn: u32 -- IM_TEXN: DDS array size / cubes / depth

impl ImgMeta

  • fn to_bytes(&self) -> [u8; 128] the 128-byte IMGMETA record of the assembly (blob pointers 0, lengths kept)

  • struct ImgOpt<'a> encode options (IMGOPT); Default = the format's defaults

    • fmt: u32 -- IMGF_* (save: 0 = from the file extension)
    • quality: u32 -- 1..100 (0 = format default)
    • flags: u32 -- IOF_*
    • bitdepth: u32 -- bits per pixel 0 auto / 32 / 24 / 8
    • samplebits: u32 -- bits per sample 8 / 10 / 12 (0 = 8)
    • dither: u32 -- dithering level + 1 (0 = default 7, 1 = off .. 9 = level 8)
    • athresh: u32 -- transparency threshold + 1 (0 = default 128)
    • chroma: u32 -- 444 / 422 / 420 / 400 (0 = format default)
    • effort: u32 -- 1..9 (0 = default); PNG: deflate level
    • ddsfmt: u32 -- DDSF_*
    • mipfilter: u32 -- MIPF_*
    • tiffcomp: u32 -- TIFC_*
    • meta: Option<&'a ImgMeta> -- metadata to embed
    • cancel: Option<&'a AtomicU32>

impl<'a> ImgOpt<'a>

  • fn from_bytes(b: &[u8]) -> ImgOpt<'a> the scalar fields of a 64-byte IMGOPT record (IO_META / IO_CANCEL ignored)

  • fn cancelled(&self) -> bool

  • type ProbeFn = fn(&[u8], &mut ImgMeta) -> bool probe(data, meta) -> recognised (header + metadata scan, no pixels)

  • type DecodeFn = fn(&[u8], &mut Surface, &ImgDec, &mut ImgMeta) -> i32 decode(data, out, dec, meta) -> 0 / ERR_* (meta: the caller's ImgMeta, already cleared with fmt set; dec.flags tell which blobs to copy)

  • type EncodeFn = fn(&Surface, &ImgOpt, &mut Vec<u8>) -> i32 encode(surface, opt, out) -> 0 / ERR_* (FMT_XRGB / PARGB / ARGB / A8 input)

  • type ScanFn = fn(&[u8], &mut ImgMeta, u32) -> i32 scan(data, meta, flags) -> 0 / ERR_*: the header / metadata scanner of a core format (png_meta, jpeg_meta, bmp_meta, gif_meta; flags = IMGD_* of the blobs to copy)

  • type DecodeFlFn = fn(&[u8], &mut Surface, u32) -> i32 decode_fl(data, out, jdf) -> 0 / ERR_*: the classic decoder (png_decode_fl, ...); jdf = JDF_STRAIGHT (FMT_ARGB) | JDF_NO_ORIENT, else FMT_PARGB

  • type SaveFn = fn(&Surface, u32) -> Option<Vec<u8>> save(surface, quality) -> file bytes or None (out of memory, unsupported surface): the classic encoder of img_save (png_encode, jpeg_encode(quality), bmp_encode, gif_encode)

  • struct Codec a codec descriptor (IMGCODEC); every codec crate exports one pub static CODEC: Codec (Codec { probe: Some(..), decode: Some(..), ..Codec::EMPTY })

    • probe: Option<ProbeFn>
    • decode: Option<DecodeFn>
    • encode: Option<EncodeFn>
    • caps: u32 -- IFC_DECODE / IFC_ENCODE really implemented
    • iom: u32 -- IOM_* the encoder honours
    • scan: Option<ScanFn> -- core formats: metadata scanner
    • decode_fl: Option<DecodeFlFn> -- core formats: classic decoder (img_decode)
    • save: Option<SaveFn> -- core formats: classic encoder (img_save)

impl Codec

  • const EMPTY: Codec = Codec { probe: None, decode: None, encode: None, caps: 0, ...

core codec options (img.inc CA)

JDF_STRAIGHT: u32 = 1  // FMT_ARGB output
JDF_NO_ORIENT: u32 = 2  // keep the stored orientation
JDF_RGB: u32 = 4  // 3 components are R, G, B (TIFF Photometric 2)
JDF_YCC: u32 = 8  // 3 components are YCbCr (Photometric 6)
JDF_CMYK: u32 = 16  // 4 components are plain CMYK (Photometric 5)
/// 4 components: the samples as stored (C, M, Y, K; a YCCK image's CMY after the YCC step) in the
/// bytes R, G, B and A instead of RGB, for a caller with its own CMYK conversion (a PDF page)
JDF_RAW_CMYK: u32 = 32
PNG_IOM: u32 = IOM_BITDEPTH | IOM_DITHER | IOM_ATHRESH | IOM_EFFORT | IO ...
JPEG_IOM: u32 = IOM_QUALITY | IOM_CHROMA | IOM_ICC | IOM_EXIF | IOM_XMP
BMP_IOM: u32 = IOM_BITDEPTH | IOM_DITHER
GIF_IOM: u32 = IOM_DITHER | IOM_ATHRESH
TXF_CUBE: u8 = 1  // ImgMeta::texfl (DDS)
TXF_VOLUME: u8 = 2
TXF_ARRAY: u8 = 4
TXF_CROSS: u8 = 8
TXF_PREMUL: u8 = 16
TXF_LEGACY: u8 = 32
  • struct FmtInfo a format table entry (IMGFMT)
    • name: &'static str
    • exts: &'static str -- ui file dialog entry "png=PNG" / "jpg;jpeg;...=JPEG"
    • mime: &'static str
    • caps: u32 -- IFC_* format properties | registered DECODE / ENCODE
    • iom: u32 -- IOM_* of the registered encoder
    • codec: Option<&'static Codec>

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