Skip to main content

compress_jpeg

Function compress_jpeg 

Source
pub fn compress_jpeg(
    src: &NDArray,
    quality: u8,
) -> Result<NDArray, JpegCompressError>
Expand description

Compress an NDArray to JPEG.

Mirrors C compressJPEG (NDPluginCodec.cpp:109-266), which decides the JPEG geometry from the dimension count (:146-169) and the source pixel layout from the ColorMode attribute (:181-227):

  • 2-D: grayscale, [x, y].
  • 3-D RGB1 [3, x, y]: already pixel-interleaved, encoded as-is.
  • 3-D RGB2 [x, 3, y] and RGB3 [x, y, 3]: C walks the three colour planes (pRed/pGreen/pBlue, plane step sizeX*3 for RGB2 and sizeX for RGB3) and re-interleaves each scanline into an RGB row before encoding. The port reaches the same pixel order through convert_rgb_layout, the single owner of RGB layout conversion (also used by the JPEG/TIFF/Magick file writers), so the interleave rule is not re-implemented here.

Both 8-bit types are accepted, as in C (case NDInt8: case NDUInt8:, :135-143). Returns None for anything C rejects: a non-8-bit type, a dimension count other than 2 or 3, or a 3-D array whose ColorMode is not one of the three RGB layouts.