pub fn write_coefficients(
jpeg: &[u8],
coeffs: &JpegCoefficients,
) -> Result<Vec<u8>, DctError>Expand description
Re-encode a JPEG with modified DCT coefficients.
Takes the original JPEG bytes and a JpegCoefficients (typically
obtained from read_coefficients and then modified), and produces a new
JPEG byte stream with the updated coefficients re-encoded using the same
Huffman tables as the original.
The output is a valid JPEG. All non-entropy-coded segments (EXIF, ICC profile, quantization tables, etc.) are preserved verbatim.
§Safety note
The output is only as valid as the input JPEG’s Huffman tables permit.
If you set a coefficient to a value whose (run, category) symbol does not
exist in the original Huffman table, encoding will return
DctError::CorruptEntropy. Stick to modifying the LSB of coefficients
with |v| >= 2 (JSteg-style) to stay safely within the table.
§Errors
Returns DctError::Incompatible if coeffs has a different number of
components, a different block count, or mismatched component IDs compared
to the original JPEG.
Returns DctError for any parse or encoding failure.