JFLib

img_core/meta.rs#

Source: lib/img_core/meta.rs

meta -- shared helpers of the codecs: metadata blobs, size limits, straight <->
premultiplied conversion, orientation, sample depth conversion, JPEG segment walking
                                                                     [src/img/imgmeta.s]

  meta_blob(meta, Blob, src)            copy src into the ICC / EXIF / XMP blob (len 0: nothing)
  meta_free(meta)                       drop the blobs (the other fields stay)
  dims_check(w, h, dec) -> 0 / ERR_DIMS / ERR_LIMIT    w, h 1..IMG_MAXDIM, w * h <= maxpx
  mem_check(bytes, dec) -> 0 / ERR_LIMIT               against dec.maxmem (0 = no limit)
  surf_alloc(w, h, fmt, dec) -> Ok(surface) / Err(ERR_*)   dims_check + a zeroed surface
  premultiply(sf) / unpremultiply(sf)   FMT_ARGB <-> FMT_PARGB in place (others untouched)
  convert(sf, fmt) -> 0 / ERR_UNSUPPORTED   in place to FMT_PARGB / FMT_ARGB from XRGB,
                                        PARGB, ARGB
  surf_convert(src, fmt) -> Ok(surface) / Err(ERR_*)   a new surface from any get_row source
  get_row(sf, y, dst, fmt)              row y as w straight (FMT_ARGB) / premultiplied
                                        (FMT_PARGB) BGRA pixels from XRGB (alpha 255),
                                        PARGB, ARGB, A8 (grey); other formats: transparent
  orient(sf, o) -> 0                    apply an EXIF orientation 2..8 (5..8 swap w and h)
  u16_to_u8(v)                          round(v / 257)
  row_u16_to_u8(dst, src, be)           16-bit samples (bytes, LE / BE) -> bytes
  half_to_f32(h)                        IEEE half -> single precision bits
  row_half_to_u8(dst, src) / row_f32_to_u8(dst, src)   round(clamp(v, 0, 1) * 255), NaN -> 0
  row_f32_to_srgb8(dst, src)            linear floats -> sRGB bytes (4096-entry table)
  jpg_seg_next(data, js) -> marker      JPEG marker segment iterator (JSeg)
  srgb_lin(v) / SRGB_LIN_BITS           sRGB byte -> linear light (f32)
  all_opaque(sf) -> bool                every alpha of a 32-bit surface is 255  [png.s]
  unpremul(p)                           premultiplied -> straight, one pixel  [png.s]

Straight -> premultiplied: round(c * a / 255); premultiplied -> straight: min(255,
(c * ceil(255 * 2^24 / a) + 2^23) >> 24), a = 0 gives 0 (gfx::straight's kernels have
exactly these rules).
  • enum Blob the blobs of an ImgMeta (IM_ICC / IM_EXIF / IM_XMP)

    • Icc
    • Exif
    • Xmp
  • fn meta_blob(meta: &mut ImgMeta, which: Blob, src: &[u8]) store a copy of src as the blob (replacing one already there); an empty src stores nothing [img_meta_blob]

  • fn meta_free(meta: &mut ImgMeta) drop the ICC / EXIF / XMP blobs (the other fields stay) [img_meta_free]

  • fn dims_check(w: u32, h: u32, dec: &ImgDec) -> i32 w and h must be 1..IMG_MAXDIM (ERR_DIMS), w * h at most dec's pixel budget (ERR_LIMIT) [img_dims_check]

  • fn mem_check(bytes: u64, dec: &ImgDec) -> i32 bytes against dec's memory budget (0 = no limit) [img_mem_check]

  • fn surf_alloc(w: u32, h: u32, fmt: u8, dec: &ImgDec) -> Result<Surface, i32> dims_check, then a zeroed surface [img_surf_alloc]

  • fn get_row(sf: &Surface, y: usize, dst: &mut [u8], fmt: u8) row y of sf as w BGRA pixels in dst: fmt FMT_ARGB = straight, FMT_PARGB = premultiplied [img_get_row]

  • fn premultiply(sf: &mut Surface) FMT_ARGB -> FMT_PARGB in place (other formats are left alone) [img_premultiply]

  • fn unpremultiply(sf: &mut Surface) FMT_PARGB -> FMT_ARGB in place (other formats are left alone) [img_unpremultiply]

  • fn convert(sf: &mut Surface, fmt: u8) -> i32 convert sf in place to fmt (FMT_PARGB or FMT_ARGB) from FMT_XRGB (alpha 255), FMT_PARGB or FMT_ARGB -> 0 / ERR_UNSUPPORTED [img_convert]

  • fn surf_convert(src: &Surface, fmt: u8) -> Result<Surface, i32> a new w x h surface (FMT_PARGB or FMT_ARGB) holding src through get_row [img_surf_convert]

  • fn orient(sf: &mut Surface, o: u32) -> i32 transform a 32-bit surface for display by its EXIF orientation: 2 mirror, 3 rotate 180, 4 flip, 5 transpose, 6 rotate 90 clockwise, 7 transverse, 8 rotate 90 counter-clockwise (1 and invalid values: nothing); 5..8 swap width and height (a new pixel buffer; opacity and blend mode kept) -> 0 / ERR_NOMEM [img_orient]

  • fn all_opaque(sf: &Surface) -> bool every alpha byte of a 32-bit surface is 255 [img_all_opaque, png.s]

  • fn unpremul(p: u32) -> u32 premultiplied -> straight of one pixel [unpremul, png.s]

  • fn u16_to_u8(v: u32) -> u8 round(v / 257) for v = 0..65535 [img_u16_to_u8]

  • fn row_u16_to_u8(dst: &mut [u8], src: &[u8], be: bool) 16-bit samples (be: big endian) -> bytes round(v / 257); n = dst.len() (bounded by the source) [img_row_u16_to_u8]

  • fn half_to_f32(h: u32) -> u32 IEEE 754 half (low 16 bits of h) -> single precision bits (subnormals, infinities and NaN preserved) [img_half_to_f32]

  • fn row_half_to_u8(dst: &mut [u8], src: &[u16]) IEEE halves -> bytes round(clamp(v, 0, 1) * 255), NaN -> 0 [img_row_half_to_u8]

  • fn row_f32_to_u8(dst: &mut [u8], src: &[f32]) floats -> bytes round(clamp(v, 0, 1) * 255), NaN -> 0 [img_row_f32_to_u8]

  • fn row_f32_to_srgb8(dst: &mut [u8], src: &[f32]) linear-light floats -> sRGB-encoded bytes (clamp 0..1, NaN -> 0, 4096-entry table) [img_row_f32_to_srgb8]

  • fn srgb_lin(v: u8) -> f32 sRGB decode (IEC 61966-2-1) of v / 255 [g_img_srgb_lin]

  • struct JSeg a JPEG segment walker (JSEG): pos = next read offset (start: 2, after SOI); ptr / len = the payload of the current segment (offset into the data)

    • pos: usize
    • ptr: usize
    • len: usize

impl JSeg

  • fn new() -> JSeg

  • fn jpg_seg_next(data: &[u8], js: &mut JSeg) -> u32 the next JPEG marker segment from js.pos: skips bytes that are not 0xFF, fill bytes, standalone markers (TEM, RSTn); returns the marker (0xC0..0xFE) with js.ptr / js.len = the payload (after the 2 length bytes) and js.pos behind it; 0xD9 (EOI) and 0xD8 (SOI) come with len 0; 0 = end of data, a truncated segment or a length < 2 [jpg_seg_next]

  • static SRGB_LIN_BITS: [u32; 256] g_img_srgb_lin[v] = sRGB decode of v / 255 as f32 bits

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